Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jcmaster.c |
| 3 | * |
DRC | a73e870 | 2012-12-31 02:52:30 +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) 1991-1997, Thomas G. Lane. |
Guido Vollbeding | 989630f | 2010-01-10 00:00:00 +0000 | [diff] [blame] | 6 | * Modified 2003-2010 by Guido Vollbeding. |
DRC | a6ef282 | 2013-09-28 03:23:49 +0000 | [diff] [blame] | 7 | * libjpeg-turbo Modifications: |
DRC | a628952 | 2018-07-24 18:36:51 -0500 | [diff] [blame] | 8 | * Copyright (C) 2010, 2016, 2018, D. R. Commander. |
DRC | 7e3acc0 | 2015-10-10 10:25:46 -0500 | [diff] [blame] | 9 | * For conditions of distribution and use, see the accompanying README.ijg |
| 10 | * file. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 11 | * |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 12 | * This file contains master control logic for the JPEG compressor. |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 13 | * These routines are concerned with parameter validation, initial setup, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 14 | * and inter-pass control (determining the number of passes and the work |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 15 | * to be done in each pass). |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 18 | #define JPEG_INTERNALS |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 19 | #include "jinclude.h" |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 20 | #include "jpeglib.h" |
DRC | c04bd3c | 2010-10-10 02:15:56 +0000 | [diff] [blame] | 21 | #include "jpegcomp.h" |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 22 | |
| 23 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 24 | /* Private state */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 25 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 26 | typedef enum { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 27 | main_pass, /* input data, also do first output step */ |
| 28 | huff_opt_pass, /* Huffman code optimization pass */ |
| 29 | output_pass /* data output pass */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 30 | } c_pass_type; |
| 31 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 32 | typedef struct { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 33 | struct jpeg_comp_master pub; /* public fields */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 34 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 35 | c_pass_type pass_type; /* the type of the current pass */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 36 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 37 | int pass_number; /* # of passes completed */ |
| 38 | int total_passes; /* total # of passes needed */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 39 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 40 | int scan_number; /* current index in scan_info[] */ |
DRC | 6f241d4 | 2016-03-16 07:10:35 -0500 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * This is here so we can add libjpeg-turbo version/build information to the |
| 44 | * global string table without introducing a new global symbol. Adding this |
| 45 | * information to the global string table allows one to examine a binary |
| 46 | * object and determine which version of libjpeg-turbo it was built from or |
| 47 | * linked against. |
| 48 | */ |
| 49 | const char *jpeg_version; |
| 50 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 51 | } my_comp_master; |
| 52 | |
DRC | bd49803 | 2016-02-19 08:53:33 -0600 | [diff] [blame] | 53 | typedef my_comp_master *my_master_ptr; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 54 | |
| 55 | |
| 56 | /* |
| 57 | * Support routines that do various essential calculations. |
| 58 | */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 59 | |
DRC | 36a6eec | 2010-10-08 08:05:44 +0000 | [diff] [blame] | 60 | #if JPEG_LIB_VERSION >= 70 |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 61 | /* |
| 62 | * Compute JPEG image dimensions and related values. |
| 63 | * NOTE: this is exported for possible use by application. |
| 64 | * Hence it mustn't do anything that can't be done twice. |
| 65 | */ |
| 66 | |
| 67 | GLOBAL(void) |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 68 | jpeg_calc_jpeg_dimensions(j_compress_ptr cinfo) |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 69 | /* Do computations that are needed before master selection phase */ |
| 70 | { |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 71 | /* Hardwire it to "no scaling" */ |
| 72 | cinfo->jpeg_width = cinfo->image_width; |
| 73 | cinfo->jpeg_height = cinfo->image_height; |
| 74 | cinfo->min_DCT_h_scaled_size = DCTSIZE; |
| 75 | cinfo->min_DCT_v_scaled_size = DCTSIZE; |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 76 | } |
DRC | 36a6eec | 2010-10-08 08:05:44 +0000 | [diff] [blame] | 77 | #endif |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 78 | |
| 79 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 80 | LOCAL(void) |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 81 | initial_setup(j_compress_ptr cinfo, boolean transcode_only) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 82 | /* Do computations that are needed before master selection phase */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 83 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 84 | int ci; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 85 | jpeg_component_info *compptr; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 86 | long samplesperrow; |
| 87 | JDIMENSION jd_samplesperrow; |
| 88 | |
DRC | 36a6eec | 2010-10-08 08:05:44 +0000 | [diff] [blame] | 89 | #if JPEG_LIB_VERSION >= 70 |
DRC | e7fde87 | 2011-04-03 07:09:49 +0000 | [diff] [blame] | 90 | #if JPEG_LIB_VERSION >= 80 |
DRC | c04bd3c | 2010-10-10 02:15:56 +0000 | [diff] [blame] | 91 | if (!transcode_only) |
DRC | e7fde87 | 2011-04-03 07:09:49 +0000 | [diff] [blame] | 92 | #endif |
Guido Vollbeding | 989630f | 2010-01-10 00:00:00 +0000 | [diff] [blame] | 93 | jpeg_calc_jpeg_dimensions(cinfo); |
DRC | 36a6eec | 2010-10-08 08:05:44 +0000 | [diff] [blame] | 94 | #endif |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 95 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 96 | /* Sanity check on image dimensions */ |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 97 | if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0 || |
| 98 | cinfo->num_components <= 0 || cinfo->input_components <= 0) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 99 | ERREXIT(cinfo, JERR_EMPTY_IMAGE); |
| 100 | |
| 101 | /* Make sure image isn't bigger than I can handle */ |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 102 | if ((long)cinfo->_jpeg_height > (long)JPEG_MAX_DIMENSION || |
| 103 | (long)cinfo->_jpeg_width > (long)JPEG_MAX_DIMENSION) |
| 104 | ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int)JPEG_MAX_DIMENSION); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 105 | |
| 106 | /* Width of an input scanline must be representable as JDIMENSION. */ |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 107 | samplesperrow = (long)cinfo->image_width * (long)cinfo->input_components; |
| 108 | jd_samplesperrow = (JDIMENSION)samplesperrow; |
| 109 | if ((long)jd_samplesperrow != samplesperrow) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 110 | ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); |
| 111 | |
| 112 | /* For now, precision must match compiled-in value... */ |
| 113 | if (cinfo->data_precision != BITS_IN_JSAMPLE) |
| 114 | ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); |
| 115 | |
| 116 | /* Check that number of components won't exceed internal array sizes */ |
| 117 | if (cinfo->num_components > MAX_COMPONENTS) |
| 118 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 119 | MAX_COMPONENTS); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 120 | |
| 121 | /* Compute maximum sampling factors; check factor validity */ |
| 122 | cinfo->max_h_samp_factor = 1; |
| 123 | cinfo->max_v_samp_factor = 1; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 124 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 125 | ci++, compptr++) { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 126 | if (compptr->h_samp_factor <= 0 || |
| 127 | compptr->h_samp_factor > MAX_SAMP_FACTOR || |
| 128 | compptr->v_samp_factor <= 0 || |
| 129 | compptr->v_samp_factor > MAX_SAMP_FACTOR) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 130 | ERREXIT(cinfo, JERR_BAD_SAMPLING); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 131 | cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 132 | compptr->h_samp_factor); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 133 | cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 134 | compptr->v_samp_factor); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 137 | /* Compute dimensions of components */ |
| 138 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 139 | ci++, compptr++) { |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 140 | /* Fill in the correct component_index value; don't rely on application */ |
| 141 | compptr->component_index = ci; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 142 | /* For compression, we never do DCT scaling. */ |
DRC | 36a6eec | 2010-10-08 08:05:44 +0000 | [diff] [blame] | 143 | #if JPEG_LIB_VERSION >= 70 |
| 144 | compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE; |
| 145 | #else |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 146 | compptr->DCT_scaled_size = DCTSIZE; |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 147 | #endif |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 148 | /* Size in DCT blocks */ |
| 149 | compptr->width_in_blocks = (JDIMENSION) |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 150 | jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor, |
| 151 | (long)(cinfo->max_h_samp_factor * DCTSIZE)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 152 | compptr->height_in_blocks = (JDIMENSION) |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 153 | jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor, |
| 154 | (long)(cinfo->max_v_samp_factor * DCTSIZE)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 155 | /* Size in samples */ |
| 156 | compptr->downsampled_width = (JDIMENSION) |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 157 | jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor, |
| 158 | (long)cinfo->max_h_samp_factor); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 159 | compptr->downsampled_height = (JDIMENSION) |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 160 | jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor, |
| 161 | (long)cinfo->max_v_samp_factor); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 162 | /* Mark component needed (this flag isn't actually used for compression) */ |
| 163 | compptr->component_needed = TRUE; |
| 164 | } |
| 165 | |
| 166 | /* Compute number of fully interleaved MCU rows (number of times that |
| 167 | * main controller will call coefficient controller). |
| 168 | */ |
| 169 | cinfo->total_iMCU_rows = (JDIMENSION) |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 170 | jdiv_round_up((long)cinfo->_jpeg_height, |
| 171 | (long)(cinfo->max_v_samp_factor * DCTSIZE)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 175 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
| 176 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 177 | LOCAL(void) |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 178 | validate_script(j_compress_ptr cinfo) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 179 | /* Verify that the scan script in cinfo->scan_info[] is valid; also |
| 180 | * determine whether it uses progressive JPEG, and set cinfo->progressive_mode. |
| 181 | */ |
| 182 | { |
DRC | bd49803 | 2016-02-19 08:53:33 -0600 | [diff] [blame] | 183 | const jpeg_scan_info *scanptr; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 184 | int scanno, ncomps, ci, coefi, thisi; |
| 185 | int Ss, Se, Ah, Al; |
| 186 | boolean component_sent[MAX_COMPONENTS]; |
| 187 | #ifdef C_PROGRESSIVE_SUPPORTED |
DRC | bd49803 | 2016-02-19 08:53:33 -0600 | [diff] [blame] | 188 | int *last_bitpos_ptr; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 189 | int last_bitpos[MAX_COMPONENTS][DCTSIZE2]; |
| 190 | /* -1 until that coefficient has been seen; then last Al for it */ |
| 191 | #endif |
| 192 | |
| 193 | if (cinfo->num_scans <= 0) |
| 194 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0); |
| 195 | |
| 196 | /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1; |
| 197 | * for progressive JPEG, no scan can have this. |
| 198 | */ |
| 199 | scanptr = cinfo->scan_info; |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 200 | if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2 - 1) { |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 201 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 202 | cinfo->progressive_mode = TRUE; |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 203 | last_bitpos_ptr = &last_bitpos[0][0]; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 204 | for (ci = 0; ci < cinfo->num_components; ci++) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 205 | for (coefi = 0; coefi < DCTSIZE2; coefi++) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 206 | *last_bitpos_ptr++ = -1; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 207 | #else |
| 208 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 209 | #endif |
| 210 | } else { |
| 211 | cinfo->progressive_mode = FALSE; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 212 | for (ci = 0; ci < cinfo->num_components; ci++) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 213 | component_sent[ci] = FALSE; |
| 214 | } |
| 215 | |
| 216 | for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) { |
| 217 | /* Validate component indexes */ |
| 218 | ncomps = scanptr->comps_in_scan; |
| 219 | if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN) |
| 220 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN); |
| 221 | for (ci = 0; ci < ncomps; ci++) { |
| 222 | thisi = scanptr->component_index[ci]; |
| 223 | if (thisi < 0 || thisi >= cinfo->num_components) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 224 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 225 | /* Components must appear in SOF order within each scan */ |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 226 | if (ci > 0 && thisi <= scanptr->component_index[ci - 1]) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 227 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 228 | } |
| 229 | /* Validate progression parameters */ |
| 230 | Ss = scanptr->Ss; |
| 231 | Se = scanptr->Se; |
| 232 | Ah = scanptr->Ah; |
| 233 | Al = scanptr->Al; |
| 234 | if (cinfo->progressive_mode) { |
| 235 | #ifdef C_PROGRESSIVE_SUPPORTED |
DRC | a628952 | 2018-07-24 18:36:51 -0500 | [diff] [blame] | 236 | /* Rec. ITU-T T.81 | ISO/IEC 10918-1 simply gives the ranges 0..13 for Ah |
| 237 | * and Al, but that seems wrong: the upper bound ought to depend on data |
| 238 | * precision. Perhaps they really meant 0..N+1 for N-bit precision. |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 239 | * Here we allow 0..10 for 8-bit data; Al larger than 10 results in |
| 240 | * out-of-range reconstructed DC values during the first DC scan, |
| 241 | * which might cause problems for some decoders. |
| 242 | */ |
| 243 | #if BITS_IN_JSAMPLE == 8 |
DRC | 293263c | 2018-03-17 15:14:35 -0500 | [diff] [blame] | 244 | #define MAX_AH_AL 10 |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 245 | #else |
DRC | 293263c | 2018-03-17 15:14:35 -0500 | [diff] [blame] | 246 | #define MAX_AH_AL 13 |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 247 | #endif |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 248 | if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 || |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 249 | Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL) |
| 250 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 251 | if (Ss == 0) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 252 | if (Se != 0) /* DC and AC together not OK */ |
| 253 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 254 | } else { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 255 | if (ncomps != 1) /* AC scans must be for only one component */ |
| 256 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 257 | } |
| 258 | for (ci = 0; ci < ncomps; ci++) { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 259 | last_bitpos_ptr = &last_bitpos[scanptr->component_index[ci]][0]; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 260 | if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */ |
| 261 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
| 262 | for (coefi = Ss; coefi <= Se; coefi++) { |
| 263 | if (last_bitpos_ptr[coefi] < 0) { |
| 264 | /* first scan of this coefficient */ |
| 265 | if (Ah != 0) |
| 266 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
| 267 | } else { |
| 268 | /* not first scan */ |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 269 | if (Ah != last_bitpos_ptr[coefi] || Al != Ah - 1) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 270 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
| 271 | } |
| 272 | last_bitpos_ptr[coefi] = Al; |
| 273 | } |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 274 | } |
| 275 | #endif |
| 276 | } else { |
| 277 | /* For sequential JPEG, all progression parameters must be these: */ |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 278 | if (Ss != 0 || Se != DCTSIZE2 - 1 || Ah != 0 || Al != 0) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 279 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 280 | /* Make sure components are not sent twice */ |
| 281 | for (ci = 0; ci < ncomps; ci++) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 282 | thisi = scanptr->component_index[ci]; |
| 283 | if (component_sent[thisi]) |
| 284 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); |
| 285 | component_sent[thisi] = TRUE; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /* Now verify that everything got sent. */ |
| 291 | if (cinfo->progressive_mode) { |
| 292 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 293 | /* For progressive mode, we only check that at least some DC data |
| 294 | * got sent for each component; the spec does not require that all bits |
| 295 | * of all coefficients be transmitted. Would it be wiser to enforce |
| 296 | * transmission of all coefficient bits?? |
| 297 | */ |
| 298 | for (ci = 0; ci < cinfo->num_components; ci++) { |
| 299 | if (last_bitpos[ci][0] < 0) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 300 | ERREXIT(cinfo, JERR_MISSING_DATA); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 301 | } |
| 302 | #endif |
| 303 | } else { |
| 304 | for (ci = 0; ci < cinfo->num_components; ci++) { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 305 | if (!component_sent[ci]) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 306 | ERREXIT(cinfo, JERR_MISSING_DATA); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | #endif /* C_MULTISCAN_FILES_SUPPORTED */ |
| 312 | |
| 313 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 314 | LOCAL(void) |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 315 | select_scan_parameters(j_compress_ptr cinfo) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 316 | /* Set up the scan parameters for the current scan */ |
| 317 | { |
| 318 | int ci; |
| 319 | |
| 320 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
| 321 | if (cinfo->scan_info != NULL) { |
| 322 | /* Prepare for current scan --- the script is already validated */ |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 323 | my_master_ptr master = (my_master_ptr)cinfo->master; |
DRC | bd49803 | 2016-02-19 08:53:33 -0600 | [diff] [blame] | 324 | const jpeg_scan_info *scanptr = cinfo->scan_info + master->scan_number; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 325 | |
| 326 | cinfo->comps_in_scan = scanptr->comps_in_scan; |
| 327 | for (ci = 0; ci < scanptr->comps_in_scan; ci++) { |
| 328 | cinfo->cur_comp_info[ci] = |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 329 | &cinfo->comp_info[scanptr->component_index[ci]]; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 330 | } |
| 331 | cinfo->Ss = scanptr->Ss; |
| 332 | cinfo->Se = scanptr->Se; |
| 333 | cinfo->Ah = scanptr->Ah; |
| 334 | cinfo->Al = scanptr->Al; |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 335 | } else |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 336 | #endif |
| 337 | { |
| 338 | /* Prepare for single sequential-JPEG scan containing all components */ |
| 339 | if (cinfo->num_components > MAX_COMPS_IN_SCAN) |
| 340 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 341 | MAX_COMPS_IN_SCAN); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 342 | cinfo->comps_in_scan = cinfo->num_components; |
| 343 | for (ci = 0; ci < cinfo->num_components; ci++) { |
| 344 | cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci]; |
| 345 | } |
| 346 | cinfo->Ss = 0; |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 347 | cinfo->Se = DCTSIZE2 - 1; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 348 | cinfo->Ah = 0; |
| 349 | cinfo->Al = 0; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 354 | LOCAL(void) |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 355 | per_scan_setup(j_compress_ptr cinfo) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 356 | /* Do computations that are needed before processing a JPEG scan */ |
| 357 | /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */ |
| 358 | { |
| 359 | int ci, mcublks, tmp; |
| 360 | jpeg_component_info *compptr; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 361 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 362 | if (cinfo->comps_in_scan == 1) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 363 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 364 | /* Noninterleaved (single-component) scan */ |
| 365 | compptr = cinfo->cur_comp_info[0]; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 366 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 367 | /* Overall image size in MCUs */ |
| 368 | cinfo->MCUs_per_row = compptr->width_in_blocks; |
| 369 | cinfo->MCU_rows_in_scan = compptr->height_in_blocks; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 370 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 371 | /* For noninterleaved scan, always one block per MCU */ |
| 372 | compptr->MCU_width = 1; |
| 373 | compptr->MCU_height = 1; |
| 374 | compptr->MCU_blocks = 1; |
| 375 | compptr->MCU_sample_width = DCTSIZE; |
| 376 | compptr->last_col_width = 1; |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 377 | /* For noninterleaved scans, it is convenient to define last_row_height |
| 378 | * as the number of block rows present in the last iMCU row. |
| 379 | */ |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 380 | tmp = (int)(compptr->height_in_blocks % compptr->v_samp_factor); |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 381 | if (tmp == 0) tmp = compptr->v_samp_factor; |
| 382 | compptr->last_row_height = tmp; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 383 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 384 | /* Prepare array describing MCU composition */ |
| 385 | cinfo->blocks_in_MCU = 1; |
| 386 | cinfo->MCU_membership[0] = 0; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 387 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 388 | } else { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 389 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 390 | /* Interleaved (multi-component) scan */ |
| 391 | if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) |
| 392 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 393 | MAX_COMPS_IN_SCAN); |
| 394 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 395 | /* Overall image size in MCUs */ |
| 396 | cinfo->MCUs_per_row = (JDIMENSION) |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 397 | jdiv_round_up((long)cinfo->_jpeg_width, |
| 398 | (long)(cinfo->max_h_samp_factor * DCTSIZE)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 399 | cinfo->MCU_rows_in_scan = (JDIMENSION) |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 400 | jdiv_round_up((long)cinfo->_jpeg_height, |
| 401 | (long)(cinfo->max_v_samp_factor * DCTSIZE)); |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 402 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 403 | cinfo->blocks_in_MCU = 0; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 404 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 405 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 406 | compptr = cinfo->cur_comp_info[ci]; |
| 407 | /* Sampling factors give # of blocks of component in each MCU */ |
| 408 | compptr->MCU_width = compptr->h_samp_factor; |
| 409 | compptr->MCU_height = compptr->v_samp_factor; |
| 410 | compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; |
| 411 | compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE; |
| 412 | /* Figure number of non-dummy blocks in last MCU column & row */ |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 413 | tmp = (int)(compptr->width_in_blocks % compptr->MCU_width); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 414 | if (tmp == 0) tmp = compptr->MCU_width; |
| 415 | compptr->last_col_width = tmp; |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 416 | tmp = (int)(compptr->height_in_blocks % compptr->MCU_height); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 417 | if (tmp == 0) tmp = compptr->MCU_height; |
| 418 | compptr->last_row_height = tmp; |
| 419 | /* Prepare array describing MCU composition */ |
| 420 | mcublks = compptr->MCU_blocks; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 421 | if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 422 | ERREXIT(cinfo, JERR_BAD_MCU_SIZE); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 423 | while (mcublks-- > 0) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 424 | cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 427 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /* Convert restart specified in rows to actual MCU count. */ |
| 431 | /* Note that count must fit in 16 bits, so we provide limiting. */ |
| 432 | if (cinfo->restart_in_rows > 0) { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 433 | long nominal = (long)cinfo->restart_in_rows * (long)cinfo->MCUs_per_row; |
| 434 | cinfo->restart_interval = (unsigned int)MIN(nominal, 65535L); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | |
| 439 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 440 | * Per-pass setup. |
| 441 | * This is called at the beginning of each pass. We determine which modules |
| 442 | * will be active during this pass and give them appropriate start_pass calls. |
| 443 | * We also set is_last_pass to indicate whether any more passes will be |
| 444 | * required. |
| 445 | */ |
| 446 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 447 | METHODDEF(void) |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 448 | prepare_for_pass(j_compress_ptr cinfo) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 449 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 450 | my_master_ptr master = (my_master_ptr)cinfo->master; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 451 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 452 | switch (master->pass_type) { |
| 453 | case main_pass: |
| 454 | /* Initial pass: will collect input data, and do either Huffman |
| 455 | * optimization or data output for the first scan. |
| 456 | */ |
| 457 | select_scan_parameters(cinfo); |
| 458 | per_scan_setup(cinfo); |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 459 | if (!cinfo->raw_data_in) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 460 | (*cinfo->cconvert->start_pass) (cinfo); |
| 461 | (*cinfo->downsample->start_pass) (cinfo); |
| 462 | (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU); |
| 463 | } |
| 464 | (*cinfo->fdct->start_pass) (cinfo); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 465 | (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding); |
| 466 | (*cinfo->coef->start_pass) (cinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 467 | (master->total_passes > 1 ? |
| 468 | JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 469 | (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 470 | if (cinfo->optimize_coding) { |
| 471 | /* No immediate data output; postpone writing frame/scan headers */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 472 | master->pub.call_pass_startup = FALSE; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 473 | } else { |
| 474 | /* Will write frame/scan headers at first jpeg_write_scanlines call */ |
| 475 | master->pub.call_pass_startup = TRUE; |
| 476 | } |
| 477 | break; |
| 478 | #ifdef ENTROPY_OPT_SUPPORTED |
| 479 | case huff_opt_pass: |
| 480 | /* Do Huffman optimization for a scan after the first one. */ |
| 481 | select_scan_parameters(cinfo); |
| 482 | per_scan_setup(cinfo); |
| 483 | if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 484 | (*cinfo->entropy->start_pass) (cinfo, TRUE); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 485 | (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 486 | master->pub.call_pass_startup = FALSE; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 487 | break; |
| 488 | } |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 489 | /* Special case: Huffman DC refinement scans need no Huffman table |
| 490 | * and therefore we can skip the optimization pass for them. |
| 491 | */ |
| 492 | master->pass_type = output_pass; |
| 493 | master->pass_number++; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 494 | #endif |
Peter Kasting | 9df5786 | 2021-06-26 16:22:21 -0700 | [diff] [blame] | 495 | FALLTHROUGH /*FALLTHROUGH*/ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 496 | case output_pass: |
| 497 | /* Do a data-output pass. */ |
| 498 | /* We need not repeat per-scan setup if prior optimization pass did it. */ |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 499 | if (!cinfo->optimize_coding) { |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 500 | select_scan_parameters(cinfo); |
| 501 | per_scan_setup(cinfo); |
| 502 | } |
| 503 | (*cinfo->entropy->start_pass) (cinfo, FALSE); |
| 504 | (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); |
| 505 | /* We emit frame/scan headers now */ |
| 506 | if (master->scan_number == 0) |
| 507 | (*cinfo->marker->write_frame_header) (cinfo); |
| 508 | (*cinfo->marker->write_scan_header) (cinfo); |
| 509 | master->pub.call_pass_startup = FALSE; |
| 510 | break; |
| 511 | default: |
| 512 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 513 | } |
| 514 | |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 515 | master->pub.is_last_pass = (master->pass_number == master->total_passes - 1); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 516 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 517 | /* Set up progress monitor's pass info if present */ |
| 518 | if (cinfo->progress != NULL) { |
| 519 | cinfo->progress->completed_passes = master->pass_number; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 520 | cinfo->progress->total_passes = master->total_passes; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 521 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | |
| 525 | /* |
| 526 | * Special start-of-pass hook. |
| 527 | * This is called by jpeg_write_scanlines if call_pass_startup is TRUE. |
| 528 | * In single-pass processing, we need this hook because we don't want to |
| 529 | * write frame/scan headers during jpeg_start_compress; we want to let the |
| 530 | * application write COM markers etc. between jpeg_start_compress and the |
| 531 | * jpeg_write_scanlines loop. |
| 532 | * In multi-pass processing, this routine is not used. |
| 533 | */ |
| 534 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 535 | METHODDEF(void) |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 536 | pass_startup(j_compress_ptr cinfo) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 537 | { |
| 538 | cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */ |
| 539 | |
| 540 | (*cinfo->marker->write_frame_header) (cinfo); |
| 541 | (*cinfo->marker->write_scan_header) (cinfo); |
| 542 | } |
| 543 | |
| 544 | |
| 545 | /* |
| 546 | * Finish up at end of pass. |
| 547 | */ |
| 548 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 549 | METHODDEF(void) |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 550 | finish_pass_master(j_compress_ptr cinfo) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 551 | { |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 552 | my_master_ptr master = (my_master_ptr)cinfo->master; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 553 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 554 | /* The entropy coder always needs an end-of-pass call, |
| 555 | * either to analyze statistics or to flush its output buffer. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 556 | */ |
| 557 | (*cinfo->entropy->finish_pass) (cinfo); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 558 | |
| 559 | /* Update state for next pass */ |
| 560 | switch (master->pass_type) { |
| 561 | case main_pass: |
| 562 | /* next pass is either output of scan 0 (after optimization) |
| 563 | * or output of scan 1 (if no optimization). |
| 564 | */ |
| 565 | master->pass_type = output_pass; |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 566 | if (!cinfo->optimize_coding) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 567 | master->scan_number++; |
| 568 | break; |
| 569 | case huff_opt_pass: |
| 570 | /* next pass is always output of current scan */ |
| 571 | master->pass_type = output_pass; |
| 572 | break; |
| 573 | case output_pass: |
| 574 | /* next pass is either optimization or output of next scan */ |
| 575 | if (cinfo->optimize_coding) |
| 576 | master->pass_type = huff_opt_pass; |
| 577 | master->scan_number++; |
| 578 | break; |
| 579 | } |
| 580 | |
| 581 | master->pass_number++; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | |
| 585 | /* |
| 586 | * Initialize master compression control. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 587 | */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 588 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 589 | GLOBAL(void) |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 590 | jinit_c_master_control(j_compress_ptr cinfo, boolean transcode_only) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 591 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 592 | my_master_ptr master; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 593 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 594 | master = (my_master_ptr) |
DRC | 19c791c | 2018-03-08 10:55:20 -0600 | [diff] [blame] | 595 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
| 596 | sizeof(my_comp_master)); |
| 597 | cinfo->master = (struct jpeg_comp_master *)master; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 598 | master->pub.prepare_for_pass = prepare_for_pass; |
| 599 | master->pub.pass_startup = pass_startup; |
| 600 | master->pub.finish_pass = finish_pass_master; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 601 | master->pub.is_last_pass = FALSE; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 602 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 603 | /* Validate parameters, determine derived values */ |
Guido Vollbeding | 989630f | 2010-01-10 00:00:00 +0000 | [diff] [blame] | 604 | initial_setup(cinfo, transcode_only); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 605 | |
| 606 | if (cinfo->scan_info != NULL) { |
| 607 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
| 608 | validate_script(cinfo); |
| 609 | #else |
| 610 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 611 | #endif |
| 612 | } else { |
| 613 | cinfo->progressive_mode = FALSE; |
| 614 | cinfo->num_scans = 1; |
| 615 | } |
| 616 | |
DRC | e621dfc | 2016-02-19 10:35:09 -0600 | [diff] [blame] | 617 | if (cinfo->progressive_mode && !cinfo->arith_code) /* TEMPORARY HACK ??? */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 618 | cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */ |
| 619 | |
| 620 | /* Initialize my private state */ |
| 621 | if (transcode_only) { |
| 622 | /* no main pass in transcoding */ |
| 623 | if (cinfo->optimize_coding) |
| 624 | master->pass_type = huff_opt_pass; |
| 625 | else |
| 626 | master->pass_type = output_pass; |
| 627 | } else { |
| 628 | /* for normal compression, first pass is always this type: */ |
| 629 | master->pass_type = main_pass; |
| 630 | } |
| 631 | master->scan_number = 0; |
| 632 | master->pass_number = 0; |
| 633 | if (cinfo->optimize_coding) |
| 634 | master->total_passes = cinfo->num_scans * 2; |
| 635 | else |
| 636 | master->total_passes = cinfo->num_scans; |
DRC | 6f241d4 | 2016-03-16 07:10:35 -0500 | [diff] [blame] | 637 | |
| 638 | master->jpeg_version = PACKAGE_NAME " version " VERSION " (build " BUILD ")"; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 639 | } |