-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitmap.cpp
More file actions
67 lines (50 loc) · 1.28 KB
/
Copy pathBitmap.cpp
File metadata and controls
67 lines (50 loc) · 1.28 KB
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
#include "Bitmap.h"
#include "BitmapInfoHeader.h"
#include "BitmapFileHeader.h"
#include <fstream>
using namespace deastisconsulting;
using namespace std;
namespace deastisconsulting
{
Bitmap::Bitmap(int width, int height):
_width(width),
_height(height),
_pPixels(new uint8_t[width * height * 3]{}) //allocate and initialize to zero
{
}
Bitmap::~Bitmap()
{
}
bool Bitmap::write(string filename)
{
BitmapFileHeader bmFileH;
BitmapInfoHeader bmInfoH;
bmFileH.fileSize = sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + _width * _height * 3;
bmFileH.dataOffset = sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader);
bmInfoH.width = _width;
bmInfoH.height = _height;
ofstream outBitMap;
outBitMap.open(filename, ios::out | ios::binary);
if (!outBitMap)
{
return false;
}
outBitMap.write((char*)(&bmFileH), sizeof(bmFileH));
outBitMap.write((char*)(&bmInfoH), sizeof(bmInfoH));
outBitMap.write((char*)_pPixels.get(), _width*_height*3);
outBitMap.close();
if (!outBitMap)
{
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;
}
}