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