Coverage Report

Created: 2023-06-07 06:03

/src/libjpeg-turbo.2.1.x/jdcolor.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jdcolor.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1991-1997, Thomas G. Lane.
6
 * Modified 2011 by Guido Vollbeding.
7
 * libjpeg-turbo Modifications:
8
 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
9
 * Copyright (C) 2009, 2011-2012, 2014-2015, D. R. Commander.
10
 * Copyright (C) 2013, Linaro Limited.
11
 * For conditions of distribution and use, see the accompanying README.ijg
12
 * file.
13
 *
14
 * This file contains output colorspace conversion routines.
15
 */
16
17
#define JPEG_INTERNALS
18
#include "jinclude.h"
19
#include "jpeglib.h"
20
#include "jsimd.h"
21
22
23
/* Private subobject */
24
25
typedef struct {
26
  struct jpeg_color_deconverter pub; /* public fields */
27
28
  /* Private state for YCC->RGB conversion */
29
  int *Cr_r_tab;                /* => table for Cr to R conversion */
30
  int *Cb_b_tab;                /* => table for Cb to B conversion */
31
  JLONG *Cr_g_tab;              /* => table for Cr to G conversion */
32
  JLONG *Cb_g_tab;              /* => table for Cb to G conversion */
33
34
  /* Private state for RGB->Y conversion */
35
  JLONG *rgb_y_tab;             /* => table for RGB to Y conversion */
36
} my_color_deconverter;
37
38
typedef my_color_deconverter *my_cconvert_ptr;
39
40
41
/**************** YCbCr -> RGB conversion: most common case **************/
42
/****************   RGB -> Y   conversion: less common case **************/
43
44
/*
45
 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
46
 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
47
 * The conversion equations to be implemented are therefore
48
 *
49
 *      R = Y                + 1.40200 * Cr
50
 *      G = Y - 0.34414 * Cb - 0.71414 * Cr
51
 *      B = Y + 1.77200 * Cb
52
 *
53
 *      Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
54
 *
55
 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
56
 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
57
 *
58
 * To avoid floating-point arithmetic, we represent the fractional constants
59
 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
60
 * the products by 2^16, with appropriate rounding, to get the correct answer.
61
 * Notice that Y, being an integral input, does not contribute any fraction
62
 * so it need not participate in the rounding.
63
 *
64
 * For even more speed, we avoid doing any multiplications in the inner loop
65
 * by precalculating the constants times Cb and Cr for all possible values.
66
 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
67
 * for 12-bit samples it is still acceptable.  It's not very reasonable for
68
 * 16-bit samples, but if you want lossless storage you shouldn't be changing
69
 * colorspace anyway.
70
 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
71
 * values for the G calculation are left scaled up, since we must add them
72
 * together before rounding.
73
 */
74
75
54.0M
#define SCALEBITS       16      /* speediest right-shift on some machines */
76
4.00M
#define ONE_HALF        ((JLONG)1 << (SCALEBITS - 1))
77
8.16M
#define FIX(x)          ((JLONG)((x) * (1L << SCALEBITS) + 0.5))
78
79
/* We allocate one big table for RGB->Y conversion and divide it up into
80
 * three parts, instead of doing three alloc_small requests.  This lets us
81
 * use a single table base address, which can be held in a register in the
82
 * inner loops on many machines (more than can hold all three addresses,
83
 * anyway).
84
 */
