home *** CD-ROM | disk | FTP | other *** search
- #include "BMPPictureFormat.h"
- #include "AutoPtr.h"
- #include "Color.h"
- #include "ResourceManagerPtr.h"
- #include "Resource.h"
- #include "String.h"
- #include "safe_new.h"
-
- BMPPictureFormat::BMPPictureFormat(const String & name)
- : data( 0 )
- {
- AutoPtr<Resource> res = ResourceManagerPtr()->create_resource( name + ".bmp" );
- if( !res )
- return;
-
- if( res->read( &file_header, sizeof(file_header) ) != sizeof(file_header) )
- return;
- if( file_header.bfType != 0x4d42 )
- return;
- if( res->read( &bitmap_info_header, sizeof(bitmap_info_header) ) != sizeof(bitmap_info_header) )
- return;
- if( bitmap_info_header.biBitCount <= 8 )
- return;
-
- unsigned sx = bitmap_info_header.biWidth;
- unsigned sy = bitmap_info_header.biHeight;
-
- unsigned char * fdata = new unsigned char[sx*sy*4];
- res->read( fdata, sx*sy*4 );
- data = new Color[sx*sy];
-
- const unsigned char * curr = fdata;
- for( unsigned y = 0; y < sy; ++y )
- {
- Color * dst = data + get_shift( 0, sy-1-y );
- for( unsigned x = 0; x < sx; ++x )
- {
- unsigned char b = *curr++;
- unsigned char g = *curr++;
- unsigned char r = *curr++;
- unsigned char a = 0xFF;
- if( bitmap_info_header.biBitCount == 32 )
- a = *curr++;
- *dst++ = Color( r,g,b,a );
- }
- if( bitmap_info_header.biBitCount == 24 )
- curr += (4 - (sx*3) % 4) % 4;
- }
- delete fdata; fdata = 0;
- }
-
- BMPPictureFormat::~BMPPictureFormat()
- {
- delete data; data = 0;
- }
-
- unsigned BMPPictureFormat::width()const
- {
- return bitmap_info_header.biWidth;
- }
-
- unsigned BMPPictureFormat::height()const
- {
- return bitmap_info_header.biHeight;
- }
-
- const Color * BMPPictureFormat::colors()const
- {
- return data;
- }
-