1

I am looking for a header-only C++ library that allows me to read images in various (common) file formats. My usage will be conceptually no more complicated than this self-explaining pseudocode:

// read image in any Format
auto my_image( "oldimage.gif" );

// read and write pixels 
my_image( row, column ).red = 55;

// save image in any format
my_image.save( "newimage.tiff" );

Up to now, I have been using Magick++ for that purposes. Not only is that overblown for my very limited purposes, but getting it to link is a pain within a large project. I hope another library just accomplishes the job with little to no frills.

1 Answer 1

1

You could try CImg. It uses C++ class templates to represent images, with T meaning what each pixel would be stored as. It consists of only a single header CImg.h. Great features of it include how modifier functions return the same class, meaning you can "pipe" them together and the support of external libraries.

The formats it natively supports are ascii, cpp, dlm, bmp, jpeg, png, pnm, pnk, pfm, rgb minc2, nifti, cimg, inr, exr, pandore, raw, off (for 3d), yuv (store multiple images into a video), and tiff. TIFF support needs to be enabled through #define cimg_use_tiff. Other image formats could be saved with this library if ImageMagick or GraphicsMagick is installed.

Here is the reference for the namesake class. You can use fill for a lot of pixels or draw_point for a specific pixel.

// read image in any Format
CImg<unsigned char> my_image( "oldimage.gif"); //T=unsigned char, meaning R/G/B must be whole number

// read and write a pixel
my_image( column, row, 0 ) = 255;
my_image( column, row, 1 ) = 0;
my_image( column, row, 2 ) = 0;
// 0 for red, 1 for green, 2 for blue.

// just write a pixel
const unsigned char red[3] = { 255, 0, 0 };
my_image.draw_point( column, row, red );

// save image in supported formats
my_image.save( "newimage.tiff" );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.