-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bitmap.cpp
63 lines (44 loc) · 1.52 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
56
57
58
59
60
61
62
63
#include <fstream>
#include "Bitmap.h"
#include "BitmapInfoHeader.h"
#include "BitmapFileHeader.h"
using namespace mandelbrot;
using namespace std;
namespace mandelbrot {
Bitmap::Bitmap(int width, int height) :
m_width(width), m_height(height), m_pPixels(
new uint8_t[width * height * 3]{}) {
}
bool Bitmap::write(string filename) {
BitmapFileHeader fileHeader;
BitmapInfoHeader infoHeader;
fileHeader.fileSize = sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader)
+ m_width * m_height * 3;
fileHeader.dataOffset = sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader);
infoHeader.width = m_width;
infoHeader.height = m_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 *) m_pPixels.get(), m_width * m_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 = m_pPixels.get();
pPixel += (y * 3) * m_width + (x * 3);
pPixel[0] = blue;
pPixel[1] = green;
pPixel[2] = red;
}
Bitmap::~Bitmap() {
// TODO Auto-generated destructor stub
}
} /* namespace mandelbrot */