如何在4字节边界处对齐扫描线

时间:2015-12-11 07:46:09

标签: c image bitmap jpeg libjpeg

我正在使用libjpeg解压缩我的JPEG图像并将其写入BMP文件。假设图像宽度设置为每像素24位(3字节)2550像素,则生成的行宽度不会是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);
}

1 个答案:

答案 0 :(得分:0)

  

bytesPerRow = 4 *((cinfo.output_width * cinfo.num_components + 3)/   4); - 汉斯帕斯特

对于四舍五入到4的倍数,公式是正确的,但在使用被乘数cinfo.num_components时不正确;根据{{​​3}}:

  

每个扫描线需要output_width * output_components JSAMPLE   在你的输出缓冲区......

     

输出数组必须是output_width * output_components   JSAMPLE广泛。

所以,它是

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