Coverage Report

Created: 2026-06-30 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dcmtk/dcmjpeg/libijg12/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 "jinclude12.h"
37
#include "jpeglib12.h"
38
39
#ifdef UPSAMPLE_MERGING_SUPPORTED
40
41
42
/* Private subobject */
43
44
typedef struct {
45
  struct jpeg_upsampler pub;    /* public fields */
46
47
  /* Pointer to routine to do actual upsampling/conversion of one row group */
48
  JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
49
               JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
50
               JSAMPARRAY output_buf));
51
52
  /* Private state for YCC->RGB conversion */
53
  int * Cr_r_tab;       /* => table for Cr to R conversion */
54
  int * Cb_b_tab;       /* => table for Cb to B conversion */
55
  IJG_INT32 * Cr_g_tab;     /* => table for Cr to G conversion */
56
  IJG_INT32 * Cb_g_tab;     /* => table for Cb to G conversion */
57
58
  /* For 2:1 vertical sampling, we produce two output rows at a time.
59
   * We need a "spare" row buffer to hold the second output row if the
60
   * application provides just a one-row buffer; we also use the spare
61
   * to discard the dummy last row if the image height is odd.
62
   */
63
  JSAMPROW spare_row;
64
  boolean spare_full;       /* T if spare buffer is occupied */
65
66
  JDIMENSION out_row_width; /* samples per output row */
67
  JDIMENSION rows_to_go;    /* counts rows remaining in image */
68
} my_upsampler;
69
70
typedef my_upsampler * my_upsample_ptr;
71
72
#define SCALEBITS   16  /* speediest right-shift on some machines */
73
#define ONE_HALF    ((IJG_INT32) 1 << (SCALEBITS-1))
74
#define FIX(x)      ((IJG_INT32) ((x) * (1L<<SCALEBITS) + 0.5))
75
76
77
/*
78
 * Initialize tables for YCC->RGB colorspace conversion.
79
 * This is taken directly from jdcolor.c; see that file for more info.
80
 */
81
82
LOCAL(void)
83
build_ycc_rgb_table (j_decompress_ptr cinfo)
84
{
85
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
86
  int i;
87
  IJG_INT32 x;
88
  SHIFT_TEMPS
89
90
  upsample->Cr_r_tab = (int *)
91
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
92
                (MAXJSAMPLE+1) * SIZEOF(int));
93
  upsample->Cb_b_tab = (int *)
94
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
95
                (MAXJSAMPLE+1) * SIZEOF(int));
96
  upsample->Cr_g_tab = (IJG_INT32 *)
97
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
98
                (MAXJSAMPLE+1) * SIZEOF(IJG_INT32));
99
  upsample->Cb_g_tab = (IJG_INT32 *)
100
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
101
                (MAXJSAMPLE+1) * SIZEOF(IJG_INT32));
102
103
  for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
104
    /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
105
    /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
106
    /* Cr=>R value is nearest int to 1.40200 * x */
107
    upsample->Cr_r_tab[i] = (int)
108
            RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
109
    /* Cb=>B value is nearest int to 1.77200 * x */
110
    upsample->Cb_b_tab[i] = (int)
111
            RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
112
    /* Cr=>G value is scaled-up -0.71414 * x */
113
    upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x;
114
    /* Cb=>G value is scaled-up -0.34414 * x */
115
    /* We also add in ONE_HALF so that need not do it in inner loop */
116
    upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
117
  }
118
}
119
120
121
/*
122
 * Initialize for an upsampling pass.
123
 */
124
125
METHODDEF(void)
126
start_pass_merged_upsample (j_decompress_ptr cinfo)
127
{
128
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
129
130
  /* Mark the spare buffer empty */
131
  upsample->spare_full = FALSE;
132
  /* Initialize total-height counter for detecting bottom of image */
133
  upsample->rows_to_go = cinfo->output_height;
134
}
135
136
137
/*
138
 * Control routine to do upsampling (and color conversion).
139
 *
140
 * The control routine just handles the row buffering considerations.
141
 */
142
143
METHODDEF(void)
144
merged_2v_upsample (j_decompress_ptr cinfo,
145
            JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
146
            JDIMENSION in_row_groups_avail,
147
            JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
148
            JDIMENSION out_rows_avail)
149
/* 2:1 vertical sampling case: may need a spare row. */
150
{
151
  (void) in_row_groups_avail;
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 */
155
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);
160
    num_rows = 1;
161
    upsample->spare_full = FALSE;
162
  } else {
163
    /* Figure number of rows to return to caller. */
164
    num_rows = 2;
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];
174
    if (num_rows > 1) {
175
      work_ptrs[1] = output_buf[*out_row_ctr + 1];
176
    } else {
177
      work_ptrs[1] = upsample->spare_row;
178
      upsample->spare_full = TRUE;
179
    }
