1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libjpeg/jccolor.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,662 @@ 1.4 +/* 1.5 + * jccolor.c 1.6 + * 1.7 + * This file was part of the Independent JPEG Group's software: 1.8 + * Copyright (C) 1991-1996, Thomas G. Lane. 1.9 + * libjpeg-turbo Modifications: 1.10 + * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB 1.11 + * Copyright (C) 2009-2012, D. R. Commander. 1.12 + * For conditions of distribution and use, see the accompanying README file. 1.13 + * 1.14 + * This file contains input colorspace conversion routines. 1.15 + */ 1.16 + 1.17 +#define JPEG_INTERNALS 1.18 +#include "jinclude.h" 1.19 +#include "jpeglib.h" 1.20 +#include "jsimd.h" 1.21 +#include "config.h" 1.22 + 1.23 + 1.24 +/* Private subobject */ 1.25 + 1.26 +typedef struct { 1.27 + struct jpeg_color_converter pub; /* public fields */ 1.28 + 1.29 + /* Private state for RGB->YCC conversion */ 1.30 + INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */ 1.31 +} my_color_converter; 1.32 + 1.33 +typedef my_color_converter * my_cconvert_ptr; 1.34 + 1.35 + 1.36 +/**************** RGB -> YCbCr conversion: most common case **************/ 1.37 + 1.38 +/* 1.39 + * YCbCr is defined per CCIR 601-1, except that Cb and Cr are 1.40 + * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5. 1.41 + * The conversion equations to be implemented are therefore 1.42 + * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B 1.43 + * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE 1.44 + * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE 1.45 + * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.) 1.46 + * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2, 1.47 + * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and 1.48 + * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0) 1.49 + * were not represented exactly. Now we sacrifice exact representation of 1.50 + * maximum red and maximum blue in order to get exact grayscales. 1.51 + * 1.52 + * To avoid floating-point arithmetic, we represent the fractional constants 1.53 + * as integers scaled up by 2^16 (about 4 digits precision); we have to divide 1.54 + * the products by 2^16, with appropriate rounding, to get the correct answer. 1.55 + * 1.56 + * For even more speed, we avoid doing any multiplications in the inner loop 1.57 + * by precalculating the constants times R,G,B for all possible values. 1.58 + * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table); 1.59 + * for 12-bit samples it is still acceptable. It's not very reasonable for 1.60 + * 16-bit samples, but if you want lossless storage you shouldn't be changing 1.61 + * colorspace anyway. 1.62 + * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included 1.63 + * in the tables to save adding them separately in the inner loop. 1.64 + */ 1.65 + 1.66 +#define SCALEBITS 16 /* speediest right-shift on some machines */ 1.67 +#define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS) 1.68 +#define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) 1.69 +#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5)) 1.70 + 1.71 +/* We allocate one big table and divide it up into eight parts, instead of 1.72 + * doing eight alloc_small requests. This lets us use a single table base 1.73 + * address, which can be held in a register in the inner loops on many 1.74 + * machines (more than can hold all eight addresses, anyway). 1.75 + */ 1.76 + 1.77 +#define R_Y_OFF 0 /* offset to R => Y section */ 1.78 +#define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */ 1.79 +#define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */ 1.80 +#define R_CB_OFF (3*(MAXJSAMPLE+1)) 1.81 +#define G_CB_OFF (4*(MAXJSAMPLE+1)) 1.82 +#define B_CB_OFF (5*(MAXJSAMPLE+1)) 1.83 +#define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */ 1.84 +#define G_CR_OFF (6*(MAXJSAMPLE+1)) 1.85 +#define B_CR_OFF (7*(MAXJSAMPLE+1)) 1.86 +#define TABLE_SIZE (8*(MAXJSAMPLE+1)) 1.87 + 1.88 + 1.89 +/* Include inline routines for colorspace extensions */ 1.90 + 1.91 +#include "jccolext.c" 1.92 +#undef RGB_RED 1.93 +#undef RGB_GREEN 1.94 +#undef RGB_BLUE 1.95 +#undef RGB_PIXELSIZE 1.96 + 1.97 +#define RGB_RED EXT_RGB_RED 1.98 +#define RGB_GREEN EXT_RGB_GREEN 1.99 +#define RGB_BLUE EXT_RGB_BLUE 1.100 +#define RGB_PIXELSIZE EXT_RGB_PIXELSIZE 1.101 +#define rgb_ycc_convert_internal extrgb_ycc_convert_internal 1.102 +#define rgb_gray_convert_internal extrgb_gray_convert_internal 1.103 +#define rgb_rgb_convert_internal extrgb_rgb_convert_internal 1.104 +#include "jccolext.c" 1.105 +#undef RGB_RED 1.106 +#undef RGB_GREEN 1.107 +#undef RGB_BLUE 1.108 +#undef RGB_PIXELSIZE 1.109 +#undef rgb_ycc_convert_internal 1.110 +#undef rgb_gray_convert_internal 1.111 +#undef rgb_rgb_convert_internal 1.112 + 1.113 +#define RGB_RED EXT_RGBX_RED 1.114 +#define RGB_GREEN EXT_RGBX_GREEN 1.115 +#define RGB_BLUE EXT_RGBX_BLUE 1.116 +#define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE 1.117 +#define rgb_ycc_convert_internal extrgbx_ycc_convert_internal 1.118 +#define rgb_gray_convert_internal extrgbx_gray_convert_internal 1.119 +#define rgb_rgb_convert_internal extrgbx_rgb_convert_internal 1.120 +#include "jccolext.c" 1.121 +#undef RGB_RED 1.122 +#undef RGB_GREEN 1.123 +#undef RGB_BLUE 1.124 +#undef RGB_PIXELSIZE 1.125 +#undef rgb_ycc_convert_internal 1.126 +#undef rgb_gray_convert_internal 1.127 +#undef rgb_rgb_convert_internal 1.128 + 1.129 +#define RGB_RED EXT_BGR_RED 1.130 +#define RGB_GREEN EXT_BGR_GREEN 1.131 +#define RGB_BLUE EXT_BGR_BLUE 1.132 +#define RGB_PIXELSIZE EXT_BGR_PIXELSIZE 1.133 +#define rgb_ycc_convert_internal extbgr_ycc_convert_internal 1.134 +#define rgb_gray_convert_internal extbgr_gray_convert_internal 1.135 +#define rgb_rgb_convert_internal extbgr_rgb_convert_internal 1.136 +#include "jccolext.c" 1.137 +#undef RGB_RED 1.138 +#undef RGB_GREEN 1.139 +#undef RGB_BLUE 1.140 +#undef RGB_PIXELSIZE 1.141 +#undef rgb_ycc_convert_internal 1.142 +#undef rgb_gray_convert_internal 1.143 +#undef rgb_rgb_convert_internal 1.144 + 1.145 +#define RGB_RED EXT_BGRX_RED 1.146 +#define RGB_GREEN EXT_BGRX_GREEN 1.147 +#define RGB_BLUE EXT_BGRX_BLUE 1.148 +#define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE 1.149 +#define rgb_ycc_convert_internal extbgrx_ycc_convert_internal 1.150 +#define rgb_gray_convert_internal extbgrx_gray_convert_internal 1.151 +#define rgb_rgb_convert_internal extbgrx_rgb_convert_internal 1.152 +#include "jccolext.c" 1.153 +#undef RGB_RED 1.154 +#undef RGB_GREEN 1.155 +#undef RGB_BLUE 1.156 +#undef RGB_PIXELSIZE 1.157 +#undef rgb_ycc_convert_internal 1.158 +#undef rgb_gray_convert_internal 1.159 +#undef rgb_rgb_convert_internal 1.160 + 1.161 +#define RGB_RED EXT_XBGR_RED 1.162 +#define RGB_GREEN EXT_XBGR_GREEN 1.163 +#define RGB_BLUE EXT_XBGR_BLUE 1.164 +#define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE 1.165 +#define rgb_ycc_convert_internal extxbgr_ycc_convert_internal 1.166 +#define rgb_gray_convert_internal extxbgr_gray_convert_internal 1.167 +#define rgb_rgb_convert_internal extxbgr_rgb_convert_internal 1.168 +#include "jccolext.c" 1.169 +#undef RGB_RED 1.170 +#undef RGB_GREEN 1.171 +#undef RGB_BLUE 1.172 +#undef RGB_PIXELSIZE 1.173 +#undef rgb_ycc_convert_internal 1.174 +#undef rgb_gray_convert_internal 1.175 +#undef rgb_rgb_convert_internal 1.176 + 1.177 +#define RGB_RED EXT_XRGB_RED 1.178 +#define RGB_GREEN EXT_XRGB_GREEN 1.179 +#define RGB_BLUE EXT_XRGB_BLUE 1.180 +#define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE 1.181 +#define rgb_ycc_convert_internal extxrgb_ycc_convert_internal 1.182 +#define rgb_gray_convert_internal extxrgb_gray_convert_internal 1.183 +#define rgb_rgb_convert_internal extxrgb_rgb_convert_internal 1.184 +#include "jccolext.c" 1.185 +#undef RGB_RED 1.186 +#undef RGB_GREEN 1.187 +#undef RGB_BLUE 1.188 +#undef RGB_PIXELSIZE 1.189 +#undef rgb_ycc_convert_internal 1.190 +#undef rgb_gray_convert_internal 1.191 +#undef rgb_rgb_convert_internal 1.192 + 1.193 + 1.194 +/* 1.195 + * Initialize for RGB->YCC colorspace conversion. 1.196 + */ 1.197 + 1.198 +METHODDEF(void) 1.199 +rgb_ycc_start (j_compress_ptr cinfo) 1.200 +{ 1.201 + my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; 1.202 + INT32 * rgb_ycc_tab; 1.203 + INT32 i; 1.204 + 1.205 + /* Allocate and fill in the conversion tables. */ 1.206 + cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *) 1.207 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.208 + (TABLE_SIZE * SIZEOF(INT32))); 1.209 + 1.210 + for (i = 0; i <= MAXJSAMPLE; i++) { 1.211 + rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i; 1.212 + rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i; 1.213 + rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF; 1.214 + rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i; 1.215 + rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i; 1.216 + /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr. 1.217 + * This ensures that the maximum output will round to MAXJSAMPLE 1.218 + * not MAXJSAMPLE+1, and thus that we don't have to range-limit. 1.219 + */ 1.220 + rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1; 1.221 +/* B=>Cb and R=>Cr tables are the same 1.222 + rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1; 1.223 +*/ 1.224 + rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i; 1.225 + rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i; 1.226 + } 1.227 +} 1.228 + 1.229 + 1.230 +/* 1.231 + * Convert some rows of samples to the JPEG colorspace. 1.232 + */ 1.233 + 1.234 +METHODDEF(void) 1.235 +rgb_ycc_convert (j_compress_ptr cinfo, 1.236 + JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 1.237 + JDIMENSION output_row, int num_rows) 1.238 +{ 1.239 + switch (cinfo->in_color_space) { 1.240 + case JCS_EXT_RGB: 1.241 + extrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, 1.242 + num_rows); 1.243 + break; 1.244 + case JCS_EXT_RGBX: 1.245 + case JCS_EXT_RGBA: 1.246 + extrgbx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, 1.247 + num_rows); 1.248 + break; 1.249 + case JCS_EXT_BGR: 1.250 + extbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, 1.251 + num_rows); 1.252 + break; 1.253 + case JCS_EXT_BGRX: 1.254 + case JCS_EXT_BGRA: 1.255 + extbgrx_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, 1.256 + num_rows); 1.257 + break; 1.258 + case JCS_EXT_XBGR: 1.259 + case JCS_EXT_ABGR: 1.260 + extxbgr_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, 1.261 + num_rows); 1.262 + break; 1.263 + case JCS_EXT_XRGB: 1.264 + case JCS_EXT_ARGB: 1.265 + extxrgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, 1.266 + num_rows); 1.267 + break; 1.268 + default: 1.269 + rgb_ycc_convert_internal(cinfo, input_buf, output_buf, output_row, 1.270 + num_rows); 1.271 + break; 1.272 + } 1.273 +} 1.274 + 1.275 + 1.276 +/**************** Cases other than RGB -> YCbCr **************/ 1.277 + 1.278 + 1.279 +/* 1.280 + * Convert some rows of samples to the JPEG colorspace. 1.281 + */ 1.282 + 1.283 +METHODDEF(void) 1.284 +rgb_gray_convert (j_compress_ptr cinfo, 1.285 + JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 1.286 + JDIMENSION output_row, int num_rows) 1.287 +{ 1.288 + switch (cinfo->in_color_space) { 1.289 + case JCS_EXT_RGB: 1.290 + extrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row, 1.291 + num_rows); 1.292 + break; 1.293 + case JCS_EXT_RGBX: 1.294 + case JCS_EXT_RGBA: 1.295 + extrgbx_gray_convert_internal(cinfo, input_buf, output_buf, output_row, 1.296 + num_rows); 1.297 + break; 1.298 + case JCS_EXT_BGR: 1.299 + extbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row, 1.300 + num_rows); 1.301 + break; 1.302 + case JCS_EXT_BGRX: 1.303 + case JCS_EXT_BGRA: 1.304 + extbgrx_gray_convert_internal(cinfo, input_buf, output_buf, output_row, 1.305 + num_rows); 1.306 + break; 1.307 + case JCS_EXT_XBGR: 1.308 + case JCS_EXT_ABGR: 1.309 + extxbgr_gray_convert_internal(cinfo, input_buf, output_buf, output_row, 1.310 + num_rows); 1.311 + break; 1.312 + case JCS_EXT_XRGB: 1.313 + case JCS_EXT_ARGB: 1.314 + extxrgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row, 1.315 + num_rows); 1.316 + break; 1.317 + default: 1.318 + rgb_gray_convert_internal(cinfo, input_buf, output_buf, output_row, 1.319 + num_rows); 1.320 + break; 1.321 + } 1.322 +} 1.323 + 1.324 + 1.325 +/* 1.326 + * Extended RGB to plain RGB conversion 1.327 + */ 1.328 + 1.329 +METHODDEF(void) 1.330 +rgb_rgb_convert (j_compress_ptr cinfo, 1.331 + JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 1.332 + JDIMENSION output_row, int num_rows) 1.333 +{ 1.334 + switch (cinfo->in_color_space) { 1.335 + case JCS_EXT_RGB: 1.336 + extrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, 1.337 + num_rows); 1.338 + break; 1.339 + case JCS_EXT_RGBX: 1.340 + case JCS_EXT_RGBA: 1.341 + extrgbx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, 1.342 + num_rows); 1.343 + break; 1.344 + case JCS_EXT_BGR: 1.345 + extbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, 1.346 + num_rows); 1.347 + break; 1.348 + case JCS_EXT_BGRX: 1.349 + case JCS_EXT_BGRA: 1.350 + extbgrx_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, 1.351 + num_rows); 1.352 + break; 1.353 + case JCS_EXT_XBGR: 1.354 + case JCS_EXT_ABGR: 1.355 + extxbgr_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, 1.356 + num_rows); 1.357 + break; 1.358 + case JCS_EXT_XRGB: 1.359 + case JCS_EXT_ARGB: 1.360 + extxrgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, 1.361 + num_rows); 1.362 + break; 1.363 + default: 1.364 + rgb_rgb_convert_internal(cinfo, input_buf, output_buf, output_row, 1.365 + num_rows); 1.366 + break; 1.367 + } 1.368 +} 1.369 + 1.370 + 1.371 +/* 1.372 + * Convert some rows of samples to the JPEG colorspace. 1.373 + * This version handles Adobe-style CMYK->YCCK conversion, 1.374 + * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same 1.375 + * conversion as above, while passing K (black) unchanged. 1.376 + * We assume rgb_ycc_start has been called. 1.377 + */ 1.378 + 1.379 +METHODDEF(void) 1.380 +cmyk_ycck_convert (j_compress_ptr cinfo, 1.381 + JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 1.382 + JDIMENSION output_row, int num_rows) 1.383 +{ 1.384 + my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; 1.385 + register int r, g, b; 1.386 + register INT32 * ctab = cconvert->rgb_ycc_tab; 1.387 + register JSAMPROW inptr; 1.388 + register JSAMPROW outptr0, outptr1, outptr2, outptr3; 1.389 + register JDIMENSION col; 1.390 + JDIMENSION num_cols = cinfo->image_width; 1.391 + 1.392 + while (--num_rows >= 0) { 1.393 + inptr = *input_buf++; 1.394 + outptr0 = output_buf[0][output_row]; 1.395 + outptr1 = output_buf[1][output_row]; 1.396 + outptr2 = output_buf[2][output_row]; 1.397 + outptr3 = output_buf[3][output_row]; 1.398 + output_row++; 1.399 + for (col = 0; col < num_cols; col++) { 1.400 + r = MAXJSAMPLE - GETJSAMPLE(inptr[0]); 1.401 + g = MAXJSAMPLE - GETJSAMPLE(inptr[1]); 1.402 + b = MAXJSAMPLE - GETJSAMPLE(inptr[2]); 1.403 + /* K passes through as-is */ 1.404 + outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */ 1.405 + inptr += 4; 1.406 + /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations 1.407 + * must be too; we do not need an explicit range-limiting operation. 1.408 + * Hence the value being shifted is never negative, and we don't 1.409 + * need the general RIGHT_SHIFT macro. 1.410 + */ 1.411 + /* Y */ 1.412 + outptr0[col] = (JSAMPLE) 1.413 + ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) 1.414 + >> SCALEBITS); 1.415 + /* Cb */ 1.416 + outptr1[col] = (JSAMPLE) 1.417 + ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) 1.418 + >> SCALEBITS); 1.419 + /* Cr */ 1.420 + outptr2[col] = (JSAMPLE) 1.421 + ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) 1.422 + >> SCALEBITS); 1.423 + } 1.424 + } 1.425 +} 1.426 + 1.427 + 1.428 +/* 1.429 + * Convert some rows of samples to the JPEG colorspace. 1.430 + * This version handles grayscale output with no conversion. 1.431 + * The source can be either plain grayscale or YCbCr (since Y == gray). 1.432 + */ 1.433 + 1.434 +METHODDEF(void) 1.435 +grayscale_convert (j_compress_ptr cinfo, 1.436 + JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 1.437 + JDIMENSION output_row, int num_rows) 1.438 +{ 1.439 + register JSAMPROW inptr; 1.440 + register JSAMPROW outptr; 1.441 + register JDIMENSION col; 1.442 + JDIMENSION num_cols = cinfo->image_width; 1.443 + int instride = cinfo->input_components; 1.444 + 1.445 + while (--num_rows >= 0) { 1.446 + inptr = *input_buf++; 1.447 + outptr = output_buf[0][output_row]; 1.448 + output_row++; 1.449 + for (col = 0; col < num_cols; col++) { 1.450 + outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */ 1.451 + inptr += instride; 1.452 + } 1.453 + } 1.454 +} 1.455 + 1.456 + 1.457 +/* 1.458 + * Convert some rows of samples to the JPEG colorspace. 1.459 + * This version handles multi-component colorspaces without conversion. 1.460 + * We assume input_components == num_components. 1.461 + */ 1.462 + 1.463 +METHODDEF(void) 1.464 +null_convert (j_compress_ptr cinfo, 1.465 + JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 1.466 + JDIMENSION output_row, int num_rows) 1.467 +{ 1.468 + register JSAMPROW inptr; 1.469 + register JSAMPROW outptr; 1.470 + register JDIMENSION col; 1.471 + register int ci; 1.472 + int nc = cinfo->num_components; 1.473 + JDIMENSION num_cols = cinfo->image_width; 1.474 + 1.475 + while (--num_rows >= 0) { 1.476 + /* It seems fastest to make a separate pass for each component. */ 1.477 + for (ci = 0; ci < nc; ci++) { 1.478 + inptr = *input_buf; 1.479 + outptr = output_buf[ci][output_row]; 1.480 + for (col = 0; col < num_cols; col++) { 1.481 + outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */ 1.482 + inptr += nc; 1.483 + } 1.484 + } 1.485 + input_buf++; 1.486 + output_row++; 1.487 + } 1.488 +} 1.489 + 1.490 + 1.491 +/* 1.492 + * Empty method for start_pass. 1.493 + */ 1.494 + 1.495 +METHODDEF(void) 1.496 +null_method (j_compress_ptr cinfo) 1.497 +{ 1.498 + /* no work needed */ 1.499 +} 1.500 + 1.501 + 1.502 +/* 1.503 + * Module initialization routine for input colorspace conversion. 1.504 + */ 1.505 + 1.506 +GLOBAL(void) 1.507 +jinit_color_converter (j_compress_ptr cinfo) 1.508 +{ 1.509 + my_cconvert_ptr cconvert; 1.510 + 1.511 + cconvert = (my_cconvert_ptr) 1.512 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.513 + SIZEOF(my_color_converter)); 1.514 + cinfo->cconvert = (struct jpeg_color_converter *) cconvert; 1.515 + /* set start_pass to null method until we find out differently */ 1.516 + cconvert->pub.start_pass = null_method; 1.517 + 1.518 + /* Make sure input_components agrees with in_color_space */ 1.519 + switch (cinfo->in_color_space) { 1.520 + case JCS_GRAYSCALE: 1.521 + if (cinfo->input_components != 1) 1.522 + ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 1.523 + break; 1.524 + 1.525 + case JCS_RGB: 1.526 + case JCS_EXT_RGB: 1.527 + case JCS_EXT_RGBX: 1.528 + case JCS_EXT_BGR: 1.529 + case JCS_EXT_BGRX: 1.530 + case JCS_EXT_XBGR: 1.531 + case JCS_EXT_XRGB: 1.532 + case JCS_EXT_RGBA: 1.533 + case JCS_EXT_BGRA: 1.534 + case JCS_EXT_ABGR: 1.535 + case JCS_EXT_ARGB: 1.536 + if (cinfo->input_components != rgb_pixelsize[cinfo->in_color_space]) 1.537 + ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 1.538 + break; 1.539 + 1.540 + case JCS_YCbCr: 1.541 + if (cinfo->input_components != 3) 1.542 + ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 1.543 + break; 1.544 + 1.545 + case JCS_CMYK: 1.546 + case JCS_YCCK: 1.547 + if (cinfo->input_components != 4) 1.548 + ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 1.549 + break; 1.550 + 1.551 + default: /* JCS_UNKNOWN can be anything */ 1.552 + if (cinfo->input_components < 1) 1.553 + ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 1.554 + break; 1.555 + } 1.556 + 1.557 + /* Check num_components, set conversion method based on requested space */ 1.558 + switch (cinfo->jpeg_color_space) { 1.559 + case JCS_GRAYSCALE: 1.560 + if (cinfo->num_components != 1) 1.561 + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 1.562 + if (cinfo->in_color_space == JCS_GRAYSCALE) 1.563 + cconvert->pub.color_convert = grayscale_convert; 1.564 + else if (cinfo->in_color_space == JCS_RGB || 1.565 + cinfo->in_color_space == JCS_EXT_RGB || 1.566 + cinfo->in_color_space == JCS_EXT_RGBX || 1.567 + cinfo->in_color_space == JCS_EXT_BGR || 1.568 + cinfo->in_color_space == JCS_EXT_BGRX || 1.569 + cinfo->in_color_space == JCS_EXT_XBGR || 1.570 + cinfo->in_color_space == JCS_EXT_XRGB || 1.571 + cinfo->in_color_space == JCS_EXT_RGBA || 1.572 + cinfo->in_color_space == JCS_EXT_BGRA || 1.573 + cinfo->in_color_space == JCS_EXT_ABGR || 1.574 + cinfo->in_color_space == JCS_EXT_ARGB) { 1.575 + if (jsimd_can_rgb_gray()) 1.576 + cconvert->pub.color_convert = jsimd_rgb_gray_convert; 1.577 + else { 1.578 + cconvert->pub.start_pass = rgb_ycc_start; 1.579 + cconvert->pub.color_convert = rgb_gray_convert; 1.580 + } 1.581 + } else if (cinfo->in_color_space == JCS_YCbCr) 1.582 + cconvert->pub.color_convert = grayscale_convert; 1.583 + else 1.584 + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 1.585 + break; 1.586 + 1.587 + case JCS_RGB: 1.588 + if (cinfo->num_components != 3) 1.589 + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 1.590 + if (rgb_red[cinfo->in_color_space] == 0 && 1.591 + rgb_green[cinfo->in_color_space] == 1 && 1.592 + rgb_blue[cinfo->in_color_space] == 2 && 1.593 + rgb_pixelsize[cinfo->in_color_space] == 3) 1.594 + cconvert->pub.color_convert = null_convert; 1.595 + else if (cinfo->in_color_space == JCS_RGB || 1.596 + cinfo->in_color_space == JCS_EXT_RGB || 1.597 + cinfo->in_color_space == JCS_EXT_RGBX || 1.598 + cinfo->in_color_space == JCS_EXT_BGR || 1.599 + cinfo->in_color_space == JCS_EXT_BGRX || 1.600 + cinfo->in_color_space == JCS_EXT_XBGR || 1.601 + cinfo->in_color_space == JCS_EXT_XRGB || 1.602 + cinfo->in_color_space == JCS_EXT_RGBA || 1.603 + cinfo->in_color_space == JCS_EXT_BGRA || 1.604 + cinfo->in_color_space == JCS_EXT_ABGR || 1.605 + cinfo->in_color_space == JCS_EXT_ARGB) 1.606 + cconvert->pub.color_convert = rgb_rgb_convert; 1.607 + else 1.608 + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 1.609 + break; 1.610 + 1.611 + case JCS_YCbCr: 1.612 + if (cinfo->num_components != 3) 1.613 + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 1.614 + if (cinfo->in_color_space == JCS_RGB || 1.615 + cinfo->in_color_space == JCS_EXT_RGB || 1.616 + cinfo->in_color_space == JCS_EXT_RGBX || 1.617 + cinfo->in_color_space == JCS_EXT_BGR || 1.618 + cinfo->in_color_space == JCS_EXT_BGRX || 1.619 + cinfo->in_color_space == JCS_EXT_XBGR || 1.620 + cinfo->in_color_space == JCS_EXT_XRGB || 1.621 + cinfo->in_color_space == JCS_EXT_RGBA || 1.622 + cinfo->in_color_space == JCS_EXT_BGRA || 1.623 + cinfo->in_color_space == JCS_EXT_ABGR || 1.624 + cinfo->in_color_space == JCS_EXT_ARGB) { 1.625 + if (jsimd_can_rgb_ycc()) 1.626 + cconvert->pub.color_convert = jsimd_rgb_ycc_convert; 1.627 + else { 1.628 + cconvert->pub.start_pass = rgb_ycc_start; 1.629 + cconvert->pub.color_convert = rgb_ycc_convert; 1.630 + } 1.631 + } else if (cinfo->in_color_space == JCS_YCbCr) 1.632 + cconvert->pub.color_convert = null_convert; 1.633 + else 1.634 + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 1.635 + break; 1.636 + 1.637 + case JCS_CMYK: 1.638 + if (cinfo->num_components != 4) 1.639 + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 1.640 + if (cinfo->in_color_space == JCS_CMYK) 1.641 + cconvert->pub.color_convert = null_convert; 1.642 + else 1.643 + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 1.644 + break; 1.645 + 1.646 + case JCS_YCCK: 1.647 + if (cinfo->num_components != 4) 1.648 + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 1.649 + if (cinfo->in_color_space == JCS_CMYK) { 1.650 + cconvert->pub.start_pass = rgb_ycc_start; 1.651 + cconvert->pub.color_convert = cmyk_ycck_convert; 1.652 + } else if (cinfo->in_color_space == JCS_YCCK) 1.653 + cconvert->pub.color_convert = null_convert; 1.654 + else 1.655 + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 1.656 + break; 1.657 + 1.658 + default: /* allow null conversion of JCS_UNKNOWN */ 1.659 + if (cinfo->jpeg_color_space != cinfo->in_color_space || 1.660 + cinfo->num_components != cinfo->input_components) 1.661 + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 1.662 + cconvert->pub.color_convert = null_convert; 1.663 + break; 1.664 + } 1.665 +}