Bug 1885602 - Part 1: Add strings for the sign in header in the menu redesign r=andro...
[gecko.git] / media / libjpeg / jdatadst.c
blob6b4fed2339715ef8fbd02cc07b0de84a04fb243f
1 /*
2 * jdatadst.c
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.
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.
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"
27 /* Expanded data destination object for stdio output */
29 typedef struct {
30 struct jpeg_destination_mgr pub; /* public fields */
32 FILE *outfile; /* target stream */
33 JOCTET *buffer; /* start of buffer */
34 } my_destination_mgr;
36 typedef my_destination_mgr *my_dest_ptr;
38 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
41 #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
42 /* Expanded data destination object for memory output */
44 typedef struct {
45 struct jpeg_destination_mgr pub; /* public fields */
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;
54 typedef my_mem_destination_mgr *my_mem_dest_ptr;
55 #endif
59 * Initialize destination --- called by jpeg_start_compress
60 * before any data is actually written.
63 METHODDEF(void)
64 init_destination(j_compress_ptr cinfo)
66 my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
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));
73 dest->pub.next_output_byte = dest->buffer;
74 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
77 #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
78 METHODDEF(void)
79 init_mem_destination(j_compress_ptr cinfo)
81 /* no work necessary here */
83 #endif
87 * Empty the output buffer --- called whenever buffer fills up.
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.
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.
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.
109 METHODDEF(boolean)
110 empty_output_buffer(j_compress_ptr cinfo)
112 my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
114 if (fwrite(dest->buffer, 1, OUTPUT_BUF_SIZE, dest->outfile) !=
115 (size_t)OUTPUT_BUF_SIZE)
116 ERREXIT(cinfo, JERR_FILE_WRITE);
118 dest->pub.next_output_byte = dest->buffer;
119 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
121 return TRUE;
124 #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
125 METHODDEF(boolean)
126 empty_mem_output_buffer(j_compress_ptr cinfo)
128 size_t nextsize;
129 JOCTET *nextbuffer;
130 my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
132 /* Try to allocate new buffer with double size */
133 nextsize = dest->bufsize * 2;
134 nextbuffer = (JOCTET *)malloc(nextsize);
136 if (nextbuffer == NULL)
137 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
139 memcpy(nextbuffer, dest->buffer, dest->bufsize);
141 free(dest->newbuffer);
143 dest->newbuffer = nextbuffer;
145 dest->pub.next_output_byte = nextbuffer + dest->bufsize;
146 dest->pub.free_in_buffer = dest->bufsize;
148 dest->buffer = nextbuffer;
149 dest->bufsize = nextsize;
151 return TRUE;
153 #endif
157 * Terminate destination --- called by jpeg_finish_compress
158 * after all data has been written. Usually needs to flush buffer.
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.
165 METHODDEF(void)
166 term_destination(j_compress_ptr cinfo)
168 my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
169 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
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);
176 fflush(dest->outfile);
177 /* Make sure we wrote the output file OK */
178 if (ferror(dest->outfile))
179 ERREXIT(cinfo, JERR_FILE_WRITE);
182 #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
183 METHODDEF(void)
184 term_mem_destination(j_compress_ptr cinfo)
186 my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
188 *dest->outbuffer = dest->buffer;
189 *dest->outsize = (unsigned long)(dest->bufsize - dest->pub.free_in_buffer);
191 #endif
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.
200 GLOBAL(void)
201 jpeg_stdio_dest(j_compress_ptr cinfo, FILE *outfile)
203 my_dest_ptr dest;
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.
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.
219 ERREXIT(cinfo, JERR_BUFFER_SIZE);
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;
230 #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
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.
245 GLOBAL(void)
246 jpeg_mem_dest(j_compress_ptr cinfo, unsigned char **outbuffer,
247 unsigned long *outsize)
249 my_mem_dest_ptr dest;
251 if (outbuffer == NULL || outsize == NULL) /* sanity check */
252 ERREXIT(cinfo, JERR_BUFFER_SIZE);
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.
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.
265 ERREXIT(cinfo, JERR_BUFFER_SIZE);
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;
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;
284 dest->pub.next_output_byte = dest->buffer = *outbuffer;
285 dest->pub.free_in_buffer = dest->bufsize = *outsize;
287 #endif