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