bitmap is loaded mirrored and inverted

255 Views Asked by At

I obtain a CF_DIB from a clipboard and then call:

GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(info->bmiColors, GDK_COLORSPACE_RGB, TRUE, 8, info->bmiHeader.biWidth, info->bmiHeader.biHeight, rowstride, NULL, NULL);

to create a GdkPixbuf out of the pixel data. However the image appears to be flipped upside down and mirrored inverted (I think as it is BGR).

How do I create the pixbuf properly?

1

There are 1 best solutions below

3
Edenia On

I solved my problem with this function I wrote:

GdkPixbuf* soft_drm_dib_to_pixbuf (BITMAPINFO* info)
{
    gint    rowstride   = 4 * ((info->bmiHeader.biBitCount * info->bmiHeader.biWidth + 31) / 32);
    gsize   size        = rowstride * info->bmiHeader.biHeight;
    gchar*  copy        = (gchar *)info->bmiColors;
    gchar*  newp        = g_malloc0(size);
    gint    x           = 0;
    gint    y           = 0;

    for(y = 0; y < info->bmiHeader.biHeight; y++)
    {
        for(x = 0; x < info->bmiHeader.biWidth; x++)
        {
            gint    col     = x;
            gint    row     = info->bmiHeader.biHeight - y - 1;
            gint    index   = (row * info->bmiHeader.biWidth + col) * 4;
            gint    index2  = (row * info->bmiHeader.biWidth + (info->bmiHeader.biWidth - col)) * 4;

            newp[(size - index2) + 0] = copy[index + 2];
            newp[(size - index2) + 1] = copy[index + 1];
            newp[(size - index2) + 2] = copy[index + 0];
        }
    }

    return gdk_pixbuf_new_from_data(newp, GDK_COLORSPACE_RGB, TRUE, 8, info->bmiHeader.biWidth, info->bmiHeader.biHeight, rowstride, (GdkPixbufDestroyNotify)g_free, NULL);
}

it definitely can look much better though.