Coverage Report

Created: 2023-06-07 06:03

/src/libjpeg-turbo.main/jccolor.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jccolor.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1991-1996, Thomas G. Lane.
6
 * libjpeg-turbo Modifications:
7
 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
 * Copyright (C) 2009-2012, 2015, 2022, D. R. Commander.
9
 * Copyright (C) 2014, MIPS Technologies, Inc., California.
10
 * For conditions of distribution and use, see the accompanying README.ijg
11
 * file.
12
 *
13
 * This file contains input colorspace conversion routines.
14
 */
15
16
#define JPEG_INTERNALS
17
#include "jinclude.h"
18
#include "jpeglib.h"
19
#include "jsimd.h"
20
#include "jsamplecomp.h"
21
22
23
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
24
25
/* Private subobject */
26
27
typedef struct {
28
  struct jpeg_color_converter pub; /* public fields */
29
30
#if BITS_IN_JSAMPLE != 16
31
  /* Private state for RGB->YCC conversion */
32
  JLONG *rgb_ycc_tab;           /* => table for RGB to YCbCr conversion */
33
#endif
34
} my_color_converter;
35
36
typedef my_color_converter *my_cconvert_ptr;
37
38
39
/**************** RGB -> YCbCr conversion: most common case **************/
40
41
/*
42
 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
43
 * normalized to the range 0.._MAXJSAMPLE rather than -0.5 .. 0.5.
44
 * The conversion equations to be implemented are therefore
45
 *      Y  =  0.29900 * R + 0.58700 * G + 0.11400 * B
46
 *      Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B  + _CENTERJSAMPLE
47
 *      Cr =  0.50000 * R - 0.41869 * G - 0.08131 * B  + _CENTERJSAMPLE
48
 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
49
 * Note: older versions of the IJG code used a zero offset of _MAXJSAMPLE/2,
50
 * rather than _CENTERJSAMPLE, for Cb and Cr.  This gave equal positive and
51
 * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
52
 * were not represented exactly.  Now we sacrifice exact representation of
53
 * maximum red and maximum blue in order to get exact grayscales.
54
 *
55
 * To avoid floating-point arithmetic, we represent the fractional constants
56
 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
57
 * the products by 2^16, with appropriate rounding, to get the correct answer.
58
 *
59
 * For even more speed, we avoid doing any multiplications in the inner loop
60
 * by precalculating the constants times R,G,B for all possible values.
61
 * For 8-bit samples this is very reasonable (only 256 entries per table);
62
 * for 12-bit samples it is still acceptable.  It's not very reasonable for
63
 * 16-bit samples, but if you want lossless storage you shouldn't be changing
64
 * colorspace anyway.
65
 * The _CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
66
 * in the tables to save adding them separately in the inner loop.
67
 */
68
69
1.56G
#define SCALEBITS       16      /* speediest right-shift on some machines */
70
94.4M
#define CBCR_OFFSET     ((JLONG)_CENTERJSAMPLE << SCALEBITS)
71
188M
#define ONE_HALF        ((JLONG)1 << (SCALEBITS - 1))
72
755M
#define FIX(x)          ((JLONG)((x) * (1L << SCALEBITS) + 0.5))
73
74
/* We allocate one big table and divide it up into eight parts, instead of
75
 * doing eight alloc_small requests.  This lets us use a single table base
76
 * address, which can be held in a register in the inner loops on many
77
 * machines (more than can hold all eight addresses, anyway).
78
 */
79
80
285M
#define R_Y_OFF         0                       /* offset to R => Y section */
81
285M
#define G_Y_OFF         (1 * (_MAXJSAMPLE + 1)) /* offset to G => Y section */
82
285M
#define B_Y_OFF         (2 * (_MAXJSAMPLE + 1)) /* etc. */
83
260M
#define R_CB_OFF        (3 * (_MAXJSAMPLE + 1))
84
260M
#define G_CB_OFF        (4 * (_MAXJSAMPLE + 1))
85
426M
#define B_CB_OFF        (5 * (_MAXJSAMPLE + 1))
86
165M
#define R_CR_OFF        B_CB_OFF                /* B=>Cb, R=>Cr are the same */
87
260M
#define G_CR_OFF        (6 * (_MAXJSAMPLE + 1))
88
260M
#define B_CR_OFF        (7 * (_MAXJSAMPLE + 1))
89
26.8k
#define TABLE_SIZE      (8 * (_MAXJSAMPLE + 1))
90
91
/* 12-bit samples use a 16-bit data type, so it is possible to pass
92
 * out-of-range sample values (< 0 or > 4095) to jpeg_write_scanlines().
93
 * Thus, we mask the incoming 12-bit samples to guard against overrunning
94
 * or underrunning the conversion tables.
95
 */
