Coverage Report

Created: 2023-12-08 06:53

/src/freeimage-svn/FreeImage/trunk/Source/LibJPEG/jdmerge.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jdmerge.c
3
 *
4
 * Copyright (C) 1994-1996, Thomas G. Lane.
5
 * Modified 2013-2019 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.
8
 *
9
 * This file contains code for merged upsampling/color conversion.
10
 *
11
 * This file combines functions from jdsample.c and jdcolor.c;
12
 * read those files first to understand what's going on.
13
 *
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
18
 *  R = Y           + K1 * Cr
19
 *  G = Y + K2 * Cb + K3 * Cr
20
 *  B = Y + K4 * Cb
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.
25
 *
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.)
34
 */
35
36
#define JPEG_INTERNALS
37
#include "jinclude.h"
38
#include "jpeglib.h"
39
40
#ifdef UPSAMPLE_MERGING_SUPPORTED
41
42
43
#if RANGE_BITS < 2
44
  /* Deliberate syntax err */
45
  Sorry, this code requires 2 or more range extension bits.
46
#endif
47
48
49
/* Private subobject */
50
51
typedef struct {
52
  struct jpeg_upsampler pub;  /* public fields */
53
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));
58
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 */
64
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.
69
   */
70
  JSAMPROW spare_row;
71
  boolean spare_full;   /* T if spare buffer is occupied */
72
73
  JDIMENSION out_row_width; /* samples per output row */
74
  JDIMENSION rows_to_go;  /* counts rows remaining in image */
75
} my_upsampler;
76
77
typedef my_upsampler * my_upsample_ptr;
78
79
0
#define SCALEBITS 16  /* speediest right-shift on some machines */
80
0
#define ONE_HALF  ((INT32) 1 << (SCALEBITS-1))
81
0
#define FIX(x)    ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
82
83
84
/*
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.
87
 */
88
89
LOCAL(void)
90
build_ycc_rgb_table (j_decompress_ptr cinfo)
91
/* Normal case, sYCC */
92
0
{
93
0
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
94
0
  int i;
95
0
  INT32 x;
96
0
  SHIFT_TEMPS
97
98
0
  upsample->Cr_r_tab = (int *) (*cinfo->mem->alloc_small)
99
0
    ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
100
0
  upsample->Cb_b_tab = (int *) (*cinfo->mem->alloc_small)
101
0
    ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
102
0
  upsample->Cr_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
103
0
    ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
104
0
  upsample->Cb_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
105
0
    ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
106
107
0
  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
0
    upsample->Cr_r_tab[i] = (int) DESCALE(FIX(1.402) * x, SCALEBITS);
112
    /* Cb=>B value is nearest int to 1.772 * x */
113
0
    upsample->Cb_b_tab[i] = (int) DESCALE(FIX(1.772) * x, SCALEBITS);
114
    /* Cr=>G value is scaled-up -0.714136286 * x */
115
0
    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
0
    upsample->Cb_g_tab[i] = (- FIX(0.344136286)) * x + ONE_HALF;
119
0
  }
120
0
}
121
122
123
LOCAL(void)
124
build_bg_ycc_rgb_table (j_decompress_ptr cinfo)
125
/* Wide gamut case, bg-sYCC */
126
0
{
127
0
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
128
0
  int i;
129
0
  INT32 x;
130
0
  SHIFT_TEMPS
131
132
0
  upsample->Cr_r_tab = (int *) (*cinfo->mem->alloc_small)
133
0
    ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
134
0
  upsample->Cb_b_tab = (int *) (*cinfo->mem->alloc_small)
135
0
    ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
136
0
  upsample->Cr_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
137
0
    ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
138
0
  upsample->Cb_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
139
0
    ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
140
141
0
  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
0
    upsample->Cr_r_tab[i] = (int) DESCALE(FIX(2.804) * x, SCALEBITS);
146
    /* Cb=>B value is nearest int to 3.544 * x */
147
0
    upsample->Cb_b_tab[i] = (int) DESCALE(FIX(3.544) * x, SCALEBITS);
148
    /* Cr=>G value is scaled-up -1.428272572 * x */
149
0
    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
0
    upsample->Cb_g_tab[i] = (- FIX(0.688272572)) * x + ONE_HALF;
153
0
  }
