-
Notifications
You must be signed in to change notification settings - Fork 15
/
SZPCX.pas
422 lines (324 loc) · 9.77 KB
/
SZPCX.pas
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
unit SZPCX;
/////////////////////////////
// Version 1.0.2
/////////////////////////////
{
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
specific language governing rights and limitations under the License.
The original code is SZPCX.pas, released 19. July, 2004.
The initial developer of the original code is
Sasa Zeman ([email protected], www.szutils.net)
Copyright(C) 2004-2006 Sasa Zeman. All Rights Reserved.
}
{--------------------------------------------------------------------
- Simple and fast reader of PCX format.
- Capable to read B/W, 16, 256 and 16M colors
- Register as a common graphical format in Delphi (TPicture support)
TODO:
- Encoding to PCX format.
- Full Assign support
Revision History:
----------------------------------
Version 1.0.2, 2006-12-06
- Memory leak fixed
Version 1.0.1, 2006-12-01
- Some small class rearanging and source publishing
Version 1.0.0, 2004-07-19
- Initial version
----------------------------------
Author : Sasa Zeman
E-mail : [email protected] or [email protected]
Web site : www.szutils.net
}
interface
uses SysUtils,Windows,Classes,Graphics;
type
TSZPCXPixelFormat=(SZPCXpfUnknown, SZPCXpf1bit, SZPCXpf4bit, SZPCXpf8bit, SZPCXpf24bit);
TRGBColor = packed record
R, G, B: byte;
end;
TArr8RGB = array[1..8] of TRGBColor;
VGAPalette = array[0..255] of TRGBColor;
PRGBArray=^TRGBArray;
TRGBArray= array[0..0] of TRGBColor;
TSZPCXHeader = Record
Manufacturer : byte; {Const 10 = ZSoft}
Version : byte; {0 = 2.5 PC Paintbrush}
{2 = 2.8 PC Paintbrush with palette info}
{3 = 2.8 PC Paintbrush without palette info}
{4 = PC Paintbrush for Windows}
{5 = 3.0 PC Paintbrush, Paintbrush Plus,
Publisher's Paintbrush}
Encode : byte; {1 = Run-Length compression}
BitsPerPixel : byte; {Bits per pixel
1 - 16 color
2 -
4,8 - 256 and 16M}
Window : record
Xmin,Ymin,
Xmax,Ymax: word;
end; {Picture dimension}
HDPI : word; {Horizontal resolution in DPI}
VDPI : word; {Vertical resolution in DPI}
ColorMap : array[0..15] of TRGBColor;
{Color palette - only on 16 color map}
{768 last bytes of file for color palette
above 16 color - 1 byte before must be 12}
Reserved : byte; {Reserved = 0}
NPlanes : byte; {Number of bits for planes
1 - 256 color
3 - 16M color
4 - 16 color }
BytesPerLine : word; {Bytes per line - mod 2 = 0}
PaletteInfo : word; {1 - Standard color/BW palette}
{2 - Grayscale}
HScreenSize : word;
VScreenSize : word;
Filler : array [0..53] of byte;
End;
TSZPCX = class(TBitmap)
private
Header : TSZPCXHeader;
VGApal : VGAPalette;
RawData : TMemoryStream;
OutData : Pointer;
pRawData, pOutData: PByte;
pR,pG,pB: PByte;
PCXPixelFormat: TSZPCXPixelFormat;
procedure DecodeBytes(pOutData: pByte; TotalBytes : integer);
procedure ReadLine(LineNum,TotalBytes: integer);
function GetPixelFormat: TSZPCXPixelFormat;
public
FBitmap: TBitmap;
constructor Create; override;
destructor Destroy; override;
procedure LoadFromFile( Const Filename: String); override;
procedure LoadFromStream(Stream: TStream); override;
procedure ReadAll;
end;
TSZPCXImage=class(TBitmap)
Public
constructor Create; override;
destructor Destroy; override;
procedure LoadFromStream(Stream: TStream); override;
end;
implementation
const
Bin: array[0..7] of byte =
(1,2,4,8,16,32,64,128);
constructor TSZPCX.Create;
begin
FBitmap:=TBitmap.Create;
RawData:=TMemoryStream.Create;
end;
destructor TSZPCX.Destroy;
begin
RawData.Free;
FBitmap.Free;
inherited destroy;
end;
function TSZPCX.GetPixelFormat: TSZPCXPixelFormat;
begin
result := SZPCXpfUnknown;
case Header.BitsPerPixel of
8 : begin
if (Header.PaletteInfo = 1) and
(Header.Nplanes = 3)
then
// True color
result := SZPCXpf24bit
else
// 256 color
result := SZPCXpf8bit;
end;
4: begin
// 16 color
result := SZPCXpf4bit;
end;
1: begin
// BW color
result := SZPCXpf1bit;
end;
end;
end;
procedure TSZPCX.DecodeBytes(pOutData: pByte; TotalBytes : integer);
var
b: byte;
bytes: integer;
a: PByte;
begin
bytes:=0;
while bytes<TotalBytes do
begin
a:= pRawData; inc(pRawData);
if (a^ and $C0)=$C0 then
begin
b := a^ and $3F;
a := pRawData; inc(pRawData);
end else
b:=1;
if b>1 then
begin
FillChar(pOutData^,b,a^);
inc(pOutData,b);
inc(bytes,b);
end else
repeat
pOutData^:=a^; inc(pOutData);
inc(bytes);
dec(b);
until b=0;
end;
end;
procedure TSZPCX.LoadFromFile(Const Filename: String);
begin
RawData.LoadFromFile(Filename);
pRawData:=RawData.Memory;
ReadAll;
end;
procedure TSZPCX.LoadFromStream(Stream: TStream);
begin
RawData.LoadFromStream(Stream);
ReadAll;
end;
procedure TSZPCX.ReadLine(LineNum, TotalBytes: integer);
var
bytes: integer;
P:^TRGBTriple;
P1Byte: PByte;
begin
pOutData:=OutData;
DecodeBytes(pOutData,TotalBytes);
pOutData:=OutData;
P1Byte := FBitmap.ScanLine[LineNum];
case PCXPixelFormat of
SZPCXpf1bit,
SZPCXpf4bit,
SZPCXpf8bit: CopyMemory(p1Byte,pOutData,Header.BytesPerLine);
SZPCXpf24bit:
begin
pR:=pOutData;
pG:=pR; inc(pG,Header.BytesPerLine);
pB:=pG; inc(pB,Header.BytesPerLine);
P:=@(P1Byte^);
for bytes:=1 to Header.BytesPerLine do
begin
p.rgbtRed := pR^; inc(pR);
p.rgbtGreen := pG^; inc(pG);
p.rgbtBlue := pB^; inc(pB);
inc(p)
end;
end
end
end;
procedure TSZPCX.ReadAll;
var
i,y: integer;
n: byte;
Size: record
X,Y:word;
end;
TotalBytes: integer;
LogPalette: TMaxLogPalette;
begin
RawData.Seek(0,soFromBeginning);
RawData.Read(header,sizeof(TSZPCXHeader));
with Header do
begin
case PaletteInfo of
0,1: begin
if (Version>=5)then
begin
//RawData.Seek(RawData.Size-769,soFromBeginning);
RawData.Seek(-769,soFromEnd);
RawData.Read(n,1);
if n=12
then RawData.read(VGApal,768)
else Move(Header.ColorMap,VGAPal,48)
end else
Move(Header.ColorMap,VGAPal,48);
end;
end;
RawData.seek(128,soFromBeginning);
with Window do
begin
Size.X := XMax - XMin + 1;
Size.Y := YMax - YMin + 1;
end;
PCXPixelFormat:=GetPixelFormat;
with FBitmap do
begin
Assign(nil);
case PCXPixelFormat of
SZPCXpf1bit: PixelFormat := pf1bit;
SZPCXpf4bit: PixelFormat := pf4bit;
SZPCXpf8bit: PixelFormat := pf8bit;
SZPCXpf24bit: PixelFormat := pf24bit;
else
PixelFormat := pf24bit;
end;
Width :=Size.X;
Height:=Size.Y;
// Create palette
if PCXPixelFormat in [SZPCXpf4bit,SZPCXpf8bit] then
begin
FillChar(LogPalette, SizeOf(LogPalette), 0);
LogPalette.palVersion := $300;
if PCXPixelFormat= SZPCXpf8bit then
LogPalette.palNumEntries := 256;
if PCXPixelFormat= SZPCXpf4bit then
LogPalette.palNumEntries := 48;
for i:=0 to LogPalette.palNumEntries-1 do
begin
LogPalette.palPalEntry[i].peRed := VGAPal[i].R;
LogPalette.palPalEntry[i].peGreen := VGAPal[i].G;
LogPalette.palPalEntry[i].peBlue := VGAPal[i].B;
end;
FBitmap.Palette := CreatePalette(PLogPalette(@LogPalette)^);
end;
end;
TotalBytes := NPlanes * BytesPerLine;
//point to the first dot data
pRawData:=RawData.Memory;
inc(pRawData,128);
if PCXPixelFormat = SZPCXpf24bit then
begin
pR := pRawData;
pG := pR; inc(pG,Header.BytesPerLine);
pB := pG; inc(pB,Header.BytesPerLine);
end;
GetMem(OutData, TotalBytes);
for y:=0 to Size.Y-1 do
ReadLine(y,TotalBytes);
FreeMem(OutData);
end;
end;
constructor TSZPCXImage.Create;
begin
inherited Create;
end;
destructor TSZPCXImage.Destroy;
begin
inherited Destroy
end;
procedure TSZPCXImage.LoadFromStream(Stream: TStream);
var
PCX: TSZPCX;
begin
PCX:=TSZPCX.Create;
try
PCX.RawData.LoadFromStream(Stream);
PCX.ReadAll;
Assign(PCX.FBitmap);
except end;
PCX.Free;
end;
initialization
TPicture.RegisterFileFormat('pcx', 'PCX Image File (SZ)', TSZPCXImage);
finalization
TPicture.UnregisterGraphicClass(TSZPCXImage);
end.