2015-12-11 66 views
0

我正在使用libjpeg來解壓縮JPEG圖像並將其寫入BMP文件。假設圖像寬度設置爲2550像素,每像素24位(3字節),結果行寬度將不是4的倍數。如何在4字節邊界處對齊每行?如何在4字節邊界對齊掃描線

struct jpeg_decompress_struct cinfo; 
unsigned int bytesPerRow = cinfo.output_width * cinfo.num_components; 
unsigned int colColor; 
FILE *bmpFile = NULL; 

while (cinfo.output_scanline < cinfo.image_height) { 

    JSAMPROW row_pointer[1]; 
    row_pointer[0] = raw_image 
      + cinfo.output_scanline * bytesPerRow; 
    jpeg_read_scanlines(&cinfo, row_pointer, 1); 
    for (colColor = 0; colColor < cinfo.image_width; colColor++) { 

     /* BMP scanlines should be aligned at 4-byte boundary */ 

    } 

    /* write each row to bmp file */ 
    fwrite(row_pointer[0], 1, bytesPerRow, bmpFile); 
} 
+0

複製它通過行開始在對齊的地址行。 –

+0

您應該將像素解包到rgba或rgbx中,以便每個像素對齊,假設性能而非存儲更值得關注。 – user3528438

+0

bytesPerRow = 4 *((cinfo.output_width * cinfo.num_components + 3)/ 4); –

回答

0

bytesPerRow = 4 *((* cinfo.output_width + cinfo.num_components 3)/ 4); - Hans Passant

該公式是正確的,就四捨五入爲倍數而言,但在使用被乘數cinfo.num_components時不正確;根據USING THE IJG JPEG LIBRARY

您需要output_width每個掃描線 * output_components JSAMPLEs在輸出緩衝器...

輸出數組必須是output_width * output_components JSAMPLEs寬。

所以,這是

unsigned int bytesPerRow = (cinfo.output_width * cinfo.output_components + 3)/4 * 4;