Bug 573948 - Part 1: Use libjpeg-turbo instead of libjpeg. r=jmuizelaar
[gecko.git] / jpeg / jdatadst.c
blob2f488696c3be4b0b817b6b1775fd2094715acf5c
1 /*
2 * jdatadst.c
4 * Copyright (C) 1994-1996, Thomas G. Lane.
5 * Modified 2009 by Guido Vollbeding.
6 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
9 * This file contains compression data destination routines for the case of
10 * emitting JPEG data to memory or to a file (or any stdio stream).
11 * While these routines are sufficient for most applications,
12 * some will want to use a different destination manager.
13 * IMPORTANT: we assume that fwrite() will correctly transcribe an array of
14 * JOCTETs into 8-bit-wide elements on external storage. If char is wider
15 * than 8 bits on your machine, you may need to do some tweaking.
18 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
19 #include "jinclude.h"
20 #include "jpeglib.h"
21 #include "jerror.h"
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
29 /* Expanded data destination object for stdio output */
31 typedef struct {
32 struct jpeg_destination_mgr pub; /* public fields */
34 FILE * outfile; /* target stream */
35 JOCTET * buffer; /* start of buffer */
36 } my_destination_mgr;
38 typedef my_destination_mgr * my_dest_ptr;
40 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
43 #if JPEG_LIB_VERSION >= 80
44 /* Expanded data destination object for memory output */
46 typedef struct {
47 struct jpeg_destination_mgr pub; /* public fields */
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;
56 typedef my_mem_destination_mgr * my_mem_dest_ptr;
57 #endif
61 * Initialize destination --- called by jpeg_start_compress
62 * before any data is actually written.
65 METHODDEF(void)
66 init_destination (j_compress_ptr cinfo)
68 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
70 /* Allocate the output buffer --- it will be released when done with image */
71 dest->buffer = (JOCTET *)
72 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
73 OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
75 dest->pub.next_output_byte = dest->buffer;
76 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
79 #if JPEG_LIB_VERSION >= 80
80 METHODDEF(void)
81 init_mem_destination (j_compress_ptr cinfo)
83 /* no work necessary here */
85 #endif
89 * Empty the output buffer --- called whenever buffer fills up.
91 * In typical applications, this should write the entire output buffer
92 * (ignoring the current state of next_output_byte & free_in_buffer),
93 * reset the pointer & count to the start of the buffer, and return TRUE
94 * indicating that the buffer has been dumped.
96 * In applications that need to be able to suspend compression due to output
97 * overrun, a FALSE return indicates that the buffer cannot be emptied now.
98 * In this situation, the compressor will return to its caller (possibly with
99 * an indication that it has not accepted all the supplied scanlines). The
100 * application should resume compression after it has made more room in the
101 * output buffer. Note that there are substantial restrictions on the use of
102 * suspension --- see the documentation.
104 * When suspending, the compressor will back up to a convenient restart point
105 * (typically the start of the current MCU). next_output_byte & free_in_buffer
106 * indicate where the restart point will be if the current call returns FALSE.
107 * Data beyond this point will be regenerated after resumption, so do not
108 * write it out when emptying the buffer externally.
111 METHODDEF(boolean)
112 empty_output_buffer (j_compress_ptr cinfo)
114 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
116 if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
117 (size_t) OUTPUT_BUF_SIZE)
118 ERREXIT(cinfo, JERR_FILE_WRITE);
120 dest->pub.next_output_byte = dest->buffer;
121 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
123 return TRUE;
126 #if JPEG_LIB_VERSION >= 80
127 METHODDEF(boolean)
128 empty_mem_output_buffer (j_compress_ptr cinfo)
130 size_t nextsize;
131 JOCTET * nextbuffer;
132 my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;
134 /* Try to allocate new buffer with double size */
135 nextsize = dest->bufsize * 2;
136 nextbuffer = malloc(nextsize);
138 if (nextbuffer == NULL)
139 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
141 MEMCOPY(nextbuffer, dest->buffer, dest->bufsize);
143 if (dest->newbuffer != NULL)
144 free(dest->newbuffer);
146 dest->newbuffer = nextbuffer;
148 dest->pub.next_output_byte = nextbuffer + dest->bufsize;
149 dest->pub.free_in_buffer = dest->bufsize;
151 dest->buffer = nextbuffer;
152 dest->bufsize = nextsize;
154 return TRUE;
156 #endif
160 * Terminate destination --- called by jpeg_finish_compress
161 * after all data has been written. Usually needs to flush buffer.
163 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
164 * application must deal with any cleanup that should happen even
165 * for error exit.
168 METHODDEF(void)
169 term_destination (j_compress_ptr cinfo)
171 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
172 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
174 /* Write any data remaining in the buffer */
175 if (datacount > 0) {
176 if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
177 ERREXIT(cinfo, JERR_FILE_WRITE);
179 fflush(dest->outfile);
180 /* Make sure we wrote the output file OK */
181 if (ferror(dest->outfile))
182 ERREXIT(cinfo, JERR_FILE_WRITE);
185 #if JPEG_LIB_VERSION >= 80
186 METHODDEF(void)
187 term_mem_destination (j_compress_ptr cinfo)
189 my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;
191 *dest->outbuffer = dest->buffer;
192 *dest->outsize = dest->bufsize - dest->pub.free_in_buffer;
194 #endif
198 * Prepare for output to a stdio stream.
199 * The caller must have already opened the stream, and is responsible
200 * for closing it after finishing compression.
203 GLOBAL(void)
204 jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile)
206 my_dest_ptr dest;
208 /* The destination object is made permanent so that multiple JPEG images
209 * can be written to the same file without re-executing jpeg_stdio_dest.
210 * This makes it dangerous to use this manager and a different destination
211 * manager serially with the same JPEG object, because their private object
212 * sizes may be different. Caveat programmer.
214 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
215 cinfo->dest = (struct jpeg_destination_mgr *)
216 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
217 SIZEOF(my_destination_mgr));
220 dest = (my_dest_ptr) cinfo->dest;
221 dest->pub.init_destination = init_destination;
222 dest->pub.empty_output_buffer = empty_output_buffer;
223 dest->pub.term_destination = term_destination;
224 dest->outfile = outfile;
228 #if JPEG_LIB_VERSION >= 80
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.
240 GLOBAL(void)
241 jpeg_mem_dest (j_compress_ptr cinfo,
242 unsigned char ** outbuffer, unsigned long * outsize)
244 my_mem_dest_ptr dest;
246 if (outbuffer == NULL || outsize == NULL) /* sanity check */
247 ERREXIT(cinfo, JERR_BUFFER_SIZE);
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.
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));
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;
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;
274 dest->pub.next_output_byte = dest->buffer = *outbuffer;
275 dest->pub.free_in_buffer = dest->bufsize = *outsize;
277 #endif