if defined __BITMAP_HEADER_FILE __ define __BITMAP_HEADER_FILE __ incl

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#if !defined(__BITMAP_HEADER_FILE__)
#define __BITMAP_HEADER_FILE__
#include <windows.h>
class pixel {
public:
BYTE r,
g,
b;
public:
pixel() : r(0), g(0), b(0) {
};
pixel(BYTE _r, BYTE _g, BYTE _b) : r(_r), g(_g), b(_b) {
};
};
class bitmap {
HBITMAP _hBmp;
BITMAP _bmp;
BYTE *_bits;
long _buf_size;
public:
bitmap() : _hBmp(NULL), _bits(NULL), _buf_size(0) {
};
~bitmap() {
Free();
};
public:
bool Load(const char *path) {
Free ();
_hBmp = (HBITMAP)::LoadImage (0, path, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if(!_hBmp)
return false;
::GetObject(_hBmp, sizeof(_bmp), &_bmp);
_buf_size = _bmp.bmHeight * _bmp.bmWidthBytes;
_bits = new BYTE[_buf_size];
GetBitmapBits(_hBmp, _buf_size, _bits);
return true;
};
void SaveAs(const char *path) {
BITMAPINFOHEADER bmpInfoHeader = {0};
bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfoHeader.biBitCount = _bmp.bmBitsPixel;
bmpInfoHeader.biClrImportant = 0;
bmpInfoHeader.biClrUsed = 0;
bmpInfoHeader.biCompression = BI_RGB;
bmpInfoHeader.biHeight = _bmp.bmHeight;
bmpInfoHeader.biWidth = _bmp.bmWidth;
bmpInfoHeader.biPlanes = 1;
bmpInfoHeader.biSizeImage = bmpInfoHeader.biWidth* bmpInfoHeader.biHeight * (bmpInfoHeader.biBitCount/8);
BITMAPFILEHEADER bfh = {0};
bfh.bfType=0x4D42;
bfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
bfh.bfSize = bfh.bfOffBits + bmpInfoHeader.biSizeImage;
// Create the file in disk to write
HANDLE hFile = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);
if(!hFile)
return;
DWORD dwWritten = 0;
WriteFile( hFile, &bfh, sizeof(bfh), &dwWritten , NULL );
WriteFile( hFile, &bmpInfoHeader, sizeof(bmpInfoHeader), &dwWritten, NULL );
WriteFile( hFile, _bits, bmpInfoHeader.biSizeImage, &dwWritten, NULL );
CloseHandle( hFile );
};
public:
bool IsOk() {
return (_hBmp != NULL);
};
int GetWidth() {
return _bmp.bmWidth;
};
int GetHeight() {
return _bmp.bmHeight;
};
BYTE *GetBits() {
return _bits;
};
pixel GetPixel(int x, int y) {
pixel p;
if(!IsOk())
return p;
if(x < 0 || y < 0 || x > _bmp.bmWidth || y > _bmp.bmHeight)
return p;
size_t i = 0;
i = (_bmp.bmBitsPixel / 8) * y * _bmp.bmWidth + (_bmp.bmBitsPixel / 8) * x;
p.b = _bits[i + 0];
p.g = _bits[i + 1];
p.r = _bits[i + 2];
return p;
};
void SetPixel(int x, int y, pixel p) {
if(!IsOk())
return;
if(x < 0 || y < 0 || x > _bmp.bmWidth || y > _bmp.bmHeight)
return;
size_t i = 0;
i = (_bmp.bmBitsPixel / 8) * y * _bmp.bmWidth + (_bmp.bmBitsPixel / 8) * x;
_bits[i + 0] = p.b;
_bits[i + 1] = p.g;
_bits[i + 2] = p.r;
};
private:
void Free() {
if(_hBmp)
::DeleteObject(_hBmp);
if(_bits) {
delete [] _bits;
_bits = NULL;
}
_buf_size = 0;
};
};
#endif