4 * Copyright (C) 1994-1996, Thomas G. Lane.
5 * Modified 2013-2020 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
44 /* Deliberate syntax err */
45 Sorry
, this code requires
2 or more range extension bits
.
49 /* Private subobject */
52 struct jpeg_upsampler pub
; /* public fields */
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
));
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 */
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.
71 boolean spare_full
; /* T if spare buffer is occupied */
73 JDIMENSION out_row_width
; /* samples per output row */
74 JDIMENSION rows_to_go
; /* counts rows remaining in image */
77 typedef my_upsampler
* my_upsample_ptr
;
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))
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.
90 build_ycc_rgb_table (j_decompress_ptr cinfo
)
91 /* Normal case, sYCC */
93 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
98 upsample
->Cr_r_tab
= (int *) (*cinfo
->mem
->alloc_small
)
99 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, (MAXJSAMPLE
+1) * SIZEOF(int));
100 upsample
->Cb_b_tab
= (int *) (*cinfo
->mem
->alloc_small
)
101 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, (MAXJSAMPLE
+1) * SIZEOF(int));
102 upsample
->Cr_g_tab
= (INT32
*) (*cinfo
->mem
->alloc_small
)
103 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, (MAXJSAMPLE
+1) * SIZEOF(INT32
));
104 upsample
->Cb_g_tab
= (INT32
*) (*cinfo
->mem
->alloc_small
)
105 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, (MAXJSAMPLE
+1) * SIZEOF(INT32
));
107 for (i
= 0, x
= -CENTERJSAMPLE
; i
<= MAXJSAMPLE
; i
++, x
++) {
108 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
109 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
110 /* Cr=>R value is nearest int to 1.402 * x */
111 upsample
->Cr_r_tab
[i
] = (int) DESCALE(FIX(1.402) * x
, SCALEBITS
);
112 /* Cb=>B value is nearest int to 1.772 * x */
113 upsample
->Cb_b_tab
[i
] = (int) DESCALE(FIX(1.772) * x
, 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 *) (*cinfo
->mem
->alloc_small
)
133 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, (MAXJSAMPLE
+1) * SIZEOF(int));
134 upsample
->Cb_b_tab
= (int *) (*cinfo
->mem
->alloc_small
)
135 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, (MAXJSAMPLE
+1) * SIZEOF(int));
136 upsample
->Cr_g_tab
= (INT32
*) (*cinfo
->mem
->alloc_small
)
137 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, (MAXJSAMPLE
+1) * SIZEOF(INT32
));
138 upsample
->Cb_g_tab
= (INT32
*) (*cinfo
->mem
->alloc_small
)
139 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, (MAXJSAMPLE
+1) * SIZEOF(INT32
));
141 for (i
= 0, x
= -CENTERJSAMPLE
; i
<= MAXJSAMPLE
; i
++, x
++) {
142 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
143 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
144 /* Cr=>R value is nearest int to 2.804 * x */
145 upsample
->Cr_r_tab
[i
] = (int) DESCALE(FIX(2.804) * x
, SCALEBITS
);
146 /* Cb=>B value is nearest int to 3.544 * x */
147 upsample
->Cb_b_tab
[i
] = (int) DESCALE(FIX(3.544) * x
, SCALEBITS
);
148 /* Cr=>G value is scaled-up -1.428272572 * x */
149 upsample
->Cr_g_tab
[i
] = (- FIX(1.428272572)) * x
;
150 /* Cb=>G value is scaled-up -0.688272572 * x */
151 /* We also add in ONE_HALF so that need not do it in inner loop */
152 upsample
->Cb_g_tab
[i
] = (- FIX(0.688272572)) * x
+ ONE_HALF
;
158 * Initialize for an upsampling pass.
162 start_pass_merged_upsample (j_decompress_ptr cinfo
)
164 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
166 /* Mark the spare buffer empty */
167 upsample
->spare_full
= FALSE
;
168 /* Initialize total-height counter for detecting bottom of image */
169 upsample
->rows_to_go
= cinfo
->output_height
;
174 * Control routine to do upsampling (and color conversion).
176 * The control routine just handles the row buffering considerations.
180 merged_2v_upsample (j_decompress_ptr cinfo
,
181 JSAMPIMAGE input_buf
, JDIMENSION
*in_row_group_ctr
,
182 JDIMENSION in_row_groups_avail
,
183 JSAMPARRAY output_buf
, JDIMENSION
*out_row_ctr
,
184 JDIMENSION out_rows_avail
)
185 /* 2:1 vertical sampling case: may need a spare row. */
187 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
188 JSAMPROW work_ptrs
[2];
189 JDIMENSION num_rows
; /* number of rows returned to caller */
191 if (upsample
->spare_full
) {
192 /* If we have a spare row saved from a previous cycle, just return it. */
193 jcopy_sample_rows(& upsample
->spare_row
, output_buf
+ *out_row_ctr
,
194 1, upsample
->out_row_width
);
196 upsample
->spare_full
= FALSE
;
198 /* Figure number of rows to return to caller. */
200 /* Not more than the distance to the end of the image. */
201 if (num_rows
> upsample
->rows_to_go
)
202 num_rows
= upsample
->rows_to_go
;
203 /* And not more than what the client can accept: */
204 out_rows_avail
-= *out_row_ctr
;
205 if (num_rows
> out_rows_avail
)
206 num_rows
= out_rows_avail
;
207 /* Create output pointer array for upsampler. */
208 work_ptrs
[0] = output_buf
[*out_row_ctr
];
210 work_ptrs
[1] = output_buf
[*out_row_ctr
+ 1];
212 work_ptrs
[1] = upsample
->spare_row
;
213 upsample
->spare_full
= TRUE
;
215 /* Now do the upsampling. */
216 (*upsample
->upmethod
) (cinfo
, input_buf
, *in_row_group_ctr
, work_ptrs
);
220 *out_row_ctr
+= num_rows
;
221 upsample
->rows_to_go
-= num_rows
;
222 /* When the buffer is emptied, declare this input row group consumed */
223 if (! upsample
->spare_full
)
224 (*in_row_group_ctr
)++;
229 merged_1v_upsample (j_decompress_ptr cinfo
,
230 JSAMPIMAGE input_buf
, JDIMENSION
*in_row_group_ctr
,
231 JDIMENSION in_row_groups_avail
,
232 JSAMPARRAY output_buf
, JDIMENSION
*out_row_ctr
,
233 JDIMENSION out_rows_avail
)
234 /* 1:1 vertical sampling case: much easier, never need a spare row. */
236 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
238 /* Just do the upsampling. */
239 (*upsample
->upmethod
) (cinfo
, input_buf
, *in_row_group_ctr
,
240 output_buf
+ *out_row_ctr
);
243 (*in_row_group_ctr
)++;
248 * These are the routines invoked by the control routines to do
249 * the actual upsampling/conversion. One row group is processed per call.
251 * Note: since we may be writing directly into application-supplied buffers,
252 * we have to be honest about the output width; we can't assume the buffer
253 * has been rounded up to an even width.
258 * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
262 h2v1_merged_upsample (j_decompress_ptr cinfo
,
263 JSAMPIMAGE input_buf
, JDIMENSION in_row_group_ctr
,
264 JSAMPARRAY output_buf
)
266 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
267 register int y
, cred
, cgreen
, cblue
;
269 register JSAMPROW outptr
;
270 JSAMPROW inptr0
, inptr1
, inptr2
;
272 /* copy these pointers into registers if possible */
273 register JSAMPLE
* range_limit
= cinfo
->sample_range_limit
;
274 int * Crrtab
= upsample
->Cr_r_tab
;
275 int * Cbbtab
= upsample
->Cb_b_tab
;
276 INT32
* Crgtab
= upsample
->Cr_g_tab
;
277 INT32
* Cbgtab
= upsample
->Cb_g_tab
;
280 inptr0
= input_buf
[0][in_row_group_ctr
];
281 inptr1
= input_buf
[1][in_row_group_ctr
];
282 inptr2
= input_buf
[2][in_row_group_ctr
];
283 outptr
= output_buf
[0];
284 /* Loop for each pair of output pixels */
285 for (col
= cinfo
->output_width
>> 1; col
> 0; col
--) {
286 /* Do the chroma part of the calculation */
287 cb
= GETJSAMPLE(*inptr1
++);
288 cr
= GETJSAMPLE(*inptr2
++);
290 cgreen
= (int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
], SCALEBITS
);
292 /* Fetch 2 Y values and emit 2 pixels */
293 y
= GETJSAMPLE(*inptr0
++);
294 outptr
[RGB_RED
] = range_limit
[y
+ cred
];
295 outptr
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
296 outptr
[RGB_BLUE
] = range_limit
[y
+ cblue
];
297 outptr
+= RGB_PIXELSIZE
;
298 y
= GETJSAMPLE(*inptr0
++);
299 outptr
[RGB_RED
] = range_limit
[y
+ cred
];
300 outptr
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
301 outptr
[RGB_BLUE
] = range_limit
[y
+ cblue
];
302 outptr
+= RGB_PIXELSIZE
;
304 /* If image width is odd, do the last output column separately */
305 if (cinfo
->output_width
& 1) {
306 cb
= GETJSAMPLE(*inptr1
);
307 cr
= GETJSAMPLE(*inptr2
);
309 cgreen
= (int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
], SCALEBITS
);
311 y
= GETJSAMPLE(*inptr0
);
312 outptr
[RGB_RED
] = range_limit
[y
+ cred
];
313 outptr
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
314 outptr
[RGB_BLUE
] = range_limit
[y
+ cblue
];
320 * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
324 h2v2_merged_upsample (j_decompress_ptr cinfo
,
325 JSAMPIMAGE input_buf
, JDIMENSION in_row_group_ctr
,
326 JSAMPARRAY output_buf
)
328 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
329 register int y
, cred
, cgreen
, cblue
;
331 register JSAMPROW outptr0
, outptr1
;
332 JSAMPROW inptr00
, inptr01
, inptr1
, inptr2
;
334 /* copy these pointers into registers if possible */
335 register JSAMPLE
* range_limit
= cinfo
->sample_range_limit
;
336 int * Crrtab
= upsample
->Cr_r_tab
;
337 int * Cbbtab
= upsample
->Cb_b_tab
;
338 INT32
* Crgtab
= upsample
->Cr_g_tab
;
339 INT32
* Cbgtab
= upsample
->Cb_g_tab
;
342 inptr00
= input_buf
[0][in_row_group_ctr
*2];
343 inptr01
= input_buf
[0][in_row_group_ctr
*2 + 1];
344 inptr1
= input_buf
[1][in_row_group_ctr
];
345 inptr2
= input_buf
[2][in_row_group_ctr
];
346 outptr0
= output_buf
[0];
347 outptr1
= output_buf
[1];
348 /* Loop for each group of output pixels */
349 for (col
= cinfo
->output_width
>> 1; col
> 0; col
--) {
350 /* Do the chroma part of the calculation */
351 cb
= GETJSAMPLE(*inptr1
++);
352 cr
= GETJSAMPLE(*inptr2
++);
354 cgreen
= (int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
], SCALEBITS
);
356 /* Fetch 4 Y values and emit 4 pixels */
357 y
= GETJSAMPLE(*inptr00
++);
358 outptr0
[RGB_RED
] = range_limit
[y
+ cred
];
359 outptr0
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
360 outptr0
[RGB_BLUE
] = range_limit
[y
+ cblue
];
361 outptr0
+= RGB_PIXELSIZE
;
362 y
= GETJSAMPLE(*inptr00
++);
363 outptr0
[RGB_RED
] = range_limit
[y
+ cred
];
364 outptr0
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
365 outptr0
[RGB_BLUE
] = range_limit
[y
+ cblue
];
366 outptr0
+= RGB_PIXELSIZE
;
367 y
= GETJSAMPLE(*inptr01
++);
368 outptr1
[RGB_RED
] = range_limit
[y
+ cred
];
369 outptr1
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
370 outptr1
[RGB_BLUE
] = range_limit
[y
+ cblue
];
371 outptr1
+= RGB_PIXELSIZE
;
372 y
= GETJSAMPLE(*inptr01
++);
373 outptr1
[RGB_RED
] = range_limit
[y
+ cred
];
374 outptr1
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
375 outptr1
[RGB_BLUE
] = range_limit
[y
+ cblue
];
376 outptr1
+= RGB_PIXELSIZE
;
378 /* If image width is odd, do the last output column separately */
379 if (cinfo
->output_width
& 1) {
380 cb
= GETJSAMPLE(*inptr1
);
381 cr
= GETJSAMPLE(*inptr2
);
383 cgreen
= (int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
], SCALEBITS
);
385 y
= GETJSAMPLE(*inptr00
);
386 outptr0
[RGB_RED
] = range_limit
[y
+ cred
];
387 outptr0
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
388 outptr0
[RGB_BLUE
] = range_limit
[y
+ cblue
];
389 y
= GETJSAMPLE(*inptr01
);
390 outptr1
[RGB_RED
] = range_limit
[y
+ cred
];
391 outptr1
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
392 outptr1
[RGB_BLUE
] = range_limit
[y
+ cblue
];
398 * Module initialization routine for merged upsampling/color conversion.
400 * NB: this is called under the conditions determined by use_merged_upsample()
401 * in jdmaster.c. That routine MUST correspond to the actual capabilities
402 * of this module; no safety checks are made here.
406 jinit_merged_upsampler (j_decompress_ptr cinfo
)
408 my_upsample_ptr upsample
;
410 upsample
= (my_upsample_ptr
) (*cinfo
->mem
->alloc_small
)
411 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, SIZEOF(my_upsampler
));
412 cinfo
->upsample
= &upsample
->pub
;
413 upsample
->pub
.start_pass
= start_pass_merged_upsample
;
414 upsample
->pub
.need_context_rows
= FALSE
;
416 upsample
->out_row_width
= cinfo
->output_width
* cinfo
->out_color_components
;
418 if (cinfo
->max_v_samp_factor
== 2) {
419 upsample
->pub
.upsample
= merged_2v_upsample
;
420 upsample
->upmethod
= h2v2_merged_upsample
;
421 /* Allocate a spare row buffer */
422 upsample
->spare_row
= (JSAMPROW
) (*cinfo
->mem
->alloc_large
)
423 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
424 (size_t) upsample
->out_row_width
* SIZEOF(JSAMPLE
));
426 upsample
->pub
.upsample
= merged_1v_upsample
;
427 upsample
->upmethod
= h2v1_merged_upsample
;
428 /* No spare row needed */
429 upsample
->spare_row
= NULL
;
432 if (cinfo
->jpeg_color_space
== JCS_BG_YCC
)
433 build_bg_ycc_rgb_table(cinfo
);
435 build_ycc_rgb_table(cinfo
);
438 #endif /* UPSAMPLE_MERGING_SUPPORTED */