96
97
#if BITS_IN_JSAMPLE == 12
98
#define RANGE_LIMIT(value)  ((value) & 0xFFF)
99
#else
100
573M
#define RANGE_LIMIT(value)  (value)
101
#endif
102
103
104
/* Include inline routines for colorspace extensions */
105
106
#include "jccolext.c"
107
#undef RGB_RED
108
#undef RGB_GREEN
109
#undef RGB_BLUE
110
#undef RGB_PIXELSIZE
111
112
0
#define RGB_RED  EXT_RGB_RED
113
0
#define RGB_GREEN  EXT_RGB_GREEN
114
0
#define RGB_BLUE  EXT_RGB_BLUE
115
25.3M
#define RGB_PIXELSIZE  EXT_RGB_PIXELSIZE
116
#define rgb_ycc_convert_internal  extrgb_ycc_convert_internal
117
#define rgb_gray_convert_internal  extrgb_gray_convert_internal
118
#define rgb_rgb_convert_internal  extrgb_rgb_convert_internal
119
#include "jccolext.c"
120
#undef RGB_RED
121
#undef RGB_GREEN
122
#undef RGB_BLUE
123
#undef RGB_PIXELSIZE
124
#undef rgb_ycc_convert_internal
125
#undef rgb_gray_convert_internal
126
#undef rgb_rgb_convert_internal
127
128
21.6M
#define RGB_RED  EXT_RGBX_RED
129
21.6M
#define RGB_GREEN  EXT_RGBX_GREEN
130
21.6M
#define RGB_BLUE  EXT_RGBX_BLUE
131
46.9M
#define RGB_PIXELSIZE  EXT_RGBX_PIXELSIZE
132
#define rgb_ycc_convert_internal  extrgbx_ycc_convert_internal
133
#define rgb_gray_convert_internal  extrgbx_gray_convert_internal
134
#define rgb_rgb_convert_internal  extrgbx_rgb_convert_internal
135
#include "jccolext.c"
136
#undef RGB_RED
137
#undef RGB_GREEN
138
#undef RGB_BLUE
139
#undef RGB_PIXELSIZE
140
#undef rgb_ycc_convert_internal
141
#undef rgb_gray_convert_internal
142
#undef rgb_rgb_convert_internal
143
144
21.6M
#define RGB_RED  EXT_BGR_RED
145
21.6M
#define RGB_GREEN  EXT_BGR_GREEN
146
21.6M
#define RGB_BLUE  EXT_BGR_BLUE
147
46.9M
#define RGB_PIXELSIZE  EXT_BGR_PIXELSIZE
148
#define rgb_ycc_convert_internal  extbgr_ycc_convert_internal
149
#define rgb_gray_convert_internal  extbgr_gray_convert_internal
150
#define rgb_rgb_convert_internal  extbgr_rgb_convert_internal
151
#include "jccolext.c"
152
#undef RGB_RED
153
#undef RGB_GREEN
154
#undef RGB_BLUE
155
#undef RGB_PIXELSIZE
156
#undef rgb_ycc_convert_internal
157
#undef rgb_gray_convert_internal
158
#undef rgb_rgb_convert_internal
159
160
21.6M
#define RGB_RED  EXT_BGRX_RED
161
21.6M
#define RGB_GREEN  EXT_BGRX_GREEN
162
21.6M
#define RGB_BLUE  EXT_BGRX_BLUE
163
46.9M
#define RGB_PIXELSIZE  EXT_BGRX_PIXELSIZE
164
#define rgb_ycc_convert_internal  extbgrx_ycc_convert_internal
165
#define rgb_gray_convert_internal  extbgrx_gray_convert_internal
166
#define rgb_rgb_convert_internal  extbgrx_rgb_convert_internal
167
#include "jccolext.c"
168
#undef RGB_RED
169
#undef RGB_GREEN
170
#undef RGB_BLUE
171
#undef RGB_PIXELSIZE
172
#undef rgb_ycc_convert_internal
173
#undef rgb_gray_convert_internal
174
#undef rgb_rgb_convert_internal
175
176
0
#define RGB_RED  EXT_XBGR_RED
177
0
#define RGB_GREEN  EXT_XBGR_GREEN
178
0
#define RGB_BLUE  EXT_XBGR_BLUE
179
0
#define RGB_PIXELSIZE  EXT_XBGR_PIXELSIZE
180
#define rgb_ycc_convert_internal  extxbgr_ycc_convert_internal
181
#define rgb_gray_convert_internal  extxbgr_gray_convert_internal
182
#define rgb_rgb_convert_internal  extxbgr_rgb_convert_internal
183
#include "jccolext.c"
184
#undef RGB_RED
185
#undef RGB_GREEN
186
#undef RGB_BLUE
187
#undef RGB_PIXELSIZE
188
#undef rgb_ycc_convert_internal
189
#undef rgb_gray_convert_internal
190
#undef rgb_rgb_convert_internal
191
192
21.6M
#define RGB_RED  EXT_XRGB_RED
193
21.6M
#define RGB_GREEN  EXT_XRGB_GREEN
194
21.6M
#define RGB_BLUE  EXT_XRGB_BLUE
195
46.9M
#define RGB_PIXELSIZE  EXT_XRGB_PIXELSIZE
196
#define rgb_ycc_convert_internal  extxrgb_ycc_convert_internal
197
#define rgb_gray_convert_internal  extxrgb_gray_convert_internal
198
#define rgb_rgb_convert_internal  extxrgb_rgb_convert_internal
199
#include "jccolext.c"
200
#undef RGB_RED
201
#undef RGB_GREEN
202
#undef RGB_BLUE
203
#undef RGB_PIXELSIZE
204
#undef rgb_ycc_convert_internal
205
#undef rgb_gray_convert_internal
206
#undef rgb_rgb_convert_internal
207
208
209
/*
210
 * Initialize for RGB->YCC colorspace conversion.
211
 */
212
213
METHODDEF(void)
214
rgb_ycc_start(j_compress_ptr cinfo)
215
26.8k
{
216
26.8k
#if BITS_IN_JSAMPLE != 16
217
26.8k
  my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
218
26.8k
  JLONG *rgb_ycc_tab;
219
26.8k
  JLONG i;
220
221
  /* Allocate and fill in the conversion tables. */
222
26.8k
  cconvert->rgb_ycc_tab = rgb_ycc_tab = (JLONG *)
223
26.8k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
224
26.8k
                                (TABLE_SIZE * sizeof(JLONG)));
225
226
94.4M
  for (i = 0; i <= _MAXJSAMPLE; i++) {
227
94.4M
    rgb_ycc_tab[i + R_Y_OFF] = FIX(0.29900) * i;
228
94.4M
    rgb_ycc_tab[i + G_Y_OFF] = FIX(0.58700) * i;
229
94.4M
    rgb_ycc_tab[i + B_Y_OFF] = FIX(0.11400) * i   + ONE_HALF;
230
94.4M
    rgb_ycc_tab[i + R_CB_OFF] = (-FIX(0.16874)) * i;
231
94.4M
    rgb_ycc_tab[i + G_CB_OFF] = (-FIX(0.33126)) * i;
232
    /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
233
     * This ensures that the maximum output will round to _MAXJSAMPLE
234
     * not _MAXJSAMPLE+1, and thus that we don't have to range-limit.
235
     */
236
94.4M
    rgb_ycc_tab[i + B_CB_OFF] = FIX(0.50000) * i  + CBCR_OFFSET + ONE_HALF - 1;
237
/*  B=>Cb and R=>Cr tables are the same
238
    rgb_ycc_tab[i + R_CR_OFF] = FIX(0.50000) * i  + CBCR_OFFSET + ONE_HALF - 1;
239
*/
240
94.4M
    rgb_ycc_tab[i + G_CR_OFF] = (-FIX(0.41869)) * i;
241
94.4M
    rgb_ycc_tab[i + B_CR_OFF] = (-FIX(0.08131)) * i;
242
94.4M
  }
243
#else
244
  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
245
#endif
246
26.8k
}
247
248
249
/*
250
 * Convert some rows of samples to the JPEG colorspace.
251
 */
252
253
METHODDEF(void)
254
rgb_ycc_convert(j_compress_ptr cinfo, _JSAMPARRAY input_buf,
255
                _JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)
256
50.9M
{
257
50.9M
  switch (cinfo->in_color_space) {
258
14.5M
  case JCS_EXT_RGB:
259
14.5M
    extrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
260
14.5M
                                num_rows);
261
14.5M
    break;
262
7.27M
  case JCS_EXT_RGBX:
263
7.27M
  case JCS_EXT_RGBA:
264
7.27M
    extrgbx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
265
7.27M
                                 num_rows);
266
7.27M
    break;
267
14.5M
  case JCS_EXT_BGR:
268
14.5M
    extbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
269
14.5M
                                num_rows);
270
14.5M
    break;
271
0
  case JCS_EXT_BGRX:
272
14.5M
  case JCS_EXT_BGRA:
273
14.5M
    extbgrx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
274
14.5M
                                 num_rows);
275
14.5M
    break;
