Read and write images from files

To run the Histogram project, students may consider to use the CImg library. The library consists in a single header file to be included and compiled linking the pthread and the X11 library

 g++/icc -pthread -lX11 file.cpp -o ... 

The library has been tested working on the ninja.itc.unipi.it PHI KNL machine, and a copy of the header file may be found on the machine under /home/marcod/CImg/CImg.h:

 icc -I /home/marcod/CImg -pthread -lX11 ... 

In order to read images from disk and to save images to dist, the

cimg_library::CImg

constructor and the

save

method may be used. Pixels are accessed through 3 dim coordinates (x, y, color channel). The following code reads a JPEG image from disk, changes some pixels and writes it back with a different name:

#include "CImg.h"
 
using namespace cimg_library;
 
int main(int argc, char * argv[]) {
 
  // read a jpg image
  CImg<float> imgin("imageA.jpg");
  // do some computation on the pixels (R,G,B values)
  for(int i=0; i<100; i++)
    for(int j=0; j<10; j++)
      imgin(i,i+j,0) = imgin(i,i+j,1) = imgin(i, i+j, 2) = 0;
  // write image back
  imgin.save("imageB.jpg");
  return(0);
}

The library obviously hosts a number of different image processing methods and function calls, including histograms, but you have to implement the histogram on your own and the library has to be considered only to read and write images. The documentation is available at the CImg doc web site.