c++ - libjpeg:复制整个数据

标签 c++ c libjpeg

我正在尝试使用 libjpeg 将图像从一个文件复制到另一个文件。 我尝试使用 jpeg_read_scanlines/jpeg_write_scanlines 来复制图像数据,但据我所知这些函数计算 DCT/IDCT 加上去/量化。我不需要执行这些操作,事实上我也不想。复制数据后,我想对量化的 DCT 系数进行操作,因此无法使用 ImageMagick 或其他软件。 如果没有 IDCT/DCT 步骤,有没有更快的方法来做到这一点?

最佳答案

是的。您可以使用 jpeg_read_coefficients() ,它将为您读取量化的 DCT 系数。然后您可以使用 jpeg_write_coefficients() 将它们写回另一个图像。典型用法类似于:

struct jpeg_decompress_struct decomp;
struct jpeg_compress_struct comp;
jvirt_barray_ptr *coefficients;
/* Set your error handlers for decomp and comp here */
jpeg_create_decompress(&decomp);
/* Set your decompression source here */
jpeg_read_header(&decomp, 1);
coefficients = read_coefficients(&decomp);
jpeg_create_compress(&comp);
jpeg_copy_critical_parameters(&decomp, &comp);
comp.in_color_space = decomp.out_color_space; /* Not copied by previous call */
jpeg_write_coefficients(&comp, coefficients);
jpeg_finish_compress(&comp);
/* Destroy comp and decomp */

这些内容都在文件 libjpeg.txt(特别是真正原始数据部分)中进行了描述,该文件包含在 libjpeg-turbo 的源代码分发中。该部分还包括有关实际使用 DCT 系数并在再次写入它们之前对其进行操作的信息,但基本流程如下:

/* assume that variables are as above */
for(int ci = 0; ci < total_components_in_image; ci++) {
    jpeg_component_info *info = decomp.comp_info + ci;
    JBLOCKARRAY buffer = (*decomp.mem->access_virt_barray)
        ((j_common_ptr) &decomp, coefficients[ci], 0, info->height_in_blocks, 0);
}

现在,buffer 是一个三维数组,其中 buffer[y][x][c] 是第 x 个 block 中的第 c 个系数,并且第 y 个街区向下。 access_virt_barray 声明如下:

access_virt_barray(j_common_ptr cinfo, jvirt_barray_ptr ptr,
    JDIMENSION starting_row, JDIMENSION rows_to_read, boolean is_write_access);

关于c++ - libjpeg:复制整个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16734925/

相关文章:

c - 如何使用 C 程序从网络摄像头获取输入?

C宏生成printf格式字符串

c - 将 JPEG 图像从 RGB 重写为 HSL : how can i optimize this?

c++ - Cin对象返回值c++

c++ - typedef更改含义

c++ - 候选构造函数(隐式复制构造函数)不可行 : expects an l-value for 1st argument

C 警告 : incompatible pointer types passing

c++ - MFC中如何给组合框添加字符串

python - 如何安装libjpeg?

python - PIL 编码器 jpeg 不可用