-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitmap.cpp
More file actions
127 lines (111 loc) · 3.52 KB
/
bitmap.cpp
File metadata and controls
127 lines (111 loc) · 3.52 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
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
118
119
120
121
122
123
124
125
126
127
#include "bitmap.h"
/**
* We want to write a lot of pixels...
*/
std::ostream &operator<<(std::ostream &out, const pixel &p)
{
out << p.blue << p.green << p.red;
return out;
}
/**
* Puts the pixel p (aka the color) at position (x,y).
*/
void bitmap::put_pixel(const int32_t x,const int32_t y, const pixel &p)
{
data.at(x + y * effective_width) = p;
}
/**
* Takes an int an stores it in the passed char array.
* WARNING: This should probably be done better, but I don't know how.
*/
void int_to_char_arr(px * to, const int32_t from)
{
for(size_t i = 0; i < sizeof(int32_t); i++)
{
to[i] = (from >> i*8) ;
}
}
void bitmap::write()
{
std::fstream file (filename, std::fstream::out | std::fstream::trunc | std::fstream::binary);
std::array<px, HEADER_SIZE> header = BMP_HEADER;
// Start: BMP Magic
// BMP need a line width divisible by 4.
const int32_t size_data = data.size();
const int32_t size_all = size_data + sizeof(header);
int_to_char_arr(header.data() + WIDTH_POS, effective_width);
int_to_char_arr(header.data() + HEIGHT_POS, height);
int_to_char_arr(header.data() + DATA_SIZE_POS, size_data);
int_to_char_arr(header.data() + BYTE_SIZE_POS, size_all);
for(auto c : header)
{
file << c;
}
// End: BMP Magic
for (int32_t i = 0; i < height; i++)
{
for (int32_t j = i * effective_width; j < (i + 1) * effective_width; j++)
{
file << data.at(j);
}
}
}
/**
* Draws the edges of the specified rectangle with the specified color.
* Applys scaling.
*/
void bitmap::draw_rectangle(const rectangle &rect, const pixel &color)
{
for (dimension dim : all_dimensions)
{
for (int32_t i = scaling * (rect.get_pos(dim) - base.coord(dim)); i <= scaling * (rect.get_max(dim) - base.coord(dim)); ++i)
{
if(dim == dimension::x)
{
put_pixel(i, (rect.get_pos(dim, true) - base.coord(dim, true)) * scaling, color);
put_pixel(i, (rect.get_max(dim, true) - base.coord(dim, true)) * scaling, color);
}
else
{
put_pixel((rect.get_pos(dim, true) - base.coord(dim, true)) * scaling, i, color);
put_pixel((rect.get_max(dim, true) - base.coord(dim, true)) * scaling, i, color);
}
}
}
}
/**
* Fills the interior of the specified rectangle with the specified color.
* Applys scaling.
*/
void bitmap::fill_rectangle(const rectangle &rect, const pixel &color)
{
for (int32_t x = (rect.base.x - base.x) * scaling + 1; x < (rect.get_max(dimension::x) - base.x) * scaling; x++)
{
for (int32_t y = (rect.base.y - base.y) * scaling + 1; y < (rect.get_max(dimension::y) - base.y) * scaling; y++)
{
put_pixel(x, y, color);
}
}
}
/**
* Draws a point. The size is more than one pixel for visibility reasons.
*/
void bitmap::draw_point(const point &p, const pixel &color)
{
assert(p.set);
int32_t x = scaling * (p.x - base.x);
int32_t y = scaling * (p.y - base.y);
for(int32_t i = std::max(0, x - 3); i < std::min(width - 1, x + 3); i++)
{
for(int32_t j = std::max(0, y - 3); j < std::min(height - 1, y + 3); j++)
{
put_pixel(i, j, color);
}
}
}
bool bitmap::valid(const int32_t width,const int32_t height)
{
const int64_t long_width = static_cast<int64_t> (width);
const int64_t long_height = static_cast<int64_t> (height);
return (long_height * long_width) <= MAX_FILE_SIZE/3;
}