85
86
42.0M
#define R_Y_OFF         0                       /* offset to R => Y section */
87
42.0M
#define G_Y_OFF         (1 * (MAXJSAMPLE + 1))  /* offset to G => Y section */
88
42.0M
#define B_Y_OFF         (2 * (MAXJSAMPLE + 1))  /* etc. */
89
216
#define TABLE_SIZE      (3 * (MAXJSAMPLE + 1))
90
91
92
/* Include inline routines for colorspace extensions */
93
94
#include "jdcolext.c"
95
#undef RGB_RED
96
#undef RGB_GREEN
97
#undef RGB_BLUE
98
#undef RGB_PIXELSIZE
99
100
1.84G
#define RGB_RED  EXT_RGB_RED
101
1.84G
#define RGB_GREEN  EXT_RGB_GREEN
102
1.84G
#define RGB_BLUE  EXT_RGB_BLUE
103
1.84G
#define RGB_PIXELSIZE  EXT_RGB_PIXELSIZE
104
#define ycc_rgb_convert_internal  ycc_extrgb_convert_internal
105
#define gray_rgb_convert_internal  gray_extrgb_convert_internal
106
#define rgb_rgb_convert_internal  rgb_extrgb_convert_internal
107
#include "jdcolext.c"
108
#undef RGB_RED
109
#undef RGB_GREEN
110
#undef RGB_BLUE
111
#undef RGB_PIXELSIZE
112
#undef ycc_rgb_convert_internal
113
#undef gray_rgb_convert_internal
114
#undef rgb_rgb_convert_internal
115
116
0
#define RGB_RED  EXT_RGBX_RED
117
0
#define RGB_GREEN  EXT_RGBX_GREEN
118
0
#define RGB_BLUE  EXT_RGBX_BLUE
119
0
#define RGB_ALPHA  3
120
0
#define RGB_PIXELSIZE  EXT_RGBX_PIXELSIZE
121
#define ycc_rgb_convert_internal  ycc_extrgbx_convert_internal
122
#define gray_rgb_convert_internal  gray_extrgbx_convert_internal
123
#define rgb_rgb_convert_internal  rgb_extrgbx_convert_internal
124
#include "jdcolext.c"
125
#undef RGB_RED
126
#undef RGB_GREEN
127
#undef RGB_BLUE
128
#undef RGB_ALPHA
129
#undef RGB_PIXELSIZE
130
#undef ycc_rgb_convert_internal
131
#undef gray_rgb_convert_internal
132
#undef rgb_rgb_convert_internal
133
134
106M
#define RGB_RED  EXT_BGR_RED
135
106M
#define RGB_GREEN  EXT_BGR_GREEN
136
106M
#define RGB_BLUE  EXT_BGR_BLUE
137
106M
#define RGB_PIXELSIZE  EXT_BGR_PIXELSIZE
138
#define ycc_rgb_convert_internal  ycc_extbgr_convert_internal
139
#define gray_rgb_convert_internal  gray_extbgr_convert_internal
140
#define rgb_rgb_convert_internal  rgb_extbgr_convert_internal
141
#include "jdcolext.c"
142
#undef RGB_RED
143
#undef RGB_GREEN
144
#undef RGB_BLUE
145
#undef RGB_PIXELSIZE
146
#undef ycc_rgb_convert_internal
147
#undef gray_rgb_convert_internal
148
#undef rgb_rgb_convert_internal
149
150
65.9M
#define RGB_RED  EXT_BGRX_RED
151
65.9M
#define RGB_GREEN  EXT_BGRX_GREEN
152
65.9M
#define RGB_BLUE  EXT_BGRX_BLUE
153
65.9M
#define RGB_ALPHA  3
154
65.9M
#define RGB_PIXELSIZE  EXT_BGRX_PIXELSIZE
155
#define ycc_rgb_convert_internal  ycc_extbgrx_convert_internal
156
#define gray_rgb_convert_internal  gray_extbgrx_convert_internal
157
#define rgb_rgb_convert_internal  rgb_extbgrx_convert_internal
158
#include "jdcolext.c"
159
#undef RGB_RED
160
#undef RGB_GREEN
161
#undef RGB_BLUE
162
#undef RGB_ALPHA
163
#undef RGB_PIXELSIZE
164
#undef ycc_rgb_convert_internal
165
#undef gray_rgb_convert_internal
166
#undef rgb_rgb_convert_internal
167
168
0
#define RGB_RED  EXT_XBGR_RED
169
0
#define RGB_GREEN  EXT_XBGR_GREEN
170
0
#define RGB_BLUE  EXT_XBGR_BLUE
171
0
#define RGB_ALPHA  0
172
0
#define RGB_PIXELSIZE  EXT_XBGR_PIXELSIZE
173
#define ycc_rgb_convert_internal  ycc_extxbgr_convert_internal
174
#define gray_rgb_convert_internal  gray_extxbgr_convert_internal
175
#define rgb_rgb_convert_internal  rgb_extxbgr_convert_internal
176
#include "jdcolext.c"
177
#undef RGB_RED
178
#undef RGB_GREEN
179
#undef RGB_BLUE
180
#undef RGB_ALPHA
181
#undef RGB_PIXELSIZE
182
#undef ycc_rgb_convert_internal
183
#undef gray_rgb_convert_internal
184
#undef rgb_rgb_convert_internal
185
186
26.0M
#define RGB_RED  EXT_XRGB_RED
187
26.0M
#define RGB_GREEN  EXT_XRGB_GREEN
188
26.0M
#define RGB_BLUE  EXT_XRGB_BLUE
189
26.0M
#define RGB_ALPHA  0
190
26.0M
#define RGB_PIXELSIZE  EXT_XRGB_PIXELSIZE
191
#define ycc_rgb_convert_internal  ycc_extxrgb_convert_internal
192
#define gray_rgb_convert_internal  gray_extxrgb_convert_internal
193
#define rgb_rgb_convert_internal  rgb_extxrgb_convert_internal
194
#include "jdcolext.c"
195
#undef RGB_RED
196
#undef RGB_GREEN
197
#undef RGB_BLUE
198
#undef RGB_ALPHA
199
#undef RGB_PIXELSIZE
200
#undef ycc_rgb_convert_internal
201
#undef gray_rgb_convert_internal
202
#undef rgb_rgb_convert_internal
203
204
205
/*
206
 * Initialize tables for YCC->RGB colorspace conversion.
207
 */
208
209
LOCAL(void)
210
build_ycc_rgb_table(j_decompress_ptr cinfo)
211
937
{
212
937
  my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
213
937
  int i;
214
937
  JLONG x;
215
937
  SHIFT_TEMPS
216
217
937
  cconvert->Cr_r_tab = (int *)
218
937
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
219
937
                                (MAXJSAMPLE + 1) * sizeof(int));
220
937
  cconvert->Cb_b_tab = (int *)
221
937
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
222
937
                                (MAXJSAMPLE + 1) * sizeof(int));
223
937
  cconvert->Cr_g_tab = (JLONG *)
224
937
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
225
937
                                (MAXJSAMPLE + 1) * sizeof(JLONG));
226
937
  cconvert->Cb_g_tab = (JLONG *)
227
937
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
228
937
                                (MAXJSAMPLE + 1) * sizeof(JLONG));
229
230
3.83M
  for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
231
    /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
232
    /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
233
    /* Cr=>R value is nearest int to 1.40200 * x */
234
3.83M
    cconvert->Cr_r_tab[i] = (int)
235
3.83M
                    RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
236
    /* Cb=>B value is nearest int to 1.77200 * x */
237
3.83M
    cconvert->Cb_b_tab[i] = (int)
238
3.83M
                    RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
239
    /* Cr=>G value is scaled-up -0.71414 * x */
240
3.83M
    cconvert->Cr_g_tab[i] = (-FIX(0.71414)) * x;
241
    /* Cb=>G value is scaled-up -0.34414 * x */
242
    /* We also add in ONE_HALF so that need not do it in inner loop */
243
3.83M
    cconvert->Cb_g_tab[i] = (-FIX(0.34414)) * x + ONE_HALF;
244
3.83M
  }
245
937
}
246
247
248
/*
249
 * Convert some rows of samples to the output colorspace.
250
 */
251
252
METHODDEF(void)
253
ycc_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
254
                JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
255
875k
{
256
875k
  switch (cinfo->out_color_space) {
257
769k
  case JCS_EXT_RGB:
258
769k
    ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
259
769k
                                num_rows);
260
769k
    break;
261
0
  case JCS_EXT_RGBX:
262
0
  case JCS_EXT_RGBA:
263
0
    ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
264
0
                                 num_rows);
265
0
    break;
266
0
  case JCS_EXT_BGR:
267
0
    ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
268
0
                                num_rows);
269
0
    break;
270
106k
  case JCS_EXT_BGRX:
271
106k
  case JCS_EXT_BGRA:
272
106k
    ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
273
106k
                                 num_rows);
274
106k
    break;
275
0
  case JCS_EXT_XBGR:
276
0
  case JCS_EXT_ABGR:
277
0
    ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
278
0
                                 num_rows);
279
0
    break;
280
0
  case JCS_EXT_XRGB:
281
0
  case JCS_EXT_ARGB:
282
0
    ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
283
0
                                 num_rows);
284
0
    break;
285
0
  default:
286
0
    ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
287
0
                             num_rows);
288
0
    break;
289
875k
  }
290
875k
}
291
292
293
/**************** Cases other than YCbCr -> RGB **************/
294
295
296
/*
297
 * Initialize for RGB->grayscale colorspace conversion.
298
 */
299
300
LOCAL(void)
301
build_rgb_y_table(j_decompress_ptr cinfo)
302
216
{
303
216
  my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
304
216
  JLONG *rgb_y_tab;
305
216
  JLONG i;
306
307
  /* Allocate and fill in the conversion tables. */
308
216
  cconvert->rgb_y_tab = rgb_y_tab = (JLONG *)
309
216
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
310
216
                                (TABLE_SIZE * sizeof(JLONG)));
