-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitmap.cpp
55 lines (45 loc) · 1.42 KB
/
Bitmap.cpp
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
#include <fstream>
#include "Bitmap.h"
#include "BitmapInfoHeader.h"
#include "BitmapFileHeader.h"
using namespace caveofprogramming;
using namespace std;
namespace caveofprogramming
{
Bitmap::Bitmap(){}
Bitmap::Bitmap(int width, int height) :
_width(width), _height(height), _pPixels(new uint8_t[width * height * 3]{}) { // memory allocated and initialized to 0
}
bool Bitmap::write(string filename) {
BitmapFileHeader fileHeader;
BitmapInfoHeader infoHeader;
// file header set
fileHeader.fileSize = sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + _width * _height * 3; // x3 due to rgb colors
fileHeader.dataOffset = sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader); // where bitmap data starts
// info header ser
infoHeader.width = _width;
infoHeader.height = _height;
ofstream file;
file.open(filename, ios::out | ios::binary);
if (!file) {
return false;
}
file.write((char*)&fileHeader, sizeof(fileHeader));
file.write((char*)&infoHeader, sizeof(infoHeader));
file.write((char *)_pPixels.get(), _width * _height * 3);
file.close();
if (!file) {
return false;
}
return true;
}
void Bitmap::setPixel(int x, int y, uint8_t red, uint8_t green, uint8_t blue) {
uint8_t* pPixel = _pPixels.get();
pPixel += (y * 3) * _width + (x * 3);
pPixel[0] = blue;
pPixel[1] = green;
pPixel[2] = red;
}
Bitmap::~Bitmap() {
}
}