c - 位图被镜像和反转加载

标签 c winapi gtk gdkpixbuf pixbuf

我从剪贴板获取CF_DIB,然后调用:

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

从像素数据创建 GdkPixbuf。然而,图像似乎上下翻转并镜像反转(我认为它是 BGR)。

如何正确创建 pixbuf?

最佳答案

我用我编写的这个函数解决了我的问题:

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);
}

不过它看起来肯定会更好。

关于c - 位图被镜像和反转加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56727077/

相关文章:

c - 从 .txt 文件获取整数并在 c 中对它们执行操作

winapi -\??\和\\?\路径之间有区别吗?

winapi - 如何将 Win32 C + MASM "solution"从 Visual Studio 2015 升级到 VS 2017/2019

c++ - 如何将 WebKit 嵌入到我的 C/C++/Win32 应用程序中?

使用 GTK 按钮创建网格

eclipse - 如何启用 eclipse 内部浏览器 (Ubuntu 12.10)?

python - 强制 matplotlib 图的背景透明

android - 在 Android 上使用 ffmpeg 2.1.1 时出现 undefined reference

c - 下面的代码有什么作用?

c - 重复图像检测算法?