chromium/third_party/libjpeg_turbo/jccolor.c

/*
 * jccolor.c
 *
 * This file was part of the Independent JPEG Group's software:
 * Copyright (C) 1991-1996, Thomas G. Lane.
 * libjpeg-turbo Modifications:
 * Copyright 2009 Pierre Ossman <[email protected]> for Cendio AB
 * Copyright (C) 2009-2012, 2015, 2022, D. R. Commander.
 * Copyright (C) 2014, MIPS Technologies, Inc., California.
 * For conditions of distribution and use, see the accompanying README.ijg
 * file.
 *
 * This file contains input colorspace conversion routines.
 */

#define JPEG_INTERNALS
#include "jinclude.h"
#include "jpeglib.h"
#include "jsimd.h"


/* Private subobject */

my_color_converter;

my_cconvert_ptr;


/**************** RGB -> YCbCr conversion: most common case **************/

/*
 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
 * The conversion equations to be implemented are therefore
 *      Y  =  0.29900 * R + 0.58700 * G + 0.11400 * B
 *      Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B  + CENTERJSAMPLE
 *      Cr =  0.50000 * R - 0.41869 * G - 0.08131 * B  + CENTERJSAMPLE
 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
 * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
 * rather than CENTERJSAMPLE, for Cb and Cr.  This gave equal positive and
 * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
 * were not represented exactly.  Now we sacrifice exact representation of
 * maximum red and maximum blue in order to get exact grayscales.
 *
 * To avoid floating-point arithmetic, we represent the fractional constants
 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
 * the products by 2^16, with appropriate rounding, to get the correct answer.
 *
 * For even more speed, we avoid doing any multiplications in the inner loop
 * by precalculating the constants times R,G,B for all possible values.
 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
 * for 12-bit samples it is still acceptable.  It's not very reasonable for
 * 16-bit samples, but if you want lossless storage you shouldn't be changing
 * colorspace anyway.
 * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
 * in the tables to save adding them separately in the inner loop.
 */

#define SCALEBITS
#define CBCR_OFFSET
#define ONE_HALF
#define FIX(x)

/* We allocate one big table and divide it up into eight parts, instead of
 * doing eight alloc_small requests.  This lets us use a single table base
 * address, which can be held in a register in the inner loops on many
 * machines (more than can hold all eight addresses, anyway).
 */

#define R_Y_OFF
#define G_Y_OFF
#define B_Y_OFF
#define R_CB_OFF
#define G_CB_OFF
#define B_CB_OFF
#define R_CR_OFF
#define G_CR_OFF
#define B_CR_OFF
#define TABLE_SIZE

/* 12-bit samples use a 16-bit data type, so it is possible to pass
 * out-of-range sample values (< 0 or > 4095) to jpeg_write_scanlines().
 * Thus, we mask the incoming 12-bit samples to guard against overrunning
 * or underrunning the conversion tables.
 */

#if BITS_IN_JSAMPLE == 12
#define RANGE_LIMIT
#else
#define RANGE_LIMIT(value)
#endif


/* Include inline routines for colorspace extensions */

#include "jccolext.c"
#undef RGB_RED
#undef RGB_GREEN
#undef RGB_BLUE
#undef RGB_PIXELSIZE

#define RGB_RED
#define RGB_GREEN
#define RGB_BLUE
#define RGB_PIXELSIZE
#define rgb_ycc_convert_internal
#define rgb_gray_convert_internal
#define rgb_rgb_convert_internal
#include "jccolext.c"
#undef RGB_RED
#undef RGB_GREEN
#undef RGB_BLUE
#undef RGB_PIXELSIZE
#undef rgb_ycc_convert_internal
#undef rgb_gray_convert_internal
#undef rgb_rgb_convert_internal

#define RGB_RED
#define RGB_GREEN
#define RGB_BLUE
#define RGB_PIXELSIZE
#define rgb_ycc_convert_internal
#define rgb_gray_convert_internal
#define rgb_rgb_convert_internal
#include "jccolext.c"
#undef RGB_RED
#undef RGB_GREEN
#undef RGB_BLUE
#undef RGB_PIXELSIZE
#undef rgb_ycc_convert_internal
#undef rgb_gray_convert_internal
#undef rgb_rgb_convert_internal

