Question

I am trying to copy an image from one file to another one using libjpeg. I tried to use jpeg_read_scanlines/jpeg_write_scanlines to copy image data but as far as I know these functions compute DCT/IDCT plus de/quantization. I do not need to perform these operations, in fact I do not want to. After data copying I want to manipulate on quantized DCT coefficients, so using ImageMagick or some other software is not an option. Is there any faster way to do that, without IDCT/DCT step?

Was it helpful?

Solution

Yes. You can use jpeg_read_coefficients() which will read quantized DCT coefficients for you. You can then use jpeg_write_coefficients() to write them back into another image. Typical usage would be something like:

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 */

This stuff is all described in the file libjpeg.txt (specifically the section Really raw data) which is included in the source distribution of libjpeg-turbo. That section also includes information on actually using the DCT coefficients and manipulating them before writing them again, but the basic flow of that goes:

/* 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);
}

So now, buffer is a three dimensional array where buffer[y][x][c] is the cth coefficient in the xth block across and the yth block down. access_virt_barray is declared something like:

access_virt_barray(j_common_ptr cinfo, jvirt_barray_ptr ptr,
    JDIMENSION starting_row, JDIMENSION rows_to_read, boolean is_write_access);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top