?ifndef _PCXLOAD_
?define _PCXLOAD_ TRUE

?define PCXBUFSIZE 1024

struct _PCX_HEADER_{
  byte PCX_manufacturer;      // manufacturer byte (always 0A0h)
  byte PCX_version;           // pcx Version
  byte PCX_encoding;          // (always 1)
  byte PCX_bits_per_pixel;    // color bits per pixel
  word PCX_xmin;              // image origin x
  word PCX_ymin;              // image origin y
  word PCX_xmax;              // image end x
  word PCX_ymax;              // image end y
  word PCX_hres;              // horizontal resolution
  word PCX_vres;              // vertical resolution
  byte PCX_palette1[48];      // (color palette, for older not 256 col vers.)
  byte PCX_reserved;          // reserved byte
  byte PCX_color_planes;      // number of color planes
  word PCX_bytes_per_line;    // line buffer size
  word PCX_palette_type;      // grey or color palette indicator
  byte PCX_reserved2[58];     // reserved
};

byte loadPCX(word filename,bufseg)
word xcount;
word ycount;
word local_bytesperline;
byte readbuf[PCXBUFSIZE];
struct _PCX_HEADER_ hpcx;
{
	@FOPEN(0, , ,filename);
	if(AX!=0){
		AX><BX;
		if(FREAD(,,sizeof(hpcx),#hpcx)==sizeof(hpcx))&&
				(hpcx.PCX_manufacturer==0x0A)&&(hpcx.PCX_version==5)&&
				(hpcx.PCX_bits_per_pixel==8)&&(hpcx.PCX_color_planes<=1){
			ycount=hpcx.PCX_ymax-hpcx.PCX_ymin+1;
			local_bytesperline=hpcx.PCX_xmax-hpcx.PCX_xmin+1;
			ES=bufseg;
			DI=0;
			SI=PCXBUFSIZE;
			loop(ycount){
				xcount=local_bytesperline;
				do{
					IF(SI==PCXBUFSIZE){
						IF(FREAD(,,PCXBUFSIZE,#readbuf)==0)goto ERR;
						SI=0;
					}
					AL=readbuf[SI];
					SI++;
					IF(AL>=0xC0){
						AL&=0x3F;
						CX=0;
						CL=AL;
						xcount-=CX;
						IF(SI==PCXBUFSIZE){
							$PUSH CX
							FREAD(,,PCXBUFSIZE,#readbuf);
							$POP CX
							IF(AX==0)GOTO ERR;
							SI=0;
						}
						AL=readbuf[SI];
						SI++;
						$REPNE
						$STOSB
					}
					ELSE{
						xcount--;
						$STOSB
					}
				}while(xcount>0);
			}
			CX=-1;
			DX=-769;
			AX=0x4202;
			$INT 0x21
			IF(NOTCARRYFLAG){
				AX=FREAD(,,769,#readbuf);
				IF(readbuf==0xC){
					DI=#readbuf[1];
					CX=768;
					loop(CX){
						DSBYTE[DI]>>=2;
						DI++;
					}
					@SETVGADAC(0,,768,,,#readbuf[1]);
					FCLOSE();
					RETURN 0;
				}
			}
		}
ERR:
		FCLOSE();
	}
	return -1;
}

?endif
/* end of PCXLOAD.H-- */
