1 /* |
1 /* |
2 * jdsample.c |
2 * jdsample.c |
3 * |
3 * |
4 * Copyright (C) 1991-1996, Thomas G. Lane. |
4 * Copyright (C) 1991-1996, Thomas G. Lane. |
|
5 * Modified 2002-2008 by Guido Vollbeding. |
5 * This file is part of the Independent JPEG Group's software. |
6 * This file is part of the Independent JPEG Group's software. |
6 * For conditions of distribution and use, see the accompanying README file. |
7 * For conditions of distribution and use, see the accompanying README file. |
7 * |
8 * |
8 * This file contains upsampling routines. |
9 * This file contains upsampling routines. |
9 * |
10 * |
10 * Upsampling input data is counted in "row groups". A row group |
11 * Upsampling input data is counted in "row groups". A row group |
11 * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size) |
12 * is defined to be (v_samp_factor * DCT_v_scaled_size / min_DCT_v_scaled_size) |
12 * sample rows of each component. Upsampling will normally produce |
13 * sample rows of each component. Upsampling will normally produce |
13 * max_v_samp_factor pixel rows from each row group (but this could vary |
14 * max_v_samp_factor pixel rows from each row group (but this could vary |
14 * if the upsampler is applying a scale factor of its own). |
15 * if the upsampler is applying a scale factor of its own). |
15 * |
16 * |
16 * An excellent reference for image resampling is |
17 * An excellent reference for image resampling is |
284 } |
285 } |
285 } |
286 } |
286 |
287 |
287 |
288 |
288 /* |
289 /* |
289 * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical. |
|
290 * |
|
291 * The upsampling algorithm is linear interpolation between pixel centers, |
|
292 * also known as a "triangle filter". This is a good compromise between |
|
293 * speed and visual quality. The centers of the output pixels are 1/4 and 3/4 |
|
294 * of the way between input pixel centers. |
|
295 * |
|
296 * A note about the "bias" calculations: when rounding fractional values to |
|
297 * integer, we do not want to always round 0.5 up to the next integer. |
|
298 * If we did that, we'd introduce a noticeable bias towards larger values. |
|
299 * Instead, this code is arranged so that 0.5 will be rounded up or down at |
|
300 * alternate pixel locations (a simple ordered dither pattern). |
|
301 */ |
|
302 |
|
303 METHODDEF(void) |
|
304 h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
|
305 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) |
|
306 { |
|
307 JSAMPARRAY output_data = *output_data_ptr; |
|
308 register JSAMPROW inptr, outptr; |
|
309 register int invalue; |
|
310 register JDIMENSION colctr; |
|
311 int inrow; |
|
312 |
|
313 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) { |
|
314 inptr = input_data[inrow]; |
|
315 outptr = output_data[inrow]; |
|
316 /* Special case for first column */ |
|
317 invalue = GETJSAMPLE(*inptr++); |
|
318 *outptr++ = (JSAMPLE) invalue; |
|
319 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2); |
|
320 |
|
321 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) { |
|
322 /* General case: 3/4 * nearer pixel + 1/4 * further pixel */ |
|
323 invalue = GETJSAMPLE(*inptr++) * 3; |
|
324 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2); |
|
325 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2); |
|
326 } |
|
327 |
|
328 /* Special case for last column */ |
|
329 invalue = GETJSAMPLE(*inptr); |
|
330 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2); |
|
331 *outptr++ = (JSAMPLE) invalue; |
|
332 } |
|
333 } |
|
334 |
|
335 |
|
336 /* |
|
337 * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical. |
|
338 * Again a triangle filter; see comments for h2v1 case, above. |
|
339 * |
|
340 * It is OK for us to reference the adjacent input rows because we demanded |
|
341 * context from the main buffer controller (see initialization code). |
|
342 */ |
|
343 |
|
344 METHODDEF(void) |
|
345 h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
|
346 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) |
|
347 { |
|
348 JSAMPARRAY output_data = *output_data_ptr; |
|
349 register JSAMPROW inptr0, inptr1, outptr; |
|
350 #if BITS_IN_JSAMPLE == 8 |
|
351 register int thiscolsum, lastcolsum, nextcolsum; |
|
352 #else |
|
353 register INT32 thiscolsum, lastcolsum, nextcolsum; |
|
354 #endif |
|
355 register JDIMENSION colctr; |
|
356 int inrow, outrow, v; |
|
357 |
|
358 inrow = outrow = 0; |
|
359 while (outrow < cinfo->max_v_samp_factor) { |
|
360 for (v = 0; v < 2; v++) { |
|
361 /* inptr0 points to nearest input row, inptr1 points to next nearest */ |
|
362 inptr0 = input_data[inrow]; |
|
363 if (v == 0) /* next nearest is row above */ |
|
364 inptr1 = input_data[inrow-1]; |
|
365 else /* next nearest is row below */ |
|
366 inptr1 = input_data[inrow+1]; |
|
367 outptr = output_data[outrow++]; |
|
368 |
|
369 /* Special case for first column */ |
|
370 thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++); |
|
371 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++); |
|
372 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4); |
|
373 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4); |
|
374 lastcolsum = thiscolsum; thiscolsum = nextcolsum; |
|
375 |
|
376 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) { |
|
377 /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */ |
|
378 /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */ |
|
379 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++); |
|
380 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4); |
|
381 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4); |
|
382 lastcolsum = thiscolsum; thiscolsum = nextcolsum; |
|
383 } |
|
384 |
|
385 /* Special case for last column */ |
|
386 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4); |
|
387 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4); |
|
388 } |
|
389 inrow++; |
|
390 } |
|
391 } |
|
392 |
|
393 |
|
394 /* |
|
395 * Module initialization routine for upsampling. |
290 * Module initialization routine for upsampling. |
396 */ |
291 */ |
397 |
292 |
398 GLOBAL(void) |
293 GLOBAL(void) |
399 jinit_upsampler (j_decompress_ptr cinfo) |
294 jinit_upsampler (j_decompress_ptr cinfo) |
400 { |
295 { |
401 my_upsample_ptr upsample; |
296 my_upsample_ptr upsample; |
402 int ci; |
297 int ci; |
403 jpeg_component_info * compptr; |
298 jpeg_component_info * compptr; |
404 boolean need_buffer, do_fancy; |
299 boolean need_buffer; |
405 int h_in_group, v_in_group, h_out_group, v_out_group; |
300 int h_in_group, v_in_group, h_out_group, v_out_group; |
406 |
301 |
407 upsample = (my_upsample_ptr) |
302 upsample = (my_upsample_ptr) |
408 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
303 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
409 SIZEOF(my_upsampler)); |
304 SIZEOF(my_upsampler)); |
412 upsample->pub.upsample = sep_upsample; |
307 upsample->pub.upsample = sep_upsample; |
413 upsample->pub.need_context_rows = FALSE; /* until we find out differently */ |
308 upsample->pub.need_context_rows = FALSE; /* until we find out differently */ |
414 |
309 |
415 if (cinfo->CCIR601_sampling) /* this isn't supported */ |
310 if (cinfo->CCIR601_sampling) /* this isn't supported */ |
416 ERREXIT(cinfo, JERR_CCIR601_NOTIMPL); |
311 ERREXIT(cinfo, JERR_CCIR601_NOTIMPL); |
417 |
|
418 /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1, |
|
419 * so don't ask for it. |
|
420 */ |
|
421 do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1; |
|
422 |
312 |
423 /* Verify we can handle the sampling factors, select per-component methods, |
313 /* Verify we can handle the sampling factors, select per-component methods, |
424 * and create storage as needed. |
314 * and create storage as needed. |
425 */ |
315 */ |
426 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
316 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
427 ci++, compptr++) { |
317 ci++, compptr++) { |
428 /* Compute size of an "input group" after IDCT scaling. This many samples |
318 /* Compute size of an "input group" after IDCT scaling. This many samples |
429 * are to be converted to max_h_samp_factor * max_v_samp_factor pixels. |
319 * are to be converted to max_h_samp_factor * max_v_samp_factor pixels. |
430 */ |
320 */ |
431 h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) / |
321 h_in_group = (compptr->h_samp_factor * compptr->DCT_h_scaled_size) / |
432 cinfo->min_DCT_scaled_size; |
322 cinfo->min_DCT_h_scaled_size; |
433 v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) / |
323 v_in_group = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) / |
434 cinfo->min_DCT_scaled_size; |
324 cinfo->min_DCT_v_scaled_size; |
435 h_out_group = cinfo->max_h_samp_factor; |
325 h_out_group = cinfo->max_h_samp_factor; |
436 v_out_group = cinfo->max_v_samp_factor; |
326 v_out_group = cinfo->max_v_samp_factor; |
437 upsample->rowgroup_height[ci] = v_in_group; /* save for use later */ |
327 upsample->rowgroup_height[ci] = v_in_group; /* save for use later */ |
438 need_buffer = TRUE; |
328 need_buffer = TRUE; |
439 if (! compptr->component_needed) { |
329 if (! compptr->component_needed) { |
444 /* Fullsize components can be processed without any work. */ |
334 /* Fullsize components can be processed without any work. */ |
445 upsample->methods[ci] = fullsize_upsample; |
335 upsample->methods[ci] = fullsize_upsample; |
446 need_buffer = FALSE; |
336 need_buffer = FALSE; |
447 } else if (h_in_group * 2 == h_out_group && |
337 } else if (h_in_group * 2 == h_out_group && |
448 v_in_group == v_out_group) { |
338 v_in_group == v_out_group) { |
449 /* Special cases for 2h1v upsampling */ |
339 /* Special case for 2h1v upsampling */ |
450 if (do_fancy && compptr->downsampled_width > 2) |
340 upsample->methods[ci] = h2v1_upsample; |
451 upsample->methods[ci] = h2v1_fancy_upsample; |
|
452 else |
|
453 upsample->methods[ci] = h2v1_upsample; |
|
454 } else if (h_in_group * 2 == h_out_group && |
341 } else if (h_in_group * 2 == h_out_group && |
455 v_in_group * 2 == v_out_group) { |
342 v_in_group * 2 == v_out_group) { |
456 /* Special cases for 2h2v upsampling */ |
343 /* Special case for 2h2v upsampling */ |
457 if (do_fancy && compptr->downsampled_width > 2) { |
344 upsample->methods[ci] = h2v2_upsample; |
458 upsample->methods[ci] = h2v2_fancy_upsample; |
|
459 upsample->pub.need_context_rows = TRUE; |
|
460 } else |
|
461 upsample->methods[ci] = h2v2_upsample; |
|
462 } else if ((h_out_group % h_in_group) == 0 && |
345 } else if ((h_out_group % h_in_group) == 0 && |
463 (v_out_group % v_in_group) == 0) { |
346 (v_out_group % v_in_group) == 0) { |
464 /* Generic integral-factors upsampling method */ |
347 /* Generic integral-factors upsampling method */ |
465 upsample->methods[ci] = int_upsample; |
348 upsample->methods[ci] = int_upsample; |
466 upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group); |
349 upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group); |