311
312
163k
  for (i = 0; i <= MAXJSAMPLE; i++) {
313
162k
    rgb_y_tab[i + R_Y_OFF] = FIX(0.29900) * i;
314
162k
    rgb_y_tab[i + G_Y_OFF] = FIX(0.58700) * i;
315
162k
    rgb_y_tab[i + B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
316
162k
  }
317
216
}
318
319
320
/*
321
 * Convert RGB to grayscale.
322
 */
323
324
METHODDEF(void)
325
rgb_gray_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
326
                 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
327
472k
{
328
472k
  my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
329
472k
  register int r, g, b;
330
472k
  register JLONG *ctab = cconvert->rgb_y_tab;
331
472k
  register JSAMPROW outptr;
332
472k
  register JSAMPROW inptr0, inptr1, inptr2;
333
472k
  register JDIMENSION col;
334
472k
  JDIMENSION num_cols = cinfo->output_width;
335
336
1.41M
  while (--num_rows >= 0) {
337
943k
    inptr0 = input_buf[0][input_row];
338
943k
    inptr1 = input_buf[1][input_row];
339
943k
    inptr2 = input_buf[2][input_row];
340
943k
    input_row++;
341
943k
    outptr = *output_buf++;
342
42.8M
    for (col = 0; col < num_cols; col++) {
343
41.8M
      r = inptr0[col];
344
41.8M
      g = inptr1[col];
345
41.8M
      b = inptr2[col];
346
      /* Y */
347
41.8M
      outptr[col] = (JSAMPLE)((ctab[r + R_Y_OFF] + ctab[g + G_Y_OFF] +
348
41.8M
                               ctab[b + B_Y_OFF]) >> SCALEBITS);
349
41.8M
    }
350
943k
  }
351
472k
}
352
353
354
/*
355
 * Color conversion for no colorspace change: just copy the data,
356
 * converting from separate-planes to interleaved representation.
357
 */
358
359
METHODDEF(void)
360
null_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
361
             JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
362
1.07M
{
363
1.07M
  register JSAMPROW inptr, inptr0, inptr1, inptr2, inptr3, outptr;
364
1.07M
  register JDIMENSION col;
365
1.07M
  register int num_components = cinfo->num_components;
366
1.07M
  JDIMENSION num_cols = cinfo->output_width;
367
1.07M
  int ci;
368
369
1.07M
  if (num_components == 3) {
370
3.41M
    while (--num_rows >= 0) {
371
2.33M
      inptr0 = input_buf[0][input_row];
372
2.33M
      inptr1 = input_buf[1][input_row];
373
2.33M
      inptr2 = input_buf[2][input_row];
374
2.33M
      input_row++;
375
2.33M
      outptr = *output_buf++;
376
76.7M
      for (col = 0; col < num_cols; col++) {
377
74.4M
        *outptr++ = inptr0[col];
378
74.4M
        *outptr++ = inptr1[col];
379
74.4M
        *outptr++ = inptr2[col];
380
74.4M
      }
381
2.33M
    }
382
1.07M
  } else if (num_components == 4) {
383
0
    while (--num_rows >= 0) {
384
0
      inptr0 = input_buf[0][input_row];
385
0
      inptr1 = input_buf[1][input_row];
386
0
      inptr2 = input_buf[2][input_row];
387
0
      inptr3 = input_buf[3][input_row];
388
0
      input_row++;
389
0
      outptr = *output_buf++;
390
0
      for (col = 0; col < num_cols; col++) {
391
0
        *outptr++ = inptr0[col];
392
0
        *outptr++ = inptr1[col];
393
0
        *outptr++ = inptr2[col];
394
0
        *outptr++ = inptr3[col];
395
0
      }
396
0
    }
397
0
  } else {
398
0
    while (--num_rows >= 0) {
399
0
      for (ci = 0; ci < num_components; ci++) {
400
0
        inptr = input_buf[ci][input_row];
401
0
        outptr = *output_buf;
402
0
        for (col = 0; col < num_cols; col++) {
403
0
          outptr[ci] = inptr[col];
404
0
          outptr += num_components;
405
0
        }
406
0
      }
407
0
      output_buf++;
408
0
      input_row++;
409
0
    }
410
0
  }
411
1.07M
}
412
413
414
/*
415
 * Color conversion for grayscale: just copy the data.
416
 * This also works for YCbCr -> grayscale conversion, in which
417
 * we just copy the Y (luminance) component and ignore chrominance.
418
 */
419
420
METHODDEF(void)
421
grayscale_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
422
                  JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
