Moeb
Moeb

Reputation: 10871

How to manipulate an image at pixel level in C?

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?

Upvotes: 8

Views: 15016

Answers (5)

Xolve
Xolve

Reputation: 23220

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, ...).

Upvotes: 3

Harriv
Harriv

Reputation: 6137

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.

Upvotes: 4

Lou Franco
Lou Franco

Reputation: 89242

MagickWand from ImageMagick is another good option

Upvotes: 1

quinmars
quinmars

Reputation: 11583

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.

Upvotes: 1

Artelius
Artelius

Reputation: 49134

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.

Upvotes: 4

Related Questions