1 /*
2  * jdmerge.c
3  *
4  * Copyright (C) 1994-1996, Thomas G. Lane.
5  * Modified 2013-2017 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  *	YCC => RGB color conversion only (YCbCr or BG_YCC).
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 #if RANGE_BITS < 2
44   /* Deliberate syntax err */
45   Sorry, this code requires 2 or more range extension bits.
46 #endif
47 
48 
49 /* Private subobject */
50 
51 typedef struct {
52   struct jpeg_upsampler pub;	/* public fields */
53 
54   /* Pointer to routine to do actual upsampling/conversion of one row group */
55   JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
56 			   JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
57 			   JSAMPARRAY output_buf));
58 
59   /* Private state for YCC->RGB conversion */
60   int * Cr_r_tab;		/* => table for Cr to R conversion */
61   int * Cb_b_tab;		/* => table for Cb to B conversion */
62   INT32 * Cr_g_tab;		/* => table for Cr to G conversion */
63   INT32 * Cb_g_tab;		/* => table for Cb to G conversion */
64 
65   /* For 2:1 vertical sampling, we produce two output rows at a time.
66    * We need a "spare" row buffer to hold the second output row if the
67    * application provides just a one-row buffer; we also use the spare
68    * to discard the dummy last row if the image height is odd.
69    */
70   JSAMPROW spare_row;
71   boolean spare_full;		/* T if spare buffer is occupied */
72 
73   JDIMENSION out_row_width;	/* samples per output row */
74   JDIMENSION rows_to_go;	/* counts rows remaining in image */
75 } my_upsampler;
76 
77 typedef my_upsampler * my_upsample_ptr;
78 
79 #define SCALEBITS	16	/* speediest right-shift on some machines */
80 #define ONE_HALF	((INT32) 1 << (SCALEBITS-1))
81 #define FIX(x)		((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
82 
83 
84 /*
85  * Initialize tables for YCbCr->RGB and BG_YCC->RGB colorspace conversion.
86  * This is taken directly from jdcolor.c; see that file for more info.
87  */
88 
89 LOCAL(void)
build_ycc_rgb_table(j_decompress_ptr cinfo)90 build_ycc_rgb_table (j_decompress_ptr cinfo)
91 /* Normal case, sYCC */
92 {
93   my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
94   int i;
95   INT32 x;
96   SHIFT_TEMPS
97 
98   upsample->Cr_r_tab = (int *)
99     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
100 				(MAXJSAMPLE+1) * SIZEOF(int));
101   upsample->Cb_b_tab = (int *)
102     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
103 				(MAXJSAMPLE+1) * SIZEOF(int));
104   upsample->Cr_g_tab = (INT32 *)
105     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
106 				(MAXJSAMPLE+1) * SIZEOF(INT32));
107   upsample->Cb_g_tab = (INT32 *)
108     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
109 				(MAXJSAMPLE+1) * SIZEOF(INT32));
110 
111   for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
112     /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
113     /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
114     /* Cr=>R value is nearest int to 1.402 * x */
115     upsample->Cr_r_tab[i] = (int)
116 		    RIGHT_SHIFT(FIX(1.402) * x + ONE_HALF, SCALEBITS);
117     /* Cb=>B value is nearest int to 1.772 * x */
118     upsample->Cb_b_tab[i] = (int)
119 		    RIGHT_SHIFT(FIX(1.772) * x + ONE_HALF, SCALEBITS);
120     /* Cr=>G value is scaled-up -0.714136286 * x */
121     upsample->Cr_g_tab[i] = (- FIX(0.714136286)) * x;
122     /* Cb=>G value is scaled-up -0.344136286 * x */
123     /* We also add in ONE_HALF so that need not do it in inner loop */
124     upsample->Cb_g_tab[i] = (- FIX(0.344136286)) * x + ONE_HALF;
125   }
126 }
127 
128 
129 LOCAL(void)
build_bg_ycc_rgb_table(j_decompress_ptr cinfo)130 build_bg_ycc_rgb_table (j_decompress_ptr cinfo)
131 /* Wide gamut case, bg-sYCC */
132 {
133   my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
134   int i;
135   INT32 x;
136   SHIFT_TEMPS
137 
138   upsample->Cr_r_tab = (int *)
139     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
140 				(MAXJSAMPLE+1) * SIZEOF(int));
141   upsample->Cb_b_tab = (int *)
142     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
143 				(MAXJSAMPLE+1) * SIZEOF(int));
144   upsample->Cr_g_tab = (INT32 *)
145     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
146 				(MAXJSAMPLE+1) * SIZEOF(INT32));
147   upsample->Cb_g_tab = (INT32 *)
148     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
149 				(MAXJSAMPLE+1) * SIZEOF(INT32));
150 
151   for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
152     /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
153     /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
154     /* Cr=>R value is nearest int to 2.804 * x */
155     upsample->Cr_r_tab[i] = (int)
156 		    RIGHT_SHIFT(FIX(2.804) * x + ONE_HALF, SCALEBITS);
157     /* Cb=>B value is nearest int to 3.544 * x */
158     upsample->Cb_b_tab[i] = (int)
159 		    RIGHT_SHIFT(FIX(3.544) * x + ONE_HALF, SCALEBITS);
160     /* Cr=>G value is scaled-up -1.428272572 * x */
161     upsample->Cr_g_tab[i] = (- FIX(1.428272572)) * x;
162     /* Cb=>G value is scaled-up -0.688272572 * x */
163     /* We also add in ONE_HALF so that need not do it in inner loop */
164     upsample->Cb_g_tab[i] = (- FIX(0.688272572)) * x + ONE_HALF;
165   }
166 }
167 
168 
169 /*
170  * Initialize for an upsampling pass.
171  */
172 
173 METHODDEF(void)
start_pass_merged_upsample(j_decompress_ptr cinfo)174 start_pass_merged_upsample (j_decompress_ptr cinfo)
175 {
176   my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
177 
178   /* Mark the spare buffer empty */
179   upsample->spare_full = FALSE;
180   /* Initialize total-height counter for detecting bottom of image */
181   upsample->rows_to_go = cinfo->output_height;
182 }
183 
184 
185 /*
186  * Control routine to do upsampling (and color conversion).
187  *
188  * The control routine just handles the row buffering considerations.
189  */
190 
191 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)192 merged_2v_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 /* 2:1 vertical sampling case: may need a spare row. */
198 {
199   my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
200   JSAMPROW work_ptrs[2];
201   JDIMENSION num_rows;		/* number of rows returned to caller */
202 
203   if (upsample->spare_full) {
204     /* If we have a spare row saved from a previous cycle, just return it. */
205     jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
206 		      1, upsample->out_row_width);
207     num_rows = 1;
208     upsample->spare_full = FALSE;
209   } else {
210     /* Figure number of rows to return to caller. */
211     num_rows = 2;
212     /* Not more than the distance to the end of the image. */
213     if (num_rows > upsample->rows_to_go)
214       num_rows = upsample->rows_to_go;
215     /* And not more than what the client can accept: */
216     out_rows_avail -= *out_row_ctr;
217     if (num_rows > out_rows_avail)
218       num_rows = out_rows_avail;
219     /* Create output pointer array for upsampler. */
220     work_ptrs[0] = output_buf[*out_row_ctr];
221     if (num_rows > 1) {
222       work_ptrs[1] = output_buf[*out_row_ctr + 1];
223     } else {
224       work_ptrs[1] = upsample->spare_row;
225       upsample->spare_full = TRUE;
226     }
227     /* Now do the upsampling. */
228     (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
229   }
230 
231   /* Adjust counts */
232   *out_row_ctr += num_rows;
233   upsample->rows_to_go -= num_rows;
234   /* When the buffer is emptied, declare this input row group consumed */
235   if (! upsample->spare_full)
236     (*in_row_group_ctr)++;
237 }
238 
239 
240 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)241 merged_1v_upsample (j_decompress_ptr cinfo,
242 		    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
243 		    JDIMENSION in_row_groups_avail,
244 		    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
245 		    JDIMENSION out_rows_avail)
246 /* 1:1 vertical sampling case: much easier, never need a spare row. */
247 {
248   my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
249 
250   /* Just do the upsampling. */
251   (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
252 			 output_buf + *out_row_ctr);
253   /* Adjust counts */
254   (*out_row_ctr)++;
255   (*in_row_group_ctr)++;
256 }
257 
258 
259 /*
260  * These are the routines invoked by the control routines to do
261  * the actual upsampling/conversion.  One row group is processed per call.
262  *
263  * Note: since we may be writing directly into application-supplied buffers,
264  * we have to be honest about the output width; we can't assume the buffer
265  * has been rounded up to an even width.
266  */
267 
268 
269 /*
270  * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
271  */
272 
273 METHODDEF(void)
h2v1_merged_upsample(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION in_row_group_ctr,JSAMPARRAY output_buf)274 h2v1_merged_upsample (j_decompress_ptr cinfo,
275 		      JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
276 		      JSAMPARRAY output_buf)
277 {
278   my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
279   register int y, cred, cgreen, cblue;
280   int cb, cr;
281   register JSAMPROW outptr;
282   JSAMPROW inptr0, inptr1, inptr2;
283   JDIMENSION col;
284   /* copy these pointers into registers if possible */
285   register JSAMPLE * range_limit = cinfo->sample_range_limit;
286   int * Crrtab = upsample->Cr_r_tab;
287   int * Cbbtab = upsample->Cb_b_tab;
288   INT32 * Crgtab = upsample->Cr_g_tab;
289   INT32 * Cbgtab = upsample->Cb_g_tab;
290   SHIFT_TEMPS
291 
292   inptr0 = input_buf[0][in_row_group_ctr];
293   inptr1 = input_buf[1][in_row_group_ctr];
294   inptr2 = input_buf[2][in_row_group_ctr];
295   outptr = output_buf[0];
296   /* Loop for each pair of output pixels */
297   for (col = cinfo->output_width >> 1; col > 0; col--) {
298     /* Do the chroma part of the calculation */
299     cb = GETJSAMPLE(*inptr1++);
300     cr = GETJSAMPLE(*inptr2++);
301     cred   = Crrtab[cr];
302     cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
303     cblue  = Cbbtab[cb];
304     /* Fetch 2 Y values and emit 2 pixels */
305     y  = GETJSAMPLE(*inptr0++);
306     outptr[RGB_RED]   = range_limit[y + cred];
307     outptr[RGB_GREEN] = range_limit[y + cgreen];
308     outptr[RGB_BLUE]  = range_limit[y + cblue];
309     outptr += RGB_PIXELSIZE;
310     y  = GETJSAMPLE(*inptr0++);
311     outptr[RGB_RED]   = range_limit[y + cred];
312     outptr[RGB_GREEN] = range_limit[y + cgreen];
313     outptr[RGB_BLUE]  = range_limit[y + cblue];
314     outptr += RGB_PIXELSIZE;
315   }
316   /* If image width is odd, do the last output column separately */
317   if (cinfo->output_width & 1) {
318     cb = GETJSAMPLE(*inptr1);
319     cr = GETJSAMPLE(*inptr2);
320     cred   = Crrtab[cr];
321     cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
322     cblue  = Cbbtab[cb];
323     y  = GETJSAMPLE(*inptr0);
324     outptr[RGB_RED]   = range_limit[y + cred];
325     outptr[RGB_GREEN] = range_limit[y + cgreen];
326     outptr[RGB_BLUE]  = range_limit[y + cblue];
327   }
328 }
329 
330 
331 /*
332  * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
333  */
334 
335 METHODDEF(void)
h2v2_merged_upsample(j_decompress_ptr cinfo,JSAMPIMAGE input_buf,JDIMENSION in_row_group_ctr,JSAMPARRAY output_buf)336 h2v2_merged_upsample (j_decompress_ptr cinfo,
337 		      JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
338 		      JSAMPARRAY output_buf)
339 {
340   my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
341   register int y, cred, cgreen, cblue;
342   int cb, cr;
343   register JSAMPROW outptr0, outptr1;
344   JSAMPROW inptr00, inptr01, inptr1, inptr2;
345   JDIMENSION col;
346   /* copy these pointers into registers if possible */
347   register JSAMPLE * range_limit = cinfo->sample_range_limit;
348   int * Crrtab = upsample->Cr_r_tab;
349   int * Cbbtab = upsample->Cb_b_tab;
350   INT32 * Crgtab = upsample->Cr_g_tab;
351   INT32 * Cbgtab = upsample->Cb_g_tab;
352   SHIFT_TEMPS
353 
354   inptr00 = input_buf[0][in_row_group_ctr*2];
355   inptr01 = input_buf[0][in_row_group_ctr*2 + 1];
356   inptr1 = input_buf[1][in_row_group_ctr];
357   inptr2 = input_buf[2][in_row_group_ctr];
358   outptr0 = output_buf[0];
359   outptr1 = output_buf[1];
360   /* Loop for each group of output pixels */
361   for (col = cinfo->output_width >> 1; col > 0; col--) {
362     /* Do the chroma part of the calculation */
363     cb = GETJSAMPLE(*inptr1++);
364     cr = GETJSAMPLE(*inptr2++);
365     cred   = Crrtab[cr];
366     cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
367     cblue  = Cbbtab[cb];
368     /* Fetch 4 Y values and emit 4 pixels */
369     y  = GETJSAMPLE(*inptr00++);
370     outptr0[RGB_RED]   = range_limit[y + cred];
371     outptr0[RGB_GREEN] = range_limit[y + cgreen];
372     outptr0[RGB_BLUE]  = range_limit[y + cblue];
373     outptr0 += RGB_PIXELSIZE;
374     y  = GETJSAMPLE(*inptr00++);
375     outptr0[RGB_RED]   = range_limit[y + cred];
376     outptr0[RGB_GREEN] = range_limit[y + cgreen];
377     outptr0[RGB_BLUE]  = range_limit[y + cblue];
378     outptr0 += RGB_PIXELSIZE;
379     y  = GETJSAMPLE(*inptr01++);
380     outptr1[RGB_RED]   = range_limit[y + cred];
381     outptr1[RGB_GREEN] = range_limit[y + cgreen];
382     outptr1[RGB_BLUE]  = range_limit[y + cblue];
383     outptr1 += RGB_PIXELSIZE;
384     y  = GETJSAMPLE(*inptr01++);
385     outptr1[RGB_RED]   = range_limit[y + cred];
386     outptr1[RGB_GREEN] = range_limit[y + cgreen];
387     outptr1[RGB_BLUE]  = range_limit[y + cblue];
388     outptr1 += RGB_PIXELSIZE;
389   }
390   /* If image width is odd, do the last output column separately */
391   if (cinfo->output_width & 1) {
392     cb = GETJSAMPLE(*inptr1);
393     cr = GETJSAMPLE(*inptr2);
394     cred   = Crrtab[cr];
395     cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
396     cblue  = Cbbtab[cb];
397     y  = GETJSAMPLE(*inptr00);
398     outptr0[RGB_RED]   = range_limit[y + cred];
399     outptr0[RGB_GREEN] = range_limit[y + cgreen];
400     outptr0[RGB_BLUE]  = range_limit[y + cblue];
401     y  = GETJSAMPLE(*inptr01);
402     outptr1[RGB_RED]   = range_limit[y + cred];
403     outptr1[RGB_GREEN] = range_limit[y + cgreen];
404     outptr1[RGB_BLUE]  = range_limit[y + cblue];
405   }
406 }
407 
408 
409 /*
410  * Module initialization routine for merged upsampling/color conversion.
411  *
412  * NB: this is called under the conditions determined by use_merged_upsample()
413  * in jdmaster.c.  That routine MUST correspond to the actual capabilities
414  * of this module; no safety checks are made here.
415  */
416 
417 GLOBAL(void)
jinit_merged_upsampler(j_decompress_ptr cinfo)418 jinit_merged_upsampler (j_decompress_ptr cinfo)
419 {
420   my_upsample_ptr upsample;
421 
422   upsample = (my_upsample_ptr)
423     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
424 				SIZEOF(my_upsampler));
425   cinfo->upsample = &upsample->pub;
426   upsample->pub.start_pass = start_pass_merged_upsample;
427   upsample->pub.need_context_rows = FALSE;
428 
429   upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
430 
431   if (cinfo->max_v_samp_factor == 2) {
432     upsample->pub.upsample = merged_2v_upsample;
433     upsample->upmethod = h2v2_merged_upsample;
434     /* Allocate a spare row buffer */
435     upsample->spare_row = (JSAMPROW)
436       (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
437 		(size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));
438   } else {
439     upsample->pub.upsample = merged_1v_upsample;
440     upsample->upmethod = h2v1_merged_upsample;
441     /* No spare row needed */
442     upsample->spare_row = NULL;
443   }
444 
445   if (cinfo->jpeg_color_space == JCS_BG_YCC)
446     build_bg_ycc_rgb_table(cinfo);
447   else
448     build_ycc_rgb_table(cinfo);
449 }
450 
451 #endif /* UPSAMPLE_MERGING_SUPPORTED */
452