home *** CD-ROM | disk | FTP | other *** search
/ Game.EXE 2002 April / Game.EXE_04_2002.iso / Alawar / BMPPictureFormat.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-02-25  |  1.6 KB  |  71 lines

  1. #include "BMPPictureFormat.h"
  2. #include "AutoPtr.h"
  3. #include "Color.h"
  4. #include "ResourceManagerPtr.h"
  5. #include "Resource.h"
  6. #include "String.h"
  7. #include "safe_new.h"
  8.  
  9. BMPPictureFormat::BMPPictureFormat(const String & name)
  10. :    data( 0 )
  11. {
  12.     AutoPtr<Resource> res = ResourceManagerPtr()->create_resource( name + ".bmp" );
  13.     if( !res )
  14.         return;
  15.  
  16.     if( res->read( &file_header, sizeof(file_header) ) != sizeof(file_header) )
  17.         return;
  18.     if( file_header.bfType != 0x4d42 )
  19.         return;
  20.     if( res->read( &bitmap_info_header, sizeof(bitmap_info_header) ) != sizeof(bitmap_info_header) )
  21.         return;
  22.     if( bitmap_info_header.biBitCount <= 8 )
  23.         return;
  24.  
  25.     unsigned sx = bitmap_info_header.biWidth;
  26.     unsigned sy = bitmap_info_header.biHeight;
  27.  
  28.     unsigned char * fdata = new unsigned char[sx*sy*4];
  29.     res->read( fdata, sx*sy*4 );
  30.     data = new Color[sx*sy];
  31.  
  32.     const unsigned char * curr = fdata;
  33.     for( unsigned y = 0; y < sy; ++y )
  34.     {
  35.         Color * dst = data + get_shift( 0, sy-1-y );
  36.         for( unsigned x = 0; x < sx; ++x )
  37.         {
  38.             unsigned char b = *curr++;
  39.             unsigned char g = *curr++;
  40.             unsigned char r = *curr++;
  41.             unsigned char a = 0xFF;
  42.             if( bitmap_info_header.biBitCount == 32 )
  43.                 a = *curr++;
  44.             *dst++ = Color( r,g,b,a );
  45.         }
  46.         if( bitmap_info_header.biBitCount == 24 )
  47.             curr += (4 - (sx*3) % 4) % 4;
  48.     }
  49.     delete fdata; fdata = 0;
  50. }
  51.  
  52. BMPPictureFormat::~BMPPictureFormat()
  53. {
  54.     delete data; data = 0;
  55. }
  56.  
  57. unsigned BMPPictureFormat::width()const
  58. {
  59.     return bitmap_info_header.biWidth;
  60. }
  61.  
  62. unsigned BMPPictureFormat::height()const
  63. {
  64.     return bitmap_info_header.biHeight;
  65. }
  66.  
  67. const Color * BMPPictureFormat::colors()const
  68. {
  69.     return data;
  70. }
  71.