Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(718)

Side by Side Diff: third_party/libjpeg_turbo/jdsample.c

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

Powered by Google App Engine
This is Rietveld 408576698