首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >libjpeg/CreateDIBSection问题

libjpeg/CreateDIBSection问题
EN

Stack Overflow用户
提问于 2011-03-19 06:06:31
回答 3查看 1.1K关注 0票数 2

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

代码语言:javascript
复制
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;
}

我确信我向JpegToRaw()传递了有效的jpeg数组,但我不知道为什么图像显示不同。有人能帮我拿一下吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-03-19 06:58:38

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

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

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

代码语言:javascript
复制
int row_stride = (cinfo.output_width * 3 + 3) / 4 * 4;

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

票数 3
EN

Stack Overflow用户

发布于 2011-03-19 06:59:26

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

票数 2
EN

Stack Overflow用户

发布于 2011-03-22 04:39:25

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5358307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档