views:

184

answers:

6

I want to iterate over each pixel color in a jpg format image,

which library should I refer to to do this so that the code can be as short as possible?

+2  A: 

jpeglib is quite a handy library to play with jpegs.

bhups
+5  A: 

I can think of either ImageMagick or CImg. Here is a CImg tutorial for you. They abstract away a lot of the decompression details and just give you a grid to work with.

If you go with CImg, you only need to use the data call. You can probably do something like:

CImg<unsigned char> src("image.jpg");
int width = src.width();
int height = src.height();
unsigned char* ptr = src.data(10,10); // get pointer to pixel @ 10,10
unsigned char pixel = *ptr;
SB
CImg is a good library. Seconded.
Computer Guru
How to get the width and height ?
I changed up the example to show you how to retrieve the height/width
SB
ImageMagick is a command line tool. Don't think it is suitable if you wish to build a standalone application. However I do no familiar with CImg.
ttchong
ttchong - ImageMagick also provides a set of APIs that you can link against. Check out Magick++. I've done it myself.
SB
@SB - Just know it from you. I will try out.
ttchong
@SB, I got a "Debug Error!" trying to run the programe, can you take a look what's wrong? http://stackoverflow.com/questions/3270489/why-i-got-a-run-time-debug-error-with-this-cimg-app-built-with-cmake
+1  A: 

For access to pixel level information. libjpeg & JAI Image I/O Tools should be sufficient. JAI provide advance image processing framework more than this. All options allow you to read/write pixels to an image file.

libjpeg In C/C++. It is available for Linux and Window. I have used it in Linux before and it works well. However, if you would like to process other image type, you may need to get other package and learn another API interface.

Java Advanced Imaging(JAI) Image I/O Tools Remember to download the platform specific java package because there is specific optimization for different system. It covers not only JPG but other image format too with exact same API interface.

Java Advanced Imaging(JAI) If you plan to do more advance level of processing such as using image operator and filter, you can use this. This is a package provide higher level functionality than JAI Image I/O Tools.

EDIT: to remove the 1, 2 & 3 as suggested

ttchong
answers aren't displayed in any permanent order, so referring to other answers as #1, #2, #3 is confusing. It's helpful to either summarize the answer (here that could be the framework name) or name the answerer in order to identify other answers. Linking the other answers is even better.
Ben Voigt
@Ben Voigt: noted and thanks for sharing this good practice.
ttchong
A: 

I would use Allegro Game Library http://alleg.sourceforge.net/latestdocs/en/alleg010.html it's simple / open source / free / multiplatform, and you can iterate pixel by pixel if you want

Hernán Eche
+2  A: 

Qt has a QImage class:

QImage i("input.jpg");
int x, y;
for (y = 0; &lt; i.height(); ++y) {
   for (x = 0; x &lt; i.width(); ++x) {
      doSomethingWith(i.pixel(x, y));
   }
}
akira
Yes, Qt is great
Hernán Eche
A: 

Try FreeImage, it seems pretty versatile and many projects use it.

sauparna