我正在尝试读取 jpeg 图像中的每个像素。当我读取扫描线时,图像似乎已被压缩到原始图像大小的八分之一。扫描线的宽度正确,但扫描线的其余 7/8 部分未填充 (0, 0, 0)。
我不能简单地使用每个像素 8 次,因为大量信息丢失了。
我不使用任何解压参数并且对默认设置非常满意。我使用的 Libjpeg 版本是来自 https://conan.io 包中心的“libjpeg/9d”。
如何获得正确纵横比的扫描线?
FILE* file_p = openfile(fileaddress);
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr err; //the error handler
cinfo.err = jpeg_std_error( &err );
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, file_p);
int result = jpeg_read_header(&cinfo, TRUE);
bool startedfine = jpeg_start_decompress(&cinfo);
int row_stride = cinfo.output_width * cinfo.output_components;
image output = create_image(cinfo.output_width, cinfo.output_height);
JSAMPARRAY buffer = calloc(1, sizeof(JSAMPROW*));
buffer[0] = calloc(1, sizeof(JSAMPROW) * row_stride);
while (cinfo.output_scanline < cinfo.output_height) {
int current_y = cinfo.output_scanline;
jpeg_read_scanlines(&cinfo, buffer, 1);
JSAMPROW row = buffer[0];
for(int row_i = 0; row_i < row_stride; row_i += 3) {
int r = (int)row[row_i];
int g = (int)row[row_i + 1];
int b = (int)row[row_i + 2];
int actual_x = row_i / 3;
output.pixels_array_2d[actual_x][current_y].r = r;
output.pixels_array_2d[actual_x][current_y].g = b;
output.pixels_array_2d[actual_x][current_y].b = g;
}
}
free(buffer[0]);
free(buffer);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(file_p);
作为旁注,您可能会注意到我在复制像素时将蓝色值分配给绿色值。这是因为没有它,我的测试图像是错误的颜色,我不知道为什么。
问题是我需要调用
tjDecompress2
函数并给出正确的音高值作为参数。
tjDecompress2(
handle,
jpegBuffer,
jpegSize,
scaledBuffer,
scaledWidth,
0,
scaledHeight,
pixelFormat,
flags);
这里是关于此参数的库文档的链接。