/src/dcmtk/dcmjpeg/libijg16/jdmerge.c
Line | Count | Source |
1 | | /* |
2 | | * jdmerge.c |
3 | | * |
4 | | * Copyright (C) 1994-1996, Thomas G. Lane. |
5 | | * This file is part of the Independent JPEG Group's software. |
6 | | * For conditions of distribution and use, see the accompanying README file. |
7 | | * |
8 | | * This file contains code for merged upsampling/color conversion. |
9 | | * |
10 | | * This file combines functions from jdsample.c and jdcolor.c; |
11 | | * read those files first to understand what's going on. |
12 | | * |
13 | | * When the chroma components are to be upsampled by simple replication |
14 | | * (ie, box filtering), we can save some work in color conversion by |
15 | | * calculating all the output pixels corresponding to a pair of chroma |
16 | | * samples at one time. In the conversion equations |
17 | | * R = Y + K1 * Cr |
18 | | * G = Y + K2 * Cb + K3 * Cr |
19 | | * B = Y + K4 * Cb |
20 | | * only the Y term varies among the group of pixels corresponding to a pair |
21 | | * of chroma samples, so the rest of the terms can be calculated just once. |
22 | | * At typical sampling ratios, this eliminates half or three-quarters of the |
23 | | * multiplications needed for color conversion. |
24 | | * |
25 | | * This file currently provides implementations for the following cases: |
26 | | * YCbCr => RGB color conversion only. |
27 | | * Sampling ratios of 2h1v or 2h2v. |
28 | | * No scaling needed at upsample time. |
29 | | * Corner-aligned (non-CCIR601) sampling alignment. |
30 | | * Other special cases could be added, but in most applications these are |
31 | | * the only common cases. (For uncommon cases we fall back on the more |
32 | | * general code in jdsample.c and jdcolor.c.) |
33 | | */ |
34 | | |
35 | | #define JPEG_INTERNALS |
36 | | #include "dcmtk/config/osconfig.h" |
37 | | #include "jinclude16.h" |
38 | | #include "jpeglib16.h" |
39 | | |
40 | | /* check if we have a 64-bit integer type */ |
41 | | #if SIZEOF_LONG == 8 |
42 | | typedef long jdmerge_sint64; |
43 | | #elif defined(_WIN32) |
44 | | typedef __int64 jdmerge_sint64; |
45 | | #elif defined(HAVE_LONG_LONG) |
46 | | typedef long long jdmerge_sint64; |
47 | | #else |
48 | | #define JDMERGE_NO_SINT64 |
49 | | #endif |
50 | | |
51 | | #ifdef UPSAMPLE_MERGING_SUPPORTED |
52 | | |
53 | | |
54 | | /* Private subobject */ |
55 | | |
56 | | typedef struct { |
57 | | struct jpeg_upsampler pub; /* public fields */ |
58 | | |
59 | | /* Pointer to routine to do actual upsampling/conversion of one row group */ |
60 | | JMETHOD(void, upmethod, (j_decompress_ptr cinfo, |
61 | | JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, |
62 | | JSAMPARRAY output_buf)); |
63 | | |
64 | | /* Private state for YCC->RGB conversion */ |
65 | | int * Cr_r_tab; /* => table for Cr to R conversion */ |
66 | | int * Cb_b_tab; /* => table for Cb to B conversion */ |
67 | | IJG_INT32 * Cr_g_tab; /* => table for Cr to G conversion */ |
68 | | IJG_INT32 * Cb_g_tab; /* => table for Cb to G conversion */ |
69 | | |
70 | | /* For 2:1 vertical sampling, we produce two output rows at a time. |
71 | | * We need a "spare" row buffer to hold the second output row if the |
72 | | * application provides just a one-row buffer; we also use the spare |
73 | | * to discard the dummy last row if the image height is odd. |
74 | | */ |
75 | | JSAMPROW spare_row; |
76 | | boolean spare_full; /* T if spare buffer is occupied */ |
77 | | |
78 | | JDIMENSION out_row_width; /* samples per output row */ |
79 | | JDIMENSION rows_to_go; /* counts rows remaining in image */ |
80 | | } my_upsampler; |
81 | | |
82 | | typedef my_upsampler * my_upsample_ptr; |
83 | | |
84 | | #define SCALEBITS 16 /* speediest right-shift on some machines */ |
85 | | #define ONE_HALF ((IJG_INT32) 1 << (SCALEBITS-1)) |
86 | | #define FIX(x) ((IJG_INT32) ((x) * (1L<<SCALEBITS) + 0.5)) |
87 | | |
88 | | |
89 | | /* |
90 | | * Initialize tables for YCC->RGB colorspace conversion. |
91 | | * This is taken directly from jdcolor.c; see that file for more info. |
92 | | */ |
93 | | |
94 | | LOCAL(void) |
95 | | build_ycc_rgb_table (j_decompress_ptr cinfo) |
96 | | { |
97 | | my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; |
98 | | int i; |
99 | | IJG_INT32 x; |
100 | | SHIFT_TEMPS |
101 | | |
102 | | upsample->Cr_r_tab = (int *) |
103 | | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
104 | | (MAXJSAMPLE+1) * SIZEOF(int)); |
105 | | upsample->Cb_b_tab = (int *) |
106 | | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
107 | | (MAXJSAMPLE+1) * SIZEOF(int)); |
108 | | upsample->Cr_g_tab = (IJG_INT32 *) |
109 | | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
110 | | (MAXJSAMPLE+1) * SIZEOF(IJG_INT32)); |
111 | | upsample->Cb_g_tab = (IJG_INT32 *) |
112 | | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
113 | | (MAXJSAMPLE+1) * SIZEOF(IJG_INT32)); |
114 | | |
115 | | for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) { |
116 | | /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */ |
117 | | /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */ |
118 | | #ifdef JDMERGE_NO_SINT64 |
119 | | /* Cr=>R value is nearest int to 1.40200 * x */ |
120 | | upsample->Cr_r_tab[i] = (int) |
121 | | RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS); |
122 | | /* Cb=>B value is nearest int to 1.77200 * x */ |
123 | | upsample->Cb_b_tab[i] = (int) |
124 | | RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS); |
125 | | #else |
126 | | /* Cr=>R value is nearest int to 1.40200 * x */ |
127 | | upsample->Cr_r_tab[i] = (int) |
128 | | RIGHT_SHIFT((jdmerge_sint64) FIX(1.40200) * x + ONE_HALF, SCALEBITS); |
129 | | /* Cb=>B value is nearest int to 1.77200 * x */ |
130 | | upsample->Cb_b_tab[i] = (int) |
131 | | RIGHT_SHIFT((jdmerge_sint64) FIX(1.77200) * x + ONE_HALF, SCALEBITS); |
132 | | #endif |
133 | | /* Cr=>G value is scaled-up -0.71414 * x */ |
134 | | upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x; |
135 | | /* Cb=>G value is scaled-up -0.34414 * x */ |
136 | | /* We also add in ONE_HALF so that need not do it in inner loop */ |
137 | | upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF; |
138 | | } |
139 | | } |
140 | | |
141 | | |
142 | | /* |
143 | | * Initialize for an upsampling pass. |
144 | | */ |
145 | | |
146 | | METHODDEF(void) |
147 | | start_pass_merged_upsample (j_decompress_ptr cinfo) |
148 | | { |
149 | | my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; |
150 | | |
151 | | /* Mark the spare buffer empty */ |
152 | | upsample->spare_full = FALSE; |
153 | | /* Initialize total-height counter for detecting bottom of image */ |
154 | | upsample->rows_to_go = cinfo->output_height; |
155 | | } |
156 | | |
157 | | |
158 | | /* |
159 | | * Control routine to do upsampling (and color conversion). |
160 | | * |
161 | | * The control routine just handles the row buffering considerations. |
162 | | */ |
163 | | |
164 | | METHODDEF(void) |
165 | | merged_2v_upsample (j_decompress_ptr cinfo, |
166 | | JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, |
167 | | JDIMENSION in_row_groups_avail, |
168 | | JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, |
169 | | JDIMENSION out_rows_avail) |
170 | | /* 2:1 vertical sampling case: may need a spare row. */ |
171 | | { |
172 | | (void) in_row_groups_avail; |
173 | | my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; |
174 | | JSAMPROW work_ptrs[2]; |
175 | | JDIMENSION num_rows; /* number of rows returned to caller */ |
176 | | |
177 | | if (upsample->spare_full) { |
178 | | /* If we have a spare row saved from a previous cycle, just return it. */ |
179 | | jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0, |
180 | | 1, upsample->out_row_width); |
181 | | num_rows = 1; |
182 | | upsample->spare_full = FALSE; |
183 | | } else { |
184 | | /* Figure number of rows to return to caller. */ |
185 | | num_rows = 2; |
186 | | /* Not more than the distance to the end of the image. */ |
187 | | if (num_rows > upsample->rows_to_go) |
188 | | num_rows = upsample->rows_to_go; |
189 | | /* And not more than what the client can accept: */ |
190 | | out_rows_avail -= *out_row_ctr; |
191 | | if (num_rows > out_rows_avail) |
192 | | num_rows = out_rows_avail; |
193 | | /* Create output pointer array for upsampler. */ |
194 | | work_ptrs[0] = output_buf[*out_row_ctr]; |
195 | | if (num_rows > 1) { |
196 | | work_ptrs[1] = output_buf[*out_row_ctr + 1]; |
197 | | } else { |
198 | | work_ptrs[1] = upsample->spare_row; |
199 | | upsample->spare_full = TRUE; |
200 | | } |
201 | | /* Now do the upsampling. */ |
202 | | (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs); |
203 | | } |
204 | | |
205 | | /* Adjust counts */ |
206 | | *out_row_ctr += num_rows; |
207 | | upsample->rows_to_go -= num_rows; |
208 | | /* When the buffer is emptied, declare this input row group consumed */ |
209 | | if (! upsample->spare_full) |
210 | | (*in_row_group_ctr)++; |
211 | | } |
212 | | |
213 | | |
214 | | METHODDEF(void) |
215 | | merged_1v_upsample (j_decompress_ptr cinfo, |
216 | | JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, |
217 | | JDIMENSION in_row_groups_avail, |
218 | | JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, |
219 | | JDIMENSION out_rows_avail) |
220 | | /* 1:1 vertical sampling case: much easier, never need a spare row. */ |
221 | | { |
222 | | (void) in_row_groups_avail; |
223 | | (void) out_rows_avail; |
224 | | my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; |
225 | | |
226 | | /* Just do the upsampling. */ |
227 | | (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, |
228 | | output_buf + *out_row_ctr); |
229 | | /* Adjust counts */ |
230 | | (*out_row_ctr)++; |
231 | | (*in_row_group_ctr)++; |
232 | | } |
233 | | |
234 | | |
235 | | /* |
236 | | * These are the routines invoked by the control routines to do |
237 | | * the actual upsampling/conversion. One row group is processed per call. |
238 | | * |
239 | | * Note: since we may be writing directly into application-supplied buffers, |
240 | | * we have to be honest about the output width; we can't assume the buffer |
241 | | * has been rounded up to an even width. |
242 | | */ |
243 | | |
244 | | |
245 | | /* |
246 | | * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical. |
247 | | */ |
248 | | |
249 | | METHODDEF(void) |
250 | | h2v1_merged_upsample (j_decompress_ptr cinfo, |
251 | | JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, |
252 | | JSAMPARRAY output_buf) |
253 | | { |
254 | | my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; |
255 | | register int y, cred, cgreen, cblue; |
256 | | int cb, cr; |
257 | | register JSAMPROW outptr; |
258 | | JSAMPROW inptr0, inptr1, inptr2; |
259 | | JDIMENSION col; |
260 | | /* copy these pointers into registers if possible */ |
261 | | register JSAMPLE * range_limit = cinfo->sample_range_limit; |
262 | | int * Crrtab = upsample->Cr_r_tab; |
263 | | int * Cbbtab = upsample->Cb_b_tab; |
264 | | IJG_INT32 * Crgtab = upsample->Cr_g_tab; |
265 | | IJG_INT32 * Cbgtab = upsample->Cb_g_tab; |
266 | | SHIFT_TEMPS |
267 | | |
268 | | inptr0 = input_buf[0][in_row_group_ctr]; |
269 | | inptr1 = input_buf[1][in_row_group_ctr]; |
270 | | inptr2 = input_buf[2][in_row_group_ctr]; |
271 | | outptr = output_buf[0]; |
272 | | /* Loop for each pair of output pixels */ |
273 | | for (col = cinfo->output_width >> 1; col > 0; col--) { |
274 | | /* Do the chroma part of the calculation */ |
275 | | cb = GETJSAMPLE(*inptr1++); |
276 | | cr = GETJSAMPLE(*inptr2++); |
277 | | cred = Crrtab[cr]; |
278 | | cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS); |
279 | | cblue = Cbbtab[cb]; |
280 | | /* Fetch 2 Y values and emit 2 pixels */ |
281 | | y = GETJSAMPLE(*inptr0++); |
282 | | outptr[RGB_RED] = range_limit[y + cred]; |
283 | | outptr[RGB_GREEN] = range_limit[y + cgreen]; |
284 | | outptr[RGB_BLUE] = range_limit[y + cblue]; |
285 | | outptr += RGB_PIXELSIZE; |
286 | | y = GETJSAMPLE(*inptr0++); |
287 | | outptr[RGB_RED] = range_limit[y + cred]; |
288 | | outptr[RGB_GREEN] = range_limit[y + cgreen]; |
289 | | outptr[RGB_BLUE] = range_limit[y + cblue]; |
290 | | outptr += RGB_PIXELSIZE; |
291 | | } |
292 | | /* If image width is odd, do the last output column separately */ |
293 | | if (cinfo->output_width & 1) { |
294 | | cb = GETJSAMPLE(*inptr1); |
295 | | cr = GETJSAMPLE(*inptr2); |
296 | | cred = Crrtab[cr]; |
297 | | cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS); |
298 | | cblue = Cbbtab[cb]; |
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 | | } |
304 | | } |
305 | | |
306 | | |
307 | | /* |
308 | | * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical. |
309 | | */ |
310 | | |
311 | | METHODDEF(void) |
312 | | h2v2_merged_upsample (j_decompress_ptr cinfo, |
313 | | JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr, |
314 | | JSAMPARRAY output_buf) |
315 | | { |
316 | | my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample; |
317 | | register int y, cred, cgreen, cblue; |
318 | | int cb, cr; |
319 | | register JSAMPROW outptr0, outptr1; |
320 | | JSAMPROW inptr00, inptr01, inptr1, inptr2; |
321 | | JDIMENSION col; |
322 | | /* copy these pointers into registers if possible */ |
323 | | register JSAMPLE * range_limit = cinfo->sample_range_limit; |
324 | | int * Crrtab = upsample->Cr_r_tab; |
325 | | int * Cbbtab = upsample->Cb_b_tab; |
326 | | IJG_INT32 * Crgtab = upsample->Cr_g_tab; |
327 | | IJG_INT32 * Cbgtab = upsample->Cb_g_tab; |
328 | | SHIFT_TEMPS |
329 | | |
330 | | inptr00 = input_buf[0][in_row_group_ctr*2]; |
331 | | inptr01 = input_buf[0][in_row_group_ctr*2 + 1]; |
332 | | inptr1 = input_buf[1][in_row_group_ctr]; |
333 | | inptr2 = input_buf[2][in_row_group_ctr]; |
334 | | outptr0 = output_buf[0]; |
335 | | outptr1 = output_buf[1]; |
336 | | /* Loop for each group of output pixels */ |
337 | | for (col = cinfo->output_width >> 1; col > 0; col--) { |
338 | | /* Do the chroma part of the calculation */ |
339 | | cb = GETJSAMPLE(*inptr1++); |
340 | | cr = GETJSAMPLE(*inptr2++); |
341 | | cred = Crrtab[cr]; |
342 | | cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS); |
343 | | cblue = Cbbtab[cb]; |
344 | | /* Fetch 4 Y values and emit 4 pixels */ |
345 | | y = GETJSAMPLE(*inptr00++); |
346 | | outptr0[RGB_RED] = range_limit[y + cred]; |
347 | | outptr0[RGB_GREEN] = range_limit[y + cgreen]; |
348 | | outptr0[RGB_BLUE] = range_limit[y + cblue]; |
349 | | outptr0 += RGB_PIXELSIZE; |
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 | | outptr0 += RGB_PIXELSIZE; |
355 | | y = GETJSAMPLE(*inptr01++); |
356 | | outptr1[RGB_RED] = range_limit[y + cred]; |
357 | | outptr1[RGB_GREEN] = range_limit[y + cgreen]; |
358 | | outptr1[RGB_BLUE] = range_limit[y + cblue]; |
359 | | outptr1 += RGB_PIXELSIZE; |
360 | | y = GETJSAMPLE(*inptr01++); |
361 | | outptr1[RGB_RED] = range_limit[y + cred]; |
362 | | outptr1[RGB_GREEN] = range_limit[y + cgreen]; |
363 | | outptr1[RGB_BLUE] = range_limit[y + cblue]; |
364 | | outptr1 += RGB_PIXELSIZE; |
365 | | } |
366 | | /* If image width is odd, do the last output column separately */ |
367 | | if (cinfo->output_width & 1) { |
368 | | cb = GETJSAMPLE(*inptr1); |
369 | | cr = GETJSAMPLE(*inptr2); |
370 | | cred = Crrtab[cr]; |
371 | | cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS); |
372 | | cblue = Cbbtab[cb]; |
373 | | y = GETJSAMPLE(*inptr00); |
374 | | outptr0[RGB_RED] = range_limit[y + cred]; |
375 | | outptr0[RGB_GREEN] = range_limit[y + cgreen]; |
376 | | outptr0[RGB_BLUE] = range_limit[y + cblue]; |
377 | | y = GETJSAMPLE(*inptr01); |
378 | | outptr1[RGB_RED] = range_limit[y + cred]; |
379 | | outptr1[RGB_GREEN] = range_limit[y + cgreen]; |
380 | | outptr1[RGB_BLUE] = range_limit[y + cblue]; |
381 | | } |
382 | | } |
383 | | |
384 | | |
385 | | /* |
386 | | * Module initialization routine for merged upsampling/color conversion. |
387 | | * |
388 | | * NB: this is called under the conditions determined by use_merged_upsample() |
389 | | * in jdmaster.c. That routine MUST correspond to the actual capabilities |
390 | | * of this module; no safety checks are made here. |
391 | | */ |
392 | | |
393 | | GLOBAL(void) |
394 | | jinit_merged_upsampler (j_decompress_ptr cinfo) |
395 | 0 | { |
396 | 0 | my_upsample_ptr upsample; |
397 | |
|
398 | 0 | upsample = (my_upsample_ptr) |
399 | 0 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
400 | 0 | SIZEOF(my_upsampler)); |
401 | 0 | cinfo->upsample = (struct jpeg_upsampler *) upsample; |
402 | 0 | upsample->pub.start_pass = start_pass_merged_upsample; |
403 | 0 | upsample->pub.need_context_rows = FALSE; |
404 | |
|
405 | 0 | upsample->out_row_width = cinfo->output_width * (JDIMENSION)cinfo->out_color_components; |
406 | |
|
407 | 0 | if (cinfo->max_v_samp_factor == 2) { |
408 | 0 | upsample->pub.upsample = merged_2v_upsample; |
409 | 0 | upsample->upmethod = h2v2_merged_upsample; |
410 | | /* Allocate a spare row buffer */ |
411 | 0 | upsample->spare_row = (JSAMPROW) |
412 | 0 | (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
413 | 0 | (size_t) (upsample->out_row_width * SIZEOF(JSAMPLE))); |
414 | 0 | } else { |
415 | 0 | upsample->pub.upsample = merged_1v_upsample; |
416 | 0 | upsample->upmethod = h2v1_merged_upsample; |
417 | | /* No spare row needed */ |
418 | 0 | upsample->spare_row = NULL; |
419 | 0 | } |
420 | |
|
421 | 0 | build_ycc_rgb_table(cinfo); |
422 | 0 | } |
423 | | |
424 | | #endif /* UPSAMPLE_MERGING_SUPPORTED */ |