michael@0: /* michael@0: * jccolor.c michael@0: * michael@0: * This file was part of the Independent JPEG Group's software: michael@0: * Copyright (C) 1991-1996, Thomas G. Lane. michael@0: * libjpeg-turbo Modifications: michael@0: * Copyright 2009 Pierre Ossman for Cendio AB michael@0: * Copyright (C) 2009-2012, D. R. Commander. michael@0: * For conditions of distribution and use, see the accompanying README file. michael@0: * michael@0: * This file contains input colorspace conversion routines. michael@0: */ michael@0: michael@0: #define JPEG_INTERNALS michael@0: #include "jinclude.h" michael@0: #include "jpeglib.h" michael@0: #include "jsimd.h" michael@0: #include "config.h" michael@0: michael@0: michael@0: /* Private subobject */ michael@0: michael@0: typedef struct { michael@0: struct jpeg_color_converter pub; /* public fields */ michael@0: michael@0: /* Private state for RGB->YCC conversion */ michael@0: INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */ michael@0: } my_color_converter; michael@0: michael@0: typedef my_color_converter * my_cconvert_ptr; michael@0: michael@0: michael@0: /**************** RGB -> YCbCr conversion: most common case **************/ michael@0: michael@0: /* michael@0: * YCbCr is defined per CCIR 601-1, except that Cb and Cr are michael@0: * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5. michael@0: * The conversion equations to be implemented are therefore michael@0: * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B michael@0: * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE michael@0: * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE michael@0: * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.) michael@0: * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2, michael@0: * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and michael@0: * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0) michael@0: * were not represented exactly. Now we sacrifice exact representation of michael@0: * maximum red and maximum blue in order to get exact grayscales. michael@0: * michael@0: * To avoid floating-point arithmetic, we represent the fractional constants michael@0: * as integers scaled up by 2^16 (about 4 digits precision); we have to divide michael@0: * the products by 2^16, with appropriate rounding, to get the correct answer. michael@0: * michael@0: * For even more speed, we avoid doing any multiplications in the inner loop michael@0: * by precalculating the constants times R,G,B for all possible values. michael@0: * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table); michael@0: * for 12-bit samples it is still acceptable. It's not very reasonable for michael@0: * 16-bit samples, but if you want lossless storage you shouldn't be changing michael@0: * colorspace anyway. michael@0: * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included michael@0: * in the tables to save adding them separately in the inner loop. michael@0: */ michael@0: michael@0: #define SCALEBITS 16 /* speediest right-shift on some machines */ michael@0: #define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS) michael@0: #define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) michael@0: #define FIX(x) ((INT32) ((x) * (1L< Y section */ michael@0: #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */ michael@0: #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */ michael@0: #define R_CB_OFF (3*(MAXJSAMPLE+1)) michael@0: #define G_CB_OFF (4*(MAXJSAMPLE+1)) michael@0: #define B_CB_OFF (5*(MAXJSAMPLE+1)) michael@0: #define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */ michael@0: #define G_CR_OFF (6*(MAXJSAMPLE+1)) michael@0: #define B_CR_OFF (7*(MAXJSAMPLE+1)) michael@0: #define TABLE_SIZE (8*(MAXJSAMPLE+1)) michael@0: michael@0: michael@0: /* Include inline routines for colorspace extensions */ michael@0: michael@0: #include "jccolext.c" michael@0: #undef RGB_RED michael@0: #undef RGB_GREEN michael@0: #undef RGB_BLUE michael@0: #undef RGB_PIXELSIZE michael@0: michael@0: #define RGB_RED EXT_RGB_RED michael@0: #define RGB_GREEN EXT_RGB_GREEN michael@0: #define RGB_BLUE EXT_RGB_BLUE michael@0: #define RGB_PIXELSIZE EXT_RGB_PIXELSIZE michael@0: #define rgb_ycc_convert_internal extrgb_ycc_convert_internal michael@0: #define rgb_gray_convert_internal extrgb_gray_convert_internal michael@0: #define rgb_rgb_convert_internal extrgb_rgb_convert_internal michael@0: #include "jccolext.c" michael@0: #undef RGB_RED michael@0: #undef RGB_GREEN michael@0: #undef RGB_BLUE michael@0: #undef RGB_PIXELSIZE michael@0: #undef rgb_ycc_convert_internal michael@0: #undef rgb_gray_convert_internal michael@0: #undef rgb_rgb_convert_internal michael@0: michael@0: #define RGB_RED EXT_RGBX_RED michael@0: #define RGB_GREEN EXT_RGBX_GREEN michael@0: #define RGB_BLUE EXT_RGBX_BLUE michael@0: #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE michael@0: #define rgb_ycc_convert_internal extrgbx_ycc_convert_internal michael@0: #define rgb_gray_convert_internal extrgbx_gray_convert_internal michael@0: #define rgb_rgb_convert_internal extrgbx_rgb_convert_internal michael@0: #include "jccolext.c" michael@0: #undef RGB_RED michael@0: #undef RGB_GREEN michael@0: #undef RGB_BLUE michael@0: #undef RGB_PIXELSIZE michael@0: #undef rgb_ycc_convert_internal michael@0: #undef rgb_gray_convert_internal michael@0: #undef rgb_rgb_convert_internal michael@0: michael@0: #define RGB_RED EXT_BGR_RED michael@0: #define RGB_GREEN EXT_BGR_GREEN michael@0: #define RGB_BLUE EXT_BGR_BLUE michael@0: #define RGB_PIXELSIZE EXT_BGR_PIXELSIZE michael@0: #define rgb_ycc_convert_internal extbgr_ycc_convert_internal michael@0: #define rgb_gray_convert_internal extbgr_gray_convert_internal michael@0: #define rgb_rgb_convert_internal extbgr_rgb_convert_internal michael@0: #include "jccolext.c" michael@0: #undef RGB_RED michael@0: #undef RGB_GREEN michael@0: #undef RGB_BLUE michael@0: #undef RGB_PIXELSIZE michael@0: #undef rgb_ycc_convert_internal michael@0: #undef rgb_gray_convert_internal michael@0: #undef rgb_rgb_convert_internal michael@0: michael@0: #define RGB_RED EXT_BGRX_RED michael@0: #define RGB_GREEN EXT_BGRX_GREEN michael@0: #define RGB_BLUE EXT_BGRX_BLUE michael@0: #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE michael@0: #define rgb_ycc_convert_internal extbgrx_ycc_convert_internal michael@0: #define rgb_gray_convert_internal extbgrx_gray_convert_internal michael@0: #define rgb_rgb_convert_internal extbgrx_rgb_convert_internal michael@0: #include "jccolext.c" michael@0: #undef RGB_RED michael@0: #undef RGB_GREEN michael@0: #undef RGB_BLUE michael@0: #undef RGB_PIXELSIZE michael@0: #undef rgb_ycc_convert_internal michael@0: #undef rgb_gray_convert_internal michael@0: #undef rgb_rgb_convert_internal michael@0: michael@0: #define RGB_RED EXT_XBGR_RED michael@0: #define RGB_GREEN EXT_XBGR_GREEN michael@0: #define RGB_BLUE EXT_XBGR_BLUE michael@0: #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE michael@0: #define rgb_ycc_convert_internal extxbgr_ycc_convert_internal michael@0: #define rgb_gray_convert_internal extxbgr_gray_convert_internal michael@0: #define rgb_rgb_convert_internal extxbgr_rgb_convert_internal michael@0: #include "jccolext.c" michael@0: #undef RGB_RED michael@0: #undef RGB_GREEN michael@0: #undef RGB_BLUE michael@0: #undef RGB_PIXELSIZE michael@0: #undef rgb_ycc_convert_internal michael@0: #undef rgb_gray_convert_internal michael@0: #undef rgb_rgb_convert_internal michael@0: michael@0: #define RGB_RED EXT_XRGB_RED michael@0: #define RGB_GREEN EXT_XRGB_GREEN michael@0: #define RGB_BLUE EXT_XRGB_BLUE michael@0: #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE michael@0: #define rgb_ycc_convert_internal extxrgb_ycc_convert_internal michael@0: #define rgb_gray_convert_internal extxrgb_gray_convert_internal michael@0: #define rgb_rgb_convert_internal extxrgb_rgb_convert_internal michael@0: #include "jccolext.c" michael@0: #undef RGB_RED michael@0: #undef RGB_GREEN michael@0: #undef RGB_BLUE michael@0: #undef RGB_PIXELSIZE michael@0: #undef rgb_ycc_convert_internal michael@0: #undef rgb_gray_convert_internal michael@0: #undef rgb_rgb_convert_internal michael@0: michael@0: michael@0: /* michael@0: * Initialize for RGB->YCC colorspace conversion. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: rgb_ycc_start (j_compress_ptr cinfo) michael@0: { michael@0: my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; michael@0: INT32 * rgb_ycc_tab; michael@0: INT32 i; michael@0: michael@0: /* Allocate and fill in the conversion tables. */ michael@0: cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: (TABLE_SIZE * SIZEOF(INT32))); michael@0: michael@0: for (i = 0; i <= MAXJSAMPLE; i++) { michael@0: rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i; michael@0: rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i; michael@0: rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF; michael@0: rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i; michael@0: rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i; michael@0: /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr. michael@0: * This ensures that the maximum output will round to MAXJSAMPLE michael@0: * not MAXJSAMPLE+1, and thus that we don't have to range-limit. michael@0: */ michael@0: rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1; michael@0: /* B=>Cb and R=>Cr tables are the same michael@0: rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1; michael@0: */ michael@0: rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i; michael@0: rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Convert some rows of samples to the JPEG colorspace. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: rgb_ycc_convert (j_compress_ptr cinfo, michael@0: JSAMPARRAY input_buf, JSAMPIMAGE output_buf, michael@0: JDIMENSION output_row, int num_rows) michael@0: { michael@0: switch (cinfo->in_color_space) { michael@0: case JCS_EXT_RGB: michael@0: extrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_RGBX: michael@0: case JCS_EXT_RGBA: michael@0: extrgbx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_BGR: michael@0: extbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_BGRX: michael@0: case JCS_EXT_BGRA: michael@0: extbgrx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_XBGR: michael@0: case JCS_EXT_ABGR: michael@0: extxbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_XRGB: michael@0: case JCS_EXT_ARGB: michael@0: extxrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: default: michael@0: rgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: michael@0: /**************** Cases other than RGB -> YCbCr **************/ michael@0: michael@0: michael@0: /* michael@0: * Convert some rows of samples to the JPEG colorspace. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: rgb_gray_convert (j_compress_ptr cinfo, michael@0: JSAMPARRAY input_buf, JSAMPIMAGE output_buf, michael@0: JDIMENSION output_row, int num_rows) michael@0: { michael@0: switch (cinfo->in_color_space) { michael@0: case JCS_EXT_RGB: michael@0: extrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_RGBX: michael@0: case JCS_EXT_RGBA: michael@0: extrgbx_gray_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_BGR: michael@0: extbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_BGRX: michael@0: case JCS_EXT_BGRA: michael@0: extbgrx_gray_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_XBGR: michael@0: case JCS_EXT_ABGR: michael@0: extxbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_XRGB: michael@0: case JCS_EXT_ARGB: michael@0: extxrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: default: michael@0: rgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Extended RGB to plain RGB conversion michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: rgb_rgb_convert (j_compress_ptr cinfo, michael@0: JSAMPARRAY input_buf, JSAMPIMAGE output_buf, michael@0: JDIMENSION output_row, int num_rows) michael@0: { michael@0: switch (cinfo->in_color_space) { michael@0: case JCS_EXT_RGB: michael@0: extrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_RGBX: michael@0: case JCS_EXT_RGBA: michael@0: extrgbx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_BGR: michael@0: extbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_BGRX: michael@0: case JCS_EXT_BGRA: michael@0: extbgrx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_XBGR: michael@0: case JCS_EXT_ABGR: michael@0: extxbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_XRGB: michael@0: case JCS_EXT_ARGB: michael@0: extxrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: default: michael@0: rgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, michael@0: num_rows); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Convert some rows of samples to the JPEG colorspace. michael@0: * This version handles Adobe-style CMYK->YCCK conversion, michael@0: * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same michael@0: * conversion as above, while passing K (black) unchanged. michael@0: * We assume rgb_ycc_start has been called. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: cmyk_ycck_convert (j_compress_ptr cinfo, michael@0: JSAMPARRAY input_buf, JSAMPIMAGE output_buf, michael@0: JDIMENSION output_row, int num_rows) michael@0: { michael@0: my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; michael@0: register int r, g, b; michael@0: register INT32 * ctab = cconvert->rgb_ycc_tab; michael@0: register JSAMPROW inptr; michael@0: register JSAMPROW outptr0, outptr1, outptr2, outptr3; michael@0: register JDIMENSION col; michael@0: JDIMENSION num_cols = cinfo->image_width; michael@0: michael@0: while (--num_rows >= 0) { michael@0: inptr = *input_buf++; michael@0: outptr0 = output_buf[0][output_row]; michael@0: outptr1 = output_buf[1][output_row]; michael@0: outptr2 = output_buf[2][output_row]; michael@0: outptr3 = output_buf[3][output_row]; michael@0: output_row++; michael@0: for (col = 0; col < num_cols; col++) { michael@0: r = MAXJSAMPLE - GETJSAMPLE(inptr[0]); michael@0: g = MAXJSAMPLE - GETJSAMPLE(inptr[1]); michael@0: b = MAXJSAMPLE - GETJSAMPLE(inptr[2]); michael@0: /* K passes through as-is */ michael@0: outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */ michael@0: inptr += 4; michael@0: /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations michael@0: * must be too; we do not need an explicit range-limiting operation. michael@0: * Hence the value being shifted is never negative, and we don't michael@0: * need the general RIGHT_SHIFT macro. michael@0: */ michael@0: /* Y */ michael@0: outptr0[col] = (JSAMPLE) michael@0: ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) michael@0: >> SCALEBITS); michael@0: /* Cb */ michael@0: outptr1[col] = (JSAMPLE) michael@0: ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) michael@0: >> SCALEBITS); michael@0: /* Cr */ michael@0: outptr2[col] = (JSAMPLE) michael@0: ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) michael@0: >> SCALEBITS); michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Convert some rows of samples to the JPEG colorspace. michael@0: * This version handles grayscale output with no conversion. michael@0: * The source can be either plain grayscale or YCbCr (since Y == gray). michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: grayscale_convert (j_compress_ptr cinfo, michael@0: JSAMPARRAY input_buf, JSAMPIMAGE output_buf, michael@0: JDIMENSION output_row, int num_rows) michael@0: { michael@0: register JSAMPROW inptr; michael@0: register JSAMPROW outptr; michael@0: register JDIMENSION col; michael@0: JDIMENSION num_cols = cinfo->image_width; michael@0: int instride = cinfo->input_components; michael@0: michael@0: while (--num_rows >= 0) { michael@0: inptr = *input_buf++; michael@0: outptr = output_buf[0][output_row]; michael@0: output_row++; michael@0: for (col = 0; col < num_cols; col++) { michael@0: outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */ michael@0: inptr += instride; michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Convert some rows of samples to the JPEG colorspace. michael@0: * This version handles multi-component colorspaces without conversion. michael@0: * We assume input_components == num_components. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: null_convert (j_compress_ptr cinfo, michael@0: JSAMPARRAY input_buf, JSAMPIMAGE output_buf, michael@0: JDIMENSION output_row, int num_rows) michael@0: { michael@0: register JSAMPROW inptr; michael@0: register JSAMPROW outptr; michael@0: register JDIMENSION col; michael@0: register int ci; michael@0: int nc = cinfo->num_components; michael@0: JDIMENSION num_cols = cinfo->image_width; michael@0: michael@0: while (--num_rows >= 0) { michael@0: /* It seems fastest to make a separate pass for each component. */ michael@0: for (ci = 0; ci < nc; ci++) { michael@0: inptr = *input_buf; michael@0: outptr = output_buf[ci][output_row]; michael@0: for (col = 0; col < num_cols; col++) { michael@0: outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */ michael@0: inptr += nc; michael@0: } michael@0: } michael@0: input_buf++; michael@0: output_row++; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Empty method for start_pass. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: null_method (j_compress_ptr cinfo) michael@0: { michael@0: /* no work needed */ michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Module initialization routine for input colorspace conversion. michael@0: */ michael@0: michael@0: GLOBAL(void) michael@0: jinit_color_converter (j_compress_ptr cinfo) michael@0: { michael@0: my_cconvert_ptr cconvert; michael@0: michael@0: cconvert = (my_cconvert_ptr) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: SIZEOF(my_color_converter)); michael@0: cinfo->cconvert = (struct jpeg_color_converter *) cconvert; michael@0: /* set start_pass to null method until we find out differently */ michael@0: cconvert->pub.start_pass = null_method; michael@0: michael@0: /* Make sure input_components agrees with in_color_space */ michael@0: switch (cinfo->in_color_space) { michael@0: case JCS_GRAYSCALE: michael@0: if (cinfo->input_components != 1) michael@0: ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); michael@0: break; michael@0: michael@0: case JCS_RGB: michael@0: case JCS_EXT_RGB: michael@0: case JCS_EXT_RGBX: michael@0: case JCS_EXT_BGR: michael@0: case JCS_EXT_BGRX: michael@0: case JCS_EXT_XBGR: michael@0: case JCS_EXT_XRGB: michael@0: case JCS_EXT_RGBA: michael@0: case JCS_EXT_BGRA: michael@0: case JCS_EXT_ABGR: michael@0: case JCS_EXT_ARGB: michael@0: if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space]) michael@0: ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); michael@0: break; michael@0: michael@0: case JCS_YCbCr: michael@0: if (cinfo->input_components != 3) michael@0: ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); michael@0: break; michael@0: michael@0: case JCS_CMYK: michael@0: case JCS_YCCK: michael@0: if (cinfo->input_components != 4) michael@0: ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); michael@0: break; michael@0: michael@0: default: /* JCS_UNKNOWN can be anything */ michael@0: if (cinfo->input_components < 1) michael@0: ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); michael@0: break; michael@0: } michael@0: michael@0: /* Check num_components, set conversion method based on requested space */ michael@0: switch (cinfo->jpeg_color_space) { michael@0: case JCS_GRAYSCALE: michael@0: if (cinfo->num_components != 1) michael@0: ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); michael@0: if (cinfo->in_color_space == JCS_GRAYSCALE) michael@0: cconvert->pub.color_convert = grayscale_convert; michael@0: else if (cinfo->in_color_space == JCS_RGB || michael@0: cinfo->in_color_space == JCS_EXT_RGB || michael@0: cinfo->in_color_space == JCS_EXT_RGBX || michael@0: cinfo->in_color_space == JCS_EXT_BGR || michael@0: cinfo->in_color_space == JCS_EXT_BGRX || michael@0: cinfo->in_color_space == JCS_EXT_XBGR || michael@0: cinfo->in_color_space == JCS_EXT_XRGB || michael@0: cinfo->in_color_space == JCS_EXT_RGBA || michael@0: cinfo->in_color_space == JCS_EXT_BGRA || michael@0: cinfo->in_color_space == JCS_EXT_ABGR || michael@0: cinfo->in_color_space == JCS_EXT_ARGB) { michael@0: if (jsimd_can_rgb_gray()) michael@0: cconvert->pub.color_convert = jsimd_rgb_gray_convert; michael@0: else { michael@0: cconvert->pub.start_pass = rgb_ycc_start; michael@0: cconvert->pub.color_convert = rgb_gray_convert; michael@0: } michael@0: } else if (cinfo->in_color_space == JCS_YCbCr) michael@0: cconvert->pub.color_convert = grayscale_convert; michael@0: else michael@0: ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); michael@0: break; michael@0: michael@0: case JCS_RGB: michael@0: if (cinfo->num_components != 3) michael@0: ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); michael@0: if (rgb_red[cinfo->in_color_space] == 0 && michael@0: rgb_green[cinfo->in_color_space] == 1 && michael@0: rgb_blue[cinfo->in_color_space] == 2 && michael@0: rgb_pixelsize[cinfo->in_color_space] == 3) michael@0: cconvert->pub.color_convert = null_convert; michael@0: else if (cinfo->in_color_space == JCS_RGB || michael@0: cinfo->in_color_space == JCS_EXT_RGB || michael@0: cinfo->in_color_space == JCS_EXT_RGBX || michael@0: cinfo->in_color_space == JCS_EXT_BGR || michael@0: cinfo->in_color_space == JCS_EXT_BGRX || michael@0: cinfo->in_color_space == JCS_EXT_XBGR || michael@0: cinfo->in_color_space == JCS_EXT_XRGB || michael@0: cinfo->in_color_space == JCS_EXT_RGBA || michael@0: cinfo->in_color_space == JCS_EXT_BGRA || michael@0: cinfo->in_color_space == JCS_EXT_ABGR || michael@0: cinfo->in_color_space == JCS_EXT_ARGB) michael@0: cconvert->pub.color_convert = rgb_rgb_convert; michael@0: else michael@0: ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); michael@0: break; michael@0: michael@0: case JCS_YCbCr: michael@0: if (cinfo->num_components != 3) michael@0: ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); michael@0: if (cinfo->in_color_space == JCS_RGB || michael@0: cinfo->in_color_space == JCS_EXT_RGB || michael@0: cinfo->in_color_space == JCS_EXT_RGBX || michael@0: cinfo->in_color_space == JCS_EXT_BGR || michael@0: cinfo->in_color_space == JCS_EXT_BGRX || michael@0: cinfo->in_color_space == JCS_EXT_XBGR || michael@0: cinfo->in_color_space == JCS_EXT_XRGB || michael@0: cinfo->in_color_space == JCS_EXT_RGBA || michael@0: cinfo->in_color_space == JCS_EXT_BGRA || michael@0: cinfo->in_color_space == JCS_EXT_ABGR || michael@0: cinfo->in_color_space == JCS_EXT_ARGB) { michael@0: if (jsimd_can_rgb_ycc()) michael@0: cconvert->pub.color_convert = jsimd_rgb_ycc_convert; michael@0: else { michael@0: cconvert->pub.start_pass = rgb_ycc_start; michael@0: cconvert->pub.color_convert = rgb_ycc_convert; michael@0: } michael@0: } else if (cinfo->in_color_space == JCS_YCbCr) michael@0: cconvert->pub.color_convert = null_convert; michael@0: else michael@0: ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); michael@0: break; michael@0: michael@0: case JCS_CMYK: michael@0: if (cinfo->num_components != 4) michael@0: ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); michael@0: if (cinfo->in_color_space == JCS_CMYK) michael@0: cconvert->pub.color_convert = null_convert; michael@0: else michael@0: ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); michael@0: break; michael@0: michael@0: case JCS_YCCK: michael@0: if (cinfo->num_components != 4) michael@0: ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); michael@0: if (cinfo->in_color_space == JCS_CMYK) { michael@0: cconvert->pub.start_pass = rgb_ycc_start; michael@0: cconvert->pub.color_convert = cmyk_ycck_convert; michael@0: } else if (cinfo->in_color_space == JCS_YCCK) michael@0: cconvert->pub.color_convert = null_convert; michael@0: else michael@0: ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); michael@0: break; michael@0: michael@0: default: /* allow null conversion of JCS_UNKNOWN */ michael@0: if (cinfo->jpeg_color_space != cinfo->in_color_space || michael@0: cinfo->num_components != cinfo->input_components) michael@0: ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); michael@0: cconvert->pub.color_convert = null_convert; michael@0: break; michael@0: } michael@0: }