Coverage Report

Created: 2025-07-01 06:27

/src/libjpeg-turbo.3.0.x/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.65G
#define SCALEBITS       16      /* speediest right-shift on some machines */
70
96.7M
#define CBCR_OFFSET     ((JLONG)_CENTERJSAMPLE << SCALEBITS)
71
193M
#define ONE_HALF        ((JLONG)1 << (SCALEBITS - 1))
72
774M
#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
315M
#define R_Y_OFF         0                       /* offset to R => Y section */
81
315M
#define G_Y_OFF         (1 * (_MAXJSAMPLE + 1)) /* offset to G => Y section */
82
315M
#define B_Y_OFF         (2 * (_MAXJSAMPLE + 1)) /* etc. */
83
281M
#define R_CB_OFF        (3 * (_MAXJSAMPLE + 1))
84
281M
#define G_CB_OFF        (4 * (_MAXJSAMPLE + 1))
85
467M
#define B_CB_OFF        (5 * (_MAXJSAMPLE + 1))
86
185M
#define R_CR_OFF        B_CB_OFF                /* B=>Cb, R=>Cr are the same */
87
281M
#define G_CR_OFF        (6 * (_MAXJSAMPLE + 1))
88
281M
#define B_CR_OFF        (7 * (_MAXJSAMPLE + 1))
89
25.5k
#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
656M
#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
33.7M
#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
19.5M
#define RGB_RED  EXT_RGBX_RED
129
19.5M
#define RGB_GREEN  EXT_RGBX_GREEN
130
19.5M
#define RGB_BLUE  EXT_RGBX_BLUE
131
53.3M
#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
19.5M
#define RGB_RED  EXT_BGR_RED
145
19.5M
#define RGB_GREEN  EXT_BGR_GREEN
146
19.5M
#define RGB_BLUE  EXT_BGR_BLUE
147
53.3M
#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
19.5M
#define RGB_RED  EXT_BGRX_RED
161
19.5M
#define RGB_GREEN  EXT_BGRX_GREEN
162
19.5M
#define RGB_BLUE  EXT_BGRX_BLUE
163
53.3M
#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
19.5M
#define RGB_RED  EXT_XRGB_RED
193
19.5M
#define RGB_GREEN  EXT_XRGB_GREEN
194
19.5M
#define RGB_BLUE  EXT_XRGB_BLUE
195
53.3M
#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
25.5k
{
216
25.5k
#if BITS_IN_JSAMPLE != 16
217
25.5k
  my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
218
25.5k
  JLONG *rgb_ycc_tab;
219
25.5k
  JLONG i;
220
221
  /* Allocate and fill in the conversion tables. */
222
25.5k
  cconvert->rgb_ycc_tab = rgb_ycc_tab = (JLONG *)
223
25.5k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
224
25.5k
                                (TABLE_SIZE * sizeof(JLONG)));
225
226
96.7M
  for (i = 0; i <= _MAXJSAMPLE; i++) {
227
96.7M
    rgb_ycc_tab[i + R_Y_OFF] = FIX(0.29900) * i;
228
96.7M
    rgb_ycc_tab[i + G_Y_OFF] = FIX(0.58700) * i;
229
96.7M
    rgb_ycc_tab[i + B_Y_OFF] = FIX(0.11400) * i   + ONE_HALF;
230
96.7M
    rgb_ycc_tab[i + R_CB_OFF] = (-FIX(0.16874)) * i;
231
96.7M
    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
96.7M
    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
96.7M
    rgb_ycc_tab[i + G_CR_OFF] = (-FIX(0.41869)) * i;
241
96.7M
    rgb_ycc_tab[i + B_CR_OFF] = (-FIX(0.08131)) * i;
242
96.7M
  }
243
#else
244
  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
245
#endif
246
25.5k
}
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
47.8M
{
257
47.8M
  switch (cinfo->in_color_space) {
258
13.6M
  case JCS_EXT_RGB:
259
13.6M
    extrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
260
13.6M
                                num_rows);
261
13.6M
    break;
262
6.84M
  case JCS_EXT_RGBX:
263
6.84M
  case JCS_EXT_RGBA:
264
6.84M
    extrgbx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
265
6.84M
                                 num_rows);
266
6.84M
    break;
267
13.6M
  case JCS_EXT_BGR:
268
13.6M
    extbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
269
13.6M
                                num_rows);
270
13.6M
    break;
271
0
  case JCS_EXT_BGRX:
272
13.6M
  case JCS_EXT_BGRA:
273
13.6M
    extbgrx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,
274
13.6M
                                 num_rows);
275
13.6M
    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
47.8M
  }
291
47.8M
}
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
13.6M
{
305
13.6M
  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
13.6M
  case JCS_EXT_XRGB:
330
13.6M
  case JCS_EXT_ARGB:
331
13.6M
    extxrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,
332
13.6M
                                  num_rows);
333
13.6M
    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
13.6M
  }
339
13.6M
}
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
15.1M
{
350
15.1M
  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
3.79M
  case JCS_EXT_RGBX:
356
3.79M
  case JCS_EXT_RGBA:
357
3.79M
    extrgbx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
358
3.79M
                                 num_rows);
359
3.79M
    break;
