8 * Copyright (C) 1994-1996, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains code for merged upsampling/color conversion.
14 * This file combines functions from jdsample.c and jdcolor.c;
15 * read those files first to understand what's going on.
17 * When the chroma components are to be upsampled by simple replication
18 * (ie, box filtering), we can save some work in color conversion by
19 * calculating all the output pixels corresponding to a pair of chroma
20 * samples at one time. In the conversion equations
22 * G = Y + K2 * Cb + K3 * Cr
24 * only the Y term varies among the group of pixels corresponding to a pair
25 * of chroma samples, so the rest of the terms can be calculated just once.
26 * At typical sampling ratios, this eliminates half or three-quarters of the
27 * multiplications needed for color conversion.
29 * This file currently provides implementations for the following cases:
30 * YCbCr => RGB color conversion only.
31 * Sampling ratios of 2h1v or 2h2v.
32 * No scaling needed at upsample time.
33 * Corner-aligned (non-CCIR601) sampling alignment.
34 * Other special cases could be added, but in most applications these are
35 * the only common cases. (For uncommon cases we fall back on the more
36 * general code in jdsample.c and jdcolor.c.)
39 #define JPEG_INTERNALS
43 #ifdef UPSAMPLE_MERGING_SUPPORTED
46 /* Private subobject */
49 struct jpeg_upsampler pub
; /* public fields */
51 /* Pointer to routine to do actual upsampling/conversion of one row group */
52 JMETHOD(void, upmethod
, (j_decompress_ptr cinfo
,
53 JSAMPIMAGE input_buf
, JDIMENSION in_row_group_ctr
,
54 JSAMPARRAY output_buf
));
56 /* Private state for YCC->RGB conversion */
57 int * Cr_r_tab
; /* => table for Cr to R conversion */
58 int * Cb_b_tab
; /* => table for Cb to B conversion */
59 INT32
* Cr_g_tab
; /* => table for Cr to G conversion */
60 INT32
* Cb_g_tab
; /* => table for Cb to G conversion */
62 /* For 2:1 vertical sampling, we produce two output rows at a time.
63 * We need a "spare" row buffer to hold the second output row if the
64 * application provides just a one-row buffer; we also use the spare
65 * to discard the dummy last row if the image height is odd.
68 boolean spare_full
; /* T if spare buffer is occupied */
70 JDIMENSION out_row_width
; /* samples per output row */
71 JDIMENSION rows_to_go
; /* counts rows remaining in image */
74 typedef my_upsampler
* my_upsample_ptr
;
76 #define SCALEBITS 16 /* speediest right-shift on some machines */
77 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
78 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
82 * Initialize tables for YCC->RGB colorspace conversion.
83 * This is taken directly from jdcolor.c; see that file for more info.
87 build_ycc_rgb_table (j_decompress_ptr cinfo
)
89 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
94 upsample
->Cr_r_tab
= (int *)
95 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
96 (MAXJSAMPLE
+1) * SIZEOF(int));
97 upsample
->Cb_b_tab
= (int *)
98 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
99 (MAXJSAMPLE
+1) * SIZEOF(int));
100 upsample
->Cr_g_tab
= (INT32
*)
101 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
102 (MAXJSAMPLE
+1) * SIZEOF(INT32
));
103 upsample
->Cb_g_tab
= (INT32
*)
104 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
105 (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.40200 * x */
111 upsample
->Cr_r_tab
[i
] = (int)
112 RIGHT_SHIFT(FIX(1.40200) * x
+ ONE_HALF
, SCALEBITS
);
113 /* Cb=>B value is nearest int to 1.77200 * x */
114 upsample
->Cb_b_tab
[i
] = (int)
115 RIGHT_SHIFT(FIX(1.77200) * x
+ ONE_HALF
, SCALEBITS
);
116 /* Cr=>G value is scaled-up -0.71414 * x */
117 upsample
->Cr_g_tab
[i
] = (- FIX(0.71414)) * x
;
118 /* Cb=>G value is scaled-up -0.34414 * x */
119 /* We also add in ONE_HALF so that need not do it in inner loop */
120 upsample
->Cb_g_tab
[i
] = (- FIX(0.34414)) * x
+ ONE_HALF
;
126 * Initialize for an upsampling pass.
130 start_pass_merged_upsample (j_decompress_ptr cinfo
)
132 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
134 /* Mark the spare buffer empty */
135 upsample
->spare_full
= FALSE
;
136 /* Initialize total-height counter for detecting bottom of image */
137 upsample
->rows_to_go
= cinfo
->output_height
;
142 * Control routine to do upsampling (and color conversion).
144 * The control routine just handles the row buffering considerations.
148 merged_2v_upsample (j_decompress_ptr cinfo
,
149 JSAMPIMAGE input_buf
, JDIMENSION
*in_row_group_ctr
,
150 JDIMENSION in_row_groups_avail
,
151 JSAMPARRAY output_buf
, JDIMENSION
*out_row_ctr
,
152 JDIMENSION out_rows_avail
)
153 /* 2:1 vertical sampling case: may need a spare row. */
155 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
156 JSAMPROW work_ptrs
[2];
157 JDIMENSION num_rows
; /* number of rows returned to caller */
159 if (upsample
->spare_full
) {
160 /* If we have a spare row saved from a previous cycle, just return it. */
161 jcopy_sample_rows(& upsample
->spare_row
, 0, output_buf
+ *out_row_ctr
, 0,
162 1, upsample
->out_row_width
);
164 upsample
->spare_full
= FALSE
;
166 /* Figure number of rows to return to caller. */
168 /* Not more than the distance to the end of the image. */
169 if (num_rows
> upsample
->rows_to_go
)
170 num_rows
= upsample
->rows_to_go
;
171 /* And not more than what the client can accept: */
172 out_rows_avail
-= *out_row_ctr
;
173 if (num_rows
> out_rows_avail
)
174 num_rows
= out_rows_avail
;
175 /* Create output pointer array for upsampler. */
176 work_ptrs
[0] = output_buf
[*out_row_ctr
];
178 work_ptrs
[1] = output_buf
[*out_row_ctr
+ 1];
180 work_ptrs
[1] = upsample
->spare_row
;
181 upsample
->spare_full
= TRUE
;
183 /* Now do the upsampling. */
184 (*upsample
->upmethod
) (cinfo
, input_buf
, *in_row_group_ctr
, work_ptrs
);
188 *out_row_ctr
+= num_rows
;
189 upsample
->rows_to_go
-= num_rows
;
190 /* When the buffer is emptied, declare this input row group consumed */
191 if (! upsample
->spare_full
)
192 (*in_row_group_ctr
)++;
197 merged_1v_upsample (j_decompress_ptr cinfo
,
198 JSAMPIMAGE input_buf
, JDIMENSION
*in_row_group_ctr
,
199 JDIMENSION in_row_groups_avail
,
200 JSAMPARRAY output_buf
, JDIMENSION
*out_row_ctr
,
201 JDIMENSION out_rows_avail
)
202 /* 1:1 vertical sampling case: much easier, never need a spare row. */
204 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
206 /* Just do the upsampling. */
207 (*upsample
->upmethod
) (cinfo
, input_buf
, *in_row_group_ctr
,
208 output_buf
+ *out_row_ctr
);
211 (*in_row_group_ctr
)++;
216 * These are the routines invoked by the control routines to do
217 * the actual upsampling/conversion. One row group is processed per call.
219 * Note: since we may be writing directly into application-supplied buffers,
220 * we have to be honest about the output width; we can't assume the buffer
221 * has been rounded up to an even width.
226 * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
230 h2v1_merged_upsample (j_decompress_ptr cinfo
,
231 JSAMPIMAGE input_buf
, JDIMENSION in_row_group_ctr
,
232 JSAMPARRAY output_buf
)
234 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
235 register int y
, cred
, cgreen
, cblue
;
237 register JSAMPROW outptr
;
238 JSAMPROW inptr0
, inptr1
, inptr2
;
240 /* copy these pointers into registers if possible */
241 register JSAMPLE
* range_limit
= cinfo
->sample_range_limit
;
242 int * Crrtab
= upsample
->Cr_r_tab
;
243 int * Cbbtab
= upsample
->Cb_b_tab
;
244 INT32
* Crgtab
= upsample
->Cr_g_tab
;
245 INT32
* Cbgtab
= upsample
->Cb_g_tab
;
248 inptr0
= input_buf
[0][in_row_group_ctr
];
249 inptr1
= input_buf
[1][in_row_group_ctr
];
250 inptr2
= input_buf
[2][in_row_group_ctr
];
251 outptr
= output_buf
[0];
252 /* Loop for each pair of output pixels */
253 for (col
= cinfo
->output_width
>> 1; col
> 0; col
--) {
254 /* Do the chroma part of the calculation */
255 cb
= GETJSAMPLE(*inptr1
++);
256 cr
= GETJSAMPLE(*inptr2
++);
258 cgreen
= (int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
], SCALEBITS
);
260 /* Fetch 2 Y values and emit 2 pixels */
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 y
= GETJSAMPLE(*inptr0
++);
267 outptr
[RGB_RED
] = range_limit
[y
+ cred
];
268 outptr
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
269 outptr
[RGB_BLUE
] = range_limit
[y
+ cblue
];
270 outptr
+= RGB_PIXELSIZE
;
272 /* If image width is odd, do the last output column separately */
273 if (cinfo
->output_width
& 1) {
274 cb
= GETJSAMPLE(*inptr1
);
275 cr
= GETJSAMPLE(*inptr2
);
277 cgreen
= (int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
], SCALEBITS
);
279 y
= GETJSAMPLE(*inptr0
);
280 outptr
[RGB_RED
] = range_limit
[y
+ cred
];
281 outptr
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
282 outptr
[RGB_BLUE
] = range_limit
[y
+ cblue
];
288 * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
292 h2v2_merged_upsample (j_decompress_ptr cinfo
,
293 JSAMPIMAGE input_buf
, JDIMENSION in_row_group_ctr
,
294 JSAMPARRAY output_buf
)
296 my_upsample_ptr upsample
= (my_upsample_ptr
) cinfo
->upsample
;
297 register int y
, cred
, cgreen
, cblue
;
299 register JSAMPROW outptr0
, outptr1
;
300 JSAMPROW inptr00
, inptr01
, inptr1
, inptr2
;
302 /* copy these pointers into registers if possible */
303 register JSAMPLE
* range_limit
= cinfo
->sample_range_limit
;
304 int * Crrtab
= upsample
->Cr_r_tab
;
305 int * Cbbtab
= upsample
->Cb_b_tab
;
306 INT32
* Crgtab
= upsample
->Cr_g_tab
;
307 INT32
* Cbgtab
= upsample
->Cb_g_tab
;
310 inptr00
= input_buf
[0][in_row_group_ctr
*2];
311 inptr01
= input_buf
[0][in_row_group_ctr
*2 + 1];
312 inptr1
= input_buf
[1][in_row_group_ctr
];
313 inptr2
= input_buf
[2][in_row_group_ctr
];
314 outptr0
= output_buf
[0];
315 outptr1
= output_buf
[1];
316 /* Loop for each group of output pixels */
317 for (col
= cinfo
->output_width
>> 1; col
> 0; col
--) {
318 /* Do the chroma part of the calculation */
319 cb
= GETJSAMPLE(*inptr1
++);
320 cr
= GETJSAMPLE(*inptr2
++);
322 cgreen
= (int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
], SCALEBITS
);
324 /* Fetch 4 Y values and emit 4 pixels */
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(*inptr00
++);
331 outptr0
[RGB_RED
] = range_limit
[y
+ cred
];
332 outptr0
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
333 outptr0
[RGB_BLUE
] = range_limit
[y
+ cblue
];
334 outptr0
+= 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 y
= GETJSAMPLE(*inptr01
++);
341 outptr1
[RGB_RED
] = range_limit
[y
+ cred
];
342 outptr1
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
343 outptr1
[RGB_BLUE
] = range_limit
[y
+ cblue
];
344 outptr1
+= RGB_PIXELSIZE
;
346 /* If image width is odd, do the last output column separately */
347 if (cinfo
->output_width
& 1) {
348 cb
= GETJSAMPLE(*inptr1
);
349 cr
= GETJSAMPLE(*inptr2
);
351 cgreen
= (int) RIGHT_SHIFT(Cbgtab
[cb
] + Crgtab
[cr
], SCALEBITS
);
353 y
= GETJSAMPLE(*inptr00
);
354 outptr0
[RGB_RED
] = range_limit
[y
+ cred
];
355 outptr0
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
356 outptr0
[RGB_BLUE
] = range_limit
[y
+ cblue
];
357 y
= GETJSAMPLE(*inptr01
);
358 outptr1
[RGB_RED
] = range_limit
[y
+ cred
];
359 outptr1
[RGB_GREEN
] = range_limit
[y
+ cgreen
];
360 outptr1
[RGB_BLUE
] = range_limit
[y
+ cblue
];
366 * Module initialization routine for merged upsampling/color conversion.
368 * NB: this is called under the conditions determined by use_merged_upsample()
369 * in jdmaster.c. That routine MUST correspond to the actual capabilities
370 * of this module; no safety checks are made here.
374 jinit_merged_upsampler (j_decompress_ptr cinfo
)
376 my_upsample_ptr upsample
;
378 upsample
= (my_upsample_ptr
)
379 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
380 SIZEOF(my_upsampler
));
381 cinfo
->upsample
= (struct jpeg_upsampler
*) upsample
;
382 upsample
->pub
.start_pass
= start_pass_merged_upsample
;
383 upsample
->pub
.need_context_rows
= FALSE
;
385 upsample
->out_row_width
= cinfo
->output_width
* cinfo
->out_color_components
;
387 if (cinfo
->max_v_samp_factor
== 2) {
388 upsample
->pub
.upsample
= merged_2v_upsample
;
389 upsample
->upmethod
= h2v2_merged_upsample
;
390 /* Allocate a spare row buffer */
391 upsample
->spare_row
= (JSAMPROW
)
392 (*cinfo
->mem
->alloc_large
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
393 (size_t) (upsample
->out_row_width
* SIZEOF(JSAMPLE
)));
395 upsample
->pub
.upsample
= merged_1v_upsample
;
396 upsample
->upmethod
= h2v1_merged_upsample
;
397 /* No spare row needed */
398 upsample
->spare_row
= NULL
;
401 build_ycc_rgb_table(cinfo
);
404 #endif /* UPSAMPLE_MERGING_SUPPORTED */