📄 jdcolor.pas
字号:
Unit JdColor;
{ This file contains output colorspace conversion routines. }
{ Original: jdcolor.c ; Copyright (C) 1991-1997, Thomas G. Lane. }
interface
{$I jconfig.inc}
uses
jmorecfg,
jinclude,
jutils,
jdeferr,
jerror,
jpeglib;
{ Module initialization routine for output colorspace conversion. }
{GLOBAL}
procedure jinit_color_deconverter (cinfo : j_decompress_ptr);
implementation
{ Private subobject }
type
int_Color_Table = array[0..MAXJSAMPLE+1-1] of int;
int_table_ptr = ^int_Color_Table;
INT32_Color_Table = array[0..MAXJSAMPLE+1-1] of INT32;
INT32_table_ptr = ^INT32_Color_Table;
type
my_cconvert_ptr = ^my_color_deconverter;
my_color_deconverter = record
pub : jpeg_color_deconverter; { public fields }
{ Private state for YCC^.RGB conversion }
Cr_r_tab : int_table_ptr; { => table for Cr to R conversion }
Cb_b_tab : int_table_ptr; { => table for Cb to B conversion }
Cr_g_tab : INT32_table_ptr; { => table for Cr to G conversion }
Cb_g_tab : INT32_table_ptr; { => table for Cb to G conversion }
end;
{*************** YCbCr ^. RGB conversion: most common case *************}
{ YCbCr is defined per CCIR 601-1, except that Cb and Cr are
normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
The conversion equations to be implemented are therefore
R = Y + 1.40200 * Cr
G = Y - 0.34414 * Cb - 0.71414 * Cr
B = Y + 1.77200 * Cb
where Cb and Cr represent the incoming values less CENTERJSAMPLE.
(These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
To avoid floating-point arithmetic, we represent the fractional constants
as integers scaled up by 2^16 (about 4 digits precision); we have to divide
the products by 2^16, with appropriate rounding, to get the correct answer.
Notice that Y, being an integral input, does not contribute any fraction
so it need not participate in the rounding.
For even more speed, we avoid doing any multiplications in the inner loop
by precalculating the constants times Cb and Cr for all possible values.
For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
for 12-bit samples it is still acceptable. It's not very reasonable for
16-bit samples, but if you want lossless storage you shouldn't be changing
colorspace anyway.
The Cr=>R and Cb=>B values can be rounded to integers in advance; the
values for the G calculation are left scaled up, since we must add them
together before rounding. }
const
SCALEBITS = 16; { speediest right-shift on some machines }
ONE_HALF = (INT32(1) shl (SCALEBITS-1));
{ Initialize tables for YCC->RGB colorspace conversion. }
{LOCAL}
procedure build_ycc_rgb_table (cinfo : j_decompress_ptr);
const
FIX_1_40200 = INT32(Round( 1.40200 * (1 shl SCALEBITS)));
FIX_1_77200 = INT32(Round( 1.77200 * (1 shl SCALEBITS)));
FIX_0_71414 = INT32(Round( 0.71414 * (1 shl SCALEBITS)));
FIX_0_34414 = INT32(Round( 0.34414 * (1 shl SCALEBITS)));
var
cconvert : my_cconvert_ptr;
i : int;
x : INT32;
var
shift_temp : INT32;
begin
cconvert := my_cconvert_ptr (cinfo^.cconvert);
cconvert^.Cr_r_tab := int_table_ptr(
cinfo^.mem^.alloc_small ( j_common_ptr(cinfo), JPOOL_IMAGE,
(MAXJSAMPLE+1) * SIZEOF(int)) );
cconvert^.Cb_b_tab := int_table_ptr (
cinfo^.mem^.alloc_small ( j_common_ptr(cinfo), JPOOL_IMAGE,
(MAXJSAMPLE+1) * SIZEOF(int)) );
cconvert^.Cr_g_tab := INT32_table_ptr (
cinfo^.mem^.alloc_small ( j_common_ptr(cinfo), JPOOL_IMAGE,
(MAXJSAMPLE+1) * SIZEOF(INT32)) );
cconvert^.Cb_g_tab := INT32_table_ptr (
cinfo^.mem^.alloc_small ( j_common_ptr(cinfo), JPOOL_IMAGE,
(MAXJSAMPLE+1) * SIZEOF(INT32)) );
x := -CENTERJSAMPLE;
for i := 0 to MAXJSAMPLE do
begin
{ i is the actual input pixel value, in the range 0..MAXJSAMPLE }
{ The Cb or Cr value we are thinking of is x := i - CENTERJSAMPLE }
{ Cr=>R value is nearest int to 1.40200 * x }
shift_temp := FIX_1_40200 * x + ONE_HALF;
if shift_temp < 0 then { SHIFT arithmetic RIGHT }
cconvert^.Cr_r_tab^[i] := int((shift_temp shr SCALEBITS)
or ( (not INT32(0)) shl (32-SCALEBITS)))
else
cconvert^.Cr_r_tab^[i] := int(shift_temp shr SCALEBITS);
{ Cb=>B value is nearest int to 1.77200 * x }
shift_temp := FIX_1_77200 * x + ONE_HALF;
if shift_temp < 0 then { SHIFT arithmetic RIGHT }
cconvert^.Cb_b_tab^[i] := int((shift_temp shr SCALEBITS)
or ( (not INT32(0)) shl (32-SCALEBITS)))
else
cconvert^.Cb_b_tab^[i] := int(shift_temp shr SCALEBITS);
{ Cr=>G value is scaled-up -0.71414 * x }
cconvert^.Cr_g_tab^[i] := (- FIX_0_71414 ) * x;
{ Cb=>G value is scaled-up -0.34414 * x }
{ We also add in ONE_HALF so that need not do it in inner loop }
cconvert^.Cb_g_tab^[i] := (- FIX_0_34414 ) * x + ONE_HALF;
Inc(x);
end;
end;
{ Convert some rows of samples to the output colorspace.
Note that we change from noninterleaved, one-plane-per-component format
to interleaved-pixel format. The output buffer is therefore three times
as wide as the input buffer.
A starting row offset is provided only for the input buffer. The caller
can easily adjust the passed output_buf value to accommodate any row
offset required on that side. }
{METHODDEF}
procedure ycc_rgb_convert (cinfo : j_decompress_ptr;
input_buf : JSAMPIMAGE;
input_row : JDIMENSION;
output_buf : JSAMPARRAY;
num_rows : int); far;
var
cconvert : my_cconvert_ptr;
{register} y, cb, cr : int;
{register} outptr : JSAMPROW;
{register} inptr0, inptr1, inptr2 : JSAMPROW;
{register} col : JDIMENSION;
num_cols : JDIMENSION;
{ copy these pointers into registers if possible }
{register} range_limit : range_limit_table_ptr;
{register} Crrtab : int_table_ptr;
{register} Cbbtab : int_table_ptr;
{register} Crgtab : INT32_table_ptr;
{register} Cbgtab : INT32_table_ptr;
var
shift_temp : INT32;
begin
cconvert := my_cconvert_ptr (cinfo^.cconvert);
num_cols := cinfo^.output_width;
range_limit := cinfo^.sample_range_limit;
Crrtab := cconvert^.Cr_r_tab;
Cbbtab := cconvert^.Cb_b_tab;
Crgtab := cconvert^.Cr_g_tab;
Cbgtab := cconvert^.Cb_g_tab;
while (num_rows > 0) do
begin
Dec(num_rows);
inptr0 := input_buf^[0]^[input_row];
inptr1 := input_buf^[1]^[input_row];
inptr2 := input_buf^[2]^[input_row];
Inc(input_row);
outptr := output_buf^[0];
Inc(JSAMPROW_PTR(output_buf));
for col := 0 to pred(num_cols) do
begin
y := GETJSAMPLE(inptr0^[col]);
cb := GETJSAMPLE(inptr1^[col]);
cr := GETJSAMPLE(inptr2^[col]);
{ Range-limiting is essential due to noise introduced by DCT losses. }
outptr^[RGB_RED] := range_limit^[y + Crrtab^[cr]];
shift_temp := Cbgtab^[cb] + Crgtab^[cr];
if shift_temp < 0 then { SHIFT arithmetic RIGHT }
outptr^[RGB_GREEN] := range_limit^[y + int((shift_temp shr SCALEBITS)
or ( (not INT32(0)) shl (32-SCALEBITS)))]
else
outptr^[RGB_GREEN] := range_limit^[y + int(shift_temp shr SCALEBITS)];
outptr^[RGB_BLUE] := range_limit^[y + Cbbtab^[cb]];
Inc(JSAMPLE_PTR(outptr), RGB_PIXELSIZE);
end;
end;
end;
{*************** Cases other than YCbCr -> RGB *************}
{ Color conversion for no colorspace change: just copy the data,
converting from separate-planes to interleaved representation. }
{METHODDEF}
procedure null_convert (cinfo : j_decompress_ptr;
input_buf : JSAMPIMAGE;
input_row : JDIMENSION;
output_buf : JSAMPARRAY;
num_rows : int); far;
var
{register} inptr,
outptr : JSAMPLE_PTR;
{register} count : JDIMENSION;
{register} num_components : int;
num_cols : JDIMENSION;
ci : int;
begin
num_components := cinfo^.num_components;
num_cols := cinfo^.output_width;
while (num_rows > 0) do
begin
Dec(num_rows);
for ci := 0 to pred(num_components) do
begin
inptr := JSAMPLE_PTR(input_buf^[ci]^[input_row]);
outptr := JSAMPLE_PTR(@(output_buf^[0]^[ci]));
for count := pred(num_cols) downto 0 do
begin
outptr^ := inptr^; { needn't bother with GETJSAMPLE() here }
Inc(inptr);
Inc(outptr, num_components);
end;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -