Path: blob/master/thirdparty/libjpeg-turbo/src/jccolor.c
9904 views
/*1* jccolor.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1991-1996, Thomas G. Lane.5* libjpeg-turbo Modifications:6* Copyright 2009 Pierre Ossman <[email protected]> for Cendio AB7* Copyright (C) 2009-2012, 2015, 2022, 2024, D. R. Commander.8* Copyright (C) 2014, MIPS Technologies, Inc., California.9* For conditions of distribution and use, see the accompanying README.ijg10* file.11*12* This file contains input colorspace conversion routines.13*/1415#define JPEG_INTERNALS16#include "jinclude.h"17#include "jpeglib.h"18#include "jsimd.h"19#include "jsamplecomp.h"202122#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)2324/* Private subobject */2526typedef struct {27struct jpeg_color_converter pub; /* public fields */2829#if BITS_IN_JSAMPLE != 1630/* Private state for RGB->YCC conversion */31JLONG *rgb_ycc_tab; /* => table for RGB to YCbCr conversion */32#endif33} my_color_converter;3435typedef my_color_converter *my_cconvert_ptr;363738/**************** RGB -> YCbCr conversion: most common case **************/3940/*41* YCbCr is defined per CCIR 601-1, except that Cb and Cr are42* normalized to the range 0.._MAXJSAMPLE rather than -0.5 .. 0.5.43* The conversion equations to be implemented are therefore44* Y = 0.29900 * R + 0.58700 * G + 0.11400 * B45* Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + _CENTERJSAMPLE46* Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + _CENTERJSAMPLE47* (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)48* Note: older versions of the IJG code used a zero offset of _MAXJSAMPLE/2,49* rather than _CENTERJSAMPLE, for Cb and Cr. This gave equal positive and50* negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)51* were not represented exactly. Now we sacrifice exact representation of52* maximum red and maximum blue in order to get exact grayscales.53*54* To avoid floating-point arithmetic, we represent the fractional constants55* as integers scaled up by 2^16 (about 4 digits precision); we have to divide56* the products by 2^16, with appropriate rounding, to get the correct answer.57*58* For even more speed, we avoid doing any multiplications in the inner loop59* by precalculating the constants times R,G,B for all possible values.60* For 8-bit samples this is very reasonable (only 256 entries per table);61* for 12-bit samples it is still acceptable. It's not very reasonable for62* 16-bit samples, but if you want lossless storage you shouldn't be changing63* colorspace anyway.64* The _CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included65* in the tables to save adding them separately in the inner loop.66*/6768#define SCALEBITS 16 /* speediest right-shift on some machines */69#define CBCR_OFFSET ((JLONG)_CENTERJSAMPLE << SCALEBITS)70#define ONE_HALF ((JLONG)1 << (SCALEBITS - 1))71#define FIX(x) ((JLONG)((x) * (1L << SCALEBITS) + 0.5))7273/* We allocate one big table and divide it up into eight parts, instead of74* doing eight alloc_small requests. This lets us use a single table base75* address, which can be held in a register in the inner loops on many76* machines (more than can hold all eight addresses, anyway).77*/7879#define R_Y_OFF 0 /* offset to R => Y section */80#define G_Y_OFF (1 * (_MAXJSAMPLE + 1)) /* offset to G => Y section */81#define B_Y_OFF (2 * (_MAXJSAMPLE + 1)) /* etc. */82#define R_CB_OFF (3 * (_MAXJSAMPLE + 1))83#define G_CB_OFF (4 * (_MAXJSAMPLE + 1))84#define B_CB_OFF (5 * (_MAXJSAMPLE + 1))85#define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */86#define G_CR_OFF (6 * (_MAXJSAMPLE + 1))87#define B_CR_OFF (7 * (_MAXJSAMPLE + 1))88#define TABLE_SIZE (8 * (_MAXJSAMPLE + 1))8990/* 12-bit samples use a 16-bit data type, so it is possible to pass91* out-of-range sample values (< 0 or > 4095) to jpeg_write_scanlines().92* Thus, we mask the incoming 12-bit samples to guard against overrunning93* or underrunning the conversion tables.94*/9596#if BITS_IN_JSAMPLE == 1297#define RANGE_LIMIT(value) ((value) & 0xFFF)98#else99#define RANGE_LIMIT(value) (value)100#endif101102103/* Include inline routines for colorspace extensions */104105#include "jccolext.c"106#undef RGB_RED107#undef RGB_GREEN108#undef RGB_BLUE109#undef RGB_PIXELSIZE110111#define RGB_RED EXT_RGB_RED112#define RGB_GREEN EXT_RGB_GREEN113#define RGB_BLUE EXT_RGB_BLUE114#define RGB_PIXELSIZE EXT_RGB_PIXELSIZE115#define rgb_ycc_convert_internal extrgb_ycc_convert_internal116#define rgb_gray_convert_internal extrgb_gray_convert_internal117#define rgb_rgb_convert_internal extrgb_rgb_convert_internal118#include "jccolext.c"119#undef RGB_RED120#undef RGB_GREEN121#undef RGB_BLUE122#undef RGB_PIXELSIZE123#undef rgb_ycc_convert_internal124#undef rgb_gray_convert_internal125#undef rgb_rgb_convert_internal126127#define RGB_RED EXT_RGBX_RED128#define RGB_GREEN EXT_RGBX_GREEN129#define RGB_BLUE EXT_RGBX_BLUE130#define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE131#define rgb_ycc_convert_internal extrgbx_ycc_convert_internal132#define rgb_gray_convert_internal extrgbx_gray_convert_internal133#define rgb_rgb_convert_internal extrgbx_rgb_convert_internal134#include "jccolext.c"135#undef RGB_RED136#undef RGB_GREEN137#undef RGB_BLUE138#undef RGB_PIXELSIZE139#undef rgb_ycc_convert_internal140#undef rgb_gray_convert_internal141#undef rgb_rgb_convert_internal142143#define RGB_RED EXT_BGR_RED144#define RGB_GREEN EXT_BGR_GREEN145#define RGB_BLUE EXT_BGR_BLUE146#define RGB_PIXELSIZE EXT_BGR_PIXELSIZE147#define rgb_ycc_convert_internal extbgr_ycc_convert_internal148#define rgb_gray_convert_internal extbgr_gray_convert_internal149#define rgb_rgb_convert_internal extbgr_rgb_convert_internal150#include "jccolext.c"151#undef RGB_RED152#undef RGB_GREEN153#undef RGB_BLUE154#undef RGB_PIXELSIZE155#undef rgb_ycc_convert_internal156#undef rgb_gray_convert_internal157#undef rgb_rgb_convert_internal158159#define RGB_RED EXT_BGRX_RED160#define RGB_GREEN EXT_BGRX_GREEN161#define RGB_BLUE EXT_BGRX_BLUE162#define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE163#define rgb_ycc_convert_internal extbgrx_ycc_convert_internal164#define rgb_gray_convert_internal extbgrx_gray_convert_internal165#define rgb_rgb_convert_internal extbgrx_rgb_convert_internal166#include "jccolext.c"167#undef RGB_RED168#undef RGB_GREEN169#undef RGB_BLUE170#undef RGB_PIXELSIZE171#undef rgb_ycc_convert_internal172#undef rgb_gray_convert_internal173#undef rgb_rgb_convert_internal174175#define RGB_RED EXT_XBGR_RED176#define RGB_GREEN EXT_XBGR_GREEN177#define RGB_BLUE EXT_XBGR_BLUE178#define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE179#define rgb_ycc_convert_internal extxbgr_ycc_convert_internal180#define rgb_gray_convert_internal extxbgr_gray_convert_internal181#define rgb_rgb_convert_internal extxbgr_rgb_convert_internal182#include "jccolext.c"183#undef RGB_RED184#undef RGB_GREEN185#undef RGB_BLUE186#undef RGB_PIXELSIZE187#undef rgb_ycc_convert_internal188#undef rgb_gray_convert_internal189#undef rgb_rgb_convert_internal190191#define RGB_RED EXT_XRGB_RED192#define RGB_GREEN EXT_XRGB_GREEN193#define RGB_BLUE EXT_XRGB_BLUE194#define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE195#define rgb_ycc_convert_internal extxrgb_ycc_convert_internal196#define rgb_gray_convert_internal extxrgb_gray_convert_internal197#define rgb_rgb_convert_internal extxrgb_rgb_convert_internal198#include "jccolext.c"199#undef RGB_RED200#undef RGB_GREEN201#undef RGB_BLUE202#undef RGB_PIXELSIZE203#undef rgb_ycc_convert_internal204#undef rgb_gray_convert_internal205#undef rgb_rgb_convert_internal206207208/*209* Initialize for RGB->YCC colorspace conversion.210*/211212METHODDEF(void)213rgb_ycc_start(j_compress_ptr cinfo)214{215#if BITS_IN_JSAMPLE != 16216my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;217JLONG *rgb_ycc_tab;218JLONG i;219220/* Allocate and fill in the conversion tables. */221cconvert->rgb_ycc_tab = rgb_ycc_tab = (JLONG *)222(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,223(TABLE_SIZE * sizeof(JLONG)));224225for (i = 0; i <= _MAXJSAMPLE; i++) {226rgb_ycc_tab[i + R_Y_OFF] = FIX(0.29900) * i;227rgb_ycc_tab[i + G_Y_OFF] = FIX(0.58700) * i;228rgb_ycc_tab[i + B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;229rgb_ycc_tab[i + R_CB_OFF] = (-FIX(0.16874)) * i;230rgb_ycc_tab[i + G_CB_OFF] = (-FIX(0.33126)) * i;231/* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.232* This ensures that the maximum output will round to _MAXJSAMPLE233* not _MAXJSAMPLE+1, and thus that we don't have to range-limit.234*/235rgb_ycc_tab[i + B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF - 1;236/* B=>Cb and R=>Cr tables are the same237rgb_ycc_tab[i + R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF - 1;238*/239rgb_ycc_tab[i + G_CR_OFF] = (-FIX(0.41869)) * i;240rgb_ycc_tab[i + B_CR_OFF] = (-FIX(0.08131)) * i;241}242#else243ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);244#endif245}246247248/*249* Convert some rows of samples to the JPEG colorspace.250*/251252METHODDEF(void)253rgb_ycc_convert(j_compress_ptr cinfo, _JSAMPARRAY input_buf,254_JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)255{256switch (cinfo->in_color_space) {257case JCS_EXT_RGB:258extrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,259num_rows);260break;261case JCS_EXT_RGBX:262case JCS_EXT_RGBA:263extrgbx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,264num_rows);265break;266case JCS_EXT_BGR:267extbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,268num_rows);269break;270case JCS_EXT_BGRX:271case JCS_EXT_BGRA:272extbgrx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,273num_rows);274break;275case JCS_EXT_XBGR:276case JCS_EXT_ABGR:277extxbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,278num_rows);279break;280case JCS_EXT_XRGB:281case JCS_EXT_ARGB:282extxrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,283num_rows);284break;285default:286rgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row,287num_rows);288break;289}290}291292293/**************** Cases other than RGB -> YCbCr **************/294295296/*297* Convert some rows of samples to the JPEG colorspace.298*/299300METHODDEF(void)301rgb_gray_convert(j_compress_ptr cinfo, _JSAMPARRAY input_buf,302_JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)303{304switch (cinfo->in_color_space) {305case JCS_EXT_RGB:306extrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,307num_rows);308break;309case JCS_EXT_RGBX:310case JCS_EXT_RGBA:311extrgbx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,312num_rows);313break;314case JCS_EXT_BGR:315extbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,316num_rows);317break;318case JCS_EXT_BGRX:319case JCS_EXT_BGRA:320extbgrx_gray_convert_internal(cinfo, input_buf, output_buf, output_row,321num_rows);322break;323case JCS_EXT_XBGR:324case JCS_EXT_ABGR:325extxbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row,326num_rows);327break;328case JCS_EXT_XRGB:329case JCS_EXT_ARGB:330extxrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,331num_rows);332break;333default:334rgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row,335num_rows);336break;337}338}339340341/*342* Extended RGB to plain RGB conversion343*/344345METHODDEF(void)346rgb_rgb_convert(j_compress_ptr cinfo, _JSAMPARRAY input_buf,347_JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)348{349switch (cinfo->in_color_space) {350case JCS_EXT_RGB:351extrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,352num_rows);353break;354case JCS_EXT_RGBX:355case JCS_EXT_RGBA:356extrgbx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,357num_rows);358break;359case JCS_EXT_BGR:360extbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,361num_rows);362break;363case JCS_EXT_BGRX:364case JCS_EXT_BGRA:365extbgrx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,366num_rows);367break;368case JCS_EXT_XBGR:369case JCS_EXT_ABGR:370extxbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,371num_rows);372break;373case JCS_EXT_XRGB:374case JCS_EXT_ARGB:375extxrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,376num_rows);377break;378default:379rgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row,380num_rows);381break;382}383}384385386/*387* Convert some rows of samples to the JPEG colorspace.388* This version handles Adobe-style CMYK->YCCK conversion,389* where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same390* conversion as above, while passing K (black) unchanged.391* We assume rgb_ycc_start has been called.392*/393394METHODDEF(void)395cmyk_ycck_convert(j_compress_ptr cinfo, _JSAMPARRAY input_buf,396_JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)397{398#if BITS_IN_JSAMPLE != 16399my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;400register int r, g, b;401register JLONG *ctab = cconvert->rgb_ycc_tab;402register _JSAMPROW inptr;403register _JSAMPROW outptr0, outptr1, outptr2, outptr3;404register JDIMENSION col;405JDIMENSION num_cols = cinfo->image_width;406407while (--num_rows >= 0) {408inptr = *input_buf++;409outptr0 = output_buf[0][output_row];410outptr1 = output_buf[1][output_row];411outptr2 = output_buf[2][output_row];412outptr3 = output_buf[3][output_row];413output_row++;414for (col = 0; col < num_cols; col++) {415r = _MAXJSAMPLE - RANGE_LIMIT(inptr[0]);416g = _MAXJSAMPLE - RANGE_LIMIT(inptr[1]);417b = _MAXJSAMPLE - RANGE_LIMIT(inptr[2]);418/* K passes through as-is */419outptr3[col] = inptr[3];420inptr += 4;421/* If the inputs are 0.._MAXJSAMPLE, the outputs of these equations422* must be too; we do not need an explicit range-limiting operation.423* Hence the value being shifted is never negative, and we don't424* need the general RIGHT_SHIFT macro.425*/426/* Y */427outptr0[col] = (_JSAMPLE)((ctab[r + R_Y_OFF] + ctab[g + G_Y_OFF] +428ctab[b + B_Y_OFF]) >> SCALEBITS);429/* Cb */430outptr1[col] = (_JSAMPLE)((ctab[r + R_CB_OFF] + ctab[g + G_CB_OFF] +431ctab[b + B_CB_OFF]) >> SCALEBITS);432/* Cr */433outptr2[col] = (_JSAMPLE)((ctab[r + R_CR_OFF] + ctab[g + G_CR_OFF] +434ctab[b + B_CR_OFF]) >> SCALEBITS);435}436}437#else438ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);439#endif440}441442443/*444* Convert some rows of samples to the JPEG colorspace.445* This version handles grayscale output with no conversion.446* The source can be either plain grayscale or YCbCr (since Y == gray).447*/448449METHODDEF(void)450grayscale_convert(j_compress_ptr cinfo, _JSAMPARRAY input_buf,451_JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)452{453register _JSAMPROW inptr;454register _JSAMPROW outptr;455register JDIMENSION col;456JDIMENSION num_cols = cinfo->image_width;457int instride = cinfo->input_components;458459while (--num_rows >= 0) {460inptr = *input_buf++;461outptr = output_buf[0][output_row];462output_row++;463for (col = 0; col < num_cols; col++) {464outptr[col] = inptr[0];465inptr += instride;466}467}468}469470471/*472* Convert some rows of samples to the JPEG colorspace.473* This version handles multi-component colorspaces without conversion.474* We assume input_components == num_components.475*/476477METHODDEF(void)478null_convert(j_compress_ptr cinfo, _JSAMPARRAY input_buf,479_JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)480{481register _JSAMPROW inptr;482register _JSAMPROW outptr, outptr0, outptr1, outptr2, outptr3;483register JDIMENSION col;484register int ci;485int nc = cinfo->num_components;486JDIMENSION num_cols = cinfo->image_width;487488if (nc == 3) {489while (--num_rows >= 0) {490inptr = *input_buf++;491outptr0 = output_buf[0][output_row];492outptr1 = output_buf[1][output_row];493outptr2 = output_buf[2][output_row];494output_row++;495for (col = 0; col < num_cols; col++) {496outptr0[col] = *inptr++;497outptr1[col] = *inptr++;498outptr2[col] = *inptr++;499}500}501} else if (nc == 4) {502while (--num_rows >= 0) {503inptr = *input_buf++;504outptr0 = output_buf[0][output_row];505outptr1 = output_buf[1][output_row];506outptr2 = output_buf[2][output_row];507outptr3 = output_buf[3][output_row];508output_row++;509for (col = 0; col < num_cols; col++) {510outptr0[col] = *inptr++;511outptr1[col] = *inptr++;512outptr2[col] = *inptr++;513outptr3[col] = *inptr++;514}515}516} else {517while (--num_rows >= 0) {518/* It seems fastest to make a separate pass for each component. */519for (ci = 0; ci < nc; ci++) {520inptr = *input_buf;521outptr = output_buf[ci][output_row];522for (col = 0; col < num_cols; col++) {523outptr[col] = inptr[ci];524inptr += nc;525}526}527input_buf++;528output_row++;529}530}531}532533534/*535* Empty method for start_pass.536*/537538METHODDEF(void)539null_method(j_compress_ptr cinfo)540{541/* no work needed */542}543544545/*546* Module initialization routine for input colorspace conversion.547*/548549GLOBAL(void)550_jinit_color_converter(j_compress_ptr cinfo)551{552my_cconvert_ptr cconvert;553554#ifdef C_LOSSLESS_SUPPORTED555if (cinfo->master->lossless) {556#if BITS_IN_JSAMPLE == 8557if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)558#else559if (cinfo->data_precision > BITS_IN_JSAMPLE ||560cinfo->data_precision < BITS_IN_JSAMPLE - 3)561#endif562ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);563} else564#endif565{566if (cinfo->data_precision != BITS_IN_JSAMPLE)567ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);568}569570cconvert = (my_cconvert_ptr)571(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,572sizeof(my_color_converter));573cinfo->cconvert = (struct jpeg_color_converter *)cconvert;574/* set start_pass to null method until we find out differently */575cconvert->pub.start_pass = null_method;576577/* Make sure input_components agrees with in_color_space */578switch (cinfo->in_color_space) {579case JCS_GRAYSCALE:580if (cinfo->input_components != 1)581ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);582break;583584case JCS_RGB:585case JCS_EXT_RGB:586case JCS_EXT_RGBX:587case JCS_EXT_BGR:588case JCS_EXT_BGRX:589case JCS_EXT_XBGR:590case JCS_EXT_XRGB:591case JCS_EXT_RGBA:592case JCS_EXT_BGRA:593case JCS_EXT_ABGR:594case JCS_EXT_ARGB:595if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space])596ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);597break;598599case JCS_YCbCr:600if (cinfo->input_components != 3)601ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);602break;603604case JCS_CMYK:605case JCS_YCCK:606if (cinfo->input_components != 4)607ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);608break;609610default: /* JCS_UNKNOWN can be anything */611if (cinfo->input_components < 1)612ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);613break;614}615616/* Check num_components, set conversion method based on requested space.617* NOTE: We do not allow any lossy color conversion algorithms in lossless618* mode.619*/620switch (cinfo->jpeg_color_space) {621case JCS_GRAYSCALE:622#ifdef C_LOSSLESS_SUPPORTED623if (cinfo->master->lossless &&624cinfo->in_color_space != cinfo->jpeg_color_space)625ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);626#endif627if (cinfo->num_components != 1)628ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);629if (cinfo->in_color_space == JCS_GRAYSCALE)630cconvert->pub._color_convert = grayscale_convert;631else if (IsExtRGB(cinfo->in_color_space)) {632#ifdef WITH_SIMD633if (jsimd_can_rgb_gray())634cconvert->pub._color_convert = jsimd_rgb_gray_convert;635else636#endif637{638cconvert->pub.start_pass = rgb_ycc_start;639cconvert->pub._color_convert = rgb_gray_convert;640}641} else if (cinfo->in_color_space == JCS_YCbCr)642cconvert->pub._color_convert = grayscale_convert;643else644ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);645break;646647case JCS_RGB:648#ifdef C_LOSSLESS_SUPPORTED649if (cinfo->master->lossless && !IsExtRGB(cinfo->in_color_space))650ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);651#endif652if (cinfo->num_components != 3)653ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);654if (rgb_red[cinfo->in_color_space] == 0 &&655rgb_green[cinfo->in_color_space] == 1 &&656rgb_blue[cinfo->in_color_space] == 2 &&657rgb_pixelsize[cinfo->in_color_space] == 3) {658#if defined(WITH_SIMD) && defined(__mips__)659if (jsimd_c_can_null_convert())660cconvert->pub._color_convert = jsimd_c_null_convert;661else662#endif663cconvert->pub._color_convert = null_convert;664} else if (IsExtRGB(cinfo->in_color_space))665cconvert->pub._color_convert = rgb_rgb_convert;666else667ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);668break;669670case JCS_YCbCr:671#ifdef C_LOSSLESS_SUPPORTED672if (cinfo->master->lossless &&673cinfo->in_color_space != cinfo->jpeg_color_space)674ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);675#endif676if (cinfo->num_components != 3)677ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);678if (IsExtRGB(cinfo->in_color_space)) {679#ifdef WITH_SIMD680if (jsimd_can_rgb_ycc())681cconvert->pub._color_convert = jsimd_rgb_ycc_convert;682else683#endif684{685cconvert->pub.start_pass = rgb_ycc_start;686cconvert->pub._color_convert = rgb_ycc_convert;687}688} else if (cinfo->in_color_space == JCS_YCbCr) {689#if defined(WITH_SIMD) && defined(__mips__)690if (jsimd_c_can_null_convert())691cconvert->pub._color_convert = jsimd_c_null_convert;692else693#endif694cconvert->pub._color_convert = null_convert;695} else696ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);697break;698699case JCS_CMYK:700#ifdef C_LOSSLESS_SUPPORTED701if (cinfo->master->lossless &&702cinfo->in_color_space != cinfo->jpeg_color_space)703ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);704#endif705if (cinfo->num_components != 4)706ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);707if (cinfo->in_color_space == JCS_CMYK) {708#if defined(WITH_SIMD) && defined(__mips__)709if (jsimd_c_can_null_convert())710cconvert->pub._color_convert = jsimd_c_null_convert;711else712#endif713cconvert->pub._color_convert = null_convert;714} else715ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);716break;717718case JCS_YCCK:719#ifdef C_LOSSLESS_SUPPORTED720if (cinfo->master->lossless &&721cinfo->in_color_space != cinfo->jpeg_color_space)722ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);723#endif724if (cinfo->num_components != 4)725ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);726if (cinfo->in_color_space == JCS_CMYK) {727cconvert->pub.start_pass = rgb_ycc_start;728cconvert->pub._color_convert = cmyk_ycck_convert;729} else if (cinfo->in_color_space == JCS_YCCK) {730#if defined(WITH_SIMD) && defined(__mips__)731if (jsimd_c_can_null_convert())732cconvert->pub._color_convert = jsimd_c_null_convert;733else734#endif735cconvert->pub._color_convert = null_convert;736} else737ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);738break;739740default: /* allow null conversion of JCS_UNKNOWN */741if (cinfo->jpeg_color_space != cinfo->in_color_space ||742cinfo->num_components != cinfo->input_components)743ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);744#if defined(WITH_SIMD) && defined(__mips__)745if (jsimd_c_can_null_convert())746cconvert->pub._color_convert = jsimd_c_null_convert;747else748#endif749cconvert->pub._color_convert = null_convert;750break;751}752}753754#endif /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */755756757