a6bde33c174da8990188017f90d98e57d6b70888
[reactos.git] / reactos / dll / 3rdparty / libjpeg / jdmerge.c
1 /*
2 * jdmerge.c
3 *
4 * Copyright (C) 1994-1996, Thomas G. Lane.
5 * Modified 2013 by Guido Vollbeding.
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 code for merged upsampling/color conversion.
10 *
11 * This file combines functions from jdsample.c and jdcolor.c;
12 * read those files first to understand what's going on.
13 *
14 * When the chroma components are to be upsampled by simple replication
15 * (ie, box filtering), we can save some work in color conversion by
16 * calculating all the output pixels corresponding to a pair of chroma
17 * samples at one time. In the conversion equations
18 * R = Y + K1 * Cr
19 * G = Y + K2 * Cb + K3 * Cr
20 * B = Y + K4 * Cb
21 * only the Y term varies among the group of pixels corresponding to a pair
22 * of chroma samples, so the rest of the terms can be calculated just once.
23 * At typical sampling ratios, this eliminates half or three-quarters of the
24 * multiplications needed for color conversion.
25 *
26 * This file currently provides implementations for the following cases:
27 * YCbCr => RGB color conversion only.
28 * Sampling ratios of 2h1v or 2h2v.
29 * No scaling needed at upsample time.
30 * Corner-aligned (non-CCIR601) sampling alignment.
31 * Other special cases could be added, but in most applications these are
32 * the only common cases. (For uncommon cases we fall back on the more
33 * general code in jdsample.c and jdcolor.c.)
34 */
35
36 #define JPEG_INTERNALS
37 #include "jinclude.h"
38 #include "jpeglib.h"
39
40 #ifdef UPSAMPLE_MERGING_SUPPORTED
41
42
43 /* Private subobject */
44
45 typedef struct {
46 struct jpeg_upsampler pub; /* public fields */
47
48 /* Pointer to routine to do actual upsampling/conversion of one row group */
49 JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
50 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
51 JSAMPARRAY output_buf));
52
53 /* Private state for YCC->RGB conversion */
54 int * Cr_r_tab; /* => table for Cr to R conversion */
55 int * Cb_b_tab; /* => table for Cb to B conversion */
56 INT32 * Cr_g_tab; /* => table for Cr to G conversion */
57 INT32 * Cb_g_tab; /* => table for Cb to G conversion */
58
59 /* For 2:1 vertical sampling, we produce two output rows at a time.
60 * We need a "spare" row buffer to hold the second output row if the
61 * application provides just a one-row buffer; we also use the spare
62 * to discard the dummy last row if the image height is odd.
63 */
64 JSAMPROW spare_row;
65 boolean spare_full; /* T if spare buffer is occupied */
66
67 JDIMENSION out_row_width; /* samples per output row */
68 JDIMENSION rows_to_go; /* counts rows remaining in image */
69 } my_upsampler;
70
71 typedef my_upsampler * my_upsample_ptr;
72
73 #define SCALEBITS 16 /* speediest right-shift on some machines */
74 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
75 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
76
77
78 /*
79 * Initialize tables for YCC->RGB colorspace conversion.
80 * This is taken directly from jdcolor.c; see that file for more info.
81 */
82
83 LOCAL(void)
84 build_ycc_rgb_table (j_decompress_ptr cinfo)
85 {
86 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
87 int i;
88 INT32 x;
89 SHIFT_TEMPS
90
91 upsample->Cr_r_tab = (int *)
92 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
93 (MAXJSAMPLE+1) * SIZEOF(int));
94 upsample->Cb_b_tab = (int *)
95 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
96 (MAXJSAMPLE+1) * SIZEOF(int));
97 upsample->Cr_g_tab = (INT32 *)
98 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
99 (MAXJSAMPLE+1) * SIZEOF(INT32));
100 upsample->Cb_g_tab = (INT32 *)
101 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
102 (MAXJSAMPLE+1) * SIZEOF(INT32));
103
104 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
105 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
106 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
107 /* Cr=>R value is nearest int to 1.402 * x */
108 upsample->Cr_r_tab[i] = (int)
109 RIGHT_SHIFT(FIX(1.402) * x + ONE_HALF, SCALEBITS);
110 /* Cb=>B value is nearest int to 1.772 * x */
111 upsample->Cb_b_tab[i] = (int)
112 RIGHT_SHIFT(FIX(1.772) * x + ONE_HALF, SCALEBITS);
113 /* Cr=>G value is scaled-up -0.714136286 * x */
114 upsample->Cr_g_tab[i] = (- FIX(0.714136286)) * x;
115 /* Cb=>G value is scaled-up -0.344136286 * x */
116 /* We also add in ONE_HALF so that need not do it in inner loop */
117 upsample->Cb_g_tab[i] = (- FIX(0.344136286)) * x + ONE_HALF;
118 }
119 }
120
121
122 /*
123 * Initialize for an upsampling pass.
124 */
125
126 METHODDEF(void)
127 start_pass_merged_upsample (j_decompress_ptr cinfo)
128 {
129 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
130
131 /* Mark the spare buffer empty */
132 upsample->spare_full = FALSE;
133 /* Initialize total-height counter for detecting bottom of image */
134 upsample->rows_to_go = cinfo->output_height;
135 }
136
137
138 /*
139 * Control routine to do upsampling (and color conversion).
140 *
141 * The control routine just handles the row buffering considerations.
142 */
143
144 METHODDEF(void)
145 merged_2v_upsample (j_decompress_ptr cinfo,
146 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
147 JDIMENSION in_row_groups_avail,
148 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
149 JDIMENSION out_rows_avail)
150 /* 2:1 vertical sampling case: may need a spare row. */
151 {
152 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
153 JSAMPROW work_ptrs[2];
154 JDIMENSION num_rows; /* number of rows returned to caller */
155
156 if (upsample->spare_full) {
157 /* If we have a spare row saved from a previous cycle, just return it. */
158 jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
159 1, upsample->out_row_width);
160 num_rows = 1;
161 upsample->spare_full = FALSE;
162 } else {
163 /* Figure number of rows to return to caller. */
164 num_rows = 2;
165 /* Not more than the distance to the end of the image. */
166 if (num_rows > upsample->rows_to_go)
167 num_rows = upsample->rows_to_go;
168 /* And not more than what the client can accept: */
169 out_rows_avail -= *out_row_ctr;
170 if (num_rows > out_rows_avail)
171 num_rows = out_rows_avail;
172 /* Create output pointer array for upsampler. */
173 work_ptrs[0] = output_buf[*out_row_ctr];
174 if (num_rows > 1) {
175 work_ptrs[1] = output_buf[*out_row_ctr + 1];
176 } else {
177 work_ptrs[1] = upsample->spare_row;
178 upsample->spare_full = TRUE;
179 }
180 /* Now do the upsampling. */
181 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
182 }
183
184 /* Adjust counts */
185 *out_row_ctr += num_rows;
186 upsample->rows_to_go -= num_rows;
187 /* When the buffer is emptied, declare this input row group consumed */
188 if (! upsample->spare_full)
189 (*in_row_group_ctr)++;
190 }
191
192
193 METHODDEF(void)
194 merged_1v_upsample (j_decompress_ptr cinfo,
195 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
196 JDIMENSION in_row_groups_avail,
197 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
198 JDIMENSION out_rows_avail)
199 /* 1:1 vertical sampling case: much easier, never need a spare row. */
200 {
201 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
202
203 /* Just do the upsampling. */
204 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
205 output_buf + *out_row_ctr);
206 /* Adjust counts */
207 (*out_row_ctr)++;
208 (*in_row_group_ctr)++;
209 }
210
211
212 /*
213 * These are the routines invoked by the control routines to do
214 * the actual upsampling/conversion. One row group is processed per call.
215 *
216 * Note: since we may be writing directly into application-supplied buffers,
217 * we have to be honest about the output width; we can't assume the buffer
218 * has been rounded up to an even width.
219 */
220
221
222 /*
223 * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
224 */
225
226 METHODDEF(void)
227 h2v1_merged_upsample (j_decompress_ptr cinfo,
228 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
229 JSAMPARRAY output_buf)
230 {
231 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
232 register int y, cred, cgreen, cblue;
233 int cb, cr;
234 register JSAMPROW outptr;
235 JSAMPROW inptr0, inptr1, inptr2;
236 JDIMENSION col;
237 /* copy these pointers into registers if possible */
238 register JSAMPLE * range_limit = cinfo->sample_range_limit;
239 int * Crrtab = upsample->Cr_r_tab;
240 int * Cbbtab = upsample->Cb_b_tab;
241 INT32 * Crgtab = upsample->Cr_g_tab;
242 INT32 * Cbgtab = upsample->Cb_g_tab;
243 SHIFT_TEMPS
244
245 inptr0 = input_buf[0][in_row_group_ctr];
246 inptr1 = input_buf[1][in_row_group_ctr];
247 inptr2 = input_buf[2][in_row_group_ctr];
248 outptr = output_buf[0];
249 /* Loop for each pair of output pixels */
250 for (col = cinfo->output_width >> 1; col > 0; col--) {
251 /* Do the chroma part of the calculation */
252 cb = GETJSAMPLE(*inptr1++);
253 cr = GETJSAMPLE(*inptr2++);
254 cred = Crrtab[cr];
255 cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
256 cblue = Cbbtab[cb];
257 /* Fetch 2 Y values and emit 2 pixels */
258 y = GETJSAMPLE(*inptr0++);
259 outptr[RGB_RED] = range_limit[y + cred];
260 outptr[RGB_GREEN] = range_limit[y + cgreen];
261 outptr[RGB_BLUE] = range_limit[y + cblue];
262 outptr += RGB_PIXELSIZE;
263 y = GETJSAMPLE(*inptr0++);
264 outptr[RGB_RED] = range_limit[y + cred];
265 outptr[RGB_GREEN] = range_limit[y + cgreen];
266 outptr[RGB_BLUE] = range_limit[y + cblue];
267 outptr += RGB_PIXELSIZE;
268 }
269 /* If image width is odd, do the last output column separately */
270 if (cinfo->output_width & 1) {
271 cb = GETJSAMPLE(*inptr1);
272 cr = GETJSAMPLE(*inptr2);
273 cred = Crrtab[cr];
274 cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
275 cblue = Cbbtab[cb];
276 y = GETJSAMPLE(*inptr0);
277 outptr[RGB_RED] = range_limit[y + cred];
278 outptr[RGB_GREEN] = range_limit[y + cgreen];
279 outptr[RGB_BLUE] = range_limit[y + cblue];
280 }
281 }
282
283
284 /*
285 * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
286 */
287
288 METHODDEF(void)
289 h2v2_merged_upsample (j_decompress_ptr cinfo,
290 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
291 JSAMPARRAY output_buf)
292 {
293 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
294 register int y, cred, cgreen, cblue;
295 int cb, cr;
296 register JSAMPROW outptr0, outptr1;
297 JSAMPROW inptr00, inptr01, inptr1, inptr2;
298 JDIMENSION col;
299 /* copy these pointers into registers if possible */
300 register JSAMPLE * range_limit = cinfo->sample_range_limit;
301 int * Crrtab = upsample->Cr_r_tab;
302 int * Cbbtab = upsample->Cb_b_tab;
303 INT32 * Crgtab = upsample->Cr_g_tab;
304 INT32 * Cbgtab = upsample->Cb_g_tab;
305 SHIFT_TEMPS
306
307 inptr00 = input_buf[0][in_row_group_ctr*2];
308 inptr01 = input_buf[0][in_row_group_ctr*2 + 1];
309 inptr1 = input_buf[1][in_row_group_ctr];
310 inptr2 = input_buf[2][in_row_group_ctr];
311 outptr0 = output_buf[0];
312 outptr1 = output_buf[1];
313 /* Loop for each group of output pixels */
314 for (col = cinfo->output_width >> 1; col > 0; col--) {
315 /* Do the chroma part of the calculation */
316 cb = GETJSAMPLE(*inptr1++);
317 cr = GETJSAMPLE(*inptr2++);
318 cred = Crrtab[cr];
319 cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
320 cblue = Cbbtab[cb];
321 /* Fetch 4 Y values and emit 4 pixels */
322 y = GETJSAMPLE(*inptr00++);
323 outptr0[RGB_RED] = range_limit[y + cred];
324 outptr0[RGB_GREEN] = range_limit[y + cgreen];
325 outptr0[RGB_BLUE] = range_limit[y + cblue];
326 outptr0 += RGB_PIXELSIZE;
327 y = GETJSAMPLE(*inptr00++);
328 outptr0[RGB_RED] = range_limit[y + cred];
329 outptr0[RGB_GREEN] = range_limit[y + cgreen];
330 outptr0[RGB_BLUE] = range_limit[y + cblue];
331 outptr0 += RGB_PIXELSIZE;
332 y = GETJSAMPLE(*inptr01++);
333 outptr1[RGB_RED] = range_limit[y + cred];
334 outptr1[RGB_GREEN] = range_limit[y + cgreen];
335 outptr1[RGB_BLUE] = range_limit[y + cblue];
336 outptr1 += RGB_PIXELSIZE;
337 y = GETJSAMPLE(*inptr01++);
338 outptr1[RGB_RED] = range_limit[y + cred];
339 outptr1[RGB_GREEN] = range_limit[y + cgreen];
340 outptr1[RGB_BLUE] = range_limit[y + cblue];
341 outptr1 += RGB_PIXELSIZE;
342 }
343 /* If image width is odd, do the last output column separately */
344 if (cinfo->output_width & 1) {
345 cb = GETJSAMPLE(*inptr1);
346 cr = GETJSAMPLE(*inptr2);
347 cred = Crrtab[cr];
348 cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
349 cblue = Cbbtab[cb];
350 y = GETJSAMPLE(*inptr00);
351 outptr0[RGB_RED] = range_limit[y + cred];
352 outptr0[RGB_GREEN] = range_limit[y + cgreen];
353 outptr0[RGB_BLUE] = range_limit[y + cblue];
354 y = GETJSAMPLE(*inptr01);
355 outptr1[RGB_RED] = range_limit[y + cred];
356 outptr1[RGB_GREEN] = range_limit[y + cgreen];
357 outptr1[RGB_BLUE] = range_limit[y + cblue];
358 }
359 }
360
361
362 /*
363 * Module initialization routine for merged upsampling/color conversion.
364 *
365 * NB: this is called under the conditions determined by use_merged_upsample()
366 * in jdmaster.c. That routine MUST correspond to the actual capabilities
367 * of this module; no safety checks are made here.
368 */
369
370 GLOBAL(void)
371 jinit_merged_upsampler (j_decompress_ptr cinfo)
372 {
373 my_upsample_ptr upsample;
374
375 upsample = (my_upsample_ptr)
376 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
377 SIZEOF(my_upsampler));
378 cinfo->upsample = (struct jpeg_upsampler *) upsample;
379 upsample->pub.start_pass = start_pass_merged_upsample;
380 upsample->pub.need_context_rows = FALSE;
381
382 upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
383
384 if (cinfo->max_v_samp_factor == 2) {
385 upsample->pub.upsample = merged_2v_upsample;
386 upsample->upmethod = h2v2_merged_upsample;
387 /* Allocate a spare row buffer */
388 upsample->spare_row = (JSAMPROW)
389 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
390 (size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));
391 } else {
392 upsample->pub.upsample = merged_1v_upsample;
393 upsample->upmethod = h2v1_merged_upsample;
394 /* No spare row needed */
395 upsample->spare_row = NULL;
396 }
397
398 build_ycc_rgb_table(cinfo);
399 }
400
401 #endif /* UPSAMPLE_MERGING_SUPPORTED */