📄 jquant2.pas
字号:
Unit JQuant2;
{ This file contains 2-pass color quantization (color mapping) routines.
These routines provide selection of a custom color map for an image,
followed by mapping of the image to that color map, with optional
Floyd-Steinberg dithering.
It is also possible to use just the second pass to map to an arbitrary
externally-given color map.
Note: ordered dithering is not supported, since there isn't any fast
way to compute intercolor distances; it's unclear that ordered dither's
fundamental assumptions even hold with an irregularly spaced color map. }
{ Original: jquant2.c; Copyright (C) 1991-1996, Thomas G. Lane. }
interface
{$I jconfig.inc}
uses
jmorecfg,
jdeferr,
jerror,
jutils,
jpeglib;
{ Module initialization routine for 2-pass color quantization. }
{GLOBAL}
procedure jinit_2pass_quantizer (cinfo : j_decompress_ptr);
implementation
{ This module implements the well-known Heckbert paradigm for color
quantization. Most of the ideas used here can be traced back to
Heckbert's seminal paper
Heckbert, Paul. "Color Image Quantization for Frame Buffer Display",
Proc. SIGGRAPH '82, Computer Graphics v.16 #3 (July 1982), pp 297-304.
In the first pass over the image, we accumulate a histogram showing the
usage count of each possible color. To keep the histogram to a reasonable
size, we reduce the precision of the input; typical practice is to retain
5 or 6 bits per color, so that 8 or 4 different input values are counted
in the same histogram cell.
Next, the color-selection step begins with a box representing the whole
color space, and repeatedly splits the "largest" remaining box until we
have as many boxes as desired colors. Then the mean color in each
remaining box becomes one of the possible output colors.
The second pass over the image maps each input pixel to the closest output
color (optionally after applying a Floyd-Steinberg dithering correction).
This mapping is logically trivial, but making it go fast enough requires
considerable care.
Heckbert-style quantizers vary a good deal in their policies for choosing
the "largest" box and deciding where to cut it. The particular policies
used here have proved out well in experimental comparisons, but better ones
may yet be found.
In earlier versions of the IJG code, this module quantized in YCbCr color
space, processing the raw upsampled data without a color conversion step.
This allowed the color conversion math to be done only once per colormap
entry, not once per pixel. However, that optimization precluded other
useful optimizations (such as merging color conversion with upsampling)
and it also interfered with desired capabilities such as quantizing to an
externally-supplied colormap. We have therefore abandoned that approach.
The present code works in the post-conversion color space, typically RGB.
To improve the visual quality of the results, we actually work in scaled
RGB space, giving G distances more weight than R, and R in turn more than
B. To do everything in integer math, we must use integer scale factors.
The 2/3/1 scale factors used here correspond loosely to the relative
weights of the colors in the NTSC grayscale equation.
If you want to use this code to quantize a non-RGB color space, you'll
probably need to change these scale factors. }
const
R_SCALE = 2; { scale R distances by this much }
G_SCALE = 3; { scale G distances by this much }
B_SCALE = 1; { and B by this much }
{ Relabel R/G/B as components 0/1/2, respecting the RGB ordering defined
in jmorecfg.h. As the code stands, it will do the right thing for R,G,B
and B,G,R orders. If you define some other weird order in jmorecfg.h,
you'll get compile errors until you extend this logic. In that case
you'll probably want to tweak the histogram sizes too. }
{$ifdef RGB_RED_IS_0}
const
C0_SCALE = R_SCALE;
C1_SCALE = G_SCALE;
C2_SCALE = B_SCALE;
{$else}
const
C0_SCALE = B_SCALE;
C1_SCALE = G_SCALE;
C2_SCALE = R_SCALE;
{$endif}
{ First we have the histogram data structure and routines for creating it.
The number of bits of precision can be adjusted by changing these symbols.
We recommend keeping 6 bits for G and 5 each for R and B.
If you have plenty of memory and cycles, 6 bits all around gives marginally
better results; if you are short of memory, 5 bits all around will save
some space but degrade the results.
To maintain a fully accurate histogram, we'd need to allocate a "long"
(preferably unsigned long) for each cell. In practice this is overkill;
we can get by with 16 bits per cell. Few of the cell counts will overflow,
and clamping those that do overflow to the maximum value will give close-
enough results. This reduces the recommended histogram size from 256Kb
to 128Kb, which is a useful savings on PC-class machines.
(In the second pass the histogram space is re-used for pixel mapping data;
in that capacity, each cell must be able to store zero to the number of
desired colors. 16 bits/cell is plenty for that too.)
Since the JPEG code is intended to run in small memory model on 80x86
machines, we can't just allocate the histogram in one chunk. Instead
of a true 3-D array, we use a row of pointers to 2-D arrays. Each
pointer corresponds to a C0 value (typically 2^5 = 32 pointers) and
each 2-D array has 2^6*2^5 = 2048 or 2^6*2^6 = 4096 entries. Note that
on 80x86 machines, the pointer row is in near memory but the actual
arrays are in far memory (same arrangement as we use for image arrays). }
const
MAXNUMCOLORS = (MAXJSAMPLE+1); { maximum size of colormap }
{ These will do the right thing for either R,G,B or B,G,R color order,
but you may not like the results for other color orders. }
const
HIST_C0_BITS = 5; { bits of precision in R/B histogram }
HIST_C1_BITS = 6; { bits of precision in G histogram }
HIST_C2_BITS = 5; { bits of precision in B/R histogram }
{ Number of elements along histogram axes. }
const
HIST_C0_ELEMS = (1 shl HIST_C0_BITS);
HIST_C1_ELEMS = (1 shl HIST_C1_BITS);
HIST_C2_ELEMS = (1 shl HIST_C2_BITS);
{ These are the amounts to shift an input value to get a histogram index. }
const
C0_SHIFT = (BITS_IN_JSAMPLE-HIST_C0_BITS);
C1_SHIFT = (BITS_IN_JSAMPLE-HIST_C1_BITS);
C2_SHIFT = (BITS_IN_JSAMPLE-HIST_C2_BITS);
type { Nomssi }
RGBptr = ^RGBtype;
RGBtype = packed record
r,g,b : JSAMPLE;
end;
type
histcell = UINT16; { histogram cell; prefer an unsigned type }
type
histptr = ^histcell {FAR}; { for pointers to histogram cells }
type
hist1d = array[0..HIST_C2_ELEMS-1] of histcell; { typedefs for the array }
{hist1d_ptr = ^hist1d;}
hist1d_field = array[0..HIST_C1_ELEMS-1] of hist1d;
{ type for the 2nd-level pointers }
hist2d = ^hist1d_field;
hist2d_field = array[0..HIST_C0_ELEMS-1] of hist2d;
hist3d = ^hist2d_field; { type for top-level pointer }
{ Declarations for Floyd-Steinberg dithering.
Errors are accumulated into the array fserrors[], at a resolution of
1/16th of a pixel count. The error at a given pixel is propagated
to its not-yet-processed neighbors using the standard F-S fractions,
... (here) 7/16
3/16 5/16 1/16
We work left-to-right on even rows, right-to-left on odd rows.
We can get away with a single array (holding one row's worth of errors)
by using it to store the current row's errors at pixel columns not yet
processed, but the next row's errors at columns already processed. We
need only a few extra variables to hold the errors immediately around the
current column. (If we are lucky, those variables are in registers, but
even if not, they're probably cheaper to access than array elements are.)
The fserrors[] array has (#columns + 2) entries; the extra entry at
each end saves us from special-casing the first and last pixels.
Each entry is three values long, one value for each color component.
Note: on a wide image, we might not have enough room in a PC's near data
segment to hold the error array; so it is allocated with alloc_large. }
{$ifdef BITS_IN_JSAMPLE_IS_8}
type
FSERROR = INT16; { 16 bits should be enough }
LOCFSERROR = int; { use 'int' for calculation temps }
{$else}
type
FSERROR = INT32; { may need more than 16 bits }
LOCFSERROR = INT32; { be sure calculation temps are big enough }
{$endif}
type { Nomssi }
RGB_FSERROR_PTR = ^RGB_FSERROR;
RGB_FSERROR = packed record
r,g,b : FSERROR;
end;
LOCRGB_FSERROR = packed record
r,g,b : LOCFSERROR;
end;
type
FSERROR_PTR = ^FSERROR;
jFSError = 0..(MaxInt div SIZEOF(RGB_FSERROR))-1;
FS_ERROR_FIELD = array[jFSError] of RGB_FSERROR;
FS_ERROR_FIELD_PTR = ^FS_ERROR_FIELD;{far}
{ pointer to error array (in FAR storage!) }
type
error_limit_array = array[-MAXJSAMPLE..MAXJSAMPLE] of int;
{ table for clamping the applied error }
error_limit_ptr = ^error_limit_array;
{ Private subobject }
type
my_cquantize_ptr = ^my_cquantizer;
my_cquantizer = record
pub : jpeg_color_quantizer; { public fields }
{ Space for the eventually created colormap is stashed here }
sv_colormap : JSAMPARRAY; { colormap allocated at init time }
desired : int; { desired # of colors = size of colormap }
{ Variables for accumulating image statistics }
histogram : hist3d; { pointer to the histogram }
needs_zeroed : boolean; { TRUE if next pass must zero histogram }
{ Variables for Floyd-Steinberg dithering }
fserrors : FS_ERROR_FIELD_PTR; { accumulated errors }
on_odd_row : boolean; { flag to remember which row we are on }
error_limiter : error_limit_ptr; { table for clamping the applied error }
end;
{ Prescan some rows of pixels.
In this module the prescan simply updates the histogram, which has been
initialized to zeroes by start_pass.
An output_buf parameter is required by the method signature, but no data
is actually output (in fact the buffer controller is probably passing a
NIL pointer). }
{METHODDEF}
procedure prescan_quantize (cinfo : j_decompress_ptr;
input_buf : JSAMPARRAY;
output_buf : JSAMPARRAY;
num_rows : int); far;
var
cquantize : my_cquantize_ptr;
{register} ptr : RGBptr;
{register} histp : histptr;
{register} histogram : hist3d;
row : int;
col : JDIMENSION;
width : JDIMENSION;
begin
cquantize := my_cquantize_ptr(cinfo^.cquantize);
histogram := cquantize^.histogram;
width := cinfo^.output_width;
for row := 0 to pred(num_rows) do
begin
ptr := RGBptr(input_buf^[row]);
for col := pred(width) downto 0 do
begin
{ get pixel value and index into the histogram }
histp := @(histogram^[GETJSAMPLE(ptr^.r) shr C0_SHIFT]^
[GETJSAMPLE(ptr^.g) shr C1_SHIFT]
[GETJSAMPLE(ptr^.b) shr C2_SHIFT]);
{ increment, check for overflow and undo increment if so. }
Inc(histp^);
if (histp^ <= 0) then
Dec(histp^);
Inc(ptr);
end;
end;
end;
{ Next we have the really interesting routines: selection of a colormap
given the completed histogram.
These routines work with a list of "boxes", each representing a rectangular
subset of the input color space (to histogram precision). }
type
box = record
{ The bounds of the box (inclusive); expressed as histogram indexes }
c0min, c0max : int;
c1min, c1max : int;
c2min, c2max : int;
{ The volume (actually 2-norm) of the box }
volume : INT32;
{ The number of nonzero histogram cells within this box }
colorcount : long;
end;
type
jBoxList = 0..(MaxInt div SizeOf(box))-1;
box_field = array[jBoxlist] of box;
boxlistptr = ^box_field;
boxptr = ^box;
{LOCAL}
function find_biggest_color_pop (boxlist : boxlistptr; numboxes : int) : boxptr;
{ Find the splittable box with the largest color population }
{ Returns NIL if no splittable boxes remain }
var
boxp : boxptr ; {register}
i : int; {register}
maxc : long; {register}
which : boxptr;
begin
which := NIL;
boxp := @(boxlist^[0]);
maxc := 0;
for i := 0 to pred(numboxes) do
begin
if (boxp^.colorcount > maxc) and (boxp^.volume > 0) then
begin
which := boxp;
maxc := boxp^.colorcount;
end;
Inc(boxp);
end;
find_biggest_color_pop := which;
end;
{LOCAL}
function find_biggest_volume (boxlist : boxlistptr; numboxes : int) : boxptr;
{ Find the splittable box with the largest (scaled) volume }
{ Returns NULL if no splittable boxes remain }
var
{register} boxp : boxptr;
{register} i : int;
{register} maxv : INT32;
which : boxptr;
begin
maxv := 0;
which := NIL;
boxp := @(boxlist^[0]);
for i := 0 to pred(numboxes) do
begin
if (boxp^.volume > maxv) then
begin
which := boxp;
maxv := boxp^.volume;
end;
Inc(boxp);
end;
find_biggest_volume := which;
end;
{LOCAL}
procedure update_box (cinfo : j_decompress_ptr; var boxp : box);
label
have_c0min, have_c0max,
have_c1min, have_c1max,
have_c2min, have_c2max;
{ Shrink the min/max bounds of a box to enclose only nonzero elements, }
{ and recompute its volume and population }
var
cquantize : my_cquantize_ptr;
histogram : hist3d;
histp : histptr;
c0,c1,c2 : int;
c0min,c0max,c1min,c1max,c2min,c2max : int;
dist0,dist1,dist2 : INT32;
ccount : long;
begin
cquantize := my_cquantize_ptr(cinfo^.cquantize);
histogram := cquantize^.histogram;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -