| 1 |
unit data2; |
| 2 |
(******************************************************************** |
| 3 |
This program is free software: you can redistribute it and/or modify |
| 4 |
it under the terms of the GNU General Public License as published by |
| 5 |
the Free Software Foundation, either version 3 of the License, or |
| 6 |
(at your option) any later version. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, |
| 9 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License |
| 14 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
On Debian systems, the complete text of the GNU General Public |
| 17 |
License, version 3, can be found in /usr/share/common-licenses/GPL-3. |
| 18 |
********************************************************************) |
| 19 |
|
| 20 |
{********************************************* |
| 21 |
Data Generator: shared structs for graphic conversions |
| 22 |
CPR bits mostly copied from data.pas and utils_.pas, TARGA code is by Matija Nalis |
| 23 |
|
| 24 |
Copyright: |
| 25 |
1994 Channel 7, Destiny: Virtual |
| 26 |
2013 y-salnikov |
| 27 |
2020 Matija Nalis <[email protected]> |
| 28 |
**********************************************} |
| 29 |
|
| 30 |
{$I-} |
| 31 |
|
| 32 |
interface |
| 33 |
{$PACKRECORDS 1} |
| 34 |
|
| 35 |
const { compression constants } |
| 36 |
CPR_VER4=4; { 4 new header } |
| 37 |
CPR_ERROR=255; { global error } |
| 38 |
CPR_CURRENT=CPR_VER4; { current version } |
| 39 |
CPR_BUFFSIZE= 8192; { adjustable buffer size } |
| 40 |
|
| 41 |
type |
| 42 |
CPR_HEADER= |
| 43 |
record |
| 44 |
signature: word; {RWM, no version. RM, version } |
| 45 |
version: byte; |
| 46 |
width,height: word; |
| 47 |
flags: byte; |
| 48 |
headersize: byte; |
| 49 |
end; |
| 50 |
pCPR_HEADER= ^CPR_HEADER; |
| 51 |
|
| 52 |
type |
| 53 |
TGA_HEADER= |
| 54 |
record |
| 55 |
id_len: byte; |
| 56 |
cmap_type: byte; |
| 57 |
img_type: byte; |
| 58 |
cmap_ofs: word; |
| 59 |
cmap_len: word; |
| 60 |
cmap_esize: byte; |
| 61 |
x_org, y_org: word; |
| 62 |
width, height: word; |
| 63 |
pixel_depth: byte; |
| 64 |
img_descriptor_b: byte; |
| 65 |
img_id: dword; |
| 66 |
end; |
| 67 |
pTGA_HEADER= ^TGA_HEADER; |
| 68 |
|
| 69 |
type |
| 70 |
screentype= array[0..199,0..319] of byte; |
| 71 |
paltype=array[0..255,1..3] of byte; |
| 72 |
pscreentype= ^screentype; |
| 73 |
|
| 74 |
var |
| 75 |
colors: paltype; |
| 76 |
screen: screentype; |
| 77 |
has_pal: boolean; |
| 78 |
cpr_head: CPR_HEADER; |
| 79 |
tga_head: TGA_HEADER; |
| 80 |
|
| 81 |
procedure quicksavescreen(s : String; scr : pscreentype; savepal : Boolean); |
| 82 |
procedure quickloadscreen(s : String; scr : pscreentype; loadpal : Boolean); |
| 83 |
procedure loadscreen(s: string; ts: pointer); |
| 84 |
procedure compressfile(s: string; ts: pscreentype; w2,h2:word; fl: byte); |
| 85 |
procedure errorhandler(s: string; errtype: integer); |
| 86 |
procedure loadpal(s: string); |
| 87 |
procedure savetga(s: string; ts: pscreentype); |
| 88 |
procedure loadtga(s: string); |
| 89 |
|
| 90 |
|
| 91 |
implementation |
| 92 |
|
| 93 |
uses sysutils; |
| 94 |
|
| 95 |
procedure errorhandler(s: string; errtype: integer); |
| 96 |
begin |
| 97 |
writeln; |
| 98 |
case errtype of |
| 99 |
1: writeln(StdErr, 'Open File Error: ',s,' (ioresult=',ioresult,')'); |
| 100 |
2: writeln(StdErr, 'Mouse Error: ',s); |
| 101 |
3: writeln(StdErr, 'Sound Error: ',s); |
| 102 |
4: writeln(StdErr, 'EMS Error: ',s); |
| 103 |
5: writeln(StdErr, 'Fatal File Error: ',s,' (ioresult=',ioresult,')'); |
| 104 |
6: writeln(StdErr, 'Program Error: ',s); |
| 105 |
7: writeln(StdErr, 'Music Error: ',s); |
| 106 |
end; |
| 107 |
halt(4); |
| 108 |
end; |
| 109 |
|
| 110 |
procedure uncompressfile(s: string; ts: pscreentype; h: pCPR_HEADER); |
| 111 |
type |
| 112 |
buftype= array[0..CPR_BUFFSIZE] of byte; |
| 113 |
var |
| 114 |
f: file; |
| 115 |
err,num,count,databyte,index,x: word; |
| 116 |
total,totalsize,j: longint; |
| 117 |
buffer: ^buftype; |
| 118 |
|
| 119 |
procedure handleerror(s:string); |
| 120 |
begin |
| 121 |
writeln (StdErr, 'handleerror: '+s); |
| 122 |
h^.version:=CPR_ERROR; |
| 123 |
if buffer<>nil then dispose(buffer); |
| 124 |
buffer:=nil; |
| 125 |
close(f); |
| 126 |
j:=ioresult; |
| 127 |
end; |
| 128 |
|
| 129 |
procedure getbuffer; |
| 130 |
begin |
| 131 |
if total>CPR_BUFFSIZE then num:=CPR_BUFFSIZE else num:=total; |
| 132 |
blockread(f,buffer^,num,err); |
| 133 |
if (err<num) or (ioresult<>0) then |
| 134 |
begin |
| 135 |
handleerror('getbuffer'); |
| 136 |
exit; |
| 137 |
end; |
| 138 |
total:=total-num; |
| 139 |
index:=0; |
| 140 |
end; |
| 141 |
|
| 142 |
function handleversion(n: integer): boolean; |
| 143 |
begin |
| 144 |
handleversion:=false; |
| 145 |
writeln (' file using sig=',chr(lo(h^.signature)), chr(hi(h^.signature)), ' v=',h^.version, ' width=',h^.width, ' height=', h^.height, ' flags=', h^.flags); |
| 146 |
if n<>4 then exit; |
| 147 |
has_pal := false; |
| 148 |
if h^.flags and 1>0 then |
| 149 |
begin |
| 150 |
has_pal := true; |
| 151 |
num:=768; |
| 152 |
seek(f,h^.headersize); |
| 153 |
blockread(f,colors,num,err); |
| 154 |
if (ioresult<>0) or (num<>err) then exit; |
| 155 |
total:=filesize(f)-768-h^.headersize; |
| 156 |
end |
| 157 |
else total:=filesize(f)-h^.headersize; |
| 158 |
seek(f,filesize(f)-total); |
| 159 |
if ioresult<>0 then exit; |
| 160 |
handleversion:=true; |
| 161 |
end; |
| 162 |
|
| 163 |
function checkversion: boolean; |
| 164 |
begin |
| 165 |
checkversion:=false; |
| 166 |
num:=sizeof(CPR_HEADER); |
| 167 |
blockread(f,h^,num,err); |
| 168 |
if (err<num) or (ioresult<>0) or (h^.signature<>19794) |
| 169 |
or (not handleversion(h^.version)) then exit; |
| 170 |
checkversion:=true; |
| 171 |
end; |
| 172 |
|
| 173 |
function decode: boolean; |
| 174 |
begin |
| 175 |
decode:=false; |
| 176 |
getbuffer; |
| 177 |
j:=0; |
| 178 |
totalsize:=h^.width; |
| 179 |
totalsize:=totalsize*h^.height; |
| 180 |
// writeln(h^.width,' ',h^.height); |
| 181 |
x:=0; |
| 182 |
repeat |
| 183 |
if buffer^[index]=255 then |
| 184 |
begin |
| 185 |
inc(index); |
| 186 |
if index=CPR_BUFFSIZE then getbuffer; |
| 187 |
count:=buffer^[index]; |
| 188 |
inc(index); |
| 189 |
if index=CPR_BUFFSIZE then getbuffer; |
| 190 |
databyte:=buffer^[index]; |
| 191 |
if j+count>totalsize then count:=totalsize-j; |
| 192 |
j:=j+count; |
| 193 |
while count>0 do |
| 194 |
begin |
| 195 |
ts^[0,x]:=databyte; |
| 196 |
inc(x); |
| 197 |
dec(count); |
| 198 |
end; |
| 199 |
end |
| 200 |
else |
| 201 |
begin |
| 202 |
databyte:=buffer^[index]; |
| 203 |
ts^[0,x]:=databyte; |
| 204 |
inc(j); |
| 205 |
inc(x); |
| 206 |
end; |
| 207 |
inc(index); |
| 208 |
if index=CPR_BUFFSIZE then getbuffer; |
| 209 |
until j=totalsize; |
| 210 |
decode:=true; |
| 211 |
end; |
| 212 |
|
| 213 |
begin |
| 214 |
new(buffer); |
| 215 |
assign(f,s); |
| 216 |
reset(f,1); |
| 217 |
if ioresult<>0 then |
| 218 |
begin |
| 219 |
handleerror('open'); |
| 220 |
exit; |
| 221 |
end; |
| 222 |
if not checkversion then |
| 223 |
begin |
| 224 |
handleerror('checkversion'); |
| 225 |
exit; |
| 226 |
end; |
| 227 |
if not decode then |
| 228 |
begin |
| 229 |
handleerror('decode'); |
| 230 |
exit; |
| 231 |
end; |
| 232 |
close(f); |
| 233 |
if buffer<>nil then dispose(buffer); |
| 234 |
end; |
| 235 |
|
| 236 |
procedure loadscreen(s: string; ts: pointer); |
| 237 |
begin |
| 238 |
uncompressfile(s+'.cpr',ts,@cpr_head); |
| 239 |
if cpr_head.version=CPR_ERROR then errorhandler(s+'.cpr CPR_ERROR',5); |
| 240 |
end; |
| 241 |
|
| 242 |
procedure compressfile(s: string; ts: pscreentype; w2,h2:word; fl: byte); |
| 243 |
type |
| 244 |
buftype= array[0..CPR_BUFFSIZE] of byte; |
| 245 |
var |
| 246 |
f: file; |
| 247 |
err,num,count,databyte,j,x,index: word; |
| 248 |
buf: ^buftype; |
| 249 |
h: CPR_HEADER; |
| 250 |
|
| 251 |
procedure handleerror(s:string); |
| 252 |
begin |
| 253 |
writeln (StdErr, 'handleerror: '+s); |
| 254 |
if buf<>nil then dispose(buf); |
| 255 |
buf:=nil; |
| 256 |
close(f); |
| 257 |
j:=ioresult; |
| 258 |
end; |
| 259 |
|
| 260 |
procedure setheader(w2,h2:word; fl:byte); |
| 261 |
begin |
| 262 |
with h do |
| 263 |
begin |
| 264 |
signature:=19794; |
| 265 |
version:=CPR_CURRENT; |
| 266 |
headersize:=sizeof(CPR_HEADER); |
| 267 |
width:=w2; |
| 268 |
height:=h2; |
| 269 |
flags:=fl; |
| 270 |
end; |
| 271 |
num:=sizeof(CPR_HEADER); |
| 272 |
blockwrite(f,h,num,err); |
| 273 |
if (err<num) or (ioresult<>0) then errorhandler(s+'.cpr setheader',5); |
| 274 |
if h.flags and 1>0 then |
| 275 |
begin |
| 276 |
num:=768; |
| 277 |
blockwrite(f,colors,num,err); |
| 278 |
if (ioresult<>0) or (err<num) then errorhandler(s+'.cpr palette',5); |
| 279 |
end; |
| 280 |
end; |
| 281 |
|
| 282 |
procedure saveindex; |
| 283 |
begin |
| 284 |
num:=index; |
| 285 |
blockwrite(f,buf^,num,err); |
| 286 |
if (ioresult<>0) or (num<>err) then |
| 287 |
begin |
| 288 |
handleerror('saveindex'); |
| 289 |
exit; |
| 290 |
end; |
| 291 |
index:=0; |
| 292 |
end; |
| 293 |
|
| 294 |
begin |
| 295 |
new(buf); |
| 296 |
assign(f,s+'.cpr'); |
| 297 |
rewrite(f,1); |
| 298 |
if ioresult<>0 then errorhandler(s+'.cpr',1); |
| 299 |
setheader(w2,h2,fl); |
| 300 |
databyte:=ts^[0,0]; |
| 301 |
count:=0; |
| 302 |
index:=0; |
| 303 |
x:=0; |
| 304 |
repeat |
| 305 |
count:=0; |
| 306 |
databyte:=ts^[0,x]; |
| 307 |
while (ts^[0,x]=databyte) and (x<w2*h2) do |
| 308 |
begin |
| 309 |
inc(count); |
| 310 |
inc(x); |
| 311 |
end; |
| 312 |
if (count<4) and (databyte<255) then |
| 313 |
for j:=1 to count do |
| 314 |
begin |
| 315 |
buf^[index]:=databyte; |
| 316 |
inc(index); |
| 317 |
if index=CPR_BUFFSIZE then saveindex; |
| 318 |
end |
| 319 |
else |
| 320 |
begin |
| 321 |
while count>255 do |
| 322 |
begin |
| 323 |
buf^[index]:=255; |
| 324 |
inc(index); |
| 325 |
if index=CPR_BUFFSIZE then saveindex; |
| 326 |
buf^[index]:=255; |
| 327 |
inc(index); |
| 328 |
if index=CPR_BUFFSIZE then saveindex; |
| 329 |
buf^[index]:=databyte; |
| 330 |
inc(index); |
| 331 |
if index=CPR_BUFFSIZE then saveindex; |
| 332 |
dec(count,255); |
| 333 |
end; |
| 334 |
if (count<4) and (databyte<255) then |
| 335 |
for j:=1 to count do |
| 336 |
begin |
| 337 |
buf^[index]:=databyte; |
| 338 |
inc(index); |
| 339 |
if index=CPR_BUFFSIZE then saveindex; |
| 340 |
end |
| 341 |
else |
| 342 |
begin |
| 343 |
buf^[index]:=255; |
| 344 |
inc(index); |
| 345 |
if index=CPR_BUFFSIZE then saveindex; |
| 346 |
buf^[index]:=count; |
| 347 |
inc(index); |
| 348 |
if index=CPR_BUFFSIZE then saveindex; |
| 349 |
buf^[index]:=databyte; |
| 350 |
inc(index); |
| 351 |
if index=CPR_BUFFSIZE then saveindex; |
| 352 |
end; |
| 353 |
end; |
| 354 |
until x=w2*h2; |
| 355 |
saveindex; |
| 356 |
close(f); |
| 357 |
if buf<>nil then dispose(buf); |
| 358 |
end; |
| 359 |
|
| 360 |
procedure loadpal(s: string); |
| 361 |
var palfile: file of paltype; |
| 362 |
begin |
| 363 |
assign(palfile,s); |
| 364 |
reset(palfile); |
| 365 |
if ioresult<>0 then errorhandler(s,1); |
| 366 |
read(palfile,colors); |
| 367 |
if ioresult<>0 then errorhandler(s,5); |
| 368 |
close(palfile); |
| 369 |
end; |
| 370 |
|
| 371 |
procedure quicksavescreen(s : String; scr : pscreentype; savepal : Boolean); |
| 372 |
var |
| 373 |
fs : file of screentype; |
| 374 |
fp : file of paltype; |
| 375 |
begin |
| 376 |
assign(fs, s + '.scr'); |
| 377 |
rewrite(fs); |
| 378 |
if ioresult<>0 then errorhandler(s + '.scr', 1); |
| 379 |
write(fs, scr^); |
| 380 |
if ioresult<>0 then errorhandler(s + '.scr', 5); |
| 381 |
close(fs); |
| 382 |
if savepal then |
| 383 |
begin |
| 384 |
assign(fp, s + '.pal'); |
| 385 |
rewrite(fp); |
| 386 |
if ioresult<>0 then errorhandler(s + '.pal', 1); |
| 387 |
write(fp, colors); |
| 388 |
if ioresult<>0 then errorhandler(s + '.pal', 5); |
| 389 |
close(fp); |
| 390 |
end; |
| 391 |
end; |
| 392 |
|
| 393 |
procedure quickloadscreen(s : String; scr : pscreentype; loadpal : Boolean); |
| 394 |
var |
| 395 |
fs : file of screentype; |
| 396 |
fp : file of paltype; |
| 397 |
begin |
| 398 |
assign(fs, s + '.scr'); |
| 399 |
reset(fs); |
| 400 |
if ioresult<>0 then errorhandler(s + '.scr', 1); |
| 401 |
read(fs, scr^); |
| 402 |
if ioresult<>0 then errorhandler(s + '.scr', 5); |
| 403 |
close(fs); |
| 404 |
if loadpal then |
| 405 |
begin |
| 406 |
assign(fp, s + '.pal'); |
| 407 |
reset(fp); |
| 408 |
if ioresult<>0 then errorhandler(s + '.pal', 1); |
| 409 |
read(fp, colors); |
| 410 |
if ioresult<>0 then errorhandler(s + '.pal', 5); |
| 411 |
close(fp); |
| 412 |
end; |
| 413 |
end; |
| 414 |
|
| 415 |
{ save TARGA file } |
| 416 |
procedure savetga(s: string; ts: pscreentype); |
| 417 |
var written,num: LongInt; |
| 418 |
f: file; |
| 419 |
|
| 420 |
procedure setheader; |
| 421 |
begin |
| 422 |
with tga_head do |
| 423 |
begin |
| 424 |
id_len := 4; { 4 byte ID field } |
| 425 |
cmap_type := 1; { we use colormap/palette } |
| 426 |
img_type := 1; { 1 = uncompressed color-mapped image } |
| 427 |
cmap_ofs := 0; { we start at beginning of palette } |
| 428 |
cmap_len := 256; { 256 palette entries... } |
| 429 |
cmap_esize := 24; { ...of 3 bytes each } |
| 430 |
x_org := 0; |
| 431 |
y_org := 0; |
| 432 |
width := cpr_head.width; { copy width from .CPR ... } |
| 433 |
height := cpr_head.height; { ... and height } |
| 434 |
pixel_depth := 8; { 8 bpp index } |
| 435 |
img_descriptor_b := 32; { bit 5=1, bit 4=0: image origin is top-left } |
| 436 |
img_id := 808603213; { ID } |
| 437 |
end; |
| 438 |
num:=sizeof(TGA_HEADER); |
| 439 |
blockwrite(f,tga_head,num,written); |
| 440 |
if (written<num) or (ioresult<>0) then errorhandler(s+'.tga header',5); |
| 441 |
end; |
| 442 |
|
| 443 |
begin |
| 444 |
assign(f,s+'.tga'); |
| 445 |
rewrite(f,1); |
| 446 |
if ioresult<>0 then errorhandler(s+'.tga create',1); |
| 447 |
setheader(); |
| 448 |
|
| 449 |
{ write palette } |
| 450 |
num:=768; |
| 451 |
blockwrite(f,colors,num,written); |
| 452 |
if (ioresult<>0) or (written<num) then errorhandler(s+'.tga palette',5); |
| 453 |
|
| 454 |
{ write image } |
| 455 |
num:=tga_head.width * tga_head.height; { 64000 for 320x200 image } |
| 456 |
repeat |
| 457 |
blockwrite(f,ts^,num,written); |
| 458 |
if (ioresult<>0) then errorhandler(s+'.tga img write',5); |
| 459 |
dec (num, written); |
| 460 |
inc (ts, written); |
| 461 |
until num=0; |
| 462 |
if (ioresult<>0) or (written<num) then errorhandler(s+'.tga img '+IntToStr(written)+'<>'+IntToStr(num),5); |
| 463 |
|
| 464 |
{ finish } |
| 465 |
close(f); |
| 466 |
if (ioresult<>0) then errorhandler(s+'.tga finish',5); |
| 467 |
end; |
| 468 |
|
| 469 |
{ load TARGA file } |
| 470 |
procedure loadtga(s: string); |
| 471 |
var err,num,y: LongInt; |
| 472 |
f: file; |
| 473 |
|
| 474 |
function checkversion: boolean; |
| 475 |
begin |
| 476 |
checkversion:=false; |
| 477 |
num:=18; // minimum size of used part of TGA_HEADER |
| 478 |
blockread(f,tga_head,num,err); |
| 479 |
if (err<num) or (ioresult<>0) |
| 480 |
or (tga_head.cmap_type<>1) or (tga_head.img_type<>1) or (tga_head.cmap_ofs<>0) or (tga_head.cmap_len<>256) or (tga_head.cmap_esize<>24) |
| 481 |
or (tga_head.x_org<>0) or (tga_head.y_org<>0) or (tga_head.width>320) or (tga_head.height>200) or (tga_head.pixel_depth<>8) or ((tga_head.img_descriptor_b and not (1 shl 5))<>0) |
| 482 |
then exit; |
| 483 |
checkversion:=true; |
| 484 |
end; |
| 485 |
|
| 486 |
procedure saferead(var f:file; ts:pbyte; num:LongInt); |
| 487 |
begin |
| 488 |
repeat |
| 489 |
blockread(f,ts^,num,err); |
| 490 |
if (ioresult<>0) then errorhandler(s+' img read',5); |
| 491 |
dec (num, err); |
| 492 |
inc (ts, err); |
| 493 |
until num=0; |
| 494 |
if (ioresult<>0) or (err<num) then errorhandler(s+' img read '+IntToStr(err)+'<>'+IntToStr(num),5); |
| 495 |
end; |
| 496 |
|
| 497 |
begin |
| 498 |
assign(f,s); |
| 499 |
reset(f,1); |
| 500 |
if (ioresult<>0) then errorhandler(s+' open',1); |
| 501 |
if not checkversion then errorhandler(s+' must be 256-color 8-bit index-colored 24-bit palette TARGA file',5); |
| 502 |
|
| 503 |
{ read TGA palette } |
| 504 |
seek(f,Int64(tga_head.id_len)+18); // skip required header and (optional) variable-sized image_id |
| 505 |
num:=tga_head.cmap_esize div 8 * tga_head.cmap_len; |
| 506 |
assert (num=768, 'palette size mismatch'); |
| 507 |
blockread(f,colors,num,err); |
| 508 |
if (ioresult<>0) or (num<>err) then errorhandler (s+' palette read error',5); |
| 509 |
|
| 510 |
{ read image bitmap } |
| 511 |
with tga_head do |
| 512 |
for y := 0 to height-1 do |
| 513 |
if (img_descriptor_b and (1 shl 5))=0 |
| 514 |
then { bottom left origin } |
| 515 |
saferead(f, @screen[0,(height-1-y)*width], width) |
| 516 |
else { top left origin } |
| 517 |
saferead(f, @screen[0,y*width], width); |
| 518 |
|
| 519 |
close(f); |
| 520 |
end; |
| 521 |
|
| 522 |
|
| 523 |
begin |
| 524 |
//loadpal ('data/main.pal'); { default palette if not overridden } |
| 525 |
end. |