| OLD | NEW |
| 1 /* | 1 /* |
| 2 * jdatadst.c | 2 * jdatadst.c |
| 3 * | 3 * |
| 4 * Copyright (C) 1994-1996, Thomas G. Lane. | 4 * Copyright (C) 1994-1996, Thomas G. Lane. |
| 5 * Modified 2009 by Guido Vollbeding. |
| 5 * This file is part of the Independent JPEG Group's software. | 6 * This file is part of the Independent JPEG Group's software. |
| 6 * For conditions of distribution and use, see the accompanying README file. | 7 * For conditions of distribution and use, see the accompanying README file. |
| 7 * | 8 * |
| 8 * This file contains compression data destination routines for the case of | 9 * This file contains compression data destination routines for the case of |
| 9 * emitting JPEG data to a file (or any stdio stream). While these routines | 10 * emitting JPEG data to memory or to a file (or any stdio stream). |
| 10 * are sufficient for most applications, some will want to use a different | 11 * While these routines are sufficient for most applications, |
| 11 * destination manager. | 12 * some will want to use a different destination manager. |
| 12 * IMPORTANT: we assume that fwrite() will correctly transcribe an array of | 13 * IMPORTANT: we assume that fwrite() will correctly transcribe an array of |
| 13 * JOCTETs into 8-bit-wide elements on external storage. If char is wider | 14 * JOCTETs into 8-bit-wide elements on external storage. If char is wider |
| 14 * than 8 bits on your machine, you may need to do some tweaking. | 15 * than 8 bits on your machine, you may need to do some tweaking. |
| 15 */ | 16 */ |
| 16 | 17 |
| 17 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */ | 18 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */ |
| 18 #include "jinclude.h" | 19 #include "jinclude.h" |
| 19 #include "jpeglib.h" | 20 #include "jpeglib.h" |
| 20 #include "jerror.h" | 21 #include "jerror.h" |
| 21 | 22 |
| 23 #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */ |
| 24 extern void * malloc JPP((size_t size)); |
| 25 extern void free JPP((void *ptr)); |
| 26 #endif |
| 27 |
| 22 | 28 |
| 23 /* Expanded data destination object for stdio output */ | 29 /* Expanded data destination object for stdio output */ |
| 24 | 30 |
| 25 typedef struct { | 31 typedef struct { |
| 26 struct jpeg_destination_mgr pub; /* public fields */ | 32 struct jpeg_destination_mgr pub; /* public fields */ |
| 27 | 33 |
| 28 FILE * outfile; /* target stream */ | 34 FILE * outfile; /* target stream */ |
| 29 JOCTET * buffer; /* start of buffer */ | 35 JOCTET * buffer; /* start of buffer */ |
| 30 } my_destination_mgr; | 36 } my_destination_mgr; |
| 31 | 37 |
| 32 typedef my_destination_mgr * my_dest_ptr; | 38 typedef my_destination_mgr * my_dest_ptr; |
| 33 | 39 |
| 34 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */ | 40 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */ |
| 35 | 41 |
| 36 | 42 |
| 43 #if JPEG_LIB_VERSION >= 80 |
| 44 /* Expanded data destination object for memory output */ |
| 45 |
| 46 typedef struct { |
| 47 struct jpeg_destination_mgr pub; /* public fields */ |
| 48 |
| 49 unsigned char ** outbuffer; /* target buffer */ |
| 50 unsigned long * outsize; |
| 51 unsigned char * newbuffer; /* newly allocated buffer */ |
| 52 JOCTET * buffer; /* start of buffer */ |
| 53 size_t bufsize; |
| 54 } my_mem_destination_mgr; |
| 55 |
| 56 typedef my_mem_destination_mgr * my_mem_dest_ptr; |
| 57 #endif |
| 58 |
| 59 |
| 37 /* | 60 /* |
| 38 * Initialize destination --- called by jpeg_start_compress | 61 * Initialize destination --- called by jpeg_start_compress |
| 39 * before any data is actually written. | 62 * before any data is actually written. |
| 40 */ | 63 */ |
| 41 | 64 |
| 42 METHODDEF(void) | 65 METHODDEF(void) |
| 43 init_destination (j_compress_ptr cinfo) | 66 init_destination (j_compress_ptr cinfo) |
| 44 { | 67 { |
| 45 my_dest_ptr dest = (my_dest_ptr) cinfo->dest; | 68 my_dest_ptr dest = (my_dest_ptr) cinfo->dest; |
| 46 | 69 |
| 47 /* Allocate the output buffer --- it will be released when done with image */ | 70 /* Allocate the output buffer --- it will be released when done with image */ |
| 48 dest->buffer = (JOCTET *) | 71 dest->buffer = (JOCTET *) |
| 49 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | 72 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 50 OUTPUT_BUF_SIZE * SIZEOF(JOCTET)); | 73 OUTPUT_BUF_SIZE * SIZEOF(JOCTET)); |
| 51 | 74 |
| 52 dest->pub.next_output_byte = dest->buffer; | 75 dest->pub.next_output_byte = dest->buffer; |
| 53 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; | 76 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; |
| 54 } | 77 } |
| 55 | 78 |
| 79 #if JPEG_LIB_VERSION >= 80 |
| 80 METHODDEF(void) |
| 81 init_mem_destination (j_compress_ptr cinfo) |
| 82 { |
| 83 /* no work necessary here */ |
| 84 } |
| 85 #endif |
| 86 |
| 56 | 87 |
| 57 /* | 88 /* |
| 58 * Empty the output buffer --- called whenever buffer fills up. | 89 * Empty the output buffer --- called whenever buffer fills up. |
| 59 * | 90 * |
| 60 * In typical applications, this should write the entire output buffer | 91 * In typical applications, this should write the entire output buffer |
| 61 * (ignoring the current state of next_output_byte & free_in_buffer), | 92 * (ignoring the current state of next_output_byte & free_in_buffer), |
| 62 * reset the pointer & count to the start of the buffer, and return TRUE | 93 * reset the pointer & count to the start of the buffer, and return TRUE |
| 63 * indicating that the buffer has been dumped. | 94 * indicating that the buffer has been dumped. |
| 64 * | 95 * |
| 65 * In applications that need to be able to suspend compression due to output | 96 * In applications that need to be able to suspend compression due to output |
| (...skipping 19 matching lines...) Expand all Loading... |
| 85 if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) != | 116 if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) != |
| 86 (size_t) OUTPUT_BUF_SIZE) | 117 (size_t) OUTPUT_BUF_SIZE) |
| 87 ERREXIT(cinfo, JERR_FILE_WRITE); | 118 ERREXIT(cinfo, JERR_FILE_WRITE); |
| 88 | 119 |
| 89 dest->pub.next_output_byte = dest->buffer; | 120 dest->pub.next_output_byte = dest->buffer; |
| 90 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; | 121 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; |
| 91 | 122 |
| 92 return TRUE; | 123 return TRUE; |
| 93 } | 124 } |
| 94 | 125 |
| 126 #if JPEG_LIB_VERSION >= 80 |
| 127 METHODDEF(boolean) |
| 128 empty_mem_output_buffer (j_compress_ptr cinfo) |
| 129 { |
| 130 size_t nextsize; |
| 131 JOCTET * nextbuffer; |
| 132 my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest; |
| 133 |
| 134 /* Try to allocate new buffer with double size */ |
| 135 nextsize = dest->bufsize * 2; |
| 136 nextbuffer = malloc(nextsize); |
| 137 |
| 138 if (nextbuffer == NULL) |
| 139 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); |
| 140 |
| 141 MEMCOPY(nextbuffer, dest->buffer, dest->bufsize); |
| 142 |
| 143 if (dest->newbuffer != NULL) |
| 144 free(dest->newbuffer); |
| 145 |
| 146 dest->newbuffer = nextbuffer; |
| 147 |
| 148 dest->pub.next_output_byte = nextbuffer + dest->bufsize; |
| 149 dest->pub.free_in_buffer = dest->bufsize; |
| 150 |
| 151 dest->buffer = nextbuffer; |
| 152 dest->bufsize = nextsize; |
| 153 |
| 154 return TRUE; |
| 155 } |
| 156 #endif |
| 157 |
| 95 | 158 |
| 96 /* | 159 /* |
| 97 * Terminate destination --- called by jpeg_finish_compress | 160 * Terminate destination --- called by jpeg_finish_compress |
| 98 * after all data has been written. Usually needs to flush buffer. | 161 * after all data has been written. Usually needs to flush buffer. |
| 99 * | 162 * |
| 100 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding | 163 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding |
| 101 * application must deal with any cleanup that should happen even | 164 * application must deal with any cleanup that should happen even |
| 102 * for error exit. | 165 * for error exit. |
| 103 */ | 166 */ |
| 104 | 167 |
| 105 METHODDEF(void) | 168 METHODDEF(void) |
| 106 term_destination (j_compress_ptr cinfo) | 169 term_destination (j_compress_ptr cinfo) |
| 107 { | 170 { |
| 108 my_dest_ptr dest = (my_dest_ptr) cinfo->dest; | 171 my_dest_ptr dest = (my_dest_ptr) cinfo->dest; |
| 109 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer; | 172 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer; |
| 110 | 173 |
| 111 /* Write any data remaining in the buffer */ | 174 /* Write any data remaining in the buffer */ |
| 112 if (datacount > 0) { | 175 if (datacount > 0) { |
| 113 if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount) | 176 if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount) |
| 114 ERREXIT(cinfo, JERR_FILE_WRITE); | 177 ERREXIT(cinfo, JERR_FILE_WRITE); |
| 115 } | 178 } |
| 116 fflush(dest->outfile); | 179 fflush(dest->outfile); |
| 117 /* Make sure we wrote the output file OK */ | 180 /* Make sure we wrote the output file OK */ |
| 118 if (ferror(dest->outfile)) | 181 if (ferror(dest->outfile)) |
| 119 ERREXIT(cinfo, JERR_FILE_WRITE); | 182 ERREXIT(cinfo, JERR_FILE_WRITE); |
| 120 } | 183 } |
| 121 | 184 |
| 185 #if JPEG_LIB_VERSION >= 80 |
| 186 METHODDEF(void) |
| 187 term_mem_destination (j_compress_ptr cinfo) |
| 188 { |
| 189 my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest; |
| 190 |
| 191 *dest->outbuffer = dest->buffer; |
| 192 *dest->outsize = dest->bufsize - dest->pub.free_in_buffer; |
| 193 } |
| 194 #endif |
| 195 |
| 122 | 196 |
| 123 /* | 197 /* |
| 124 * Prepare for output to a stdio stream. | 198 * Prepare for output to a stdio stream. |
| 125 * The caller must have already opened the stream, and is responsible | 199 * The caller must have already opened the stream, and is responsible |
| 126 * for closing it after finishing compression. | 200 * for closing it after finishing compression. |
| 127 */ | 201 */ |
| 128 | 202 |
| 129 GLOBAL(void) | 203 GLOBAL(void) |
| 130 jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile) | 204 jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile) |
| 131 { | 205 { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 142 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, | 216 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, |
| 143 SIZEOF(my_destination_mgr)); | 217 SIZEOF(my_destination_mgr)); |
| 144 } | 218 } |
| 145 | 219 |
| 146 dest = (my_dest_ptr) cinfo->dest; | 220 dest = (my_dest_ptr) cinfo->dest; |
| 147 dest->pub.init_destination = init_destination; | 221 dest->pub.init_destination = init_destination; |
| 148 dest->pub.empty_output_buffer = empty_output_buffer; | 222 dest->pub.empty_output_buffer = empty_output_buffer; |
| 149 dest->pub.term_destination = term_destination; | 223 dest->pub.term_destination = term_destination; |
| 150 dest->outfile = outfile; | 224 dest->outfile = outfile; |
| 151 } | 225 } |
| 226 |
| 227 |
| 228 #if JPEG_LIB_VERSION >= 80 |
| 229 /* |
| 230 * Prepare for output to a memory buffer. |
| 231 * The caller may supply an own initial buffer with appropriate size. |
| 232 * Otherwise, or when the actual data output exceeds the given size, |
| 233 * the library adapts the buffer size as necessary. |
| 234 * The standard library functions malloc/free are used for allocating |
| 235 * larger memory, so the buffer is available to the application after |
| 236 * finishing compression, and then the application is responsible for |
| 237 * freeing the requested memory. |
| 238 */ |
| 239 |
| 240 GLOBAL(void) |
| 241 jpeg_mem_dest (j_compress_ptr cinfo, |
| 242 unsigned char ** outbuffer, unsigned long * outsize) |
| 243 { |
| 244 my_mem_dest_ptr dest; |
| 245 |
| 246 if (outbuffer == NULL || outsize == NULL) /* sanity check */ |
| 247 ERREXIT(cinfo, JERR_BUFFER_SIZE); |
| 248 |
| 249 /* The destination object is made permanent so that multiple JPEG images |
| 250 * can be written to the same buffer without re-executing jpeg_mem_dest. |
| 251 */ |
| 252 if (cinfo->dest == NULL) { /* first time for this JPEG object? */ |
| 253 cinfo->dest = (struct jpeg_destination_mgr *) |
| 254 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, |
| 255 SIZEOF(my_mem_destination_mgr)); |
| 256 } |
| 257 |
| 258 dest = (my_mem_dest_ptr) cinfo->dest; |
| 259 dest->pub.init_destination = init_mem_destination; |
| 260 dest->pub.empty_output_buffer = empty_mem_output_buffer; |
| 261 dest->pub.term_destination = term_mem_destination; |
| 262 dest->outbuffer = outbuffer; |
| 263 dest->outsize = outsize; |
| 264 dest->newbuffer = NULL; |
| 265 |
| 266 if (*outbuffer == NULL || *outsize == 0) { |
| 267 /* Allocate initial buffer */ |
| 268 dest->newbuffer = *outbuffer = malloc(OUTPUT_BUF_SIZE); |
| 269 if (dest->newbuffer == NULL) |
| 270 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); |
| 271 *outsize = OUTPUT_BUF_SIZE; |
| 272 } |
| 273 |
| 274 dest->pub.next_output_byte = dest->buffer = *outbuffer; |
| 275 dest->pub.free_in_buffer = dest->bufsize = *outsize; |
| 276 } |
| 277 #endif |
| OLD | NEW |