1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
Unit RdColMap;
{ rdcolmap.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
{ This file implements djpeg's "-map file" switch. It reads a source image
and constructs a colormap to be supplied to the JPEG decompressor.
Currently, these file formats are supported for the map file:
GIF: the contents of the GIF's global colormap are used.
PPM (either text or raw flavor): the entire file is read and
each unique pixel value is entered in the map.
Note that reading a large PPM file will be horrendously slow.
Typically, a PPM-format map file should contain just one pixel
of each desired color. Such a file can be extracted from an
ordinary image PPM file with ppmtomap(1).
Rescaling a PPM that has a maxval unequal to MAXJSAMPLE is not
currently implemented. }
interface
{$I jconfig.inc}
uses
jmorecfg,
cdjpeg, { Common decls for cjpeg/djpeg applications }
jdeferr,
jerror,
jpeglib;
{ Main entry point from djpeg.c.
Input: opened input file (from file name argument on command line).
Output: colormap and actual_number_of_colors fields are set in cinfo. }
{GLOBAL}
{$ifdef QUANT_2PASS_SUPPORTED}
procedure read_color_map (cinfo : j_decompress_ptr; var infile : FILE);
{$endif} { QUANT_2PASS_SUPPORTED }
implementation
{$ifdef QUANT_2PASS_SUPPORTED}
{ otherwise can't quantize to supplied map }
{ Portions of this code are based on the PBMPLUS library, which is:
*
* Copyright (C) 1988 by Jef Poskanzer.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation. This software is provided "as is" without express or
* implied warranty.
}
{ Add a (potentially) new color to the color map. }
{LOCAL}
procedure add_map_entry (cinfo : j_decompress_ptr;
R : int; G : int; B : int);
var
colormap0 : JSAMPROW;
colormap1 : JSAMPROW;
colormap2 : JSAMPROW;
ncolors : int;
index : int;
begin
colormap0 := cinfo^.colormap^[0];
colormap1 := cinfo^.colormap^[1];
colormap2 := cinfo^.colormap^[2];
ncolors := cinfo^.actual_number_of_colors;
{ Check for duplicate color. }
for index := 0 to pred(ncolors) do
begin
if (GETJSAMPLE(colormap0^[index]) = R) and
(GETJSAMPLE(colormap1^[index]) = G) and
(GETJSAMPLE(colormap2^[index]) = B) then
exit; { color is already in map }
end;
{ Check for map overflow. }
if (ncolors >= (MAXJSAMPLE+1)) then
ERREXIT1(j_common_ptr(cinfo), JERR_QUANT_MANY_COLORS, (MAXJSAMPLE+1));
{ OK, add color to map. }
colormap0^[ncolors] := JSAMPLE (R);
colormap1^[ncolors] := JSAMPLE (G);
colormap2^[ncolors] := JSAMPLE (B);
Inc(cinfo^.actual_number_of_colors);
end;
{ Extract color map from a GIF file. }
{LOCAL}
procedure read_gif_map (cinfo : j_decompress_ptr; var infile : file);
var
header : packed array[1..13-1] of byte;
i, colormaplen : int;
var
color : RGBtype;
var
count : int;
begin
{ Initial 'G' has already been read by read_color_map }
{ Read the rest of the GIF header and logical screen descriptor }
blockread(infile, header, 13-1, count);
if (count <> 13-1) then
ERREXIT(j_common_ptr(cinfo), JERR_BAD_CMAP_FILE);
{ Verify GIF Header }
if (header[1] <> byte('I')) or (header[2] <> byte('F')) then
ERREXIT(j_common_ptr(cinfo), JERR_BAD_CMAP_FILE);
{ There must be a global color map. }
if ((header[10] and $80) = 0) then
ERREXIT(j_common_ptr(cinfo), JERR_BAD_CMAP_FILE);
{ OK, fetch it. }
colormaplen := 2 shl (header[10] and $07);
for i := 0 to pred(colormaplen) do
begin
blockread(infile, color, 3, count);
if (count <> 3) then
ERREXIT(j_common_ptr(cinfo), JERR_BAD_CMAP_FILE);
add_map_entry(cinfo,
color.R shl (BITS_IN_JSAMPLE-8),
color.G shl (BITS_IN_JSAMPLE-8),
color.B shl (BITS_IN_JSAMPLE-8));
end;
end;
{$IFDEF PPM}
{ Support routines for reading PPM }
{LOCAL}
function pbm_getc (var infile : FILE) : int;
{ Read next char, skipping over any comments }
{ A comment/newline sequence is returned as a newline }
var
{register} ch : int;
begin
ch := getc(infile);
if (ch = '#') then
begin
repeat
ch := getc(infile);
until not (ch <> '\n') and not EOF(infile);
end;
pbm_get := ch;
end;
{LOCAL}
function read_pbm_integer (cinfo : j_decompress_ptr;
var infile : FILE) : uInt;
{ Read an unsigned decimal integer from the PPM file }
{ Swallows one trailing character after the integer }
{ Note that on a 16-bit-int machine, only values up to 64k can be read. }
{ This should not be a problem in practice. }
var
{register} ch : int;
{register} val : uInt;
begin
{ Skip any leading whitespace }
repeat
ch := pbm_getc(infile);
if EOF(infile) then
ERREXIT(j_common_ptr(cinfo), JERR_BAD_CMAP_FILE);
until (ch <> ' ') and (ch <> '\t') and (ch <> '\n') and (ch <> '\r');
if (ch < '0') or (ch > '9') then
ERREXIT(j_common_ptr(cinfo), JERR_BAD_CMAP_FILE);
val := ch - '0';
ch := pbm_getc(infile);
while (ch >= '0') and (ch <= '9') do
begin
val := val * 10;
Inc(val, ch - '0');
ch := pbm_getc(infile);
end;
read_pbm_integer := val;
end;
{ Extract color map from a PPM file. }
{LOCAL}
procedure read_ppm_map (cinfo : j_decompress_ptr; var infile : FILE);
var
c : int;
w, h, maxval, row, col : uInt;
R, G, B : int;
begin
{ Initial 'P' has already been read by read_color_map }
c := getc(infile); { save format discriminator for a sec }
{ while we fetch the remaining header info }
w := read_pbm_integer(cinfo, infile);
h := read_pbm_integer(cinfo, infile);
maxval := read_pbm_integer(cinfo, infile);
if (w <= 0) or (h <= 0) or (maxval <= 0) then { error check }
ERREXIT(j_common_ptr(cinfo), JERR_BAD_CMAP_FILE);
{ For now, we don't support rescaling from an unusual maxval. }
if (maxval <> (unsigned int) MAXJSAMPLE) then
ERREXIT(j_common_ptr(cinfo), JERR_BAD_CMAP_FILE);
case (c) of
'3': { it's a text-format PPM file }
for row := 0 to pred(h) do
begin
for col := 0 to pred(w) do
begin
R := read_pbm_integer(cinfo, infile);
G := read_pbm_integer(cinfo, infile);
B := read_pbm_integer(cinfo, infile);
add_map_entry(cinfo, R, G, B);
end;
end;
'6': { it's a raw-format PPM file }
for row := 0 to pred(h) do
begin
for col := 0 to pred(w) do
begin
R := pbm_getc(infile);
G := pbm_getc(infile);
B := pbm_getc(infile);
if (R = EOF) or (G = EOF) or (B = EOF) then
ERREXIT(j_common_ptr(cinfo), JERR_BAD_CMAP_FILE);
add_map_entry(cinfo, R, G, B);
end;
end;
else
ERREXIT(j_common_ptr(cinfo), JERR_BAD_CMAP_FILE);
end;
end;
{$ENDIF}
{ Main entry point from djpeg.c.
Input: opened input file (from file name argument on command line).
Output: colormap and actual_number_of_colors fields are set in cinfo. }
{GLOBAL}
procedure read_color_map (cinfo : j_decompress_ptr;
var infile : FILE);
var
ch : char;
begin
{ Allocate space for a color map of maximum supported size. }
cinfo^.colormap := cinfo^.mem^.alloc_sarray
(j_common_ptr (cinfo), JPOOL_IMAGE,
JDIMENSION (MAXJSAMPLE+1), JDIMENSION (3));
cinfo^.actual_number_of_colors := 0; { initialize map to empty }
{ Read first byte to determine file format }
BlockRead(infile, ch, 1);
case ch of
'G': read_gif_map(cinfo, infile);
{$IFDEF PPM}
'P': read_ppm_map(cinfo, infile);
{$ENDIF}
else
ERREXIT(j_common_ptr(cinfo), JERR_BAD_CMAP_FILE);
end;
end;
{$endif} { QUANT_2PASS_SUPPORTED }
end.
|