home *** CD-ROM | disk | FTP | other *** search
/ Game.EXE 2002 May / Game.EXE_05_2002.iso / Alawar / Lib / Format / PictureFormatBMP.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-03  |  1.6 KB  |  70 lines

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