423
7.26M
{
424
7.26M
  jcopy_sample_rows(input_buf[0], (int)input_row, output_buf, 0, num_rows,
425
7.26M
                    cinfo->output_width);
426
7.26M
}
427
428
429
/*
430
 * Convert grayscale to RGB
431
 */
432
433
METHODDEF(void)
434
gray_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
435
                 JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
436
22.8M
{
437
22.8M
  switch (cinfo->out_color_space) {
438
18.5M
  case JCS_EXT_RGB:
439
18.5M
    gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
440
18.5M
                                 num_rows);
441
18.5M
    break;
442
0
  case JCS_EXT_RGBX:
443
0
  case JCS_EXT_RGBA:
444
0
    gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
445
0
                                  num_rows);
446
0
    break;
447
2.46M
  case JCS_EXT_BGR:
448
2.46M
    gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
449
2.46M
                                 num_rows);
450
2.46M
    break;
451
1.11M
  case JCS_EXT_BGRX:
452
1.11M
  case JCS_EXT_BGRA:
453
1.11M
    gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
454
1.11M
                                  num_rows);
455
1.11M
    break;
456
0
  case JCS_EXT_XBGR:
457
0
  case JCS_EXT_ABGR:
458
0
    gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
459
0
                                  num_rows);
460
0
    break;
461
783k
  case JCS_EXT_XRGB:
462
783k
  case JCS_EXT_ARGB:
463
783k
    gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
464
783k
                                  num_rows);
465
783k
    break;
466
0
  default:
467
0
    gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
468
0
                              num_rows);
469
0
    break;
470
22.8M
  }
471
22.8M
}
472
473
474
/*
475
 * Convert plain RGB to extended RGB
476
 */
477
478
METHODDEF(void)
479
rgb_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
480
                JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
481
452k
{
482
452k
  switch (cinfo->out_color_space) {
483
0
  case JCS_EXT_RGB:
484
0
    rgb_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
485
0
                                num_rows);
486
0
    break;
487
0
  case JCS_EXT_RGBX:
488
0
  case JCS_EXT_RGBA:
489
0
    rgb_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
490
0
                                 num_rows);
491
0
    break;
492
270k
  case JCS_EXT_BGR:
493
270k
    rgb_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
494
270k
                                num_rows);
495
270k
    break;
496
101k
  case JCS_EXT_BGRX:
497
101k
  case JCS_EXT_BGRA:
498
101k
    rgb_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
499
101k
                                 num_rows);
500
101k
    break;
501
0
  case JCS_EXT_XBGR:
502
0
  case JCS_EXT_ABGR:
503
0
    rgb_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
504
0
                                 num_rows);
505
0
    break;
506
80.5k
  case JCS_EXT_XRGB:
507
80.5k
  case JCS_EXT_ARGB:
508
80.5k
    rgb_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
509
80.5k
                                 num_rows);
510
80.5k
    break;
511
0
  default:
512
0
    rgb_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
513
0
                             num_rows);
514
0
    break;
515
452k
  }
516
452k
}
517
518
519
/*
520
 * Adobe-style YCCK->CMYK conversion.
521
 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
522
 * conversion as above, while passing K (black) unchanged.
523
 * We assume build_ycc_rgb_table has been called.
524
 */
525
526
METHODDEF(void)
527
ycck_cmyk_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
528
                  JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
529
0
{
530
0
  my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
531
0
  register int y, cb, cr;
532
0
  register JSAMPROW outptr;
533
0
  register JSAMPROW inptr0, inptr1, inptr2, inptr3;
534
0
  register JDIMENSION col;
535
0
  JDIMENSION num_cols = cinfo->output_width;
536
  /* copy these pointers into registers if possible */
537
0
  register JSAMPLE *range_limit = cinfo->sample_range_limit;
538
0
  register int *Crrtab = cconvert->Cr_r_tab;
539
0
  register int *Cbbtab = cconvert->Cb_b_tab;
540
0
  register JLONG *Crgtab = cconvert->Cr_g_tab;
541
0
  register JLONG *Cbgtab = cconvert->Cb_g_tab;
542
0
  SHIFT_TEMPS
543
544
0
  while (--num_rows >= 0) {
545
0
    inptr0 = input_buf[0][input_row];
546
0
    inptr1 = input_buf[1][input_row];
547
0
    inptr2 = input_buf[2][input_row];
548
0
    inptr3 = input_buf[3][input_row];
549
0
    input_row++;
550
0
    outptr = *output_buf++;
551
0
    for (col = 0; col < num_cols; col++) {
552
0
      y  = inptr0[col];
553
0
      cb = inptr1[col];
554
0
      cr = inptr2[col];
555
      /* Range-limiting is essential due to noise introduced by DCT losses. */
556
0
      outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])];   /* red */
