media/libjpeg/jdatadst.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial