All the time, image processing (compression algorithms, filters, etc.) was very much attracting attention. Alas, it turned out that the work is practically not connected either with image processing or with programming in general. Nevertheless, interest in my favorite work has not diminished, and therefore I want to bring to your attention the recently opened CImg library. CImg is a C ++ library that provides classes and image processing functions. These include elementary functions (loading, saving, viewing), and algorithms for resizing / rotating, applying effects, drawing objects (text, lines, surfaces, ellipses, ...), etc.
Library structure
The library consists of a single header file CImg.h , which includes all the classes and functions of the CImg. This is a distinctive feature of the library, which has some advantages:
no need to pre-compile the library, since the CImg-code is compiled (sorry for the tautology :-) during the compilation of the main program, including CImg.h ;
no complex dependencies: just include CImg.h in your project;
compilation occurs on the fly: only the functionality used in the program is included in the executable file. This allows you to create very compact applications;
class members and functions are inline, which results in better performance during program execution;
The CImg library has the following structure:
All library classes and functions are defined in the cimg_library namespace, which encapsulates all library functionality and avoids collisions that may occur when adding other header files to a project. Usually only use this namespace as the standard:
#include"CImg.h" using namespace cimg_library;
The namespace cimg_library :: cimg defines a set of low-level functions and variables used in the library;
The cimg_library :: CImg <T> class is the main library class whose instances represent an entity (image) up to 4-dimensional (ranging from a one-dimensional scalar to 3-dimensional pixel sets), with template pixel types;
The cimg_library :: CImgList <T> class represents lists of cimg_library :: CImg images. It can be used, for example, to store a sequence of images (frames, ...); The cimg_library :: CImgDisplay class displays images or sets of images in a graphical environment. We can safely say that the code of this class depends heavily on the system, but in fact it does not care about the programmer, since environment variables are set automatically by the CImg library; The cimg_library :: CImgException class (and its subclasses) is used by the library to handle exceptions when errors occur. Exceptions are handled using try {..} catch (CImgException) {...}. Subclasses allow you to accurately determine the type of error;
Knowledge of these four classes is enough to make full use of the functionality of the CImg library. ')
Hello, world!
Well, okay, it was said quite a lot. Let us consider a better visual example of how an elementary program works, written using CImg.
#include"CImg.h" using namespace cimg_library; int main() { CImg<unsigned char> img(640,400,1,3); img.fill(0); unsigned char purple[] = { 255,0,255 }; img.draw_text(100,100,"Hello World",purple); img.display("My first CImg code"); return 0; }
Let us consider in more detail each line of the program:
Let's connect the header file of the CImg library
#include"CImg.h"
We will use the cimg_library namespace to facilitate type declarations.
usingnamespace cimg_library;
Announce the main function of the program
intmain(){
Create an instance of the image - img , with the pixel type unsigned char , size 640 * 400 * 1 pixels (in this case, 1 indicates that the image will be flat, not three-dimensional). Each pixel has 3 channels - RED, GREEN and BLUE. This is indicated by the last parameter of the constructor.
CImg<unsignedchar> img(640,400,1,3);
Paint the image in black (“0” means black)
img.fill(0);
Let's declare the variable purple: it will be color
unsignedchar purple[] = { 255,0,255 };
Let's output the text “Hello World” in color purple from the dot (100,100) on the image
img.draw_text(100,100,"Hello World",purple);
Show the image in the graphics window with the title “My first CImg code”
img.display("My first CImg code");
Finish the program
return0;
As you can see, the CImg library is fairly easy to use, method names are intuitive. Although the above code could be written even more compactly:
#include"CImg.h" using namespace cimg_library; int main() { const unsigned char purple[] = { 255,0,255 }; CImg<unsigned char>(640,400,1,3,0).draw_text(100,100,"Hello World",purple).display("My first CImg code"); return 0; }
Screenshot (clickable): I hope that in the future we will examine some more examples, already more complicated! Project website: cimg.sourceforge.net Thank you for attention! PS
On Linux, compiled with the command: g++ -o hello hello.cpp -O2 -L/usr/X11R6/lib -lm -lpthread -lX11 (You can do without -O2)
Compiles long enough (Linux Gentoo, Pentium® Dual-Core CPU T4500 @ 2.30GHz): $ time g++ -o hello hello.cpp -O2 -L/usr/X11R6/lib -lm -lpthread -lX11 real 0m28.397s user 0m27.991s sys 0m0.265s 28 seconds (!) For Hello, World! isn't it too? Although without the "-O2" three times faster.
The size of the binary is 742K, which is generally comparable to the compile time!