2011-03-18 22 views
2

我在寫一個基於Win32的應用程序,它顯示來自數據庫的jpeg圖像。我選擇了libjpeg作爲解碼器,但大多數圖像顯示不正確。它可以通過增加或減少圖像的寬度來固定,但是,在修復之後顯示不正確之前已正確顯示的圖像。這裏的(不包括RGB到BGR轉換)我的代碼部分:libjpeg/CreateDIBSection問題

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(),但我不知道爲什麼在圖片顯示不同。有人可以幫助我得到它嗎?

回答

3

文檔約BITMAPINFO狀態這大約一個DIB:

[...]每個掃描線必須用零來結束LONG數據型邊界上填充。

這意味着row_stride必須是4個字節的倍數。下面是計算一個方法:

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

同樣,DDB行的大小必須是2

+0

我不敢相信我可以忽略這個重要的事實!非常感謝! – Joulukuusi 2011-03-19 00:53:52

2

對於Windows位圖的倍數,則掃描線需要對其進行填充到DWORD邊界。您的測試圖像可能有一個奇數的寬度。

+0

非常感謝! – Joulukuusi 2011-03-19 00:56:11

0

你不需要libjpeg! Jpeg原生Windows(Shell),像所有圖形格式一樣