使用libpng将PNG显示到图形缓冲区的正确方法

时间:2017-01-06 10:57:14

标签: c libpng

我正在尝试使用LibPNG c库将png图像加载到图形缓冲区中,如此

//m_pBuffer is the graphics buffer
//row_pointers is the data from png
//width is width of image
//height height of image
//y = 100
//x = 100
//ScreenWidth = 1024
//channel_num = 4  

int pDst = (y + height - 1) * ScreenWidth + x;
for (y = height - 1; y >= 0 ; y--)
{
    png_bytep row = row_pointers[y];
    u32 *pBuffer = m_pBuffer + pDst;
    for(x = 0; x < width; x++) {
      png_bytep px = &(row[x * channel_num]);
      *pBuffer++ = RGBA(px[3], px[2], px[1], px[0]);
    }
    pDst -= ScreenWidth;
} 

但我得到的是一个略微扭曲的图像。 which looks like this

然而这是最初的照片 which looks like this

如何摆脱淡黄色背景和白色边框?

这就是row_pointers的填充方式

    png_bytep row_pointers[height];

   int row = 0;
   for (row = 0; row < height; row++)
      row_pointers[row] = NULL;

   for (row = 0; row < height; row++)
      row_pointers[row] = (png_byte*)png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));

   int pass = 0;   
   int number_passes = 1;
   for (pass = 0; pass < number_passes; pass++)
   {
      for (int j = 0; j < height; j++)
      {
          png_read_rows(png_ptr, NULL, &row_pointers[j], 1);
      }
   }
   png_read_end(png_ptr, info_ptr);

0 个答案:

没有答案