gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:07:05 25 4
gpt4 key购买 nike

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

最佳答案

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

就四舍五入到 4 的倍数而言,该公式是正确的,但使用被乘数 cinfo.num_components 时不正确;根据USING THE IJG JPEG LIBRARY :

You will need output_width * output_components JSAMPLEs per scanline in your output buffer…

The output arrays are required to be output_width * output_components JSAMPLEs wide.

所以,是

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

关于c - 如何在 4 字节边界对齐扫描线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34218472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com