tags:

views:

361

answers:

6

hi all,

I need to read an image file in C/C++. It would be very great, if some one can post the code for me.

I work on gray scale images and the images are JPEG. I would like to read the images into a 2D array which will make my work easy.

Thanks in advance

+1  A: 

Try out the CImg library. The tutorial will help you get familiarized. Once you have a CImg object, the data() function will give you access to the 2D pixel buffer array.

mwc
+4  A: 

You could write your own by looking at the JPEG format.

That said, try a pre-existing library like CImg, or Boost's GIL. Or for strictly JPEG's, libjpeg. There is also the CxImage class on CodeProject.

Here's a big list.

GMan
Boost.GIL does not work and is not being maintained.
Tronic
+1  A: 

Check out the Magick++ API to ImageMagick.

R Samuel Klatchko
A: 

corona is nice. From the tutorial:

corona::Image* image = corona::OpenImage("img.jpg", corona::PF_R8G8B8A8);
if (!image) {
  // error!
}

int width  = image->getWidth();
int height = image->getHeight();
void* pixels = image->getPixels();

// we're guaranteed that the first eight bits of every pixel is red,
// the next eight bits is green, and so on...
typedef unsigned char byte;
byte* p = (byte*)pixels;
for (int i = 0; i < width * height; ++i) {
  byte red   = *p++;
  byte green = *p++;
  byte blue  = *p++;
  byte alpha = *p++;
}

pixels would be a one dimensional array, but you could easily convert a given x and y position to a position in a 1D array. Something like pos = (y * width) + x

Firas Assaad
A: 

Check out Intel Open CV library ...

Ashish
+1  A: 

Check this thread out: read and write image file.

Also, have a look at this other question at Stackoverflow.

Lazer