Convert image into useable byte array in C? - c++

Does anyone know how to open an image, specifically a jpg, to a byte array in C or C++? Any form of help is appreciated.
Thanks!

The ImageMagick library can do this too, although often it provides enough image manipulation functions that you can do many things without needing to convert the image to a byte array and handle it yourself.

You could try the DevIL Image Library I've only used it in relation to OpenGL related things, but it also functions as just a plain image loading library.

Check out the source code for wxImage in the wxWidgets GUI Framework. You will most likely be interested in the *nix distribution.
Another alternative is the GNU Jpeg library.

Here is how I would do it using GDIPlus Bitmap.LockBits method defined in the header GdiPlusBitmap.h:
Gdiplus::BitmapData bitmapData;
Gdiplus::Rect rect(0, 0, bitmap.GetWidth(), bitmap.GetHeight());
//get the bitmap data
if(Gdiplus::Ok == bitmap.LockBits(
&rect, //A rectangle structure that specifies the portion of the Bitmap to lock.
Gdiplus::ImageLockModeRead | Gdiplus::ImageLockModeWrite, //ImageLockMode values that specifies the access level (read/write) for the Bitmap.
bitmap.GetPixelFormat(),// PixelFormat values that specifies the data format of the Bitmap.
&bitmapData //BitmapData that will contain the information about the lock operation.
))
{
//get the lenght of the bitmap data in bytes
int len = bitmapData.Height * std::abs(bitmapData.Stride);
BYTE* buffer = new BYTE[len];
memcpy(bitmapData.Scan0, buffer, len);//copy it to an array of BYTEs
//...
//cleanup
pBitmapImageRot.UnlockBits(&bitmapData);
delete []buffer;
}

I have my students use netpbm to represent images because it comes with a handy C library, but you can also put images into text form, create them by hand, and so on. The nice thing here is that you can convert all sorts of images, not just JPEGs, into PBM format, using command-line tools the Unix way. The djpeg tool is available a number of places including the JPEG Club. Students with relatively little experience can write some fairly sophisticated programs using this format.

OpenCV can also do this.
http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/index.html
search for: "Accessing image elements"

Related

Converting a Byte Array into a Bitmap Image

I have a byte array named pBuffe of type unsigned char* pBuffer that was fed an 'image' (a bunch of bytes) by a high powered camera after it captured a picture. How do I go about converting this byte array into a viewable bitmap image file?
I think you can solve this easily by using the well-known and popular EasyBMP library. It creates the proper file header and everything you need for it. As a bonus, it supports multiple platforms.

Read image and access bytes in boost::gil

Is it possible to:
read an image given by just a filename (not knowing the image format) to a 2d matrix rgb uncompressed form (e.g. read an JPG to a 2d array)
access the bytes of that image, copy them, change them... (e.g. inverse the colors, I need a pointer to the image bytes, setters/getters won't do )
save those bytes to any given image format (e.g. save the inversed image to PNG)
Is it possible with boost::gil ? Maybe there is a different library more appropriate for such a task?
Sample code would be highly appreciated.
Yes, you can do all that in boost::gil.
What you should know though, is that boost::gil is only a universal interface and doesn't handle reading/writing images all by itself. You still need to use a second library, e.g. libpng..
Yes, yes and yes.
There are functions that enable you to read and write JPEG, TIFF and PNG images: see here.
For the second bullet, it is what just GIL is meant to do. You can manipulate images using its facilities (click here).

How can I process an image?

I'm building a program to convert an image file (whatever file type would be easiest) to G-Code for use on a rep-rap with a pen plotter attachment.
I'm wondering if i wanted to process the image pixel by pixel and check things like pixel color, how could I do this with C++?
I would really like to know how I can process a bitmap image, pixel by pixel, to check the color of the pixel.
The best way is to use a library, like for example Magick++.
When you load an image, you can access it's pixels data with Blob
You will probably want to use an existing library that has been tested.
But for fun/practice/etc, this would be a good exercise and wouldn't be impossible to do. The Bitmap Format is (relatively) simple compared with other image formats. The Wikipedia page has some tons of info, including some C++ code. It looks like once you've gotten past the header information, you get to a pixel array that shouldn't be difficult to parse.
Good luck.
Most image formats consist of a header and the actual raw image data. A bimpap image is no different. If you don't want to use one of the existing libraries, or if you are not allowed to, you should read about bitmap format :
http://en.wikipedia.org/wiki/BMP_file_format
Once you understand this you could create appropriate structs/classes to store the information you want from the header such as x,y size, bpp etc. And also have a pointer to the raw image data. You could then simpy iterate through every pixel and do whatever you want with it :)
Once you decipher the image file, I suggest you place the pixels into a matrix, for the first pass. (Future revisions can use other methods to access the pixels).
You can apply transformations to the pixels by using matrix multiplication. You can also access the pixels individually by using array indexing.
Search the web and SO for "introduction to graphics c++".

c++ read image pixels

I want a c++ Code to read every pixel from an image file. and i want to save the pixels like:
r[]
g[]
b[]
does somebody know how to do this?
The answer depends on the format of the image file. Is it a format which contains raw RGB data (such as uncompressed TGA)? Is it a YUV image? Is it a compressed image such as JPEG or PNG?
There are already plenty of C++ libraries out there which can read a wide variety of image file formats, and then provide the pixel-level access you require. Take a look at Adobe's GIL, or CImg for example.
There are many freely available libraries for reading different image file formats. Since you're using C++ you might want to look at Adobe's Generic Image Library (GIL) or even OpenCV.
This will sort you out, very easy to use and 'low level' image library:
http://easybmp.sourceforge.net/
Two libraries that I've used that jump to mind are:
ImageMagick
libGD
These libraries can handle a wide variety of image formats, depending on what you need.

How to get into image manipulation programming?

How can I do simple thing like manipulate image programmatically ? ( with C++ I guess .. )
jpgs/png/gif .....
check out BOOST , it has a simple Image Processing Library called GIL. It also has extensions to import common formats.
http://www.boost.org/doc/libs/1_39_0/libs/gil/doc/index.html
Using .NET you have two options:
GDI+ from System.Drawing namespace (Bitmap class)
WPF engine wich can do a lot of things
If you want low level processing you can use unsafe code and pointers.
A Bitmap or Image is just a big array of bytes.
You need to learn:
what is a stride (extra padding bytes after each row of pixels)
how to compute the next row or a specific pixel location using width, height, stride
the image formats RGB, ARGB, white&black
basic image processing functions (luminosity, midtone, contrast, color detection, edge detection, matrix convulsion)
3D vectorial representation of a RGB color
Depending on how fancy you want to get, you may want to look at OpenCV. It's a computer vision library that has functions ranging from reading and writing images to image processing to advanced things like object detection.
Magick++ is a C++ API for the excellent ImageMagick library.
An advantage of ImageMagick is that it can be used from the command-line and a bunch of popular scripting and compiled languages too, and some of those might be more accessible to you than C++.