How to use "CImg" and its main functions

I tried to figure out how to use CImg's drawing functions, but the documentation is not very clear to me. I just want to draw a pixel, but I don't understand how draw_point works. Can anyone provide some examples of draw_point and how to declare the image? Also, is there a more efficient alternative for C ++? I just want a simple image library for C ++. I want to manipulate a blank pixel of an image pixel by pixel. Is there a better alternative?


source to share

1 answer

I modified the CImg tutorial to show how to use draw_point, the code is below:

#include "CImg.h"

using namespace cimg_library;

int main() 

    int size_x = 640;
    int size_y = 480;
    int size_z = 1;
    int numberOfColorChannels = 3; // R G B
    unsigned char initialValue = 0;

    CImg<unsigned char> image(size_x, size_y, size_z, numberOfColorChannels, initialValue);

    CImgDisplay display(image, "Click a point");

    while (!display.is_closed())
        if (display.button() && display.mouse_y() >= 0 && display.mouse_x() >= 0)
            const int y = display.mouse_y();
            const int x = display.mouse_x();

            unsigned char randomColor[3];
            randomColor[0] = rand() % 256;
            randomColor[1] = rand() % 256;
            randomColor[2] = rand() % 256;

            image.draw_point(x, y, randomColor);
    return 0;


The draw_point method has three overloads that can be confusing to use. I used the following:

template<typename tc>
CImg<T>& draw_point(const int x0, const int y0,
                    const tc *const color, const float opacity=1)


See this one for details .

As for alternatives, if you only want to change the pixel of the data pixel by pixel, perhaps you can treat the image as raw data using libpng and libjpeg for I / O.

Anyway, I would recommend that you read the additional documentation about CImg. I have used it in some of my projects and it is very convenient for me.



All Articles