09fb8af2044dfb3c9b8dd90b0a3d49c256b2771d
3 { This file contains input colorspace conversion routines. }
5 { Original : jccolor.c ; Copyright (C) 1991-1996, Thomas G. Lane. }
7 interface
9 {$I imjconfig.inc}
11 uses
12 imjmorecfg,
13 imjinclude,
14 imjdeferr,
15 imjerror,
16 imjpeglib;
18 { Module initialization routine for input colorspace conversion. }
20 {GLOBAL}
23 implementation
25 { Private subobject }
26 type
30 type
35 { Private state for RGB -> YCC conversion }
40 {*************** RGB -> YCbCr conversion: most common case *************}
42 {
43 YCbCr is defined per CCIR 601-1, except that Cb and Cr are
44 normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
45 The conversion equations to be implemented are therefore
46 Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
47 Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
48 Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
49 (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
50 Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
51 rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and
52 negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
53 were not represented exactly. Now we sacrifice exact representation of
54 maximum red and maximum blue in order to get exact grayscales.
56 To avoid floating-point arithmetic, we represent the fractional constants
57 as integers scaled up by 2^16 (about 4 digits precision); we have to divide
58 the products by 2^16, with appropriate rounding, to get the correct answer.
60 For even more speed, we avoid doing any multiplications in the inner loop
61 by precalculating the constants times R,G,B for all possible values.
62 For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
63 for 12-bit samples it is still acceptable. It's not very reasonable for
64 16-bit samples, but if you want lossless storage you shouldn't be changing
65 colorspace anyway.
66 The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
67 in the tables to save adding them separately in the inner loop. }
68 const
74 { We allocate one big table and divide it up into eight parts, instead of
75 doing eight alloc_small requests. This lets us use a single table base
76 address, which can be held in a register in the inner loops on many
77 machines (more than can hold all eight addresses, anyway). }
91 { Initialize for RGB->YCC colorspace conversion. }
93 {METHODDEF}
95 const
104 var
108 begin
111 { Allocate and fill in the conversion tables. }
118 begin
124 { We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
125 This ensures that the maximum output will round to MAXJSAMPLE
126 not MAXJSAMPLE+1, and thus that we don't have to range-limit. }
129 { B=>Cb and R=>Cr tables are the same
130 rgb_ycc_tab^[i+R_CR_OFF] := FIX_0_50000 * i + CBCR_OFFSET + ONE_HALF-1;
131 }
138 { Convert some rows of samples to the JPEG colorspace.
140 Note that we change from the application's interleaved-pixel format
141 to our internal noninterleaved, one-plane-per-component format.
142 The input buffer is therefore three times as wide as the output buffer.
144 A starting row offset is provided only for the output buffer. The caller
145 can easily adjust the passed input_buf value to accommodate any row
146 offset required on that side. }
148 {METHODDEF}
154 var
162 begin
168 begin
177 begin
182 { If the inputs are 0..MAXJSAMPLE, the outputs of these equations
183 must be too; we do not need an explicit range-limiting operation.
184 Hence the value being shifted is never negative, and we don't
185 need the general RIGHT_SHIFT macro. }
187 { Y }
191 { Cb }
195 { Cr }
204 {*************** Cases other than RGB -> YCbCr *************}
207 { Convert some rows of samples to the JPEG colorspace.
208 This version handles RGB -> grayscale conversion, which is the same
209 as the RGB -> Y portion of RGB -> YCbCr.
210 We assume rgb_ycc_start has been called (we only use the Y tables). }
212 {METHODDEF}
218 var
226 begin
232 begin
239 begin
244 (* Y *)
245 // kylix 3 compiler crashes on this
246 // it also crashes Delphi OSX compiler 9 years later :(
247 {$IF not (Defined(DCC) and not Defined(MSWINDOWS))}
249 {$IFEND}
255 { Convert some rows of samples to the JPEG colorspace.
256 This version handles Adobe-style CMYK -> YCCK conversion,
257 where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
258 conversion as above, while passing K (black) unchanged.
259 We assume rgb_ycc_start has been called. }
261 {METHODDEF}
267 var
275 begin
281 begin
291 begin
295 { K passes through as-is }
298 { If the inputs are 0..MAXJSAMPLE, the outputs of these equations
299 must be too; we do not need an explicit range-limiting operation.
300 Hence the value being shifted is never negative, and we don't
301 need the general RIGHT_SHIFT macro. }
303 { Y }
307 { Cb }
311 { Cr }
320 { Convert some rows of samples to the JPEG colorspace.
321 This version handles grayscale output with no conversion.
322 The source can be either plain grayscale or YCbCr (since Y = gray). }
324 {METHODDEF}
330 var
336 begin
341 begin
348 begin
356 { Convert some rows of samples to the JPEG colorspace.
357 This version handles multi-component colorspaces without conversion.
358 We assume input_components = num_components. }
360 {METHODDEF}
366 var
373 begin
378 begin
380 { It seems fastest to make a separate pass for each component. }
382 begin
386 begin
397 { Empty method for start_pass. }
399 {METHODDEF}
401 begin
402 { no work needed }
406 { Module initialization routine for input colorspace conversion. }
408 {GLOBAL}
410 var
412 begin
417 { set start_pass to null method until we find out differently }
420 { Make sure input_components agrees with in_color_space }
422 JCS_GRAYSCALE:
426 {$ifdef RGB_PIXELSIZE <> 3}
427 JCS_RGB:
431 JCS_RGB,
432 {$endif}
433 JCS_YCbCr:
437 JCS_CMYK,
438 JCS_YCCK:
447 { Check num_components, set conversion method based on requested space }
449 JCS_GRAYSCALE:
450 begin
455 else
457 begin
460 end
461 else
464 else
468 JCS_RGB:
469 begin
474 else
478 JCS_YCbCr:
479 begin
483 begin
486 end
487 else
490 else
494 JCS_CMYK:
495 begin
500 else
504 JCS_YCCK:
505 begin
509 begin
512 end
513 else
516 else
521 begin