154
0
}
155
156
157
/*
158
 * Initialize for an upsampling pass.
159
 */
160
161
METHODDEF(void)
162
start_pass_merged_upsample (j_decompress_ptr cinfo)
163
0
{
164
0
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
165
166
  /* Mark the spare buffer empty */
167
0
  upsample->spare_full = FALSE;
168
  /* Initialize total-height counter for detecting bottom of image */
169
0
  upsample->rows_to_go = cinfo->output_height;
170
0
}
171
172
173
/*
174
 * Control routine to do upsampling (and color conversion).
175
 *
176
 * The control routine just handles the row buffering considerations.
177
 */
178
179
METHODDEF(void)
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. */
186
0
{
187
0
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
188
0
  JSAMPROW work_ptrs[2];
189
0
  JDIMENSION num_rows;    /* number of rows returned to caller */
190
191
0
  if (upsample->spare_full) {
192
    /* If we have a spare row saved from a previous cycle, just return it. */
193
0
    jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
194
0
          1, upsample->out_row_width);
195
0
    num_rows = 1;
196
0
    upsample->spare_full = FALSE;
197
0
  } else {
198
    /* Figure number of rows to return to caller. */
199
0
    num_rows = 2;
200
    /* Not more than the distance to the end of the image. */
201
0
    if (num_rows > upsample->rows_to_go)
202
0
      num_rows = upsample->rows_to_go;
203
    /* And not more than what the client can accept: */
204
0
    out_rows_avail -= *out_row_ctr;
205
0
    if (num_rows > out_rows_avail)
206
0
      num_rows = out_rows_avail;
207
    /* Create output pointer array for upsampler. */
208
0
    work_ptrs[0] = output_buf[*out_row_ctr];
209
0
    if (num_rows > 1) {
210
0
      work_ptrs[1] = output_buf[*out_row_ctr + 1];
211
0
    } else {
212
0
      work_ptrs[1] = upsample->spare_row;
213
0
      upsample->spare_full = TRUE;
214
0
    }
215
    /* Now do the upsampling. */
216
0
    (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
217
0
  }
218
219
  /* Adjust counts */
220
0
  *out_row_ctr += num_rows;
221
0
  upsample->rows_to_go -= num_rows;
222
  /* When the buffer is emptied, declare this input row group consumed */
223
0
  if (! upsample->spare_full)
224
0
    (*in_row_group_ctr)++;
225
0
}
226
227
228
METHODDEF(void)
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. */
235
0
{
236
0
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
237
238
  /* Just do the upsampling. */
239
0
  (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
240
0
       output_buf + *out_row_ctr);
241
  /* Adjust counts */
242
0
  (*out_row_ctr)++;
243
0
  (*in_row_group_ctr)++;
244
0
}
245
246
247
/*
248
 * These are the routines invoked by the control routines to do
249
 * the actual upsampling/conversion.  One row group is processed per call.
250
 *
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.
254
 */
255
256
257
/*
258
 * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
259
 */
260
261
METHODDEF(void)
262
h2v1_merged_upsample (j_decompress_ptr cinfo,
263
          JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
264
          JSAMPARRAY output_buf)