360
3.79M
  case JCS_EXT_BGR:
361
3.79M
    extbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
362
3.79M
                                num_rows);
363
3.79M
    break;
364
0
  case JCS_EXT_BGRX:
365
3.79M
  case JCS_EXT_BGRA:
366
3.79M
    extbgrx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
367
3.79M
                                 num_rows);
368
3.79M
    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
3.79M
  case JCS_EXT_XRGB:
375
3.79M
  case JCS_EXT_ARGB:
376
3.79M
    extxrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,
377
3.79M
                                 num_rows);
378
3.79M
    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
15.1M
  }
384
15.1M
}
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
10.2M
{
399
10.2M
#if BITS_IN_JSAMPLE != 16
400
10.2M
  my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
401
10.2M
  register int r, g, b;
402
10.2M
  register JLONG *ctab = cconvert->rgb_ycc_tab;
403
10.2M
  register _JSAMPROW inptr;
404
10.2M
  register _JSAMPROW outptr0, outptr1, outptr2, outptr3;
405
10.2M
  register JDIMENSION col;
406
10.2M
  JDIMENSION num_cols = cinfo->image_width;
407
408
30.8M
  while (--num_rows >= 0) {
409
20.5M
    inptr = *input_buf++;
410
20.5M
    outptr0 = output_buf[0][output_row];
411
20.5M
    outptr1 = output_buf[1][output_row];
412
20.5M
    outptr2 = output_buf[2][output_row];
413
20.5M
    outptr3 = output_buf[3][output_row];
414
20.5M
    output_row++;
415
70.7M
    for (col = 0; col < num_cols; col++) {
416
50.1M
      r = _MAXJSAMPLE - RANGE_LIMIT(inptr[0]);
417
50.1M
      g = _MAXJSAMPLE - RANGE_LIMIT(inptr[1]);
418
50.1M
      b = _MAXJSAMPLE - RANGE_LIMIT(inptr[2]);
419
      /* K passes through as-is */
420
50.1M
      outptr3[col] = inptr[3];
421
50.1M
      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
50.1M
      outptr0[col] = (_JSAMPLE)((ctab[r + R_Y_OFF] + ctab[g + G_Y_OFF] +
429
50.1M
                                 ctab[b + B_Y_OFF]) >> SCALEBITS);
430
      /* Cb */
431
50.1M
      outptr1[col] = (_JSAMPLE)((ctab[r + R_CB_OFF] + ctab[g + G_CB_OFF] +
432
50.1M
                                 ctab[b + B_CB_OFF]) >> SCALEBITS);
433
      /* Cr */
434
50.1M
      outptr2[col] = (_JSAMPLE)((ctab[r + R_CR_OFF] + ctab[g + G_CR_OFF] +
435
50.1M
                                 ctab[b + B_CR_OFF]) >> SCALEBITS);
436
50.1M
    }
437
20.5M
  }
438
#else
439
  ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
440
#endif
441
10.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
28.5M
{
454
28.5M
  register _JSAMPROW inptr;
455
28.5M
  register _JSAMPROW outptr;
456
28.5M
  register JDIMENSION col;
457
28.5M
  JDIMENSION num_cols = cinfo->image_width;
458
28.5M
  int instride = cinfo->input_components;
459
460
57.1M
  while (--num_rows >= 0) {
461
28.5M
    inptr = *input_buf++;
462
28.5M
    outptr = output_buf[0][output_row];
463
28.5M
    output_row++;
464
101M
    for (col = 0; col < num_cols; col++) {
465
73.1M
      outptr[col] = inptr[0];
466
73.1M
      inptr += instride;
467
73.1M
    }
468
28.5M
  }
469
28.5M
}
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
7.58M
{
482
7.58M
  register _JSAMPROW inptr;
483
7.58M
  register _JSAMPROW outptr, outptr0, outptr1, outptr2, outptr3;
484
7.58M
  register JDIMENSION col;
485
7.58M
  register int ci;
486
7.58M
  int nc = cinfo->num_components;
487
7.58M
  JDIMENSION num_cols = cinfo->image_width;
488
489
7.58M
  if (nc == 3) {
490
7.58M
    while (--num_rows >= 0) {
491
3.79M
      inptr = *input_buf++;
492
3.79M
      outptr0 = output_buf[0][output_row];
493
3.79M
      outptr1 = output_buf[1][output_row];
494
3.79M
      outptr2 = output_buf[2][output_row];
495
3.79M
      output_row++;
496
23.3M
      for (col = 0; col < num_cols; col++) {
497
19.5M
        outptr0[col] = *inptr++;
498
19.5M
        outptr1[col] = *inptr++;
499
19.5M
        outptr2[col] = *inptr++;
500
19.5M
      }
501
3.79M
    }
502
3.79M
  } else if (nc == 4) {
503
7.58M
    while (--num_rows >= 0) {
504
3.79M
      inptr = *input_buf++;
505
3.79M
      outptr0 = output_buf[0][output_row];
506
3.79M
      outptr1 = output_buf[1][output_row];
507
3.79M
      outptr2 = output_buf[2][output_row];
508
3.79M
      outptr3 = output_buf[3][output_row];
509
3.79M
      output_row++;
510
23.3M
      for (col = 0; col < num_cols; col++) {
511
19.5M
        outptr0[col] = *inptr++;
512
19.5M
        outptr1[col] = *inptr++;
513
19.5M
        outptr2[col] = *inptr++;
514
19.5M
        outptr3[col] = *inptr++;
515
19.5M
      }
516
3.79M
    }
517
3.79M
  } 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
7.58M
}
533
534
535
/*
536
 * Empty method for start_pass.
537
 */
538
539
METHODDEF(void)
540
null_method(j_compress_ptr cinfo)
541
35.2k
{
542
  /* no work needed */
543
35.2k
}
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.7k
{
553
60.7k
  my_cconvert_ptr cconvert;
554
555
60.7k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
556
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
557
558
60.7k
  cconvert = (my_cconvert_ptr)
559
60.7k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
560
60.7k
                                sizeof(my_color_converter));
561
60.7k
  cinfo->cconvert = (struct jpeg_color_converter *)cconvert;
562
  /* set start_pass to null method until we find out differently */
563
60.7k
  cconvert->pub.start_pass = null_method;
564
565
  /* Make sure input_components agrees with in_color_space */
566
60.7k
  switch (cinfo->in_color_space) {
567
7.61k
  case JCS_GRAYSCALE:
568
7.61k
    if (cinfo->input_components != 1)
569
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
570
7.61k
    break;
571
572
0
  case JCS_RGB:
573
9.17k
  case JCS_EXT_RGB:
574
16.4k
  case JCS_EXT_RGBX:
575
27.5k
  case JCS_EXT_BGR:
576
27.5k
  case JCS_EXT_BGRX:
577
29.4k
  case JCS_EXT_XBGR:
578
38.6k
  case JCS_EXT_XRGB:
579
38.6k
  case JCS_EXT_RGBA:
580
45.8k
  case JCS_EXT_BGRA:
581
45.8k
  case JCS_EXT_ABGR:
582
45.8k
  case JCS_EXT_ARGB:
583
45.8k
    if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
584
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
585
45.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
7.24k
  case JCS_CMYK:
593
7.24k
  case JCS_YCCK:
594
7.24k
    if (cinfo->input_components != 4)
595
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
596
7.24k
    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.7k
  }
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.7k
  switch (cinfo->jpeg_color_space) {
609
15.4k
  case JCS_GRAYSCALE:
610
15.4k
#ifdef C_LOSSLESS_SUPPORTED
611
15.4k
    if (cinfo->master->lossless &&
612
15.4k
        cinfo->in_color_space != cinfo->jpeg_color_space)
613
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
614
15.4k
#endif
615
15.4k
    if (cinfo->num_components != 1)
616
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
617
15.4k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
618
7.61k
      cconvert->pub._color_convert = grayscale_convert;
619
7.84k
    else if (IsExtRGB(cinfo->in_color_space)) {
620
#ifdef WITH_SIMD
621
3.92k
      if (jsimd_can_rgb_gray())
622
3.92k
        cconvert->pub._color_convert = jsimd_rgb_gray_convert;
623
0
      else
624
0
#endif
625
0
      {
626
0
        cconvert->pub.start_pass = rgb_ycc_start;
627
3.91k
        cconvert->pub._color_convert = rgb_gray_convert;
628
0
      }
629
7.84k
    } else if (cinfo->in_color_space == JCS_YCbCr)
630
0
      cconvert->pub._color_convert = grayscale_convert;
631
0
    else
632
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
633
15.4k
    break;
634
635
6.63k
  case JCS_RGB:
636
6.63k
#ifdef C_LOSSLESS_SUPPORTED
637
6.63k
    if (cinfo->master->lossless && !IsExtRGB(cinfo->in_color_space))
638
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
639
6.63k
#endif
640
6.63k
    if (cinfo->num_components != 3)
641
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
642
6.63k
    if (rgb_red[cinfo->in_color_space] == 0 &&
643
6.63k
        rgb_green[cinfo->in_color_space] == 1 &&
644
6.63k
        rgb_blue[cinfo->in_color_space] == 2 &&
645
6.63k
        rgb_pixelsize[cinfo->in_color_space] == 3) {
646
#if defined(WITH_SIMD) && defined(__mips__)
647
      if (jsimd_c_can_null_convert())
648
        cconvert->pub._color_convert = jsimd_c_null_convert;
649
      else
650
#endif
651
1.32k
        cconvert->pub._color_convert = null_convert;
652
5.30k
    } else if (IsExtRGB(cinfo->in_color_space))
653
5.30k
      cconvert->pub._color_convert = rgb_rgb_convert;
654
0
    else
655
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
656
6.63k
    break;
657
658
31.3k
  case JCS_YCbCr:
659
31.3k
#ifdef C_LOSSLESS_SUPPORTED
660
31.3k
    if (cinfo->master->lossless &&
661
31.3k
        cinfo->in_color_space != cinfo->jpeg_color_space)
662
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
663
31.3k
#endif
664
31.3k
    if (cinfo->num_components != 3)
665
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
666
31.3k
    if (IsExtRGB(cinfo->in_color_space)) {
667
#ifdef WITH_SIMD
668
15.7k
      if (jsimd_can_rgb_ycc())
669
15.7k
        cconvert->pub._color_convert = jsimd_rgb_ycc_convert;
670
0
      else
671
0
#endif
672
0
      {
673
0
        cconvert->pub.start_pass = rgb_ycc_start;
674
15.6k
        cconvert->pub._color_convert = rgb_ycc_convert;
675
0
      }
676
31.3k
    } else if (cinfo->in_color_space == JCS_YCbCr) {
677
#if defined(WITH_SIMD) && defined(__mips__)
678
      if (jsimd_c_can_null_convert())
679
        cconvert->pub._color_convert = jsimd_c_null_convert;
680
      else
681
#endif
682
0
        cconvert->pub._color_convert = null_convert;
683
0
    } else
684
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
685
31.3k
    break;
686
687
1.32k
  case JCS_CMYK:
688
1.32k
#ifdef C_LOSSLESS_SUPPORTED
689
1.32k
    if (cinfo->master->lossless &&
690
1.32k
        cinfo->in_color_space != cinfo->jpeg_color_space)
691
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
692
1.32k
#endif
693
1.32k
    if (cinfo->num_components != 4)
694
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
695
1.32k
    if (cinfo->in_color_space == JCS_CMYK) {
696
#if defined(WITH_SIMD) && defined(__mips__)
697
      if (jsimd_c_can_null_convert())
698
        cconvert->pub._color_convert = jsimd_c_null_convert;
699
      else
700
#endif
701
1.32k
        cconvert->pub._color_convert = null_convert;
702
1.32k
    } else
703
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
704
1.32k
    break;
705
706
5.92k
  case JCS_YCCK:
707
5.92k
#ifdef C_LOSSLESS_SUPPORTED
708
5.92k
    if (cinfo->master->lossless &&
709
5.92k
        cinfo->in_color_space != cinfo->jpeg_color_space)
710
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
711
5.92k
#endif
712
5.92k
    if (cinfo->num_components != 4)
713
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
714
5.92k
    if (cinfo->in_color_space == JCS_CMYK) {
715
5.92k
      cconvert->pub.start_pass = rgb_ycc_start;
716
5.92k
      cconvert->pub._color_convert = cmyk_ycck_convert;
717
5.92k
    } else if (cinfo->in_color_space == JCS_YCCK) {
718
#if defined(WITH_SIMD) && defined(__mips__)
719
      if (jsimd_c_can_null_convert())
720
        cconvert->pub._color_convert = jsimd_c_null_convert;
721
      else
722
#endif
723
0
        cconvert->pub._color_convert = null_convert;
724
0
    } else
725
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
726
5.92k
    break;
727
728
0
  default:                      /* allow null conversion of JCS_UNKNOWN */
729
0
    if (cinfo->jpeg_color_space != cinfo->in_color_space ||
730
0
        cinfo->num_components != cinfo->input_components)
731
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
732
#if defined(WITH_SIMD) && defined(__mips__)
733
    if (jsimd_c_can_null_convert())
734
      cconvert->pub._color_convert = jsimd_c_null_convert;
735
    else
736
#endif
737
0
      cconvert->pub._color_convert = null_convert;
738
0
    break;
739
60.7k
  }
740
60.7k
}
jinit_color_converter
Line
Count
Source
552
29.4k
{
553
29.4k
  my_cconvert_ptr cconvert;
554
555
29.4k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
556
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
557
558
29.4k
  cconvert = (my_cconvert_ptr)
559
29.4k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
560
29.4k
                                sizeof(my_color_converter));
