2013-02-02 55 views
0

我想用libpng讀取PNG文件,我想使用過濾器png_set_rgb_to_gray_fixed將RGB值轉換爲灰度。原始圖像每通道8位,因此每像素3字節。我期望輸出爲每像素8位。但是,png_get_rowbytes告訴我行寬是3 *寬。我做錯了什麼?使用libpng過濾器

這裏是我的代碼(我刪除了錯誤檢查簡碼):

FILE *fp = fopen(filename,"rb"); 
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); 
png_infop info_ptr = png_create_info_struct(png_ptr);  
png_infop end_info = png_create_info_struct(png_ptr); 
png_init_io(png_ptr, fp); 
png_uint_32 width,height; 
int color_depth,color_type, interlace_type, compression_type, filter_method;  
png_read_info(png_ptr, info_ptr);  
png_get_IHDR(png_ptr, info_ptr, &width, &height, 
      &color_depth, &color_type, &interlace_type, 
      &compression_type, &filter_method); 
assert(color_type == PNG_COLOR_TYPE_RGB); 
png_set_rgb_to_gray_fixed(png_ptr, 3,-1,-1); 

int rowbytes = png_get_rowbytes(png_ptr, info_ptr); 
assert(rowbytes == width); // FAILS: rowbytes == 3*width 

回答

1

你需要調用png_read_update_info

png_read_update_info()結構更新指向info_ptr反映任何要求的轉換。例如,rowbytes將被更新以處理用png_read_update_info()擴展隔行圖像。

所以:

​​
+0

謝謝,這解決了! –