istereo2

annotate libs/libjpeg/jcsample.c @ 7:a3c4fcc9f8f3

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