276
0
  case JCS_EXT_XBGR:
277
0
  case JCS_EXT_ABGR:
278
0
    extxbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
279
0
                                 num_rows);
280
0
    break;
281
0
  case JCS_EXT_XRGB:
282
0
  case JCS_EXT_ARGB:
283
0
    extxrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
284
0
                                 num_rows);
285
0
    break;
286
0
  default:
287
0
    rgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
288
0
                             num_rows);
289
0
    break;
290
50.9M
  }
291
50.9M
}
292
293
294
/**************** Cases other than RGB -> YCbCr **************/
295
296
297
/*
298
 * Convert some rows of samples to the JPEG colorspace.
299
 */
300
301
METHODDEF(void)
302
rgb_gray_convert(j_compress_ptr cinfo, _JSAMPARRAY input_buf,
303
                 _JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)
304
14.5M
{
305
14.5M
  switch (cinfo->in_color_space) {
306
0
  case JCS_EXT_RGB:
307
0
    extrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
308
0
                                 num_rows);
309
0
    break;
310
0
  case JCS_EXT_RGBX:
311
0
  case JCS_EXT_RGBA:
312
0
    extrgbx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
313
0
                                  num_rows);
314
0
    break;
315
0
  case JCS_EXT_BGR:
316
0
    extbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
317
0
                                 num_rows);
318
0
    break;
319
0
  case JCS_EXT_BGRX:
320
0
  case JCS_EXT_BGRA:
321
0
    extbgrx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
322
0
                                  num_rows);
323
0
    break;
324
0
  case JCS_EXT_XBGR:
325
0
  case JCS_EXT_ABGR:
326
0
    extxbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
327
0
                                  num_rows);
328
0
    break;
329
14.5M
  case JCS_EXT_XRGB:
330
14.5M
  case JCS_EXT_ARGB:
331
14.5M
    extxrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
332
14.5M
                                  num_rows);
333
14.5M
    break;
334
0
  default:
335
0
    rgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
336
0
                              num_rows);
337
0
    break;
338
14.5M
  }
339
14.5M
}
340
341
342
/*
343
 * Extended RGB to plain RGB conversion
344
 */
345
346
METHODDEF(void)
347
rgb_rgb_convert(j_compress_ptr cinfo, _JSAMPARRAY input_buf,
348
                _JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)
349
16.3M
{
350
16.3M
  switch (cinfo->in_color_space) {
351
0
  case JCS_EXT_RGB:
352
0
    extrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
353
0
                                num_rows);
354
0
    break;
355
4.07M
  case JCS_EXT_RGBX:
356
4.07M
  case JCS_EXT_RGBA:
357
4.07M
    extrgbx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
358
4.07M
                                 num_rows);
359
4.07M
    break;
360
4.07M
  case JCS_EXT_BGR:
361
4.07M
    extbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
362
4.07M
                                num_rows);
363
4.07M
    break;
364
0
  case JCS_EXT_BGRX:
365
4.07M
  case JCS_EXT_BGRA:
366
4.07M
    extbgrx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
367
4.07M
                                 num_rows);
368
4.07M
    break;
369
0
  case JCS_EXT_XBGR:
370
0
  case JCS_EXT_ABGR:
371
0
    extxbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
372
0
                                 num_rows);
373
0
    break;
374
4.07M
  case JCS_EXT_XRGB:
375
4.07M
  case JCS_EXT_ARGB:
376
4.07M
    extxrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
377
4.07M
                                 num_rows);
378
4.07M
    break;
379
0
  default:
380
0
    rgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
381
0
                             num_rows);
382
0
    break;
383
16.3M
  }
384
16.3M
}
385
386
387
/*
388
 * Convert some rows of samples to the JPEG colorspace.
389
 * This version handles Adobe-style CMYK->YCCK conversion,
390
 * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
391
 * conversion as above, while passing K (black) unchanged.
392
 * We assume rgb_ycc_start has been called.
393
 */
394
395
METHODDEF(void)
396
cmyk_ycck_convert(j_compress_ptr cinfo, _JSAMPARRAY input_buf,
397
                  _JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)
398
14.2M
{
399
14.2M
#if BITS_IN_JSAMPLE != 16
400
14.2M
  my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
401
14.2M
  register int r, g, b;
402
14.2M
  register JLONG *ctab = cconvert->rgb_ycc_tab;
403
14.2M
  register _JSAMPROW inptr;
404
14.2M
  register _JSAMPROW outptr0, outptr1, outptr2, outptr3;
405
14.2M
  register JDIMENSION col;
406
14.2M
  JDIMENSION num_cols = cinfo->image_width;
407
408
42.8M
  while (--num_rows >= 0) {
409
28.5M
    inptr = *input_buf++;
410
28.5M
    outptr0 = output_buf[0][output_row];
411
28.5M
    outptr1 = output_buf[1][output_row];
412
28.5M
    outptr2 = output_buf[2][output_row];
413
28.5M
    outptr3 = output_buf[3][output_row];
414
28.5M
    output_row++;
415
93.1M
    for (col = 0; col < num_cols; col++) {
416
64.6M
      r = _MAXJSAMPLE - RANGE_LIMIT(inptr[0]);
417
64.6M
      g = _MAXJSAMPLE - RANGE_LIMIT(inptr[1]);
418
64.6M
      b = _MAXJSAMPLE - RANGE_LIMIT(inptr[2]);
419
      /* K passes through as-is */
420
64.6M
      outptr3[col] = inptr[3];
421
64.6M
      inptr += 4;
422
      /* If the inputs are 0.._MAXJSAMPLE, the outputs of these equations
423
       * must be too; we do not need an explicit range-limiting operation.
424
       * Hence the value being shifted is never negative, and we don't
425
       * need the general RIGHT_SHIFT macro.
426
       */
427
      /* Y */
428
64.6M
      outptr0[col] = (_JSAMPLE)((ctab[r + R_Y_OFF] + ctab[g + G_Y_OFF] +
429
64.6M
                                 ctab[b + B_Y_OFF]) >> SCALEBITS);
430
      /* Cb */
431
64.6M
      outptr1[col] = (_JSAMPLE)((ctab[r + R_CB_OFF] + ctab[g + G_CB_OFF] +
432
64.6M
                                 ctab[b + B_CB_OFF]) >> SCALEBITS);
433
      /* Cr */
434
64.6M
      outptr2[col] = (_JSAMPLE)((ctab[r + R_CR_OFF] + ctab[g + G_CR_OFF] +
435
64.6M
                                 ctab[b + B_CR_OFF]) >> SCALEBITS);
436
64.6M
    }
437
28.5M
  }
438
#else
439
  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
440
#endif
441
14.2M
}
442
443
444
/*
445
 * Convert some rows of samples to the JPEG colorspace.
446
 * This version handles grayscale output with no conversion.
447
 * The source can be either plain grayscale or YCbCr (since Y == gray).
448
 */