180
    /* Now do the upsampling. */
181
    (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
182
  }
183
184
  /* Adjust counts */
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)++;
190
}
191
192
193
METHODDEF(void)
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. */
200
{
201
  (void) in_row_groups_avail;
202
  (void) out_rows_avail;
203
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
204
205
  /* Just do the upsampling. */
206
  (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
207
             output_buf + *out_row_ctr);
208
  /* Adjust counts */
209
  (*out_row_ctr)++;
210
  (*in_row_group_ctr)++;
211
}
212
213
214
/*
215
 * These are the routines invoked by the control routines to do
216
 * the actual upsampling/conversion.  One row group is processed per call.
217
 *
218
 * Note: since we may be writing directly into application-supplied buffers,
219
 * we have to be honest about the output width; we can't assume the buffer
220
 * has been rounded up to an even width.
221
 */
222
223
224
/*
225
 * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
226
 */
227
228
METHODDEF(void)
229
h2v1_merged_upsample (j_decompress_ptr cinfo,
230
              JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
231
              JSAMPARRAY output_buf)
232
{
233
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
234
  register int y, cred, cgreen, cblue;
235
  int cb, cr;
236
  register JSAMPROW outptr;
237
  JSAMPROW inptr0, inptr1, inptr2;
238
  JDIMENSION col;
239
  /* copy these pointers into registers if possible */
240
  register JSAMPLE * range_limit = cinfo->sample_range_limit;
241
  int * Crrtab = upsample->Cr_r_tab;
242
  int * Cbbtab = upsample->Cb_b_tab;
243
  IJG_INT32 * Crgtab = upsample->Cr_g_tab;
244
  IJG_INT32 * Cbgtab = upsample->Cb_g_tab;
245
  SHIFT_TEMPS
246
247
  inptr0 = input_buf[0][in_row_group_ctr];
248
  inptr1 = input_buf[1][in_row_group_ctr];
249
  inptr2 = input_buf[2][in_row_group_ctr];
250
  outptr = output_buf[0];
251
  /* Loop for each pair of output pixels */
252
  for (col = cinfo->output_width >> 1; col > 0; col--) {
253
    /* Do the chroma part of the calculation */
254
    cb = GETJSAMPLE(*inptr1++);
255
    cr = GETJSAMPLE(*inptr2++);
256
    cred = Crrtab[cr];
257
    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
258
    cblue = Cbbtab[cb];
259
    /* Fetch 2 Y values and emit 2 pixels */
260
    y  = GETJSAMPLE(*inptr0++);
261
    outptr[RGB_RED] =   range_limit[y + cred];
262
    outptr[RGB_GREEN] = range_limit[y + cgreen];
263
    outptr[RGB_BLUE] =  range_limit[y + cblue];
264
    outptr += RGB_PIXELSIZE;
265
    y  = GETJSAMPLE(*inptr0++);
266
    outptr[RGB_RED] =   range_limit[y + cred];
267
    outptr[RGB_GREEN] = range_limit[y + cgreen];
268
    outptr[RGB_BLUE] =  range_limit[y + cblue];
269
    outptr += RGB_PIXELSIZE;
270
  }
271
  /* If image width is odd, do the last output column separately */
272
  if (cinfo->output_width & 1) {
273
    cb = GETJSAMPLE(*inptr1);
274
    cr = GETJSAMPLE(*inptr2);
275
    cred = Crrtab[cr];
276
    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
277
    cblue = Cbbtab[cb];
278
    y  = GETJSAMPLE(*inptr0);
279
    outptr[RGB_RED] =   range_limit[y + cred];
280
    outptr[RGB_GREEN] = range_limit[y + cgreen];
281
    outptr[RGB_BLUE] =  range_limit[y + cblue];
282
  }
283
}
284
285
286
/*
287
 * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
288
 */
289
290
METHODDEF(void)
291
h2v2_merged_upsample (j_decompress_ptr cinfo,
292
              JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
293
              JSAMPARRAY output_buf)
