www.pudn.com > 32709.zip > image.cpp


/*************************************************** 
 * Developer: Clinton Jon Selke                    * 
 *   Version: Totally FreeWare (Do what you will)  * 
 *   Section: Image Implementation                 * 
 ***************************************************/ 
 
#include "image.h" 
#include "winexception.h" 
#include  
 
Image::Image(): _size_x(0), _size_y(0), _data(0) {} 
Image::Image(const char *filename): _size_x(0), _size_y(0), _data(0) { loadFile(filename); } 
 
Image::~Image() { 
    if (_data) { 
        delete [] _data; 
        _data = 0; 
    } 
    _size_x = _size_y = 0; 
} 
 
void Image::loadFile(const char *filename) { 
    FILE *f = fopen(filename, "rb"); 
    if (!f) { 
        WinException("Unable to load bitmap texture.").doMessageBox(); 
        return; 
    } 
    fseek(f, 18, SEEK_CUR); 
    fread(&_size_x, sizeof(_size_x), 1, f); 
    fread(&_size_y, sizeof(_size_y), 1, f); 
     
    unsigned int size = 3 * _size_x * _size_y; 
     
    short planes; 
    fread(&planes, sizeof(planes), 1, f); 
    if (planes != 1) { 
        WinException("The number of planes in texture must equil one.").doMessageBox(); 
        fclose(f); 
        return; 
    } 
     
    short bpp; 
    fread(&bpp, sizeof(bpp), 1, f); 
    if (bpp != 24) { 
        WinException("The number of bits per pixel must equil 24.").doMessageBox(); 
        fclose(f); 
        return; 
    } 
     
    fseek(f, 24, SEEK_CUR); 
     
    _data = new char[size]; 
    fread(_data, size, 1, f); 
     
    fclose(f); 
     
    for (int i = 0; i < size; i += 3) { 
        char tmp = _data[i]; 
        _data[i] = _data[i + 2]; 
        _data[i + 2] = tmp; 
    } 
}