449
450
METHODDEF(void)
451
grayscale_convert(j_compress_ptr cinfo, _JSAMPARRAY input_buf,
452
                  _JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)
453
46.8M
{
454
46.8M
  register _JSAMPROW inptr;
455
46.8M
  register _JSAMPROW outptr;
456
46.8M
  register JDIMENSION col;
457
46.8M
  JDIMENSION num_cols = cinfo->image_width;
458
46.8M
  int instride = cinfo->input_components;
459
460
93.6M
  while (--num_rows >= 0) {
461
46.8M
    inptr = *input_buf++;
462
46.8M
    outptr = output_buf[0][output_row];
463
46.8M
    output_row++;
464
178M
    for (col = 0; col < num_cols; col++) {
465
131M
      outptr[col] = inptr[0];
466
131M
      inptr += instride;
467
131M
    }
468
46.8M
  }
469
46.8M
}
470
471
472
/*
473
 * Convert some rows of samples to the JPEG colorspace.
474
 * This version handles multi-component colorspaces without conversion.
475
 * We assume input_components == num_components.
476
 */
477
478
METHODDEF(void)
479
null_convert(j_compress_ptr cinfo, _JSAMPARRAY input_buf,
480
             _JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)
481
20.3M
{
482
20.3M
  register _JSAMPROW inptr;
483
20.3M
  register _JSAMPROW outptr, outptr0, outptr1, outptr2, outptr3;
484
20.3M
  register JDIMENSION col;
485
20.3M
  register int ci;
486
20.3M
  int nc = cinfo->num_components;
487
20.3M
  JDIMENSION num_cols = cinfo->image_width;
488
489
20.3M
  if (nc == 3) {
490
32.4M
    while (--num_rows >= 0) {
491
16.2M
      inptr = *input_buf++;
492
16.2M
      outptr0 = output_buf[0][output_row];
493
16.2M
      outptr1 = output_buf[1][output_row];
494
16.2M
      outptr2 = output_buf[2][output_row];
495
16.2M
      output_row++;
496
222M
      for (col = 0; col < num_cols; col++) {
497
205M
        outptr0[col] = *inptr++;
498
205M
        outptr1[col] = *inptr++;
499
205M
        outptr2[col] = *inptr++;
500
205M
      }
501
16.2M
    }
502
16.2M
  } else if (nc == 4) {
503
8.15M
    while (--num_rows >= 0) {
504
4.07M
      inptr = *input_buf++;
505
4.07M
      outptr0 = output_buf[0][output_row];
506
4.07M
      outptr1 = output_buf[1][output_row];
507
4.07M
      outptr2 = output_buf[2][output_row];
508
4.07M
      outptr3 = output_buf[3][output_row];
509
4.07M
      output_row++;
510
25.6M
      for (col = 0; col < num_cols; col++) {
511
21.6M
        outptr0[col] = *inptr++;
512
21.6M
        outptr1[col] = *inptr++;
513
21.6M
        outptr2[col] = *inptr++;
514
21.6M
        outptr3[col] = *inptr++;
515
21.6M
      }
516
4.07M
    }
517
4.07M
  } else {
518
0
    while (--num_rows >= 0) {
519
      /* It seems fastest to make a separate pass for each component. */
520
0
      for (ci = 0; ci < nc; ci++) {
521
0
        inptr = *input_buf;
522
0
        outptr = output_buf[ci][output_row];
523
0
        for (col = 0; col < num_cols; col++) {
524
0
          outptr[col] = inptr[ci];
525
0
          inptr += nc;
526
0
        }
527
0
      }
528
0
      input_buf++;
529
0
      output_row++;
530
0
    }
531
0
  }
532
20.3M
}
533
534
535
/*
536
 * Empty method for start_pass.
537
 */
538
539
METHODDEF(void)
540
null_method(j_compress_ptr cinfo)
541
77.0k
{
542
  /* no work needed */
543
77.0k
}
544
545
546
/*
547
 * Module initialization routine for input colorspace conversion.
548
 */
549
550
GLOBAL(void)
551
_jinit_color_converter(j_compress_ptr cinfo)
552
60.0k
{
553
60.0k
  my_cconvert_ptr cconvert;
554
555
60.0k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
556
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
557
558
60.0k
  cconvert = (my_cconvert_ptr)
559
60.0k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
560
60.0k
                                sizeof(my_color_converter));
561
60.0k
  cinfo->cconvert = (struct jpeg_color_converter *)cconvert;
562
  /* set start_pass to null method until we find out differently */
563
60.0k
  cconvert->pub.start_pass = null_method;
564
565
  /* Make sure input_components agrees with in_color_space */
566
60.0k
  switch (cinfo->in_color_space) {
567
7.60k
  case JCS_GRAYSCALE:
568
7.60k
    if (cinfo->input_components != 1)
569
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
570
7.60k
    break;
571
572
0
  case JCS_RGB:
573
9.05k
  case JCS_EXT_RGB:
574
16.2k
  case JCS_EXT_RGBX:
575
27.1k
  case JCS_EXT_BGR:
576
27.1k
  case JCS_EXT_BGRX:
577
29.0k
  case JCS_EXT_XBGR:
578
38.1k
  case JCS_EXT_XRGB:
579
38.1k
  case JCS_EXT_RGBA:
580
45.2k
  case JCS_EXT_BGRA:
581
45.2k
  case JCS_EXT_ABGR:
582
45.2k
  case JCS_EXT_ARGB:
583
45.2k
    if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
584
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
585
45.2k
    break;
586
587
0
  case JCS_YCbCr:
588
0
    if (cinfo->input_components != 3)
589
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
590
0
    break;
591
592
7.14k
  case JCS_CMYK:
593
7.14k
  case JCS_YCCK:
594
7.14k
    if (cinfo->input_components != 4)
595
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
596
7.14k
    break;
597
598
0
  default:                      /* JCS_UNKNOWN can be anything */
599
0
    if (cinfo->input_components < 1)
600
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
601
0
    break;
602
60.0k
  }
603
604
  /* Check num_components, set conversion method based on requested space.
605
   * NOTE: We do not allow any lossy color conversion algorithms in lossless
606
   * mode.
607
   */
