Reputation: 125
I have created my own DCT calculation.
How do I use jpeg_write_coefficients
to write my 64 DCT values into JPEG file using jpeg_write_coefficients
(it needs jvirt_barray_ptr * coef_arrays
)?
How do I create this?
Upvotes: 4
Views: 2096
Reputation: 2740
you need coefficient array as input to write your files as variable type jvirt_barray_ptr
. you may fill it by reading it from an image and
jvirt_barray_ptr *coeffs_array = jpeg_read_coefficients(&cinfo);
or fill it by yourself. Then you have to write your dct input data like this. In the following function I used jpeg_decompress_struct
as input to use my first image header to the output image header
int write_jpeg_file_dct(std::string outname,jpeg_decompress_struct in_cinfo, jvirt_barray_ptr *coeffs_array ){
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE * infile;
if ((infile = fopen(outname.c_str(), "wb")) == NULL) {
fprintf(stderr, "can't open %s\n", outname.c_str());
return 0;
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, infile);
j_compress_ptr cinfo_ptr = &cinfo;
jpeg_copy_critical_parameters((j_decompress_ptr)&in_cinfo,cinfo_ptr);
jpeg_write_coefficients(cinfo_ptr, coeffs_array);
jpeg_finish_compress( &cinfo );
jpeg_destroy_compress( &cinfo );
fclose( infile );
return 1;
}
Upvotes: 0
Reputation: 1
You can follow jpeg_read_coefficients
to create structure of dct coefficient manager,such as
:dstInfo->mem->alloc_small,dstInfo->mem->request_virt_barray,dstInfo->mem->realize_virt_arrays
And use virt_barray_list
to get dst target to put new dct coefficient/quantyTableQ into it. Then you call jpeg_write_coefficients to generate JPEG file.
Upvotes: -1
Reputation: 21912
Look around for the request_virt_barray
function (in jmemmgr.c
).
Also have a look at this question. It's reading DCT coefficients as opposed to writing them, but it should give you an idea of how coefficient arrays are stored like. Aside from the coefficients, you also need to pass in the quantization table (through the j_compress_ptr cinfo
struct). This is because inside the libjpeg library, forward DCT and quantization is performed in one step. If you want to do the forward DCT by yourself, you have to do the quantization yourself too.
It's also worth reading through the libjpeg documentation. It's long, but actually quite readable, and will greatly improve your understanding of the library. The most helpful file is structure.txt
. It has sections on memory management and the encoder structure that are likely to help you.
Upvotes: 2