libjpeg & data destination manager
Hi, I'm just trying to get libjpeg to output its compressed file (ie: the jpeg) to a memory buffer, instead of an output file. (ie: do not call jpeg_stdio_dest, instead set the destination to a buffer) According to the documentation you need to write a "data destination manager", but I have no idea how to do this, has anyone written one before? I would appreciate any code snippets or advice in getting this going. Thanks.
Following the old axiom "The source code is the documentation",
snippets:
Of course you're going to want to modify this code to use a resizable buffer..
snippets:
typedef struct { struct jpeg_destination_mgr pub; /* public fields */ int size; JOCTET * buffer; /* start of buffer */} buf_destination_mgr;typedef buf_destination_mgr * buf_dest_ptr;METHODDEF(void)init_destination (j_compress_ptr cinfo){ buf_dest_ptr dest = (buf_dest_ptr) cinfo->dest; dest->size = 65536*256; /* Allocate the output buffer --- it will be released when done with image */ dest->buffer = (JOCTET *) malloc(dest->size * sizeof(JOCTET)); dest->pub.next_output_byte = dest->buffer; dest->pub.free_in_buffer = dest->size;}METHODDEF(boolean)empty_output_buffer (j_compress_ptr cinfo){ buf_dest_ptr dest = (buf_dest_ptr) cinfo->dest; printf("TOO BIG!\n"); exit (0); return TRUE;}METHODDEF(void)term_destination (j_compress_ptr cinfo){ buf_dest_ptr dest = (buf_dest_ptr) cinfo->dest; size_t datacount = dest->size - dest->pub.free_in_buffer; dest->size=datacount;}//in main or wherever: if (cinfo.dest == NULL) { // first time for this JPEG object? cinfo.dest = (struct jpeg_destination_mgr *) (*cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_PERMANENT, SIZEOF(buf_destination_mgr)); } dest = (buf_dest_ptr) cinfo.dest; dest->pub.init_destination = init_destination; dest->pub.empty_output_buffer = empty_output_buffer; dest->pub.term_destination = term_destination;//and finally, output to whever (Eg: a file) fwrite(dest->buffer,dest->size,1,outfile);
Of course you're going to want to modify this code to use a resizable buffer..
Intresting. Is that a solution to the problem, or is there more to be done still?
Quote:
Original post by aboeing
According to the documentation you need to write a "data destination manager", but I have no idea how to do this, has anyone written one before?
The libjpeg documentation is a joke. I did what you did, just went to the source for the stdio manager and modified it to use my own stream class (which has memory and file derived implementations).
Quote:
Intresting. Is that a solution to the problem, or is there more to be done still?
Its a solution in terms of that it works, not in terms of that you would ever want to actually use the code.. ;) :) But thanks for checking in.
(ie: no theres not more to be done, because the rest of the modifications are my-application-specific..)
Quote:
The libjpeg documentation is a joke. I did what you did, just went to the source for the stdio manager and modified it to use my own stream class (which has memory and file derived implementations).
Yeah its not too great, the biggest problem is theres a lot of it, and not much of it is usefull. But I think that it was made before the days of doc++ etc, so I guess they can be excused. :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement