C:来自byte []问题的WinAPI CreateDIBitmap()

问题描述 投票:0回答:4

我一直在研究这个问题。 我正在尝试使用libjpeg为程序添加JPEG支持。 在大多数情况下,它工作得相当好,但对于某些JPEG,它们显示为左侧的图片。

(与original image比较。)

它可能并不明显,但背景显示交替的红绿蓝行。如果有人之前已经看过这种行为并且知道可能的原因,我将不胜感激。

我已经将行填充为四个字节的倍数,它只是稍微帮助了这个问题。

码:

  rowSize = cinfo.output_width * cinfo.num_components;
  /* Windows needs bitmaps to be defined on Four Byte Boundaries */
  winRowSize = (rowSize + 3) & -4;
  imgSize = (cinfo.output_height * winRowSize + 3) & -4;
  while(cinfo.output_scanline < cinfo.output_height){
        jpeg_read_scanlines(&cinfo, &row_pointer, 1);

        /* stagger read to get lines Bottom->Top (As BMP Requires) */
        location = (imgSize) - (cinfo.output_scanline * winRowSize);
        rowsRead++;

        for(i = 0; i < winRowSize; i++){
           rawImage[location++] = row_pointer[i];
        }
     }

     /* Convert BGR to RGB */
     if(cinfo.num_components == 3){
        for(i = 0; i < imgSize; i += 3){
           tmp = rawImage[i+2];
           rawImage[i+2] = rawImage[i];
           rawImage[i] = tmp;
        }
     }

     biSize = sizeof(BITMAPINFOHEADER);
     if(cinfo.num_components == 1){ /* Greyscale */
        biPallete = 32 * 256;
        biSize += biPallete;
     }

     bitInf = (BITMAPINFO *)malloc(biSize);

     bitInf->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
     bitInf->bmiHeader.biWidth = cinfo.output_width;
     bitInf->bmiHeader.biHeight = cinfo.output_height;
     bitInf->bmiHeader.biPlanes = 1;
     bitInf->bmiHeader.biBitCount = 8*cinfo.num_components;
     bitInf->bmiHeader.biCompression = BI_RGB;
     bitInf->bmiHeader.biSizeImage = 0;
     bitInf->bmiHeader.biXPelsPerMeter = 0;
     bitInf->bmiHeader.biYPelsPerMeter = 0;
     bitInf->bmiHeader.biClrUsed       = 0;
     bitInf->bmiHeader.biClrImportant  = 0;

     if(cinfo.num_components == 1){
        for(i = 0; i < 256; i++){
           bitInf->bmiColors[i].rgbBlue = i;
           bitInf->bmiColors[i].rgbGreen = i;
           bitInf->bmiColors[i].rgbRed = i;
           bitInf->bmiColors[i].rgbReserved = 0;
        }
     }

     /* Loads rawImage into an HBITMAP */
     /* retval = CreateDIBitmap(inDC, &bitInf->bmiHeader, CBM_INIT, rawImage, bitInf, DIB_RGB_COLORS); */
     retval = CreateCompatibleBitmap(inDC, cinfo.output_width, cinfo.output_height);
     errorCode = SetDIBits(inDC, retval, 0, cinfo.output_height, rawImage, bitInf, DIB_RGB_COLORS);

解决方案:我将RGB / BGR转换器更改为:

if(cinfo.num_components == 3){
   for(i = 0; i < cinfo.output_height; i++){
      location = (i * winRowSize);
      for(j = 0; j < rowSize; j += 3){
         tmp = rawImage[location+2];
         rawImage[location+2] = rawImage[location];
         rawImage[location] = tmp;
         location += 3;
      }
   }
}

它就像一个魅力。感谢roygbiv

c arrays winapi jpeg libjpeg
4个回答
1
投票

左图像最常见的原因之一是未正确对齐的缓冲区。

我相信Windows需要一个DWORD对齐的缓冲区。

我在上面的代码中看到的一个问题是你不想使用winRowSize复制实际的像素,你想使用一个变量(图像的宽度*每像素的字节数)。 winRowSize复制DWORD对齐的大小,这可能太大了(虽然有些图像可能会工作,因为默认情况下它们会落在DWORD对齐上。)

更改for循环:

        for(i = 0; i < (width of the image * bytes per pixel); i++){ 
           rawImage[location++] = row_pointer[i]; 
        }

(您可能还需要将rgb调整为bgr代码。)


0
投票

看起来你正在接受RGB的输入并将其视为RGBA(32位位图而不是24位)。


0
投票

也许你忘了填充每一行像素来占用整数个DWORD。


-1
投票

也许问题jpeg不是rgb,而是cmyk(甚至是灰度)。并非所有的jpeg都是rgb。

PS,(是的,我知道jpegs实际上不是rgb - 而是yuv,只是试图保持这个答案简单)

© www.soinside.com 2019 - 2024. All rights reserved.