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