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.
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 * 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.)
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 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
)
86 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
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
));
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
;
123 * Initialize for an upsampling pass.
127 start_pass_merged_upsample (j_decompress_ptr cinfo
)
129 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
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
;
139 * Control routine to do upsampling (and color conversion).
141 * The control routine just handles the row buffering considerations.
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. */
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 */
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
);
161 upsample
->spare_full
= FALSE
;
163 /* Figure number of rows to return to caller. */
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
];
175 work_ptrs
[1] = output_buf
[*out_row_ctr
+ 1];
177 work_ptrs
[1] = upsample
->spare_row
;
178 upsample
->spare_full
= TRUE
;
180 /* Now do the upsampling. */
181 (*upsample
->upmethod
) (cinfo
, input_buf
, *in_row_group_ctr
, work_ptrs
);
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
)++;
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. */
201 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
203 /* Just do the upsampling. */
204 (*upsample
->upmethod
) (cinfo
, input_buf
, *in_row_group_ctr
,
205 output_buf
+ *out_row_ctr
);
208 (*in_row_group_ctr
)++;
213 * These are the routines invoked by the control routines to do
214 * the actual upsampling/conversion. One row group is processed per call.
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.
223 * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
227 h2v1_merged_upsample (j_decompress_ptr cinfo
,
228 JSAMPIMAGE input_buf
, JDIMENSION in_row_group_ctr
,
229 JSAMPARRAY output_buf
)
231 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
232 register int y
, cred
, cgreen
, cblue
;
234 register JSAMPROW outptr
;
235 JSAMPROW inptr0
, inptr1
, inptr2
;
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
;
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
++);
255 cgreen
= (int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
], SCALEBITS
);
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
;
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
);
274 cgreen
= (int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
], SCALEBITS
);
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
];
285 * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
289 h2v2_merged_upsample (j_decompress_ptr cinfo
,
290 JSAMPIMAGE input_buf
, JDIMENSION in_row_group_ctr
,
291 JSAMPARRAY output_buf
)
293 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
294 register int y
, cred
, cgreen
, cblue
;
296 register JSAMPROW outptr0
, outptr1
;
297 JSAMPROW inptr00
, inptr01
, inptr1
, inptr2
;
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
;
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
++);
319 cgreen
= (int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
], SCALEBITS
);
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
;
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
);
348 cgreen
= (int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
], SCALEBITS
);
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
];
363 * Module initialization routine for merged upsampling/color conversion.
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.
371 jinit_merged_upsampler (j_decompress_ptr cinfo
)
373 my_upsample_ptr upsample
;
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
;
382 upsample
->out_row_width
= cinfo
->output_width
* cinfo
->out_color_components
;
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
)));
392 upsample
->pub
.upsample
= merged_1v_upsample
;
393 upsample
->upmethod
= h2v1_merged_upsample
;
394 /* No spare row needed */
395 upsample
->spare_row
= NULL
;
398 build_ycc_rgb_table(cinfo
);
401 #endif /* UPSAMPLE_MERGING_SUPPORTED */