608
60.0k
  switch (cinfo->jpeg_color_space) {
609
15.3k
  case JCS_GRAYSCALE:
610
15.3k
    if (cinfo->master->lossless &&
611
15.3k
        cinfo->in_color_space != cinfo->jpeg_color_space)
612
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
613
15.3k
    if (cinfo->num_components != 1)
614
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
615
15.3k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
616
7.60k
      cconvert->pub._color_convert = grayscale_convert;
617
7.73k
    else if (IsExtRGB(cinfo->in_color_space)) {
618
#ifdef WITH_SIMD
619
3.93k
      if (jsimd_can_rgb_gray())
620
3.93k
        cconvert->pub._color_convert = jsimd_rgb_gray_convert;
621
0
      else
622
0
#endif
623
0
      {
624
0
        cconvert->pub.start_pass = rgb_ycc_start;
625
3.80k
        cconvert->pub._color_convert = rgb_gray_convert;
626
0
      }
627
7.73k
    } else if (cinfo->in_color_space == JCS_YCbCr)
628
0
      cconvert->pub._color_convert = grayscale_convert;
629
0
    else
630
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
631
15.3k
    break;
632
633
6.58k
  case JCS_RGB:
634
6.58k
    if (cinfo->master->lossless && !IsExtRGB(cinfo->in_color_space))
635
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
636
6.58k
    if (cinfo->num_components != 3)
637
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
638
6.58k
    if (rgb_red[cinfo->in_color_space] == 0 &&
639
6.58k
        rgb_green[cinfo->in_color_space] == 1 &&
640
6.58k
        rgb_blue[cinfo->in_color_space] == 2 &&
641
6.58k
        rgb_pixelsize[cinfo->in_color_space] == 3) {
642
#if defined(WITH_SIMD) && defined(__mips__)
643
      if (jsimd_c_can_null_convert())
644
        cconvert->pub._color_convert = jsimd_c_null_convert;
645
      else
646
#endif
647
1.31k
        cconvert->pub._color_convert = null_convert;
648
5.26k
    } else if (IsExtRGB(cinfo->in_color_space))
649
5.26k
      cconvert->pub._color_convert = rgb_rgb_convert;
650
0
    else
651
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
652
6.58k
    break;
653
654
30.9k
  case JCS_YCbCr:
655
30.9k
    if (cinfo->master->lossless &&
656
30.9k
        cinfo->in_color_space != cinfo->jpeg_color_space)
657
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
658
30.9k
    if (cinfo->num_components != 3)
659
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
660
30.9k
    if (IsExtRGB(cinfo->in_color_space)) {
661
#ifdef WITH_SIMD
662
15.7k
      if (jsimd_can_rgb_ycc())
663
15.7k
        cconvert->pub._color_convert = jsimd_rgb_ycc_convert;
664
0
      else
665
0
#endif
666
0
      {
667
0
        cconvert->pub.start_pass = rgb_ycc_start;
668
15.2k
        cconvert->pub._color_convert = rgb_ycc_convert;
669
0
      }
670
30.9k
    } else if (cinfo->in_color_space == JCS_YCbCr) {
671
#if defined(WITH_SIMD) && defined(__mips__)
672
      if (jsimd_c_can_null_convert())
673
        cconvert->pub._color_convert = jsimd_c_null_convert;
674
      else
675
#endif
676
0
        cconvert->pub._color_convert = null_convert;
677
0
    } else
678
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
679
30.9k
    break;
680
681
1.31k
  case JCS_CMYK:
682
1.31k
    if (cinfo->master->lossless &&
683
1.31k
        cinfo->in_color_space != cinfo->jpeg_color_space)
684
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
685
1.31k
    if (cinfo->num_components != 4)
686
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
687
1.31k
    if (cinfo->in_color_space == JCS_CMYK) {
688
#if defined(WITH_SIMD) && defined(__mips__)
689
      if (jsimd_c_can_null_convert())
690
        cconvert->pub._color_convert = jsimd_c_null_convert;
691
      else
692
#endif
693
1.31k
        cconvert->pub._color_convert = null_convert;
694
1.31k
    } else
695
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
696
1.31k
    break;
697
698
5.82k
  case JCS_YCCK:
699
5.82k
    if (cinfo->master->lossless &&
700
5.82k
        cinfo->in_color_space != cinfo->jpeg_color_space)
701
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
702
5.82k
    if (cinfo->num_components != 4)
703
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
704
5.82k
    if (cinfo->in_color_space == JCS_CMYK) {
705
5.82k
      cconvert->pub.start_pass = rgb_ycc_start;
706
5.82k
      cconvert->pub._color_convert = cmyk_ycck_convert;
707
5.82k
    } else if (cinfo->in_color_space == JCS_YCCK) {
708
#if defined(WITH_SIMD) && defined(__mips__)
709
      if (jsimd_c_can_null_convert())
710
        cconvert->pub._color_convert = jsimd_c_null_convert;
711
      else
712
#endif
713
0
        cconvert->pub._color_convert = null_convert;
714
0
    } else
715
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
716
5.82k
    break;
717
718
0
  default:                      /* allow null conversion of JCS_UNKNOWN */
719
0
    if (cinfo->jpeg_color_space != cinfo->in_color_space ||
720
0
        cinfo->num_components != cinfo->input_components)
721
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
722
#if defined(WITH_SIMD) && defined(__mips__)
723
    if (jsimd_c_can_null_convert())
724
      cconvert->pub._color_convert = jsimd_c_null_convert;
725
    else
726
#endif
727
0
      cconvert->pub._color_convert = null_convert;
728
0
    break;
729
60.0k
  }
730
60.0k
}
jinit_color_converter
Line
Count
Source
552
29.2k
{
553
29.2k
  my_cconvert_ptr cconvert;
554
555
29.2k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
556
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
557
558
29.2k
  cconvert = (my_cconvert_ptr)
559
29.2k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
560
29.2k
                                sizeof(my_color_converter));
561
29.2k
  cinfo->cconvert = (struct jpeg_color_converter *)cconvert;
562
  /* set start_pass to null method until we find out differently */
563
29.2k
  cconvert->pub.start_pass = null_method;
564
565
  /* Make sure input_components agrees with in_color_space */
566
29.2k
  switch (cinfo->in_color_space) {
567
3.80k
  case JCS_GRAYSCALE:
568
3.80k
    if (cinfo->input_components != 1)
569
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
570
3.80k
    break;
571
572
0
  case JCS_RGB:
573
4.56k
  case JCS_EXT_RGB:
574
7.21k
  case JCS_EXT_RGBX:
575
13.6k
  case JCS_EXT_BGR:
576
13.6k
  case JCS_EXT_BGRX:
577
15.6k
  case JCS_EXT_XBGR:
578
20.1k
  case JCS_EXT_XRGB:
579
20.1k
  case JCS_EXT_RGBA:
580
22.8k
  case JCS_EXT_BGRA:
581
22.8k
  case JCS_EXT_ABGR:
582
22.8k
  case JCS_EXT_ARGB:
583
22.8k
    if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
584
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
585
22.8k
    break;
586
587
0
  case JCS_YCbCr:
588
0
    if (cinfo->input_components != 3)
589
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
590
0
    break;
591
592
2.65k
  case JCS_CMYK:
593
2.65k
  case JCS_YCCK:
594
2.65k
    if (cinfo->input_components != 4)
595
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
596
2.65k
    break;
597
598
0
  default:                      /* JCS_UNKNOWN can be anything */
599
0
    if (cinfo->input_components < 1)
600
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
601
0
    break;
602
29.2k
  }
