-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPCX256.PAS
263 lines (245 loc) · 6.42 KB
/
PCX256.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
Unit PCX256;
INTERFACE
uses ggraph,gbasics,tmaths,ttypes,diskop,colour;
const
{////PICTURE ATTRIBUTES////}
PAflat=BMflat;
PAPlanes=BMPlanes;
PACheckVidMode=0; {DDB}
PAtypemask=BMflat+BMPlanes;
PAcolcheck=4;
type
pcx_header=record
header:byte;
version:byte; {5 for extra colours at end of file}
encoding:byte; {1 for runlength}
bits_per_pixel:byte;
window:trect;
resolution:pointtype;
cmap:array[0..15]of RGBtype;
reserved:byte;
numplanes:byte;
offset:integer; {screen width in bytes}
PaletteInfo:integer; {1 =colour/bw, 2=greyscale}
screensize:pointtype;
filler:array[0..53]of byte;
end;
var
LCmap:bytearray;
header:pcx_header;
const
no_bytes_back=769;
Check_256Val=12;
chkbits=$C0;
maskbits=255-chkbits;
Function readPCX(filename:string;Pattrib:byte):pointer;
Procedure writePCX(pic:bitmaptypeptr;filename:string);
IMPLEMENTATION
Procedure writePCX(pic:bitmaptypeptr;filename:string);
var
writecol:rgbtype;
lop:byte;
xlop,ylop,runsize,subtotal:integer;
writeheader:pcx_header;
f:file;
recsize,bufsize:word;
linebuf,currbyte:^byte;
temp,oldbyte,runcolour:byte;
procedure endrun;
begin
currbyte^:=chkbits or runsize;
inc(currbyte);
inc(bufsize);
currbyte^:=runcolour;
runsize:=0;
{on off subtotal count -there was a 'run flag' byte}
end;
begin
recsize:=1;
assign(f,filename);
rewrite(f,recsize);
with writeheader,pic^ do begin
header:=$0a;
version:=5;
encoding:=1;
bits_per_pixel:=8;
window.rassign(0,0,size.x,size.y-1);
resolution.x:=1+sc.screenport.x2-sc.screenport.x1;
resolution.y:=1+sc.screenport.y2-sc.screenport.y1;
for lop:=0 to 15 do with cmap[lop] do
getrgb(lop,r,g,b);
reserved:=0;
numplanes:=1;
offset:=size.x;
if offset mod 2<>0 then inc(offset);
PaletteInfo:=1;
screensize:=resolution;
fillchar(filler,54,0);
blockwrite(f,writeheader,sizeof(writeheader),recsize);
{reset internal picture coords}
Qsetxy(0,0);
getmem(linebuf,size.x);
{blockwrite(f,pic^,planesize,recsize);}
for ylop:=1 to size.y do begin
currbyte:=linebuf;
bufsize:=0;
oldbyte:=currbyte^+1;
runsize:=0;
subtotal:=0;
while (subtotal<offset) do begin
currbyte^:=qget;
temp:=currbyte^;
if oldbyte<>temp then begin
{end an ongoing run?}
if runsize>0 then
endrun;
{no run}
inc(currbyte);
inc(bufsize);
if temp and chkbits<>0 then dec(temp);
oldbyte:=temp;
{inc(subtotal);
{reset runsize}
end else if runsize<=64 then begin
{run in progress...}
if runsize=0 then runcolour:=temp;
inc(runsize);
{inc(subtotal);}
end else
endrun;
end;
{end run if currently in one}
if runsize>0 then endrun;
blockwrite(f,linebuf^,bufsize,recsize);
end;
freemem(linebuf,size.x);
t_col:=check_256val;
blockwrite(f,t_col,1,recsize);
for lop:=0 to 255 do with writecol do begin
getrgb(lop,r,g,b);
blockwrite(f,writecol,3,recsize);
end;
end;
close(f);
end;
Function readPCX(filename:string;pattrib:byte):pointer;
var
buf:bufferedblock;
xln,yln,xpos,ypos:integer;
tmpRGB:RGBtype;
tmpcolref:byte;
bmtype:byte;
lop,chk,loadbyte:byte;
subtotal,scanlop,xlop,palsize:word;
oldfilepos:word;
tmp:byteptr;
pic:bitmaptypeptr;
begin
buf.open(filename);
buf.blockread(@header,sizeof(pcx_header));
with header do begin
xln:=1+(window.x2-window.x1);
yln:=1+(window.y2-window.y1);
if memavail<(((xln shr 3)*yln) *4) then begin
buf.close;
readpcx:=nil;
end;
{PCX files xoffset needs to be even}
if xln mod 2<>0 then inc(xln);
bmtype:=Pattrib and PAtypemask;
case bmtype of
PAcheckvidmode:
if SC.colourdepth>16 then
pic:=createBitmap(xln,yln,8,BMflat)
else
pic:=createBitmap(xln,yln,4,BMplanes);
PAflat:
pic:=createBitmap(xln,yln,8,BMflat);
PAPlanes:
pic:=createBitmap(xln,yln,4,BMplanes);
end;
SetBitmapContext(pic);
palsize:=0;
if (bits_per_pixel<=4) then begin
palsize:=16;
Getmem(LCmap,palsize);
for lop:=0 to 15 do begin
with Cmap[lop] do
LCmap^[lop]:=AllocateRGB(r shr 2,g shr 2,b shr 2);
end
end else begin
oldfilepos:=buf.fpos;
buf.fseek(filesize(buf.f)-no_bytes_back);
chk:=buf.get;
if (chk=check_256val) then begin
palsize:=256;
Getmem(LCmap,palsize);
for lop:=0 to 255 do begin
buf.blockread(@TmpRGB,3);
with TmpRGB do
LCmap^[lop]:=AllocateRGB(r shr 2,g shr 2,b shr 2);
end;
end;
buf.fseek(oldfilepos+1);
end;
{begin image decode}
xpos:=0;
ypos:=0;
if bits_per_pixel=8 then begin
for scanlop:=0 to pic^.size.y-1 do begin
subtotal:=0;
while subtotal<offset do begin
chk:=buf.get;
if ((chk and chkbits)=chkbits)and(encoding=1) then begin
chk:=chk and maskbits;
if (pattrib and PAcolcheck <>0) then
t_col:=LCmap^[buf.get]
else
t_Col:=buf.get;
for xpos:=xpos to xpos+chk do
BMputpixel(xpos,ypos);
inc(subtotal,chk);
end else begin
if (pattrib and PAcolcheck <>0) then
t_col:=LCmap^[chk]
else
t_col:=chk;
BMputpixel(xpos,ypos);
inc(xpos);
inc(subtotal);
end;
end;
inc(ypos);
xpos:=0;
end;
end else begin
{begin image decode}
{getmem(buf,offset*bitplanes);
for scanlop:=0 to size.y-1 do begin
for currplane:=0 to bitplanes-1 do begin
subtotal:=0;
while subtotal<offset do begin
chk:=getbyte;
if ((chk and chkbits)=chkbits)and(encoding=1) then begin
chk:=chk and maskbits;
loadbyte:=getbyte;
fillchar(count[currplane]^,chk,loadbyte);
inc(count[currplane],chk);
inc(subtotal,chk);
end else begin
count[currplane]^:=chk;
inc(subtotal);
inc(count[currplane]);
end;
end;
end;
end;
getmem(buf,offset*bitplanes);}
end;
if palsize<>0 then
freemem(LCmap,palsize);
end;
buf.close;
readPCX:=pic;
end;
end.