home *** CD-ROM | disk | FTP | other *** search
- #include <stddef.h>
- #include <dos.h>
- #include "gobjects.h"
-
- struct pcx_header {
- CHAR manufacturer; // Always set to 0
- CHAR version; // Always 5 for 256-color files
- CHAR encoding; // Always set to 1
- CHAR bits_per_pixel; // Should be 8 for 256-color files
- SHORT xmin,ymin; // Coordinates for top left corner
- SHORT xmax,ymax; // Width and height of image
- SHORT hres; // Horizontal resolution of image
- SHORT vres; // Vertical resolution of image
- CHAR palette16[48]; // EGA palette; not used for
- // 256-color files
- CHAR reserved; // Reserved for future use
- CHAR color_planes; // Color planes
- SHORT bytes_per_line; // Number of bytes in 1 line of
- // pixels
- SHORT palette_type; // Should be 2 for color palette
- CHAR filler[58]; // Nothing but junk
- };
-
-
- gobject::gobject(UCHAR imagenum) : inpcxfile(), outpcxfile()
- {
- holdimage=FALSE;
- image=new PUCHAR [imagenum];
- ni=imagenum;
- for (SHORT i=0; i<imagenum; i++)
- image[i]=NULL;
- }
-
- LONG gobject::assigninfile(PCHAR filename,ULONG locate)
- {
- if (inpcxfile.is_open())
- inpcxfile.close();
- inpcxfile.open(filename,ios::binary | ios::in);
- if (inpcxfile.fail())
- return -1;
- else {inpcxfile.seekg(locate,ios::beg);
- return 0;}
- }
-
- LONG gobject::assignoutfile(PCHAR filename,ULONG locate)
- {
- if (outpcxfile.is_open())
- outpcxfile.close();
- outpcxfile.open(filename,ios::binary | ios::out | ios::truncate);
- if (outpcxfile.fail())
- return -1;
- else {outpcxfile.seekp(locate,ios::beg);
- return 0;}
- }
-
- LONG gobject::setup()
- {
- if (!inpcxfile.is_open())
- return -1;
- LONG seek=inpcxfile.tellg();
- inpcxfile.seekg(0L,ios::beg);
- pcx_header header;
- inpcxfile.read((PCHAR)&header,128);
- image_size=((LONG)header.bytes_per_line)*((LONG)header.color_planes)
- *((LONG)(1+header.ymax-header.ymin));
- width=1+header.xmax-header.xmin;
- height=1+header.ymax-header.ymin;
- inpcxfile.seekg(seek,ios::beg);
- return 0;
- }
-
- LONG gobject::writesetup()
- {
- if (!outpcxfile.is_open())
- return -1;
- pcx_header header;
- header.manufacturer=10;
- header.version=5;
- header.encoding=1;
- header.bits_per_pixel=8;
- header.xmin=0;header.ymin=0;
- header.xmax=width-1;header.ymax=height-1;
- header.hres=300; header.vres=300;
- header.palette16[0]=1;
- header.palette16[1]=0;
- header.palette16[2]=1;
- header.palette16[3]=0;
- header.palette16[4]=1;
- header.reserved=0;
- header.color_planes=1;
- header.bytes_per_line=width;
- header.palette_type=1;
- outpcxfile.write((PCHAR)&header,sizeof(header));
- return 0;
- }
-
- LONG gobject::writepcx(UCHAR imagenum)
- {
- if (!outpcxfile.is_open())
- return (-1);
- length=0;
- static UCHAR buffer[5*1024];
- PUCHAR image_ptr=image[imagenum];
- char mode=1;
- static UCHAR writeout;
- static UCHAR bytecount;
- LONG bufptr=0;
- LONG buflen=5*1024;
- LONG i=0;
- while (i<image_size) {
- if (mode>0) {
- if (bufptr>=buflen) {
- length+=5*1024;
- bufptr=0;
- outpcxfile.write(buffer,5*1024);}
- writeout=*(image_ptr++);
- if (((image_size-i>1)&&(writeout==*(image_ptr)))||(writeout > 0xbf)) {
- bytecount=1;
- mode=0;
- } else { buffer[bufptr++]=writeout; i++;}
- }
- else {
- if ((image_size-i>1)&&
- (bytecount<0x39)&&(*(image_ptr)==writeout)){
- bytecount++;
- image_ptr++;}
- else {
- buffer[bufptr++]=bytecount+0xc0;
- if (bufptr>=buflen) {
- length+=5*1024;
- bufptr=0;
- outpcxfile.write(buffer,buflen);}
- buffer[bufptr++]=writeout;
- mode=1;
- }
- i++;
- }
- }
- outpcxfile.write(buffer,bufptr);
- length+=bufptr;
- return 0;
- }
-
- LONG gobject::loadpcx(UCHAR imagenum)
- {
- length=0;
- if (!inpcxfile.is_open())
- return (-1);
- if ((image[imagenum]=new UCHAR [image_size])==NULL){
- delete image[imagenum];
- return 1;}
- LONG seek=inpcxfile.tellg();
- inpcxfile.seekg(128L,ios::cur);
- static UCHAR buffer[5*1024];
- PUCHAR image_ptr=image[imagenum];
- CHAR mode=1;
- LONG bufptr=0;
- LONG buflen=0;
- static UCHAR readin;
- static UCHAR bytecount;
- for (LONG i=0;i<image_size;i++){
- if (mode > 0)
- {
- if (bufptr>=buflen)
- {bufptr=0;
- inpcxfile.read(buffer,5*1024);
- buflen=inpcxfile.gcount();
- length+=buflen;
- if (buflen==0)
- break;} //if (bufptr>=buflen)
- readin=buffer[bufptr++];
- if (readin>0xbf)
- {
- bytecount= (LONG)((LONG)readin & 0x3f);
- if (bufptr>=buflen)
- {bufptr=0;
- inpcxfile.read(buffer,5*1024);
- buflen=inpcxfile.gcount();
- length+=buflen;
- if (buflen==0)
- break;} //if (bufptr>=buflen)
- readin=buffer[bufptr++];
- if (--bytecount > 0) mode=0;
- } //if (readin>0xbf)
- } //if (mode)
- else if (--bytecount == 0) mode=1;
- *image_ptr++=readin;
- } //for loop
- inpcxfile.clear();
- inpcxfile.seekg(seek,ios::beg);
- return 0;
- }
-
- LONG gobject::load(UCHAR imagenum)
- {
- if (imagenum>=ni)
- return -1;
- if (image[imagenum]!=NULL)
- delete image[imagenum];
- setup();
- if (loadpcx(imagenum))
- return -1;
- return 0;
- }
-
- LONG gobject::Write(UCHAR imagenum)
- {
- if (imagenum>=ni)
- return -1;
- if (image[imagenum]==NULL)
- return -1;
- writesetup();
- if (writepcx(imagenum))
- return -1;
- return 0;
- }
-
- void gobject::show(PUCHAR screen, SHORT screen_width, USHORT x, USHORT y, UCHAR imagenum)
- {
- PUCHAR imageptr=image[imagenum];
- PUCHAR scrnptr=(screen+(long)screen_width*(long)y+(long)x);
- for (LONG i=0;i<height;i++) {
- for (LONG j=0;j<width;j++)
- *(scrnptr++)=*(imageptr++);
- scrnptr+=(screen_width-width);
- }
- }
-
- void gobject::transparent_show(PUCHAR screen, SHORT screen_width, USHORT x, USHORT y, UCHAR imagenum)
- {
- PUCHAR imageptr=image[imagenum];
- PUCHAR scrnptr=(screen+(long)screen_width*(long)y+(long)x);
- UCHAR pixel;
- for (LONG i=0; i<height; i++) {
- for (LONG j=0; j<width; j++) {
- pixel=*(imageptr++);
- if (pixel) {
- *(scrnptr)=pixel;
- } /* endif */
- scrnptr++;
- } /* endfor */
- scrnptr+=(screen_width-width);
- } /* endfor */
- }
-
- gobject::~gobject()
- {
- if (!holdimage) {
- for (short i=0; i<ni; i++)
- delete image[i];
- delete image;
- }
- if (inpcxfile.is_open())
- inpcxfile.close();
- if (outpcxfile.is_open())
- outpcxfile.close();
- }
-