home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Monster Media 1994 #1
/
monster.zip
/
monster
/
PROG_GEN
/
TCYBER.ZIP
/
PCX.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1994-01-15
|
9KB
|
105 lines
{
Turbo Vision CyberTools 1.0
(C) 1994 Steve Goldsmith
All Rights Reserved
Fast PCX objects for encoding and decoding various PCX formats.
Currently supports:
Decode/Encode 2 color.
X size <= 640
Y size <= 480
Decode/Encode 256 color/gray.
X size <= 320
Y size <= 200
}
UNIT PCX;{$I APP.INC}{$X+}INTERFACE USES DOS,OBJECTS,VGA;CONST PCXMANUFACTURER=$0a;PCXSTART256PAL=$0c;
PCXVER25=0;PCXVER28PAL=2;PCXVER28NOPAL=3;PCXVER30=5;PCXCOLORPAL=1;PCXGRAYPAL=2;PCXMAXXSIZE1=640;
PCXMAXYSIZE1=480;PCXMAXXSIZE256=320;PCXMAXYSIZE256=200;PCXMAXENCODESIZE=4096;PCXMAXDECODESIZE=4096;
PCXMEMALLOC=-100;PCXNOTPCXFORMAT=-101;PCXNOT2COLOR=-102;PCXNOT256COLOR=-103;PCXXSIZE=-104;PCXYSIZE=-105
;TYPE PCX16PAL=ARRAY [0..47] OF BYTE;PCXHEADER=RECORD MANUFACTURER,VERSION,ENCODING,BITSPERPIXEL:BYTE;
XMIN,YMIN,XMAX,YMAX,HRES,VRES:INTEGER;PALETTE:PCX16PAL;RESERVED,COLORPLANES:BYTE;BYTESPERLINE,
PALETTETYPE:INTEGER;FILLER:ARRAY [0..57] OF BYTE;END;PREADPCXFILE=^TREADPCXFILE;
TREADPCXFILE=OBJECT(TOBJECT)READERROR:INTEGER;XSIZE,YSIZE,ENCODEBUFPOS:WORD;READFILESIZE,
ENCODESIZE:LONGINT;READFILE:FILE;ENCODEBUFPTR:VGADATABUFPTR;DECODEBUFPTR:VGADATABUFPTR;HEADER:PCXHEADER;
READPALETTE:VGAPALETTE;CONSTRUCTOR INIT(FILENAME:PATHSTR);DESTRUCTOR DONE;VIRTUAL;FUNCTION GETENCODEBYTE:BYTE;
PROCEDURE DECODEFILE;VIRTUAL;END;PDECODEPCXFILE2=^TDECODEPCXFILE2;
TDECODEPCXFILE2=OBJECT(TREADPCXFILE)CONSTRUCTOR INIT(FILENAME:PATHSTR);END;PDECODEPCXFILE256=^TDECODEPCXFILE256;
TDECODEPCXFILE256=OBJECT(TREADPCXFILE)CONSTRUCTOR INIT(FILENAME:PATHSTR);PROCEDURE READPAL256;PROCEDURE DECODEFILE
;VIRTUAL;PROCEDURE PALETTE256TO64;END;PWRITEPCXFILE=^TWRITEPCXFILE;
TWRITEPCXFILE=OBJECT(TOBJECT)WRITEERROR:INTEGER;ENCODEBUFPOS:WORD;WRITEFILE:FILE;ENCODEBUFPTR:VGADATABUFPTR;
HEADER:PCXHEADER;CONSTRUCTOR INIT(FILENAME:PATHSTR);DESTRUCTOR DONE;VIRTUAL;PROCEDURE WRITEHEADER;
PROCEDURE SETHEADER(XS,YS:INTEGER;BP,CP:BYTE);PROCEDURE WRITEENCODEBYTE(E:BYTE);PROCEDURE FLUSHENCODEBUF;
PROCEDURE ENCODEFILE(PCXIMAGE:VGADATABUFPTR);VIRTUAL;END;PENCODEPCXFILE2=^TENCODEPCXFILE2;
TENCODEPCXFILE2=OBJECT(TWRITEPCXFILE)PROCEDURE ENCODEFILE(PCXIMAGE:VGADATABUFPTR);VIRTUAL;END;
PENCODEPCXFILE256=^TENCODEPCXFILE256;TENCODEPCXFILE256=OBJECT(TWRITEPCXFILE)PROCEDURE ENCODEFILE
(PCXIMAGE:VGADATABUFPTR);VIRTUAL;PROCEDURE WRITEPAL256(PAL:VGAPALETTEPTR);PROCEDURE PALETTE64TO256
(PAL:VGAPALETTEPTR);END;CONST PCXDEF1BITPAL:PCX16PAL=($00,$00,$00,$ff,$ff,$ff,$00,$aa,$00,$00,
$aa,$aa,$aa,$00,$00,$aa,$00,$aa,$aa,$aa,$00,$aa,$aa,$aa,$55,$55,$55,$55,$55,$ff,
$55,$ff,$55,$55,$ff,$ff,$ff,$55,$55,$ff,$55,$ff,$ff,$ff,$55,$ff,$ff,$ff);
IMPLEMENTATION USES MEMORY;CONSTRUCTOR TREADPCXFILE.INIT(FILENAME:PATHSTR);BEGIN INHERITED INIT;ENCODEBUFPOS:=
PCXMAXENCODESIZE;ASSIGN(READFILE,FILENAME);{$I-}RESET(READFILE,1);{$I+}READERROR:=IORESULT;IF READERROR=0
THEN BEGIN{$I-}READFILESIZE:=FILESIZE(READFILE);{$I+}READERROR:=IORESULT;IF READERROR=0 THEN BEGIN{$I-}
BLOCKREAD(READFILE,HEADER,SIZEOF(HEADER));{$I+}READERROR:=IORESULT;IF READERROR=0 THEN BEGIN IF
HEADER.MANUFACTURER=PCXMANUFACTURER THEN BEGIN IF HEADER.BITSPERPIXEL=8 THEN ENCODESIZE:=READFILESIZE-SIZEOF
(READPALETTE)-SIZEOF(HEADER)ELSE ENCODESIZE:=READFILESIZE-SIZEOF(HEADER);ENCODEBUFPTR:=MEMALLOC
(PCXMAXENCODESIZE);IF ENCODEBUFPTR=NIL THEN READERROR:=PCXMEMALLOC END ELSE READERROR:=PCXNOTPCXFORMAT
END END END END;DESTRUCTOR TREADPCXFILE.DONE;BEGIN IF ENCODEBUFPTR<> NIL THEN FREEMEM(ENCODEBUFPTR,PCXMAXENCODESIZE
);IF DECODEBUFPTR<> NIL THEN FREEMEM(DECODEBUFPTR,HEADER.BYTESPERLINE * YSIZE);{$I-}CLOSE(READFILE);{$I+}
READERROR:=IORESULT;INHERITED DONE;END;FUNCTION TREADPCXFILE.GETENCODEBYTE:BYTE;VAR O1lIII0lO0I1:WORD;BEGIN IF
ENCODEBUFPOS=PCXMAXENCODESIZE THEN BEGIN ENCODEBUFPOS:=0;{$I-}BLOCKREAD(READFILE,ENCODEBUFPTR^,PCXMAXENCODESIZE
,O1lIII0lO0I1);{$I+}READERROR:=IORESULT END;GETENCODEBYTE:=ENCODEBUFPTR^[ENCODEBUFPOS];INC(ENCODEBUFPOS
)END;PROCEDURE TREADPCXFILE.DECODEFILE;VAR OIOOIOl1lI,OOlIlOO0OlOI,O1lIOO0O100l:BYTE;OI11IIlOO110:WORD;
OOllO11OOI0I:LONGINT;BEGIN OOllO11OOI0I:=1;OI11IIlOO110:=0;WHILE OOllO11OOI0I<ENCODESIZE DO BEGIN OIOOIOl1lI:=
GETENCODEBYTE;INC(OOllO11OOI0I);IF(OIOOIOl1lI AND $c0)=$c0 THEN BEGIN OIOOIOl1lI:=(OIOOIOl1lI AND $3f)-1;
O1lIOO0O100l:=GETENCODEBYTE;INC(OOllO11OOI0I);FOR OOlIlOO0OlOI:=0 TO OIOOIOl1lI DO DECODEBUFPTR^[OI11IIlOO110+
OOlIlOO0OlOI]:=O1lIOO0O100l;OI11IIlOO110:=OI11IIlOO110+OOlIlOO0OlOI+1 END ELSE BEGIN DECODEBUFPTR^[
OI11IIlOO110]:=OIOOIOl1lI;INC(OI11IIlOO110)END END END;CONSTRUCTOR TDECODEPCXFILE2.INIT(FILENAME:PATHSTR);
BEGIN INHERITED INIT(FILENAME);IF READERROR=0 THEN BEGIN IF(HEADER.BITSPERPIXEL=1)AND(HEADER.COLORPLANES=1)THEN
BEGIN XSIZE:=HEADER.XMAX-HEADER.XMIN+1;YSIZE:=HEADER.YMAX-HEADER.YMIN+1;IF XSIZE<=PCXMAXXSIZE1 THEN
BEGIN IF YSIZE<=PCXMAXYSIZE1 THEN BEGIN DECODEBUFPTR:=MEMALLOC(HEADER.BYTESPERLINE * YSIZE);IF DECODEBUFPTR=NIL
THEN READERROR:=PCXMEMALLOC END ELSE READERROR:=PCXYSIZE END ELSE READERROR:=PCXXSIZE END ELSE READERROR:=
PCXNOT2COLOR END END;CONSTRUCTOR TDECODEPCXFILE256.INIT(FILENAME:PATHSTR);BEGIN INHERITED INIT(FILENAME);IF READERROR
=0 THEN BEGIN IF(HEADER.VERSION=PCXVER30)AND(HEADER.BITSPERPIXEL=8)THEN BEGIN XSIZE:=HEADER.XMAX-HEADER.XMIN+
1;YSIZE:=HEADER.YMAX-HEADER.YMIN+1;IF XSIZE<=PCXMAXXSIZE256 THEN BEGIN IF YSIZE<=PCXMAXYSIZE256 THEN
BEGIN DECODEBUFPTR:=NIL;DECODEBUFPTR:=MEMALLOC(XSIZE * YSIZE);IF DECODEBUFPTR=NIL THEN READERROR:=PCXMEMALLOC
END ELSE READERROR:=PCXYSIZE END ELSE READERROR:=PCXXSIZE END ELSE READERROR:=PCXNOT256COLOR END END;
PROCEDURE TDECODEPCXFILE256.READPAL256;BEGIN{$I-}SEEK(READFILE,SIZEOF(HEADER)+ENCODESIZE);{$I+}READERROR:=
IORESULT;IF READERROR=0 THEN BEGIN{$I-}BLOCKREAD(READFILE,READPALETTE,SIZEOF(READPALETTE));{$I+}READERROR:=
IORESULT END END;PROCEDURE TDECODEPCXFILE256.DECODEFILE;BEGIN INHERITED DECODEFILE;READPAL256 END;
PROCEDURE TDECODEPCXFILE256.PALETTE256TO64;VAR OI10Illl01l,OI11I0I0ll0:BYTE;BEGIN FOR OI11I0I0ll0:=0 TO VGADACREGMAX
DO FOR OI10Illl01l:=0 TO VGARGBMAX DO READPALETTE [OI11I0I0ll0,OI10Illl01l]:=READPALETTE [OI11I0I0ll0,
OI10Illl01l] SHR 2 END;CONSTRUCTOR TWRITEPCXFILE.INIT(FILENAME:PATHSTR);BEGIN INHERITED INIT;ASSIGN(WRITEFILE,
FILENAME);{$I-}REWRITE(WRITEFILE,1);{$I+}WRITEERROR:=IORESULT;IF WRITEERROR=0 THEN BEGIN ENCODEBUFPTR:=
MEMALLOC(PCXMAXENCODESIZE);IF ENCODEBUFPTR=NIL THEN WRITEERROR:=PCXMEMALLOC END END;
PROCEDURE TWRITEPCXFILE.WRITEHEADER;BEGIN{$I-}BLOCKWRITE(WRITEFILE,HEADER,SIZEOF(HEADER));{$I+}WRITEERROR:=
IORESULT END;PROCEDURE TWRITEPCXFILE.SETHEADER(XS,YS:INTEGER;BP,CP:BYTE);BEGIN WITH HEADER DO BEGIN MANUFACTURER:=
PCXMANUFACTURER;VERSION:=PCXVER30;ENCODING:=1;BITSPERPIXEL:=BP;XMIN:=0;YMIN:=0;XMAX:=XS-1;YMAX:=YS
-1;HRES:=640;VRES:=480;CASE BITSPERPIXEL OF 1:PALETTE:=PCXDEF1BITPAL;8:PALETTE:=PCXDEF1BITPAL END;
RESERVED:=0;COLORPLANES:=CP;BYTESPERLINE:=XS DIV(8 DIV BP);IF XS AND $07<> 0 THEN INC(BYTESPERLINE);
CASE BITSPERPIXEL OF 1:PALETTETYPE:=0;8:PALETTETYPE:=PCXCOLORPAL END;FILLCHAR(FILLER,SIZEOF(FILLER),0);
END;END;DESTRUCTOR TWRITEPCXFILE.DONE;BEGIN IF ENCODEBUFPTR<> NIL THEN FREEMEM(ENCODEBUFPTR,PCXMAXENCODESIZE);
{$I-}CLOSE(WRITEFILE);{$I+}WRITEERROR:=IORESULT;INHERITED DONE;END;PROCEDURE TWRITEPCXFILE.WRITEENCODEBYTE
(E:BYTE);BEGIN IF ENCODEBUFPOS=PCXMAXENCODESIZE THEN BEGIN{$I-}BLOCKWRITE(WRITEFILE,ENCODEBUFPTR^,
PCXMAXENCODESIZE);{$I+}WRITEERROR:=IORESULT;ENCODEBUFPOS:=0 END;ENCODEBUFPTR^[ENCODEBUFPOS]:=E;INC
(ENCODEBUFPOS)END;PROCEDURE TWRITEPCXFILE.FLUSHENCODEBUF;VAR OI1OOO0OO0:WORD;BEGIN IF ENCODEBUFPOS> 0 THEN
BEGIN{$I-}BLOCKWRITE(WRITEFILE,ENCODEBUFPTR^,ENCODEBUFPOS,OI1OOO0OO0);{$I+}WRITEERROR:=IORESULT;
ENCODEBUFPOS:=0 END END;PROCEDURE TWRITEPCXFILE.ENCODEFILE(PCXIMAGE:VGADATABUFPTR);VAR OOlIlOO0OlOI:BYTE;
OOlOOO1I1OI0,O10OIIlIlO0O0,O10OIIllll0IO,OOIl:WORD;BEGIN FOR OOIl:=0 TO HEADER.YMAX DO BEGIN OOlOOO1I1OI0:=
(HEADER.BYTESPERLINE)* OOIl;O10OIIllll0IO:=OOlOOO1I1OI0+HEADER.BYTESPERLINE-1;OOlIlOO0OlOI:=0;
WHILE OOlOOO1I1OI0<=O10OIIllll0IO DO BEGIN O10OIIlIlO0O0:=OOlOOO1I1OI0+1;WHILE(O10OIIlIlO0O0<=O10OIIllll0IO
)AND(OOlIlOO0OlOI<62)AND(PCXIMAGE^[OOlOOO1I1OI0]=PCXIMAGE^[O10OIIlIlO0O0])DO BEGIN INC(O10OIIlIlO0O0);
INC(OOlIlOO0OlOI)END;IF OOlIlOO0OlOI> 0 THEN BEGIN INC(OOlIlOO0OlOI);WRITEENCODEBYTE(OOlIlOO0OlOI OR $c0);
WRITEENCODEBYTE(PCXIMAGE^[OOlOOO1I1OI0]);OOlOOO1I1OI0:=O10OIIlIlO0O0;OOlIlOO0OlOI:=0 END ELSE BEGIN IF
(PCXIMAGE^[OOlOOO1I1OI0] AND $c0)=$c0 THEN WRITEENCODEBYTE($c1);WRITEENCODEBYTE(PCXIMAGE^[OOlOOO1I1OI0]);INC
(OOlOOO1I1OI0)END END END END;PROCEDURE TENCODEPCXFILE2.ENCODEFILE(PCXIMAGE:VGADATABUFPTR);
BEGIN INHERITED ENCODEFILE(PCXIMAGE);FLUSHENCODEBUF END;PROCEDURE TENCODEPCXFILE256.ENCODEFILE
(PCXIMAGE:VGADATABUFPTR);BEGIN INHERITED ENCODEFILE(PCXIMAGE);WRITEENCODEBYTE(PCXSTART256PAL);FLUSHENCODEBUF END;
PROCEDURE TENCODEPCXFILE256.WRITEPAL256(PAL:VGAPALETTEPTR);BEGIN{$I-}BLOCKWRITE(WRITEFILE,PAL^,SIZEOF(PAL^));
{$I+}WRITEERROR:=IORESULT END;PROCEDURE TENCODEPCXFILE256.PALETTE64TO256(PAL:VGAPALETTEPTR);
VAR OI10Illl01l,OI11I0I0ll0:BYTE;BEGIN FOR OI11I0I0ll0:=0 TO VGADACREGMAX DO FOR OI10Illl01l:=0 TO VGARGBMAX DO PAL
^[OI11I0I0ll0,OI10Illl01l]:=PAL^[OI11I0I0ll0,OI10Illl01l] SHL 2 END;END.