-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmp.cpp
executable file
·83 lines (73 loc) · 1.57 KB
/
bmp.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "bmp.h"
#include "vector.h"
#include <fstream>
#include <iostream>
const int BPP=32;
struct bmpfile_magic {
unsigned char magic[2];
};
struct bmpfile_header {
int filesz;
short creator1;
short creator2;
int bmp_offset;
};
typedef struct {
int header_sz;
int width;
int height;
short nplanes;
short bitspp;
int compress_type;
int bmp_bytesz;
int hres;
int vres;
int ncolors;
int nimpcolors;
} BITMAPINFOHEADER;
Bmp::Bmp(int width, int height) {
this->width = width;
this->height = height;
image = new Vector*[height];
for (int i = 0; i < height; ++i) {
image[i] = new Vector[width];
}
}
Bmp::~Bmp() {
for (int i = 0; i < height; ++i) {
delete[] image[i];
}
delete[] image;
}
void Bmp::fill(Vector color) {
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
image[i][j] = color;
}
}
}
void Bmp::setPixel(int x, int y, Vector color) {
if (x < 0 || y < 0 || x >= width || y >= height) {
return;
}
image[y][x] = color;
}
void Bmp::save(std::string fileName) {
std::ofstream out(fileName.c_str(),std::ios::binary);
bmpfile_magic bm={'B','M'};
bmpfile_header bh={54+((BPP*width)/8)*height,0,0,54};
BITMAPINFOHEADER bhi={40,width,height,1,BPP,0,((BPP*width)/8)*height,2750,2750,0,0};
out.write((char*)&bm,sizeof(bm));
out.write((char*)&bh,sizeof(bh));
out.write((char*)&bhi,sizeof(bhi));
for(int i=0;i < height;i++) {
for(int j=0;j < width;j++)
{
out.put((char)image[i][j].getZ());
out.put((char)image[i][j].getY());
out.put((char)image[i][j].getX());
out.put((char)255);
}
}
out.close();
}