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