/src/libjpeg-turbo.main/jdsample.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * jdsample.c  | 
3  |  |  *  | 
4  |  |  * This file was part of the Independent JPEG Group's software:  | 
5  |  |  * Copyright (C) 1991-1996, Thomas G. Lane.  | 
6  |  |  * libjpeg-turbo Modifications:  | 
7  |  |  * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB  | 
8  |  |  * Copyright (C) 2010, 2015-2016, D. R. Commander.  | 
9  |  |  * Copyright (C) 2014, MIPS Technologies, Inc., California.  | 
10  |  |  * Copyright (C) 2015, Google, Inc.  | 
11  |  |  * Copyright (C) 2019-2020, Arm Limited.  | 
12  |  |  * For conditions of distribution and use, see the accompanying README.ijg  | 
13  |  |  * file.  | 
14  |  |  *  | 
15  |  |  * This file contains upsampling routines.  | 
16  |  |  *  | 
17  |  |  * Upsampling input data is counted in "row groups".  A row group  | 
18  |  |  * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)  | 
19  |  |  * sample rows of each component.  Upsampling will normally produce  | 
20  |  |  * max_v_samp_factor pixel rows from each row group (but this could vary  | 
21  |  |  * if the upsampler is applying a scale factor of its own).  | 
22  |  |  *  | 
23  |  |  * An excellent reference for image resampling is  | 
24  |  |  *   Digital Image Warping, George Wolberg, 1990.  | 
25  |  |  *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.  | 
26  |  |  */  | 
27  |  |  | 
28  |  | #include "jinclude.h"  | 
29  |  | #include "jdsample.h"  | 
30  |  | #include "jsimd.h"  | 
31  |  | #include "jpegcomp.h"  | 
32  |  |  | 
33  |  |  | 
34  |  |  | 
35  |  | /*  | 
36  |  |  * Initialize for an upsampling pass.  | 
37  |  |  */  | 
38  |  |  | 
39  |  | METHODDEF(void)  | 
40  |  | start_pass_upsample(j_decompress_ptr cinfo)  | 
41  | 9.26k  | { | 
42  | 9.26k  |   my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;  | 
43  |  |  | 
44  |  |   /* Mark the conversion buffer empty */  | 
45  | 9.26k  |   upsample->next_row_out = cinfo->max_v_samp_factor;  | 
46  |  |   /* Initialize total-height counter for detecting bottom of image */  | 
47  | 9.26k  |   upsample->rows_to_go = cinfo->output_height;  | 
48  | 9.26k  | }  | 
49  |  |  | 
50  |  |  | 
51  |  | /*  | 
52  |  |  * Control routine to do upsampling (and color conversion).  | 
53  |  |  *  | 
54  |  |  * In this version we upsample each component independently.  | 
55  |  |  * We upsample one row group into the conversion buffer, then apply  | 
56  |  |  * color conversion a row at a time.  | 
57  |  |  */  | 
58  |  |  | 
59  |  | METHODDEF(void)  | 
60  |  | sep_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,  | 
61  |  |              JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail,  | 
62  |  |              JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,  | 
63  |  |              JDIMENSION out_rows_avail)  | 
64  | 5.21M  | { | 
65  | 5.21M  |   my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;  | 
66  | 5.21M  |   int ci;  | 
67  | 5.21M  |   jpeg_component_info *compptr;  | 
68  | 5.21M  |   JDIMENSION num_rows;  | 
69  |  |  | 
70  |  |   /* Fill the conversion buffer, if it's empty */  | 
71  | 5.21M  |   if (upsample->next_row_out >= cinfo->max_v_samp_factor) { | 
72  | 18.0M  |     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;  | 
73  | 12.7M  |          ci++, compptr++) { | 
74  |  |       /* Invoke per-component upsample method.  Notice we pass a POINTER  | 
75  |  |        * to color_buf[ci], so that fullsize_upsample can change it.  | 
76  |  |        */  | 
77  | 12.7M  |       (*upsample->methods[ci]) (cinfo, compptr,  | 
78  | 12.7M  |         input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),  | 
79  | 12.7M  |         upsample->color_buf + ci);  | 
80  | 12.7M  |     }  | 
81  | 5.21M  |     upsample->next_row_out = 0;  | 
82  | 5.21M  |   }  | 
83  |  |  | 
84  |  |   /* Color-convert and emit rows */  | 
85  |  |  | 
86  |  |   /* How many we have in the buffer: */  | 
87  | 5.21M  |   num_rows = (JDIMENSION)(cinfo->max_v_samp_factor - upsample->next_row_out);  | 
88  |  |   /* Not more than the distance to the end of the image.  Need this test  | 
89  |  |    * in case the image height is not a multiple of max_v_samp_factor:  | 
90  |  |    */  | 
91  | 5.21M  |   if (num_rows > upsample->rows_to_go)  | 
92  | 3.45k  |     num_rows = upsample->rows_to_go;  | 
93  |  |   /* And not more than what the client can accept: */  | 
94  | 5.21M  |   out_rows_avail -= *out_row_ctr;  | 
95  | 5.21M  |   if (num_rows > out_rows_avail)  | 
96  | 0  |     num_rows = out_rows_avail;  | 
97  |  |  | 
98  | 5.21M  |   (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,  | 
99  | 5.21M  |                                      (JDIMENSION)upsample->next_row_out,  | 
100  | 5.21M  |                                      output_buf + *out_row_ctr, (int)num_rows);  | 
101  |  |  | 
102  |  |   /* Adjust counts */  | 
103  | 5.21M  |   *out_row_ctr += num_rows;  | 
104  | 5.21M  |   upsample->rows_to_go -= num_rows;  | 
105  | 5.21M  |   upsample->next_row_out += num_rows;  | 
106  |  |   /* When the buffer is emptied, declare this input row group consumed */  | 
107  | 5.21M  |   if (upsample->next_row_out >= cinfo->max_v_samp_factor)  | 
108  | 5.21M  |     (*in_row_group_ctr)++;  | 
109  | 5.21M  | }  | 
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  |  |  | 
125  |  | METHODDEF(void)  | 
126  |  | fullsize_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,  | 
127  |  |                   JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)  | 
128  | 4.08M  | { | 
129  | 4.08M  |   *output_data_ptr = input_data;  | 
130  | 4.08M  | }  | 
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  |  |  | 
138  |  | METHODDEF(void)  | 
139  |  | noop_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,  | 
140  |  |               JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)  | 
141  | 4.27M  | { | 
142  | 4.27M  |   *output_data_ptr = NULL;      /* safety check */  | 
143  | 4.27M  | }  | 
144  |  |  | 
145  |  |  | 
146  |  | /*  | 
147  |  |  * This version handles any integral sampling ratios.  | 
148  |  |  * This is not used for typical JPEG files, so it need not be fast.  | 
149  |  |  * 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.  | 
155  |  |  */  | 
156  |  |  | 
157  |  | METHODDEF(void)  | 
158  |  | int_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,  | 
159  |  |              JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)  | 
160  | 2.67M  | { | 
161  | 2.67M  |   my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;  | 
162  | 2.67M  |   JSAMPARRAY output_data = *output_data_ptr;  | 
163  | 2.67M  |   register JSAMPROW inptr, outptr;  | 
164  | 2.67M  |   register JSAMPLE invalue;  | 
165  | 2.67M  |   register int h;  | 
166  | 2.67M  |   JSAMPROW outend;  | 
167  | 2.67M  |   int h_expand, v_expand;  | 
168  | 2.67M  |   int inrow, outrow;  | 
169  |  |  | 
170  | 2.67M  |   h_expand = upsample->h_expand[compptr->component_index];  | 
171  | 2.67M  |   v_expand = upsample->v_expand[compptr->component_index];  | 
172  |  |  | 
173  | 2.67M  |   inrow = outrow = 0;  | 
174  | 6.11M  |   while (outrow < cinfo->max_v_samp_factor) { | 
175  |  |     /* Generate one output row with proper horizontal expansion */  | 
176  | 3.43M  |     inptr = input_data[inrow];  | 
177  | 3.43M  |     outptr = output_data[outrow];  | 
178  | 3.43M  |     outend = outptr + cinfo->output_width;  | 
179  | 55.8M  |     while (outptr < outend) { | 
180  | 52.3M  |       invalue = *inptr++;  | 
181  | 137M  |       for (h = h_expand; h > 0; h--) { | 
182  | 84.7M  |         *outptr++ = invalue;  | 
183  | 84.7M  |       }  | 
184  | 52.3M  |     }  | 
185  |  |     /* Generate any additional output rows by duplicating the first one */  | 
186  | 3.43M  |     if (v_expand > 1) { | 
187  | 2.52M  |       jcopy_sample_rows(output_data, outrow, output_data, outrow + 1,  | 
188  | 2.52M  |                         v_expand - 1, cinfo->output_width);  | 
189  | 2.52M  |     }  | 
190  | 3.43M  |     inrow++;  | 
191  | 3.43M  |     outrow += v_expand;  | 
192  | 3.43M  |   }  | 
193  | 2.67M  | }  | 
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  |  |  | 
201  |  | METHODDEF(void)  | 
202  |  | h2v1_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,  | 
203  |  |               JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)  | 
204  | 0  | { | 
205  | 0  |   JSAMPARRAY output_data = *output_data_ptr;  | 
206  | 0  |   register JSAMPROW inptr, outptr;  | 
207  | 0  |   register JSAMPLE invalue;  | 
208  | 0  |   JSAMPROW outend;  | 
209  | 0  |   int inrow;  | 
210  |  | 
  | 
211  | 0  |   for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) { | 
212  | 0  |     inptr = input_data[inrow];  | 
213  | 0  |     outptr = output_data[inrow];  | 
214  | 0  |     outend = outptr + cinfo->output_width;  | 
215  | 0  |     while (outptr < outend) { | 
216  | 0  |       invalue = *inptr++;  | 
217  | 0  |       *outptr++ = invalue;  | 
218  | 0  |       *outptr++ = invalue;  | 
219  | 0  |     }  | 
220  | 0  |   }  | 
221  | 0  | }  | 
222  |  |  | 
223  |  |  | 
224  |  | /*  | 
225  |  |  * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.  | 
226  |  |  * It's still a box filter.  | 
227  |  |  */  | 
228  |  |  | 
229  |  | METHODDEF(void)  | 
230  |  | h2v2_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,  | 
231  |  |               JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)  | 
232  | 0  | { | 
233  | 0  |   JSAMPARRAY output_data = *output_data_ptr;  | 
234  | 0  |   register JSAMPROW inptr, outptr;  | 
235  | 0  |   register JSAMPLE invalue;  | 
236  | 0  |   JSAMPROW outend;  | 
237  | 0  |   int inrow, outrow;  | 
238  |  | 
  | 
239  | 0  |   inrow = outrow = 0;  | 
240  | 0  |   while (outrow < cinfo->max_v_samp_factor) { | 
241  | 0  |     inptr = input_data[inrow];  | 
242  | 0  |     outptr = output_data[outrow];  | 
243  | 0  |     outend = outptr + cinfo->output_width;  | 
244  | 0  |     while (outptr < outend) { | 
245  | 0  |       invalue = *inptr++;  | 
246  | 0  |       *outptr++ = invalue;  | 
247  | 0  |       *outptr++ = invalue;  | 
248  | 0  |     }  | 
249  | 0  |     jcopy_sample_rows(output_data, outrow, output_data, outrow + 1, 1,  | 
250  | 0  |                       cinfo->output_width);  | 
251  | 0  |     inrow++;  | 
252  | 0  |     outrow += 2;  | 
253  | 0  |   }  | 
254  | 0  | }  | 
255  |  |  | 
256  |  |  | 
257  |  | /*  | 
258  |  |  * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.  | 
259  |  |  *  | 
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.  | 
264  |  |  *  | 
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).  | 
270  |  |  */  | 
271  |  |  | 
272  |  | METHODDEF(void)  | 
273  |  | h2v1_fancy_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,  | 
274  |  |                     JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)  | 
275  | 0  | { | 
276  | 0  |   JSAMPARRAY output_data = *output_data_ptr;  | 
277  | 0  |   register JSAMPROW inptr, outptr;  | 
278  | 0  |   register int invalue;  | 
279  | 0  |   register JDIMENSION colctr;  | 
280  | 0  |   int inrow;  | 
281  |  | 
  | 
282  | 0  |   for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) { | 
283  | 0  |     inptr = input_data[inrow];  | 
284  | 0  |     outptr = output_data[inrow];  | 
285  |  |     /* Special case for first column */  | 
286  | 0  |     invalue = *inptr++;  | 
287  | 0  |     *outptr++ = (JSAMPLE)invalue;  | 
288  | 0  |     *outptr++ = (JSAMPLE)((invalue * 3 + inptr[0] + 2) >> 2);  | 
289  |  | 
  | 
290  | 0  |     for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) { | 
291  |  |       /* General case: 3/4 * nearer pixel + 1/4 * further pixel */  | 
292  | 0  |       invalue = (*inptr++) * 3;  | 
293  | 0  |       *outptr++ = (JSAMPLE)((invalue + inptr[-2] + 1) >> 2);  | 
294  | 0  |       *outptr++ = (JSAMPLE)((invalue + inptr[0] + 2) >> 2);  | 
295  | 0  |     }  | 
296  |  |  | 
297  |  |     /* Special case for last column */  | 
298  | 0  |     invalue = *inptr;  | 
299  | 0  |     *outptr++ = (JSAMPLE)((invalue * 3 + inptr[-1] + 1) >> 2);  | 
300  | 0  |     *outptr++ = (JSAMPLE)invalue;  | 
301  | 0  |   }  | 
302  | 0  | }  | 
303  |  |  | 
304  |  |  | 
305  |  | /*  | 
306  |  |  * Fancy processing for 1:1 horizontal and 2:1 vertical (4:4:0 subsampling).  | 
307  |  |  *  | 
308  |  |  * This is a less common case, but it can be encountered when losslessly  | 
309  |  |  * rotating/transposing a JPEG file that uses 4:2:2 chroma subsampling.  | 
310  |  |  */  | 
311  |  |  | 
312  |  | METHODDEF(void)  | 
313  |  | h1v2_fancy_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,  | 
314  |  |                     JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)  | 
315  | 417k  | { | 
316  | 417k  |   JSAMPARRAY output_data = *output_data_ptr;  | 
317  | 417k  |   JSAMPROW inptr0, inptr1, outptr;  | 
318  | 417k  | #if BITS_IN_JSAMPLE == 8  | 
319  | 417k  |   int thiscolsum, bias;  | 
320  |  | #else  | 
321  |  |   JLONG thiscolsum, bias;  | 
322  |  | #endif  | 
323  | 417k  |   JDIMENSION colctr;  | 
324  | 417k  |   int inrow, outrow, v;  | 
325  |  |  | 
326  | 417k  |   inrow = outrow = 0;  | 
327  | 1.10M  |   while (outrow < cinfo->max_v_samp_factor) { | 
328  | 2.05M  |     for (v = 0; v < 2; v++) { | 
329  |  |       /* inptr0 points to nearest input row, inptr1 points to next nearest */  | 
330  | 1.37M  |       inptr0 = input_data[inrow];  | 
331  | 1.37M  |       if (v == 0) {             /* next nearest is row above */ | 
332  | 686k  |         inptr1 = input_data[inrow - 1];  | 
333  | 686k  |         bias = 1;  | 
334  | 686k  |       } else {                  /* next nearest is row below */ | 
335  | 686k  |         inptr1 = input_data[inrow + 1];  | 
336  | 686k  |         bias = 2;  | 
337  | 686k  |       }  | 
338  | 1.37M  |       outptr = output_data[outrow++];  | 
339  |  |  | 
340  | 22.3M  |       for (colctr = 0; colctr < compptr->downsampled_width; colctr++) { | 
341  | 20.9M  |         thiscolsum = (*inptr0++) * 3 + (*inptr1++);  | 
342  | 20.9M  |         *outptr++ = (JSAMPLE)((thiscolsum + bias) >> 2);  | 
343  | 20.9M  |       }  | 
344  | 1.37M  |     }  | 
345  | 686k  |     inrow++;  | 
346  | 686k  |   }  | 
347  | 417k  | }  | 
348  |  |  | 
349  |  |  | 
350  |  | /*  | 
351  |  |  * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.  | 
352  |  |  * Again a triangle filter; see comments for h2v1 case, above.  | 
353  |  |  *  | 
354  |  |  * It is OK for us to reference the adjacent input rows because we demanded  | 
355  |  |  * context from the main buffer controller (see initialization code).  | 
356  |  |  */  | 
357  |  |  | 
358  |  | METHODDEF(void)  | 
359  |  | h2v2_fancy_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,  | 
360  |  |                     JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)  | 
361  | 0  | { | 
362  | 0  |   JSAMPARRAY output_data = *output_data_ptr;  | 
363  | 0  |   register JSAMPROW inptr0, inptr1, outptr;  | 
364  | 0  | #if BITS_IN_JSAMPLE == 8  | 
365  | 0  |   register int thiscolsum, lastcolsum, nextcolsum;  | 
366  |  | #else  | 
367  |  |   register JLONG thiscolsum, lastcolsum, nextcolsum;  | 
368  |  | #endif  | 
369  | 0  |   register JDIMENSION colctr;  | 
370  | 0  |   int inrow, outrow, v;  | 
371  |  | 
  | 
372  | 0  |   inrow = outrow = 0;  | 
373  | 0  |   while (outrow < cinfo->max_v_samp_factor) { | 
374  | 0  |     for (v = 0; v < 2; v++) { | 
375  |  |       /* inptr0 points to nearest input row, inptr1 points to next nearest */  | 
376  | 0  |       inptr0 = input_data[inrow];  | 
377  | 0  |       if (v == 0)               /* next nearest is row above */  | 
378  | 0  |         inptr1 = input_data[inrow - 1];  | 
379  | 0  |       else                      /* next nearest is row below */  | 
380  | 0  |         inptr1 = input_data[inrow + 1];  | 
381  | 0  |       outptr = output_data[outrow++];  | 
382  |  |  | 
383  |  |       /* Special case for first column */  | 
384  | 0  |       thiscolsum = (*inptr0++) * 3 + (*inptr1++);  | 
385  | 0  |       nextcolsum = (*inptr0++) * 3 + (*inptr1++);  | 
386  | 0  |       *outptr++ = (JSAMPLE)((thiscolsum * 4 + 8) >> 4);  | 
387  | 0  |       *outptr++ = (JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);  | 
388  | 0  |       lastcolsum = thiscolsum;  thiscolsum = nextcolsum;  | 
389  |  | 
  | 
390  | 0  |       for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) { | 
391  |  |         /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */  | 
392  |  |         /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */  | 
393  | 0  |         nextcolsum = (*inptr0++) * 3 + (*inptr1++);  | 
394  | 0  |         *outptr++ = (JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);  | 
395  | 0  |         *outptr++ = (JSAMPLE)((thiscolsum * 3 + nextcolsum + 7) >> 4);  | 
396  | 0  |         lastcolsum = thiscolsum;  thiscolsum = nextcolsum;  | 
397  | 0  |       }  | 
398  |  |  | 
399  |  |       /* Special case for last column */  | 
400  | 0  |       *outptr++ = (JSAMPLE)((thiscolsum * 3 + lastcolsum + 8) >> 4);  | 
401  | 0  |       *outptr++ = (JSAMPLE)((thiscolsum * 4 + 7) >> 4);  | 
402  | 0  |     }  | 
403  | 0  |     inrow++;  | 
404  | 0  |   }  | 
405  | 0  | }  | 
406  |  |  | 
407  |  |  | 
408  |  | /*  | 
409  |  |  * Module initialization routine for upsampling.  | 
410  |  |  */  | 
411  |  |  | 
412  |  | GLOBAL(void)  | 
413  |  | jinit_upsampler(j_decompress_ptr cinfo)  | 
414  | 25.6k  | { | 
415  | 25.6k  |   my_upsample_ptr upsample;  | 
416  | 25.6k  |   int ci;  | 
417  | 25.6k  |   jpeg_component_info *compptr;  | 
418  | 25.6k  |   boolean need_buffer, do_fancy;  | 
419  | 25.6k  |   int h_in_group, v_in_group, h_out_group, v_out_group;  | 
420  |  |  | 
421  | 25.6k  |   if (!cinfo->master->jinit_upsampler_no_alloc) { | 
422  | 25.6k  |     upsample = (my_upsample_ptr)  | 
423  | 25.6k  |       (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,  | 
424  | 25.6k  |                                   sizeof(my_upsampler));  | 
425  | 25.6k  |     cinfo->upsample = (struct jpeg_upsampler *)upsample;  | 
426  | 25.6k  |     upsample->pub.start_pass = start_pass_upsample;  | 
427  | 25.6k  |     upsample->pub.upsample = sep_upsample;  | 
428  | 25.6k  |     upsample->pub.need_context_rows = FALSE; /* until we find out differently */  | 
429  | 25.6k  |   } else  | 
430  | 0  |     upsample = (my_upsample_ptr)cinfo->upsample;  | 
431  |  |  | 
432  | 25.6k  |   if (cinfo->CCIR601_sampling)  /* this isn't supported */  | 
433  | 0  |     ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);  | 
434  |  |  | 
435  |  |   /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,  | 
436  |  |    * so don't ask for it.  | 
437  |  |    */  | 
438  | 25.6k  |   do_fancy = cinfo->do_fancy_upsampling && cinfo->_min_DCT_scaled_size > 1;  | 
439  |  |  | 
440  |  |   /* Verify we can handle the sampling factors, select per-component methods,  | 
441  |  |    * and create storage as needed.  | 
442  |  |    */  | 
443  | 99.6k  |   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;  | 
444  | 74.0k  |        ci++, compptr++) { | 
445  |  |     /* Compute size of an "input group" after IDCT scaling.  This many samples  | 
446  |  |      * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.  | 
447  |  |      */  | 
448  | 74.0k  |     h_in_group = (compptr->h_samp_factor * compptr->_DCT_scaled_size) /  | 
449  | 74.0k  |                  cinfo->_min_DCT_scaled_size;  | 
450  | 74.0k  |     v_in_group = (compptr->v_samp_factor * compptr->_DCT_scaled_size) /  | 
451  | 74.0k  |                  cinfo->_min_DCT_scaled_size;  | 
452  | 74.0k  |     h_out_group = cinfo->max_h_samp_factor;  | 
453  | 74.0k  |     v_out_group = cinfo->max_v_samp_factor;  | 
454  | 74.0k  |     upsample->rowgroup_height[ci] = v_in_group; /* save for use later */  | 
455  | 74.0k  |     need_buffer = TRUE;  | 
456  | 74.0k  |     if (!compptr->component_needed) { | 
457  |  |       /* Don't bother to upsample an uninteresting component. */  | 
458  | 16.2k  |       upsample->methods[ci] = noop_upsample;  | 
459  | 16.2k  |       need_buffer = FALSE;  | 
460  | 57.7k  |     } else if (h_in_group == h_out_group && v_in_group == v_out_group) { | 
461  |  |       /* Fullsize components can be processed without any work. */  | 
462  | 11.3k  |       upsample->methods[ci] = fullsize_upsample;  | 
463  | 11.3k  |       need_buffer = FALSE;  | 
464  | 46.4k  |     } else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) { | 
465  |  |       /* Special cases for 2h1v upsampling */  | 
466  | 10.6k  |       if (do_fancy && compptr->downsampled_width > 2) { | 
467  | 5.35k  |         if (jsimd_can_h2v1_fancy_upsample())  | 
468  | 5.35k  |           upsample->methods[ci] = jsimd_h2v1_fancy_upsample;  | 
469  | 0  |         else  | 
470  | 0  |           upsample->methods[ci] = h2v1_fancy_upsample;  | 
471  | 5.35k  |       } else { | 
472  | 5.25k  |         if (jsimd_can_h2v1_upsample())  | 
473  | 5.25k  |           upsample->methods[ci] = jsimd_h2v1_upsample;  | 
474  | 0  |         else  | 
475  | 0  |           upsample->methods[ci] = h2v1_upsample;  | 
476  | 5.25k  |       }  | 
477  | 35.8k  |     } else if (h_in_group == h_out_group &&  | 
478  | 35.8k  |                v_in_group * 2 == v_out_group && do_fancy) { | 
479  |  |       /* Non-fancy upsampling is handled by the generic method */  | 
480  |  | #if defined(__arm__) || defined(__aarch64__) || \  | 
481  |  |     defined(_M_ARM) || defined(_M_ARM64)  | 
482  |  |       if (jsimd_can_h1v2_fancy_upsample())  | 
483  |  |         upsample->methods[ci] = jsimd_h1v2_fancy_upsample;  | 
484  |  |       else  | 
485  |  | #endif  | 
486  | 6.21k  |         upsample->methods[ci] = h1v2_fancy_upsample;  | 
487  | 6.21k  |       upsample->pub.need_context_rows = TRUE;  | 
488  | 29.6k  |     } else if (h_in_group * 2 == h_out_group &&  | 
489  | 29.6k  |                v_in_group * 2 == v_out_group) { | 
490  |  |       /* Special cases for 2h2v upsampling */  | 
491  | 765  |       if (do_fancy && compptr->downsampled_width > 2) { | 
492  | 379  |         if (jsimd_can_h2v2_fancy_upsample())  | 
493  | 379  |           upsample->methods[ci] = jsimd_h2v2_fancy_upsample;  | 
494  | 0  |         else  | 
495  | 0  |           upsample->methods[ci] = h2v2_fancy_upsample;  | 
496  | 379  |         upsample->pub.need_context_rows = TRUE;  | 
497  | 386  |       } else { | 
498  | 386  |         if (jsimd_can_h2v2_upsample())  | 
499  | 386  |           upsample->methods[ci] = jsimd_h2v2_upsample;  | 
500  | 0  |         else  | 
501  | 0  |           upsample->methods[ci] = h2v2_upsample;  | 
502  | 386  |       }  | 
503  | 28.8k  |     } else if ((h_out_group % h_in_group) == 0 &&  | 
504  | 28.8k  |                (v_out_group % v_in_group) == 0) { | 
505  |  |       /* Generic integral-factors upsampling method */  | 
506  |  | #if defined(__mips__)  | 
507  |  |       if (jsimd_can_int_upsample())  | 
508  |  |         upsample->methods[ci] = jsimd_int_upsample;  | 
509  |  |       else  | 
510  |  | #endif  | 
511  | 24.5k  |         upsample->methods[ci] = int_upsample;  | 
512  | 24.5k  |       upsample->h_expand[ci] = (UINT8)(h_out_group / h_in_group);  | 
513  | 24.5k  |       upsample->v_expand[ci] = (UINT8)(v_out_group / v_in_group);  | 
514  | 24.5k  |     } else  | 
515  | 4.36k  |       ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);  | 
516  | 74.0k  |     if (need_buffer && !cinfo->master->jinit_upsampler_no_alloc) { | 
517  | 42.0k  |       upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)  | 
518  | 42.0k  |         ((j_common_ptr)cinfo, JPOOL_IMAGE,  | 
519  | 42.0k  |          (JDIMENSION)jround_up((long)cinfo->output_width,  | 
520  | 42.0k  |                                (long)cinfo->max_h_samp_factor),  | 
521  | 42.0k  |          (JDIMENSION)cinfo->max_v_samp_factor);  | 
522  | 42.0k  |     }  | 
523  | 74.0k  |   }  | 
524  | 25.6k  | }  |