michael@0: /* michael@0: * jdsample.c michael@0: * michael@0: * This file was part of the Independent JPEG Group's software: michael@0: * Copyright (C) 1991-1996, Thomas G. Lane. michael@0: * libjpeg-turbo Modifications: michael@0: * Copyright 2009 Pierre Ossman for Cendio AB michael@0: * Copyright (C) 2010, D. R. Commander. michael@0: * For conditions of distribution and use, see the accompanying README file. michael@0: * michael@0: * This file contains upsampling routines. michael@0: * michael@0: * Upsampling input data is counted in "row groups". A row group michael@0: * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size) michael@0: * sample rows of each component. Upsampling will normally produce michael@0: * max_v_samp_factor pixel rows from each row group (but this could vary michael@0: * if the upsampler is applying a scale factor of its own). michael@0: * michael@0: * An excellent reference for image resampling is michael@0: * Digital Image Warping, George Wolberg, 1990. michael@0: * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7. michael@0: */ michael@0: michael@0: #define JPEG_INTERNALS michael@0: #include "jinclude.h" michael@0: #include "jpeglib.h" michael@0: #include "jsimd.h" michael@0: #include "jpegcomp.h" michael@0: michael@0: michael@0: /* Pointer to routine to upsample a single component */ michael@0: typedef JMETHOD(void, upsample1_ptr, michael@0: (j_decompress_ptr cinfo, jpeg_component_info * compptr, michael@0: JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)); michael@0: michael@0: /* Private subobject */ michael@0: michael@0: typedef struct { michael@0: struct jpeg_upsampler pub; /* public fields */ michael@0: michael@0: /* Color conversion buffer. When using separate upsampling and color michael@0: * conversion steps, this buffer holds one upsampled row group until it michael@0: * has been color converted and output. michael@0: * Note: we do not allocate any storage for component(s) which are full-size, michael@0: * ie do not need rescaling. The corresponding entry of color_buf[] is michael@0: * simply set to point to the input data array, thereby avoiding copying. michael@0: */ michael@0: JSAMPARRAY color_buf[MAX_COMPONENTS]; michael@0: michael@0: /* Per-component upsampling method pointers */ michael@0: upsample1_ptr methods[MAX_COMPONENTS]; michael@0: michael@0: int next_row_out; /* counts rows emitted from color_buf */ michael@0: JDIMENSION rows_to_go; /* counts rows remaining in image */ michael@0: michael@0: /* Height of an input row group for each component. */ michael@0: int rowgroup_height[MAX_COMPONENTS]; michael@0: michael@0: /* These arrays save pixel expansion factors so that int_expand need not michael@0: * recompute them each time. They are unused for other upsampling methods. michael@0: */ michael@0: UINT8 h_expand[MAX_COMPONENTS]; michael@0: UINT8 v_expand[MAX_COMPONENTS]; michael@0: } my_upsampler; michael@0: michael@0: typedef my_upsampler * my_upsample_ptr; michael@0: michael@0: michael@0: /* michael@0: * Initialize for an upsampling pass. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: start_pass_upsample (j_decompress_ptr cinfo) michael@0: { michael@0: my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; michael@0: michael@0: /* Mark the conversion buffer empty */ michael@0: upsample->next_row_out = cinfo->max_v_samp_factor; michael@0: /* Initialize total-height counter for detecting bottom of image */ michael@0: upsample->rows_to_go = cinfo->output_height; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Control routine to do upsampling (and color conversion). michael@0: * michael@0: * In this version we upsample each component independently. michael@0: * We upsample one row group into the conversion buffer, then apply michael@0: * color conversion a row at a time. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: sep_upsample (j_decompress_ptr cinfo, michael@0: JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, michael@0: JDIMENSION in_row_groups_avail, michael@0: JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, michael@0: JDIMENSION out_rows_avail) michael@0: { michael@0: my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; michael@0: int ci; michael@0: jpeg_component_info * compptr; michael@0: JDIMENSION num_rows; michael@0: michael@0: /* Fill the conversion buffer, if it's empty */ michael@0: if (upsample->next_row_out >= cinfo->max_v_samp_factor) { michael@0: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; michael@0: ci++, compptr++) { michael@0: /* Invoke per-component upsample method. Notice we pass a POINTER michael@0: * to color_buf[ci], so that fullsize_upsample can change it. michael@0: */ michael@0: (*upsample->methods[ci]) (cinfo, compptr, michael@0: input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]), michael@0: upsample->color_buf + ci); michael@0: } michael@0: upsample->next_row_out = 0; michael@0: } michael@0: michael@0: /* Color-convert and emit rows */ michael@0: michael@0: /* How many we have in the buffer: */ michael@0: num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out); michael@0: /* Not more than the distance to the end of the image. Need this test michael@0: * in case the image height is not a multiple of max_v_samp_factor: michael@0: */ michael@0: if (num_rows > upsample->rows_to_go) michael@0: num_rows = upsample->rows_to_go; michael@0: /* And not more than what the client can accept: */ michael@0: out_rows_avail -= *out_row_ctr; michael@0: if (num_rows > out_rows_avail) michael@0: num_rows = out_rows_avail; michael@0: michael@0: (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf, michael@0: (JDIMENSION) upsample->next_row_out, michael@0: output_buf + *out_row_ctr, michael@0: (int) num_rows); michael@0: michael@0: /* Adjust counts */ michael@0: *out_row_ctr += num_rows; michael@0: upsample->rows_to_go -= num_rows; michael@0: upsample->next_row_out += num_rows; michael@0: /* When the buffer is emptied, declare this input row group consumed */ michael@0: if (upsample->next_row_out >= cinfo->max_v_samp_factor) michael@0: (*in_row_group_ctr)++; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * These are the routines invoked by sep_upsample to upsample pixel values michael@0: * of a single component. One row group is processed per call. michael@0: */ michael@0: michael@0: michael@0: /* michael@0: * For full-size components, we just make color_buf[ci] point at the michael@0: * input buffer, and thus avoid copying any data. Note that this is michael@0: * safe only because sep_upsample doesn't declare the input row group michael@0: * "consumed" until we are done color converting and emitting it. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, michael@0: JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) michael@0: { michael@0: *output_data_ptr = input_data; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * This is a no-op version used for "uninteresting" components. michael@0: * These components will not be referenced by color conversion. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, michael@0: JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) michael@0: { michael@0: *output_data_ptr = NULL; /* safety check */ michael@0: } michael@0: michael@0: michael@0: /* michael@0: * This version handles any integral sampling ratios. michael@0: * This is not used for typical JPEG files, so it need not be fast. michael@0: * Nor, for that matter, is it particularly accurate: the algorithm is michael@0: * simple replication of the input pixel onto the corresponding output michael@0: * pixels. The hi-falutin sampling literature refers to this as a michael@0: * "box filter". A box filter tends to introduce visible artifacts, michael@0: * so if you are actually going to use 3:1 or 4:1 sampling ratios michael@0: * you would be well advised to improve this code. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, michael@0: JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) michael@0: { michael@0: my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; michael@0: JSAMPARRAY output_data = *output_data_ptr; michael@0: register JSAMPROW inptr, outptr; michael@0: register JSAMPLE invalue; michael@0: register int h; michael@0: JSAMPROW outend; michael@0: int h_expand, v_expand; michael@0: int inrow, outrow; michael@0: michael@0: h_expand = upsample->h_expand[compptr->component_index]; michael@0: v_expand = upsample->v_expand[compptr->component_index]; michael@0: michael@0: inrow = outrow = 0; michael@0: while (outrow < cinfo->max_v_samp_factor) { michael@0: /* Generate one output row with proper horizontal expansion */ michael@0: inptr = input_data[inrow]; michael@0: outptr = output_data[outrow]; michael@0: outend = outptr + cinfo->output_width; michael@0: while (outptr < outend) { michael@0: invalue = *inptr++; /* don't need GETJSAMPLE() here */ michael@0: for (h = h_expand; h > 0; h--) { michael@0: *outptr++ = invalue; michael@0: } michael@0: } michael@0: /* Generate any additional output rows by duplicating the first one */ michael@0: if (v_expand > 1) { michael@0: jcopy_sample_rows(output_data, outrow, output_data, outrow+1, michael@0: v_expand-1, cinfo->output_width); michael@0: } michael@0: inrow++; michael@0: outrow += v_expand; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Fast processing for the common case of 2:1 horizontal and 1:1 vertical. michael@0: * It's still a box filter. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, michael@0: JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) michael@0: { michael@0: JSAMPARRAY output_data = *output_data_ptr; michael@0: register JSAMPROW inptr, outptr; michael@0: register JSAMPLE invalue; michael@0: JSAMPROW outend; michael@0: int inrow; michael@0: michael@0: for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) { michael@0: inptr = input_data[inrow]; michael@0: outptr = output_data[inrow]; michael@0: outend = outptr + cinfo->output_width; michael@0: while (outptr < outend) { michael@0: invalue = *inptr++; /* don't need GETJSAMPLE() here */ michael@0: *outptr++ = invalue; michael@0: *outptr++ = invalue; michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Fast processing for the common case of 2:1 horizontal and 2:1 vertical. michael@0: * It's still a box filter. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, michael@0: JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) michael@0: { michael@0: JSAMPARRAY output_data = *output_data_ptr; michael@0: register JSAMPROW inptr, outptr; michael@0: register JSAMPLE invalue; michael@0: JSAMPROW outend; michael@0: int inrow, outrow; michael@0: michael@0: inrow = outrow = 0; michael@0: while (outrow < cinfo->max_v_samp_factor) { michael@0: inptr = input_data[inrow]; michael@0: outptr = output_data[outrow]; michael@0: outend = outptr + cinfo->output_width; michael@0: while (outptr < outend) { michael@0: invalue = *inptr++; /* don't need GETJSAMPLE() here */ michael@0: *outptr++ = invalue; michael@0: *outptr++ = invalue; michael@0: } michael@0: jcopy_sample_rows(output_data, outrow, output_data, outrow+1, michael@0: 1, cinfo->output_width); michael@0: inrow++; michael@0: outrow += 2; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical. michael@0: * michael@0: * The upsampling algorithm is linear interpolation between pixel centers, michael@0: * also known as a "triangle filter". This is a good compromise between michael@0: * speed and visual quality. The centers of the output pixels are 1/4 and 3/4 michael@0: * of the way between input pixel centers. michael@0: * michael@0: * A note about the "bias" calculations: when rounding fractional values to michael@0: * integer, we do not want to always round 0.5 up to the next integer. michael@0: * If we did that, we'd introduce a noticeable bias towards larger values. michael@0: * Instead, this code is arranged so that 0.5 will be rounded up or down at michael@0: * alternate pixel locations (a simple ordered dither pattern). michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, michael@0: JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) michael@0: { michael@0: JSAMPARRAY output_data = *output_data_ptr; michael@0: register JSAMPROW inptr, outptr; michael@0: register int invalue; michael@0: register JDIMENSION colctr; michael@0: int inrow; michael@0: michael@0: for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) { michael@0: inptr = input_data[inrow]; michael@0: outptr = output_data[inrow]; michael@0: /* Special case for first column */ michael@0: invalue = GETJSAMPLE(*inptr++); michael@0: *outptr++ = (JSAMPLE) invalue; michael@0: *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2); michael@0: michael@0: for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) { michael@0: /* General case: 3/4 * nearer pixel + 1/4 * further pixel */ michael@0: invalue = GETJSAMPLE(*inptr++) * 3; michael@0: *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2); michael@0: *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2); michael@0: } michael@0: michael@0: /* Special case for last column */ michael@0: invalue = GETJSAMPLE(*inptr); michael@0: *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2); michael@0: *outptr++ = (JSAMPLE) invalue; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical. michael@0: * Again a triangle filter; see comments for h2v1 case, above. michael@0: * michael@0: * It is OK for us to reference the adjacent input rows because we demanded michael@0: * context from the main buffer controller (see initialization code). michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr, michael@0: JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr) michael@0: { michael@0: JSAMPARRAY output_data = *output_data_ptr; michael@0: register JSAMPROW inptr0, inptr1, outptr; michael@0: #if BITS_IN_JSAMPLE == 8 michael@0: register int thiscolsum, lastcolsum, nextcolsum; michael@0: #else michael@0: register INT32 thiscolsum, lastcolsum, nextcolsum; michael@0: #endif michael@0: register JDIMENSION colctr; michael@0: int inrow, outrow, v; michael@0: michael@0: inrow = outrow = 0; michael@0: while (outrow < cinfo->max_v_samp_factor) { michael@0: for (v = 0; v < 2; v++) { michael@0: /* inptr0 points to nearest input row, inptr1 points to next nearest */ michael@0: inptr0 = input_data[inrow]; michael@0: if (v == 0) /* next nearest is row above */ michael@0: inptr1 = input_data[inrow-1]; michael@0: else /* next nearest is row below */ michael@0: inptr1 = input_data[inrow+1]; michael@0: outptr = output_data[outrow++]; michael@0: michael@0: /* Special case for first column */ michael@0: thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++); michael@0: nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++); michael@0: *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4); michael@0: *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4); michael@0: lastcolsum = thiscolsum; thiscolsum = nextcolsum; michael@0: michael@0: for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) { michael@0: /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */ michael@0: /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */ michael@0: nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++); michael@0: *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4); michael@0: *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4); michael@0: lastcolsum = thiscolsum; thiscolsum = nextcolsum; michael@0: } michael@0: michael@0: /* Special case for last column */ michael@0: *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4); michael@0: *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4); michael@0: } michael@0: inrow++; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Module initialization routine for upsampling. michael@0: */ michael@0: michael@0: GLOBAL(void) michael@0: jinit_upsampler (j_decompress_ptr cinfo) michael@0: { michael@0: my_upsample_ptr upsample; michael@0: int ci; michael@0: jpeg_component_info * compptr; michael@0: boolean need_buffer, do_fancy; michael@0: int h_in_group, v_in_group, h_out_group, v_out_group; michael@0: michael@0: upsample = (my_upsample_ptr) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: SIZEOF(my_upsampler)); michael@0: cinfo->upsample = (struct jpeg_upsampler *) upsample; michael@0: upsample->pub.start_pass = start_pass_upsample; michael@0: upsample->pub.upsample = sep_upsample; michael@0: upsample->pub.need_context_rows = FALSE; /* until we find out differently */ michael@0: michael@0: if (cinfo->CCIR601_sampling) /* this isn't supported */ michael@0: ERREXIT(cinfo, JERR_CCIR601_NOTIMPL); michael@0: michael@0: /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1, michael@0: * so don't ask for it. michael@0: */ michael@0: do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1; michael@0: michael@0: /* Verify we can handle the sampling factors, select per-component methods, michael@0: * and create storage as needed. michael@0: */ michael@0: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; michael@0: ci++, compptr++) { michael@0: /* Compute size of an "input group" after IDCT scaling. This many samples michael@0: * are to be converted to max_h_samp_factor * max_v_samp_factor pixels. michael@0: */ michael@0: h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) / michael@0: cinfo->_min_DCT_scaled_size; michael@0: v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) / michael@0: cinfo->_min_DCT_scaled_size; michael@0: h_out_group = cinfo->max_h_samp_factor; michael@0: v_out_group = cinfo->max_v_samp_factor; michael@0: upsample->rowgroup_height[ci] = v_in_group; /* save for use later */ michael@0: need_buffer = TRUE; michael@0: if (! compptr->component_needed) { michael@0: /* Don't bother to upsample an uninteresting component. */ michael@0: upsample->methods[ci] = noop_upsample; michael@0: need_buffer = FALSE; michael@0: } else if (h_in_group == h_out_group && v_in_group == v_out_group) { michael@0: /* Fullsize components can be processed without any work. */ michael@0: upsample->methods[ci] = fullsize_upsample; michael@0: need_buffer = FALSE; michael@0: } else if (h_in_group * 2 == h_out_group && michael@0: v_in_group == v_out_group) { michael@0: /* Special cases for 2h1v upsampling */ michael@0: if (do_fancy && compptr->downsampled_width > 2) { michael@0: if (jsimd_can_h2v1_fancy_upsample()) michael@0: upsample->methods[ci] = jsimd_h2v1_fancy_upsample; michael@0: else michael@0: upsample->methods[ci] = h2v1_fancy_upsample; michael@0: } else { michael@0: if (jsimd_can_h2v1_upsample()) michael@0: upsample->methods[ci] = jsimd_h2v1_upsample; michael@0: else michael@0: upsample->methods[ci] = h2v1_upsample; michael@0: } michael@0: } else if (h_in_group * 2 == h_out_group && michael@0: v_in_group * 2 == v_out_group) { michael@0: /* Special cases for 2h2v upsampling */ michael@0: if (do_fancy && compptr->downsampled_width > 2) { michael@0: if (jsimd_can_h2v2_fancy_upsample()) michael@0: upsample->methods[ci] = jsimd_h2v2_fancy_upsample; michael@0: else michael@0: upsample->methods[ci] = h2v2_fancy_upsample; michael@0: upsample->pub.need_context_rows = TRUE; michael@0: } else { michael@0: if (jsimd_can_h2v2_upsample()) michael@0: upsample->methods[ci] = jsimd_h2v2_upsample; michael@0: else michael@0: upsample->methods[ci] = h2v2_upsample; michael@0: } michael@0: } else if ((h_out_group % h_in_group) == 0 && michael@0: (v_out_group % v_in_group) == 0) { michael@0: /* Generic integral-factors upsampling method */ michael@0: upsample->methods[ci] = int_upsample; michael@0: upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group); michael@0: upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group); michael@0: } else michael@0: ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL); michael@0: if (need_buffer) { michael@0: upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray) michael@0: ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: (JDIMENSION) jround_up((long) cinfo->output_width, michael@0: (long) cinfo->max_h_samp_factor), michael@0: (JDIMENSION) cinfo->max_v_samp_factor); michael@0: } michael@0: } michael@0: }