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