123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- #include "jinclude.h"
- #include "jpeglib.h"
- #include "jerror.h"
- #ifndef HAVE_STDLIB_H
- extern void *malloc(size_t size);
- extern void free(void *ptr);
- #endif
- typedef struct {
- struct jpeg_destination_mgr pub;
- FILE *outfile;
- JOCTET *buffer;
- } my_destination_mgr;
- typedef my_destination_mgr *my_dest_ptr;
- #define OUTPUT_BUF_SIZE 4096
- #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
- typedef struct {
- struct jpeg_destination_mgr pub;
- unsigned char **outbuffer;
- unsigned long *outsize;
- unsigned char *newbuffer;
- JOCTET *buffer;
- size_t bufsize;
- } my_mem_destination_mgr;
- typedef my_mem_destination_mgr *my_mem_dest_ptr;
- #endif
- METHODDEF(void)
- init_destination(j_compress_ptr cinfo)
- {
- my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
-
- dest->buffer = (JOCTET *)
- (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
- OUTPUT_BUF_SIZE * sizeof(JOCTET));
- dest->pub.next_output_byte = dest->buffer;
- dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
- }
- #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
- METHODDEF(void)
- init_mem_destination(j_compress_ptr cinfo)
- {
-
- }
- #endif
- METHODDEF(boolean)
- empty_output_buffer(j_compress_ptr cinfo)
- {
- my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
- if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
- (size_t)OUTPUT_BUF_SIZE)
- ERREXIT(cinfo, JERR_FILE_WRITE);
- dest->pub.next_output_byte = dest->buffer;
- dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
- return TRUE;
- }
- #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
- METHODDEF(boolean)
- empty_mem_output_buffer(j_compress_ptr cinfo)
- {
- size_t nextsize;
- JOCTET *nextbuffer;
- my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
-
- nextsize = dest->bufsize * 2;
- nextbuffer = (JOCTET *)malloc(nextsize);
- if (nextbuffer == NULL)
- ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
- MEMCOPY(nextbuffer, dest->buffer, dest->bufsize);
- free(dest->newbuffer);
- dest->newbuffer = nextbuffer;
- dest->pub.next_output_byte = nextbuffer + dest->bufsize;
- dest->pub.free_in_buffer = dest->bufsize;
- dest->buffer = nextbuffer;
- dest->bufsize = nextsize;
- return TRUE;
- }
- #endif
- METHODDEF(void)
- term_destination(j_compress_ptr cinfo)
- {
- my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
- size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
-
- if (datacount > 0) {
- if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
- ERREXIT(cinfo, JERR_FILE_WRITE);
- }
- fflush(dest->outfile);
-
- if (ferror(dest->outfile))
- ERREXIT(cinfo, JERR_FILE_WRITE);
- }
- #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
- METHODDEF(void)
- term_mem_destination(j_compress_ptr cinfo)
- {
- my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
- *dest->outbuffer = dest->buffer;
- *dest->outsize = (unsigned long)(dest->bufsize - dest->pub.free_in_buffer);
- }
- #endif
- GLOBAL(void)
- jpeg_stdio_dest(j_compress_ptr cinfo, FILE *outfile)
- {
- my_dest_ptr dest;
-
- if (cinfo->dest == NULL) {
- cinfo->dest = (struct jpeg_destination_mgr *)
- (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
- sizeof(my_destination_mgr));
- } else if (cinfo->dest->init_destination != init_destination) {
-
- ERREXIT(cinfo, JERR_BUFFER_SIZE);
- }
- dest = (my_dest_ptr)cinfo->dest;
- dest->pub.init_destination = init_destination;
- dest->pub.empty_output_buffer = empty_output_buffer;
- dest->pub.term_destination = term_destination;
- dest->outfile = outfile;
- }
- #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
- GLOBAL(void)
- jpeg_mem_dest(j_compress_ptr cinfo, unsigned char **outbuffer,
- unsigned long *outsize)
- {
- my_mem_dest_ptr dest;
- if (outbuffer == NULL || outsize == NULL)
- ERREXIT(cinfo, JERR_BUFFER_SIZE);
-
- if (cinfo->dest == NULL) {
- cinfo->dest = (struct jpeg_destination_mgr *)
- (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
- sizeof(my_mem_destination_mgr));
- } else if (cinfo->dest->init_destination != init_mem_destination) {
-
- ERREXIT(cinfo, JERR_BUFFER_SIZE);
- }
- dest = (my_mem_dest_ptr)cinfo->dest;
- dest->pub.init_destination = init_mem_destination;
- dest->pub.empty_output_buffer = empty_mem_output_buffer;
- dest->pub.term_destination = term_mem_destination;
- dest->outbuffer = outbuffer;
- dest->outsize = outsize;
- dest->newbuffer = NULL;
- if (*outbuffer == NULL || *outsize == 0) {
-
- dest->newbuffer = *outbuffer = (unsigned char *)malloc(OUTPUT_BUF_SIZE);
- if (dest->newbuffer == NULL)
- ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
- *outsize = OUTPUT_BUF_SIZE;
- }
- dest->pub.next_output_byte = dest->buffer = *outbuffer;
- dest->pub.free_in_buffer = dest->bufsize = *outsize;
- }
- #endif
|