561
29.4k
  cinfo->cconvert = (struct jpeg_color_converter *)cconvert;
562
  /* set start_pass to null method until we find out differently */
563
29.4k
  cconvert->pub.start_pass = null_method;
564
565
  /* Make sure input_components agrees with in_color_space */
566
29.4k
  switch (cinfo->in_color_space) {
567
3.81k
  case JCS_GRAYSCALE:
568
3.81k
    if (cinfo->input_components != 1)
569
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
570
3.81k
    break;
571
572
0
  case JCS_RGB:
573
4.59k
  case JCS_EXT_RGB:
574
7.26k
  case JCS_EXT_RGBX:
575
13.7k
  case JCS_EXT_BGR:
576
13.7k
  case JCS_EXT_BGRX:
577
15.7k
  case JCS_EXT_XBGR:
578
20.2k
  case JCS_EXT_XRGB:
579
20.2k
  case JCS_EXT_RGBA:
580
22.9k
  case JCS_EXT_BGRA:
581
22.9k
  case JCS_EXT_ABGR:
582
22.9k
  case JCS_EXT_ARGB:
583
22.9k
    if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
584
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
585
22.9k
    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.66k
  case JCS_CMYK:
593
2.66k
  case JCS_YCCK:
594
2.66k
    if (cinfo->input_components != 4)
595
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
596
2.66k
    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.4k
  }
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.4k
  switch (cinfo->jpeg_color_space) {
609
7.74k
  case JCS_GRAYSCALE:
610
7.74k
#ifdef C_LOSSLESS_SUPPORTED
611
7.74k
    if (cinfo->master->lossless &&
612
7.74k
        cinfo->in_color_space != cinfo->jpeg_color_space)
613
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
614
7.74k
#endif
615
7.74k
    if (cinfo->num_components != 1)
616
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
617
7.74k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
618
3.81k
      cconvert->pub._color_convert = grayscale_convert;
619
3.92k
    else if (IsExtRGB(cinfo->in_color_space)) {
620
3.92k
#ifdef WITH_SIMD
621
3.92k
      if (jsimd_can_rgb_gray())
622
3.92k
        cconvert->pub._color_convert = jsimd_rgb_gray_convert;
623
0
      else
624
0
#endif
625
0
      {
626
0
        cconvert->pub.start_pass = rgb_ycc_start;
627
0
        cconvert->pub._color_convert = rgb_gray_convert;
628
0
      }
629
3.92k
    } else if (cinfo->in_color_space == JCS_YCbCr)
630
0
      cconvert->pub._color_convert = grayscale_convert;
631
0
    else
632
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
633
7.74k
    break;
634
635
3.31k
  case JCS_RGB:
636
3.31k
#ifdef C_LOSSLESS_SUPPORTED
637
3.31k
    if (cinfo->master->lossless && !IsExtRGB(cinfo->in_color_space))
638
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
639
3.31k
#endif
640
3.31k
    if (cinfo->num_components != 3)
641
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
642
3.31k
    if (rgb_red[cinfo->in_color_space] == 0 &&
643
3.31k
        rgb_green[cinfo->in_color_space] == 1 &&
644
3.31k
        rgb_blue[cinfo->in_color_space] == 2 &&
645
3.31k
        rgb_pixelsize[cinfo->in_color_space] == 3) {
646
#if defined(WITH_SIMD) && defined(__mips__)
647
      if (jsimd_c_can_null_convert())
648
        cconvert->pub._color_convert = jsimd_c_null_convert;
649
      else
650
#endif
651
663
        cconvert->pub._color_convert = null_convert;
652
2.65k
    } else if (IsExtRGB(cinfo->in_color_space))
653
2.65k
      cconvert->pub._color_convert = rgb_rgb_convert;
654
0
    else
655
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
656
3.31k
    break;
657
658
15.7k
  case JCS_YCbCr:
659
15.7k
#ifdef C_LOSSLESS_SUPPORTED
660
15.7k
    if (cinfo->master->lossless &&
661
15.7k
        cinfo->in_color_space != cinfo->jpeg_color_space)
662
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
663
15.7k
#endif
664
15.7k
    if (cinfo->num_components != 3)
665
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
666
15.7k
    if (IsExtRGB(cinfo->in_color_space)) {
667
15.7k
#ifdef WITH_SIMD
668
15.7k
      if (jsimd_can_rgb_ycc())
669
15.7k
        cconvert->pub._color_convert = jsimd_rgb_ycc_convert;
670
0
      else
671
0
#endif
672
0
      {
673
0
        cconvert->pub.start_pass = rgb_ycc_start;
674
0
        cconvert->pub._color_convert = rgb_ycc_convert;
675
0
      }
676
15.7k
    } else if (cinfo->in_color_space == JCS_YCbCr) {
677
#if defined(WITH_SIMD) && defined(__mips__)
678
      if (jsimd_c_can_null_convert())
679
        cconvert->pub._color_convert = jsimd_c_null_convert;
680
      else
681
#endif
682
0
        cconvert->pub._color_convert = null_convert;
683
0
    } else
684
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
685
15.7k
    break;
686
687
663
  case JCS_CMYK:
688
663
#ifdef C_LOSSLESS_SUPPORTED
689
663
    if (cinfo->master->lossless &&
690
663
        cinfo->in_color_space != cinfo->jpeg_color_space)
691
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
692
663
#endif
693
663
    if (cinfo->num_components != 4)
694
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
695
663
    if (cinfo->in_color_space == JCS_CMYK) {
696
#if defined(WITH_SIMD) && defined(__mips__)
697
      if (jsimd_c_can_null_convert())
698
        cconvert->pub._color_convert = jsimd_c_null_convert;
699
      else
700
#endif
701
663
        cconvert->pub._color_convert = null_convert;
702
663
    } else
703
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
704
663
    break;
705
706
2.00k
  case JCS_YCCK:
707
2.00k
#ifdef C_LOSSLESS_SUPPORTED
708
2.00k
    if (cinfo->master->lossless &&
709
2.00k
        cinfo->in_color_space != cinfo->jpeg_color_space)
710
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
711
2.00k
#endif
712
2.00k
    if (cinfo->num_components != 4)
713
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
714
2.00k
    if (cinfo->in_color_space == JCS_CMYK) {
715
2.00k
      cconvert->pub.start_pass = rgb_ycc_start;
716
2.00k
      cconvert->pub._color_convert = cmyk_ycck_convert;
717
2.00k
    } else if (cinfo->in_color_space == JCS_YCCK) {
718
#if defined(WITH_SIMD) && defined(__mips__)
719
      if (jsimd_c_can_null_convert())
720
        cconvert->pub._color_convert = jsimd_c_null_convert;
721
      else
722
#endif
723
0
        cconvert->pub._color_convert = null_convert;
724
0
    } else
725
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
726
2.00k
    break;
727
728
0
  default:                      /* allow null conversion of JCS_UNKNOWN */
729
0
    if (cinfo->jpeg_color_space != cinfo->in_color_space ||
730
0
        cinfo->num_components != cinfo->input_components)
731
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
732
#if defined(WITH_SIMD) && defined(__mips__)
733
    if (jsimd_c_can_null_convert())
734
      cconvert->pub._color_convert = jsimd_c_null_convert;
735
    else
736
#endif
737
0
      cconvert->pub._color_convert = null_convert;
738
0
    break;
739
29.4k
  }
740
29.4k
}
j12init_color_converter
Line
Count
Source
552
26.7k
{
553
26.7k
  my_cconvert_ptr cconvert;
554
555
26.7k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
556
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
557
558
26.7k
  cconvert = (my_cconvert_ptr)
559
26.7k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
560
26.7k
                                sizeof(my_color_converter));
561
26.7k
  cinfo->cconvert = (struct jpeg_color_converter *)cconvert;
562
  /* set start_pass to null method until we find out differently */
563
26.7k
  cconvert->pub.start_pass = null_method;
564
565
  /* Make sure input_components agrees with in_color_space */
566
26.7k
  switch (cinfo->in_color_space) {
567
3.22k
  case JCS_GRAYSCALE:
568
3.22k
    if (cinfo->input_components != 1)
569
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
570
3.22k
    break;
571
572
0
  case JCS_RGB:
573
3.91k
  case JCS_EXT_RGB:
574
7.83k
  case JCS_EXT_RGBX:
575
11.7k
  case JCS_EXT_BGR:
576
11.7k
  case JCS_EXT_BGRX:
577
11.7k
  case JCS_EXT_XBGR:
578
15.6k
  case JCS_EXT_XRGB:
579
15.6k
  case JCS_EXT_RGBA:
580
19.5k
  case JCS_EXT_BGRA:
581
19.5k
  case JCS_EXT_ABGR:
582
19.5k
  case JCS_EXT_ARGB:
583
19.5k
    if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
584
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
585
19.5k
    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.91k
  case JCS_CMYK:
593
3.91k
  case JCS_YCCK:
594
3.91k
    if (cinfo->input_components != 4)
595
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
596
3.91k
    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
26.7k
  }
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
26.7k
  switch (cinfo->jpeg_color_space) {
609
7.13k
  case JCS_GRAYSCALE:
610
7.13k
#ifdef C_LOSSLESS_SUPPORTED
611
7.13k
    if (cinfo->master->lossless &&
612
7.13k
        cinfo->in_color_space != cinfo->jpeg_color_space)
613
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
614
7.13k
#endif
615
7.13k
    if (cinfo->num_components != 1)
616
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
617
7.13k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
618
3.22k
      cconvert->pub._color_convert = grayscale_convert;
619
3.91k
    else if (IsExtRGB(cinfo->in_color_space)) {
620
#ifdef WITH_SIMD
621
      if (jsimd_can_rgb_gray())
622
        cconvert->pub._color_convert = jsimd_rgb_gray_convert;
623
      else
624
#endif
625
3.91k
      {
626
3.91k
        cconvert->pub.start_pass = rgb_ycc_start;
627
3.91k
        cconvert->pub._color_convert = rgb_gray_convert;
628
3.91k
      }
629
3.91k
    } else if (cinfo->in_color_space == JCS_YCbCr)
630
0
      cconvert->pub._color_convert = grayscale_convert;
631
0
    else
632
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
633
7.13k
    break;
634
635
0
  case JCS_RGB:
636
0
#ifdef C_LOSSLESS_SUPPORTED
637
0
    if (cinfo->master->lossless && !IsExtRGB(cinfo->in_color_space))
638
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
639
0
#endif
640
0
    if (cinfo->num_components != 3)
641
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
642
0
    if (rgb_red[cinfo->in_color_space] == 0 &&
643
0
        rgb_green[cinfo->in_color_space] == 1 &&
644
0
        rgb_blue[cinfo->in_color_space] == 2 &&
645
0
        rgb_pixelsize[cinfo->in_color_space] == 3) {
646
#if defined(WITH_SIMD) && defined(__mips__)
647
      if (jsimd_c_can_null_convert())
648
        cconvert->pub._color_convert = jsimd_c_null_convert;
649
      else
650
#endif
651
0
        cconvert->pub._color_convert = null_convert;
652
0
    } else if (IsExtRGB(cinfo->in_color_space))
653
0
      cconvert->pub._color_convert = rgb_rgb_convert;
654
0
    else
655
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
656
0
    break;
657
658
15.6k
  case JCS_YCbCr:
659
15.6k
#ifdef C_LOSSLESS_SUPPORTED
660
15.6k
    if (cinfo->master->lossless &&
661
15.6k
        cinfo->in_color_space != cinfo->jpeg_color_space)
662
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
663
15.6k
#endif
664
15.6k
    if (cinfo->num_components != 3)
665
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
666
15.6k
    if (IsExtRGB(cinfo->in_color_space)) {
667
#ifdef WITH_SIMD
668
      if (jsimd_can_rgb_ycc())
669
        cconvert->pub._color_convert = jsimd_rgb_ycc_convert;
670
      else
671
#endif
672
15.6k
      {
673
15.6k
        cconvert->pub.start_pass = rgb_ycc_start;
674
15.6k
        cconvert->pub._color_convert = rgb_ycc_convert;
675
15.6k
      }
676
15.6k
    } else if (cinfo->in_color_space == JCS_YCbCr) {
677
#if defined(WITH_SIMD) && defined(__mips__)
678
      if (jsimd_c_can_null_convert())
679
        cconvert->pub._color_convert = jsimd_c_null_convert;
680
      else
681
#endif
682
0
        cconvert->pub._color_convert = null_convert;
683
0
    } else
684
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
685
15.6k
    break;
686
687
0
  case JCS_CMYK:
688
0
#ifdef C_LOSSLESS_SUPPORTED
689
0
    if (cinfo->master->lossless &&
690
0
        cinfo->in_color_space != cinfo->jpeg_color_space)
691
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
692
0
#endif
693
0
    if (cinfo->num_components != 4)
694
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
695
0
    if (cinfo->in_color_space == JCS_CMYK) {
696
#if defined(WITH_SIMD) && defined(__mips__)
697
      if (jsimd_c_can_null_convert())
698
        cconvert->pub._color_convert = jsimd_c_null_convert;
699
      else
700
#endif
701
0
        cconvert->pub._color_convert = null_convert;
702
0
    } else
703
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
704
0
    break;
705
706
3.91k
  case JCS_YCCK:
707
3.91k
#ifdef C_LOSSLESS_SUPPORTED
708
3.91k
    if (cinfo->master->lossless &&
709
3.91k
        cinfo->in_color_space != cinfo->jpeg_color_space)
710
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
711
3.91k
#endif
712
3.91k
    if (cinfo->num_components != 4)
713
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
714
3.91k
    if (cinfo->in_color_space == JCS_CMYK) {
715
3.91k
      cconvert->pub.start_pass = rgb_ycc_start;
716
3.91k
      cconvert->pub._color_convert = cmyk_ycck_convert;
717
3.91k
    } else if (cinfo->in_color_space == JCS_YCCK) {
718
#if defined(WITH_SIMD) && defined(__mips__)
719
      if (jsimd_c_can_null_convert())
720
        cconvert->pub._color_convert = jsimd_c_null_convert;
721
      else
722
#endif
723
0
        cconvert->pub._color_convert = null_convert;
724
0
    } else
725
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
726
3.91k
    break;
727
728
0
  default:                      /* allow null conversion of JCS_UNKNOWN */
729
0
    if (cinfo->jpeg_color_space != cinfo->in_color_space ||
730
0
        cinfo->num_components != cinfo->input_components)
731
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
732
#if defined(WITH_SIMD) && defined(__mips__)
733
    if (jsimd_c_can_null_convert())
734
      cconvert->pub._color_convert = jsimd_c_null_convert;
735
    else
736
#endif
737
0
      cconvert->pub._color_convert = null_convert;
738
0
    break;
739
26.7k
  }
