7

How do I read an image in C so that I can have direct control over its pixels (like we do in MATLAB)?

I tried the old way:

FILE *fp;
fp = fopen("C:\\a.tif","r");

that just gives me the ascii form of the image file (I think).

How can I get pixel level control over the image and do some basic operations like say, inverting the image?

Moeb
  • 10,527
  • 31
  • 84
  • 110
  • 1
    C is a bit lower level than you seem to think it is. That fopen is giving you raw binary, and it's up to you to either figure out how to interpret that binary as an image, or use a library as suggested below. – Breton Oct 20 '09 at 11:39
  • @Breton, he specifically asked how to decode these files, not the binary content. I feel that C is a better option when you want your program to be fast, else Python provides PIL which is good enough. – Xolve Oct 20 '09 at 19:31
  • 1
    @Breton - That `fopen()` is giving you no such raw binary, it's giving you text mode, which means it's likely to read some files incorrectly on Windows. – Chris Lutz Oct 21 '09 at 21:39
  • But on Linux and UNIX it's fine. – Artelius Oct 21 '09 at 23:15
  • @Xolve he did not, he asked how to manipulate the pixels, like he does in matlab. I've never used matlab, but I suspect it's high level enough to not force the user to decode raw binary data into an image. I'm merely pointing out that C is not that high level, and it does not understand images without a library, or without writing a program to decode the binary compressed information into pixel data. @Lutz, oh right I guess it needs a "b" flag or something? I was not aware that C made any distinction between text and binary. – Breton Oct 24 '09 at 22:02

5 Answers5

4

Have a look at libtiff. Nearly all image formats have some sort of header data and many are compressed to keep filesize reasonable. You can of course write C code to read the headers and perform decompression, but there's no need since someone else has already done it. Look here for some C image libraries.

Another question is what form you want the data in for manipulation - a common choice is 24-bit RGB (i.e. R, G, and B each vary from 0 to 255). In MATLAB you also see R, G, B as doubles varying from 0.0 to 1.0. Or you may want a different colour space (HSV, YUV, etc.).

Remember that integer operations are faster, but they can be more troublesome.

Community
  • 1
  • 1
Artelius
  • 48,337
  • 13
  • 89
  • 105
4

OpenCV is computer vision library, but can be used for "low level" tasks too. It supports BMP, DIB, JPEG, JPG, JPE, PNG, PBM, PGM, PPM, SR, RAS, TIFF, TIF.

Harriv
  • 6,029
  • 6
  • 44
  • 76
3

To manipulate jpeg files use libjpeg, the tiff files use libtiff, etc.

Another easy option is gd which allows manipulation of many image formats (PNG, JPEG, GIF, ...).

evuez
  • 3,257
  • 4
  • 29
  • 44
Xolve
  • 22,298
  • 21
  • 77
  • 125
1

There are some open-source libs to load images of different types and libtiff to load tiff images. If you just want to play around and the file format is not very important, then I would take a look on netpbm format (inparticular P5 and P6). It is pretty easy to write a loader and saver for it and of course a good exercise.

quinmars
  • 11,175
  • 8
  • 32
  • 41
1

MagickWand from ImageMagick is another good option

Lou Franco
  • 87,846
  • 14
  • 132
  • 192