#define RGB_RED
#define RGB_GREEN
#define RGB_BLUE
#define RGB_PIXELSIZE
#define rgb_ycc_convert_internal
#define rgb_gray_convert_internal
#define rgb_rgb_convert_internal
#include "jccolext.c"
#undef RGB_RED
#undef RGB_GREEN
#undef RGB_BLUE
#undef RGB_PIXELSIZE
#undef rgb_ycc_convert_internal
#undef rgb_gray_convert_internal
#undef rgb_rgb_convert_internal

#define RGB_RED
#define RGB_GREEN
#define RGB_BLUE
#define RGB_PIXELSIZE
#define rgb_ycc_convert_internal
#define rgb_gray_convert_internal
#define rgb_rgb_convert_internal
#include "jccolext.c"
#undef RGB_RED
#undef RGB_GREEN
#undef RGB_BLUE
#undef RGB_PIXELSIZE
#undef rgb_ycc_convert_internal
#undef rgb_gray_convert_internal
#undef rgb_rgb_convert_internal

#define RGB_RED
#define RGB_GREEN
#define RGB_BLUE
#define RGB_PIXELSIZE
#define rgb_ycc_convert_internal
#define rgb_gray_convert_internal
#define rgb_rgb_convert_internal
#include "jccolext.c"
#undef RGB_RED
#undef RGB_GREEN
#undef RGB_BLUE
#undef RGB_PIXELSIZE
#undef rgb_ycc_convert_internal
#undef rgb_gray_convert_internal
#undef rgb_rgb_convert_internal

#define RGB_RED
#define RGB_GREEN
#define RGB_BLUE
#define RGB_PIXELSIZE
#define rgb_ycc_convert_internal
#define rgb_gray_convert_internal
#define rgb_rgb_convert_internal
#include "jccolext.c"
#undef RGB_RED
#undef RGB_GREEN
#undef RGB_BLUE
#undef RGB_PIXELSIZE
#undef rgb_ycc_convert_internal
#undef rgb_gray_convert_internal
#undef rgb_rgb_convert_internal


/*
 * Initialize for RGB->YCC colorspace conversion.
 */

METHODDEF(void)
rgb_ycc_start(j_compress_ptr cinfo)
{}


/*
 * Convert some rows of samples to the JPEG colorspace.
 */

METHODDEF(void)
rgb_ycc_convert(j_compress_ptr cinfo, JSAMPARRAY input_buf,
                JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)
{}


/**************** Cases other than RGB -> YCbCr **************/


/*
 * Convert some rows of samples to the JPEG colorspace.
 */

METHODDEF(void)
rgb_gray_convert(j_compress_ptr cinfo, JSAMPARRAY input_buf,
                 JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)
{}


/*
 * Extended RGB to plain RGB conversion
 */

METHODDEF(void)
rgb_rgb_convert(j_compress_ptr cinfo, JSAMPARRAY input_buf,
                JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)
{}


/*
 * Convert some rows of samples to the JPEG colorspace.
 * This version handles Adobe-style CMYK->YCCK conversion,
 * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
 * conversion as above, while passing K (black) unchanged.
 * We assume rgb_ycc_start has been called.
 */

METHODDEF(void)
cmyk_ycck_convert(j_compress_ptr cinfo, JSAMPARRAY input_buf,
                  JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)
{}


/*
 * Convert some rows of samples to the JPEG colorspace.
 * This version handles grayscale output with no conversion.
 * The source can be either plain grayscale or YCbCr (since Y == gray).
 */

METHODDEF(void)
grayscale_convert(j_compress_ptr cinfo, JSAMPARRAY input_buf,
                  JSAMPIMAGE output_buf, JDIMENSION output_row, int num_rows)
{}


/*
 * Convert some rows of samples to the JPEG colorspace.
 * This version handles multi-component colorspaces without conversion.
 * We assume input_components == num_components.
 */

METHODDEF(void)
null_convert(j_compress_ptr cinfo, JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
             JDIMENSION output_row, int num_rows)
{}


/*
 * Empty method for start_pass.
 */

METHODDEF(void)
null_method(j_compress_ptr cinfo)
{}


/*
 * Module initialization routine for input colorspace conversion.
 */

GLOBAL(void)
jinit_color_converter(j_compress_ptr cinfo)
{}