740
26.7k
}
j16init_color_converter
Line
Count
Source
552
4.56k
{
553
4.56k
  my_cconvert_ptr cconvert;
554
555
4.56k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
556
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
557
558
4.56k
  cconvert = (my_cconvert_ptr)
559
4.56k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
560
4.56k
                                sizeof(my_color_converter));
561
4.56k
  cinfo->cconvert = (struct jpeg_color_converter *)cconvert;
562
  /* set start_pass to null method until we find out differently */
563
4.56k
  cconvert->pub.start_pass = null_method;
564
565
  /* Make sure input_components agrees with in_color_space */
566
4.56k
  switch (cinfo->in_color_space) {
567
576
  case JCS_GRAYSCALE:
568
576
    if (cinfo->input_components != 1)
569
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
570
576
    break;
571
572
0
  case JCS_RGB:
573
664
  case JCS_EXT_RGB:
574
1.32k
  case JCS_EXT_RGBX:
575
1.99k
  case JCS_EXT_BGR:
576
1.99k
  case JCS_EXT_BGRX:
577
1.99k
  case JCS_EXT_XBGR:
578
2.65k
  case JCS_EXT_XRGB:
579
2.65k
  case JCS_EXT_RGBA:
580
3.32k
  case JCS_EXT_BGRA:
581
3.32k
  case JCS_EXT_ABGR:
582
3.32k
  case JCS_EXT_ARGB:
583
3.32k
    if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])
