Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jdsample.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 | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 5 | * Copyright (C) 1991-1996, Thomas G. Lane. |
DRC | a6ef282 | 2013-09-28 03:23:49 +0000 | [diff] [blame] | 6 | * libjpeg-turbo Modifications: |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 7 | * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 8 | * Copyright (C) 2010, 2015-2016, D. R. Commander. |
| 9 | * Copyright (C) 2014, MIPS Technologies, Inc., California. |
DRC | 0ef076f | 2016-02-19 18:32:10 -0600 | [diff] [blame] | 10 | * Copyright (C) 2015, Google, Inc. |
| 11 | * For conditions of distribution and use, see the accompanying README.ijg |
| 12 | * file. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 13 | * |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 14 | * This file contains upsampling routines. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 15 | * |
| 16 | * Upsampling input data is counted in "row groups". A row group |
| 17 | * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size) |
| 18 | * sample rows of each component. Upsampling will normally produce |
| 19 | * max_v_samp_factor pixel rows from each row group (but this could vary |
| 20 | * if the upsampler is applying a scale factor of its own). |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 21 | * |
| 22 | * An excellent reference for image resampling is |
| 23 | * Digital Image Warping, George Wolberg, 1990. |
| 24 | * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 25 | */ |
| 26 | |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 27 | #include "jinclude.h" |
DRC | ac30a1b | 2015-06-25 03:44:36 +0000 | [diff] [blame] | 28 | #include "jdsample.h" |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 29 | #include "jsimd.h" |
DRC | 36a6eec | 2010-10-08 08:05:44 +0000 | [diff] [blame] | 30 | #include "jpegcomp.h" |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 31 | |
| 32 | |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 33 | |
| 34 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 35 | * Initialize for an upsampling pass. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 36 | */ |
| 37 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 38 | METHODDEF(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 39 | start_pass_upsample(j_decompress_ptr cinfo) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 40 | { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 41 | my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 42 | |
| 43 | /* Mark the conversion buffer empty */ |
| 44 | upsample->next_row_out = cinfo->max_v_samp_factor; |
| 45 | /* Initialize total-height counter for detecting bottom of image */ |
| 46 | upsample->rows_to_go = cinfo->output_height; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | |
| 50 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 51 | * Control routine to do upsampling (and color conversion). |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 52 | * |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 53 | * In this version we upsample each component independently. |
| 54 | * We upsample one row group into the conversion buffer, then apply |
| 55 | * color conversion a row at a time. |
| 56 | */ |
| 57 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 58 | METHODDEF(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 59 | sep_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, |
| 60 | JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail, |
| 61 | JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, |
| 62 | JDIMENSION out_rows_avail) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 63 | { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 64 | my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 65 | int ci; |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 66 | jpeg_component_info *compptr; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 67 | JDIMENSION num_rows; |
| 68 | |
| 69 | /* Fill the conversion buffer, if it's empty */ |
| 70 | if (upsample->next_row_out >= cinfo->max_v_samp_factor) { |
| 71 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 72 | ci++, compptr++) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 73 | /* Invoke per-component upsample method. Notice we pass a POINTER |
| 74 | * to color_buf[ci], so that fullsize_upsample can change it. |
| 75 | */ |
| 76 | (*upsample->methods[ci]) (cinfo, compptr, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 77 | input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]), |
| 78 | upsample->color_buf + ci); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 79 | } |
| 80 | upsample->next_row_out = 0; |
| 81 | } |
| 82 | |
| 83 | /* Color-convert and emit rows */ |
| 84 | |
| 85 | /* How many we have in the buffer: */ |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 86 | num_rows = (JDIMENSION)(cinfo->max_v_samp_factor - upsample->next_row_out); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 87 | /* Not more than the distance to the end of the image. Need this test |
| 88 | * in case the image height is not a multiple of max_v_samp_factor: |
| 89 | */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 90 | if (num_rows > upsample->rows_to_go) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 91 | num_rows = upsample->rows_to_go; |
| 92 | /* And not more than what the client can accept: */ |
| 93 | out_rows_avail -= *out_row_ctr; |
| 94 | if (num_rows > out_rows_avail) |
| 95 | num_rows = out_rows_avail; |
| 96 | |
| 97 | (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf, |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 98 | (JDIMENSION)upsample->next_row_out, |
| 99 | output_buf + *out_row_ctr, (int)num_rows); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 100 | |
| 101 | /* Adjust counts */ |
| 102 | *out_row_ctr += num_rows; |
| 103 | upsample->rows_to_go -= num_rows; |
| 104 | upsample->next_row_out += num_rows; |
| 105 | /* When the buffer is emptied, declare this input row group consumed */ |
| 106 | if (upsample->next_row_out >= cinfo->max_v_samp_factor) |
| 107 | (*in_row_group_ctr)++; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | /* |
| 112 | * These are the routines invoked by sep_upsample to upsample pixel values |
| 113 | * of a single component. One row group is processed per call. |
| 114 | */ |
| 115 | |
| 116 | |
| 117 | /* |
| 118 | * For full-size components, we just make color_buf[ci] point at the |
| 119 | * input buffer, and thus avoid copying any data. Note that this is |
| 120 | * safe only because sep_upsample doesn't declare the input row group |
| 121 | * "consumed" until we are done color converting and emitting it. |
| 122 | */ |
| 123 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 124 | METHODDEF(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 125 | fullsize_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr, |
| 126 | JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 127 | { |
| 128 | *output_data_ptr = input_data; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /* |
| 133 | * This is a no-op version used for "uninteresting" components. |
| 134 | * These components will not be referenced by color conversion. |
| 135 | */ |
| 136 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 137 | METHODDEF(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 138 | noop_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr, |
| 139 | JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 140 | { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 141 | *output_data_ptr = NULL; /* safety check */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | |
| 145 | /* |
| 146 | * This version handles any integral sampling ratios. |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 147 | * This is not used for typical JPEG files, so it need not be fast. |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 148 | * Nor, for that matter, is it particularly accurate: the algorithm is |
| 149 | * simple replication of the input pixel onto the corresponding output |
| 150 | * pixels. The hi-falutin sampling literature refers to this as a |
| 151 | * "box filter". A box filter tends to introduce visible artifacts, |
| 152 | * so if you are actually going to use 3:1 or 4:1 sampling ratios |
| 153 | * you would be well advised to improve this code. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 154 | */ |
| 155 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 156 | METHODDEF(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 157 | int_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr, |
| 158 | JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 159 | { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 160 | my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 161 | JSAMPARRAY output_data = *output_data_ptr; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 162 | register JSAMPROW inptr, outptr; |
| 163 | register JSAMPLE invalue; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 164 | register int h; |
| 165 | JSAMPROW outend; |
| 166 | int h_expand, v_expand; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 167 | int inrow, outrow; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 168 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 169 | h_expand = upsample->h_expand[compptr->component_index]; |
| 170 | v_expand = upsample->v_expand[compptr->component_index]; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 171 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 172 | inrow = outrow = 0; |
| 173 | while (outrow < cinfo->max_v_samp_factor) { |
| 174 | /* Generate one output row with proper horizontal expansion */ |
| 175 | inptr = input_data[inrow]; |
| 176 | outptr = output_data[outrow]; |
| 177 | outend = outptr + cinfo->output_width; |
| 178 | while (outptr < outend) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 179 | invalue = *inptr++; /* don't need GETJSAMPLE() here */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 180 | for (h = h_expand; h > 0; h--) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 181 | *outptr++ = invalue; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 184 | /* Generate any additional output rows by duplicating the first one */ |
| 185 | if (v_expand > 1) { |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 186 | jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, |
| 187 | v_expand - 1, cinfo->output_width); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 188 | } |
| 189 | inrow++; |
| 190 | outrow += v_expand; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | |
| 195 | /* |
| 196 | * Fast processing for the common case of 2:1 horizontal and 1:1 vertical. |
| 197 | * It's still a box filter. |
| 198 | */ |
| 199 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 200 | METHODDEF(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 201 | h2v1_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr, |
| 202 | JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 203 | { |
| 204 | JSAMPARRAY output_data = *output_data_ptr; |
| 205 | register JSAMPROW inptr, outptr; |
| 206 | register JSAMPLE invalue; |
| 207 | JSAMPROW outend; |
| 208 | int inrow; |
| 209 | |
| 210 | for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) { |
| 211 | inptr = input_data[inrow]; |
| 212 | outptr = output_data[inrow]; |
| 213 | outend = outptr + cinfo->output_width; |
| 214 | while (outptr < outend) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 215 | invalue = *inptr++; /* don't need GETJSAMPLE() here */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 216 | *outptr++ = invalue; |
| 217 | *outptr++ = invalue; |
| 218 | } |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | |
| 223 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 224 | * Fast processing for the common case of 2:1 horizontal and 2:1 vertical. |
| 225 | * It's still a box filter. |
| 226 | */ |
| 227 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 228 | METHODDEF(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 229 | h2v2_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr, |
| 230 | JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 231 | { |
| 232 | JSAMPARRAY output_data = *output_data_ptr; |
| 233 | register JSAMPROW inptr, outptr; |
| 234 | register JSAMPLE invalue; |
| 235 | JSAMPROW outend; |
| 236 | int inrow, outrow; |
| 237 | |
| 238 | inrow = outrow = 0; |
| 239 | while (outrow < cinfo->max_v_samp_factor) { |
| 240 | inptr = input_data[inrow]; |
| 241 | outptr = output_data[outrow]; |
| 242 | outend = outptr + cinfo->output_width; |
| 243 | while (outptr < outend) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 244 | invalue = *inptr++; /* don't need GETJSAMPLE() here */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 245 | *outptr++ = invalue; |
| 246 | *outptr++ = invalue; |
| 247 | } |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 248 | jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, 1, |
| 249 | cinfo->output_width); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 250 | inrow++; |
| 251 | outrow += 2; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | |
| 256 | /* |
| 257 | * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical. |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 258 | * |
| 259 | * The upsampling algorithm is linear interpolation between pixel centers, |
| 260 | * also known as a "triangle filter". This is a good compromise between |
| 261 | * speed and visual quality. The centers of the output pixels are 1/4 and 3/4 |
| 262 | * of the way between input pixel centers. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 263 | * |
| 264 | * A note about the "bias" calculations: when rounding fractional values to |
| 265 | * integer, we do not want to always round 0.5 up to the next integer. |
| 266 | * If we did that, we'd introduce a noticeable bias towards larger values. |
| 267 | * Instead, this code is arranged so that 0.5 will be rounded up or down at |
| 268 | * alternate pixel locations (a simple ordered dither pattern). |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 269 | */ |
| 270 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 271 | METHODDEF(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 272 | h2v1_fancy_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr, |
| 273 | JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr) |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 274 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 275 | JSAMPARRAY output_data = *output_data_ptr; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 276 | register JSAMPROW inptr, outptr; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 277 | register int invalue; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 278 | register JDIMENSION colctr; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 279 | int inrow; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 280 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 281 | for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) { |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 282 | inptr = input_data[inrow]; |
| 283 | outptr = output_data[inrow]; |
| 284 | /* Special case for first column */ |
| 285 | invalue = GETJSAMPLE(*inptr++); |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 286 | *outptr++ = (JSAMPLE)invalue; |
| 287 | *outptr++ = (JSAMPLE)((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 288 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 289 | for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) { |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 290 | /* General case: 3/4 * nearer pixel + 1/4 * further pixel */ |
| 291 | invalue = GETJSAMPLE(*inptr++) * 3; |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 292 | *outptr++ = (JSAMPLE)((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2); |
| 293 | *outptr++ = (JSAMPLE)((invalue + GETJSAMPLE(*inptr) + 2) >> 2); |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /* Special case for last column */ |
| 297 | invalue = GETJSAMPLE(*inptr); |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 298 | *outptr++ = (JSAMPLE)((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2); |
| 299 | *outptr++ = (JSAMPLE)invalue; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
| 303 | |
| 304 | /* |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 305 | * Fancy processing for 1:1 horizontal and 2:1 vertical (4:4:0 subsampling). |
| 306 | * |
| 307 | * This is a less common case, but it can be encountered when losslessly |
| 308 | * rotating/transposing a JPEG file that uses 4:2:2 chroma subsampling. |
| 309 | */ |
| 310 | |
| 311 | METHODDEF(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 312 | h1v2_fancy_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr, |
| 313 | JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr) |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 314 | { |
| 315 | JSAMPARRAY output_data = *output_data_ptr; |
| 316 | JSAMPROW inptr0, inptr1, outptr; |
| 317 | #if BITS_IN_JSAMPLE == 8 |
| 318 | int thiscolsum; |
| 319 | #else |
| 320 | JLONG thiscolsum; |
| 321 | #endif |
| 322 | JDIMENSION colctr; |
| 323 | int inrow, outrow, v; |
| 324 | |
| 325 | inrow = outrow = 0; |
| 326 | while (outrow < cinfo->max_v_samp_factor) { |
| 327 | for (v = 0; v < 2; v++) { |
| 328 | /* inptr0 points to nearest input row, inptr1 points to next nearest */ |
| 329 | inptr0 = input_data[inrow]; |
| 330 | if (v == 0) /* next nearest is row above */ |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 331 | inptr1 = input_data[inrow - 1]; |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 332 | else /* next nearest is row below */ |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 333 | inptr1 = input_data[inrow + 1]; |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 334 | outptr = output_data[outrow++]; |
| 335 | |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 336 | for (colctr = 0; colctr < compptr->downsampled_width; colctr++) { |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 337 | thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++); |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 338 | *outptr++ = (JSAMPLE)((thiscolsum + 1) >> 2); |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | inrow++; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | |
| 346 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 347 | * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical. |
| 348 | * Again a triangle filter; see comments for h2v1 case, above. |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 349 | * |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 350 | * It is OK for us to reference the adjacent input rows because we demanded |
| 351 | * context from the main buffer controller (see initialization code). |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 352 | */ |
| 353 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 354 | METHODDEF(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 355 | h2v2_fancy_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr, |
| 356 | JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr) |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 357 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 358 | JSAMPARRAY output_data = *output_data_ptr; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 359 | register JSAMPROW inptr0, inptr1, outptr; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 360 | #if BITS_IN_JSAMPLE == 8 |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 361 | register int thiscolsum, lastcolsum, nextcolsum; |
| 362 | #else |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 363 | register JLONG thiscolsum, lastcolsum, nextcolsum; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 364 | #endif |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 365 | register JDIMENSION colctr; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 366 | int inrow, outrow, v; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 367 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 368 | inrow = outrow = 0; |
| 369 | while (outrow < cinfo->max_v_samp_factor) { |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 370 | for (v = 0; v < 2; v++) { |
| 371 | /* inptr0 points to nearest input row, inptr1 points to next nearest */ |
| 372 | inptr0 = input_data[inrow]; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 373 | if (v == 0) /* next nearest is row above */ |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 374 | inptr1 = input_data[inrow - 1]; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 375 | else /* next nearest is row below */ |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 376 | inptr1 = input_data[inrow + 1]; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 377 | outptr = output_data[outrow++]; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 378 | |
| 379 | /* Special case for first column */ |
| 380 | thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++); |
| 381 | nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++); |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 382 | *outptr++ = (JSAMPLE)((thiscolsum * 4 + 8) >> 4); |
| 383 | *outptr++ = (JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4); |
| 384 | lastcolsum = thiscolsum; thiscolsum = nextcolsum; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 385 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 386 | for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 387 | /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */ |
| 388 | /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */ |
| 389 | nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++); |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 390 | *outptr++ = (JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4); |
| 391 | *outptr++ = (JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4); |
| 392 | lastcolsum = thiscolsum; thiscolsum = nextcolsum; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 393 | } |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 394 | |
| 395 | /* Special case for last column */ |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 396 | *outptr++ = (JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4); |
| 397 | *outptr++ = (JSAMPLE)((thiscolsum * 4 + 7) >> 4); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 398 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 399 | inrow++; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
| 403 | |
| 404 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 405 | * Module initialization routine for upsampling. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 406 | */ |
| 407 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 408 | GLOBAL(void) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 409 | jinit_upsampler(j_decompress_ptr cinfo) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 410 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 411 | my_upsample_ptr upsample; |
| 412 | int ci; |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 413 | jpeg_component_info *compptr; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 414 | boolean need_buffer, do_fancy; |
| 415 | int h_in_group, v_in_group, h_out_group, v_out_group; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 416 | |
DRC | 0ef076f | 2016-02-19 18:32:10 -0600 | [diff] [blame] | 417 | if (!cinfo->master->jinit_upsampler_no_alloc) { |
| 418 | upsample = (my_upsample_ptr) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 419 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
DRC | 0ef076f | 2016-02-19 18:32:10 -0600 | [diff] [blame] | 420 | sizeof(my_upsampler)); |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 421 | cinfo->upsample = (struct jpeg_upsampler *)upsample; |
DRC | 0ef076f | 2016-02-19 18:32:10 -0600 | [diff] [blame] | 422 | upsample->pub.start_pass = start_pass_upsample; |
| 423 | upsample->pub.upsample = sep_upsample; |
| 424 | upsample->pub.need_context_rows = FALSE; /* until we find out differently */ |
| 425 | } else |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 426 | upsample = (my_upsample_ptr)cinfo->upsample; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 427 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 428 | if (cinfo->CCIR601_sampling) /* this isn't supported */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 429 | ERREXIT(cinfo, JERR_CCIR601_NOTIMPL); |
| 430 | |
| 431 | /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1, |
| 432 | * so don't ask for it. |
| 433 | */ |
DRC | 49967cd | 2010-10-09 19:57:51 +0000 | [diff] [blame] | 434 | do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 435 | |
| 436 | /* Verify we can handle the sampling factors, select per-component methods, |
| 437 | * and create storage as needed. |
| 438 | */ |
| 439 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 440 | ci++, compptr++) { |
| 441 | /* Compute size of an "input group" after IDCT scaling. This many samples |
| 442 | * are to be converted to max_h_samp_factor * max_v_samp_factor pixels. |
| 443 | */ |
DRC | 49967cd | 2010-10-09 19:57:51 +0000 | [diff] [blame] | 444 | h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) / |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 445 | cinfo->_min_DCT_scaled_size; |
DRC | 49967cd | 2010-10-09 19:57:51 +0000 | [diff] [blame] | 446 | v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) / |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 447 | cinfo->_min_DCT_scaled_size; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 448 | h_out_group = cinfo->max_h_samp_factor; |
| 449 | v_out_group = cinfo->max_v_samp_factor; |
| 450 | upsample->rowgroup_height[ci] = v_in_group; /* save for use later */ |
| 451 | need_buffer = TRUE; |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 452 | if (!compptr->component_needed) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 453 | /* Don't bother to upsample an uninteresting component. */ |
| 454 | upsample->methods[ci] = noop_upsample; |
| 455 | need_buffer = FALSE; |
| 456 | } else if (h_in_group == h_out_group && v_in_group == v_out_group) { |
| 457 | /* Fullsize components can be processed without any work. */ |
| 458 | upsample->methods[ci] = fullsize_upsample; |
| 459 | need_buffer = FALSE; |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 460 | } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 461 | /* Special cases for 2h1v upsampling */ |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 462 | if (do_fancy && compptr->downsampled_width > 2) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 463 | if (jsimd_can_h2v1_fancy_upsample()) |
| 464 | upsample->methods[ci] = jsimd_h2v1_fancy_upsample; |
| 465 | else |
| 466 | upsample->methods[ci] = h2v1_fancy_upsample; |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 467 | } else { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 468 | if (jsimd_can_h2v1_upsample()) |
| 469 | upsample->methods[ci] = jsimd_h2v1_upsample; |
| 470 | else |
| 471 | upsample->methods[ci] = h2v1_upsample; |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 472 | } |
Alex Naidis | 6eb7d37 | 2016-10-16 23:10:08 +0200 | [diff] [blame] | 473 | } else if (h_in_group == h_out_group && |
| 474 | v_in_group * 2 == v_out_group && do_fancy) { |
| 475 | /* Non-fancy upsampling is handled by the generic method */ |
| 476 | upsample->methods[ci] = h1v2_fancy_upsample; |
| 477 | upsample->pub.need_context_rows = TRUE; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 478 | } else if (h_in_group * 2 == h_out_group && |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 479 | v_in_group * 2 == v_out_group) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 480 | /* Special cases for 2h2v upsampling */ |
| 481 | if (do_fancy && compptr->downsampled_width > 2) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 482 | if (jsimd_can_h2v2_fancy_upsample()) |
| 483 | upsample->methods[ci] = jsimd_h2v2_fancy_upsample; |
| 484 | else |
| 485 | upsample->methods[ci] = h2v2_fancy_upsample; |
| 486 | upsample->pub.need_context_rows = TRUE; |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 487 | } else { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 488 | if (jsimd_can_h2v2_upsample()) |
| 489 | upsample->methods[ci] = jsimd_h2v2_upsample; |
| 490 | else |
| 491 | upsample->methods[ci] = h2v2_upsample; |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 492 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 493 | } else if ((h_out_group % h_in_group) == 0 && |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 494 | (v_out_group % v_in_group) == 0) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 495 | /* Generic integral-factors upsampling method */ |
DRC | 5ef4630 | 2014-05-18 20:04:47 +0000 | [diff] [blame] | 496 | #if defined(__mips__) |
| 497 | if (jsimd_can_int_upsample()) |
| 498 | upsample->methods[ci] = jsimd_int_upsample; |
| 499 | else |
| 500 | #endif |
| 501 | upsample->methods[ci] = int_upsample; |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 502 | upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group); |
| 503 | upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 504 | } else |
| 505 | ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL); |
DRC | 0ef076f | 2016-02-19 18:32:10 -0600 | [diff] [blame] | 506 | if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 507 | upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray) |
Leon Scroggins III | 3993b37 | 2018-07-16 10:43:45 -0400 | [diff] [blame] | 508 | ((j_common_ptr)cinfo, JPOOL_IMAGE, |
| 509 | (JDIMENSION)jround_up((long)cinfo->output_width, |
| 510 | (long)cinfo->max_h_samp_factor), |
| 511 | (JDIMENSION)cinfo->max_v_samp_factor); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 512 | } |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 513 | } |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 514 | } |