jdk
482 строки · 16.6 Кб
1/*
2* reserved comment block
3* DO NOT REMOVE OR ALTER!
4*/
5/*
6* jdsample.c
7*
8* Copyright (C) 1991-1996, Thomas G. Lane.
9* This file is part of the Independent JPEG Group's software.
10* For conditions of distribution and use, see the accompanying README file.
11*
12* This file contains upsampling routines.
13*
14* Upsampling input data is counted in "row groups". A row group
15* is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
16* sample rows of each component. Upsampling will normally produce
17* max_v_samp_factor pixel rows from each row group (but this could vary
18* if the upsampler is applying a scale factor of its own).
19*
20* An excellent reference for image resampling is
21* Digital Image Warping, George Wolberg, 1990.
22* Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
23*/
24
25#define JPEG_INTERNALS26#include "jinclude.h"27#include "jpeglib.h"28
29
30/* Pointer to routine to upsample a single component */
31typedef JMETHOD(void, upsample1_ptr,32(j_decompress_ptr cinfo, jpeg_component_info * compptr,33JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));34
35/* Private subobject */
36
37typedef struct {38struct jpeg_upsampler pub; /* public fields */39
40/* Color conversion buffer. When using separate upsampling and color41* conversion steps, this buffer holds one upsampled row group until it
42* has been color converted and output.
43* Note: we do not allocate any storage for component(s) which are full-size,
44* ie do not need rescaling. The corresponding entry of color_buf[] is
45* simply set to point to the input data array, thereby avoiding copying.
46*/
47JSAMPARRAY color_buf[MAX_COMPONENTS];48
49/* Per-component upsampling method pointers */50upsample1_ptr methods[MAX_COMPONENTS];51
52int next_row_out; /* counts rows emitted from color_buf */53JDIMENSION rows_to_go; /* counts rows remaining in image */54
55/* Height of an input row group for each component. */56int rowgroup_height[MAX_COMPONENTS];57
58/* These arrays save pixel expansion factors so that int_expand need not59* recompute them each time. They are unused for other upsampling methods.
60*/
61UINT8 h_expand[MAX_COMPONENTS];62UINT8 v_expand[MAX_COMPONENTS];63} my_upsampler;64
65typedef my_upsampler * my_upsample_ptr;66
67
68/*
69* Initialize for an upsampling pass.
70*/
71
72METHODDEF(void)73start_pass_upsample (j_decompress_ptr cinfo)74{
75my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;76
77/* Mark the conversion buffer empty */78upsample->next_row_out = cinfo->max_v_samp_factor;79/* Initialize total-height counter for detecting bottom of image */80upsample->rows_to_go = cinfo->output_height;81}
82
83
84/*
85* Control routine to do upsampling (and color conversion).
86*
87* In this version we upsample each component independently.
88* We upsample one row group into the conversion buffer, then apply
89* color conversion a row at a time.
90*/
91
92METHODDEF(void)93sep_upsample (j_decompress_ptr cinfo,94JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,95JDIMENSION in_row_groups_avail,96JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,97JDIMENSION out_rows_avail)98{
99my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;100int ci;101jpeg_component_info * compptr;102JDIMENSION num_rows;103
104/* Fill the conversion buffer, if it's empty */105if (upsample->next_row_out >= cinfo->max_v_samp_factor) {106for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;107ci++, compptr++) {108/* Invoke per-component upsample method. Notice we pass a POINTER109* to color_buf[ci], so that fullsize_upsample can change it.
110*/
111(*upsample->methods[ci]) (cinfo, compptr,112input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),113upsample->color_buf + ci);114}115upsample->next_row_out = 0;116}117
118/* Color-convert and emit rows */119
120/* How many we have in the buffer: */121num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);122/* Not more than the distance to the end of the image. Need this test123* in case the image height is not a multiple of max_v_samp_factor:
124*/
125if (num_rows > upsample->rows_to_go)126num_rows = upsample->rows_to_go;127/* And not more than what the client can accept: */128out_rows_avail -= *out_row_ctr;129if (num_rows > out_rows_avail)130num_rows = out_rows_avail;131
132(*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,133(JDIMENSION) upsample->next_row_out,134output_buf + *out_row_ctr,135(int) num_rows);136
137/* Adjust counts */138*out_row_ctr += num_rows;139upsample->rows_to_go -= num_rows;140upsample->next_row_out += num_rows;141/* When the buffer is emptied, declare this input row group consumed */142if (upsample->next_row_out >= cinfo->max_v_samp_factor)143(*in_row_group_ctr)++;144}
145
146
147/*
148* These are the routines invoked by sep_upsample to upsample pixel values
149* of a single component. One row group is processed per call.
150*/
151
152
153/*
154* For full-size components, we just make color_buf[ci] point at the
155* input buffer, and thus avoid copying any data. Note that this is
156* safe only because sep_upsample doesn't declare the input row group
157* "consumed" until we are done color converting and emitting it.
158*/
159
160METHODDEF(void)161fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,162JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)163{
164*output_data_ptr = input_data;165}
166
167
168/*
169* This is a no-op version used for "uninteresting" components.
170* These components will not be referenced by color conversion.
171*/
172
173METHODDEF(void)174noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,175JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)176{
177*output_data_ptr = NULL; /* safety check */178}
179
180
181/*
182* This version handles any integral sampling ratios.
183* This is not used for typical JPEG files, so it need not be fast.
184* Nor, for that matter, is it particularly accurate: the algorithm is
185* simple replication of the input pixel onto the corresponding output
186* pixels. The hi-falutin sampling literature refers to this as a
187* "box filter". A box filter tends to introduce visible artifacts,
188* so if you are actually going to use 3:1 or 4:1 sampling ratios
189* you would be well advised to improve this code.
190*/
191
192METHODDEF(void)193int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,194JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)195{
196my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;197JSAMPARRAY output_data = *output_data_ptr;198register JSAMPROW inptr, outptr;199register JSAMPLE invalue;200register int h;201JSAMPROW outend;202int h_expand, v_expand;203int inrow, outrow;204
205h_expand = upsample->h_expand[compptr->component_index];206v_expand = upsample->v_expand[compptr->component_index];207
208inrow = outrow = 0;209while (outrow < cinfo->max_v_samp_factor) {210/* Generate one output row with proper horizontal expansion */211inptr = input_data[inrow];212outptr = output_data[outrow];213outend = outptr + cinfo->output_width;214while (outptr < outend) {215invalue = *inptr++; /* don't need GETJSAMPLE() here */216for (h = h_expand; h > 0; h--) {217*outptr++ = invalue;218}219}220/* Generate any additional output rows by duplicating the first one */221if (v_expand > 1) {222jcopy_sample_rows(output_data, outrow, output_data, outrow+1,223v_expand-1, cinfo->output_width);224}225inrow++;226outrow += v_expand;227}228}
229
230
231/*
232* Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
233* It's still a box filter.
234*/
235
236METHODDEF(void)237h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,238JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)239{
240JSAMPARRAY output_data = *output_data_ptr;241register JSAMPROW inptr, outptr;242register JSAMPLE invalue;243JSAMPROW outend;244int inrow;245
246for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {247inptr = input_data[inrow];248outptr = output_data[inrow];249outend = outptr + cinfo->output_width;250while (outptr < outend) {251invalue = *inptr++; /* don't need GETJSAMPLE() here */252*outptr++ = invalue;253*outptr++ = invalue;254}255}256}
257
258
259/*
260* Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
261* It's still a box filter.
262*/
263
264METHODDEF(void)265h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,266JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)267{
268JSAMPARRAY output_data = *output_data_ptr;269register JSAMPROW inptr, outptr;270register JSAMPLE invalue;271JSAMPROW outend;272int inrow, outrow;273
274inrow = outrow = 0;275while (outrow < cinfo->max_v_samp_factor) {276inptr = input_data[inrow];277outptr = output_data[outrow];278outend = outptr + cinfo->output_width;279while (outptr < outend) {280invalue = *inptr++; /* don't need GETJSAMPLE() here */281*outptr++ = invalue;282*outptr++ = invalue;283}284jcopy_sample_rows(output_data, outrow, output_data, outrow+1,2851, cinfo->output_width);286inrow++;287outrow += 2;288}289}
290
291
292/*
293* Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
294*
295* The upsampling algorithm is linear interpolation between pixel centers,
296* also known as a "triangle filter". This is a good compromise between
297* speed and visual quality. The centers of the output pixels are 1/4 and 3/4
298* of the way between input pixel centers.
299*
300* A note about the "bias" calculations: when rounding fractional values to
301* integer, we do not want to always round 0.5 up to the next integer.
302* If we did that, we'd introduce a noticeable bias towards larger values.
303* Instead, this code is arranged so that 0.5 will be rounded up or down at
304* alternate pixel locations (a simple ordered dither pattern).
305*/
306
307METHODDEF(void)308h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,309JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)310{
311JSAMPARRAY output_data = *output_data_ptr;312register JSAMPROW inptr, outptr;313register int invalue;314register JDIMENSION colctr;315int inrow;316
317for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {318inptr = input_data[inrow];319outptr = output_data[inrow];320/* Special case for first column */321invalue = GETJSAMPLE(*inptr++);322*outptr++ = (JSAMPLE) invalue;323*outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);324
325for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {326/* General case: 3/4 * nearer pixel + 1/4 * further pixel */327invalue = GETJSAMPLE(*inptr++) * 3;328*outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);329*outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);330}331
332/* Special case for last column */333invalue = GETJSAMPLE(*inptr);334*outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);335*outptr++ = (JSAMPLE) invalue;336}337}
338
339
340/*
341* Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
342* Again a triangle filter; see comments for h2v1 case, above.
343*
344* It is OK for us to reference the adjacent input rows because we demanded
345* context from the main buffer controller (see initialization code).
346*/
347
348METHODDEF(void)349h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,350JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)351{
352JSAMPARRAY output_data = *output_data_ptr;353register JSAMPROW inptr0, inptr1, outptr;354#if BITS_IN_JSAMPLE == 8355register int thiscolsum, lastcolsum, nextcolsum;356#else357register INT32 thiscolsum, lastcolsum, nextcolsum;358#endif359register JDIMENSION colctr;360int inrow, outrow, v;361
362inrow = outrow = 0;363while (outrow < cinfo->max_v_samp_factor) {364for (v = 0; v < 2; v++) {365/* inptr0 points to nearest input row, inptr1 points to next nearest */366inptr0 = input_data[inrow];367if (v == 0) /* next nearest is row above */368inptr1 = input_data[inrow-1];369else /* next nearest is row below */370inptr1 = input_data[inrow+1];371outptr = output_data[outrow++];372
373/* Special case for first column */374thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);375nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);376*outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);377*outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);378lastcolsum = thiscolsum; thiscolsum = nextcolsum;379
380for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {381/* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */382/* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */383nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);384*outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);385*outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);386lastcolsum = thiscolsum; thiscolsum = nextcolsum;387}388
389/* Special case for last column */390*outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);391*outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);392}393inrow++;394}395}
396
397
398/*
399* Module initialization routine for upsampling.
400*/
401
402GLOBAL(void)403jinit_upsampler (j_decompress_ptr cinfo)404{
405my_upsample_ptr upsample;406int ci;407jpeg_component_info * compptr;408boolean need_buffer, do_fancy;409int h_in_group, v_in_group, h_out_group, v_out_group;410
411upsample = (my_upsample_ptr)412(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,413SIZEOF(my_upsampler));414cinfo->upsample = (struct jpeg_upsampler *) upsample;415upsample->pub.start_pass = start_pass_upsample;416upsample->pub.upsample = sep_upsample;417upsample->pub.need_context_rows = FALSE; /* until we find out differently */418
419if (cinfo->CCIR601_sampling) /* this isn't supported */420ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);421
422/* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,423* so don't ask for it.
424*/
425do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1;426
427/* Verify we can handle the sampling factors, select per-component methods,428* and create storage as needed.
429*/
430for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;431ci++, compptr++) {432/* Compute size of an "input group" after IDCT scaling. This many samples433* are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
434*/
435h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) /436cinfo->min_DCT_scaled_size;437v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) /438cinfo->min_DCT_scaled_size;439h_out_group = cinfo->max_h_samp_factor;440v_out_group = cinfo->max_v_samp_factor;441upsample->rowgroup_height[ci] = v_in_group; /* save for use later */442need_buffer = TRUE;443if (! compptr->component_needed) {444/* Don't bother to upsample an uninteresting component. */445upsample->methods[ci] = noop_upsample;446need_buffer = FALSE;447} else if (h_in_group == h_out_group && v_in_group == v_out_group) {448/* Fullsize components can be processed without any work. */449upsample->methods[ci] = fullsize_upsample;450need_buffer = FALSE;451} else if (h_in_group * 2 == h_out_group &&452v_in_group == v_out_group) {453/* Special cases for 2h1v upsampling */454if (do_fancy && compptr->downsampled_width > 2)455upsample->methods[ci] = h2v1_fancy_upsample;456else457upsample->methods[ci] = h2v1_upsample;458} else if (h_in_group * 2 == h_out_group &&459v_in_group * 2 == v_out_group) {460/* Special cases for 2h2v upsampling */461if (do_fancy && compptr->downsampled_width > 2) {462upsample->methods[ci] = h2v2_fancy_upsample;463upsample->pub.need_context_rows = TRUE;464} else465upsample->methods[ci] = h2v2_upsample;466} else if ((h_out_group % h_in_group) == 0 &&467(v_out_group % v_in_group) == 0) {468/* Generic integral-factors upsampling method */469upsample->methods[ci] = int_upsample;470upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);471upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);472} else473ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);474if (need_buffer) {475upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)476((j_common_ptr) cinfo, JPOOL_IMAGE,477(JDIMENSION) jround_up((long) cinfo->output_width,478(long) cinfo->max_h_samp_factor),479(JDIMENSION) cinfo->max_v_samp_factor);480}481}482}
483