2011-03-18 3 views
2

데이터베이스에서 jpeg 이미지를 표시하는 Win32 기반 응용 프로그램을 작성하고 있습니다. 나는 디코더로 libjpeg를 고르지 만 대부분의 이미지는 잘못 표시됩니다. 이미지의 너비를 1 씩 늘리거나 줄여서 고칠 수 있지만이 수정 이후 제대로 표시되지 않는 이미지는 올바르게 표시됩니다. 여기 (BGR 변환 RGB 제외) 내 코드의 일부 :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; 
} 

내가 JpegToRaw()에 유효한 JPEG 배열을 통과 확신 해요,하지만 난 이미지가 다른 표시 할 이유를 모르겠어요. 누군가 그것을 얻을 수 있도록 도와 줄 수 있습니까? DIB를 약

답변

3

문서에 대한이 상태 BITMAPINFO :

가 [...] 각 주사선은 LONG 데이터 타입 경계에서 종료 제로 패딩해야한다.

즉, row_stride은 4 바이트의 배수 여야합니다. 다음은이를 계산하는 방법 중 하나입니다 :

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

은 마찬가지로, DDB 행의 크기는 Windows 비트 맵 2.

+0

나는이 중요한 사실을 간과 할 수 있다고 믿을 수 없습니다! 정말 고맙습니다! – Joulukuusi

2

의 배수 여야의 주사선는 DWORD 경계 밖으로 패딩해야합니다. 테스트 이미지의 너비가 너비 일 수 있습니다.

+0

정말 고마워요! – Joulukuusi

0

libjpeg가 필요하지 않습니다. JPEG는 모든 그래픽 형식과 마찬가지로 Windows (셸)에서 기본입니다.