265
0
{
266
0
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
267
0
  register int y, cred, cgreen, cblue;
268
0
  int cb, cr;
269
0
  register JSAMPROW outptr;
270
0
  JSAMPROW inptr0, inptr1, inptr2;
271
0
  JDIMENSION col;
272
  /* copy these pointers into registers if possible */
273
0
  register JSAMPLE * range_limit = cinfo->sample_range_limit;
274
0
  int * Crrtab = upsample->Cr_r_tab;
275
0
  int * Cbbtab = upsample->Cb_b_tab;
276
0
  INT32 * Crgtab = upsample->Cr_g_tab;
277
0
  INT32 * Cbgtab = upsample->Cb_g_tab;
278
0
  SHIFT_TEMPS
279
280
0
  inptr0 = input_buf[0][in_row_group_ctr];
281
0
  inptr1 = input_buf[1][in_row_group_ctr];
282
0
  inptr2 = input_buf[2][in_row_group_ctr];
283
0
  outptr = output_buf[0];
284
  /* Loop for each pair of output pixels */
285
0
  for (col = cinfo->output_width >> 1; col > 0; col--) {
286
    /* Do the chroma part of the calculation */
287
0
    cb = GETJSAMPLE(*inptr1++);
288
0
    cr = GETJSAMPLE(*inptr2++);
289
0
    cred   = Crrtab[cr];
290
0
    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
291
0
    cblue  = Cbbtab[cb];
292
    /* Fetch 2 Y values and emit 2 pixels */
293
0
    y  = GETJSAMPLE(*inptr0++);
294
0
    outptr[RGB_RED]   = range_limit[y + cred];
295
0
    outptr[RGB_GREEN] = range_limit[y + cgreen];
296
0
    outptr[RGB_BLUE]  = range_limit[y + cblue];
297
0
    outptr += RGB_PIXELSIZE;
298
0
    y  = GETJSAMPLE(*inptr0++);
299
0
    outptr[RGB_RED]   = range_limit[y + cred];
300
0
    outptr[RGB_GREEN] = range_limit[y + cgreen];
301
0
    outptr[RGB_BLUE]  = range_limit[y + cblue];
302
0
    outptr += RGB_PIXELSIZE;
303
0
  }
304
  /* If image width is odd, do the last output column separately */
305
0
  if (cinfo->output_width & 1) {
306
0
    cb = GETJSAMPLE(*inptr1);
307
0
    cr = GETJSAMPLE(*inptr2);
308
0
    cred   = Crrtab[cr];
309
0
    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
310
0
    cblue  = Cbbtab[cb];
311
0
    y  = GETJSAMPLE(*inptr0);
312
0
    outptr[RGB_RED]   = range_limit[y + cred];
313
0
    outptr[RGB_GREEN] = range_limit[y + cgreen];
314
0
    outptr[RGB_BLUE]  = range_limit[y + cblue];
315
0
  }
316
0
}
317
318
319
/*
320
 * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
321
 */
322
323
METHODDEF(void)
324
h2v2_merged_upsample (j_decompress_ptr cinfo,
325
          JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
326
          JSAMPARRAY output_buf)