557
0
      outptr[1] = range_limit[MAXJSAMPLE - (y +                 /* green */
558
0
                              ((int)RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
559
0
                                                 SCALEBITS)))];
560
0
      outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])];   /* blue */
561
      /* K passes through unchanged */
562
0
      outptr[3] = inptr3[col];
563
0
      outptr += 4;
564
0
    }
565
0
  }
566
0
}
567
568
569
/*
570
 * RGB565 conversion
571
 */
572
573
#define PACK_SHORT_565_LE(r, g, b) \
574
0
  ((((r) << 8) & 0xF800) | (((g) << 3) & 0x7E0) | ((b) >> 3))
575
#define PACK_SHORT_565_BE(r, g, b) \
576
0
  (((r) & 0xF8) | ((g) >> 5) | (((g) << 11) & 0xE000) | (((b) << 5) & 0x1F00))
577
578
0
#define PACK_TWO_PIXELS_LE(l, r)    ((r << 16) | l)
579
0
#define PACK_TWO_PIXELS_BE(l, r)    ((l << 16) | r)
580
581
0
#define PACK_NEED_ALIGNMENT(ptr)    (((size_t)(ptr)) & 3)
582
583
0
#define WRITE_TWO_ALIGNED_PIXELS(addr, pixels)  ((*(int *)(addr)) = pixels)
584
585
0
#define DITHER_565_R(r, dither)  ((r) + ((dither) & 0xFF))
586
0
#define DITHER_565_G(g, dither)  ((g) + (((dither) & 0xFF) >> 1))
587
0
#define DITHER_565_B(b, dither)  ((b) + ((dither) & 0xFF))
588
589
590
/* Declarations for ordered dithering
591
 *
592
 * We use a 4x4 ordered dither array packed into 32 bits.  This array is
593
 * sufficient for dithering RGB888 to RGB565.
594
 */
595
596
0
#define DITHER_MASK       0x3
597
0
#define DITHER_ROTATE(x)  ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
598
static const JLONG dither_matrix[4] = {
599
  0x0008020A,
600
  0x0C040E06,
601
  0x030B0109,
602
  0x0F070D05
603
};
604
605
606
static INLINE boolean is_big_endian(void)
607
0
{
608
0
  int test_value = 1;
609
0
  if (*(char *)&test_value != 1)
610
0
    return TRUE;
611
0
  return FALSE;
612
0
}
613
614
615
/* Include inline routines for RGB565 conversion */
616
617
0
#define PACK_SHORT_565  PACK_SHORT_565_LE
618
0
#define PACK_TWO_PIXELS  PACK_TWO_PIXELS_LE
619
#define ycc_rgb565_convert_internal  ycc_rgb565_convert_le
620
#define ycc_rgb565D_convert_internal  ycc_rgb565D_convert_le
621
#define rgb_rgb565_convert_internal  rgb_rgb565_convert_le
622
#define rgb_rgb565D_convert_internal  rgb_rgb565D_convert_le
623
#define gray_rgb565_convert_internal  gray_rgb565_convert_le
624
#define gray_rgb565D_convert_internal  gray_rgb565D_convert_le
625
#include "jdcol565.c"
626
#undef PACK_SHORT_565
627
#undef PACK_TWO_PIXELS
628
#undef ycc_rgb565_convert_internal
629
#undef ycc_rgb565D_convert_internal
630
#undef rgb_rgb565_convert_internal
631
#undef rgb_rgb565D_convert_internal
632
#undef gray_rgb565_convert_internal
633
#undef gray_rgb565D_convert_internal
634
635
0
#define PACK_SHORT_565  PACK_SHORT_565_BE
636
0
#define PACK_TWO_PIXELS  PACK_TWO_PIXELS_BE
637
#define ycc_rgb565_convert_internal  ycc_rgb565_convert_be
638
#define ycc_rgb565D_convert_internal  ycc_rgb565D_convert_be
639
#define rgb_rgb565_convert_internal  rgb_rgb565_convert_be
640
#define rgb_rgb565D_convert_internal  rgb_rgb565D_convert_be
641
#define gray_rgb565_convert_internal  gray_rgb565_convert_be
642
#define gray_rgb565D_convert_internal  gray_rgb565D_convert_be
643
#include "jdcol565.c"
644
#undef PACK_SHORT_565
645
#undef PACK_TWO_PIXELS
646
#undef ycc_rgb565_convert_internal
647
#undef ycc_rgb565D_convert_internal
648
#undef rgb_rgb565_convert_internal
649
#undef rgb_rgb565D_convert_internal
650
#undef gray_rgb565_convert_internal
651
#undef gray_rgb565D_convert_internal
652
653
654
METHODDEF(void)
655
ycc_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
656
                   JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
