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