CImg: A Powerful C++ Library for Image Processing

Jai Sachith Paul


CImg is a simple C++ toolkit intended to simplify the efforts of developers trying to implement new image-processing algorithms from scratch. A set of useful classes and functions that helps users to load/save, process and display various types of images is provided under a single header file CImg.h.

Computer scientists or student community working on computer vision and image processing will find this tool extremely useful. Even if you are a programmer, occasionally coming across some level of image processing, this tool will be of great help as there is no standard C++ library for this purpose yet. The package is distributed under CeCILL licence.

Fig. 1: Using CImg for computation of Hough transform to detect lines in 2D images

Motivation behind CImg
People who use image processing come from various scientific disciplines; we may find a doctor trying to have a better understanding of a patient’s health condition with the diagnostic image of the patient’s heart or a civil engineer trying to get an idea of a telemetric image of a dam to find out how strong it is. There could be a mathematician, biologist or computer programmer making use of image processing. A doctor might have a working knowledge of programming but we cannot expect him to know every minute detail of C++. Also, we cannot expect all users to deal with the same kind of images for their image-processing requirements.


So, the design of a general-purpose image-processing tool should cater to the needs of people working on different kinds of images and having different programming capabilities. These designs should follow some basic rules that ensure simplicity, genericity, usefulness, extensibility, portability and freedom of use of the library. CImg library was developed keeping in mind all these design considerations.

The framework
A handful of functions are included in order to write complex algorithms using minimal code. Let us take a look at some of these:

Supported file formats. CImg supports a large number of input and output file formats. These include joint photographic experts group (JPEG), 16-bit portable network graphics (PNG), 3D object file format, tagged image file format (TIFF) and more.

Image drawing. In order to facilitate drawing on images, there are many methods such as lines, polygons, ellipses, texts and vector fields.

Usual image-processing operators. These include filters, interpolators, fast Fourier transforms (FFTs), histograms, colour-base conversions and non-linear blur/sharpening, among others.

Arithmetic operators. Usual arithmetic operators like cosine, square-root and inverse tangent are available.

Expression evaluator. Synthetic images from mathematical formulae can easily be generated with the help of a numerical evaluator of mathematical expressions in CImg. There are many functions to generate 3D vector objects from dense image data.

3D object viewer. The framework is composed of a 3D object viewer. For 3D rendering, CImg does not rely on external libraries.


69Z_box3Entire library under one header
If you are a professional C++ programmer, perhaps you would be surprised to notice that the entire library is written under a single header file. A bulky code might be time-consuming, but arranging these under different header files is not the best solution, as you might require all of these.

Of course, you can get the library precompiled. Yet again, this is not a good option because of the generic nature of the library.

Library is based on template data types. As in the case of a standard template library (STL), it is not possible to precompile library classes and functions of all possible data types.

CImg library is based on template data types, where users have the freedom to opt for the type of image that is represented by their respective codes. There will be a lot of possible combinations of classes, functions, arguments and data types, and the size of the object binary file generated to cover all possible cases will be huge. Compiling on the fly with users’ codes, in turn, introduces a lot of flexibility for the users while coding.

Interdependent classes. There are only four classes used in the library. Out of these, CImg and CImgList representing an image and a collection of image are the most important. Both these classes are interdependent. Even if we are defining these under different headers, we have to include both. Therefore in practice we do not gain anything in defining these under different header files.

Simplicity guaranteed. When the entire code is put under a single header, at first sight it may look like a total mess. But after thorough observation, we would realise that these are well-organised and structured.

From the user’s perspective, installation is as simple as putting CImg header in the source directory.

For the developer, maintenance of the code is pretty easy. If the developer is in search of a piece of code, he or she can easily spot it in the library.

Fig. 2: Image restoration and inpainting using Tschumperlé-Deriche algorithm
Fig. 2: Image restoration and inpainting using Tschumperlé-Deriche algorithm

Users’ reviews
Reviews on websites like indicate that the users are quite happy with the tool. The generic nature of the tool to suit various applications, and the simplicity and flexibility it offers, attracts many users.


Please enter your comment!
Please enter your name here