dbf-halloween2015

annotate libs/libjpeg/jcsample.c @ 3:c37fe5d8a4ed

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