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