657
0
{
658
0
  if (is_big_endian())
659
0
    ycc_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
660
0
  else
661
0
    ycc_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
662
0
}
663
664
665
METHODDEF(void)
666
ycc_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
667
                    JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
668
0
{
669
0
  if (is_big_endian())
670
0
    ycc_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
671
0
  else
672
0
    ycc_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
673
0
}
674
675
676
METHODDEF(void)
677
rgb_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
678
                   JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
679
0
{
680
0
  if (is_big_endian())
681
0
    rgb_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
682
0
  else
683
0
    rgb_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
684
0
}
685
686
687
METHODDEF(void)
688
rgb_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
689
                    JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
690
0
{
691
0
  if (is_big_endian())
692
0
    rgb_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
693
0
  else
694
0
    rgb_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
695
0
}
696
697
698
METHODDEF(void)
699
gray_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
700
                    JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
701
0
{
702
0
  if (is_big_endian())
703
0
    gray_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
704
0
  else
705
0
    gray_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
706
0
}
707
708
709
METHODDEF(void)
710
gray_rgb565D_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
711
                     JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
712
0
{
713
0
  if (is_big_endian())
714
0
    gray_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
715
0
  else
716
0
    gray_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
717
0
}
718
719
720
/*
721
 * Empty method for start_pass.
722
 */
723
724
METHODDEF(void)
725
start_pass_dcolor(j_decompress_ptr cinfo)
726
13.9k
{
727
  /* no work needed */
728
13.9k
}
729
730
731
/*
732
 * Module initialization routine for output colorspace conversion.
733
 */
734
735
GLOBAL(void)
736
jinit_color_deconverter(j_decompress_ptr cinfo)
737
11.4k
{
738
11.4k
  my_cconvert_ptr cconvert;
739
11.4k
  int ci;
740
741
11.4k
  cconvert = (my_cconvert_ptr)
742
11.4k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
743
11.4k
                                sizeof(my_color_deconverter));
744
11.4k
  cinfo->cconvert = (struct jpeg_color_deconverter *)cconvert;
745
11.4k
  cconvert->pub.start_pass = start_pass_dcolor;
746
747
  /* Make sure num_components agrees with jpeg_color_space */
748
11.4k
  switch (cinfo->jpeg_color_space) {
749
5.63k
  case JCS_GRAYSCALE:
750
5.63k
    if (cinfo->num_components != 1)
751
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
752
5.63k
    break;
753
754
554
  case JCS_RGB:
755
5.75k
  case JCS_YCbCr:
756
5.75k
    if (cinfo->num_components != 3)
757
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
758
5.75k
    break;
759
760
28
  case JCS_CMYK:
761
42
  case JCS_YCCK:
762
42
    if (cinfo->num_components != 4)
763
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
764
42
    break;
765
766
15
  default:                      /* JCS_UNKNOWN can be anything */
767
15
    if (cinfo->num_components < 1)
768
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
769
15
    break;
770
11.4k
  }
771
772
  /* Set out_color_components and conversion method based on requested space.
773
   * Also clear the component_needed flags for any unused components,
774
   * so that earlier pipeline stages can avoid useless computation.
775
   */
776
777
11.4k
  switch (cinfo->out_color_space) {
778
1.46k
  case JCS_GRAYSCALE:
779
1.46k
    cinfo->out_color_components = 1;
780
1.46k
    if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
781
1.46k
        cinfo->jpeg_color_space == JCS_YCbCr) {
782
1.33k
      cconvert->pub.color_convert = grayscale_convert;
783
      /* For color->grayscale conversion, only the Y (0) component is needed */
784
3.13k
      for (ci = 1; ci < cinfo->num_components; ci++)
785
1.80k
        cinfo->comp_info[ci].component_needed = FALSE;
786
1.33k
    } else if (cinfo->jpeg_color_space == JCS_RGB) {
787
133
      cconvert->pub.color_convert = rgb_gray_convert;
788
133
      build_rgb_y_table(cinfo);
789
133
    } else
