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