4 * Copyright (C) 1994-1996, Thomas G. Lane.
5 * Modified 2013-2015 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.
9 * This file contains code for merged upsampling/color conversion.
11 * This file combines functions from jdsample.c and jdcolor.c;
12 * read those files first to understand what's going on.
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
19 * G = Y + K2 * Cb + K3 * Cr
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.
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.)
36 #define JPEG_INTERNALS
40 #ifdef UPSAMPLE_MERGING_SUPPORTED
43 /* Private subobject */
46 struct jpeg_upsampler pub
; /* public fields */
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
));
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 */
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.
65 boolean spare_full
; /* T if spare buffer is occupied */
67 JDIMENSION out_row_width
; /* samples per output row */
68 JDIMENSION rows_to_go
; /* counts rows remaining in image */
71 typedef my_upsampler
* my_upsample_ptr
;
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))
79 * Initialize tables for YCbCr->RGB and BG_YCC->RGB colorspace conversion.
80 * This is taken directly from jdcolor.c; see that file for more info.
84 build_ycc_rgb_table (j_decompress_ptr cinfo
)
85 /* Normal case, sYCC */
87 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
92 upsample
->Cr_r_tab
= (int *)
93 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
94 (MAXJSAMPLE
+1) * SIZEOF(int));
95 upsample
->Cb_b_tab
= (int *)
96 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
97 (MAXJSAMPLE
+1) * SIZEOF(int));
98 upsample
->Cr_g_tab
= (INT32
*)
99 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
100 (MAXJSAMPLE
+1) * SIZEOF(INT32
));
101 upsample
->Cb_g_tab
= (INT32
*)
102 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
103 (MAXJSAMPLE
+1) * SIZEOF(INT32
));
105 for (i
= 0, x
= -CENTERJSAMPLE
; i
<= MAXJSAMPLE
; i
++, x
++) {
106 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
107 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
108 /* Cr=>R value is nearest int to 1.402 * x */
109 upsample
->Cr_r_tab
[i
] = (int)
110 RIGHT_SHIFT(FIX(1.402) * x
+ ONE_HALF
, SCALEBITS
);
111 /* Cb=>B value is nearest int to 1.772 * x */
112 upsample
->Cb_b_tab
[i
] = (int)
113 RIGHT_SHIFT(FIX(1.772) * x
+ ONE_HALF
, SCALEBITS
);
114 /* Cr=>G value is scaled-up -0.714136286 * x */
115 upsample
->Cr_g_tab
[i
] = (- FIX(0.714136286)) * x
;
116 /* Cb=>G value is scaled-up -0.344136286 * x */
117 /* We also add in ONE_HALF so that need not do it in inner loop */
118 upsample
->Cb_g_tab
[i
] = (- FIX(0.344136286)) * x
+ ONE_HALF
;
124 build_bg_ycc_rgb_table (j_decompress_ptr cinfo
)
125 /* Wide gamut case, bg-sYCC */
127 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
132 upsample
->Cr_r_tab
= (int *)
133 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
134 (MAXJSAMPLE
+1) * SIZEOF(int));
135 upsample
->Cb_b_tab
= (int *)
136 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
137 (MAXJSAMPLE
+1) * SIZEOF(int));
138 upsample
->Cr_g_tab
= (INT32
*)
139 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
140 (MAXJSAMPLE
+1) * SIZEOF(INT32
));
141 upsample
->Cb_g_tab
= (INT32
*)
142 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
143 (MAXJSAMPLE
+1) * SIZEOF(INT32
));
145 for (i
= 0, x
= -CENTERJSAMPLE
; i
<= MAXJSAMPLE
; i
++, x
++) {
146 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
147 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
148 /* Cr=>R value is nearest int to 2.804 * x */
149 upsample
->Cr_r_tab
[i
] = (int)
150 RIGHT_SHIFT(FIX(2.804) * x
+ ONE_HALF
, SCALEBITS
);
151 /* Cb=>B value is nearest int to 3.544 * x */
152 upsample
->Cb_b_tab
[i
] = (int)
153 RIGHT_SHIFT(FIX(3.544) * x
+ ONE_HALF
, SCALEBITS
);
154 /* Cr=>G value is scaled-up -1.428272572 * x */
155 upsample
->Cr_g_tab
[i
] = (- FIX(1.428272572)) * x
;
156 /* Cb=>G value is scaled-up -0.688272572 * x */
157 /* We also add in ONE_HALF so that need not do it in inner loop */
158 upsample
->Cb_g_tab
[i
] = (- FIX(0.688272572)) * x
+ ONE_HALF
;
164 * Initialize for an upsampling pass.
168 start_pass_merged_upsample (j_decompress_ptr cinfo
)
170 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
172 /* Mark the spare buffer empty */
173 upsample
->spare_full
= FALSE
;
174 /* Initialize total-height counter for detecting bottom of image */
175 upsample
->rows_to_go
= cinfo
->output_height
;
180 * Control routine to do upsampling (and color conversion).
182 * The control routine just handles the row buffering considerations.
186 merged_2v_upsample (j_decompress_ptr cinfo
,
187 JSAMPIMAGE input_buf
, JDIMENSION
*in_row_group_ctr
,
188 JDIMENSION in_row_groups_avail
,
189 JSAMPARRAY output_buf
, JDIMENSION
*out_row_ctr
,
190 JDIMENSION out_rows_avail
)
191 /* 2:1 vertical sampling case: may need a spare row. */
193 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
194 JSAMPROW work_ptrs
[2];
195 JDIMENSION num_rows
; /* number of rows returned to caller */
197 if (upsample
->spare_full
) {
198 /* If we have a spare row saved from a previous cycle, just return it. */
199 jcopy_sample_rows(& upsample
->spare_row
, 0, output_buf
+ *out_row_ctr
, 0,
200 1, upsample
->out_row_width
);
202 upsample
->spare_full
= FALSE
;
204 /* Figure number of rows to return to caller. */
206 /* Not more than the distance to the end of the image. */
207 if (num_rows
> upsample
->rows_to_go
)
208 num_rows
= upsample
->rows_to_go
;
209 /* And not more than what the client can accept: */
210 out_rows_avail
-= *out_row_ctr
;
211 if (num_rows
> out_rows_avail
)
212 num_rows
= out_rows_avail
;
213 /* Create output pointer array for upsampler. */
214 work_ptrs
[0] = output_buf
[*out_row_ctr
];
216 work_ptrs
[1] = output_buf
[*out_row_ctr
+ 1];
218 work_ptrs
[1] = upsample
->spare_row
;
219 upsample
->spare_full
= TRUE
;
221 /* Now do the upsampling. */
222 (*upsample
->upmethod
) (cinfo
, input_buf
, *in_row_group_ctr
, work_ptrs
);
226 *out_row_ctr
+= num_rows
;
227 upsample
->rows_to_go
-= num_rows
;
228 /* When the buffer is emptied, declare this input row group consumed */
229 if (! upsample
->spare_full
)
230 (*in_row_group_ctr
)++;
235 merged_1v_upsample (j_decompress_ptr cinfo
,
236 JSAMPIMAGE input_buf
, JDIMENSION
*in_row_group_ctr
,
237 JDIMENSION in_row_groups_avail
,
238 JSAMPARRAY output_buf
, JDIMENSION
*out_row_ctr
,
239 JDIMENSION out_rows_avail
)
240 /* 1:1 vertical sampling case: much easier, never need a spare row. */
242 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
244 /* Just do the upsampling. */
245 (*upsample
->upmethod
) (cinfo
, input_buf
, *in_row_group_ctr
,
246 output_buf
+ *out_row_ctr
);
249 (*in_row_group_ctr
)++;
254 * These are the routines invoked by the control routines to do
255 * the actual upsampling/conversion. One row group is processed per call.
257 * Note: since we may be writing directly into application-supplied buffers,
258 * we have to be honest about the output width; we can't assume the buffer
259 * has been rounded up to an even width.
264 * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
268 h2v1_merged_upsample (j_decompress_ptr cinfo
,
269 JSAMPIMAGE input_buf
, JDIMENSION in_row_group_ctr
,
270 JSAMPARRAY output_buf
)
272 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
273 register int y
, cred
, cgreen
, cblue
;
275 register JSAMPROW outptr
;
276 JSAMPROW inptr0
, inptr1
, inptr2
;
278 /* copy these pointers into registers if possible */
279 register JSAMPLE
* range_limit
= cinfo
->sample_range_limit
;
280 int * Crrtab
= upsample
->Cr_r_tab
;
281 int * Cbbtab
= upsample
->Cb_b_tab
;
282 INT32
* Crgtab
= upsample
->Cr_g_tab
;
283 INT32
* Cbgtab
= upsample
->Cb_g_tab
;
286 inptr0
= input_buf
[0][in_row_group_ctr
];
287 inptr1
= input_buf
[1][in_row_group_ctr
];
288 inptr2
= input_buf
[2][in_row_group_ctr
];
289 outptr
= output_buf
[0];
290 /* Loop for each pair of output pixels */
291 for (col
= cinfo
->output_width
>> 1; col
> 0; col
--) {
292 /* Do the chroma part of the calculation */
293 cb
= GETJSAMPLE(*inptr1
++);
294 cr
= GETJSAMPLE(*inptr2
++);
296 cgreen
= (int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
], SCALEBITS
);
298 /* Fetch 2 Y values and emit 2 pixels */
299 y
= GETJSAMPLE(*inptr0
++);
300 outptr
[RGB_RED
] = range_limit
[y
+ cred
];
301 outptr
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
302 outptr
[RGB_BLUE
] = range_limit
[y
+ cblue
];
303 outptr
+= RGB_PIXELSIZE
;
304 y
= GETJSAMPLE(*inptr0
++);
305 outptr
[RGB_RED
] = range_limit
[y
+ cred
];
306 outptr
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
307 outptr
[RGB_BLUE
] = range_limit
[y
+ cblue
];
308 outptr
+= RGB_PIXELSIZE
;
310 /* If image width is odd, do the last output column separately */
311 if (cinfo
->output_width
& 1) {
312 cb
= GETJSAMPLE(*inptr1
);
313 cr
= GETJSAMPLE(*inptr2
);
315 cgreen
= (int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
], SCALEBITS
);
317 y
= GETJSAMPLE(*inptr0
);
318 outptr
[RGB_RED
] = range_limit
[y
+ cred
];
319 outptr
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
320 outptr
[RGB_BLUE
] = range_limit
[y
+ cblue
];
326 * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
330 h2v2_merged_upsample (j_decompress_ptr cinfo
,
331 JSAMPIMAGE input_buf
, JDIMENSION in_row_group_ctr
,
332 JSAMPARRAY output_buf
)
334 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
335 register int y
, cred
, cgreen
, cblue
;
337 register JSAMPROW outptr0
, outptr1
;
338 JSAMPROW inptr00
, inptr01
, inptr1
, inptr2
;
340 /* copy these pointers into registers if possible */
341 register JSAMPLE
* range_limit
= cinfo
->sample_range_limit
;
342 int * Crrtab
= upsample
->Cr_r_tab
;
343 int * Cbbtab
= upsample
->Cb_b_tab
;
344 INT32
* Crgtab
= upsample
->Cr_g_tab
;
345 INT32
* Cbgtab
= upsample
->Cb_g_tab
;
348 inptr00
= input_buf
[0][in_row_group_ctr
*2];
349 inptr01
= input_buf
[0][in_row_group_ctr
*2 + 1];
350 inptr1
= input_buf
[1][in_row_group_ctr
];
351 inptr2
= input_buf
[2][in_row_group_ctr
];
352 outptr0
= output_buf
[0];
353 outptr1
= output_buf
[1];
354 /* Loop for each group of output pixels */
355 for (col
= cinfo
->output_width
>> 1; col
> 0; col
--) {
356 /* Do the chroma part of the calculation */
357 cb
= GETJSAMPLE(*inptr1
++);
358 cr
= GETJSAMPLE(*inptr2
++);
360 cgreen
= (int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
], SCALEBITS
);
362 /* Fetch 4 Y values and emit 4 pixels */
363 y
= GETJSAMPLE(*inptr00
++);
364 outptr0
[RGB_RED
] = range_limit
[y
+ cred
];
365 outptr0
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
366 outptr0
[RGB_BLUE
] = range_limit
[y
+ cblue
];
367 outptr0
+= RGB_PIXELSIZE
;
368 y
= GETJSAMPLE(*inptr00
++);
369 outptr0
[RGB_RED
] = range_limit
[y
+ cred
];
370 outptr0
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
371 outptr0
[RGB_BLUE
] = range_limit
[y
+ cblue
];
372 outptr0
+= RGB_PIXELSIZE
;
373 y
= GETJSAMPLE(*inptr01
++);
374 outptr1
[RGB_RED
] = range_limit
[y
+ cred
];
375 outptr1
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
376 outptr1
[RGB_BLUE
] = range_limit
[y
+ cblue
];
377 outptr1
+= RGB_PIXELSIZE
;
378 y
= GETJSAMPLE(*inptr01
++);
379 outptr1
[RGB_RED
] = range_limit
[y
+ cred
];
380 outptr1
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
381 outptr1
[RGB_BLUE
] = range_limit
[y
+ cblue
];
382 outptr1
+= RGB_PIXELSIZE
;
384 /* If image width is odd, do the last output column separately */
385 if (cinfo
->output_width
& 1) {
386 cb
= GETJSAMPLE(*inptr1
);
387 cr
= GETJSAMPLE(*inptr2
);
389 cgreen
= (int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
], SCALEBITS
);
391 y
= GETJSAMPLE(*inptr00
);
392 outptr0
[RGB_RED
] = range_limit
[y
+ cred
];
393 outptr0
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
394 outptr0
[RGB_BLUE
] = range_limit
[y
+ cblue
];
395 y
= GETJSAMPLE(*inptr01
);
396 outptr1
[RGB_RED
] = range_limit
[y
+ cred
];
397 outptr1
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
398 outptr1
[RGB_BLUE
] = range_limit
[y
+ cblue
];
404 * Module initialization routine for merged upsampling/color conversion.
406 * NB: this is called under the conditions determined by use_merged_upsample()
407 * in jdmaster.c. That routine MUST correspond to the actual capabilities
408 * of this module; no safety checks are made here.
412 jinit_merged_upsampler (j_decompress_ptr cinfo
)
414 my_upsample_ptr upsample
;
416 upsample
= (my_upsample_ptr
)
417 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
418 SIZEOF(my_upsampler
));
419 cinfo
->upsample
= &upsample
->pub
;
420 upsample
->pub
.start_pass
= start_pass_merged_upsample
;
421 upsample
->pub
.need_context_rows
= FALSE
;
423 upsample
->out_row_width
= cinfo
->output_width
* cinfo
->out_color_components
;
425 if (cinfo
->max_v_samp_factor
== 2) {
426 upsample
->pub
.upsample
= merged_2v_upsample
;
427 upsample
->upmethod
= h2v2_merged_upsample
;
428 /* Allocate a spare row buffer */
429 upsample
->spare_row
= (JSAMPROW
)
430 (*cinfo
->mem
->alloc_large
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
431 (size_t) (upsample
->out_row_width
* SIZEOF(JSAMPLE
)));
433 upsample
->pub
.upsample
= merged_1v_upsample
;
434 upsample
->upmethod
= h2v1_merged_upsample
;
435 /* No spare row needed */
436 upsample
->spare_row
= NULL
;
439 if (cinfo
->jpeg_color_space
== JCS_BG_YCC
)
440 build_bg_ycc_rgb_table(cinfo
);
442 build_ycc_rgb_table(cinfo
);
445 #endif /* UPSAMPLE_MERGING_SUPPORTED */