home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / pascal / rehack / graphics / pcx.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-26  |  2.8 KB  |  120 lines

  1. //---------------------------------------------------------------------------
  2. //
  3. //      File:           PCX.CPP
  4. //      Path:           ...\REHACK\graphics
  5. //      Version:                2.0
  6. //      Author:            Dave Boynton
  7. //      CIS Id:            71043,317
  8. //      Created On:     January 1992
  9. //      Modified On:    7/25/93
  10. //      Description:    reads/writes pcx headers, and acts as a base class
  11. //                        for type specific pcx classes.
  12. //      Tabs:           4
  13. //
  14. //---------------------------------------------------------------------------
  15. // extracted/modified from pcxlib 2.0 (c) 1992,1993 David Boynton
  16. // All rights reserved.
  17. //-----------------------------------------------------------------------//
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include "..\general\types.hpp"
  21. #include "..\graphics\pcx.hpp"
  22.  
  23. // open an output file
  24. PcxFile::PcxFile(char *filename, word xSize, word ySize)
  25. {
  26.     if ( (filename == NULL) || (strlen(filename)==0) )
  27.     {
  28.         validFlag=false;
  29.         return;
  30.     }
  31.     if ( (fp=fopen(filename,"wb")) == NULL)
  32.     {
  33.         validFlag=false;
  34.         return;
  35.     }
  36.     filestart=0L; // not a resource file
  37.     header= new PCXH;
  38.     memset(header, 0, sizeof(PCXH)); // unused areas to zero
  39.     header->magicId=PCXMAGICID;
  40.     header->encoding=1;
  41.     header->xMax=xSize-1;
  42.     header->yMax=ySize-1;
  43.     header->hRes=header->vRes=75; // 75dpi=screen, 300dpi=laser
  44.     header->bytesLine=xSize;
  45.     hPixels=xSize;
  46.     vPixels=ySize;
  47.     bytesPerLine=ALIGN_DWORD(header->bytesLine);
  48.     lineBuffer=new byte[bytesPerLine];
  49.     validFlag=true;
  50.     // doesn't write the header.
  51. }
  52.  
  53. bool PcxFile::writeHeader(void)
  54. {
  55.     if ( fwrite(header, sizeof(PCXH), 1, fp) != 1 )
  56.         return false;
  57.     else
  58.         return true;
  59. }
  60.  
  61. PcxFile::~PcxFile()
  62. {
  63.     if ( lineBuffer )
  64.         delete lineBuffer;
  65.     if ( header )
  66.         delete header;
  67.     if ( fp )
  68.         fclose(fp);
  69. }
  70.  
  71. // open read-only pcx file.
  72. PcxFile::PcxFile(char *filename)
  73. {
  74.     if ( (filename == NULL) || (strlen(filename)==0) )
  75.     {
  76.         validFlag=false;
  77.         return;
  78.     }
  79.  
  80.     if ( (fp=fopen(filename,"rb")) == NULL)
  81.     {
  82.         validFlag=false;
  83.     } else
  84.         readHeader();
  85. }
  86.  
  87. // for resource files, sets filestart to current location in *filepointer
  88. PcxFile::PcxFile(FILE *filepointer)
  89. {
  90.     if (filepointer == NULL )
  91.     {
  92.         validFlag=false;
  93.     } else {
  94.         fp=filepointer;
  95.         readHeader();
  96.     }
  97.     return;
  98. }
  99.  
  100. // expects fp to be in the right place. This allows reading from a
  101. // resource file.
  102. bool PcxFile::readHeader(void)
  103. {
  104.     header= new PCXH;
  105.  
  106.     if ( ( (filestart=ftell(fp)) == -1L ) ||
  107.          (fread(header, sizeof(PCXH), 1, fp) != 1 ) ||
  108.          (header->magicId != PCXMAGICID) ||
  109.          (header->encoding != 1) ) // all pcx files have encoding==1
  110.     {
  111.         validFlag=false;
  112.     } else {
  113.         hPixels=header->xMax - header->xMin + 1;
  114.         vPixels=header->yMax - header->yMin + 1;
  115.         bytesPerLine=header->bytesLine;
  116.         validFlag=true;
  117.     }
  118.     return validFlag;
  119. }
  120.