home *** CD-ROM | disk | FTP | other *** search
/ Cutting-Edge 3D Game Programming with C++ / CE3DC++.ISO / BOOK / CHAP11 / MORPH / BMP32.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-22  |  5.1 KB  |  192 lines

  1. //
  2. // File name: BMP32.CPP
  3. //
  4. // Description: A BMP image class implementation
  5. //
  6. // Author: John De Goes
  7. //
  8. // Project: Cutting Edge 3D Game Programming
  9. //
  10.  
  11. #ifndef BMP32HPP
  12.   #define BMP32HPP
  13.   #include "BMP32.HPP"
  14. #endif
  15.  
  16. int BMPImage::Load ( char *FileName )
  17.   {
  18.   FileName;
  19.   long ImageSize, ScanWidth, ScanPadding, Y, YPos;
  20.   FILE *InFile;
  21.   if ( ( InFile = fopen ( FileName, "rb" ) ) == 0 )
  22.      return OpenErr;
  23.  
  24.   // Load the file header data:
  25.   if ( fread ( &FileHeader, sizeof FileHeader, 1, InFile ) 
  26.        < 1 )
  27.      return ReadErr;
  28.  
  29.   // Load the information header data:
  30.   if ( fread ( &InfoHeader, sizeof InfoHeader, 1, InFile ) 
  31.        < 1 )
  32.      return ReadErr;
  33.  
  34.   ColorCount = ( long ) pow ( 2.0F, InfoHeader.biBitCount );
  35.   BitCount   = InfoHeader.biBitCount;
  36.  
  37.   if ( BitCount <= 16 )
  38.      {
  39.      if ( ( Palette = new RGBQUAD [ ColorCount ] ) == 0 )
  40.         return MemErr;
  41.      if ( fread ( Palette, sizeof ( RGBQUAD ), ColorCount, 
  42.                   InFile ) < ColorCount )
  43.         return ReadErr;
  44.      }
  45.  
  46.   Width       = InfoHeader.biWidth;
  47.   Height      = InfoHeader.biHeight;
  48.   ImageSize   = Width * Height;
  49.   ScanWidth   = ( ( Width * ( BitCount / 8 ) ) + 3 ) & ( ~3 );
  50.   ScanPadding = ScanWidth - ( Width * ( BitCount / 8 ) );
  51.  
  52.   if ( ( Image = new BYTE [ ImageSize * 
  53.        ( BitCount / 8 ) ] ) == NULL )
  54.      return MemErr;
  55.  
  56.   // Load the bitmap:
  57.   for ( Y = 0; Y < abs ( Height ); Y++ )
  58.       {
  59.       // Load a scan-line:
  60.       switch ( InfoHeader.biCompression )
  61.              {
  62.              // Uncompressed image:
  63.              case ( BI_RGB ):
  64.                   {
  65.                   switch ( BitCount )
  66.                          {
  67.                          // 256-color image:
  68.                          case ( 8 ):
  69.                               {
  70.                               if ( Height < 0 )
  71.                                  {
  72.                                  YPos = Y * Width;
  73.                                  }
  74.                               else YPos = ( ( Height - Y ) * 
  75.                                               Width ) - Width;
  76.  
  77.                               if ( fread ( &Image [ YPos ], 
  78.                                    Width, 1, InFile ) < 1 )
  79.                                  return ReadErr;
  80.                               break;
  81.                               }
  82.                          default: return ResErr;
  83.                          }
  84.                   break;
  85.                   }
  86.              default: return CompErr;
  87.              }
  88.       // Skip the padding possibly located at the end of each
  89.       // scan-line:
  90.       fseek ( InFile, ScanPadding, SEEK_CUR );
  91.       }
  92.   fclose ( InFile );
  93.   Height = abs ( Height );
  94.   return Success;
  95.   }
  96.  
  97. int BMPImage::SaveBT ( FILE *OutFile )
  98.   {
  99.   // Write the image width:
  100.   if ( fwrite ( &Width, sizeof Width, 1, OutFile ) < 1 )
  101.      return 0;
  102.  
  103.   // Write the image height:
  104.   if ( fwrite ( &Height, sizeof Height, 1, OutFile ) < 1 )
  105.      return 0;
  106.  
  107.   // Write the image's bit count per pixel:
  108.   if ( fwrite ( &BitCount, sizeof BitCount, 1, OutFile ) < 1 )
  109.      return 0;
  110.  
  111.   // Write the image's color count:
  112.   if ( fwrite ( &ColorCount, sizeof ColorCount, 1, 
  113.        OutFile ) < 1 )
  114.      return 0;
  115.  
  116.   // Write the image:
  117.   if ( fwrite ( Image, Width * Height * ( BitCount / 8 ), 1, 
  118.        OutFile ) < 1 )
  119.      return 0;
  120.   return 1;
  121.   }
  122.  
  123. int BMPImage::LoadBT ( FILE *InFile )
  124.   {
  125.   // Read the image width:
  126.   if ( fread ( &Width, sizeof Width, 1, InFile ) < 1 )
  127.      return 0;
  128.  
  129.   // Read the image height:
  130.   if ( fread ( &Height, sizeof Height, 1, InFile ) < 1 )
  131.      return 0;
  132.  
  133.   // Read the image's bit count per pixel:
  134.   if ( fread ( &BitCount, sizeof BitCount, 1, InFile ) < 1 )
  135.      return 0;
  136.  
  137.   // Read the image's color count:
  138.   if ( fread ( &ColorCount, sizeof ColorCount, 1, InFile ) < 1 )
  139.      return 0;
  140.  
  141.   // Allocate memory for image:
  142.   if ( ( Image = new BYTE [ Width * Height * ( BitCount / 8 ) ] )
  143.        == NULL )
  144.      return 0;
  145.  
  146.   // Read the image:
  147.   if ( fread ( Image, Width * Height * ( BitCount / 8 ), 1, 
  148.        InFile ) < 1 )
  149.      return 0;
  150.   return 1;
  151.   }
  152.  
  153. int BMPImage::SavePal ( FILE *OutFile )
  154.   {
  155.   // Write the color count:
  156.   if ( fwrite ( &ColorCount, sizeof ColorCount, 1, OutFile ) 
  157.        < 1 )
  158.      return 0;
  159.  
  160.   // Write the bit count:
  161.   if ( fwrite ( &BitCount, sizeof BitCount, 1, OutFile ) < 1 )
  162.      return 0;
  163.  
  164.   // Write the palette:
  165.   if ( fwrite ( Palette, sizeof ( RGBQUAD ), ColorCount, 
  166.        OutFile ) < 1 )
  167.      return 0;
  168.   return 1;
  169.   }
  170.  
  171. int BMPImage::LoadPal ( FILE *InFile )
  172.   {
  173.   // Load the color count:
  174.   if ( fread ( &ColorCount, sizeof ColorCount, 1, InFile ) 
  175.        < 1 )
  176.      return 0;
  177.  
  178.   // Load the bit count:
  179.   if ( fread ( &BitCount, sizeof BitCount, 1, InFile ) < 1 )
  180.      return 0;
  181.  
  182.   // Allocate memory for palette:
  183.   if ( ( Palette = new RGBQUAD [ ColorCount ] ) == 0 )
  184.      return 0;
  185.  
  186.   // Load the palette:
  187.   if ( fread ( Palette, sizeof ( RGBQUAD ), ColorCount, 
  188.        InFile ) < ColorCount )
  189.      return 0;
  190.   return 1;
  191.   }
  192.