home *** CD-ROM | disk | FTP | other *** search
- //---------------------------------------------------------------------------
- //
- // File: PCX.CPP
- // Path: ...\REHACK\graphics
- // Version: 2.0
- // Author: Dave Boynton
- // CIS Id: 71043,317
- // Created On: January 1992
- // Modified On: 7/25/93
- // Description: reads/writes pcx headers, and acts as a base class
- // for type specific pcx classes.
- // Tabs: 4
- //
- //---------------------------------------------------------------------------
- // extracted/modified from pcxlib 2.0 (c) 1992,1993 David Boynton
- // All rights reserved.
- //-----------------------------------------------------------------------//
- #include <stdio.h>
- #include <string.h>
- #include "..\general\types.hpp"
- #include "..\graphics\pcx.hpp"
-
- // open an output file
- PcxFile::PcxFile(char *filename, word xSize, word ySize)
- {
- if ( (filename == NULL) || (strlen(filename)==0) )
- {
- validFlag=false;
- return;
- }
- if ( (fp=fopen(filename,"wb")) == NULL)
- {
- validFlag=false;
- return;
- }
- filestart=0L; // not a resource file
- header= new PCXH;
- memset(header, 0, sizeof(PCXH)); // unused areas to zero
- header->magicId=PCXMAGICID;
- header->encoding=1;
- header->xMax=xSize-1;
- header->yMax=ySize-1;
- header->hRes=header->vRes=75; // 75dpi=screen, 300dpi=laser
- header->bytesLine=xSize;
- hPixels=xSize;
- vPixels=ySize;
- bytesPerLine=ALIGN_DWORD(header->bytesLine);
- lineBuffer=new byte[bytesPerLine];
- validFlag=true;
- // doesn't write the header.
- }
-
- bool PcxFile::writeHeader(void)
- {
- if ( fwrite(header, sizeof(PCXH), 1, fp) != 1 )
- return false;
- else
- return true;
- }
-
- PcxFile::~PcxFile()
- {
- if ( lineBuffer )
- delete lineBuffer;
- if ( header )
- delete header;
- if ( fp )
- fclose(fp);
- }
-
- // open read-only pcx file.
- PcxFile::PcxFile(char *filename)
- {
- if ( (filename == NULL) || (strlen(filename)==0) )
- {
- validFlag=false;
- return;
- }
-
- if ( (fp=fopen(filename,"rb")) == NULL)
- {
- validFlag=false;
- } else
- readHeader();
- }
-
- // for resource files, sets filestart to current location in *filepointer
- PcxFile::PcxFile(FILE *filepointer)
- {
- if (filepointer == NULL )
- {
- validFlag=false;
- } else {
- fp=filepointer;
- readHeader();
- }
- return;
- }
-
- // expects fp to be in the right place. This allows reading from a
- // resource file.
- bool PcxFile::readHeader(void)
- {
- header= new PCXH;
-
- if ( ( (filestart=ftell(fp)) == -1L ) ||
- (fread(header, sizeof(PCXH), 1, fp) != 1 ) ||
- (header->magicId != PCXMAGICID) ||
- (header->encoding != 1) ) // all pcx files have encoding==1
- {
- validFlag=false;
- } else {
- hPixels=header->xMax - header->xMin + 1;
- vPixels=header->yMax - header->yMin + 1;
- bytesPerLine=header->bytesLine;
- validFlag=true;
- }
- return validFlag;
- }
-