603
604
  /* Check num_components, set conversion method based on requested space.
605
   * NOTE: We do not allow any lossy color conversion algorithms in lossless
606
   * mode.
607
   */
608
29.2k
  switch (cinfo->jpeg_color_space) {
609
7.74k
  case JCS_GRAYSCALE:
610
7.74k
    if (cinfo->master->lossless &&
611
7.74k
        cinfo->in_color_space != cinfo->jpeg_color_space)
612
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
613
7.74k
    if (cinfo->num_components != 1)
614
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
615
7.74k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
616
3.80k
      cconvert->pub._color_convert = grayscale_convert;
617
3.93k
    else if (IsExtRGB(cinfo->in_color_space)) {
618
3.93k
#ifdef WITH_SIMD
619
3.93k
      if (jsimd_can_rgb_gray())
620
3.93k
        cconvert->pub._color_convert = jsimd_rgb_gray_convert;
621
0
      else
622
0
#endif
623
0
      {
624
0
        cconvert->pub.start_pass = rgb_ycc_start;
625
0
        cconvert->pub._color_convert = rgb_gray_convert;
626
0
      }
627
3.93k
    } else if (cinfo->in_color_space == JCS_YCbCr)
628
0
      cconvert->pub._color_convert = grayscale_convert;
629
0
    else
630
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
631
7.74k
    break;
632
633
3.14k
  case JCS_RGB:
634
3.14k
    if (cinfo->master->lossless && !IsExtRGB(cinfo->in_color_space))
635
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
636
3.14k
    if (cinfo->num_components != 3)
637
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
638
3.14k
    if (rgb_red[cinfo->in_color_space] == 0 &&
639
3.14k
        rgb_green[cinfo->in_color_space] == 1 &&
640
3.14k
        rgb_blue[cinfo->in_color_space] == 2 &&
641
3.14k
        rgb_pixelsize[cinfo->in_color_space] == 3) {
642
#if defined(WITH_SIMD) && defined(__mips__)
643
      if (jsimd_c_can_null_convert())
644
        cconvert->pub._color_convert = jsimd_c_null_convert;
645
      else
646
#endif
647
628
        cconvert->pub._color_convert = null_convert;
648
2.51k
    } else if (IsExtRGB(cinfo->in_color_space))
649
2.51k
      cconvert->pub._color_convert = rgb_rgb_convert;
650
0
    else
651
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
652
3.14k
    break;
653
654
15.7k
  case JCS_YCbCr:
655
15.7k
    if (cinfo->master->lossless &&
656
15.7k
        cinfo->in_color_space != cinfo->jpeg_color_space)
657
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
658
15.7k
    if (cinfo->num_components != 3)
659
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
660
15.7k
    if (IsExtRGB(cinfo->in_color_space)) {
661
15.7k
#ifdef WITH_SIMD
662
15.7k
      if (jsimd_can_rgb_ycc())
663
15.7k
        cconvert->pub._color_convert = jsimd_rgb_ycc_convert;
664
0
      else
665
0
#endif
666
0
      {
667
0
        cconvert->pub.start_pass = rgb_ycc_start;
668
0
        cconvert->pub._color_convert = rgb_ycc_convert;
669
0
      }
670
15.7k
    } else if (cinfo->in_color_space == JCS_YCbCr) {
671
#if defined(WITH_SIMD) && defined(__mips__)
672
      if (jsimd_c_can_null_convert())
673
        cconvert->pub._color_convert = jsimd_c_null_convert;
674
      else
675
#endif
676
0
        cconvert->pub._color_convert = null_convert;
677
0
    } else
678
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
679
15.7k
    break;
680
681
628
  case JCS_CMYK:
682
628
    if (cinfo->master->lossless &&
683
628
        cinfo->in_color_space != cinfo->jpeg_color_space)
684
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
685
628
    if (cinfo->num_components != 4)
686
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
687
628
    if (cinfo->in_color_space == JCS_CMYK) {
688
#if defined(WITH_SIMD) && defined(__mips__)
689
      if (jsimd_c_can_null_convert())
690
        cconvert->pub._color_convert = jsimd_c_null_convert;
691
      else
692
#endif
693
628
        cconvert->pub._color_convert = null_convert;
694
628
    } else
695
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
696
628
    break;
697
698
2.02k
  case JCS_YCCK:
699
2.02k
    if (cinfo->master->lossless &&
700
2.02k
        cinfo->in_color_space != cinfo->jpeg_color_space)
701
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
702
2.02k
    if (cinfo->num_components != 4)
703
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
704
2.02k
    if (cinfo->in_color_space == JCS_CMYK) {
705
2.02k
      cconvert->pub.start_pass = rgb_ycc_start;
706
2.02k
      cconvert->pub._color_convert = cmyk_ycck_convert;
707
2.02k
    } else if (cinfo->in_color_space == JCS_YCCK) {
708
#if defined(WITH_SIMD) && defined(__mips__)
709
      if (jsimd_c_can_null_convert())
710
        cconvert->pub._color_convert = jsimd_c_null_convert;
711
      else
712
#endif
713
0
        cconvert->pub._color_convert = null_convert;
714
0
    } else
715
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
716
2.02k
    break;
717
718
0
  default:                      /* allow null conversion of JCS_UNKNOWN */
719
0
    if (cinfo->jpeg_color_space != cinfo->in_color_space ||
720
0
        cinfo->num_components != cinfo->input_components)
721
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
722
#if defined(WITH_SIMD) && defined(__mips__)
723
    if (jsimd_c_can_null_convert())
724
      cconvert->pub._color_convert = jsimd_c_null_convert;
725
    else
726
#endif
727
0
      cconvert->pub._color_convert = null_convert;
728
0
    break;
729
29.2k
  }
730
29.2k
}
j12init_color_converter
Line
Count
Source
552
25.9k
{
553
25.9k
  my_cconvert_ptr cconvert;
554
555
25.9k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
556
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
557
558
25.9k
  cconvert = (my_cconvert_ptr)
559
25.9k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
560
25.9k
                                sizeof(my_color_converter));
561
25.9k
  cinfo->cconvert = (struct jpeg_color_converter *)cconvert;
562
  /* set start_pass to null method until we find out differently */
563
25.9k
  cconvert->pub.start_pass = null_method;
564
565
  /* Make sure input_components agrees with in_color_space */
