【问题标题】:libjpeg/CreateDIBSection problemlibjpeg/CreateDIBSection 问题
【发布时间】:2011-03-18 22:06:31
【问题描述】:

我正在编写一个基于 Win32 的应用程序,它显示来自数据库的 jpeg 图像。我选择 libjpeg 作为解码器,但大多数图像显示不正确。可以通过将图像的宽度增加或减少 1 来修复它,但是,在此修复后显示错误之前已正确显示的图像。这是我的部分代码(不包括 RGB 到 BGR 的转换):

int JpegToRaw(BYTE *input, int insize, BYTE *output, int &width, int &height)
{
    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_decompress(&cinfo);

    jpeg_mem_src(&cinfo, input, insize);
    jpeg_read_header(&cinfo, TRUE);

    jpeg_start_decompress(&cinfo);

    //--cinfo.output_width; or ++cinfo.output_width;

    int row_stride = cinfo.output_width * 3;
    int outsize = row_stride * cinfo.output_height;
    output = (BYTE *)malloc(outsize * sizeof(BYTE));
    BYTE *pos = output;

    while (cinfo.output_scanline < cinfo.output_height)
    {
        jpeg_read_scanlines(&cinfo, &pos, 1);
        pos += row_stride;
    }

    width = cinfo.output_width;
    height = cinfo.output_height;

    jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);
    return outsize;
}

HBITMAP RawToBitmap(BYTE *input, int size, int width, int height)
{
    BITMAPINFO bi;
    bi.bmiHeader.biSize        = sizeof(bi24BitInfo.bmiHeader);
    bi.bmiHeader.biWidth       = width;
    bi.bmiHeader.biHeight      = -height;
    bi.bmiHeader.biPlanes      = 1;
    bi.bmiHeader.biBitCount    = 24;
    bi.bmiHeader.biCompression = BI_RGB;

    HBITMAP hBitmap = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, NULL, NULL, 0);
    SetBitmapBits(hBitmap, size, input);
    return hBitmap;
}

我确定我将有效的 jpeg 数组传递给 JpegToRaw(),但我不知道为什么图像显示不同。有人可以帮我弄吗?

【问题讨论】:

    标签: c image winapi jpeg libjpeg


    【解决方案1】:

    有关 BITMAPINFO 的文档说明了有关 DIB 的内容:

    […] 每条扫描线必须用零填充以在 LONG 数据类型边界上结束。

    这意味着row_stride 必须是 4 个字节的倍数。这是一种计算方法:

    int row_stride = (cinfo.output_width * 3 + 3) / 4 * 4;
    

    同样,DDB 行的大小必须是 2 的倍数。

    【讨论】:

    • 我不敢相信我会忽略这个重要的事实!非常感谢!
    【解决方案2】:

    对于 Windows 位图,扫描线需要填充到 DWORD 边界。您的测试图像可能有一个奇怪的宽度。

    【讨论】:

      【解决方案3】:

      你不需要 libjpeg ! Jpeg 在 Windows (Shell) 中是原生的,与所有图形格式一样

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-09
        • 2015-12-08
        • 1970-01-01
        • 1970-01-01
        • 2011-11-22
        • 2012-07-14
        • 2011-12-17
        • 1970-01-01
        相关资源
        最近更新 更多