2

我正在编写一个基于 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(),但我不知道为什么图像显示不同。有人可以帮我得到它吗?

4

3 回答 3

3

关于BITMAPINFO的文档对DIB 进行了说明:

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

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

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

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

于 2011-03-18T22:58:37.663 回答
2

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

于 2011-03-18T22:59:26.343 回答
0

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

于 2011-03-21T20:39:24.647 回答