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