media/libjpeg/jcsample.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial