Final 350 project

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Committer:
shoaib_ahmed
Date:
Mon Jul 31 09:16:35 2017 +0000
Revision:
0:791a779d6220
final project;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shoaib_ahmed 0:791a779d6220 1 /*
shoaib_ahmed 0:791a779d6220 2 * jdcolor.c
shoaib_ahmed 0:791a779d6220 3 *
shoaib_ahmed 0:791a779d6220 4 * Copyright (C) 1991-1997, Thomas G. Lane.
shoaib_ahmed 0:791a779d6220 5 * Modified 2011-2015 by Guido Vollbeding.
shoaib_ahmed 0:791a779d6220 6 * This file is part of the Independent JPEG Group's software.
shoaib_ahmed 0:791a779d6220 7 * For conditions of distribution and use, see the accompanying README file.
shoaib_ahmed 0:791a779d6220 8 *
shoaib_ahmed 0:791a779d6220 9 * This file contains output colorspace conversion routines.
shoaib_ahmed 0:791a779d6220 10 */
shoaib_ahmed 0:791a779d6220 11
shoaib_ahmed 0:791a779d6220 12 #define JPEG_INTERNALS
shoaib_ahmed 0:791a779d6220 13 #include "jinclude.h"
shoaib_ahmed 0:791a779d6220 14 #include "jpeglib.h"
shoaib_ahmed 0:791a779d6220 15
shoaib_ahmed 0:791a779d6220 16
shoaib_ahmed 0:791a779d6220 17 /* Private subobject */
shoaib_ahmed 0:791a779d6220 18
shoaib_ahmed 0:791a779d6220 19 typedef struct {
shoaib_ahmed 0:791a779d6220 20 struct jpeg_color_deconverter pub; /* public fields */
shoaib_ahmed 0:791a779d6220 21
shoaib_ahmed 0:791a779d6220 22 /* Private state for YCbCr->RGB and BG_YCC->RGB conversion */
shoaib_ahmed 0:791a779d6220 23 int * Cr_r_tab; /* => table for Cr to R conversion */
shoaib_ahmed 0:791a779d6220 24 int * Cb_b_tab; /* => table for Cb to B conversion */
shoaib_ahmed 0:791a779d6220 25 INT32 * Cr_g_tab; /* => table for Cr to G conversion */
shoaib_ahmed 0:791a779d6220 26 INT32 * Cb_g_tab; /* => table for Cb to G conversion */
shoaib_ahmed 0:791a779d6220 27
shoaib_ahmed 0:791a779d6220 28 /* Private state for RGB->Y conversion */
shoaib_ahmed 0:791a779d6220 29 INT32 * rgb_y_tab; /* => table for RGB to Y conversion */
shoaib_ahmed 0:791a779d6220 30 } my_color_deconverter;
shoaib_ahmed 0:791a779d6220 31
shoaib_ahmed 0:791a779d6220 32 typedef my_color_deconverter * my_cconvert_ptr;
shoaib_ahmed 0:791a779d6220 33
shoaib_ahmed 0:791a779d6220 34
shoaib_ahmed 0:791a779d6220 35 /*************** YCbCr -> RGB conversion: most common case **************/
shoaib_ahmed 0:791a779d6220 36 /*************** BG_YCC -> RGB conversion: less common case **************/
shoaib_ahmed 0:791a779d6220 37 /*************** RGB -> Y conversion: less common case **************/
shoaib_ahmed 0:791a779d6220 38
shoaib_ahmed 0:791a779d6220 39 /*
shoaib_ahmed 0:791a779d6220 40 * YCbCr is defined per Recommendation ITU-R BT.601-7 (03/2011),
shoaib_ahmed 0:791a779d6220 41 * previously known as Recommendation CCIR 601-1, except that Cb and Cr
shoaib_ahmed 0:791a779d6220 42 * are normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
shoaib_ahmed 0:791a779d6220 43 * sRGB (standard RGB color space) is defined per IEC 61966-2-1:1999.
shoaib_ahmed 0:791a779d6220 44 * sYCC (standard luma-chroma-chroma color space with extended gamut)
shoaib_ahmed 0:791a779d6220 45 * is defined per IEC 61966-2-1:1999 Amendment A1:2003 Annex F.
shoaib_ahmed 0:791a779d6220 46 * bg-sRGB and bg-sYCC (big gamut standard color spaces)
shoaib_ahmed 0:791a779d6220 47 * are defined per IEC 61966-2-1:1999 Amendment A1:2003 Annex G.
shoaib_ahmed 0:791a779d6220 48 * Note that the derived conversion coefficients given in some of these
shoaib_ahmed 0:791a779d6220 49 * documents are imprecise. The general conversion equations are
shoaib_ahmed 0:791a779d6220 50 *
shoaib_ahmed 0:791a779d6220 51 * R = Y + K * (1 - Kr) * Cr
shoaib_ahmed 0:791a779d6220 52 * G = Y - K * (Kb * (1 - Kb) * Cb + Kr * (1 - Kr) * Cr) / (1 - Kr - Kb)
shoaib_ahmed 0:791a779d6220 53 * B = Y + K * (1 - Kb) * Cb
shoaib_ahmed 0:791a779d6220 54 *
shoaib_ahmed 0:791a779d6220 55 * Y = Kr * R + (1 - Kr - Kb) * G + Kb * B
shoaib_ahmed 0:791a779d6220 56 *
shoaib_ahmed 0:791a779d6220 57 * With Kr = 0.299 and Kb = 0.114 (derived according to SMPTE RP 177-1993
shoaib_ahmed 0:791a779d6220 58 * from the 1953 FCC NTSC primaries and CIE Illuminant C), K = 2 for sYCC,
shoaib_ahmed 0:791a779d6220 59 * the conversion equations to be implemented are therefore
shoaib_ahmed 0:791a779d6220 60 *
shoaib_ahmed 0:791a779d6220 61 * R = Y + 1.402 * Cr
shoaib_ahmed 0:791a779d6220 62 * G = Y - 0.344136286 * Cb - 0.714136286 * Cr
shoaib_ahmed 0:791a779d6220 63 * B = Y + 1.772 * Cb
shoaib_ahmed 0:791a779d6220 64 *
shoaib_ahmed 0:791a779d6220 65 * Y = 0.299 * R + 0.587 * G + 0.114 * B
shoaib_ahmed 0:791a779d6220 66 *
shoaib_ahmed 0:791a779d6220 67 * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
shoaib_ahmed 0:791a779d6220 68 * For bg-sYCC, with K = 4, the equations are
shoaib_ahmed 0:791a779d6220 69 *
shoaib_ahmed 0:791a779d6220 70 * R = Y + 2.804 * Cr
shoaib_ahmed 0:791a779d6220 71 * G = Y - 0.688272572 * Cb - 1.428272572 * Cr
shoaib_ahmed 0:791a779d6220 72 * B = Y + 3.544 * Cb
shoaib_ahmed 0:791a779d6220 73 *
shoaib_ahmed 0:791a779d6220 74 * To avoid floating-point arithmetic, we represent the fractional constants
shoaib_ahmed 0:791a779d6220 75 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
shoaib_ahmed 0:791a779d6220 76 * the products by 2^16, with appropriate rounding, to get the correct answer.
shoaib_ahmed 0:791a779d6220 77 * Notice that Y, being an integral input, does not contribute any fraction
shoaib_ahmed 0:791a779d6220 78 * so it need not participate in the rounding.
shoaib_ahmed 0:791a779d6220 79 *
shoaib_ahmed 0:791a779d6220 80 * For even more speed, we avoid doing any multiplications in the inner loop
shoaib_ahmed 0:791a779d6220 81 * by precalculating the constants times Cb and Cr for all possible values.
shoaib_ahmed 0:791a779d6220 82 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
shoaib_ahmed 0:791a779d6220 83 * for 9-bit to 12-bit samples it is still acceptable. It's not very
shoaib_ahmed 0:791a779d6220 84 * reasonable for 16-bit samples, but if you want lossless storage you
shoaib_ahmed 0:791a779d6220 85 * shouldn't be changing colorspace anyway.
shoaib_ahmed 0:791a779d6220 86 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
shoaib_ahmed 0:791a779d6220 87 * values for the G calculation are left scaled up, since we must add them
shoaib_ahmed 0:791a779d6220 88 * together before rounding.
shoaib_ahmed 0:791a779d6220 89 */
shoaib_ahmed 0:791a779d6220 90
shoaib_ahmed 0:791a779d6220 91 #define SCALEBITS 16 /* speediest right-shift on some machines */
shoaib_ahmed 0:791a779d6220 92 #define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
shoaib_ahmed 0:791a779d6220 93 #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
shoaib_ahmed 0:791a779d6220 94
shoaib_ahmed 0:791a779d6220 95 /* We allocate one big table for RGB->Y conversion and divide it up into
shoaib_ahmed 0:791a779d6220 96 * three parts, instead of doing three alloc_small requests. This lets us
shoaib_ahmed 0:791a779d6220 97 * use a single table base address, which can be held in a register in the
shoaib_ahmed 0:791a779d6220 98 * inner loops on many machines (more than can hold all three addresses,
shoaib_ahmed 0:791a779d6220 99 * anyway).
shoaib_ahmed 0:791a779d6220 100 */
shoaib_ahmed 0:791a779d6220 101
shoaib_ahmed 0:791a779d6220 102 #define R_Y_OFF 0 /* offset to R => Y section */
shoaib_ahmed 0:791a779d6220 103 #define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
shoaib_ahmed 0:791a779d6220 104 #define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
shoaib_ahmed 0:791a779d6220 105 #define TABLE_SIZE (3*(MAXJSAMPLE+1))
shoaib_ahmed 0:791a779d6220 106
shoaib_ahmed 0:791a779d6220 107
shoaib_ahmed 0:791a779d6220 108 /*
shoaib_ahmed 0:791a779d6220 109 * Initialize tables for YCbCr->RGB and BG_YCC->RGB colorspace conversion.
shoaib_ahmed 0:791a779d6220 110 */
shoaib_ahmed 0:791a779d6220 111
shoaib_ahmed 0:791a779d6220 112 LOCAL(void)
shoaib_ahmed 0:791a779d6220 113 build_ycc_rgb_table (j_decompress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 114 /* Normal case, sYCC */
shoaib_ahmed 0:791a779d6220 115 {
shoaib_ahmed 0:791a779d6220 116 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
shoaib_ahmed 0:791a779d6220 117 int i;
shoaib_ahmed 0:791a779d6220 118 INT32 x;
shoaib_ahmed 0:791a779d6220 119 SHIFT_TEMPS
shoaib_ahmed 0:791a779d6220 120
shoaib_ahmed 0:791a779d6220 121 cconvert->Cr_r_tab = (int *)
shoaib_ahmed 0:791a779d6220 122 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 123 (MAXJSAMPLE+1) * SIZEOF(int));
shoaib_ahmed 0:791a779d6220 124 cconvert->Cb_b_tab = (int *)
shoaib_ahmed 0:791a779d6220 125 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 126 (MAXJSAMPLE+1) * SIZEOF(int));
shoaib_ahmed 0:791a779d6220 127 cconvert->Cr_g_tab = (INT32 *)
shoaib_ahmed 0:791a779d6220 128 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 129 (MAXJSAMPLE+1) * SIZEOF(INT32));
shoaib_ahmed 0:791a779d6220 130 cconvert->Cb_g_tab = (INT32 *)
shoaib_ahmed 0:791a779d6220 131 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 132 (MAXJSAMPLE+1) * SIZEOF(INT32));
shoaib_ahmed 0:791a779d6220 133
shoaib_ahmed 0:791a779d6220 134 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
shoaib_ahmed 0:791a779d6220 135 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
shoaib_ahmed 0:791a779d6220 136 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
shoaib_ahmed 0:791a779d6220 137 /* Cr=>R value is nearest int to 1.402 * x */
shoaib_ahmed 0:791a779d6220 138 cconvert->Cr_r_tab[i] = (int)
shoaib_ahmed 0:791a779d6220 139 RIGHT_SHIFT(FIX(1.402) * x + ONE_HALF, SCALEBITS);
shoaib_ahmed 0:791a779d6220 140 /* Cb=>B value is nearest int to 1.772 * x */
shoaib_ahmed 0:791a779d6220 141 cconvert->Cb_b_tab[i] = (int)
shoaib_ahmed 0:791a779d6220 142 RIGHT_SHIFT(FIX(1.772) * x + ONE_HALF, SCALEBITS);
shoaib_ahmed 0:791a779d6220 143 /* Cr=>G value is scaled-up -0.714136286 * x */
shoaib_ahmed 0:791a779d6220 144 cconvert->Cr_g_tab[i] = (- FIX(0.714136286)) * x;
shoaib_ahmed 0:791a779d6220 145 /* Cb=>G value is scaled-up -0.344136286 * x */
shoaib_ahmed 0:791a779d6220 146 /* We also add in ONE_HALF so that need not do it in inner loop */
shoaib_ahmed 0:791a779d6220 147 cconvert->Cb_g_tab[i] = (- FIX(0.344136286)) * x + ONE_HALF;
shoaib_ahmed 0:791a779d6220 148 }
shoaib_ahmed 0:791a779d6220 149 }
shoaib_ahmed 0:791a779d6220 150
shoaib_ahmed 0:791a779d6220 151
shoaib_ahmed 0:791a779d6220 152 LOCAL(void)
shoaib_ahmed 0:791a779d6220 153 build_bg_ycc_rgb_table (j_decompress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 154 /* Wide gamut case, bg-sYCC */
shoaib_ahmed 0:791a779d6220 155 {
shoaib_ahmed 0:791a779d6220 156 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
shoaib_ahmed 0:791a779d6220 157 int i;
shoaib_ahmed 0:791a779d6220 158 INT32 x;
shoaib_ahmed 0:791a779d6220 159 SHIFT_TEMPS
shoaib_ahmed 0:791a779d6220 160
shoaib_ahmed 0:791a779d6220 161 cconvert->Cr_r_tab = (int *)
shoaib_ahmed 0:791a779d6220 162 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 163 (MAXJSAMPLE+1) * SIZEOF(int));
shoaib_ahmed 0:791a779d6220 164 cconvert->Cb_b_tab = (int *)
shoaib_ahmed 0:791a779d6220 165 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 166 (MAXJSAMPLE+1) * SIZEOF(int));
shoaib_ahmed 0:791a779d6220 167 cconvert->Cr_g_tab = (INT32 *)
shoaib_ahmed 0:791a779d6220 168 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 169 (MAXJSAMPLE+1) * SIZEOF(INT32));
shoaib_ahmed 0:791a779d6220 170 cconvert->Cb_g_tab = (INT32 *)
shoaib_ahmed 0:791a779d6220 171 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 172 (MAXJSAMPLE+1) * SIZEOF(INT32));
shoaib_ahmed 0:791a779d6220 173
shoaib_ahmed 0:791a779d6220 174 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
shoaib_ahmed 0:791a779d6220 175 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
shoaib_ahmed 0:791a779d6220 176 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
shoaib_ahmed 0:791a779d6220 177 /* Cr=>R value is nearest int to 2.804 * x */
shoaib_ahmed 0:791a779d6220 178 cconvert->Cr_r_tab[i] = (int)
shoaib_ahmed 0:791a779d6220 179 RIGHT_SHIFT(FIX(2.804) * x + ONE_HALF, SCALEBITS);
shoaib_ahmed 0:791a779d6220 180 /* Cb=>B value is nearest int to 3.544 * x */
shoaib_ahmed 0:791a779d6220 181 cconvert->Cb_b_tab[i] = (int)
shoaib_ahmed 0:791a779d6220 182 RIGHT_SHIFT(FIX(3.544) * x + ONE_HALF, SCALEBITS);
shoaib_ahmed 0:791a779d6220 183 /* Cr=>G value is scaled-up -1.428272572 * x */
shoaib_ahmed 0:791a779d6220 184 cconvert->Cr_g_tab[i] = (- FIX(1.428272572)) * x;
shoaib_ahmed 0:791a779d6220 185 /* Cb=>G value is scaled-up -0.688272572 * x */
shoaib_ahmed 0:791a779d6220 186 /* We also add in ONE_HALF so that need not do it in inner loop */
shoaib_ahmed 0:791a779d6220 187 cconvert->Cb_g_tab[i] = (- FIX(0.688272572)) * x + ONE_HALF;
shoaib_ahmed 0:791a779d6220 188 }
shoaib_ahmed 0:791a779d6220 189 }
shoaib_ahmed 0:791a779d6220 190
shoaib_ahmed 0:791a779d6220 191
shoaib_ahmed 0:791a779d6220 192 /*
shoaib_ahmed 0:791a779d6220 193 * Convert some rows of samples to the output colorspace.
shoaib_ahmed 0:791a779d6220 194 *
shoaib_ahmed 0:791a779d6220 195 * Note that we change from noninterleaved, one-plane-per-component format
shoaib_ahmed 0:791a779d6220 196 * to interleaved-pixel format. The output buffer is therefore three times
shoaib_ahmed 0:791a779d6220 197 * as wide as the input buffer.
shoaib_ahmed 0:791a779d6220 198 * A starting row offset is provided only for the input buffer. The caller
shoaib_ahmed 0:791a779d6220 199 * can easily adjust the passed output_buf value to accommodate any row
shoaib_ahmed 0:791a779d6220 200 * offset required on that side.
shoaib_ahmed 0:791a779d6220 201 */
shoaib_ahmed 0:791a779d6220 202
shoaib_ahmed 0:791a779d6220 203 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 204 ycc_rgb_convert (j_decompress_ptr cinfo,
shoaib_ahmed 0:791a779d6220 205 JSAMPIMAGE input_buf, JDIMENSION input_row,
shoaib_ahmed 0:791a779d6220 206 JSAMPARRAY output_buf, int num_rows)
shoaib_ahmed 0:791a779d6220 207 {
shoaib_ahmed 0:791a779d6220 208 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
shoaib_ahmed 0:791a779d6220 209 register int y, cb, cr;
shoaib_ahmed 0:791a779d6220 210 register JSAMPROW outptr;
shoaib_ahmed 0:791a779d6220 211 register JSAMPROW inptr0, inptr1, inptr2;
shoaib_ahmed 0:791a779d6220 212 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 213 JDIMENSION num_cols = cinfo->output_width;
shoaib_ahmed 0:791a779d6220 214 /* copy these pointers into registers if possible */
shoaib_ahmed 0:791a779d6220 215 register JSAMPLE * range_limit = cinfo->sample_range_limit;
shoaib_ahmed 0:791a779d6220 216 register int * Crrtab = cconvert->Cr_r_tab;
shoaib_ahmed 0:791a779d6220 217 register int * Cbbtab = cconvert->Cb_b_tab;
shoaib_ahmed 0:791a779d6220 218 register INT32 * Crgtab = cconvert->Cr_g_tab;
shoaib_ahmed 0:791a779d6220 219 register INT32 * Cbgtab = cconvert->Cb_g_tab;
shoaib_ahmed 0:791a779d6220 220 SHIFT_TEMPS
shoaib_ahmed 0:791a779d6220 221
shoaib_ahmed 0:791a779d6220 222 while (--num_rows >= 0) {
shoaib_ahmed 0:791a779d6220 223 inptr0 = input_buf[0][input_row];
shoaib_ahmed 0:791a779d6220 224 inptr1 = input_buf[1][input_row];
shoaib_ahmed 0:791a779d6220 225 inptr2 = input_buf[2][input_row];
shoaib_ahmed 0:791a779d6220 226 input_row++;
shoaib_ahmed 0:791a779d6220 227 outptr = *output_buf++;
shoaib_ahmed 0:791a779d6220 228 for (col = 0; col < num_cols; col++) {
shoaib_ahmed 0:791a779d6220 229 y = GETJSAMPLE(inptr0[col]);
shoaib_ahmed 0:791a779d6220 230 cb = GETJSAMPLE(inptr1[col]);
shoaib_ahmed 0:791a779d6220 231 cr = GETJSAMPLE(inptr2[col]);
shoaib_ahmed 0:791a779d6220 232 /* Range-limiting is essential due to noise introduced by DCT losses,
shoaib_ahmed 0:791a779d6220 233 * for extended gamut (sYCC) and wide gamut (bg-sYCC) encodings.
shoaib_ahmed 0:791a779d6220 234 */
shoaib_ahmed 0:791a779d6220 235 outptr[RGB_RED] = range_limit[y + Crrtab[cr]];
shoaib_ahmed 0:791a779d6220 236 outptr[RGB_GREEN] = range_limit[y +
shoaib_ahmed 0:791a779d6220 237 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
shoaib_ahmed 0:791a779d6220 238 SCALEBITS))];
shoaib_ahmed 0:791a779d6220 239 outptr[RGB_BLUE] = range_limit[y + Cbbtab[cb]];
shoaib_ahmed 0:791a779d6220 240 outptr += RGB_PIXELSIZE;
shoaib_ahmed 0:791a779d6220 241 }
shoaib_ahmed 0:791a779d6220 242 }
shoaib_ahmed 0:791a779d6220 243 }
shoaib_ahmed 0:791a779d6220 244
shoaib_ahmed 0:791a779d6220 245
shoaib_ahmed 0:791a779d6220 246 /**************** Cases other than YCC -> RGB ****************/
shoaib_ahmed 0:791a779d6220 247
shoaib_ahmed 0:791a779d6220 248
shoaib_ahmed 0:791a779d6220 249 /*
shoaib_ahmed 0:791a779d6220 250 * Initialize for RGB->grayscale colorspace conversion.
shoaib_ahmed 0:791a779d6220 251 */
shoaib_ahmed 0:791a779d6220 252
shoaib_ahmed 0:791a779d6220 253 LOCAL(void)
shoaib_ahmed 0:791a779d6220 254 build_rgb_y_table (j_decompress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 255 {
shoaib_ahmed 0:791a779d6220 256 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
shoaib_ahmed 0:791a779d6220 257 INT32 * rgb_y_tab;
shoaib_ahmed 0:791a779d6220 258 INT32 i;
shoaib_ahmed 0:791a779d6220 259
shoaib_ahmed 0:791a779d6220 260 /* Allocate and fill in the conversion tables. */
shoaib_ahmed 0:791a779d6220 261 cconvert->rgb_y_tab = rgb_y_tab = (INT32 *)
shoaib_ahmed 0:791a779d6220 262 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 263 (TABLE_SIZE * SIZEOF(INT32)));
shoaib_ahmed 0:791a779d6220 264
shoaib_ahmed 0:791a779d6220 265 for (i = 0; i <= MAXJSAMPLE; i++) {
shoaib_ahmed 0:791a779d6220 266 rgb_y_tab[i+R_Y_OFF] = FIX(0.299) * i;
shoaib_ahmed 0:791a779d6220 267 rgb_y_tab[i+G_Y_OFF] = FIX(0.587) * i;
shoaib_ahmed 0:791a779d6220 268 rgb_y_tab[i+B_Y_OFF] = FIX(0.114) * i + ONE_HALF;
shoaib_ahmed 0:791a779d6220 269 }
shoaib_ahmed 0:791a779d6220 270 }
shoaib_ahmed 0:791a779d6220 271
shoaib_ahmed 0:791a779d6220 272
shoaib_ahmed 0:791a779d6220 273 /*
shoaib_ahmed 0:791a779d6220 274 * Convert RGB to grayscale.
shoaib_ahmed 0:791a779d6220 275 */
shoaib_ahmed 0:791a779d6220 276
shoaib_ahmed 0:791a779d6220 277 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 278 rgb_gray_convert (j_decompress_ptr cinfo,
shoaib_ahmed 0:791a779d6220 279 JSAMPIMAGE input_buf, JDIMENSION input_row,
shoaib_ahmed 0:791a779d6220 280 JSAMPARRAY output_buf, int num_rows)
shoaib_ahmed 0:791a779d6220 281 {
shoaib_ahmed 0:791a779d6220 282 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
shoaib_ahmed 0:791a779d6220 283 register INT32 * ctab = cconvert->rgb_y_tab;
shoaib_ahmed 0:791a779d6220 284 register int r, g, b;
shoaib_ahmed 0:791a779d6220 285 register JSAMPROW outptr;
shoaib_ahmed 0:791a779d6220 286 register JSAMPROW inptr0, inptr1, inptr2;
shoaib_ahmed 0:791a779d6220 287 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 288 JDIMENSION num_cols = cinfo->output_width;
shoaib_ahmed 0:791a779d6220 289
shoaib_ahmed 0:791a779d6220 290 while (--num_rows >= 0) {
shoaib_ahmed 0:791a779d6220 291 inptr0 = input_buf[0][input_row];
shoaib_ahmed 0:791a779d6220 292 inptr1 = input_buf[1][input_row];
shoaib_ahmed 0:791a779d6220 293 inptr2 = input_buf[2][input_row];
shoaib_ahmed 0:791a779d6220 294 input_row++;
shoaib_ahmed 0:791a779d6220 295 outptr = *output_buf++;
shoaib_ahmed 0:791a779d6220 296 for (col = 0; col < num_cols; col++) {
shoaib_ahmed 0:791a779d6220 297 r = GETJSAMPLE(inptr0[col]);
shoaib_ahmed 0:791a779d6220 298 g = GETJSAMPLE(inptr1[col]);
shoaib_ahmed 0:791a779d6220 299 b = GETJSAMPLE(inptr2[col]);
shoaib_ahmed 0:791a779d6220 300 /* Y */
shoaib_ahmed 0:791a779d6220 301 outptr[col] = (JSAMPLE)
shoaib_ahmed 0:791a779d6220 302 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
shoaib_ahmed 0:791a779d6220 303 >> SCALEBITS);
shoaib_ahmed 0:791a779d6220 304 }
shoaib_ahmed 0:791a779d6220 305 }
shoaib_ahmed 0:791a779d6220 306 }
shoaib_ahmed 0:791a779d6220 307
shoaib_ahmed 0:791a779d6220 308
shoaib_ahmed 0:791a779d6220 309 /*
shoaib_ahmed 0:791a779d6220 310 * [R-G,G,B-G] to [R,G,B] conversion with modulo calculation
shoaib_ahmed 0:791a779d6220 311 * (inverse color transform).
shoaib_ahmed 0:791a779d6220 312 * This can be seen as an adaption of the general YCbCr->RGB
shoaib_ahmed 0:791a779d6220 313 * conversion equation with Kr = Kb = 0, while replacing the
shoaib_ahmed 0:791a779d6220 314 * normalization by modulo calculation.
shoaib_ahmed 0:791a779d6220 315 */
shoaib_ahmed 0:791a779d6220 316
shoaib_ahmed 0:791a779d6220 317 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 318 rgb1_rgb_convert (j_decompress_ptr cinfo,
shoaib_ahmed 0:791a779d6220 319 JSAMPIMAGE input_buf, JDIMENSION input_row,
shoaib_ahmed 0:791a779d6220 320 JSAMPARRAY output_buf, int num_rows)
shoaib_ahmed 0:791a779d6220 321 {
shoaib_ahmed 0:791a779d6220 322 register int r, g, b;
shoaib_ahmed 0:791a779d6220 323 register JSAMPROW outptr;
shoaib_ahmed 0:791a779d6220 324 register JSAMPROW inptr0, inptr1, inptr2;
shoaib_ahmed 0:791a779d6220 325 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 326 JDIMENSION num_cols = cinfo->output_width;
shoaib_ahmed 0:791a779d6220 327
shoaib_ahmed 0:791a779d6220 328 while (--num_rows >= 0) {
shoaib_ahmed 0:791a779d6220 329 inptr0 = input_buf[0][input_row];
shoaib_ahmed 0:791a779d6220 330 inptr1 = input_buf[1][input_row];
shoaib_ahmed 0:791a779d6220 331 inptr2 = input_buf[2][input_row];
shoaib_ahmed 0:791a779d6220 332 input_row++;
shoaib_ahmed 0:791a779d6220 333 outptr = *output_buf++;
shoaib_ahmed 0:791a779d6220 334 for (col = 0; col < num_cols; col++) {
shoaib_ahmed 0:791a779d6220 335 r = GETJSAMPLE(inptr0[col]);
shoaib_ahmed 0:791a779d6220 336 g = GETJSAMPLE(inptr1[col]);
shoaib_ahmed 0:791a779d6220 337 b = GETJSAMPLE(inptr2[col]);
shoaib_ahmed 0:791a779d6220 338 /* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD
shoaib_ahmed 0:791a779d6220 339 * (modulo) operator is equivalent to the bitmask operator AND.
shoaib_ahmed 0:791a779d6220 340 */
shoaib_ahmed 0:791a779d6220 341 outptr[RGB_RED] = (JSAMPLE) ((r + g - CENTERJSAMPLE) & MAXJSAMPLE);
shoaib_ahmed 0:791a779d6220 342 outptr[RGB_GREEN] = (JSAMPLE) g;
shoaib_ahmed 0:791a779d6220 343 outptr[RGB_BLUE] = (JSAMPLE) ((b + g - CENTERJSAMPLE) & MAXJSAMPLE);
shoaib_ahmed 0:791a779d6220 344 outptr += RGB_PIXELSIZE;
shoaib_ahmed 0:791a779d6220 345 }
shoaib_ahmed 0:791a779d6220 346 }
shoaib_ahmed 0:791a779d6220 347 }
shoaib_ahmed 0:791a779d6220 348
shoaib_ahmed 0:791a779d6220 349
shoaib_ahmed 0:791a779d6220 350 /*
shoaib_ahmed 0:791a779d6220 351 * [R-G,G,B-G] to grayscale conversion with modulo calculation
shoaib_ahmed 0:791a779d6220 352 * (inverse color transform).
shoaib_ahmed 0:791a779d6220 353 */
shoaib_ahmed 0:791a779d6220 354
shoaib_ahmed 0:791a779d6220 355 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 356 rgb1_gray_convert (j_decompress_ptr cinfo,
shoaib_ahmed 0:791a779d6220 357 JSAMPIMAGE input_buf, JDIMENSION input_row,
shoaib_ahmed 0:791a779d6220 358 JSAMPARRAY output_buf, int num_rows)
shoaib_ahmed 0:791a779d6220 359 {
shoaib_ahmed 0:791a779d6220 360 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
shoaib_ahmed 0:791a779d6220 361 register INT32 * ctab = cconvert->rgb_y_tab;
shoaib_ahmed 0:791a779d6220 362 register int r, g, b;
shoaib_ahmed 0:791a779d6220 363 register JSAMPROW outptr;
shoaib_ahmed 0:791a779d6220 364 register JSAMPROW inptr0, inptr1, inptr2;
shoaib_ahmed 0:791a779d6220 365 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 366 JDIMENSION num_cols = cinfo->output_width;
shoaib_ahmed 0:791a779d6220 367
shoaib_ahmed 0:791a779d6220 368 while (--num_rows >= 0) {
shoaib_ahmed 0:791a779d6220 369 inptr0 = input_buf[0][input_row];
shoaib_ahmed 0:791a779d6220 370 inptr1 = input_buf[1][input_row];
shoaib_ahmed 0:791a779d6220 371 inptr2 = input_buf[2][input_row];
shoaib_ahmed 0:791a779d6220 372 input_row++;
shoaib_ahmed 0:791a779d6220 373 outptr = *output_buf++;
shoaib_ahmed 0:791a779d6220 374 for (col = 0; col < num_cols; col++) {
shoaib_ahmed 0:791a779d6220 375 r = GETJSAMPLE(inptr0[col]);
shoaib_ahmed 0:791a779d6220 376 g = GETJSAMPLE(inptr1[col]);
shoaib_ahmed 0:791a779d6220 377 b = GETJSAMPLE(inptr2[col]);
shoaib_ahmed 0:791a779d6220 378 /* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD
shoaib_ahmed 0:791a779d6220 379 * (modulo) operator is equivalent to the bitmask operator AND.
shoaib_ahmed 0:791a779d6220 380 */
shoaib_ahmed 0:791a779d6220 381 r = (r + g - CENTERJSAMPLE) & MAXJSAMPLE;
shoaib_ahmed 0:791a779d6220 382 b = (b + g - CENTERJSAMPLE) & MAXJSAMPLE;
shoaib_ahmed 0:791a779d6220 383 /* Y */
shoaib_ahmed 0:791a779d6220 384 outptr[col] = (JSAMPLE)
shoaib_ahmed 0:791a779d6220 385 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
shoaib_ahmed 0:791a779d6220 386 >> SCALEBITS);
shoaib_ahmed 0:791a779d6220 387 }
shoaib_ahmed 0:791a779d6220 388 }
shoaib_ahmed 0:791a779d6220 389 }
shoaib_ahmed 0:791a779d6220 390
shoaib_ahmed 0:791a779d6220 391
shoaib_ahmed 0:791a779d6220 392 /*
shoaib_ahmed 0:791a779d6220 393 * No colorspace change, but conversion from separate-planes
shoaib_ahmed 0:791a779d6220 394 * to interleaved representation.
shoaib_ahmed 0:791a779d6220 395 */
shoaib_ahmed 0:791a779d6220 396
shoaib_ahmed 0:791a779d6220 397 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 398 rgb_convert (j_decompress_ptr cinfo,
shoaib_ahmed 0:791a779d6220 399 JSAMPIMAGE input_buf, JDIMENSION input_row,
shoaib_ahmed 0:791a779d6220 400 JSAMPARRAY output_buf, int num_rows)
shoaib_ahmed 0:791a779d6220 401 {
shoaib_ahmed 0:791a779d6220 402 register JSAMPROW outptr;
shoaib_ahmed 0:791a779d6220 403 register JSAMPROW inptr0, inptr1, inptr2;
shoaib_ahmed 0:791a779d6220 404 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 405 JDIMENSION num_cols = cinfo->output_width;
shoaib_ahmed 0:791a779d6220 406
shoaib_ahmed 0:791a779d6220 407 while (--num_rows >= 0) {
shoaib_ahmed 0:791a779d6220 408 inptr0 = input_buf[0][input_row];
shoaib_ahmed 0:791a779d6220 409 inptr1 = input_buf[1][input_row];
shoaib_ahmed 0:791a779d6220 410 inptr2 = input_buf[2][input_row];
shoaib_ahmed 0:791a779d6220 411 input_row++;
shoaib_ahmed 0:791a779d6220 412 outptr = *output_buf++;
shoaib_ahmed 0:791a779d6220 413 for (col = 0; col < num_cols; col++) {
shoaib_ahmed 0:791a779d6220 414 /* We can dispense with GETJSAMPLE() here */
shoaib_ahmed 0:791a779d6220 415 outptr[RGB_RED] = inptr0[col];
shoaib_ahmed 0:791a779d6220 416 outptr[RGB_GREEN] = inptr1[col];
shoaib_ahmed 0:791a779d6220 417 outptr[RGB_BLUE] = inptr2[col];
shoaib_ahmed 0:791a779d6220 418 outptr += RGB_PIXELSIZE;
shoaib_ahmed 0:791a779d6220 419 }
shoaib_ahmed 0:791a779d6220 420 }
shoaib_ahmed 0:791a779d6220 421 }
shoaib_ahmed 0:791a779d6220 422
shoaib_ahmed 0:791a779d6220 423
shoaib_ahmed 0:791a779d6220 424 /*
shoaib_ahmed 0:791a779d6220 425 * Color conversion for no colorspace change: just copy the data,
shoaib_ahmed 0:791a779d6220 426 * converting from separate-planes to interleaved representation.
shoaib_ahmed 0:791a779d6220 427 */
shoaib_ahmed 0:791a779d6220 428
shoaib_ahmed 0:791a779d6220 429 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 430 null_convert (j_decompress_ptr cinfo,
shoaib_ahmed 0:791a779d6220 431 JSAMPIMAGE input_buf, JDIMENSION input_row,
shoaib_ahmed 0:791a779d6220 432 JSAMPARRAY output_buf, int num_rows)
shoaib_ahmed 0:791a779d6220 433 {
shoaib_ahmed 0:791a779d6220 434 int ci;
shoaib_ahmed 0:791a779d6220 435 register int nc = cinfo->num_components;
shoaib_ahmed 0:791a779d6220 436 register JSAMPROW outptr;
shoaib_ahmed 0:791a779d6220 437 register JSAMPROW inptr;
shoaib_ahmed 0:791a779d6220 438 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 439 JDIMENSION num_cols = cinfo->output_width;
shoaib_ahmed 0:791a779d6220 440
shoaib_ahmed 0:791a779d6220 441 while (--num_rows >= 0) {
shoaib_ahmed 0:791a779d6220 442 for (ci = 0; ci < nc; ci++) {
shoaib_ahmed 0:791a779d6220 443 inptr = input_buf[ci][input_row];
shoaib_ahmed 0:791a779d6220 444 outptr = output_buf[0] + ci;
shoaib_ahmed 0:791a779d6220 445 for (col = 0; col < num_cols; col++) {
shoaib_ahmed 0:791a779d6220 446 *outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */
shoaib_ahmed 0:791a779d6220 447 outptr += nc;
shoaib_ahmed 0:791a779d6220 448 }
shoaib_ahmed 0:791a779d6220 449 }
shoaib_ahmed 0:791a779d6220 450 input_row++;
shoaib_ahmed 0:791a779d6220 451 output_buf++;
shoaib_ahmed 0:791a779d6220 452 }
shoaib_ahmed 0:791a779d6220 453 }
shoaib_ahmed 0:791a779d6220 454
shoaib_ahmed 0:791a779d6220 455
shoaib_ahmed 0:791a779d6220 456 /*
shoaib_ahmed 0:791a779d6220 457 * Color conversion for grayscale: just copy the data.
shoaib_ahmed 0:791a779d6220 458 * This also works for YCC -> grayscale conversion, in which
shoaib_ahmed 0:791a779d6220 459 * we just copy the Y (luminance) component and ignore chrominance.
shoaib_ahmed 0:791a779d6220 460 */
shoaib_ahmed 0:791a779d6220 461
shoaib_ahmed 0:791a779d6220 462 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 463 grayscale_convert (j_decompress_ptr cinfo,
shoaib_ahmed 0:791a779d6220 464 JSAMPIMAGE input_buf, JDIMENSION input_row,
shoaib_ahmed 0:791a779d6220 465 JSAMPARRAY output_buf, int num_rows)
shoaib_ahmed 0:791a779d6220 466 {
shoaib_ahmed 0:791a779d6220 467 jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
shoaib_ahmed 0:791a779d6220 468 num_rows, cinfo->output_width);
shoaib_ahmed 0:791a779d6220 469 }
shoaib_ahmed 0:791a779d6220 470
shoaib_ahmed 0:791a779d6220 471
shoaib_ahmed 0:791a779d6220 472 /*
shoaib_ahmed 0:791a779d6220 473 * Convert grayscale to RGB: just duplicate the graylevel three times.
shoaib_ahmed 0:791a779d6220 474 * This is provided to support applications that don't want to cope
shoaib_ahmed 0:791a779d6220 475 * with grayscale as a separate case.
shoaib_ahmed 0:791a779d6220 476 */
shoaib_ahmed 0:791a779d6220 477
shoaib_ahmed 0:791a779d6220 478 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 479 gray_rgb_convert (j_decompress_ptr cinfo,
shoaib_ahmed 0:791a779d6220 480 JSAMPIMAGE input_buf, JDIMENSION input_row,
shoaib_ahmed 0:791a779d6220 481 JSAMPARRAY output_buf, int num_rows)
shoaib_ahmed 0:791a779d6220 482 {
shoaib_ahmed 0:791a779d6220 483 register JSAMPROW outptr;
shoaib_ahmed 0:791a779d6220 484 register JSAMPROW inptr;
shoaib_ahmed 0:791a779d6220 485 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 486 JDIMENSION num_cols = cinfo->output_width;
shoaib_ahmed 0:791a779d6220 487
shoaib_ahmed 0:791a779d6220 488 while (--num_rows >= 0) {
shoaib_ahmed 0:791a779d6220 489 inptr = input_buf[0][input_row++];
shoaib_ahmed 0:791a779d6220 490 outptr = *output_buf++;
shoaib_ahmed 0:791a779d6220 491 for (col = 0; col < num_cols; col++) {
shoaib_ahmed 0:791a779d6220 492 /* We can dispense with GETJSAMPLE() here */
shoaib_ahmed 0:791a779d6220 493 outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
shoaib_ahmed 0:791a779d6220 494 outptr += RGB_PIXELSIZE;
shoaib_ahmed 0:791a779d6220 495 }
shoaib_ahmed 0:791a779d6220 496 }
shoaib_ahmed 0:791a779d6220 497 }
shoaib_ahmed 0:791a779d6220 498
shoaib_ahmed 0:791a779d6220 499
shoaib_ahmed 0:791a779d6220 500 /*
shoaib_ahmed 0:791a779d6220 501 * Adobe-style YCCK->CMYK conversion.
shoaib_ahmed 0:791a779d6220 502 * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
shoaib_ahmed 0:791a779d6220 503 * conversion as above, while passing K (black) unchanged.
shoaib_ahmed 0:791a779d6220 504 * We assume build_ycc_rgb_table has been called.
shoaib_ahmed 0:791a779d6220 505 */
shoaib_ahmed 0:791a779d6220 506
shoaib_ahmed 0:791a779d6220 507 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 508 ycck_cmyk_convert (j_decompress_ptr cinfo,
shoaib_ahmed 0:791a779d6220 509 JSAMPIMAGE input_buf, JDIMENSION input_row,
shoaib_ahmed 0:791a779d6220 510 JSAMPARRAY output_buf, int num_rows)
shoaib_ahmed 0:791a779d6220 511 {
shoaib_ahmed 0:791a779d6220 512 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
shoaib_ahmed 0:791a779d6220 513 register int y, cb, cr;
shoaib_ahmed 0:791a779d6220 514 register JSAMPROW outptr;
shoaib_ahmed 0:791a779d6220 515 register JSAMPROW inptr0, inptr1, inptr2, inptr3;
shoaib_ahmed 0:791a779d6220 516 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 517 JDIMENSION num_cols = cinfo->output_width;
shoaib_ahmed 0:791a779d6220 518 /* copy these pointers into registers if possible */
shoaib_ahmed 0:791a779d6220 519 register JSAMPLE * range_limit = cinfo->sample_range_limit;
shoaib_ahmed 0:791a779d6220 520 register int * Crrtab = cconvert->Cr_r_tab;
shoaib_ahmed 0:791a779d6220 521 register int * Cbbtab = cconvert->Cb_b_tab;
shoaib_ahmed 0:791a779d6220 522 register INT32 * Crgtab = cconvert->Cr_g_tab;
shoaib_ahmed 0:791a779d6220 523 register INT32 * Cbgtab = cconvert->Cb_g_tab;
shoaib_ahmed 0:791a779d6220 524 SHIFT_TEMPS
shoaib_ahmed 0:791a779d6220 525
shoaib_ahmed 0:791a779d6220 526 while (--num_rows >= 0) {
shoaib_ahmed 0:791a779d6220 527 inptr0 = input_buf[0][input_row];
shoaib_ahmed 0:791a779d6220 528 inptr1 = input_buf[1][input_row];
shoaib_ahmed 0:791a779d6220 529 inptr2 = input_buf[2][input_row];
shoaib_ahmed 0:791a779d6220 530 inptr3 = input_buf[3][input_row];
shoaib_ahmed 0:791a779d6220 531 input_row++;
shoaib_ahmed 0:791a779d6220 532 outptr = *output_buf++;
shoaib_ahmed 0:791a779d6220 533 for (col = 0; col < num_cols; col++) {
shoaib_ahmed 0:791a779d6220 534 y = GETJSAMPLE(inptr0[col]);
shoaib_ahmed 0:791a779d6220 535 cb = GETJSAMPLE(inptr1[col]);
shoaib_ahmed 0:791a779d6220 536 cr = GETJSAMPLE(inptr2[col]);
shoaib_ahmed 0:791a779d6220 537 /* Range-limiting is essential due to noise introduced by DCT losses,
shoaib_ahmed 0:791a779d6220 538 * and for extended gamut encodings (sYCC).
shoaib_ahmed 0:791a779d6220 539 */
shoaib_ahmed 0:791a779d6220 540 outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
shoaib_ahmed 0:791a779d6220 541 outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
shoaib_ahmed 0:791a779d6220 542 ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
shoaib_ahmed 0:791a779d6220 543 SCALEBITS)))];
shoaib_ahmed 0:791a779d6220 544 outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
shoaib_ahmed 0:791a779d6220 545 /* K passes through unchanged */
shoaib_ahmed 0:791a779d6220 546 outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
shoaib_ahmed 0:791a779d6220 547 outptr += 4;
shoaib_ahmed 0:791a779d6220 548 }
shoaib_ahmed 0:791a779d6220 549 }
shoaib_ahmed 0:791a779d6220 550 }
shoaib_ahmed 0:791a779d6220 551
shoaib_ahmed 0:791a779d6220 552
shoaib_ahmed 0:791a779d6220 553 /*
shoaib_ahmed 0:791a779d6220 554 * Empty method for start_pass.
shoaib_ahmed 0:791a779d6220 555 */
shoaib_ahmed 0:791a779d6220 556
shoaib_ahmed 0:791a779d6220 557 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 558 start_pass_dcolor (j_decompress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 559 {
shoaib_ahmed 0:791a779d6220 560 /* no work needed */
shoaib_ahmed 0:791a779d6220 561 }
shoaib_ahmed 0:791a779d6220 562
shoaib_ahmed 0:791a779d6220 563
shoaib_ahmed 0:791a779d6220 564 /*
shoaib_ahmed 0:791a779d6220 565 * Module initialization routine for output colorspace conversion.
shoaib_ahmed 0:791a779d6220 566 */
shoaib_ahmed 0:791a779d6220 567
shoaib_ahmed 0:791a779d6220 568 GLOBAL(void)
shoaib_ahmed 0:791a779d6220 569 jinit_color_deconverter (j_decompress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 570 {
shoaib_ahmed 0:791a779d6220 571 my_cconvert_ptr cconvert;
shoaib_ahmed 0:791a779d6220 572 int ci;
shoaib_ahmed 0:791a779d6220 573
shoaib_ahmed 0:791a779d6220 574 cconvert = (my_cconvert_ptr)
shoaib_ahmed 0:791a779d6220 575 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 576 SIZEOF(my_color_deconverter));
shoaib_ahmed 0:791a779d6220 577 cinfo->cconvert = &cconvert->pub;
shoaib_ahmed 0:791a779d6220 578 cconvert->pub.start_pass = start_pass_dcolor;
shoaib_ahmed 0:791a779d6220 579
shoaib_ahmed 0:791a779d6220 580 /* Make sure num_components agrees with jpeg_color_space */
shoaib_ahmed 0:791a779d6220 581 switch (cinfo->jpeg_color_space) {
shoaib_ahmed 0:791a779d6220 582 case JCS_GRAYSCALE:
shoaib_ahmed 0:791a779d6220 583 if (cinfo->num_components != 1)
shoaib_ahmed 0:791a779d6220 584 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
shoaib_ahmed 0:791a779d6220 585 break;
shoaib_ahmed 0:791a779d6220 586
shoaib_ahmed 0:791a779d6220 587 case JCS_RGB:
shoaib_ahmed 0:791a779d6220 588 case JCS_YCbCr:
shoaib_ahmed 0:791a779d6220 589 case JCS_BG_RGB:
shoaib_ahmed 0:791a779d6220 590 case JCS_BG_YCC:
shoaib_ahmed 0:791a779d6220 591 if (cinfo->num_components != 3)
shoaib_ahmed 0:791a779d6220 592 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
shoaib_ahmed 0:791a779d6220 593 break;
shoaib_ahmed 0:791a779d6220 594
shoaib_ahmed 0:791a779d6220 595 case JCS_CMYK:
shoaib_ahmed 0:791a779d6220 596 case JCS_YCCK:
shoaib_ahmed 0:791a779d6220 597 if (cinfo->num_components != 4)
shoaib_ahmed 0:791a779d6220 598 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
shoaib_ahmed 0:791a779d6220 599 break;
shoaib_ahmed 0:791a779d6220 600
shoaib_ahmed 0:791a779d6220 601 default: /* JCS_UNKNOWN can be anything */
shoaib_ahmed 0:791a779d6220 602 if (cinfo->num_components < 1)
shoaib_ahmed 0:791a779d6220 603 ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
shoaib_ahmed 0:791a779d6220 604 break;
shoaib_ahmed 0:791a779d6220 605 }
shoaib_ahmed 0:791a779d6220 606
shoaib_ahmed 0:791a779d6220 607 /* Support color transform only for RGB colorspaces */
shoaib_ahmed 0:791a779d6220 608 if (cinfo->color_transform &&
shoaib_ahmed 0:791a779d6220 609 cinfo->jpeg_color_space != JCS_RGB &&
shoaib_ahmed 0:791a779d6220 610 cinfo->jpeg_color_space != JCS_BG_RGB)
shoaib_ahmed 0:791a779d6220 611 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
shoaib_ahmed 0:791a779d6220 612
shoaib_ahmed 0:791a779d6220 613 /* Set out_color_components and conversion method based on requested space.
shoaib_ahmed 0:791a779d6220 614 * Also clear the component_needed flags for any unused components,
shoaib_ahmed 0:791a779d6220 615 * so that earlier pipeline stages can avoid useless computation.
shoaib_ahmed 0:791a779d6220 616 */
shoaib_ahmed 0:791a779d6220 617
shoaib_ahmed 0:791a779d6220 618 switch (cinfo->out_color_space) {
shoaib_ahmed 0:791a779d6220 619 case JCS_GRAYSCALE:
shoaib_ahmed 0:791a779d6220 620 cinfo->out_color_components = 1;
shoaib_ahmed 0:791a779d6220 621 switch (cinfo->jpeg_color_space) {
shoaib_ahmed 0:791a779d6220 622 case JCS_GRAYSCALE:
shoaib_ahmed 0:791a779d6220 623 case JCS_YCbCr:
shoaib_ahmed 0:791a779d6220 624 case JCS_BG_YCC:
shoaib_ahmed 0:791a779d6220 625 cconvert->pub.color_convert = grayscale_convert;
shoaib_ahmed 0:791a779d6220 626 /* For color->grayscale conversion, only the Y (0) component is needed */
shoaib_ahmed 0:791a779d6220 627 for (ci = 1; ci < cinfo->num_components; ci++)
shoaib_ahmed 0:791a779d6220 628 cinfo->comp_info[ci].component_needed = FALSE;
shoaib_ahmed 0:791a779d6220 629 break;
shoaib_ahmed 0:791a779d6220 630 case JCS_RGB:
shoaib_ahmed 0:791a779d6220 631 switch (cinfo->color_transform) {
shoaib_ahmed 0:791a779d6220 632 case JCT_NONE:
shoaib_ahmed 0:791a779d6220 633 cconvert->pub.color_convert = rgb_gray_convert;
shoaib_ahmed 0:791a779d6220 634 break;
shoaib_ahmed 0:791a779d6220 635 case JCT_SUBTRACT_GREEN:
shoaib_ahmed 0:791a779d6220 636 cconvert->pub.color_convert = rgb1_gray_convert;
shoaib_ahmed 0:791a779d6220 637 break;
shoaib_ahmed 0:791a779d6220 638 default:
shoaib_ahmed 0:791a779d6220 639 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
shoaib_ahmed 0:791a779d6220 640 }
shoaib_ahmed 0:791a779d6220 641 build_rgb_y_table(cinfo);
shoaib_ahmed 0:791a779d6220 642 break;
shoaib_ahmed 0:791a779d6220 643 default:
shoaib_ahmed 0:791a779d6220 644 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
shoaib_ahmed 0:791a779d6220 645 }
shoaib_ahmed 0:791a779d6220 646 break;
shoaib_ahmed 0:791a779d6220 647
shoaib_ahmed 0:791a779d6220 648 case JCS_RGB:
shoaib_ahmed 0:791a779d6220 649 cinfo->out_color_components = RGB_PIXELSIZE;
shoaib_ahmed 0:791a779d6220 650 switch (cinfo->jpeg_color_space) {
shoaib_ahmed 0:791a779d6220 651 case JCS_GRAYSCALE:
shoaib_ahmed 0:791a779d6220 652 cconvert->pub.color_convert = gray_rgb_convert;
shoaib_ahmed 0:791a779d6220 653 break;
shoaib_ahmed 0:791a779d6220 654 case JCS_YCbCr:
shoaib_ahmed 0:791a779d6220 655 cconvert->pub.color_convert = ycc_rgb_convert;
shoaib_ahmed 0:791a779d6220 656 build_ycc_rgb_table(cinfo);
shoaib_ahmed 0:791a779d6220 657 break;
shoaib_ahmed 0:791a779d6220 658 case JCS_BG_YCC:
shoaib_ahmed 0:791a779d6220 659 cconvert->pub.color_convert = ycc_rgb_convert;
shoaib_ahmed 0:791a779d6220 660 build_bg_ycc_rgb_table(cinfo);
shoaib_ahmed 0:791a779d6220 661 break;
shoaib_ahmed 0:791a779d6220 662 case JCS_RGB:
shoaib_ahmed 0:791a779d6220 663 switch (cinfo->color_transform) {
shoaib_ahmed 0:791a779d6220 664 case JCT_NONE:
shoaib_ahmed 0:791a779d6220 665 cconvert->pub.color_convert = rgb_convert;
shoaib_ahmed 0:791a779d6220 666 break;
shoaib_ahmed 0:791a779d6220 667 case JCT_SUBTRACT_GREEN:
shoaib_ahmed 0:791a779d6220 668 cconvert->pub.color_convert = rgb1_rgb_convert;
shoaib_ahmed 0:791a779d6220 669 break;
shoaib_ahmed 0:791a779d6220 670 default:
shoaib_ahmed 0:791a779d6220 671 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
shoaib_ahmed 0:791a779d6220 672 }
shoaib_ahmed 0:791a779d6220 673 break;
shoaib_ahmed 0:791a779d6220 674 default:
shoaib_ahmed 0:791a779d6220 675 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
shoaib_ahmed 0:791a779d6220 676 }
shoaib_ahmed 0:791a779d6220 677 break;
shoaib_ahmed 0:791a779d6220 678
shoaib_ahmed 0:791a779d6220 679 case JCS_BG_RGB:
shoaib_ahmed 0:791a779d6220 680 cinfo->out_color_components = RGB_PIXELSIZE;
shoaib_ahmed 0:791a779d6220 681 if (cinfo->jpeg_color_space == JCS_BG_RGB) {
shoaib_ahmed 0:791a779d6220 682 switch (cinfo->color_transform) {
shoaib_ahmed 0:791a779d6220 683 case JCT_NONE:
shoaib_ahmed 0:791a779d6220 684 cconvert->pub.color_convert = rgb_convert;
shoaib_ahmed 0:791a779d6220 685 break;
shoaib_ahmed 0:791a779d6220 686 case JCT_SUBTRACT_GREEN:
shoaib_ahmed 0:791a779d6220 687 cconvert->pub.color_convert = rgb1_rgb_convert;
shoaib_ahmed 0:791a779d6220 688 break;
shoaib_ahmed 0:791a779d6220 689 default:
shoaib_ahmed 0:791a779d6220 690 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
shoaib_ahmed 0:791a779d6220 691 }
shoaib_ahmed 0:791a779d6220 692 } else
shoaib_ahmed 0:791a779d6220 693 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
shoaib_ahmed 0:791a779d6220 694 break;
shoaib_ahmed 0:791a779d6220 695
shoaib_ahmed 0:791a779d6220 696 case JCS_CMYK:
shoaib_ahmed 0:791a779d6220 697 cinfo->out_color_components = 4;
shoaib_ahmed 0:791a779d6220 698 switch (cinfo->jpeg_color_space) {
shoaib_ahmed 0:791a779d6220 699 case JCS_YCCK:
shoaib_ahmed 0:791a779d6220 700 cconvert->pub.color_convert = ycck_cmyk_convert;
shoaib_ahmed 0:791a779d6220 701 build_ycc_rgb_table(cinfo);
shoaib_ahmed 0:791a779d6220 702 break;
shoaib_ahmed 0:791a779d6220 703 case JCS_CMYK:
shoaib_ahmed 0:791a779d6220 704 cconvert->pub.color_convert = null_convert;
shoaib_ahmed 0:791a779d6220 705 break;
shoaib_ahmed 0:791a779d6220 706 default:
shoaib_ahmed 0:791a779d6220 707 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
shoaib_ahmed 0:791a779d6220 708 }
shoaib_ahmed 0:791a779d6220 709 break;
shoaib_ahmed 0:791a779d6220 710
shoaib_ahmed 0:791a779d6220 711 default:
shoaib_ahmed 0:791a779d6220 712 /* Permit null conversion to same output space */
shoaib_ahmed 0:791a779d6220 713 if (cinfo->out_color_space == cinfo->jpeg_color_space) {
shoaib_ahmed 0:791a779d6220 714 cinfo->out_color_components = cinfo->num_components;
shoaib_ahmed 0:791a779d6220 715 cconvert->pub.color_convert = null_convert;
shoaib_ahmed 0:791a779d6220 716 } else /* unsupported non-null conversion */
shoaib_ahmed 0:791a779d6220 717 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
shoaib_ahmed 0:791a779d6220 718 break;
shoaib_ahmed 0:791a779d6220 719 }
shoaib_ahmed 0:791a779d6220 720
shoaib_ahmed 0:791a779d6220 721 if (cinfo->quantize_colors)
shoaib_ahmed 0:791a779d6220 722 cinfo->output_components = 1; /* single colormapped output component */
shoaib_ahmed 0:791a779d6220 723 else
shoaib_ahmed 0:791a779d6220 724 cinfo->output_components = cinfo->out_color_components;
shoaib_ahmed 0:791a779d6220 725 }