dbf-halloween2015
diff libs/libjpeg/jccolor.c @ 1:c3f5c32cb210
barfed all the libraries in the source tree to make porting easier
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 01 Nov 2015 00:36:56 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/libjpeg/jccolor.c Sun Nov 01 00:36:56 2015 +0200 1.3 @@ -0,0 +1,459 @@ 1.4 +/* 1.5 + * jccolor.c 1.6 + * 1.7 + * Copyright (C) 1991-1996, Thomas G. Lane. 1.8 + * This file is part of the Independent JPEG Group's software. 1.9 + * For conditions of distribution and use, see the accompanying README file. 1.10 + * 1.11 + * This file contains input colorspace conversion routines. 1.12 + */ 1.13 + 1.14 +#define JPEG_INTERNALS 1.15 +#include "jinclude.h" 1.16 +#include "jpeglib.h" 1.17 + 1.18 + 1.19 +/* Private subobject */ 1.20 + 1.21 +typedef struct { 1.22 + struct jpeg_color_converter pub; /* public fields */ 1.23 + 1.24 + /* Private state for RGB->YCC conversion */ 1.25 + INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */ 1.26 +} my_color_converter; 1.27 + 1.28 +typedef my_color_converter * my_cconvert_ptr; 1.29 + 1.30 + 1.31 +/**************** RGB -> YCbCr conversion: most common case **************/ 1.32 + 1.33 +/* 1.34 + * YCbCr is defined per CCIR 601-1, except that Cb and Cr are 1.35 + * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5. 1.36 + * The conversion equations to be implemented are therefore 1.37 + * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B 1.38 + * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE 1.39 + * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE 1.40 + * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.) 1.41 + * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2, 1.42 + * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and 1.43 + * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0) 1.44 + * were not represented exactly. Now we sacrifice exact representation of 1.45 + * maximum red and maximum blue in order to get exact grayscales. 1.46 + * 1.47 + * To avoid floating-point arithmetic, we represent the fractional constants 1.48 + * as integers scaled up by 2^16 (about 4 digits precision); we have to divide 1.49 + * the products by 2^16, with appropriate rounding, to get the correct answer. 1.50 + * 1.51 + * For even more speed, we avoid doing any multiplications in the inner loop 1.52 + * by precalculating the constants times R,G,B for all possible values. 1.53 + * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table); 1.54 + * for 12-bit samples it is still acceptable. It's not very reasonable for 1.55 + * 16-bit samples, but if you want lossless storage you shouldn't be changing 1.56 + * colorspace anyway. 1.57 + * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included 1.58 + * in the tables to save adding them separately in the inner loop. 1.59 + */ 1.60 + 1.61 +#define SCALEBITS 16 /* speediest right-shift on some machines */ 1.62 +#define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS) 1.63 +#define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) 1.64 +#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5)) 1.65 + 1.66 +/* We allocate one big table and divide it up into eight parts, instead of 1.67 + * doing eight alloc_small requests. This lets us use a single table base 1.68 + * address, which can be held in a register in the inner loops on many 1.69 + * machines (more than can hold all eight addresses, anyway). 1.70 + */ 1.71 + 1.72 +#define R_Y_OFF 0 /* offset to R => Y section */ 1.73 +#define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */ 1.74 +#define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */ 1.75 +#define R_CB_OFF (3*(MAXJSAMPLE+1)) 1.76 +#define G_CB_OFF (4*(MAXJSAMPLE+1)) 1.77 +#define B_CB_OFF (5*(MAXJSAMPLE+1)) 1.78 +#define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */ 1.79 +#define G_CR_OFF (6*(MAXJSAMPLE+1)) 1.80 +#define B_CR_OFF (7*(MAXJSAMPLE+1)) 1.81 +#define TABLE_SIZE (8*(MAXJSAMPLE+1)) 1.82 + 1.83 + 1.84 +/* 1.85 + * Initialize for RGB->YCC colorspace conversion. 1.86 + */ 1.87 + 1.88 +METHODDEF(void) 1.89 +rgb_ycc_start (j_compress_ptr cinfo) 1.90 +{ 1.91 + my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; 1.92 + INT32 * rgb_ycc_tab; 1.93 + INT32 i; 1.94 + 1.95 + /* Allocate and fill in the conversion tables. */ 1.96 + cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *) 1.97 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.98 + (TABLE_SIZE * SIZEOF(INT32))); 1.99 + 1.100 + for (i = 0; i <= MAXJSAMPLE; i++) { 1.101 + rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i; 1.102 + rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i; 1.103 + rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF; 1.104 + rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i; 1.105 + rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i; 1.106 + /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr. 1.107 + * This ensures that the maximum output will round to MAXJSAMPLE 1.108 + * not MAXJSAMPLE+1, and thus that we don't have to range-limit. 1.109 + */ 1.110 + rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1; 1.111 +/* B=>Cb and R=>Cr tables are the same 1.112 + rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1; 1.113 +*/ 1.114 + rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i; 1.115 + rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i; 1.116 + } 1.117 +} 1.118 + 1.119 + 1.120 +/* 1.121 + * Convert some rows of samples to the JPEG colorspace. 1.122 + * 1.123 + * Note that we change from the application's interleaved-pixel format 1.124 + * to our internal noninterleaved, one-plane-per-component format. 1.125 + * The input buffer is therefore three times as wide as the output buffer. 1.126 + * 1.127 + * A starting row offset is provided only for the output buffer. The caller 1.128 + * can easily adjust the passed input_buf value to accommodate any row 1.129 + * offset required on that side. 1.130 + */ 1.131 + 1.132 +METHODDEF(void) 1.133 +rgb_ycc_convert (j_compress_ptr cinfo, 1.134 + JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 1.135 + JDIMENSION output_row, int num_rows) 1.136 +{ 1.137 + my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; 1.138 + register int r, g, b; 1.139 + register INT32 * ctab = cconvert->rgb_ycc_tab; 1.140 + register JSAMPROW inptr; 1.141 + register JSAMPROW outptr0, outptr1, outptr2; 1.142 + register JDIMENSION col; 1.143 + JDIMENSION num_cols = cinfo->image_width; 1.144 + 1.145 + while (--num_rows >= 0) { 1.146 + inptr = *input_buf++; 1.147 + outptr0 = output_buf[0][output_row]; 1.148 + outptr1 = output_buf[1][output_row]; 1.149 + outptr2 = output_buf[2][output_row]; 1.150 + output_row++; 1.151 + for (col = 0; col < num_cols; col++) { 1.152 + r = GETJSAMPLE(inptr[RGB_RED]); 1.153 + g = GETJSAMPLE(inptr[RGB_GREEN]); 1.154 + b = GETJSAMPLE(inptr[RGB_BLUE]); 1.155 + inptr += RGB_PIXELSIZE; 1.156 + /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations 1.157 + * must be too; we do not need an explicit range-limiting operation. 1.158 + * Hence the value being shifted is never negative, and we don't 1.159 + * need the general RIGHT_SHIFT macro. 1.160 + */ 1.161 + /* Y */ 1.162 + outptr0[col] = (JSAMPLE) 1.163 + ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) 1.164 + >> SCALEBITS); 1.165 + /* Cb */ 1.166 + outptr1[col] = (JSAMPLE) 1.167 + ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) 1.168 + >> SCALEBITS); 1.169 + /* Cr */ 1.170 + outptr2[col] = (JSAMPLE) 1.171 + ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) 1.172 + >> SCALEBITS); 1.173 + } 1.174 + } 1.175 +} 1.176 + 1.177 + 1.178 +/**************** Cases other than RGB -> YCbCr **************/ 1.179 + 1.180 + 1.181 +/* 1.182 + * Convert some rows of samples to the JPEG colorspace. 1.183 + * This version handles RGB->grayscale conversion, which is the same 1.184 + * as the RGB->Y portion of RGB->YCbCr. 1.185 + * We assume rgb_ycc_start has been called (we only use the Y tables). 1.186 + */ 1.187 + 1.188 +METHODDEF(void) 1.189 +rgb_gray_convert (j_compress_ptr cinfo, 1.190 + JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 1.191 + JDIMENSION output_row, int num_rows) 1.192 +{ 1.193 + my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; 1.194 + register int r, g, b; 1.195 + register INT32 * ctab = cconvert->rgb_ycc_tab; 1.196 + register JSAMPROW inptr; 1.197 + register JSAMPROW outptr; 1.198 + register JDIMENSION col; 1.199 + JDIMENSION num_cols = cinfo->image_width; 1.200 + 1.201 + while (--num_rows >= 0) { 1.202 + inptr = *input_buf++; 1.203 + outptr = output_buf[0][output_row]; 1.204 + output_row++; 1.205 + for (col = 0; col < num_cols; col++) { 1.206 + r = GETJSAMPLE(inptr[RGB_RED]); 1.207 + g = GETJSAMPLE(inptr[RGB_GREEN]); 1.208 + b = GETJSAMPLE(inptr[RGB_BLUE]); 1.209 + inptr += RGB_PIXELSIZE; 1.210 + /* Y */ 1.211 + outptr[col] = (JSAMPLE) 1.212 + ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) 1.213 + >> SCALEBITS); 1.214 + } 1.215 + } 1.216 +} 1.217 + 1.218 + 1.219 +/* 1.220 + * Convert some rows of samples to the JPEG colorspace. 1.221 + * This version handles Adobe-style CMYK->YCCK conversion, 1.222 + * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same 1.223 + * conversion as above, while passing K (black) unchanged. 1.224 + * We assume rgb_ycc_start has been called. 1.225 + */ 1.226 + 1.227 +METHODDEF(void) 1.228 +cmyk_ycck_convert (j_compress_ptr cinfo, 1.229 + JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 1.230 + JDIMENSION output_row, int num_rows) 1.231 +{ 1.232 + my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; 1.233 + register int r, g, b; 1.234 + register INT32 * ctab = cconvert->rgb_ycc_tab; 1.235 + register JSAMPROW inptr; 1.236 + register JSAMPROW outptr0, outptr1, outptr2, outptr3; 1.237 + register JDIMENSION col; 1.238 + JDIMENSION num_cols = cinfo->image_width; 1.239 + 1.240 + while (--num_rows >= 0) { 1.241 + inptr = *input_buf++; 1.242 + outptr0 = output_buf[0][output_row]; 1.243 + outptr1 = output_buf[1][output_row]; 1.244 + outptr2 = output_buf[2][output_row]; 1.245 + outptr3 = output_buf[3][output_row]; 1.246 + output_row++; 1.247 + for (col = 0; col < num_cols; col++) { 1.248 + r = MAXJSAMPLE - GETJSAMPLE(inptr[0]); 1.249 + g = MAXJSAMPLE - GETJSAMPLE(inptr[1]); 1.250 + b = MAXJSAMPLE - GETJSAMPLE(inptr[2]); 1.251 + /* K passes through as-is */ 1.252 + outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */ 1.253 + inptr += 4; 1.254 + /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations 1.255 + * must be too; we do not need an explicit range-limiting operation. 1.256 + * Hence the value being shifted is never negative, and we don't 1.257 + * need the general RIGHT_SHIFT macro. 1.258 + */ 1.259 + /* Y */ 1.260 + outptr0[col] = (JSAMPLE) 1.261 + ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) 1.262 + >> SCALEBITS); 1.263 + /* Cb */ 1.264 + outptr1[col] = (JSAMPLE) 1.265 + ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) 1.266 + >> SCALEBITS); 1.267 + /* Cr */ 1.268 + outptr2[col] = (JSAMPLE) 1.269 + ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) 1.270 + >> SCALEBITS); 1.271 + } 1.272 + } 1.273 +} 1.274 + 1.275 + 1.276 +/* 1.277 + * Convert some rows of samples to the JPEG colorspace. 1.278 + * This version handles grayscale output with no conversion. 1.279 + * The source can be either plain grayscale or YCbCr (since Y == gray). 1.280 + */ 1.281 + 1.282 +METHODDEF(void) 1.283 +grayscale_convert (j_compress_ptr cinfo, 1.284 + JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 1.285 + JDIMENSION output_row, int num_rows) 1.286 +{ 1.287 + register JSAMPROW inptr; 1.288 + register JSAMPROW outptr; 1.289 + register JDIMENSION col; 1.290 + JDIMENSION num_cols = cinfo->image_width; 1.291 + int instride = cinfo->input_components; 1.292 + 1.293 + while (--num_rows >= 0) { 1.294 + inptr = *input_buf++; 1.295 + outptr = output_buf[0][output_row]; 1.296 + output_row++; 1.297 + for (col = 0; col < num_cols; col++) { 1.298 + outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */ 1.299 + inptr += instride; 1.300 + } 1.301 + } 1.302 +} 1.303 + 1.304 + 1.305 +/* 1.306 + * Convert some rows of samples to the JPEG colorspace. 1.307 + * This version handles multi-component colorspaces without conversion. 1.308 + * We assume input_components == num_components. 1.309 + */ 1.310 + 1.311 +METHODDEF(void) 1.312 +null_convert (j_compress_ptr cinfo, 1.313 + JSAMPARRAY input_buf, JSAMPIMAGE output_buf, 1.314 + JDIMENSION output_row, int num_rows) 1.315 +{ 1.316 + register JSAMPROW inptr; 1.317 + register JSAMPROW outptr; 1.318 + register JDIMENSION col; 1.319 + register int ci; 1.320 + int nc = cinfo->num_components; 1.321 + JDIMENSION num_cols = cinfo->image_width; 1.322 + 1.323 + while (--num_rows >= 0) { 1.324 + /* It seems fastest to make a separate pass for each component. */ 1.325 + for (ci = 0; ci < nc; ci++) { 1.326 + inptr = *input_buf; 1.327 + outptr = output_buf[ci][output_row]; 1.328 + for (col = 0; col < num_cols; col++) { 1.329 + outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */ 1.330 + inptr += nc; 1.331 + } 1.332 + } 1.333 + input_buf++; 1.334 + output_row++; 1.335 + } 1.336 +} 1.337 + 1.338 + 1.339 +/* 1.340 + * Empty method for start_pass. 1.341 + */ 1.342 + 1.343 +METHODDEF(void) 1.344 +null_method (j_compress_ptr cinfo) 1.345 +{ 1.346 + /* no work needed */ 1.347 +} 1.348 + 1.349 + 1.350 +/* 1.351 + * Module initialization routine for input colorspace conversion. 1.352 + */ 1.353 + 1.354 +GLOBAL(void) 1.355 +jinit_color_converter (j_compress_ptr cinfo) 1.356 +{ 1.357 + my_cconvert_ptr cconvert; 1.358 + 1.359 + cconvert = (my_cconvert_ptr) 1.360 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.361 + SIZEOF(my_color_converter)); 1.362 + cinfo->cconvert = (struct jpeg_color_converter *) cconvert; 1.363 + /* set start_pass to null method until we find out differently */ 1.364 + cconvert->pub.start_pass = null_method; 1.365 + 1.366 + /* Make sure input_components agrees with in_color_space */ 1.367 + switch (cinfo->in_color_space) { 1.368 + case JCS_GRAYSCALE: 1.369 + if (cinfo->input_components != 1) 1.370 + ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 1.371 + break; 1.372 + 1.373 + case JCS_RGB: 1.374 +#if RGB_PIXELSIZE != 3 1.375 + if (cinfo->input_components != RGB_PIXELSIZE) 1.376 + ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 1.377 + break; 1.378 +#endif /* else share code with YCbCr */ 1.379 + 1.380 + case JCS_YCbCr: 1.381 + if (cinfo->input_components != 3) 1.382 + ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 1.383 + break; 1.384 + 1.385 + case JCS_CMYK: 1.386 + case JCS_YCCK: 1.387 + if (cinfo->input_components != 4) 1.388 + ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 1.389 + break; 1.390 + 1.391 + default: /* JCS_UNKNOWN can be anything */ 1.392 + if (cinfo->input_components < 1) 1.393 + ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); 1.394 + break; 1.395 + } 1.396 + 1.397 + /* Check num_components, set conversion method based on requested space */ 1.398 + switch (cinfo->jpeg_color_space) { 1.399 + case JCS_GRAYSCALE: 1.400 + if (cinfo->num_components != 1) 1.401 + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 1.402 + if (cinfo->in_color_space == JCS_GRAYSCALE) 1.403 + cconvert->pub.color_convert = grayscale_convert; 1.404 + else if (cinfo->in_color_space == JCS_RGB) { 1.405 + cconvert->pub.start_pass = rgb_ycc_start; 1.406 + cconvert->pub.color_convert = rgb_gray_convert; 1.407 + } else if (cinfo->in_color_space == JCS_YCbCr) 1.408 + cconvert->pub.color_convert = grayscale_convert; 1.409 + else 1.410 + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 1.411 + break; 1.412 + 1.413 + case JCS_RGB: 1.414 + if (cinfo->num_components != 3) 1.415 + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 1.416 + if (cinfo->in_color_space == JCS_RGB && RGB_PIXELSIZE == 3) 1.417 + cconvert->pub.color_convert = null_convert; 1.418 + else 1.419 + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 1.420 + break; 1.421 + 1.422 + case JCS_YCbCr: 1.423 + if (cinfo->num_components != 3) 1.424 + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 1.425 + if (cinfo->in_color_space == JCS_RGB) { 1.426 + cconvert->pub.start_pass = rgb_ycc_start; 1.427 + cconvert->pub.color_convert = rgb_ycc_convert; 1.428 + } else if (cinfo->in_color_space == JCS_YCbCr) 1.429 + cconvert->pub.color_convert = null_convert; 1.430 + else 1.431 + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 1.432 + break; 1.433 + 1.434 + case JCS_CMYK: 1.435 + if (cinfo->num_components != 4) 1.436 + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 1.437 + if (cinfo->in_color_space == JCS_CMYK) 1.438 + cconvert->pub.color_convert = null_convert; 1.439 + else 1.440 + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 1.441 + break; 1.442 + 1.443 + case JCS_YCCK: 1.444 + if (cinfo->num_components != 4) 1.445 + ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); 1.446 + if (cinfo->in_color_space == JCS_CMYK) { 1.447 + cconvert->pub.start_pass = rgb_ycc_start; 1.448 + cconvert->pub.color_convert = cmyk_ycck_convert; 1.449 + } else if (cinfo->in_color_space == JCS_YCCK) 1.450 + cconvert->pub.color_convert = null_convert; 1.451 + else 1.452 + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 1.453 + break; 1.454 + 1.455 + default: /* allow null conversion of JCS_UNKNOWN */ 1.456 + if (cinfo->jpeg_color_space != cinfo->in_color_space || 1.457 + cinfo->num_components != cinfo->input_components) 1.458 + ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); 1.459 + cconvert->pub.color_convert = null_convert; 1.460 + break; 1.461 + } 1.462 +}