566
25.9k
  switch (cinfo->in_color_space) {
567
3.18k
  case JCS_GRAYSCALE:
568
3.18k
    if (cinfo->input_components != 1)
569
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
570
3.18k
    break;
571
572
0
  case JCS_RGB:
573
3.80k
  case JCS_EXT_RGB:
574
7.60k
  case JCS_EXT_RGBX:
575
11.4k
  case JCS_EXT_BGR:
576
11.4k
  case JCS_EXT_BGRX:
577
11.4k
  case JCS_EXT_XBGR:
578
15.2k
  case JCS_EXT_XRGB:
579
15.2k
  case JCS_EXT_RGBA:
580
19.0k
  case JCS_EXT_BGRA:
581
19.0k
  case JCS_EXT_ABGR:
582
19.0k
  case JCS_EXT_ARGB:
583
19.0k
    if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
584
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
585
19.0k
    break;
586
587
0
  case JCS_YCbCr:
588
0
    if (cinfo->input_components != 3)
589
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
590
0
    break;
591
592
3.80k
  case JCS_CMYK:
593
3.80k
  case JCS_YCCK:
594
3.80k
    if (cinfo->input_components != 4)
595
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
596
3.80k
    break;
597
598
0
  default:                      /* JCS_UNKNOWN can be anything */
599
0
    if (cinfo->input_components < 1)
600
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
601
0
    break;
602
25.9k
  }
603
604
  /* Check num_components, set conversion method based on requested space.
605
   * NOTE: We do not allow any lossy color conversion algorithms in lossless
606
   * mode.
607
   */
608
25.9k
  switch (cinfo->jpeg_color_space) {
609
6.98k
  case JCS_GRAYSCALE:
610
6.98k
    if (cinfo->master->lossless &&
611
6.98k
        cinfo->in_color_space != cinfo->jpeg_color_space)
612
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
613
6.98k
    if (cinfo->num_components != 1)
614
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
615
6.98k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
616
3.18k
      cconvert->pub._color_convert = grayscale_convert;
617
3.80k
    else if (IsExtRGB(cinfo->in_color_space)) {
618
#ifdef WITH_SIMD
619
      if (jsimd_can_rgb_gray())
620
        cconvert->pub._color_convert = jsimd_rgb_gray_convert;
621
      else
622
#endif
623
3.80k
      {
624
3.80k
        cconvert->pub.start_pass = rgb_ycc_start;
625
3.80k
        cconvert->pub._color_convert = rgb_gray_convert;
626
3.80k
      }
627
3.80k
    } else if (cinfo->in_color_space == JCS_YCbCr)
628
0
      cconvert->pub._color_convert = grayscale_convert;
629
0
    else
630
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
631
6.98k
    break;
632
633
0
  case JCS_RGB:
634
0
    if (cinfo->master->lossless && !IsExtRGB(cinfo->in_color_space))
635
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
636
0
    if (cinfo->num_components != 3)
637
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
638
0
    if (rgb_red[cinfo->in_color_space] == 0 &&
639
0
        rgb_green[cinfo->in_color_space] == 1 &&
640
0
        rgb_blue[cinfo->in_color_space] == 2 &&
641
0
        rgb_pixelsize[cinfo->in_color_space] == 3) {
642
#if defined(WITH_SIMD) && defined(__mips__)
643
      if (jsimd_c_can_null_convert())
644
        cconvert->pub._color_convert = jsimd_c_null_convert;
645
      else
646
#endif
647
0
        cconvert->pub._color_convert = null_convert;
648
0
    } else if (IsExtRGB(cinfo->in_color_space))
649
0
      cconvert->pub._color_convert = rgb_rgb_convert;
650
0
    else
651
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
652
0
    break;
653
654
15.2k
  case JCS_YCbCr:
655
15.2k
    if (cinfo->master->lossless &&
656
15.2k
        cinfo->in_color_space != cinfo->jpeg_color_space)
657
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
658
15.2k
    if (cinfo->num_components != 3)
659
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
660
15.2k
    if (IsExtRGB(cinfo->in_color_space)) {
661
#ifdef WITH_SIMD
662
      if (jsimd_can_rgb_ycc())
663
        cconvert->pub._color_convert = jsimd_rgb_ycc_convert;
664
      else
665
#endif
666
15.2k
      {
667
15.2k
        cconvert->pub.start_pass = rgb_ycc_start;
668
15.2k
        cconvert->pub._color_convert = rgb_ycc_convert;
669
15.2k
      }
670
15.2k
    } else if (cinfo->in_color_space == JCS_YCbCr) {
671
#if defined(WITH_SIMD) && defined(__mips__)
672
      if (jsimd_c_can_null_convert())
673
        cconvert->pub._color_convert = jsimd_c_null_convert;
674
      else
675
#endif
676
0
        cconvert->pub._color_convert = null_convert;
677
0
    } else
678
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
679
15.2k
    break;
680
681
0
  case JCS_CMYK:
682
0
    if (cinfo->master->lossless &&
683
0
        cinfo->in_color_space != cinfo->jpeg_color_space)
684
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
685
0
    if (cinfo->num_components != 4)
686
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
687
0
    if (cinfo->in_color_space == JCS_CMYK) {
688
#if defined(WITH_SIMD) && defined(__mips__)
689
      if (jsimd_c_can_null_convert())
690
        cconvert->pub._color_convert = jsimd_c_null_convert;
691
      else
692
#endif
693
0
        cconvert->pub._color_convert = null_convert;
694
0
    } else
695
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
696
0
    break;
697
698
3.80k
  case JCS_YCCK:
699
3.80k
    if (cinfo->master->lossless &&
700
3.80k
        cinfo->in_color_space != cinfo->jpeg_color_space)
701
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
702
3.80k
    if (cinfo->num_components != 4)
703
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
704
3.80k
    if (cinfo->in_color_space == JCS_CMYK) {
705
3.80k
      cconvert->pub.start_pass = rgb_ycc_start;
706
3.80k
      cconvert->pub._color_convert = cmyk_ycck_convert;
707
3.80k
    } else if (cinfo->in_color_space == JCS_YCCK) {
708
#if defined(WITH_SIMD) && defined(__mips__)
709
      if (jsimd_c_can_null_convert())
710
        cconvert->pub._color_convert = jsimd_c_null_convert;
711
      else
712
#endif
713
0
        cconvert->pub._color_convert = null_convert;
714
0
    } else
715
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
716
3.80k
    break;
717
718
0
  default:                      /* allow null conversion of JCS_UNKNOWN */
719
0
    if (cinfo->jpeg_color_space != cinfo->in_color_space ||
720
0
        cinfo->num_components != cinfo->input_components)
721
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
722
#if defined(WITH_SIMD) && defined(__mips__)
723
    if (jsimd_c_can_null_convert())
724
      cconvert->pub._color_convert = jsimd_c_null_convert;
725
    else
726
#endif
727
0
      cconvert->pub._color_convert = null_convert;
728
0
    break;
729
25.9k
  }
730
25.9k
}
j16init_color_converter
Line
Count
Source
552
4.74k
{
553
4.74k
  my_cconvert_ptr cconvert;
554
555
4.74k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
556
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
557
558
4.74k
  cconvert = (my_cconvert_ptr)
559
4.74k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
560
4.74k
                                sizeof(my_color_converter));
561
4.74k
  cinfo->cconvert = (struct jpeg_color_converter *)cconvert;
562
  /* set start_pass to null method until we find out differently */
563
4.74k
  cconvert->pub.start_pass = null_method;
564
565
  /* Make sure input_components agrees with in_color_space */
