3dphotoshoot

annotate libs/libjpeg/jcsample.c @ 14:06dc8b9b4f89

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