blob: e1870fcdd585f544ee77e7eafd0b80d7f76395a8 [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jdsample.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane489583f1996-02-07 00:00:00 +00005 * Copyright (C) 1991-1996, Thomas G. Lane.
DRCa6ef2822013-09-28 03:23:49 +00006 * libjpeg-turbo Modifications:
Pierre Ossman59a39382009-03-09 13:15:56 +00007 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRC36a6eec2010-10-08 08:05:44 +00008 * Copyright (C) 2010, D. R. Commander.
DRC5ef46302014-05-18 20:04:47 +00009 * Copyright (C) 2014, MIPS Technologies, Inc., California
DRC7e3acc02015-10-10 10:25:46 -050010 * For conditions of distribution and use, see the accompanying README.ijg
11 * file.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000012 *
Thomas G. Lane88aeed41992-12-10 00:00:00 +000013 * This file contains upsampling routines.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000014 *
15 * Upsampling input data is counted in "row groups". A row group
16 * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
17 * sample rows of each component. Upsampling will normally produce
18 * max_v_samp_factor pixel rows from each row group (but this could vary
19 * if the upsampler is applying a scale factor of its own).
Thomas G. Lane88aeed41992-12-10 00:00:00 +000020 *
21 * An excellent reference for image resampling is
22 * Digital Image Warping, George Wolberg, 1990.
23 * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000024 */
25
DRCeb32cc12015-06-25 03:44:36 +000026#include "jdsample.h"
Pierre Ossman59a39382009-03-09 13:15:56 +000027#include "jsimd.h"
DRC36a6eec2010-10-08 08:05:44 +000028#include "jpegcomp.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000029
30
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000031
32/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000033 * Initialize for an upsampling pass.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000034 */
35
Thomas G. Lane489583f1996-02-07 00:00:00 +000036METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000037start_pass_upsample (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000038{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000039 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
40
41 /* Mark the conversion buffer empty */
42 upsample->next_row_out = cinfo->max_v_samp_factor;
43 /* Initialize total-height counter for detecting bottom of image */
44 upsample->rows_to_go = cinfo->output_height;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000045}
46
47
48/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000049 * Control routine to do upsampling (and color conversion).
Thomas G. Lane88aeed41992-12-10 00:00:00 +000050 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000051 * In this version we upsample each component independently.
52 * We upsample one row group into the conversion buffer, then apply
53 * color conversion a row at a time.
54 */
55
Thomas G. Lane489583f1996-02-07 00:00:00 +000056METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000057sep_upsample (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +000058 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
59 JDIMENSION in_row_groups_avail,
60 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
61 JDIMENSION out_rows_avail)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000062{
63 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
64 int ci;
65 jpeg_component_info * compptr;
66 JDIMENSION num_rows;
67
68 /* Fill the conversion buffer, if it's empty */
69 if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
70 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
DRCe5eaf372014-05-09 18:00:32 +000071 ci++, compptr++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000072 /* Invoke per-component upsample method. Notice we pass a POINTER
73 * to color_buf[ci], so that fullsize_upsample can change it.
74 */
75 (*upsample->methods[ci]) (cinfo, compptr,
DRCe5eaf372014-05-09 18:00:32 +000076 input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
77 upsample->color_buf + ci);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000078 }
79 upsample->next_row_out = 0;
80 }
81
82 /* Color-convert and emit rows */
83
84 /* How many we have in the buffer: */
85 num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
86 /* Not more than the distance to the end of the image. Need this test
87 * in case the image height is not a multiple of max_v_samp_factor:
88 */
DRCe5eaf372014-05-09 18:00:32 +000089 if (num_rows > upsample->rows_to_go)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000090 num_rows = upsample->rows_to_go;
91 /* And not more than what the client can accept: */
92 out_rows_avail -= *out_row_ctr;
93 if (num_rows > out_rows_avail)
94 num_rows = out_rows_avail;
95
96 (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
DRCe5eaf372014-05-09 18:00:32 +000097 (JDIMENSION) upsample->next_row_out,
98 output_buf + *out_row_ctr,
99 (int) num_rows);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000100
101 /* Adjust counts */
102 *out_row_ctr += num_rows;
103 upsample->rows_to_go -= num_rows;
104 upsample->next_row_out += num_rows;
105 /* When the buffer is emptied, declare this input row group consumed */
106 if (upsample->next_row_out >= cinfo->max_v_samp_factor)
107 (*in_row_group_ctr)++;
108}
109
110
111/*
112 * These are the routines invoked by sep_upsample to upsample pixel values
113 * of a single component. One row group is processed per call.
114 */
115
116
117/*
118 * For full-size components, we just make color_buf[ci] point at the
119 * input buffer, and thus avoid copying any data. Note that this is
120 * safe only because sep_upsample doesn't declare the input row group
121 * "consumed" until we are done color converting and emitting it.
122 */
123
Thomas G. Lane489583f1996-02-07 00:00:00 +0000124METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000125fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000126 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000127{
128 *output_data_ptr = input_data;
129}
130
131
132/*
133 * This is a no-op version used for "uninteresting" components.
134 * These components will not be referenced by color conversion.
135 */
136
Thomas G. Lane489583f1996-02-07 00:00:00 +0000137METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000138noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000139 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000140{
DRCe5eaf372014-05-09 18:00:32 +0000141 *output_data_ptr = NULL; /* safety check */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000142}
143
144
145/*
146 * This version handles any integral sampling ratios.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000147 * This is not used for typical JPEG files, so it need not be fast.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000148 * Nor, for that matter, is it particularly accurate: the algorithm is
149 * simple replication of the input pixel onto the corresponding output
150 * pixels. The hi-falutin sampling literature refers to this as a
151 * "box filter". A box filter tends to introduce visible artifacts,
152 * so if you are actually going to use 3:1 or 4:1 sampling ratios
153 * you would be well advised to improve this code.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000154 */
155
Thomas G. Lane489583f1996-02-07 00:00:00 +0000156METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000157int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000158 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000159{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000160 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
161 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000162 register JSAMPROW inptr, outptr;
163 register JSAMPLE invalue;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000164 register int h;
165 JSAMPROW outend;
166 int h_expand, v_expand;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000167 int inrow, outrow;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000168
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000169 h_expand = upsample->h_expand[compptr->component_index];
170 v_expand = upsample->v_expand[compptr->component_index];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000171
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000172 inrow = outrow = 0;
173 while (outrow < cinfo->max_v_samp_factor) {
174 /* Generate one output row with proper horizontal expansion */
175 inptr = input_data[inrow];
176 outptr = output_data[outrow];
177 outend = outptr + cinfo->output_width;
178 while (outptr < outend) {
DRCe5eaf372014-05-09 18:00:32 +0000179 invalue = *inptr++; /* don't need GETJSAMPLE() here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000180 for (h = h_expand; h > 0; h--) {
DRCe5eaf372014-05-09 18:00:32 +0000181 *outptr++ = invalue;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000182 }
183 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000184 /* Generate any additional output rows by duplicating the first one */
185 if (v_expand > 1) {
186 jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
DRCe5eaf372014-05-09 18:00:32 +0000187 v_expand-1, cinfo->output_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000188 }
189 inrow++;
190 outrow += v_expand;
191 }
192}
193
194
195/*
196 * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
197 * It's still a box filter.
198 */
199
Thomas G. Lane489583f1996-02-07 00:00:00 +0000200METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000201h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000202 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000203{
204 JSAMPARRAY output_data = *output_data_ptr;
205 register JSAMPROW inptr, outptr;
206 register JSAMPLE invalue;
207 JSAMPROW outend;
208 int inrow;
209
210 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
211 inptr = input_data[inrow];
212 outptr = output_data[inrow];
213 outend = outptr + cinfo->output_width;
214 while (outptr < outend) {
DRCe5eaf372014-05-09 18:00:32 +0000215 invalue = *inptr++; /* don't need GETJSAMPLE() here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000216 *outptr++ = invalue;
217 *outptr++ = invalue;
218 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000219 }
220}
221
222
223/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000224 * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
225 * It's still a box filter.
226 */
227
Thomas G. Lane489583f1996-02-07 00:00:00 +0000228METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000229h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000230 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000231{
232 JSAMPARRAY output_data = *output_data_ptr;
233 register JSAMPROW inptr, outptr;
234 register JSAMPLE invalue;
235 JSAMPROW outend;
236 int inrow, outrow;
237
238 inrow = outrow = 0;
239 while (outrow < cinfo->max_v_samp_factor) {
240 inptr = input_data[inrow];
241 outptr = output_data[outrow];
242 outend = outptr + cinfo->output_width;
243 while (outptr < outend) {
DRCe5eaf372014-05-09 18:00:32 +0000244 invalue = *inptr++; /* don't need GETJSAMPLE() here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000245 *outptr++ = invalue;
246 *outptr++ = invalue;
247 }
248 jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
DRCe5eaf372014-05-09 18:00:32 +0000249 1, cinfo->output_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000250 inrow++;
251 outrow += 2;
252 }
253}
254
255
256/*
257 * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000258 *
259 * The upsampling algorithm is linear interpolation between pixel centers,
260 * also known as a "triangle filter". This is a good compromise between
261 * speed and visual quality. The centers of the output pixels are 1/4 and 3/4
262 * of the way between input pixel centers.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000263 *
264 * A note about the "bias" calculations: when rounding fractional values to
265 * integer, we do not want to always round 0.5 up to the next integer.
266 * If we did that, we'd introduce a noticeable bias towards larger values.
267 * Instead, this code is arranged so that 0.5 will be rounded up or down at
268 * alternate pixel locations (a simple ordered dither pattern).
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000269 */
270
Thomas G. Lane489583f1996-02-07 00:00:00 +0000271METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000272h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000273 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000274{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000275 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000276 register JSAMPROW inptr, outptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000277 register int invalue;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000278 register JDIMENSION colctr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000279 int inrow;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000280
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000281 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000282 inptr = input_data[inrow];
283 outptr = output_data[inrow];
284 /* Special case for first column */
285 invalue = GETJSAMPLE(*inptr++);
286 *outptr++ = (JSAMPLE) invalue;
287 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000288
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000289 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000290 /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
291 invalue = GETJSAMPLE(*inptr++) * 3;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000292 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000293 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
294 }
295
296 /* Special case for last column */
297 invalue = GETJSAMPLE(*inptr);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000298 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000299 *outptr++ = (JSAMPLE) invalue;
300 }
301}
302
303
304/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000305 * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
306 * Again a triangle filter; see comments for h2v1 case, above.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000307 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000308 * It is OK for us to reference the adjacent input rows because we demanded
309 * context from the main buffer controller (see initialization code).
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000310 */
311
Thomas G. Lane489583f1996-02-07 00:00:00 +0000312METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000313h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
DRCe5eaf372014-05-09 18:00:32 +0000314 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000315{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000316 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000317 register JSAMPROW inptr0, inptr1, outptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000318#if BITS_IN_JSAMPLE == 8
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000319 register int thiscolsum, lastcolsum, nextcolsum;
320#else
321 register INT32 thiscolsum, lastcolsum, nextcolsum;
322#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000323 register JDIMENSION colctr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000324 int inrow, outrow, v;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000325
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000326 inrow = outrow = 0;
327 while (outrow < cinfo->max_v_samp_factor) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000328 for (v = 0; v < 2; v++) {
329 /* inptr0 points to nearest input row, inptr1 points to next nearest */
330 inptr0 = input_data[inrow];
DRCe5eaf372014-05-09 18:00:32 +0000331 if (v == 0) /* next nearest is row above */
332 inptr1 = input_data[inrow-1];
333 else /* next nearest is row below */
334 inptr1 = input_data[inrow+1];
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000335 outptr = output_data[outrow++];
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000336
337 /* Special case for first column */
338 thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
339 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
340 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000341 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000342 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
343
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000344 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
DRCe5eaf372014-05-09 18:00:32 +0000345 /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
346 /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
347 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
348 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
349 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
350 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000351 }
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000352
353 /* Special case for last column */
354 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000355 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000356 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000357 inrow++;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000358 }
359}
360
361
362/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000363 * Module initialization routine for upsampling.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000364 */
365
Thomas G. Lane489583f1996-02-07 00:00:00 +0000366GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000367jinit_upsampler (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000368{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000369 my_upsample_ptr upsample;
370 int ci;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000371 jpeg_component_info * compptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000372 boolean need_buffer, do_fancy;
373 int h_in_group, v_in_group, h_out_group, v_out_group;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000374
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000375 upsample = (my_upsample_ptr)
376 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000377 sizeof(my_upsampler));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000378 cinfo->upsample = (struct jpeg_upsampler *) upsample;
379 upsample->pub.start_pass = start_pass_upsample;
380 upsample->pub.upsample = sep_upsample;
381 upsample->pub.need_context_rows = FALSE; /* until we find out differently */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000382
DRCe5eaf372014-05-09 18:00:32 +0000383 if (cinfo->CCIR601_sampling) /* this isn't supported */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000384 ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
385
386 /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
387 * so don't ask for it.
388 */
DRC49967cd2010-10-09 19:57:51 +0000389 do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000390
391 /* Verify we can handle the sampling factors, select per-component methods,
392 * and create storage as needed.
393 */
394 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
395 ci++, compptr++) {
396 /* Compute size of an "input group" after IDCT scaling. This many samples
397 * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
398 */
DRC49967cd2010-10-09 19:57:51 +0000399 h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
DRCe5eaf372014-05-09 18:00:32 +0000400 cinfo->_min_DCT_scaled_size;
DRC49967cd2010-10-09 19:57:51 +0000401 v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
DRCe5eaf372014-05-09 18:00:32 +0000402 cinfo->_min_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000403 h_out_group = cinfo->max_h_samp_factor;
404 v_out_group = cinfo->max_v_samp_factor;
405 upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
406 need_buffer = TRUE;
407 if (! compptr->component_needed) {
408 /* Don't bother to upsample an uninteresting component. */
409 upsample->methods[ci] = noop_upsample;
410 need_buffer = FALSE;
411 } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
412 /* Fullsize components can be processed without any work. */
413 upsample->methods[ci] = fullsize_upsample;
414 need_buffer = FALSE;
415 } else if (h_in_group * 2 == h_out_group &&
DRCe5eaf372014-05-09 18:00:32 +0000416 v_in_group == v_out_group) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000417 /* Special cases for 2h1v upsampling */
Pierre Ossman59a39382009-03-09 13:15:56 +0000418 if (do_fancy && compptr->downsampled_width > 2) {
DRCe5eaf372014-05-09 18:00:32 +0000419 if (jsimd_can_h2v1_fancy_upsample())
420 upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
421 else
422 upsample->methods[ci] = h2v1_fancy_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000423 } else {
DRCe5eaf372014-05-09 18:00:32 +0000424 if (jsimd_can_h2v1_upsample())
425 upsample->methods[ci] = jsimd_h2v1_upsample;
426 else
427 upsample->methods[ci] = h2v1_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000428 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000429 } else if (h_in_group * 2 == h_out_group &&
DRCe5eaf372014-05-09 18:00:32 +0000430 v_in_group * 2 == v_out_group) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000431 /* Special cases for 2h2v upsampling */
432 if (do_fancy && compptr->downsampled_width > 2) {
DRCe5eaf372014-05-09 18:00:32 +0000433 if (jsimd_can_h2v2_fancy_upsample())
434 upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
435 else
436 upsample->methods[ci] = h2v2_fancy_upsample;
437 upsample->pub.need_context_rows = TRUE;
Pierre Ossman59a39382009-03-09 13:15:56 +0000438 } else {
DRCe5eaf372014-05-09 18:00:32 +0000439 if (jsimd_can_h2v2_upsample())
440 upsample->methods[ci] = jsimd_h2v2_upsample;
441 else
442 upsample->methods[ci] = h2v2_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000443 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000444 } else if ((h_out_group % h_in_group) == 0 &&
DRCe5eaf372014-05-09 18:00:32 +0000445 (v_out_group % v_in_group) == 0) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000446 /* Generic integral-factors upsampling method */
DRC5ef46302014-05-18 20:04:47 +0000447#if defined(__mips__)
448 if (jsimd_can_int_upsample())
449 upsample->methods[ci] = jsimd_int_upsample;
450 else
451#endif
452 upsample->methods[ci] = int_upsample;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000453 upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
454 upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
455 } else
456 ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
457 if (need_buffer) {
458 upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
DRCe5eaf372014-05-09 18:00:32 +0000459 ((j_common_ptr) cinfo, JPOOL_IMAGE,
460 (JDIMENSION) jround_up((long) cinfo->output_width,
461 (long) cinfo->max_h_samp_factor),
462 (JDIMENSION) cinfo->max_v_samp_factor);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000463 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000464 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000465}