Wed, 31 Dec 2014 06:09:35 +0100
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 | * jcapimin.c |
michael@0 | 3 | * |
michael@0 | 4 | * Copyright (C) 1994-1998, Thomas G. Lane. |
michael@0 | 5 | * Modified 2003-2010 by Guido Vollbeding. |
michael@0 | 6 | * This file is part of the Independent JPEG Group's software. |
michael@0 | 7 | * For conditions of distribution and use, see the accompanying README file. |
michael@0 | 8 | * |
michael@0 | 9 | * This file contains application interface code for the compression half |
michael@0 | 10 | * of the JPEG library. These are the "minimum" API routines that may be |
michael@0 | 11 | * needed in either the normal full-compression case or the transcoding-only |
michael@0 | 12 | * case. |
michael@0 | 13 | * |
michael@0 | 14 | * Most of the routines intended to be called directly by an application |
michael@0 | 15 | * are in this file or in jcapistd.c. But also see jcparam.c for |
michael@0 | 16 | * parameter-setup helper routines, jcomapi.c for routines shared by |
michael@0 | 17 | * compression and decompression, and jctrans.c for the transcoding case. |
michael@0 | 18 | */ |
michael@0 | 19 | |
michael@0 | 20 | #define JPEG_INTERNALS |
michael@0 | 21 | #include "jinclude.h" |
michael@0 | 22 | #include "jpeglib.h" |
michael@0 | 23 | |
michael@0 | 24 | |
michael@0 | 25 | /* |
michael@0 | 26 | * Initialization of a JPEG compression object. |
michael@0 | 27 | * The error manager must already be set up (in case memory manager fails). |
michael@0 | 28 | */ |
michael@0 | 29 | |
michael@0 | 30 | GLOBAL(void) |
michael@0 | 31 | jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize) |
michael@0 | 32 | { |
michael@0 | 33 | int i; |
michael@0 | 34 | |
michael@0 | 35 | /* Guard against version mismatches between library and caller. */ |
michael@0 | 36 | cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */ |
michael@0 | 37 | if (version != JPEG_LIB_VERSION) |
michael@0 | 38 | ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version); |
michael@0 | 39 | if (structsize != SIZEOF(struct jpeg_compress_struct)) |
michael@0 | 40 | ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, |
michael@0 | 41 | (int) SIZEOF(struct jpeg_compress_struct), (int) structsize); |
michael@0 | 42 | |
michael@0 | 43 | /* For debugging purposes, we zero the whole master structure. |
michael@0 | 44 | * But the application has already set the err pointer, and may have set |
michael@0 | 45 | * client_data, so we have to save and restore those fields. |
michael@0 | 46 | * Note: if application hasn't set client_data, tools like Purify may |
michael@0 | 47 | * complain here. |
michael@0 | 48 | */ |
michael@0 | 49 | { |
michael@0 | 50 | struct jpeg_error_mgr * err = cinfo->err; |
michael@0 | 51 | void * client_data = cinfo->client_data; /* ignore Purify complaint here */ |
michael@0 | 52 | MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct)); |
michael@0 | 53 | cinfo->err = err; |
michael@0 | 54 | cinfo->client_data = client_data; |
michael@0 | 55 | } |
michael@0 | 56 | cinfo->is_decompressor = FALSE; |
michael@0 | 57 | |
michael@0 | 58 | /* Initialize a memory manager instance for this object */ |
michael@0 | 59 | jinit_memory_mgr((j_common_ptr) cinfo); |
michael@0 | 60 | |
michael@0 | 61 | /* Zero out pointers to permanent structures. */ |
michael@0 | 62 | cinfo->progress = NULL; |
michael@0 | 63 | cinfo->dest = NULL; |
michael@0 | 64 | |
michael@0 | 65 | cinfo->comp_info = NULL; |
michael@0 | 66 | |
michael@0 | 67 | for (i = 0; i < NUM_QUANT_TBLS; i++) { |
michael@0 | 68 | cinfo->quant_tbl_ptrs[i] = NULL; |
michael@0 | 69 | #if JPEG_LIB_VERSION >= 70 |
michael@0 | 70 | cinfo->q_scale_factor[i] = 100; |
michael@0 | 71 | #endif |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | for (i = 0; i < NUM_HUFF_TBLS; i++) { |
michael@0 | 75 | cinfo->dc_huff_tbl_ptrs[i] = NULL; |
michael@0 | 76 | cinfo->ac_huff_tbl_ptrs[i] = NULL; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | #if JPEG_LIB_VERSION >= 80 |
michael@0 | 80 | /* Must do it here for emit_dqt in case jpeg_write_tables is used */ |
michael@0 | 81 | cinfo->block_size = DCTSIZE; |
michael@0 | 82 | cinfo->natural_order = jpeg_natural_order; |
michael@0 | 83 | cinfo->lim_Se = DCTSIZE2-1; |
michael@0 | 84 | #endif |
michael@0 | 85 | |
michael@0 | 86 | cinfo->script_space = NULL; |
michael@0 | 87 | |
michael@0 | 88 | cinfo->input_gamma = 1.0; /* in case application forgets */ |
michael@0 | 89 | |
michael@0 | 90 | /* OK, I'm ready */ |
michael@0 | 91 | cinfo->global_state = CSTATE_START; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | |
michael@0 | 95 | /* |
michael@0 | 96 | * Destruction of a JPEG compression object |
michael@0 | 97 | */ |
michael@0 | 98 | |
michael@0 | 99 | GLOBAL(void) |
michael@0 | 100 | jpeg_destroy_compress (j_compress_ptr cinfo) |
michael@0 | 101 | { |
michael@0 | 102 | jpeg_destroy((j_common_ptr) cinfo); /* use common routine */ |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | |
michael@0 | 106 | /* |
michael@0 | 107 | * Abort processing of a JPEG compression operation, |
michael@0 | 108 | * but don't destroy the object itself. |
michael@0 | 109 | */ |
michael@0 | 110 | |
michael@0 | 111 | GLOBAL(void) |
michael@0 | 112 | jpeg_abort_compress (j_compress_ptr cinfo) |
michael@0 | 113 | { |
michael@0 | 114 | jpeg_abort((j_common_ptr) cinfo); /* use common routine */ |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | |
michael@0 | 118 | /* |
michael@0 | 119 | * Forcibly suppress or un-suppress all quantization and Huffman tables. |
michael@0 | 120 | * Marks all currently defined tables as already written (if suppress) |
michael@0 | 121 | * or not written (if !suppress). This will control whether they get emitted |
michael@0 | 122 | * by a subsequent jpeg_start_compress call. |
michael@0 | 123 | * |
michael@0 | 124 | * This routine is exported for use by applications that want to produce |
michael@0 | 125 | * abbreviated JPEG datastreams. It logically belongs in jcparam.c, but |
michael@0 | 126 | * since it is called by jpeg_start_compress, we put it here --- otherwise |
michael@0 | 127 | * jcparam.o would be linked whether the application used it or not. |
michael@0 | 128 | */ |
michael@0 | 129 | |
michael@0 | 130 | GLOBAL(void) |
michael@0 | 131 | jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress) |
michael@0 | 132 | { |
michael@0 | 133 | int i; |
michael@0 | 134 | JQUANT_TBL * qtbl; |
michael@0 | 135 | JHUFF_TBL * htbl; |
michael@0 | 136 | |
michael@0 | 137 | for (i = 0; i < NUM_QUANT_TBLS; i++) { |
michael@0 | 138 | if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL) |
michael@0 | 139 | qtbl->sent_table = suppress; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | for (i = 0; i < NUM_HUFF_TBLS; i++) { |
michael@0 | 143 | if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL) |
michael@0 | 144 | htbl->sent_table = suppress; |
michael@0 | 145 | if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL) |
michael@0 | 146 | htbl->sent_table = suppress; |
michael@0 | 147 | } |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | |
michael@0 | 151 | /* |
michael@0 | 152 | * Finish JPEG compression. |
michael@0 | 153 | * |
michael@0 | 154 | * If a multipass operating mode was selected, this may do a great deal of |
michael@0 | 155 | * work including most of the actual output. |
michael@0 | 156 | */ |
michael@0 | 157 | |
michael@0 | 158 | GLOBAL(void) |
michael@0 | 159 | jpeg_finish_compress (j_compress_ptr cinfo) |
michael@0 | 160 | { |
michael@0 | 161 | JDIMENSION iMCU_row; |
michael@0 | 162 | |
michael@0 | 163 | if (cinfo->global_state == CSTATE_SCANNING || |
michael@0 | 164 | cinfo->global_state == CSTATE_RAW_OK) { |
michael@0 | 165 | /* Terminate first pass */ |
michael@0 | 166 | if (cinfo->next_scanline < cinfo->image_height) |
michael@0 | 167 | ERREXIT(cinfo, JERR_TOO_LITTLE_DATA); |
michael@0 | 168 | (*cinfo->master->finish_pass) (cinfo); |
michael@0 | 169 | } else if (cinfo->global_state != CSTATE_WRCOEFS) |
michael@0 | 170 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
michael@0 | 171 | /* Perform any remaining passes */ |
michael@0 | 172 | while (! cinfo->master->is_last_pass) { |
michael@0 | 173 | (*cinfo->master->prepare_for_pass) (cinfo); |
michael@0 | 174 | for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) { |
michael@0 | 175 | if (cinfo->progress != NULL) { |
michael@0 | 176 | cinfo->progress->pass_counter = (long) iMCU_row; |
michael@0 | 177 | cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows; |
michael@0 | 178 | (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); |
michael@0 | 179 | } |
michael@0 | 180 | /* We bypass the main controller and invoke coef controller directly; |
michael@0 | 181 | * all work is being done from the coefficient buffer. |
michael@0 | 182 | */ |
michael@0 | 183 | if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL)) |
michael@0 | 184 | ERREXIT(cinfo, JERR_CANT_SUSPEND); |
michael@0 | 185 | } |
michael@0 | 186 | (*cinfo->master->finish_pass) (cinfo); |
michael@0 | 187 | } |
michael@0 | 188 | /* Write EOI, do final cleanup */ |
michael@0 | 189 | (*cinfo->marker->write_file_trailer) (cinfo); |
michael@0 | 190 | (*cinfo->dest->term_destination) (cinfo); |
michael@0 | 191 | /* We can use jpeg_abort to release memory and reset global_state */ |
michael@0 | 192 | jpeg_abort((j_common_ptr) cinfo); |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | |
michael@0 | 196 | /* |
michael@0 | 197 | * Write a special marker. |
michael@0 | 198 | * This is only recommended for writing COM or APPn markers. |
michael@0 | 199 | * Must be called after jpeg_start_compress() and before |
michael@0 | 200 | * first call to jpeg_write_scanlines() or jpeg_write_raw_data(). |
michael@0 | 201 | */ |
michael@0 | 202 | |
michael@0 | 203 | GLOBAL(void) |
michael@0 | 204 | jpeg_write_marker (j_compress_ptr cinfo, int marker, |
michael@0 | 205 | const JOCTET *dataptr, unsigned int datalen) |
michael@0 | 206 | { |
michael@0 | 207 | JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val)); |
michael@0 | 208 | |
michael@0 | 209 | if (cinfo->next_scanline != 0 || |
michael@0 | 210 | (cinfo->global_state != CSTATE_SCANNING && |
michael@0 | 211 | cinfo->global_state != CSTATE_RAW_OK && |
michael@0 | 212 | cinfo->global_state != CSTATE_WRCOEFS)) |
michael@0 | 213 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
michael@0 | 214 | |
michael@0 | 215 | (*cinfo->marker->write_marker_header) (cinfo, marker, datalen); |
michael@0 | 216 | write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */ |
michael@0 | 217 | while (datalen--) { |
michael@0 | 218 | (*write_marker_byte) (cinfo, *dataptr); |
michael@0 | 219 | dataptr++; |
michael@0 | 220 | } |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | /* Same, but piecemeal. */ |
michael@0 | 224 | |
michael@0 | 225 | GLOBAL(void) |
michael@0 | 226 | jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen) |
michael@0 | 227 | { |
michael@0 | 228 | if (cinfo->next_scanline != 0 || |
michael@0 | 229 | (cinfo->global_state != CSTATE_SCANNING && |
michael@0 | 230 | cinfo->global_state != CSTATE_RAW_OK && |
michael@0 | 231 | cinfo->global_state != CSTATE_WRCOEFS)) |
michael@0 | 232 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
michael@0 | 233 | |
michael@0 | 234 | (*cinfo->marker->write_marker_header) (cinfo, marker, datalen); |
michael@0 | 235 | } |
michael@0 | 236 | |
michael@0 | 237 | GLOBAL(void) |
michael@0 | 238 | jpeg_write_m_byte (j_compress_ptr cinfo, int val) |
michael@0 | 239 | { |
michael@0 | 240 | (*cinfo->marker->write_marker_byte) (cinfo, val); |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | |
michael@0 | 244 | /* |
michael@0 | 245 | * Alternate compression function: just write an abbreviated table file. |
michael@0 | 246 | * Before calling this, all parameters and a data destination must be set up. |
michael@0 | 247 | * |
michael@0 | 248 | * To produce a pair of files containing abbreviated tables and abbreviated |
michael@0 | 249 | * image data, one would proceed as follows: |
michael@0 | 250 | * |
michael@0 | 251 | * initialize JPEG object |
michael@0 | 252 | * set JPEG parameters |
michael@0 | 253 | * set destination to table file |
michael@0 | 254 | * jpeg_write_tables(cinfo); |
michael@0 | 255 | * set destination to image file |
michael@0 | 256 | * jpeg_start_compress(cinfo, FALSE); |
michael@0 | 257 | * write data... |
michael@0 | 258 | * jpeg_finish_compress(cinfo); |
michael@0 | 259 | * |
michael@0 | 260 | * jpeg_write_tables has the side effect of marking all tables written |
michael@0 | 261 | * (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress |
michael@0 | 262 | * will not re-emit the tables unless it is passed write_all_tables=TRUE. |
michael@0 | 263 | */ |
michael@0 | 264 | |
michael@0 | 265 | GLOBAL(void) |
michael@0 | 266 | jpeg_write_tables (j_compress_ptr cinfo) |
michael@0 | 267 | { |
michael@0 | 268 | if (cinfo->global_state != CSTATE_START) |
michael@0 | 269 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
michael@0 | 270 | |
michael@0 | 271 | /* (Re)initialize error mgr and destination modules */ |
michael@0 | 272 | (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo); |
michael@0 | 273 | (*cinfo->dest->init_destination) (cinfo); |
michael@0 | 274 | /* Initialize the marker writer ... bit of a crock to do it here. */ |
michael@0 | 275 | jinit_marker_writer(cinfo); |
michael@0 | 276 | /* Write them tables! */ |
michael@0 | 277 | (*cinfo->marker->write_tables_only) (cinfo); |
michael@0 | 278 | /* And clean up. */ |
michael@0 | 279 | (*cinfo->dest->term_destination) (cinfo); |
michael@0 | 280 | /* |
michael@0 | 281 | * In library releases up through v6a, we called jpeg_abort() here to free |
michael@0 | 282 | * any working memory allocated by the destination manager and marker |
michael@0 | 283 | * writer. Some applications had a problem with that: they allocated space |
michael@0 | 284 | * of their own from the library memory manager, and didn't want it to go |
michael@0 | 285 | * away during write_tables. So now we do nothing. This will cause a |
michael@0 | 286 | * memory leak if an app calls write_tables repeatedly without doing a full |
michael@0 | 287 | * compression cycle or otherwise resetting the JPEG object. However, that |
michael@0 | 288 | * seems less bad than unexpectedly freeing memory in the normal case. |
michael@0 | 289 | * An app that prefers the old behavior can call jpeg_abort for itself after |
michael@0 | 290 | * each call to jpeg_write_tables(). |
michael@0 | 291 | */ |
michael@0 | 292 | } |