566
4.74k
  switch (cinfo->in_color_space) {
567
612
  case JCS_GRAYSCALE:
568
612
    if (cinfo->input_components != 1)
569
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
570
612
    break;
571
572
0
  case JCS_RGB:
573
689
  case JCS_EXT_RGB:
574
1.37k
  case JCS_EXT_RGBX:
575
2.06k
  case JCS_EXT_BGR:
576
2.06k
  case JCS_EXT_BGRX:
577
2.06k
  case JCS_EXT_XBGR:
578
2.75k
  case JCS_EXT_XRGB:
579
2.75k
  case JCS_EXT_RGBA:
580
3.44k
  case JCS_EXT_BGRA:
581
3.44k
  case JCS_EXT_ABGR:
582
3.44k
  case JCS_EXT_ARGB:
583
3.44k
    if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
584
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
585
3.44k
    break;
586
587
0
  case JCS_YCbCr:
588
0
    if (cinfo->input_components != 3)
589
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
590
0
    break;
591
592
689
  case JCS_CMYK:
593
689
  case JCS_YCCK:
594
689
    if (cinfo->input_components != 4)
595
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
596
689
    break;
597
598
0
  default:                      /* JCS_UNKNOWN can be anything */
599
0
    if (cinfo->input_components < 1)
600
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
601
0
    break;
602
4.74k
  }
603
604
  /* Check num_components, set conversion method based on requested space.
605
   * NOTE: We do not allow any lossy color conversion algorithms in lossless
606
   * mode.
607
   */
608
4.74k
  switch (cinfo->jpeg_color_space) {
609
612
  case JCS_GRAYSCALE:
610
612
    if (cinfo->master->lossless &&
611
612
        cinfo->in_color_space != cinfo->jpeg_color_space)
612
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
613
612
    if (cinfo->num_components != 1)
614
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
615
612
    if (cinfo->in_color_space == JCS_GRAYSCALE)
616
612
      cconvert->pub._color_convert = grayscale_convert;
617
0
    else if (IsExtRGB(cinfo->in_color_space)) {
618
#ifdef WITH_SIMD
619
      if (jsimd_can_rgb_gray())
620
        cconvert->pub._color_convert = jsimd_rgb_gray_convert;
621
      else
622
#endif
623
0
      {
624
0
        cconvert->pub.start_pass = rgb_ycc_start;
625
0
        cconvert->pub._color_convert = rgb_gray_convert;
626
0
      }
627
0
    } else if (cinfo->in_color_space == JCS_YCbCr)
628
0
      cconvert->pub._color_convert = grayscale_convert;
629
0
    else
630
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
631
612
    break;
632
633
3.44k
  case JCS_RGB:
634
3.44k
    if (cinfo->master->lossless && !IsExtRGB(cinfo->in_color_space))
635
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
636
3.44k
    if (cinfo->num_components != 3)
637
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
638
3.44k
    if (rgb_red[cinfo->in_color_space] == 0 &&
639
3.44k
        rgb_green[cinfo->in_color_space] == 1 &&
640
3.44k
        rgb_blue[cinfo->in_color_space] == 2 &&
641
3.44k
        rgb_pixelsize[cinfo->in_color_space] == 3) {
642
#if defined(WITH_SIMD) && defined(__mips__)
643
      if (jsimd_c_can_null_convert())
644
        cconvert->pub._color_convert = jsimd_c_null_convert;
645
      else
646
#endif
647
689
        cconvert->pub._color_convert = null_convert;
648
2.75k
    } else if (IsExtRGB(cinfo->in_color_space))
649
2.75k
      cconvert->pub._color_convert = rgb_rgb_convert;
650
0
    else
651
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
652
3.44k
    break;
653
654
0
  case JCS_YCbCr:
655
0
    if (cinfo->master->lossless &&
656
0
        cinfo->in_color_space != cinfo->jpeg_color_space)
657
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
658
0
    if (cinfo->num_components != 3)
659
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
660
0
    if (IsExtRGB(cinfo->in_color_space)) {
661
#ifdef WITH_SIMD
662
      if (jsimd_can_rgb_ycc())
663
        cconvert->pub._color_convert = jsimd_rgb_ycc_convert;
664
      else
665
#endif
666
0
      {
667
0
        cconvert->pub.start_pass = rgb_ycc_start;
668
0
        cconvert->pub._color_convert = rgb_ycc_convert;
669
0
      }
670
0
    } else if (cinfo->in_color_space == JCS_YCbCr) {
671
#if defined(WITH_SIMD) && defined(__mips__)
672
      if (jsimd_c_can_null_convert())
673
        cconvert->pub._color_convert = jsimd_c_null_convert;
674
      else
675
#endif
676
0
        cconvert->pub._color_convert = null_convert;
677
0
    } else
678
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
679
0
    break;
680
681
689
  case JCS_CMYK:
682
689
    if (cinfo->master->lossless &&
683
689
        cinfo->in_color_space != cinfo->jpeg_color_space)
684
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
685
689
    if (cinfo->num_components != 4)
686
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
687
689
    if (cinfo->in_color_space == JCS_CMYK) {
688
#if defined(WITH_SIMD) && defined(__mips__)
689
      if (jsimd_c_can_null_convert())
690
        cconvert->pub._color_convert = jsimd_c_null_convert;
691
      else
692
#endif
693
689
        cconvert->pub._color_convert = null_convert;
694
689
    } else
695
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
696
689
    break;
697
698
0
  case JCS_YCCK:
699
0
    if (cinfo->master->lossless &&
700
0
        cinfo->in_color_space != cinfo->jpeg_color_space)
701
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
702
0
    if (cinfo->num_components != 4)
703
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
704
0
    if (cinfo->in_color_space == JCS_CMYK) {
705
0
      cconvert->pub.start_pass = rgb_ycc_start;
706
0
      cconvert->pub._color_convert = cmyk_ycck_convert;
707
0
    } else if (cinfo->in_color_space == JCS_YCCK) {
708
#if defined(WITH_SIMD) && defined(__mips__)
709
      if (jsimd_c_can_null_convert())
710
        cconvert->pub._color_convert = jsimd_c_null_convert;
711
      else
712
#endif
713
0
        cconvert->pub._color_convert = null_convert;
714
0
    } else
715
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
716
0
    break;
717
718
0
  default:                      /* allow null conversion of JCS_UNKNOWN */
719
0
    if (cinfo->jpeg_color_space != cinfo->in_color_space ||
720
0
        cinfo->num_components != cinfo->input_components)
721
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
722
#if defined(WITH_SIMD) && defined(__mips__)
723
    if (jsimd_c_can_null_convert())
724
      cconvert->pub._color_convert = jsimd_c_null_convert;
725
    else
726
#endif
727
0
      cconvert->pub._color_convert = null_convert;
728
0
    break;
729
4.74k
  }
730
4.74k
}
731
732
#endif /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */