123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- #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
- void jpeg_mem_dest_tj(j_compress_ptr cinfo, unsigned char **outbuffer,
- unsigned long *outsize, boolean alloc);
- #define OUTPUT_BUF_SIZE 4096
- typedef struct {
- struct jpeg_destination_mgr pub;
- unsigned char **outbuffer;
- unsigned long *outsize;
- unsigned char *newbuffer;
- JOCTET *buffer;
- size_t bufsize;
- boolean alloc;
- } my_mem_destination_mgr;
- typedef my_mem_destination_mgr *my_mem_dest_ptr;
- METHODDEF(void)
- init_mem_destination(j_compress_ptr cinfo)
- {
-
- }
- 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;
- if (!dest->alloc) ERREXIT(cinfo, JERR_BUFFER_SIZE);
-
- 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;
- }
- METHODDEF(void)
- term_mem_destination(j_compress_ptr cinfo)
- {
- my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
- if (dest->alloc) *dest->outbuffer = dest->buffer;
- *dest->outsize = (unsigned long)(dest->bufsize - dest->pub.free_in_buffer);
- }
- GLOBAL(void)
- jpeg_mem_dest_tj(j_compress_ptr cinfo, unsigned char **outbuffer,
- unsigned long *outsize, boolean alloc)
- {
- boolean reused = FALSE;
- 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));
- dest = (my_mem_dest_ptr)cinfo->dest;
- dest->newbuffer = NULL;
- dest->buffer = NULL;
- } 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;
- if (dest->buffer == *outbuffer && *outbuffer != NULL && alloc)
- reused = TRUE;
- dest->outbuffer = outbuffer;
- dest->outsize = outsize;
- dest->alloc = alloc;
- if (*outbuffer == NULL || *outsize == 0) {
- if (alloc) {
-
- dest->newbuffer = *outbuffer = (unsigned char *)malloc(OUTPUT_BUF_SIZE);
- if (dest->newbuffer == NULL)
- ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
- *outsize = OUTPUT_BUF_SIZE;
- } else
- ERREXIT(cinfo, JERR_BUFFER_SIZE);
- }
- dest->pub.next_output_byte = dest->buffer = *outbuffer;
- if (!reused)
- dest->bufsize = *outsize;
- dest->pub.free_in_buffer = dest->bufsize;
- }
|