views:

867

answers:

5

How to open and read the pixels of an image in c++? Read them in the form of X, Y and to know the color.

+4  A: 

Use a library like DevIL (http://openil.sourceforge.net/). DevIL will load the image data into an array and you can access the raw pixel data using a function like ilGetPixels() or so.

DevIL also has OpenGL support.

banister
A: 

BMPs are incredibly simply. Uncompressed BMPs consist of a header, some information about the BMP, the color palette (if applicable) and then the bitmap data, pixel by pixel. Writing your own bitmap parser is a fun exercise although there is a lot of extra work handling all of the features (8-bit, RLE compression, etc).

Your best bet is to use a library. Image Magick has a C library that will allow you to open just about any image format and access the pixels. SDL_image is another library that is very easy to use and SDL can easily be used with OpenGL.

What image format you should use will depend upon the application. JPGs have pretty good compression, but the compression is LOSSY, meaning that you lose detail. If the image has text, or has large areas of solid colors or edges (like a comic) this is bad, you will get noticeable artifacting. For photos, JPGs are generally fine. PNGs are a nice alternative, they are compressed but the compression is LOSSLESS. JPGs will generally be smaller than PNGs, both will be smaller than BMPs.

Niki Yoshiuchi
+2  A: 

Either you use an existing library or you write your own code. Generally the first approach is better since an image file format is often more complex than it seems on the surface and you may end up with upside down images or incorrect color component order if you are not careful.

Depending on your requirements you may also need to take capabilities of the format in consideration. If support for high dynamic range is interesting, OpenEXR is a great choice, if it's not it's probably just inconvenient since it does not have as big support among other programs as for example PNG.

Here are two libraries I often turn to when needing to read and write images: libpng, OpenExr

Laserallan
We use libpng for image generation, it's really easy to use and pure C++ code so no need to deal with a crappy interface.
Matthieu M.
@Matthieu M.: libpng is certainly not written in "pure C++". It's written in C89, as it states on the page linked to by Laserallan.
unwind
+1  A: 

If you are going to be working with images you should look into the OpenCV library, it has pretty much everything you need to work with images.

OpenCV 2.0 came out a couple of months ago and its very friendly with C++.

rem7
OpenCV is overkill for just reading images.
kigurai
Yes, it is a big library, I'm just putting it as an option if he wants to do other stuff other than reading/writing it would be good to get familiar with it.
rem7
+2  A: 

Reading the color of a pixel from an image file using the Magick++ library

#include <Magick++.h>
#include <iostream>

using namespace Magick;
using namespace std;

int main(int argc, char **argv) {
 try {
  InitializeMagick(*argv);
  Image img("C:/test.bmp");
  ColorRGB rgb(img.pixelColor(0, 0));  // ie. pixel at pos x=0, y=0
  cout << "red: " << rgb.red();
  cout << ", green: " << rgb.green();
  cout << ", blue: " << rgb.blue() << endl;
 }
  catch ( Magick::Exception & error) {
  cerr << "Caught Magick++ exception: " << error.what() << endl;
 }
 return 0;
}
Alexandre Jasmin