790
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
791
1.46k
    break;
792
793
0
  case JCS_RGB:
794
7.32k
  case JCS_EXT_RGB:
795
7.32k
  case JCS_EXT_RGBX:
796
7.74k
  case JCS_EXT_BGR:
797
8.67k
  case JCS_EXT_BGRX:
798
8.67k
  case JCS_EXT_XBGR:
799
9.07k
  case JCS_EXT_XRGB:
800
9.07k
  case JCS_EXT_RGBA:
801
9.07k
  case JCS_EXT_BGRA:
802
9.07k
  case JCS_EXT_ABGR:
803
9.07k
  case JCS_EXT_ARGB:
804
9.07k
    cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
805
9.07k
    if (cinfo->jpeg_color_space == JCS_YCbCr) {
806
3.63k
      if (jsimd_can_ycc_rgb())
807
3.63k
        cconvert->pub.color_convert = jsimd_ycc_rgb_convert;
808
0
      else {
809
0
        cconvert->pub.color_convert = ycc_rgb_convert;
810
0
        build_ycc_rgb_table(cinfo);
811
0
      }
812
5.43k
    } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
813
5.02k
      cconvert->pub.color_convert = gray_rgb_convert;
814
5.02k
    } else if (cinfo->jpeg_color_space == JCS_RGB) {
815
351
      if (rgb_red[cinfo->out_color_space] == 0 &&
816
351
          rgb_green[cinfo->out_color_space] == 1 &&
817
351
          rgb_blue[cinfo->out_color_space] == 2 &&
818
351
          rgb_pixelsize[cinfo->out_color_space] == 3)
819
149
        cconvert->pub.color_convert = null_convert;
820
202
      else
821
202
        cconvert->pub.color_convert = rgb_rgb_convert;
822
351
    } else
823
57
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
824
9.07k
    break;
825
826
0
  case JCS_RGB565:
827
0
    cinfo->out_color_components = 3;
828
0
    if (cinfo->dither_mode == JDITHER_NONE) {
829
0
      if (cinfo->jpeg_color_space == JCS_YCbCr) {
830
0
        if (jsimd_can_ycc_rgb565())
831
0
          cconvert->pub.color_convert = jsimd_ycc_rgb565_convert;
832
0
        else {
833
0
          cconvert->pub.color_convert = ycc_rgb565_convert;
834
0
          build_ycc_rgb_table(cinfo);
835
0
        }
836
0
      } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
837
0
        cconvert->pub.color_convert = gray_rgb565_convert;
838
0
      } else if (cinfo->jpeg_color_space == JCS_RGB) {
839
0
        cconvert->pub.color_convert = rgb_rgb565_convert;
840
0
      } else
841
0
        ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
842
0
    } else {
843
      /* only ordered dithering is supported */
844
0
      if (cinfo->jpeg_color_space == JCS_YCbCr) {
845
0
        cconvert->pub.color_convert = ycc_rgb565D_convert;
846
0
        build_ycc_rgb_table(cinfo);
847
0
      } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
848
0
        cconvert->pub.color_convert = gray_rgb565D_convert;
849
0
      } else if (cinfo->jpeg_color_space == JCS_RGB) {
850
0
        cconvert->pub.color_convert = rgb_rgb565D_convert;
851
0
      } else
852
0
        ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
853
0
    }
854
0
    break;
855
856
919
  case JCS_CMYK:
857
919
    cinfo->out_color_components = 4;
858
919
    if (cinfo->jpeg_color_space == JCS_YCCK) {
859
0
      cconvert->pub.color_convert = ycck_cmyk_convert;
860
0
      build_ycc_rgb_table(cinfo);
861
919
    } else if (cinfo->jpeg_color_space == JCS_CMYK) {
862
0
      cconvert->pub.color_convert = null_convert;
863
0
    } else
864
919
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
865
919
    break;
866
867
0
  default:
868
    /* Permit null conversion to same output space */
869
0
    if (cinfo->out_color_space == cinfo->jpeg_color_space) {
870
0
      cinfo->out_color_components = cinfo->num_components;
871
0
      cconvert->pub.color_convert = null_convert;
872
0
    } else                      /* unsupported non-null conversion */
873
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
874
0
    break;
875
11.4k
  }
876
877
10.4k
  if (cinfo->quantize_colors)
878
0
    cinfo->output_components = 1; /* single colormapped output component */
879
10.4k
  else
880
10.4k
    cinfo->output_components = cinfo->out_color_components;
881
10.4k
}