1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
/*
   compilation command:

   g++ -std=c++11 -Wall -o gdk-pixbuf-demo `pkg-config --cflags --libs gdk-pixbuf-2.0` gdk-pixbuf-demo.cpp
*/

#include <gdk-pixbuf/gdk-pixbuf.h>

int main()
{
    unsigned int width  = 96;
    unsigned int height = 96;

    GdkPixbuf* pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
                                       width, height);
    int n_channels     = gdk_pixbuf_get_n_channels(pixbuf);
    int rowstride      = gdk_pixbuf_get_rowstride(pixbuf);
    guchar* pixels     = gdk_pixbuf_get_pixels(pixbuf);

    for (unsigned int i=0; i<height; ++i) {
        for (unsigned int j = 0; j < width; ++j) {
            guchar* pp = pixels + i * rowstride + j * n_channels;
            if ((i / (height/8) + j / (width/8)) % 2 == 0) {
                pp[0] = 25/* red */
                pp[1] = 7/* green */
                pp[2] = 26/* blue */
            } else {
                pp[0] = 248/* red */
                pp[1] = 201/* green */
                pp[2] = 95/* blue */
            }
        }
    }
    gdk_pixbuf_save(pixbuf, "output.jpg""jpeg",
                    nullptr"quality""100"nullptr);
}