 <p>TFM:</p> <pre><code>CAIRO_FORMAT_RGB24 each pixel is a 32-bit quantity, with the upper 8 bits unused </code></pre> <p>TFM:</p> <pre><code>stride = cairo_format_stride_for_width (format, width); data = malloc (stride * height); </code></pre> <p>Hence, the correct index calculation is</p> <pre><code>data[y * stride + x * 4 + 0] = blue; data[y * stride + x * 4 + 1] = green; data[y * stride + x * 4 + 2] = red; /* yes, in this order */ </code></pre> <p>Also, masks are taken from the image and shifts are hard-coded, which makes absolutely no sense. Calculate the shifts from the masks.</p>