327
0
{
328
0
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
329
0
  register int y, cred, cgreen, cblue;
330
0
  int cb, cr;
331
0
  register JSAMPROW outptr0, outptr1;
332
0
  JSAMPROW inptr00, inptr01, inptr1, inptr2;
333
0
  JDIMENSION col;
334
  /* copy these pointers into registers if possible */
335
0
  register JSAMPLE * range_limit = cinfo->sample_range_limit;
336
0
  int * Crrtab = upsample->Cr_r_tab;
337
0
  int * Cbbtab = upsample->Cb_b_tab;
338
0
  INT32 * Crgtab = upsample->Cr_g_tab;
339
0
  INT32 * Cbgtab = upsample->Cb_g_tab;
340
0
  SHIFT_TEMPS
341
342
0
  inptr00 = input_buf[0][in_row_group_ctr*2];
343
0
  inptr01 = input_buf[0][in_row_group_ctr*2 + 1];
344
0
  inptr1 = input_buf[1][in_row_group_ctr];
345
0
  inptr2 = input_buf[2][in_row_group_ctr];
346
0
  outptr0 = output_buf[0];
347
0
  outptr1 = output_buf[1];
348
  /* Loop for each group of output pixels */
349
0
  for (col = cinfo->output_width >> 1; col > 0; col--) {
350
    /* Do the chroma part of the calculation */
351
0
    cb = GETJSAMPLE(*inptr1++);
352
0
    cr = GETJSAMPLE(*inptr2++);
353
0
    cred   = Crrtab[cr];
354
0
    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
355
0
    cblue  = Cbbtab[cb];
356
    /* Fetch 4 Y values and emit 4 pixels */
357
0
    y  = GETJSAMPLE(*inptr00++);
358
0
    outptr0[RGB_RED]   = range_limit[y + cred];
359
0
    outptr0[RGB_GREEN] = range_limit[y + cgreen];
360
0
    outptr0[RGB_BLUE]  = range_limit[y + cblue];
361
0
    outptr0 += RGB_PIXELSIZE;
362
0
    y  = GETJSAMPLE(*inptr00++);
363
0
    outptr0[RGB_RED]   = range_limit[y + cred];
364
0
    outptr0[RGB_GREEN] = range_limit[y + cgreen];
365
0
    outptr0[RGB_BLUE]  = range_limit[y + cblue];
366
0
    outptr0 += RGB_PIXELSIZE;
367
0
    y  = GETJSAMPLE(*inptr01++);
368
0
    outptr1[RGB_RED]   = range_limit[y + cred];
369
0
    outptr1[RGB_GREEN] = range_limit[y + cgreen];
370
0
    outptr1[RGB_BLUE]  = range_limit[y + cblue];
371
0
    outptr1 += RGB_PIXELSIZE;
372
0
    y  = GETJSAMPLE(*inptr01++);
373
0
    outptr1[RGB_RED]   = range_limit[y + cred];
374
0
    outptr1[RGB_GREEN] = range_limit[y + cgreen];
375
0
    outptr1[RGB_BLUE]  = range_limit[y + cblue];
376
0
    outptr1 += RGB_PIXELSIZE;
377
0
  }
378
  /* If image width is odd, do the last output column separately */
379
0
  if (cinfo->output_width & 1) {
380
0
    cb = GETJSAMPLE(*inptr1);
381
0
    cr = GETJSAMPLE(*inptr2);
382
0
    cred   = Crrtab[cr];
383
0
    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
384
0
    cblue  = Cbbtab[cb];
385
0
    y  = GETJSAMPLE(*inptr00);
386
0
    outptr0[RGB_RED]   = range_limit[y + cred];
387
0
    outptr0[RGB_GREEN] = range_limit[y + cgreen];
388
0
    outptr0[RGB_BLUE]  = range_limit[y + cblue];
389
0
    y  = GETJSAMPLE(*inptr01);
390
0
    outptr1[RGB_RED]   = range_limit[y + cred];
391
0
    outptr1[RGB_GREEN] = range_limit[y + cgreen];
392
0
    outptr1[RGB_BLUE]  = range_limit[y + cblue];
393
0
  }
394
0
}
395
396
397
/*
398
 * Module initialization routine for merged upsampling/color conversion.
399
 *
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.
403
 */
404
405
GLOBAL(void)
406
jinit_merged_upsampler (j_decompress_ptr cinfo)
407
0
{
408
0
  my_upsample_ptr upsample;
409
410
0
  upsample = (my_upsample_ptr) (*cinfo->mem->alloc_small)
411
0
    ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_upsampler));
412
0
  cinfo->upsample = &upsample->pub;
413
0
  upsample->pub.start_pass = start_pass_merged_upsample;
414
0
  upsample->pub.need_context_rows = FALSE;
415
416
0
  upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
417
418
0
  if (cinfo->max_v_samp_factor == 2) {
419
0
    upsample->pub.upsample = merged_2v_upsample;
420
0
    upsample->upmethod = h2v2_merged_upsample;
421
    /* Allocate a spare row buffer */
422
0
    upsample->spare_row = (JSAMPROW) (*cinfo->mem->alloc_large)
423
0
      ((j_common_ptr) cinfo, JPOOL_IMAGE,
424
0
       (size_t) upsample->out_row_width * SIZEOF(JSAMPLE));
425
0
  } else {
426
0
    upsample->pub.upsample = merged_1v_upsample;
427
0
    upsample->upmethod = h2v1_merged_upsample;
428
    /* No spare row needed */
429
0
    upsample->spare_row = NULL;
430
0
  }
431
432
0
  if (cinfo->jpeg_color_space == JCS_BG_YCC)
433
0
    build_bg_ycc_rgb_table(cinfo);
434
0
  else
435
0
    build_ycc_rgb_table(cinfo);
436
0
}
437
438
#endif /* UPSAMPLE_MERGING_SUPPORTED */