home *** CD-ROM | disk | FTP | other *** search
- #include "PictureFormatBMP.h"
- #include <AutoPtr.h>
- #include <Color.h>
- #include <Resource/ResourceManager.h>
- #include <String.hpp>
- #include <safe_new.h>
-
- PictureFormatBMP::PictureFormatBMP(const String & name)
- : data( 0 )
- {
- AutoPtr<Resource> res = ResourceManager::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;
-
- int sx = bitmap_info_header.biWidth;
- int 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( int y = 0; y < sy; ++y )
- {
- Color * dst = data + get_shift( 0, sy-1-y );
- for( int 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;
- }
-
- PictureFormatBMP::~PictureFormatBMP()
- {
- delete data; data = 0;
- }
-
- int PictureFormatBMP::width()const
- {
- return bitmap_info_header.biWidth;
- }
-
- int PictureFormatBMP::height()const
- {
- return bitmap_info_header.biHeight;
- }
-
- const Color * PictureFormatBMP::colors()const
- {
- return data;
- }
-