Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jcsample.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 |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 6 | * This file is part of the Independent JPEG Group's software. |
| 7 | * For conditions of distribution and use, see the accompanying README file. |
| 8 | * |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 9 | * This file contains downsampling routines. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 10 | * |
| 11 | * Downsampling input data is counted in "row groups". A row group |
| 12 | * is defined to be max_v_samp_factor pixel rows of each component, |
| 13 | * from which the downsampler produces v_samp_factor sample rows. |
| 14 | * A single row group is processed in each call to the downsampler module. |
| 15 | * |
| 16 | * The downsampler is responsible for edge-expansion of its output data |
| 17 | * to fill an integral number of DCT blocks horizontally. The source buffer |
| 18 | * may be modified if it is helpful for this purpose (the source buffer is |
| 19 | * allocated wide enough to correspond to the desired output width). |
| 20 | * The caller (the prep controller) is responsible for vertical padding. |
| 21 | * |
| 22 | * The downsampler may request "context rows" by setting need_context_rows |
| 23 | * during startup. In this case, the input arrays will contain at least |
| 24 | * one row group's worth of pixels above and below the passed-in data; |
| 25 | * the caller will create dummy rows at image top and bottom by replicating |
| 26 | * the first or last real pixel row. |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 27 | * |
| 28 | * An excellent reference for image resampling is |
| 29 | * Digital Image Warping, George Wolberg, 1990. |
| 30 | * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7. |
| 31 | * |
| 32 | * The downsampling algorithm used here is a simple average of the source |
| 33 | * pixels covered by the output pixel. The hi-falutin sampling literature |
| 34 | * refers to this as a "box filter". In general the characteristics of a box |
| 35 | * filter are not very good, but for the specific cases we normally use (1:1 |
| 36 | * and 2:1 ratios) the box is equivalent to a "triangle filter" which is not |
| 37 | * nearly so bad. If you intend to use other sampling ratios, you'd be well |
| 38 | * advised to improve this code. |
| 39 | * |
| 40 | * A simple input-smoothing capability is provided. This is mainly intended |
| 41 | * for cleaning up color-dithered GIF input files (if you find it inadequate, |
| 42 | * we suggest using an external filtering program such as pnmconvol). When |
| 43 | * enabled, each input pixel P is replaced by a weighted sum of itself and its |
| 44 | * eight neighbors. P's weight is 1-8*SF and each neighbor's weight is SF, |
| 45 | * where SF = (smoothing_factor / 1024). |
| 46 | * Currently, smoothing is only supported for 2h2v sampling factors. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 47 | */ |
| 48 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 49 | #define JPEG_INTERNALS |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 50 | #include "jinclude.h" |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 51 | #include "jpeglib.h" |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 52 | #include "jsimd.h" |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | /* Pointer to routine to downsample a single component */ |
| 56 | typedef JMETHOD(void, downsample1_ptr, |
| 57 | (j_compress_ptr cinfo, jpeg_component_info * compptr, |
| 58 | JSAMPARRAY input_data, JSAMPARRAY output_data)); |
| 59 | |
| 60 | /* Private subobject */ |
| 61 | |
| 62 | typedef struct { |
| 63 | struct jpeg_downsampler pub; /* public fields */ |
| 64 | |
| 65 | /* Downsampling method pointers, one per component */ |
| 66 | downsample1_ptr methods[MAX_COMPONENTS]; |
| 67 | } my_downsampler; |
| 68 | |
| 69 | typedef my_downsampler * my_downsample_ptr; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 70 | |
| 71 | |
| 72 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 73 | * Initialize for a downsampling pass. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 74 | */ |
| 75 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 76 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 77 | start_pass_downsample (j_compress_ptr cinfo) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 78 | { |
| 79 | /* no work for now */ |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 84 | * Expand a component horizontally from width input_cols to width output_cols, |
| 85 | * by duplicating the rightmost samples. |
| 86 | */ |
| 87 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 88 | LOCAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 89 | expand_right_edge (JSAMPARRAY image_data, int num_rows, |
| 90 | JDIMENSION input_cols, JDIMENSION output_cols) |
| 91 | { |
| 92 | register JSAMPROW ptr; |
| 93 | register JSAMPLE pixval; |
| 94 | register int count; |
| 95 | int row; |
| 96 | int numcols = (int) (output_cols - input_cols); |
| 97 | |
| 98 | if (numcols > 0) { |
| 99 | for (row = 0; row < num_rows; row++) { |
| 100 | ptr = image_data[row] + input_cols; |
| 101 | pixval = ptr[-1]; /* don't need GETJSAMPLE() here */ |
| 102 | for (count = numcols; count > 0; count--) |
| 103 | *ptr++ = pixval; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /* |
| 110 | * Do downsampling for a whole row group (all components). |
| 111 | * |
| 112 | * In this version we simply downsample each component independently. |
| 113 | */ |
| 114 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 115 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 116 | sep_downsample (j_compress_ptr cinfo, |
| 117 | JSAMPIMAGE input_buf, JDIMENSION in_row_index, |
| 118 | JSAMPIMAGE output_buf, JDIMENSION out_row_group_index) |
| 119 | { |
| 120 | my_downsample_ptr downsample = (my_downsample_ptr) cinfo->downsample; |
| 121 | int ci; |
| 122 | jpeg_component_info * compptr; |
| 123 | JSAMPARRAY in_ptr, out_ptr; |
| 124 | |
| 125 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 126 | ci++, compptr++) { |
| 127 | in_ptr = input_buf[ci] + in_row_index; |
| 128 | out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor); |
| 129 | (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /* |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 135 | * Downsample pixel values of a single component. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 136 | * One row group is processed per call. |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 137 | * This version handles arbitrary integral sampling ratios, without smoothing. |
| 138 | * Note that this version is not actually used for customary sampling ratios. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 139 | */ |
| 140 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 141 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 142 | int_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, |
| 143 | JSAMPARRAY input_data, JSAMPARRAY output_data) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 144 | { |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 145 | int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 146 | JDIMENSION outcol, outcol_h; /* outcol_h == outcol*h_expand */ |
| 147 | JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 148 | JSAMPROW inptr, outptr; |
| 149 | INT32 outvalue; |
| 150 | |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 151 | h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor; |
| 152 | v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor; |
| 153 | numpix = h_expand * v_expand; |
| 154 | numpix2 = numpix/2; |
| 155 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 156 | /* Expand input data enough to let all the output samples be generated |
| 157 | * by the standard loop. Special-casing padded output would be more |
| 158 | * efficient. |
| 159 | */ |
| 160 | expand_right_edge(input_data, cinfo->max_v_samp_factor, |
| 161 | cinfo->image_width, output_cols * h_expand); |
| 162 | |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 163 | inrow = 0; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 164 | for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) { |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 165 | outptr = output_data[outrow]; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 166 | for (outcol = 0, outcol_h = 0; outcol < output_cols; |
| 167 | outcol++, outcol_h += h_expand) { |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 168 | outvalue = 0; |
| 169 | for (v = 0; v < v_expand; v++) { |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 170 | inptr = input_data[inrow+v] + outcol_h; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 171 | for (h = 0; h < h_expand; h++) { |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 172 | outvalue += (INT32) GETJSAMPLE(*inptr++); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
Thomas G. Lane | bd543f0 | 1991-12-13 00:00:00 +0000 | [diff] [blame] | 175 | *outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 176 | } |
| 177 | inrow += v_expand; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | |
| 182 | /* |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 183 | * Downsample pixel values of a single component. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 184 | * This version handles the special case of a full-size component, |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 185 | * without smoothing. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 186 | */ |
| 187 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 188 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 189 | fullsize_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, |
| 190 | JSAMPARRAY input_data, JSAMPARRAY output_data) |
| 191 | { |
| 192 | /* Copy the data */ |
| 193 | jcopy_sample_rows(input_data, 0, output_data, 0, |
| 194 | cinfo->max_v_samp_factor, cinfo->image_width); |
| 195 | /* Edge-expand */ |
| 196 | expand_right_edge(output_data, cinfo->max_v_samp_factor, |
| 197 | cinfo->image_width, compptr->width_in_blocks * DCTSIZE); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /* |
| 202 | * Downsample pixel values of a single component. |
| 203 | * This version handles the common case of 2:1 horizontal and 1:1 vertical, |
| 204 | * without smoothing. |
| 205 | * |
| 206 | * A note about the "bias" calculations: when rounding fractional values to |
| 207 | * integer, we do not want to always round 0.5 up to the next integer. |
| 208 | * If we did that, we'd introduce a noticeable bias towards larger values. |
| 209 | * Instead, this code is arranged so that 0.5 will be rounded up or down at |
| 210 | * alternate pixel locations (a simple ordered dither pattern). |
| 211 | */ |
| 212 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 213 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 214 | h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, |
| 215 | JSAMPARRAY input_data, JSAMPARRAY output_data) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 216 | { |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 217 | int outrow; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 218 | JDIMENSION outcol; |
| 219 | JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 220 | register JSAMPROW inptr, outptr; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 221 | register int bias; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 222 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 223 | /* Expand input data enough to let all the output samples be generated |
| 224 | * by the standard loop. Special-casing padded output would be more |
| 225 | * efficient. |
| 226 | */ |
| 227 | expand_right_edge(input_data, cinfo->max_v_samp_factor, |
| 228 | cinfo->image_width, output_cols * 2); |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 229 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 230 | for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) { |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 231 | outptr = output_data[outrow]; |
| 232 | inptr = input_data[outrow]; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 233 | bias = 0; /* bias = 0,1,0,1,... for successive samples */ |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 234 | for (outcol = 0; outcol < output_cols; outcol++) { |
| 235 | *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1]) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 236 | + bias) >> 1); |
| 237 | bias ^= 1; /* 0=>1, 1=>0 */ |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 238 | inptr += 2; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /* |
| 245 | * Downsample pixel values of a single component. |
| 246 | * This version handles the standard case of 2:1 horizontal and 2:1 vertical, |
| 247 | * without smoothing. |
| 248 | */ |
| 249 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 250 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 251 | h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, |
| 252 | JSAMPARRAY input_data, JSAMPARRAY output_data) |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 253 | { |
| 254 | int inrow, outrow; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 255 | JDIMENSION outcol; |
| 256 | JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 257 | register JSAMPROW inptr0, inptr1, outptr; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 258 | register int bias; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 259 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 260 | /* Expand input data enough to let all the output samples be generated |
| 261 | * by the standard loop. Special-casing padded output would be more |
| 262 | * efficient. |
| 263 | */ |
| 264 | expand_right_edge(input_data, cinfo->max_v_samp_factor, |
| 265 | cinfo->image_width, output_cols * 2); |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 266 | |
| 267 | inrow = 0; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 268 | for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) { |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 269 | outptr = output_data[outrow]; |
| 270 | inptr0 = input_data[inrow]; |
| 271 | inptr1 = input_data[inrow+1]; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 272 | bias = 1; /* bias = 1,2,1,2,... for successive samples */ |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 273 | for (outcol = 0; outcol < output_cols; outcol++) { |
| 274 | *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) + |
| 275 | GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 276 | + bias) >> 2); |
| 277 | bias ^= 3; /* 1=>2, 2=>1 */ |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 278 | inptr0 += 2; inptr1 += 2; |
| 279 | } |
| 280 | inrow += 2; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 285 | #ifdef INPUT_SMOOTHING_SUPPORTED |
| 286 | |
| 287 | /* |
| 288 | * Downsample pixel values of a single component. |
| 289 | * This version handles the standard case of 2:1 horizontal and 2:1 vertical, |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 290 | * with smoothing. One row of context is required. |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 291 | */ |
| 292 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 293 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 294 | h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr, |
| 295 | JSAMPARRAY input_data, JSAMPARRAY output_data) |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 296 | { |
| 297 | int inrow, outrow; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 298 | JDIMENSION colctr; |
| 299 | JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 300 | register JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr; |
| 301 | INT32 membersum, neighsum, memberscale, neighscale; |
| 302 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 303 | /* Expand input data enough to let all the output samples be generated |
| 304 | * by the standard loop. Special-casing padded output would be more |
| 305 | * efficient. |
| 306 | */ |
| 307 | expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2, |
| 308 | cinfo->image_width, output_cols * 2); |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 309 | |
| 310 | /* We don't bother to form the individual "smoothed" input pixel values; |
| 311 | * we can directly compute the output which is the average of the four |
| 312 | * smoothed values. Each of the four member pixels contributes a fraction |
| 313 | * (1-8*SF) to its own smoothed image and a fraction SF to each of the three |
| 314 | * other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final |
| 315 | * output. The four corner-adjacent neighbor pixels contribute a fraction |
| 316 | * SF to just one smoothed pixel, or SF/4 to the final output; while the |
| 317 | * eight edge-adjacent neighbors contribute SF to each of two smoothed |
| 318 | * pixels, or SF/2 overall. In order to use integer arithmetic, these |
| 319 | * factors are scaled by 2^16 = 65536. |
| 320 | * Also recall that SF = smoothing_factor / 1024. |
| 321 | */ |
| 322 | |
| 323 | memberscale = 16384 - cinfo->smoothing_factor * 80; /* scaled (1-5*SF)/4 */ |
| 324 | neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */ |
| 325 | |
| 326 | inrow = 0; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 327 | for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) { |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 328 | outptr = output_data[outrow]; |
| 329 | inptr0 = input_data[inrow]; |
| 330 | inptr1 = input_data[inrow+1]; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 331 | above_ptr = input_data[inrow-1]; |
| 332 | below_ptr = input_data[inrow+2]; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 333 | |
| 334 | /* Special case for first column: pretend column -1 is same as column 0 */ |
| 335 | membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) + |
| 336 | GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]); |
| 337 | neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) + |
| 338 | GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) + |
| 339 | GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[2]) + |
| 340 | GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[2]); |
| 341 | neighsum += neighsum; |
| 342 | neighsum += GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[2]) + |
| 343 | GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[2]); |
| 344 | membersum = membersum * memberscale + neighsum * neighscale; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 345 | *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16); |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 346 | inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2; |
| 347 | |
| 348 | for (colctr = output_cols - 2; colctr > 0; colctr--) { |
| 349 | /* sum of pixels directly mapped to this output element */ |
| 350 | membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) + |
| 351 | GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]); |
| 352 | /* sum of edge-neighbor pixels */ |
| 353 | neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) + |
| 354 | GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) + |
| 355 | GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[2]) + |
| 356 | GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[2]); |
| 357 | /* The edge-neighbors count twice as much as corner-neighbors */ |
| 358 | neighsum += neighsum; |
| 359 | /* Add in the corner-neighbors */ |
| 360 | neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[2]) + |
| 361 | GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[2]); |
| 362 | /* form final output scaled up by 2^16 */ |
| 363 | membersum = membersum * memberscale + neighsum * neighscale; |
| 364 | /* round, descale and output it */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 365 | *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16); |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 366 | inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2; |
| 367 | } |
| 368 | |
| 369 | /* Special case for last column */ |
| 370 | membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) + |
| 371 | GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]); |
| 372 | neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) + |
| 373 | GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) + |
| 374 | GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[1]) + |
| 375 | GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[1]); |
| 376 | neighsum += neighsum; |
| 377 | neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[1]) + |
| 378 | GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[1]); |
| 379 | membersum = membersum * memberscale + neighsum * neighscale; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 380 | *outptr = (JSAMPLE) ((membersum + 32768) >> 16); |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 381 | |
| 382 | inrow += 2; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | |
| 387 | /* |
| 388 | * Downsample pixel values of a single component. |
| 389 | * This version handles the special case of a full-size component, |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 390 | * with smoothing. One row of context is required. |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 391 | */ |
| 392 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 393 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 394 | fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr, |
| 395 | JSAMPARRAY input_data, JSAMPARRAY output_data) |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 396 | { |
| 397 | int outrow; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 398 | JDIMENSION colctr; |
| 399 | JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 400 | register JSAMPROW inptr, above_ptr, below_ptr, outptr; |
| 401 | INT32 membersum, neighsum, memberscale, neighscale; |
| 402 | int colsum, lastcolsum, nextcolsum; |
| 403 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 404 | /* Expand input data enough to let all the output samples be generated |
| 405 | * by the standard loop. Special-casing padded output would be more |
| 406 | * efficient. |
| 407 | */ |
| 408 | expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2, |
| 409 | cinfo->image_width, output_cols); |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 410 | |
| 411 | /* Each of the eight neighbor pixels contributes a fraction SF to the |
| 412 | * smoothed pixel, while the main pixel contributes (1-8*SF). In order |
| 413 | * to use integer arithmetic, these factors are multiplied by 2^16 = 65536. |
| 414 | * Also recall that SF = smoothing_factor / 1024. |
| 415 | */ |
| 416 | |
| 417 | memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */ |
| 418 | neighscale = cinfo->smoothing_factor * 64; /* scaled SF */ |
| 419 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 420 | for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) { |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 421 | outptr = output_data[outrow]; |
| 422 | inptr = input_data[outrow]; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 423 | above_ptr = input_data[outrow-1]; |
| 424 | below_ptr = input_data[outrow+1]; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 425 | |
| 426 | /* Special case for first column */ |
| 427 | colsum = GETJSAMPLE(*above_ptr++) + GETJSAMPLE(*below_ptr++) + |
| 428 | GETJSAMPLE(*inptr); |
| 429 | membersum = GETJSAMPLE(*inptr++); |
| 430 | nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) + |
| 431 | GETJSAMPLE(*inptr); |
| 432 | neighsum = colsum + (colsum - membersum) + nextcolsum; |
| 433 | membersum = membersum * memberscale + neighsum * neighscale; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 434 | *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16); |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 435 | lastcolsum = colsum; colsum = nextcolsum; |
| 436 | |
| 437 | for (colctr = output_cols - 2; colctr > 0; colctr--) { |
| 438 | membersum = GETJSAMPLE(*inptr++); |
| 439 | above_ptr++; below_ptr++; |
| 440 | nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) + |
| 441 | GETJSAMPLE(*inptr); |
| 442 | neighsum = lastcolsum + (colsum - membersum) + nextcolsum; |
| 443 | membersum = membersum * memberscale + neighsum * neighscale; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 444 | *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16); |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 445 | lastcolsum = colsum; colsum = nextcolsum; |
| 446 | } |
| 447 | |
| 448 | /* Special case for last column */ |
| 449 | membersum = GETJSAMPLE(*inptr); |
| 450 | neighsum = lastcolsum + (colsum - membersum) + colsum; |
| 451 | membersum = membersum * memberscale + neighsum * neighscale; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 452 | *outptr = (JSAMPLE) ((membersum + 32768) >> 16); |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 453 | |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | #endif /* INPUT_SMOOTHING_SUPPORTED */ |
| 458 | |
| 459 | |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 460 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 461 | * Module initialization routine for downsampling. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 462 | * Note that we must select a routine for each component. |
| 463 | */ |
| 464 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 465 | GLOBAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 466 | jinit_downsampler (j_compress_ptr cinfo) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 467 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 468 | my_downsample_ptr downsample; |
| 469 | int ci; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 470 | jpeg_component_info * compptr; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 471 | boolean smoothok = TRUE; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 472 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 473 | downsample = (my_downsample_ptr) |
| 474 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 475 | SIZEOF(my_downsampler)); |
| 476 | cinfo->downsample = (struct jpeg_downsampler *) downsample; |
| 477 | downsample->pub.start_pass = start_pass_downsample; |
| 478 | downsample->pub.downsample = sep_downsample; |
| 479 | downsample->pub.need_context_rows = FALSE; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 480 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 481 | if (cinfo->CCIR601_sampling) |
| 482 | ERREXIT(cinfo, JERR_CCIR601_NOTIMPL); |
| 483 | |
| 484 | /* Verify we can handle the sampling factors, and set up method pointers */ |
| 485 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 486 | ci++, compptr++) { |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 487 | if (compptr->h_samp_factor == cinfo->max_h_samp_factor && |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 488 | compptr->v_samp_factor == cinfo->max_v_samp_factor) { |
| 489 | #ifdef INPUT_SMOOTHING_SUPPORTED |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 490 | if (cinfo->smoothing_factor) { |
| 491 | downsample->methods[ci] = fullsize_smooth_downsample; |
| 492 | downsample->pub.need_context_rows = TRUE; |
| 493 | } else |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 494 | #endif |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 495 | downsample->methods[ci] = fullsize_downsample; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 496 | } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor && |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 497 | compptr->v_samp_factor == cinfo->max_v_samp_factor) { |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 498 | smoothok = FALSE; |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 499 | if (jsimd_can_h2v1_downsample()) |
| 500 | downsample->methods[ci] = jsimd_h2v1_downsample; |
| 501 | else |
| 502 | downsample->methods[ci] = h2v1_downsample; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 503 | } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor && |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 504 | compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) { |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 505 | #ifdef INPUT_SMOOTHING_SUPPORTED |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 506 | if (cinfo->smoothing_factor) { |
| 507 | downsample->methods[ci] = h2v2_smooth_downsample; |
| 508 | downsample->pub.need_context_rows = TRUE; |
| 509 | } else |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 510 | #endif |
Pierre Ossman | 59a3938 | 2009-03-09 13:15:56 +0000 | [diff] [blame] | 511 | if (jsimd_can_h2v2_downsample()) |
| 512 | downsample->methods[ci] = jsimd_h2v2_downsample; |
| 513 | else |
| 514 | downsample->methods[ci] = h2v2_downsample; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 515 | } else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 && |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 516 | (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) { |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 517 | smoothok = FALSE; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 518 | downsample->methods[ci] = int_downsample; |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 519 | } else |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 520 | ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 523 | #ifdef INPUT_SMOOTHING_SUPPORTED |
| 524 | if (cinfo->smoothing_factor && !smoothok) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 525 | TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL); |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 526 | #endif |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 527 | } |