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