michael@0: /* michael@0: * jdcolor.c michael@0: * michael@0: * This file was part of the Independent JPEG Group's software: michael@0: * Copyright (C) 1991-1997, Thomas G. Lane. michael@0: * Modified 2011 by Guido Vollbeding. michael@0: * libjpeg-turbo Modifications: michael@0: * Copyright 2009 Pierre Ossman for Cendio AB michael@0: * Copyright (C) 2009, 2011-2012, D. R. Commander. michael@0: * For conditions of distribution and use, see the accompanying README file. michael@0: * michael@0: * This file contains output 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_deconverter pub; /* public fields */ michael@0: michael@0: /* Private state for YCC->RGB conversion */ michael@0: int * Cr_r_tab; /* => table for Cr to R conversion */ michael@0: int * Cb_b_tab; /* => table for Cb to B conversion */ michael@0: INT32 * Cr_g_tab; /* => table for Cr to G conversion */ michael@0: INT32 * Cb_g_tab; /* => table for Cb to G conversion */ michael@0: michael@0: /* Private state for RGB->Y conversion */ michael@0: INT32 * rgb_y_tab; /* => table for RGB to Y conversion */ michael@0: } my_color_deconverter; michael@0: michael@0: typedef my_color_deconverter * my_cconvert_ptr; michael@0: michael@0: michael@0: /**************** YCbCr -> RGB conversion: most common case **************/ michael@0: /**************** RGB -> Y conversion: less 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: * michael@0: * R = Y + 1.40200 * Cr michael@0: * G = Y - 0.34414 * Cb - 0.71414 * Cr michael@0: * B = Y + 1.77200 * Cb michael@0: * michael@0: * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B michael@0: * michael@0: * where Cb and Cr represent the incoming values less CENTERJSAMPLE. michael@0: * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.) 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: * Notice that Y, being an integral input, does not contribute any fraction michael@0: * so it need not participate in the rounding. michael@0: * michael@0: * For even more speed, we avoid doing any multiplications in the inner loop michael@0: * by precalculating the constants times Cb and Cr 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 Cr=>R and Cb=>B values can be rounded to integers in advance; the michael@0: * values for the G calculation are left scaled up, since we must add them michael@0: * together before rounding. michael@0: */ michael@0: michael@0: #define SCALEBITS 16 /* speediest right-shift on some machines */ michael@0: #define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) michael@0: #define FIX(x) ((INT32) ((x) * (1L<Y conversion and divide it up into michael@0: * three parts, instead of doing three alloc_small requests. This lets us michael@0: * use a single table base address, which can be held in a register in the michael@0: * inner loops on many machines (more than can hold all three addresses, michael@0: * anyway). michael@0: */ michael@0: michael@0: #define R_Y_OFF 0 /* offset to R => 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 TABLE_SIZE (3*(MAXJSAMPLE+1)) michael@0: michael@0: michael@0: /* Include inline routines for colorspace extensions */ michael@0: michael@0: #include "jdcolext.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 ycc_rgb_convert_internal ycc_extrgb_convert_internal michael@0: #define gray_rgb_convert_internal gray_extrgb_convert_internal michael@0: #define rgb_rgb_convert_internal rgb_extrgb_convert_internal michael@0: #include "jdcolext.c" michael@0: #undef RGB_RED michael@0: #undef RGB_GREEN michael@0: #undef RGB_BLUE michael@0: #undef RGB_PIXELSIZE michael@0: #undef ycc_rgb_convert_internal michael@0: #undef gray_rgb_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_ALPHA 3 michael@0: #define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE michael@0: #define ycc_rgb_convert_internal ycc_extrgbx_convert_internal michael@0: #define gray_rgb_convert_internal gray_extrgbx_convert_internal michael@0: #define rgb_rgb_convert_internal rgb_extrgbx_convert_internal michael@0: #include "jdcolext.c" michael@0: #undef RGB_RED michael@0: #undef RGB_GREEN michael@0: #undef RGB_BLUE michael@0: #undef RGB_ALPHA michael@0: #undef RGB_PIXELSIZE michael@0: #undef ycc_rgb_convert_internal michael@0: #undef gray_rgb_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 ycc_rgb_convert_internal ycc_extbgr_convert_internal michael@0: #define gray_rgb_convert_internal gray_extbgr_convert_internal michael@0: #define rgb_rgb_convert_internal rgb_extbgr_convert_internal michael@0: #include "jdcolext.c" michael@0: #undef RGB_RED michael@0: #undef RGB_GREEN michael@0: #undef RGB_BLUE michael@0: #undef RGB_PIXELSIZE michael@0: #undef ycc_rgb_convert_internal michael@0: #undef gray_rgb_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_ALPHA 3 michael@0: #define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE michael@0: #define ycc_rgb_convert_internal ycc_extbgrx_convert_internal michael@0: #define gray_rgb_convert_internal gray_extbgrx_convert_internal michael@0: #define rgb_rgb_convert_internal rgb_extbgrx_convert_internal michael@0: #include "jdcolext.c" michael@0: #undef RGB_RED michael@0: #undef RGB_GREEN michael@0: #undef RGB_BLUE michael@0: #undef RGB_ALPHA michael@0: #undef RGB_PIXELSIZE michael@0: #undef ycc_rgb_convert_internal michael@0: #undef gray_rgb_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_ALPHA 0 michael@0: #define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE michael@0: #define ycc_rgb_convert_internal ycc_extxbgr_convert_internal michael@0: #define gray_rgb_convert_internal gray_extxbgr_convert_internal michael@0: #define rgb_rgb_convert_internal rgb_extxbgr_convert_internal michael@0: #include "jdcolext.c" michael@0: #undef RGB_RED michael@0: #undef RGB_GREEN michael@0: #undef RGB_BLUE michael@0: #undef RGB_ALPHA michael@0: #undef RGB_PIXELSIZE michael@0: #undef ycc_rgb_convert_internal michael@0: #undef gray_rgb_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_ALPHA 0 michael@0: #define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE michael@0: #define ycc_rgb_convert_internal ycc_extxrgb_convert_internal michael@0: #define gray_rgb_convert_internal gray_extxrgb_convert_internal michael@0: #define rgb_rgb_convert_internal rgb_extxrgb_convert_internal michael@0: #include "jdcolext.c" michael@0: #undef RGB_RED michael@0: #undef RGB_GREEN michael@0: #undef RGB_BLUE michael@0: #undef RGB_ALPHA michael@0: #undef RGB_PIXELSIZE michael@0: #undef ycc_rgb_convert_internal michael@0: #undef gray_rgb_convert_internal michael@0: #undef rgb_rgb_convert_internal michael@0: michael@0: michael@0: /* michael@0: * Initialize tables for YCC->RGB colorspace conversion. michael@0: */ michael@0: michael@0: LOCAL(void) michael@0: build_ycc_rgb_table (j_decompress_ptr cinfo) michael@0: { michael@0: my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; michael@0: int i; michael@0: INT32 x; michael@0: SHIFT_TEMPS michael@0: michael@0: cconvert->Cr_r_tab = (int *) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: (MAXJSAMPLE+1) * SIZEOF(int)); michael@0: cconvert->Cb_b_tab = (int *) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: (MAXJSAMPLE+1) * SIZEOF(int)); michael@0: cconvert->Cr_g_tab = (INT32 *) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: (MAXJSAMPLE+1) * SIZEOF(INT32)); michael@0: cconvert->Cb_g_tab = (INT32 *) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: (MAXJSAMPLE+1) * SIZEOF(INT32)); michael@0: michael@0: for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) { michael@0: /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */ michael@0: /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */ michael@0: /* Cr=>R value is nearest int to 1.40200 * x */ michael@0: cconvert->Cr_r_tab[i] = (int) michael@0: RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS); michael@0: /* Cb=>B value is nearest int to 1.77200 * x */ michael@0: cconvert->Cb_b_tab[i] = (int) michael@0: RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS); michael@0: /* Cr=>G value is scaled-up -0.71414 * x */ michael@0: cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x; michael@0: /* Cb=>G value is scaled-up -0.34414 * x */ michael@0: /* We also add in ONE_HALF so that need not do it in inner loop */ michael@0: cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Convert some rows of samples to the output colorspace. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: ycc_rgb_convert (j_decompress_ptr cinfo, michael@0: JSAMPIMAGE input_buf, JDIMENSION input_row, michael@0: JSAMPARRAY output_buf, int num_rows) michael@0: { michael@0: switch (cinfo->out_color_space) { michael@0: case JCS_EXT_RGB: michael@0: ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_RGBX: michael@0: case JCS_EXT_RGBA: michael@0: ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_BGR: michael@0: ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_BGRX: michael@0: case JCS_EXT_BGRA: michael@0: ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_XBGR: michael@0: case JCS_EXT_ABGR: michael@0: ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_XRGB: michael@0: case JCS_EXT_ARGB: michael@0: ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: default: michael@0: ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: michael@0: /**************** Cases other than YCbCr -> RGB **************/ michael@0: michael@0: michael@0: /* michael@0: * Initialize for RGB->grayscale colorspace conversion. michael@0: */ michael@0: michael@0: LOCAL(void) michael@0: build_rgb_y_table (j_decompress_ptr cinfo) michael@0: { michael@0: my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; michael@0: INT32 * rgb_y_tab; michael@0: INT32 i; michael@0: michael@0: /* Allocate and fill in the conversion tables. */ michael@0: cconvert->rgb_y_tab = rgb_y_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_y_tab[i+R_Y_OFF] = FIX(0.29900) * i; michael@0: rgb_y_tab[i+G_Y_OFF] = FIX(0.58700) * i; michael@0: rgb_y_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Convert RGB to grayscale. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: rgb_gray_convert (j_decompress_ptr cinfo, michael@0: JSAMPIMAGE input_buf, JDIMENSION input_row, michael@0: JSAMPARRAY output_buf, 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_y_tab; michael@0: register JSAMPROW outptr; michael@0: register JSAMPROW inptr0, inptr1, inptr2; michael@0: register JDIMENSION col; michael@0: JDIMENSION num_cols = cinfo->output_width; michael@0: michael@0: while (--num_rows >= 0) { michael@0: inptr0 = input_buf[0][input_row]; michael@0: inptr1 = input_buf[1][input_row]; michael@0: inptr2 = input_buf[2][input_row]; michael@0: input_row++; michael@0: outptr = *output_buf++; michael@0: for (col = 0; col < num_cols; col++) { michael@0: r = GETJSAMPLE(inptr0[col]); michael@0: g = GETJSAMPLE(inptr1[col]); michael@0: b = GETJSAMPLE(inptr2[col]); michael@0: /* Y */ michael@0: outptr[col] = (JSAMPLE) michael@0: ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) michael@0: >> SCALEBITS); michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Color conversion for no colorspace change: just copy the data, michael@0: * converting from separate-planes to interleaved representation. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: null_convert (j_decompress_ptr cinfo, michael@0: JSAMPIMAGE input_buf, JDIMENSION input_row, michael@0: JSAMPARRAY output_buf, int num_rows) michael@0: { michael@0: register JSAMPROW inptr, outptr; michael@0: register JDIMENSION count; michael@0: register int num_components = cinfo->num_components; michael@0: JDIMENSION num_cols = cinfo->output_width; michael@0: int ci; michael@0: michael@0: while (--num_rows >= 0) { michael@0: for (ci = 0; ci < num_components; ci++) { michael@0: inptr = input_buf[ci][input_row]; michael@0: outptr = output_buf[0] + ci; michael@0: for (count = num_cols; count > 0; count--) { michael@0: *outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */ michael@0: outptr += num_components; michael@0: } michael@0: } michael@0: input_row++; michael@0: output_buf++; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Color conversion for grayscale: just copy the data. michael@0: * This also works for YCbCr -> grayscale conversion, in which michael@0: * we just copy the Y (luminance) component and ignore chrominance. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: grayscale_convert (j_decompress_ptr cinfo, michael@0: JSAMPIMAGE input_buf, JDIMENSION input_row, michael@0: JSAMPARRAY output_buf, int num_rows) michael@0: { michael@0: jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0, michael@0: num_rows, cinfo->output_width); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Convert grayscale to RGB michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: gray_rgb_convert (j_decompress_ptr cinfo, michael@0: JSAMPIMAGE input_buf, JDIMENSION input_row, michael@0: JSAMPARRAY output_buf, int num_rows) michael@0: { michael@0: switch (cinfo->out_color_space) { michael@0: case JCS_EXT_RGB: michael@0: gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_RGBX: michael@0: case JCS_EXT_RGBA: michael@0: gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_BGR: michael@0: gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_BGRX: michael@0: case JCS_EXT_BGRA: michael@0: gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_XBGR: michael@0: case JCS_EXT_ABGR: michael@0: gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_XRGB: michael@0: case JCS_EXT_ARGB: michael@0: gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: default: michael@0: gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Convert plain RGB to extended RGB michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: rgb_rgb_convert (j_decompress_ptr cinfo, michael@0: JSAMPIMAGE input_buf, JDIMENSION input_row, michael@0: JSAMPARRAY output_buf, int num_rows) michael@0: { michael@0: switch (cinfo->out_color_space) { michael@0: case JCS_EXT_RGB: michael@0: rgb_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_RGBX: michael@0: case JCS_EXT_RGBA: michael@0: rgb_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_BGR: michael@0: rgb_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_BGRX: michael@0: case JCS_EXT_BGRA: michael@0: rgb_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_XBGR: michael@0: case JCS_EXT_ABGR: michael@0: rgb_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: case JCS_EXT_XRGB: michael@0: case JCS_EXT_ARGB: michael@0: rgb_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: default: michael@0: rgb_rgb_convert_internal(cinfo, input_buf, input_row, output_buf, michael@0: num_rows); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Adobe-style YCCK->CMYK conversion. michael@0: * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same michael@0: * conversion as above, while passing K (black) unchanged. michael@0: * We assume build_ycc_rgb_table has been called. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: ycck_cmyk_convert (j_decompress_ptr cinfo, michael@0: JSAMPIMAGE input_buf, JDIMENSION input_row, michael@0: JSAMPARRAY output_buf, int num_rows) michael@0: { michael@0: my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; michael@0: register int y, cb, cr; michael@0: register JSAMPROW outptr; michael@0: register JSAMPROW inptr0, inptr1, inptr2, inptr3; michael@0: register JDIMENSION col; michael@0: JDIMENSION num_cols = cinfo->output_width; michael@0: /* copy these pointers into registers if possible */ michael@0: register JSAMPLE * range_limit = cinfo->sample_range_limit; michael@0: register int * Crrtab = cconvert->Cr_r_tab; michael@0: register int * Cbbtab = cconvert->Cb_b_tab; michael@0: register INT32 * Crgtab = cconvert->Cr_g_tab; michael@0: register INT32 * Cbgtab = cconvert->Cb_g_tab; michael@0: SHIFT_TEMPS michael@0: michael@0: while (--num_rows >= 0) { michael@0: inptr0 = input_buf[0][input_row]; michael@0: inptr1 = input_buf[1][input_row]; michael@0: inptr2 = input_buf[2][input_row]; michael@0: inptr3 = input_buf[3][input_row]; michael@0: input_row++; michael@0: outptr = *output_buf++; michael@0: for (col = 0; col < num_cols; col++) { michael@0: y = GETJSAMPLE(inptr0[col]); michael@0: cb = GETJSAMPLE(inptr1[col]); michael@0: cr = GETJSAMPLE(inptr2[col]); michael@0: /* Range-limiting is essential due to noise introduced by DCT losses. */ michael@0: outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */ michael@0: outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */ michael@0: ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], michael@0: SCALEBITS)))]; michael@0: outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */ michael@0: /* K passes through unchanged */ michael@0: outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */ michael@0: outptr += 4; michael@0: } 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: start_pass_dcolor (j_decompress_ptr cinfo) michael@0: { michael@0: /* no work needed */ michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Module initialization routine for output colorspace conversion. michael@0: */ michael@0: michael@0: GLOBAL(void) michael@0: jinit_color_deconverter (j_decompress_ptr cinfo) michael@0: { michael@0: my_cconvert_ptr cconvert; michael@0: int ci; 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_deconverter)); michael@0: cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert; michael@0: cconvert->pub.start_pass = start_pass_dcolor; michael@0: michael@0: /* Make sure num_components agrees with jpeg_color_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: break; michael@0: michael@0: case JCS_RGB: michael@0: case JCS_YCbCr: michael@0: if (cinfo->num_components != 3) michael@0: ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); michael@0: break; michael@0: michael@0: case JCS_CMYK: michael@0: case JCS_YCCK: michael@0: if (cinfo->num_components != 4) michael@0: ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); michael@0: break; michael@0: michael@0: default: /* JCS_UNKNOWN can be anything */ michael@0: if (cinfo->num_components < 1) michael@0: ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); michael@0: break; michael@0: } michael@0: michael@0: /* Set out_color_components and conversion method based on requested space. michael@0: * Also clear the component_needed flags for any unused components, michael@0: * so that earlier pipeline stages can avoid useless computation. michael@0: */ michael@0: michael@0: switch (cinfo->out_color_space) { michael@0: case JCS_GRAYSCALE: michael@0: cinfo->out_color_components = 1; michael@0: if (cinfo->jpeg_color_space == JCS_GRAYSCALE || michael@0: cinfo->jpeg_color_space == JCS_YCbCr) { michael@0: cconvert->pub.color_convert = grayscale_convert; michael@0: /* For color->grayscale conversion, only the Y (0) component is needed */ michael@0: for (ci = 1; ci < cinfo->num_components; ci++) michael@0: cinfo->comp_info[ci].component_needed = FALSE; michael@0: } else if (cinfo->jpeg_color_space == JCS_RGB) { michael@0: cconvert->pub.color_convert = rgb_gray_convert; michael@0: build_rgb_y_table(cinfo); michael@0: } else michael@0: ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 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: cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space]; michael@0: if (cinfo->jpeg_color_space == JCS_YCbCr) { michael@0: if (jsimd_can_ycc_rgb()) michael@0: cconvert->pub.color_convert = jsimd_ycc_rgb_convert; michael@0: else { michael@0: cconvert->pub.color_convert = ycc_rgb_convert; michael@0: build_ycc_rgb_table(cinfo); michael@0: } michael@0: } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) { michael@0: cconvert->pub.color_convert = gray_rgb_convert; michael@0: } else if (cinfo->jpeg_color_space == JCS_RGB) { michael@0: if (rgb_red[cinfo->out_color_space] == 0 && michael@0: rgb_green[cinfo->out_color_space] == 1 && michael@0: rgb_blue[cinfo->out_color_space] == 2 && michael@0: rgb_pixelsize[cinfo->out_color_space] == 3) michael@0: cconvert->pub.color_convert = null_convert; michael@0: else 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_CMYK: michael@0: cinfo->out_color_components = 4; michael@0: if (cinfo->jpeg_color_space == JCS_YCCK) { michael@0: cconvert->pub.color_convert = ycck_cmyk_convert; michael@0: build_ycc_rgb_table(cinfo); michael@0: } else if (cinfo->jpeg_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: default: michael@0: /* Permit null conversion to same output space */ michael@0: if (cinfo->out_color_space == cinfo->jpeg_color_space) { michael@0: cinfo->out_color_components = cinfo->num_components; michael@0: cconvert->pub.color_convert = null_convert; michael@0: } else /* unsupported non-null conversion */ michael@0: ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); michael@0: break; michael@0: } michael@0: michael@0: if (cinfo->quantize_colors) michael@0: cinfo->output_components = 1; /* single colormapped output component */ michael@0: else michael@0: cinfo->output_components = cinfo->out_color_components; michael@0: }