vrshoot

annotate libs/libjpeg/jcsample.c @ 0:b2f14e535253

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