home *** CD-ROM | disk | FTP | other *** search
/ Cutting-Edge 3D Game Programming with C++ / CE3DC++.ISO / BOOK / CHAP03 / TGA32.CPP < prev    next >
C/C++ Source or Header  |  1995-10-20  |  3KB  |  108 lines

  1. //
  2. // A class to read 256-color, uncompressed, TGA images
  3. // in 32-bit mode.
  4. //
  5. // Written by John De Goes
  6. //
  7.  
  8. #include <Dos.h>
  9. #include <Conio.h>
  10. #include <Iostream.h>
  11. #include "Tga32.hpp"
  12.  
  13. int TGAHeader::Load(FILE *File)
  14.   {
  15.   // Read the entire header; return 1 if success, 0 if failure.
  16.   const int HeaderSize = sizeof(TGAHeader);
  17.   if (fread(this, HeaderSize, 1, File) != 1)
  18.      return 0;
  19.   return 1;
  20.   }
  21.  
  22. // Make sure data aligned at BYTE boundary!!!  
  23. int TGAImage::Load(char *FileName)
  24.   {
  25.   FILE *File;
  26.  
  27.   // Open file in binary mode, abort if error detected:
  28.   if ((File = fopen(FileName, "rb")) == 0)
  29.      return 0;
  30.  
  31.   // Load the header C++ style:   
  32.   if (Header.Load(File) == 0)
  33.      return 0;
  34.   // Check for conditions we don't want to handle:
  35.  
  36.   // Make sure image is uncompressed:
  37.   if (Header.ImageType != 1)
  38.      return 0;
  39.  
  40.   // Make sure image is 256 colors:
  41.   if (Header.PixelBits != 8)
  42.      return 0;
  43.  
  44.   // Make sure image has a 16M color palette:
  45.   if (Header.PaletteEntrySize != 24)
  46.      return 0;
  47.  
  48.   // Check for ID string:
  49.   if (Header.IDLength > 0)
  50.      {
  51.      // If ImageID was previously initialized, unlock memory:
  52.      if ( ImageID )
  53.         delete [] ImageID;
  54.  
  55.      // Allociate memory for string:
  56.      if ((ImageID = new BYTE[Header.IDLength]) == 0)
  57.         return 0;
  58.  
  59.      // Read the ID string:
  60.      fread(ImageID, Header.IDLength, 1, File);   
  61.      }
  62.  
  63.   // If Palette was previously initialized, unlock memory:
  64.   if ( Palette )
  65.      delete [] Palette;
  66.  
  67.   // Create the palette:
  68.   if ((Palette = new BYTE[3 * 256]) == 0)
  69.      return 0;
  70.  
  71.   // Initialize misc. variables:
  72.   Width = Header.ImageWidth;
  73.   Height = Header.ImageHeight;
  74.   ImageSize = Width * Height;   
  75.  
  76.   // Load the palette, converting 24-bits to 8-bits:
  77.   for (int Index = 0; Index < 256; Index++)
  78.       for (int Color = 2; Color >= 0; --Color)
  79.           {
  80.           BYTE PaletteColor;
  81.  
  82.           // Read a single palette number:
  83.           fread(&PaletteColor, 1, 1, File);
  84.  
  85.           // Convert to 8-bit format:
  86.           PaletteColor >>= 2;
  87.  
  88.           // Change palette entry:
  89.           Palette[Index * 3 + Color] = PaletteColor;
  90.           }
  91.   // If Image was previously initialized, unlock memory:        
  92.   if ( Image )
  93.      delete [] Image;
  94.  
  95.   // Allociate image memory:
  96.   if ((Image = new BYTE[ImageSize]) == 0) // Abort if error
  97.      return 0;
  98.  
  99.   // Load the image:
  100.   fread(Image, ImageSize, 1, File);
  101.  
  102.   // Close file:
  103.   fclose(File);
  104.  
  105.   // Return success!
  106.   return 1;
  107.   }
  108.