Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jdpostct.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 | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 5 | * Copyright (C) 1994-1996, Thomas G. Lane. |
DRC | 5033f3e | 2014-05-18 18:33:44 +0000 | [diff] [blame] | 6 | * It was modified by The libjpeg-turbo Project to include only code relevant |
| 7 | * to libjpeg-turbo. |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 8 | * For conditions of distribution and use, see the accompanying README.ijg |
| 9 | * file. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 10 | * |
| 11 | * This file contains the decompression postprocessing controller. |
| 12 | * This controller manages the upsampling, color conversion, and color |
| 13 | * quantization/reduction steps; specifically, it controls the buffering |
| 14 | * between upsample/color conversion and color quantization/reduction. |
| 15 | * |
| 16 | * If no color quantization/reduction is required, then this module has no |
| 17 | * work to do, and it just hands off to the upsample/color conversion code. |
| 18 | * An integrated upsample/convert/quantize process would replace this module |
| 19 | * entirely. |
| 20 | */ |
| 21 | |
| 22 | #define JPEG_INTERNALS |
| 23 | #include "jinclude.h" |
| 24 | #include "jpeglib.h" |
| 25 | |
| 26 | |
| 27 | /* Private buffer controller object */ |
| 28 | |
| 29 | typedef struct { |
| 30 | struct jpeg_d_post_controller pub; /* public fields */ |
| 31 | |
| 32 | /* Color quantization source buffer: this holds output data from |
| 33 | * the upsample/color conversion step to be passed to the quantizer. |
| 34 | * For two-pass color quantization, we need a full-image buffer; |
| 35 | * for one-pass operation, a strip buffer is sufficient. |
| 36 | */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 37 | jvirt_sarray_ptr whole_image; /* virtual array, or NULL if one-pass */ |
| 38 | JSAMPARRAY buffer; /* strip buffer, or current strip of virtual */ |
| 39 | JDIMENSION strip_height; /* buffer size in rows */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 40 | /* for two-pass mode only: */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 41 | JDIMENSION starting_row; /* row # of first row in current strip */ |
| 42 | JDIMENSION next_row; /* index of next row to fill/empty in strip */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 43 | } my_post_controller; |
| 44 | |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 45 | typedef my_post_controller *my_post_ptr; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | /* Forward declarations */ |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 49 | METHODDEF(void) post_process_1pass(j_decompress_ptr cinfo, |
| 50 | JSAMPIMAGE input_buf, |
| 51 | JDIMENSION *in_row_group_ctr, |
| 52 | JDIMENSION in_row_groups_avail, |
| 53 | JSAMPARRAY output_buf, |
| 54 | JDIMENSION *out_row_ctr, |
| 55 | JDIMENSION out_rows_avail); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 56 | #ifdef QUANT_2PASS_SUPPORTED |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 57 | METHODDEF(void) post_process_prepass(j_decompress_ptr cinfo, |
| 58 | JSAMPIMAGE input_buf, |
| 59 | JDIMENSION *in_row_group_ctr, |
| 60 | JDIMENSION in_row_groups_avail, |
| 61 | JSAMPARRAY output_buf, |
| 62 | JDIMENSION *out_row_ctr, |
| 63 | JDIMENSION out_rows_avail); |
| 64 | METHODDEF(void) post_process_2pass(j_decompress_ptr cinfo, |
| 65 | JSAMPIMAGE input_buf, |
| 66 | JDIMENSION *in_row_group_ctr, |
| 67 | JDIMENSION in_row_groups_avail, |
| 68 | JSAMPARRAY output_buf, |
| 69 | JDIMENSION *out_row_ctr, |
| 70 | JDIMENSION out_rows_avail); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 71 | #endif |
| 72 | |
| 73 | |
| 74 | /* |
| 75 | * Initialize for a processing pass. |
| 76 | */ |
| 77 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 78 | METHODDEF(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 79 | start_pass_dpost(j_decompress_ptr cinfo, J_BUF_MODE pass_mode) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 80 | { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 81 | my_post_ptr post = (my_post_ptr)cinfo->post; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 82 | |
| 83 | switch (pass_mode) { |
| 84 | case JBUF_PASS_THRU: |
| 85 | if (cinfo->quantize_colors) { |
| 86 | /* Single-pass processing with color quantization. */ |
| 87 | post->pub.post_process_data = post_process_1pass; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 88 | /* We could be doing buffered-image output before starting a 2-pass |
| 89 | * color quantization; in that case, jinit_d_post_controller did not |
| 90 | * allocate a strip buffer. Use the virtual-array buffer as workspace. |
| 91 | */ |
| 92 | if (post->buffer == NULL) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 93 | post->buffer = (*cinfo->mem->access_virt_sarray) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 94 | ((j_common_ptr)cinfo, post->whole_image, |
| 95 | (JDIMENSION)0, post->strip_height, TRUE); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 96 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 97 | } else { |
| 98 | /* For single-pass processing without color quantization, |
| 99 | * I have no work to do; just call the upsampler directly. |
| 100 | */ |
| 101 | post->pub.post_process_data = cinfo->upsample->upsample; |
| 102 | } |
| 103 | break; |
| 104 | #ifdef QUANT_2PASS_SUPPORTED |
| 105 | case JBUF_SAVE_AND_PASS: |
| 106 | /* First pass of 2-pass quantization */ |
| 107 | if (post->whole_image == NULL) |
| 108 | ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); |
| 109 | post->pub.post_process_data = post_process_prepass; |
| 110 | break; |
| 111 | case JBUF_CRANK_DEST: |
| 112 | /* Second pass of 2-pass quantization */ |
| 113 | if (post->whole_image == NULL) |
| 114 | ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); |
| 115 | post->pub.post_process_data = post_process_2pass; |
| 116 | break; |
| 117 | #endif /* QUANT_2PASS_SUPPORTED */ |
| 118 | default: |
| 119 | ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); |
| 120 | break; |
| 121 | } |
| 122 | post->starting_row = post->next_row = 0; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | /* |
| 127 | * Process some data in the one-pass (strip buffer) case. |
| 128 | * This is used for color precision reduction as well as one-pass quantization. |
| 129 | */ |
| 130 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 131 | METHODDEF(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 132 | post_process_1pass(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, |
| 133 | JDIMENSION *in_row_group_ctr, |
| 134 | JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf, |
| 135 | JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 136 | { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 137 | my_post_ptr post = (my_post_ptr)cinfo->post; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 138 | JDIMENSION num_rows, max_rows; |
| 139 | |
| 140 | /* Fill the buffer, but not more than what we can dump out in one go. */ |
| 141 | /* Note we rely on the upsampler to detect bottom of image. */ |
| 142 | max_rows = out_rows_avail - *out_row_ctr; |
| 143 | if (max_rows > post->strip_height) |
| 144 | max_rows = post->strip_height; |
| 145 | num_rows = 0; |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 146 | (*cinfo->upsample->upsample) (cinfo, input_buf, in_row_group_ctr, |
| 147 | in_row_groups_avail, post->buffer, &num_rows, |
| 148 | max_rows); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 149 | /* Quantize and emit data. */ |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 150 | (*cinfo->cquantize->color_quantize) (cinfo, post->buffer, |
| 151 | output_buf + *out_row_ctr, |
| 152 | (int)num_rows); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 153 | *out_row_ctr += num_rows; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | #ifdef QUANT_2PASS_SUPPORTED |
| 158 | |
| 159 | /* |
| 160 | * Process some data in the first pass of 2-pass quantization. |
| 161 | */ |
| 162 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 163 | METHODDEF(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 164 | post_process_prepass(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, |
| 165 | JDIMENSION *in_row_group_ctr, |
| 166 | JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf, |
| 167 | JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 168 | { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 169 | my_post_ptr post = (my_post_ptr)cinfo->post; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 170 | JDIMENSION old_next_row, num_rows; |
| 171 | |
| 172 | /* Reposition virtual buffer if at start of strip. */ |
| 173 | if (post->next_row == 0) { |
| 174 | post->buffer = (*cinfo->mem->access_virt_sarray) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 175 | ((j_common_ptr)cinfo, post->whole_image, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 176 | post->starting_row, post->strip_height, TRUE); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* Upsample some data (up to a strip height's worth). */ |
| 180 | old_next_row = post->next_row; |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 181 | (*cinfo->upsample->upsample) (cinfo, input_buf, in_row_group_ctr, |
| 182 | in_row_groups_avail, post->buffer, |
| 183 | &post->next_row, post->strip_height); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 184 | |
| 185 | /* Allow quantizer to scan new data. No data is emitted, */ |
| 186 | /* but we advance out_row_ctr so outer loop can tell when we're done. */ |
| 187 | if (post->next_row > old_next_row) { |
| 188 | num_rows = post->next_row - old_next_row; |
| 189 | (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row, |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 190 | (JSAMPARRAY)NULL, (int)num_rows); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 191 | *out_row_ctr += num_rows; |
| 192 | } |
| 193 | |
| 194 | /* Advance if we filled the strip. */ |
| 195 | if (post->next_row >= post->strip_height) { |
| 196 | post->starting_row += post->strip_height; |
| 197 | post->next_row = 0; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | |
| 202 | /* |
| 203 | * Process some data in the second pass of 2-pass quantization. |
| 204 | */ |
| 205 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 206 | METHODDEF(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 207 | post_process_2pass(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, |
| 208 | JDIMENSION *in_row_group_ctr, |
| 209 | JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf, |
| 210 | JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 211 | { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 212 | my_post_ptr post = (my_post_ptr)cinfo->post; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 213 | JDIMENSION num_rows, max_rows; |
| 214 | |
| 215 | /* Reposition virtual buffer if at start of strip. */ |
| 216 | if (post->next_row == 0) { |
| 217 | post->buffer = (*cinfo->mem->access_virt_sarray) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 218 | ((j_common_ptr)cinfo, post->whole_image, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 219 | post->starting_row, post->strip_height, FALSE); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /* Determine number of rows to emit. */ |
| 223 | num_rows = post->strip_height - post->next_row; /* available in strip */ |
| 224 | max_rows = out_rows_avail - *out_row_ctr; /* available in output area */ |
| 225 | if (num_rows > max_rows) |
| 226 | num_rows = max_rows; |
| 227 | /* We have to check bottom of image here, can't depend on upsampler. */ |
| 228 | max_rows = cinfo->output_height - post->starting_row; |
| 229 | if (num_rows > max_rows) |
| 230 | num_rows = max_rows; |
| 231 | |
| 232 | /* Quantize and emit data. */ |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 233 | (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + post->next_row, |
| 234 | output_buf + *out_row_ctr, |
| 235 | (int)num_rows); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 236 | *out_row_ctr += num_rows; |
| 237 | |
| 238 | /* Advance if we filled the strip. */ |
| 239 | post->next_row += num_rows; |
| 240 | if (post->next_row >= post->strip_height) { |
| 241 | post->starting_row += post->strip_height; |
| 242 | post->next_row = 0; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | #endif /* QUANT_2PASS_SUPPORTED */ |
| 247 | |
| 248 | |
| 249 | /* |
| 250 | * Initialize postprocessing controller. |
| 251 | */ |
| 252 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 253 | GLOBAL(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 254 | jinit_d_post_controller(j_decompress_ptr cinfo, boolean need_full_buffer) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 255 | { |
| 256 | my_post_ptr post; |
| 257 | |
| 258 | post = (my_post_ptr) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 259 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 260 | sizeof(my_post_controller)); |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 261 | cinfo->post = (struct jpeg_d_post_controller *)post; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 262 | post->pub.start_pass = start_pass_dpost; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 263 | post->whole_image = NULL; /* flag for no virtual arrays */ |
| 264 | post->buffer = NULL; /* flag for no strip buffer */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 265 | |
| 266 | /* Create the quantization buffer, if needed */ |
| 267 | if (cinfo->quantize_colors) { |
| 268 | /* The buffer strip height is max_v_samp_factor, which is typically |
| 269 | * an efficient number of rows for upsampling to return. |
| 270 | * (In the presence of output rescaling, we might want to be smarter?) |
| 271 | */ |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 272 | post->strip_height = (JDIMENSION)cinfo->max_v_samp_factor; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 273 | if (need_full_buffer) { |
| 274 | /* Two-pass color quantization: need full-image storage. */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 275 | /* We round up the number of rows to a multiple of the strip height. */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 276 | #ifdef QUANT_2PASS_SUPPORTED |
| 277 | post->whole_image = (*cinfo->mem->request_virt_sarray) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 278 | ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 279 | cinfo->output_width * cinfo->out_color_components, |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 280 | (JDIMENSION)jround_up((long)cinfo->output_height, |
| 281 | (long)post->strip_height), |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 282 | post->strip_height); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 283 | #else |
| 284 | ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); |
| 285 | #endif /* QUANT_2PASS_SUPPORTED */ |
| 286 | } else { |
| 287 | /* One-pass color quantization: just make a strip buffer. */ |
| 288 | post->buffer = (*cinfo->mem->alloc_sarray) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 289 | ((j_common_ptr)cinfo, JPOOL_IMAGE, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 290 | cinfo->output_width * cinfo->out_color_components, |
| 291 | post->strip_height); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | } |