blob: 0eb56b7a5a1be84eeb54e7fe62b4fc4ffd069de7 [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
DRCce0dd942016-02-06 12:18:44 -06008 * Copyright (C) 2010, 2015-2016, 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
DRCce0dd942016-02-06 12:18:44 -060026#include "jinclude.h"
DRCeb32cc12015-06-25 03:44:36 +000027#include "jdsample.h"
Pierre Ossman59a39382009-03-09 13:15:56 +000028#include "jsimd.h"
DRC36a6eec2010-10-08 08:05:44 +000029#include "jpegcomp.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000030
31
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000032
33/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000034 * Initialize for an upsampling pass.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000035 */
36
Thomas G. Lane489583f1996-02-07 00:00:00 +000037METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000038start_pass_upsample (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000039{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000040 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
41
42 /* Mark the conversion buffer empty */
43 upsample->next_row_out = cinfo->max_v_samp_factor;
44 /* Initialize total-height counter for detecting bottom of image */
45 upsample->rows_to_go = cinfo->output_height;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000046}
47
48
49/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000050 * Control routine to do upsampling (and color conversion).
Thomas G. Lane88aeed41992-12-10 00:00:00 +000051 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000052 * In this version we upsample each component independently.
53 * We upsample one row group into the conversion buffer, then apply
54 * color conversion a row at a time.
55 */
56
Thomas G. Lane489583f1996-02-07 00:00:00 +000057METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000058sep_upsample (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +000059 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
60 JDIMENSION in_row_groups_avail,
61 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
62 JDIMENSION out_rows_avail)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000063{
64 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
65 int ci;
DRCbd498032016-02-19 08:53:33 -060066 jpeg_component_info *compptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000067 JDIMENSION num_rows;
68
69 /* Fill the conversion buffer, if it's empty */
70 if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
71 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
DRCe5eaf372014-05-09 18:00:32 +000072 ci++, compptr++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000073 /* Invoke per-component upsample method. Notice we pass a POINTER
74 * to color_buf[ci], so that fullsize_upsample can change it.
75 */
76 (*upsample->methods[ci]) (cinfo, compptr,
DRCe5eaf372014-05-09 18:00:32 +000077 input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
78 upsample->color_buf + ci);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000079 }
80 upsample->next_row_out = 0;
81 }
82
83 /* Color-convert and emit rows */
84
85 /* How many we have in the buffer: */
86 num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
87 /* Not more than the distance to the end of the image. Need this test
88 * in case the image height is not a multiple of max_v_samp_factor:
89 */
DRCe5eaf372014-05-09 18:00:32 +000090 if (num_rows > upsample->rows_to_go)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000091 num_rows = upsample->rows_to_go;
92 /* And not more than what the client can accept: */
93 out_rows_avail -= *out_row_ctr;
94 if (num_rows > out_rows_avail)
95 num_rows = out_rows_avail;
96
97 (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
DRCe5eaf372014-05-09 18:00:32 +000098 (JDIMENSION) upsample->next_row_out,
99 output_buf + *out_row_ctr,
100 (int) num_rows);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000101
102 /* Adjust counts */
103 *out_row_ctr += num_rows;
104 upsample->rows_to_go -= num_rows;
105 upsample->next_row_out += num_rows;
106 /* When the buffer is emptied, declare this input row group consumed */
107 if (upsample->next_row_out >= cinfo->max_v_samp_factor)
108 (*in_row_group_ctr)++;
109}
110
111
112/*
113 * These are the routines invoked by sep_upsample to upsample pixel values
114 * of a single component. One row group is processed per call.
115 */
116
117
118/*
119 * For full-size components, we just make color_buf[ci] point at the
120 * input buffer, and thus avoid copying any data. Note that this is
121 * safe only because sep_upsample doesn't declare the input row group
122 * "consumed" until we are done color converting and emitting it.
123 */
124
Thomas G. Lane489583f1996-02-07 00:00:00 +0000125METHODDEF(void)
DRCbd498032016-02-19 08:53:33 -0600126fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info *compptr,
127 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000128{
129 *output_data_ptr = input_data;
130}
131
132
133/*
134 * This is a no-op version used for "uninteresting" components.
135 * These components will not be referenced by color conversion.
136 */
137
Thomas G. Lane489583f1996-02-07 00:00:00 +0000138METHODDEF(void)
DRCbd498032016-02-19 08:53:33 -0600139noop_upsample (j_decompress_ptr cinfo, jpeg_component_info *compptr,
140 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000141{
DRCe5eaf372014-05-09 18:00:32 +0000142 *output_data_ptr = NULL; /* safety check */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000143}
144
145
146/*
147 * This version handles any integral sampling ratios.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000148 * This is not used for typical JPEG files, so it need not be fast.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000149 * Nor, for that matter, is it particularly accurate: the algorithm is
150 * simple replication of the input pixel onto the corresponding output
151 * pixels. The hi-falutin sampling literature refers to this as a
152 * "box filter". A box filter tends to introduce visible artifacts,
153 * so if you are actually going to use 3:1 or 4:1 sampling ratios
154 * you would be well advised to improve this code.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000155 */
156
Thomas G. Lane489583f1996-02-07 00:00:00 +0000157METHODDEF(void)
DRCbd498032016-02-19 08:53:33 -0600158int_upsample (j_decompress_ptr cinfo, jpeg_component_info *compptr,
159 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000160{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000161 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
162 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000163 register JSAMPROW inptr, outptr;
164 register JSAMPLE invalue;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000165 register int h;
166 JSAMPROW outend;
167 int h_expand, v_expand;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000168 int inrow, outrow;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000169
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000170 h_expand = upsample->h_expand[compptr->component_index];
171 v_expand = upsample->v_expand[compptr->component_index];
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000172
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000173 inrow = outrow = 0;
174 while (outrow < cinfo->max_v_samp_factor) {
175 /* Generate one output row with proper horizontal expansion */
176 inptr = input_data[inrow];
177 outptr = output_data[outrow];
178 outend = outptr + cinfo->output_width;
179 while (outptr < outend) {
DRCe5eaf372014-05-09 18:00:32 +0000180 invalue = *inptr++; /* don't need GETJSAMPLE() here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000181 for (h = h_expand; h > 0; h--) {
DRCe5eaf372014-05-09 18:00:32 +0000182 *outptr++ = invalue;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000183 }
184 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000185 /* Generate any additional output rows by duplicating the first one */
186 if (v_expand > 1) {
187 jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
DRCe5eaf372014-05-09 18:00:32 +0000188 v_expand-1, cinfo->output_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000189 }
190 inrow++;
191 outrow += v_expand;
192 }
193}
194
195
196/*
197 * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
198 * It's still a box filter.
199 */
200
Thomas G. Lane489583f1996-02-07 00:00:00 +0000201METHODDEF(void)
DRCbd498032016-02-19 08:53:33 -0600202h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info *compptr,
203 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000204{
205 JSAMPARRAY output_data = *output_data_ptr;
206 register JSAMPROW inptr, outptr;
207 register JSAMPLE invalue;
208 JSAMPROW outend;
209 int inrow;
210
211 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
212 inptr = input_data[inrow];
213 outptr = output_data[inrow];
214 outend = outptr + cinfo->output_width;
215 while (outptr < outend) {
DRCe5eaf372014-05-09 18:00:32 +0000216 invalue = *inptr++; /* don't need GETJSAMPLE() here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000217 *outptr++ = invalue;
218 *outptr++ = invalue;
219 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000220 }
221}
222
223
224/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000225 * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
226 * It's still a box filter.
227 */
228
Thomas G. Lane489583f1996-02-07 00:00:00 +0000229METHODDEF(void)
DRCbd498032016-02-19 08:53:33 -0600230h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info *compptr,
231 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000232{
233 JSAMPARRAY output_data = *output_data_ptr;
234 register JSAMPROW inptr, outptr;
235 register JSAMPLE invalue;
236 JSAMPROW outend;
237 int inrow, outrow;
238
239 inrow = outrow = 0;
240 while (outrow < cinfo->max_v_samp_factor) {
241 inptr = input_data[inrow];
242 outptr = output_data[outrow];
243 outend = outptr + cinfo->output_width;
244 while (outptr < outend) {
DRCe5eaf372014-05-09 18:00:32 +0000245 invalue = *inptr++; /* don't need GETJSAMPLE() here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000246 *outptr++ = invalue;
247 *outptr++ = invalue;
248 }
249 jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
DRCe5eaf372014-05-09 18:00:32 +0000250 1, cinfo->output_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000251 inrow++;
252 outrow += 2;
253 }
254}
255
256
257/*
258 * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000259 *
260 * The upsampling algorithm is linear interpolation between pixel centers,
261 * also known as a "triangle filter". This is a good compromise between
262 * speed and visual quality. The centers of the output pixels are 1/4 and 3/4
263 * of the way between input pixel centers.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000264 *
265 * A note about the "bias" calculations: when rounding fractional values to
266 * integer, we do not want to always round 0.5 up to the next integer.
267 * If we did that, we'd introduce a noticeable bias towards larger values.
268 * Instead, this code is arranged so that 0.5 will be rounded up or down at
269 * alternate pixel locations (a simple ordered dither pattern).
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000270 */
271
Thomas G. Lane489583f1996-02-07 00:00:00 +0000272METHODDEF(void)
DRCbd498032016-02-19 08:53:33 -0600273h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info *compptr,
274 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000275{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000276 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000277 register JSAMPROW inptr, outptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000278 register int invalue;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000279 register JDIMENSION colctr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000280 int inrow;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000281
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000282 for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000283 inptr = input_data[inrow];
284 outptr = output_data[inrow];
285 /* Special case for first column */
286 invalue = GETJSAMPLE(*inptr++);
287 *outptr++ = (JSAMPLE) invalue;
288 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000289
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000290 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000291 /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
292 invalue = GETJSAMPLE(*inptr++) * 3;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000293 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000294 *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
295 }
296
297 /* Special case for last column */
298 invalue = GETJSAMPLE(*inptr);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000299 *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000300 *outptr++ = (JSAMPLE) invalue;
301 }
302}
303
304
305/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000306 * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
307 * Again a triangle filter; see comments for h2v1 case, above.
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000308 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000309 * It is OK for us to reference the adjacent input rows because we demanded
310 * context from the main buffer controller (see initialization code).
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000311 */
312
Thomas G. Lane489583f1996-02-07 00:00:00 +0000313METHODDEF(void)
DRCbd498032016-02-19 08:53:33 -0600314h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info *compptr,
315 JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000316{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000317 JSAMPARRAY output_data = *output_data_ptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000318 register JSAMPROW inptr0, inptr1, outptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000319#if BITS_IN_JSAMPLE == 8
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000320 register int thiscolsum, lastcolsum, nextcolsum;
321#else
DRC1e32fe32015-10-14 17:32:39 -0500322 register JLONG thiscolsum, lastcolsum, nextcolsum;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000323#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000324 register JDIMENSION colctr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000325 int inrow, outrow, v;
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000326
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000327 inrow = outrow = 0;
328 while (outrow < cinfo->max_v_samp_factor) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000329 for (v = 0; v < 2; v++) {
330 /* inptr0 points to nearest input row, inptr1 points to next nearest */
331 inptr0 = input_data[inrow];
DRCe5eaf372014-05-09 18:00:32 +0000332 if (v == 0) /* next nearest is row above */
333 inptr1 = input_data[inrow-1];
334 else /* next nearest is row below */
335 inptr1 = input_data[inrow+1];
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000336 outptr = output_data[outrow++];
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000337
338 /* Special case for first column */
339 thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
340 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
341 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000342 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000343 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
344
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000345 for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
DRCe5eaf372014-05-09 18:00:32 +0000346 /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
347 /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
348 nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
349 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
350 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
351 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000352 }
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000353
354 /* Special case for last column */
355 *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000356 *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000357 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000358 inrow++;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000359 }
360}
361
362
363/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000364 * Module initialization routine for upsampling.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000365 */
366
Thomas G. Lane489583f1996-02-07 00:00:00 +0000367GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000368jinit_upsampler (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000369{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000370 my_upsample_ptr upsample;
371 int ci;
DRCbd498032016-02-19 08:53:33 -0600372 jpeg_component_info *compptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000373 boolean need_buffer, do_fancy;
374 int h_in_group, v_in_group, h_out_group, v_out_group;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000375
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000376 upsample = (my_upsample_ptr)
377 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000378 sizeof(my_upsampler));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000379 cinfo->upsample = (struct jpeg_upsampler *) upsample;
380 upsample->pub.start_pass = start_pass_upsample;
381 upsample->pub.upsample = sep_upsample;
382 upsample->pub.need_context_rows = FALSE; /* until we find out differently */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000383
DRCe5eaf372014-05-09 18:00:32 +0000384 if (cinfo->CCIR601_sampling) /* this isn't supported */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000385 ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
386
387 /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
388 * so don't ask for it.
389 */
DRC49967cd2010-10-09 19:57:51 +0000390 do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000391
392 /* Verify we can handle the sampling factors, select per-component methods,
393 * and create storage as needed.
394 */
395 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
396 ci++, compptr++) {
397 /* Compute size of an "input group" after IDCT scaling. This many samples
398 * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
399 */
DRC49967cd2010-10-09 19:57:51 +0000400 h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /
DRCe5eaf372014-05-09 18:00:32 +0000401 cinfo->_min_DCT_scaled_size;
DRC49967cd2010-10-09 19:57:51 +0000402 v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /
DRCe5eaf372014-05-09 18:00:32 +0000403 cinfo->_min_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000404 h_out_group = cinfo->max_h_samp_factor;
405 v_out_group = cinfo->max_v_samp_factor;
406 upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
407 need_buffer = TRUE;
408 if (! compptr->component_needed) {
409 /* Don't bother to upsample an uninteresting component. */
410 upsample->methods[ci] = noop_upsample;
411 need_buffer = FALSE;
412 } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
413 /* Fullsize components can be processed without any work. */
414 upsample->methods[ci] = fullsize_upsample;
415 need_buffer = FALSE;
416 } else if (h_in_group * 2 == h_out_group &&
DRCe5eaf372014-05-09 18:00:32 +0000417 v_in_group == v_out_group) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000418 /* Special cases for 2h1v upsampling */
Pierre Ossman59a39382009-03-09 13:15:56 +0000419 if (do_fancy && compptr->downsampled_width > 2) {
DRCe5eaf372014-05-09 18:00:32 +0000420 if (jsimd_can_h2v1_fancy_upsample())
421 upsample->methods[ci] = jsimd_h2v1_fancy_upsample;
422 else
423 upsample->methods[ci] = h2v1_fancy_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000424 } else {
DRCe5eaf372014-05-09 18:00:32 +0000425 if (jsimd_can_h2v1_upsample())
426 upsample->methods[ci] = jsimd_h2v1_upsample;
427 else
428 upsample->methods[ci] = h2v1_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000429 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000430 } else if (h_in_group * 2 == h_out_group &&
DRCe5eaf372014-05-09 18:00:32 +0000431 v_in_group * 2 == v_out_group) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000432 /* Special cases for 2h2v upsampling */
433 if (do_fancy && compptr->downsampled_width > 2) {
DRCe5eaf372014-05-09 18:00:32 +0000434 if (jsimd_can_h2v2_fancy_upsample())
435 upsample->methods[ci] = jsimd_h2v2_fancy_upsample;
436 else
437 upsample->methods[ci] = h2v2_fancy_upsample;
438 upsample->pub.need_context_rows = TRUE;
Pierre Ossman59a39382009-03-09 13:15:56 +0000439 } else {
DRCe5eaf372014-05-09 18:00:32 +0000440 if (jsimd_can_h2v2_upsample())
441 upsample->methods[ci] = jsimd_h2v2_upsample;
442 else
443 upsample->methods[ci] = h2v2_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000444 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000445 } else if ((h_out_group % h_in_group) == 0 &&
DRCe5eaf372014-05-09 18:00:32 +0000446 (v_out_group % v_in_group) == 0) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000447 /* Generic integral-factors upsampling method */
DRC5ef46302014-05-18 20:04:47 +0000448#if defined(__mips__)
449 if (jsimd_can_int_upsample())
450 upsample->methods[ci] = jsimd_int_upsample;
451 else
452#endif
453 upsample->methods[ci] = int_upsample;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000454 upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
455 upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
456 } else
457 ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
458 if (need_buffer) {
459 upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
DRCe5eaf372014-05-09 18:00:32 +0000460 ((j_common_ptr) cinfo, JPOOL_IMAGE,
461 (JDIMENSION) jround_up((long) cinfo->output_width,
462 (long) cinfo->max_h_samp_factor),
463 (JDIMENSION) cinfo->max_v_samp_factor);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000464 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000465 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000466}