294
{
295
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
296
  register int y, cred, cgreen, cblue;
297
  int cb, cr;
298
  register JSAMPROW outptr0, outptr1;
299
  JSAMPROW inptr00, inptr01, inptr1, inptr2;
300
  JDIMENSION col;
301
  /* copy these pointers into registers if possible */
302
  register JSAMPLE * range_limit = cinfo->sample_range_limit;
303
  int * Crrtab = upsample->Cr_r_tab;
304
  int * Cbbtab = upsample->Cb_b_tab;
305
  IJG_INT32 * Crgtab = upsample->Cr_g_tab;
306
  IJG_INT32 * Cbgtab = upsample->Cb_g_tab;
307
  SHIFT_TEMPS
308
309
  inptr00 = input_buf[0][in_row_group_ctr*2];
310
  inptr01 = input_buf[0][in_row_group_ctr*2 + 1];
311
  inptr1 = input_buf[1][in_row_group_ctr];
312
  inptr2 = input_buf[2][in_row_group_ctr];
313
  outptr0 = output_buf[0];
314
  outptr1 = output_buf[1];
315
  /* Loop for each group of output pixels */
316
  for (col = cinfo->output_width >> 1; col > 0; col--) {
317
    /* Do the chroma part of the calculation */
318
    cb = GETJSAMPLE(*inptr1++);
319
    cr = GETJSAMPLE(*inptr2++);
320
    cred = Crrtab[cr];
321
    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
322
    cblue = Cbbtab[cb];
323
    /* Fetch 4 Y values and emit 4 pixels */
324
    y  = GETJSAMPLE(*inptr00++);
325
    outptr0[RGB_RED] =   range_limit[y + cred];
326
    outptr0[RGB_GREEN] = range_limit[y + cgreen];
327
    outptr0[RGB_BLUE] =  range_limit[y + cblue];
328
    outptr0 += RGB_PIXELSIZE;
329
    y  = GETJSAMPLE(*inptr00++);
330
    outptr0[RGB_RED] =   range_limit[y + cred];
331
    outptr0[RGB_GREEN] = range_limit[y + cgreen];
332
    outptr0[RGB_BLUE] =  range_limit[y + cblue];
333
    outptr0 += RGB_PIXELSIZE;
334
    y  = GETJSAMPLE(*inptr01++);
335
    outptr1[RGB_RED] =   range_limit[y + cred];
336
    outptr1[RGB_GREEN] = range_limit[y + cgreen];
337
    outptr1[RGB_BLUE] =  range_limit[y + cblue];
338
    outptr1 += RGB_PIXELSIZE;
339
    y  = GETJSAMPLE(*inptr01++);
340
    outptr1[RGB_RED] =   range_limit[y + cred];
341
    outptr1[RGB_GREEN] = range_limit[y + cgreen];
342
    outptr1[RGB_BLUE] =  range_limit[y + cblue];
343
    outptr1 += RGB_PIXELSIZE;
344
  }
345
  /* If image width is odd, do the last output column separately */
346
  if (cinfo->output_width & 1) {
347
    cb = GETJSAMPLE(*inptr1);
348
    cr = GETJSAMPLE(*inptr2);
349
    cred = Crrtab[cr];
350
    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
351
    cblue = Cbbtab[cb];
352
    y  = GETJSAMPLE(*inptr00);
353
    outptr0[RGB_RED] =   range_limit[y + cred];
354
    outptr0[RGB_GREEN] = range_limit[y + cgreen];
355
    outptr0[RGB_BLUE] =  range_limit[y + cblue];
356
    y  = GETJSAMPLE(*inptr01);
357
    outptr1[RGB_RED] =   range_limit[y + cred];
358
    outptr1[RGB_GREEN] = range_limit[y + cgreen];
359
    outptr1[RGB_BLUE] =  range_limit[y + cblue];
360
  }
361
}
362
363
364
/*
365
 * Module initialization routine for merged upsampling/color conversion.
366
 *
367
 * NB: this is called under the conditions determined by use_merged_upsample()
368
 * in jdmaster.c.  That routine MUST correspond to the actual capabilities
369
 * of this module; no safety checks are made here.
370
 */
371
372
GLOBAL(void)
373
jinit_merged_upsampler (j_decompress_ptr cinfo)
374
0
{
375
0
  my_upsample_ptr upsample;
376
377
0
  upsample = (my_upsample_ptr)
378
0
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
379
0
                SIZEOF(my_upsampler));
380
0
  cinfo->upsample = (struct jpeg_upsampler *) upsample;
381
0
  upsample->pub.start_pass = start_pass_merged_upsample;
382
0
  upsample->pub.need_context_rows = FALSE;
383
384
0
  upsample->out_row_width = cinfo->output_width * (JDIMENSION)cinfo->out_color_components;
385
386
0
  if (cinfo->max_v_samp_factor == 2) {
387
0
    upsample->pub.upsample = merged_2v_upsample;
388
0
    upsample->upmethod = h2v2_merged_upsample;
389
    /* Allocate a spare row buffer */
390
0
    upsample->spare_row = (JSAMPROW)
391
0
      (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
392
0
        (size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));
393
0
  } else {
394
0
    upsample->pub.upsample = merged_1v_upsample;
395
0
    upsample->upmethod = h2v1_merged_upsample;
396
    /* No spare row needed */
397
0
    upsample->spare_row = NULL;
398
0
  }
399
400
0
  build_ycc_rgb_table(cinfo);
401
0
}
402
403
#endif /* UPSAMPLE_MERGING_SUPPORTED */