OLD | NEW |
1 /* | 1 /* |
2 * jdatadst.c | 2 * jdatadst.c |
3 * | 3 * |
4 * This file was part of the Independent JPEG Group's software: | 4 * This file was part of the Independent JPEG Group's software: |
5 * Copyright (C) 1994-1996, Thomas G. Lane. | 5 * Copyright (C) 1994-1996, Thomas G. Lane. |
6 * Modified 2009-2012 by Guido Vollbeding. | 6 * Modified 2009-2012 by Guido Vollbeding. |
7 * libjpeg-turbo Modifications: | 7 * libjpeg-turbo Modifications: |
8 * Copyright (C) 2013, D. R. Commander. | 8 * Copyright (C) 2013, D. R. Commander. |
9 * For conditions of distribution and use, see the accompanying README file. | 9 * For conditions of distribution and use, see the accompanying README.ijg |
| 10 * file. |
10 * | 11 * |
11 * This file contains compression data destination routines for the case of | 12 * This file contains compression data destination routines for the case of |
12 * emitting JPEG data to memory or to a file (or any stdio stream). | 13 * emitting JPEG data to memory or to a file (or any stdio stream). |
13 * While these routines are sufficient for most applications, | 14 * While these routines are sufficient for most applications, |
14 * some will want to use a different destination manager. | 15 * some will want to use a different destination manager. |
15 * IMPORTANT: we assume that fwrite() will correctly transcribe an array of | 16 * IMPORTANT: we assume that fwrite() will correctly transcribe an array of |
16 * JOCTETs into 8-bit-wide elements on external storage. If char is wider | 17 * JOCTETs into 8-bit-wide elements on external storage. If char is wider |
17 * than 8 bits on your machine, you may need to do some tweaking. | 18 * than 8 bits on your machine, you may need to do some tweaking. |
18 */ | 19 */ |
19 | 20 |
20 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */ | 21 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */ |
21 #include "jinclude.h" | 22 #include "jinclude.h" |
22 #include "jpeglib.h" | 23 #include "jpeglib.h" |
23 #include "jerror.h" | 24 #include "jerror.h" |
24 | 25 |
25 #ifndef HAVE_STDLIB_H» » /* <stdlib.h> should declare malloc(),free() */ | 26 #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */ |
26 extern void * malloc JPP((size_t size)); | 27 extern void *malloc (size_t size); |
27 extern void free JPP((void *ptr)); | 28 extern void free (void *ptr); |
28 #endif | 29 #endif |
29 | 30 |
30 | 31 |
31 /* Expanded data destination object for stdio output */ | 32 /* Expanded data destination object for stdio output */ |
32 | 33 |
33 typedef struct { | 34 typedef struct { |
34 struct jpeg_destination_mgr pub; /* public fields */ | 35 struct jpeg_destination_mgr pub; /* public fields */ |
35 | 36 |
36 FILE * outfile;» » /* target stream */ | 37 FILE *outfile; /* target stream */ |
37 JOCTET * buffer;» » /* start of buffer */ | 38 JOCTET *buffer; /* start of buffer */ |
38 } my_destination_mgr; | 39 } my_destination_mgr; |
39 | 40 |
40 typedef my_destination_mgr * my_dest_ptr; | 41 typedef my_destination_mgr *my_dest_ptr; |
41 | 42 |
42 #define OUTPUT_BUF_SIZE 4096» /* choose an efficiently fwrite'able size */ | 43 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */ |
43 | 44 |
44 | 45 |
45 #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) | 46 #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
46 /* Expanded data destination object for memory output */ | 47 /* Expanded data destination object for memory output */ |
47 | 48 |
48 typedef struct { | 49 typedef struct { |
49 struct jpeg_destination_mgr pub; /* public fields */ | 50 struct jpeg_destination_mgr pub; /* public fields */ |
50 | 51 |
51 unsigned char ** outbuffer;» /* target buffer */ | 52 unsigned char **outbuffer; /* target buffer */ |
52 unsigned long * outsize; | 53 unsigned long *outsize; |
53 unsigned char * newbuffer;» /* newly allocated buffer */ | 54 unsigned char *newbuffer; /* newly allocated buffer */ |
54 JOCTET * buffer;» » /* start of buffer */ | 55 JOCTET *buffer; /* start of buffer */ |
55 size_t bufsize; | 56 size_t bufsize; |
56 } my_mem_destination_mgr; | 57 } my_mem_destination_mgr; |
57 | 58 |
58 typedef my_mem_destination_mgr * my_mem_dest_ptr; | 59 typedef my_mem_destination_mgr *my_mem_dest_ptr; |
59 #endif | 60 #endif |
60 | 61 |
61 | 62 |
62 /* | 63 /* |
63 * Initialize destination --- called by jpeg_start_compress | 64 * Initialize destination --- called by jpeg_start_compress |
64 * before any data is actually written. | 65 * before any data is actually written. |
65 */ | 66 */ |
66 | 67 |
67 METHODDEF(void) | 68 METHODDEF(void) |
68 init_destination (j_compress_ptr cinfo) | 69 init_destination (j_compress_ptr cinfo) |
69 { | 70 { |
70 my_dest_ptr dest = (my_dest_ptr) cinfo->dest; | 71 my_dest_ptr dest = (my_dest_ptr) cinfo->dest; |
71 | 72 |
72 /* Allocate the output buffer --- it will be released when done with image */ | 73 /* Allocate the output buffer --- it will be released when done with image */ |
73 dest->buffer = (JOCTET *) | 74 dest->buffer = (JOCTET *) |
74 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | 75 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
75 » » » » OUTPUT_BUF_SIZE * SIZEOF(JOCTET)); | 76 OUTPUT_BUF_SIZE * sizeof(JOCTET)); |
76 | 77 |
77 dest->pub.next_output_byte = dest->buffer; | 78 dest->pub.next_output_byte = dest->buffer; |
78 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; | 79 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; |
79 } | 80 } |
80 | 81 |
81 #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) | 82 #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
82 METHODDEF(void) | 83 METHODDEF(void) |
83 init_mem_destination (j_compress_ptr cinfo) | 84 init_mem_destination (j_compress_ptr cinfo) |
84 { | 85 { |
85 /* no work necessary here */ | 86 /* no work necessary here */ |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; | 124 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; |
124 | 125 |
125 return TRUE; | 126 return TRUE; |
126 } | 127 } |
127 | 128 |
128 #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) | 129 #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
129 METHODDEF(boolean) | 130 METHODDEF(boolean) |
130 empty_mem_output_buffer (j_compress_ptr cinfo) | 131 empty_mem_output_buffer (j_compress_ptr cinfo) |
131 { | 132 { |
132 size_t nextsize; | 133 size_t nextsize; |
133 JOCTET * nextbuffer; | 134 JOCTET *nextbuffer; |
134 my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest; | 135 my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest; |
135 | 136 |
136 /* Try to allocate new buffer with double size */ | 137 /* Try to allocate new buffer with double size */ |
137 nextsize = dest->bufsize * 2; | 138 nextsize = dest->bufsize * 2; |
138 nextbuffer = (JOCTET *) malloc(nextsize); | 139 nextbuffer = (JOCTET *) malloc(nextsize); |
139 | 140 |
140 if (nextbuffer == NULL) | 141 if (nextbuffer == NULL) |
141 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); | 142 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); |
142 | 143 |
143 MEMCOPY(nextbuffer, dest->buffer, dest->bufsize); | 144 MEMCOPY(nextbuffer, dest->buffer, dest->bufsize); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 #endif | 197 #endif |
197 | 198 |
198 | 199 |
199 /* | 200 /* |
200 * Prepare for output to a stdio stream. | 201 * Prepare for output to a stdio stream. |
201 * The caller must have already opened the stream, and is responsible | 202 * The caller must have already opened the stream, and is responsible |
202 * for closing it after finishing compression. | 203 * for closing it after finishing compression. |
203 */ | 204 */ |
204 | 205 |
205 GLOBAL(void) | 206 GLOBAL(void) |
206 jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile) | 207 jpeg_stdio_dest (j_compress_ptr cinfo, FILE *outfile) |
207 { | 208 { |
208 my_dest_ptr dest; | 209 my_dest_ptr dest; |
209 | 210 |
210 /* The destination object is made permanent so that multiple JPEG images | 211 /* The destination object is made permanent so that multiple JPEG images |
211 * can be written to the same file without re-executing jpeg_stdio_dest. | 212 * can be written to the same file without re-executing jpeg_stdio_dest. |
212 * This makes it dangerous to use this manager and a different destination | 213 * This makes it dangerous to use this manager and a different destination |
213 * manager serially with the same JPEG object, because their private object | 214 * manager serially with the same JPEG object, because their private object |
214 * sizes may be different. Caveat programmer. | 215 * sizes may be different. Caveat programmer. |
215 */ | 216 */ |
216 if (cinfo->dest == NULL) {» /* first time for this JPEG object? */ | 217 if (cinfo->dest == NULL) { /* first time for this JPEG object? */ |
217 cinfo->dest = (struct jpeg_destination_mgr *) | 218 cinfo->dest = (struct jpeg_destination_mgr *) |
218 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, | 219 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, |
219 » » » » SIZEOF(my_destination_mgr)); | 220 sizeof(my_destination_mgr)); |
220 } | 221 } |
221 | 222 |
222 dest = (my_dest_ptr) cinfo->dest; | 223 dest = (my_dest_ptr) cinfo->dest; |
223 dest->pub.init_destination = init_destination; | 224 dest->pub.init_destination = init_destination; |
224 dest->pub.empty_output_buffer = empty_output_buffer; | 225 dest->pub.empty_output_buffer = empty_output_buffer; |
225 dest->pub.term_destination = term_destination; | 226 dest->pub.term_destination = term_destination; |
226 dest->outfile = outfile; | 227 dest->outfile = outfile; |
227 } | 228 } |
228 | 229 |
229 | 230 |
230 #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) | 231 #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
231 /* | 232 /* |
232 * Prepare for output to a memory buffer. | 233 * Prepare for output to a memory buffer. |
233 * The caller may supply an own initial buffer with appropriate size. | 234 * The caller may supply an own initial buffer with appropriate size. |
234 * Otherwise, or when the actual data output exceeds the given size, | 235 * Otherwise, or when the actual data output exceeds the given size, |
235 * the library adapts the buffer size as necessary. | 236 * the library adapts the buffer size as necessary. |
236 * The standard library functions malloc/free are used for allocating | 237 * The standard library functions malloc/free are used for allocating |
237 * larger memory, so the buffer is available to the application after | 238 * larger memory, so the buffer is available to the application after |
238 * finishing compression, and then the application is responsible for | 239 * finishing compression, and then the application is responsible for |
239 * freeing the requested memory. | 240 * freeing the requested memory. |
| 241 * Note: An initial buffer supplied by the caller is expected to be |
| 242 * managed by the application. The library does not free such buffer |
| 243 * when allocating a larger buffer. |
240 */ | 244 */ |
241 | 245 |
242 GLOBAL(void) | 246 GLOBAL(void) |
243 jpeg_mem_dest (j_compress_ptr cinfo, | 247 jpeg_mem_dest (j_compress_ptr cinfo, |
244 » unsigned char ** outbuffer, unsigned long * outsize) | 248 unsigned char **outbuffer, unsigned long *outsize) |
245 { | 249 { |
246 my_mem_dest_ptr dest; | 250 my_mem_dest_ptr dest; |
247 | 251 |
248 if (outbuffer == NULL || outsize == NULL)» /* sanity check */ | 252 if (outbuffer == NULL || outsize == NULL) /* sanity check */ |
249 ERREXIT(cinfo, JERR_BUFFER_SIZE); | 253 ERREXIT(cinfo, JERR_BUFFER_SIZE); |
250 | 254 |
251 /* The destination object is made permanent so that multiple JPEG images | 255 /* The destination object is made permanent so that multiple JPEG images |
252 * can be written to the same buffer without re-executing jpeg_mem_dest. | 256 * can be written to the same buffer without re-executing jpeg_mem_dest. |
253 */ | 257 */ |
254 if (cinfo->dest == NULL) {» /* first time for this JPEG object? */ | 258 if (cinfo->dest == NULL) { /* first time for this JPEG object? */ |
255 cinfo->dest = (struct jpeg_destination_mgr *) | 259 cinfo->dest = (struct jpeg_destination_mgr *) |
256 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, | 260 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, |
257 » » » » SIZEOF(my_mem_destination_mgr)); | 261 sizeof(my_mem_destination_mgr)); |
258 } | 262 } |
259 | 263 |
260 dest = (my_mem_dest_ptr) cinfo->dest; | 264 dest = (my_mem_dest_ptr) cinfo->dest; |
261 dest->pub.init_destination = init_mem_destination; | 265 dest->pub.init_destination = init_mem_destination; |
262 dest->pub.empty_output_buffer = empty_mem_output_buffer; | 266 dest->pub.empty_output_buffer = empty_mem_output_buffer; |
263 dest->pub.term_destination = term_mem_destination; | 267 dest->pub.term_destination = term_mem_destination; |
264 dest->outbuffer = outbuffer; | 268 dest->outbuffer = outbuffer; |
265 dest->outsize = outsize; | 269 dest->outsize = outsize; |
266 dest->newbuffer = NULL; | 270 dest->newbuffer = NULL; |
267 | 271 |
268 if (*outbuffer == NULL || *outsize == 0) { | 272 if (*outbuffer == NULL || *outsize == 0) { |
269 /* Allocate initial buffer */ | 273 /* Allocate initial buffer */ |
270 dest->newbuffer = *outbuffer = (unsigned char *) malloc(OUTPUT_BUF_SIZE); | 274 dest->newbuffer = *outbuffer = (unsigned char *) malloc(OUTPUT_BUF_SIZE); |
271 if (dest->newbuffer == NULL) | 275 if (dest->newbuffer == NULL) |
272 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); | 276 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); |
273 *outsize = OUTPUT_BUF_SIZE; | 277 *outsize = OUTPUT_BUF_SIZE; |
274 } | 278 } |
275 | 279 |
276 dest->pub.next_output_byte = dest->buffer = *outbuffer; | 280 dest->pub.next_output_byte = dest->buffer = *outbuffer; |
277 dest->pub.free_in_buffer = dest->bufsize = *outsize; | 281 dest->pub.free_in_buffer = dest->bufsize = *outsize; |
278 } | 282 } |
279 #endif | 283 #endif |
OLD | NEW |