584
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
585
3.32k
    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
664
  case JCS_CMYK:
593
664
  case JCS_YCCK:
594
664
    if (cinfo->input_components != 4)
595
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
596
664
    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.56k
  }
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.56k
  switch (cinfo->jpeg_color_space) {
609
576
  case JCS_GRAYSCALE:
610
576
#ifdef C_LOSSLESS_SUPPORTED
611
576
    if (cinfo->master->lossless &&
612
576
        cinfo->in_color_space != cinfo->jpeg_color_space)
613
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
614
576
#endif
615
576
    if (cinfo->num_components != 1)
616
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
617
576
    if (cinfo->in_color_space == JCS_GRAYSCALE)
618
576
      cconvert->pub._color_convert = grayscale_convert;
619
0
    else if (IsExtRGB(cinfo->in_color_space)) {
620
#ifdef WITH_SIMD
621
      if (jsimd_can_rgb_gray())
622
        cconvert->pub._color_convert = jsimd_rgb_gray_convert;
623
      else
624
#endif
625
0
      {
626
0
        cconvert->pub.start_pass = rgb_ycc_start;
627
0
        cconvert->pub._color_convert = rgb_gray_convert;
628
0
      }
629
0
    } else if (cinfo->in_color_space == JCS_YCbCr)
630
0
      cconvert->pub._color_convert = grayscale_convert;
631
0
    else
632
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
633
576
    break;
634
635
3.32k
  case JCS_RGB:
636
3.32k
#ifdef C_LOSSLESS_SUPPORTED
637
3.32k
    if (cinfo->master->lossless && !IsExtRGB(cinfo->in_color_space))
638
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
639
3.32k
#endif
640
3.32k
    if (cinfo->num_components != 3)
641
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
642
3.32k
    if (rgb_red[cinfo->in_color_space] == 0 &&
643
3.32k
        rgb_green[cinfo->in_color_space] == 1 &&
644
3.32k
        rgb_blue[cinfo->in_color_space] == 2 &&
645
3.32k
        rgb_pixelsize[cinfo->in_color_space] == 3) {
646
#if defined(WITH_SIMD) && defined(__mips__)
647
      if (jsimd_c_can_null_convert())
648
        cconvert->pub._color_convert = jsimd_c_null_convert;
649
      else
650
#endif
651
664
        cconvert->pub._color_convert = null_convert;
652
2.65k
    } else if (IsExtRGB(cinfo->in_color_space))
653
2.65k
      cconvert->pub._color_convert = rgb_rgb_convert;
654
0
    else
655
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
656
3.32k
    break;
657
658
0
  case JCS_YCbCr:
659
0
#ifdef C_LOSSLESS_SUPPORTED
660
0
    if (cinfo->master->lossless &&
661
0
        cinfo->in_color_space != cinfo->jpeg_color_space)
662
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
663
0
#endif
664
0
    if (cinfo->num_components != 3)
665
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
666
0
    if (IsExtRGB(cinfo->in_color_space)) {
667
#ifdef WITH_SIMD
668
      if (jsimd_can_rgb_ycc())
669
        cconvert->pub._color_convert = jsimd_rgb_ycc_convert;
670
      else
671
#endif
672
0
      {
673
0
        cconvert->pub.start_pass = rgb_ycc_start;
674
0
        cconvert->pub._color_convert = rgb_ycc_convert;
675
0
      }
676
0
    } else if (cinfo->in_color_space == JCS_YCbCr) {
677
#if defined(WITH_SIMD) && defined(__mips__)
678
      if (jsimd_c_can_null_convert())
679
        cconvert->pub._color_convert = jsimd_c_null_convert;
680
      else
681
#endif
682
0
        cconvert->pub._color_convert = null_convert;
683
0
    } else
684
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
685
0
    break;
686
687
664
  case JCS_CMYK:
688
664
#ifdef C_LOSSLESS_SUPPORTED
689
664
    if (cinfo->master->lossless &&
690
664
        cinfo->in_color_space != cinfo->jpeg_color_space)
691
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
692
664
#endif
693
664
    if (cinfo->num_components != 4)
694
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
695
664
    if (cinfo->in_color_space == JCS_CMYK) {
696
#if defined(WITH_SIMD) && defined(__mips__)
697
      if (jsimd_c_can_null_convert())
698
        cconvert->pub._color_convert = jsimd_c_null_convert;
699
      else
700
#endif
701
664
        cconvert->pub._color_convert = null_convert;
702
664
    } else
703
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
704
664
    break;
705
706
0
  case JCS_YCCK:
707
0
#ifdef C_LOSSLESS_SUPPORTED
708
0
    if (cinfo->master->lossless &&
709
0
        cinfo->in_color_space != cinfo->jpeg_color_space)
710
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
711
0
#endif
712
0
    if (cinfo->num_components != 4)
713
0
      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
714
0
    if (cinfo->in_color_space == JCS_CMYK) {
715
0
      cconvert->pub.start_pass = rgb_ycc_start;
716
0
      cconvert->pub._color_convert = cmyk_ycck_convert;
717
0
    } else if (cinfo->in_color_space == JCS_YCCK) {
718
#if defined(WITH_SIMD) && defined(__mips__)
719
      if (jsimd_c_can_null_convert())
720
        cconvert->pub._color_convert = jsimd_c_null_convert;
721
      else
722
#endif
723
0
        cconvert->pub._color_convert = null_convert;
724
0
    } else
725
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
726
0
    break;
727
728
0
  default:                      /* allow null conversion of JCS_UNKNOWN */
729
0
    if (cinfo->jpeg_color_space != cinfo->in_color_space ||
730
0
        cinfo->num_components != cinfo->input_components)
731
0
      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
732
#if defined(WITH_SIMD) && defined(__mips__)
733
    if (jsimd_c_can_null_convert())
734
      cconvert->pub._color_convert = jsimd_c_null_convert;
735
    else
736
#endif
737
0
      cconvert->pub._color_convert = null_convert;
738
0
    break;
739
4.56k
  }
740
4.56k
}
741
742
#endif /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */