-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitmap.h
107 lines (89 loc) · 2.95 KB
/
bitmap.h
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
#ifndef BITMAP_H
#define BITMAP_H
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <fstream>
#include <array>
#include <cassert>
#include <cstdint>
#include "rectangle.h"
typedef unsigned char px;
constexpr int64_t MAX_FILE_SIZE = 10000000; //10 MB
constexpr int32_t HEADER_SIZE = 54;
constexpr std::array<px, HEADER_SIZE> BMP_HEADER = {/* Bitmapfileheader */
'B', 'M', //ASCII "BM"
0, 0, 0, 0, //size in byte
0, 0, 0, 0, //default 0
54, 0, 0, 0, //image data offset in byte
/* Bitmapinfoheader */
40, 0, 0, 0, //size of Bitmapinfoheader in byte
0, 0, 0, 0, //width in pxl
0, 0, 0, 0, //height in pxl
1, 0, //outdated
24, 0, //color depth
0, 0, 0, 0, //compression type
0x00, 0x0C, 0, 0, //size of image data
0, 0, 0, 0, //horizontal resolution, default 0
0, 0, 0, 0, //vertical resolution, default 0
0, 0, 0, 0, //color palette, if used
0, 0, 0, 0 //only if color palette used
};
constexpr int32_t WIDTH_POS = 18; // width
constexpr int32_t HEIGHT_POS = 22; // height
constexpr int32_t DATA_SIZE_POS = 34; // width * height
constexpr int32_t BYTE_SIZE_POS = 2; // width * height + 54
struct pixel
{
px blue;
px green;
px red;
pixel(px blue_, px green_, px red_):
blue(blue_),
green(green_),
red(red_)
{}
pixel () : pixel(255, 255, 255)
{}
};
const pixel BLUE{255, 0, 0};
const pixel GREEN{0, 255, 0};
const pixel RED{0, 0, 255};
const pixel WHITE{255, 255, 255};
const pixel BLACK{0, 0, 0};
struct bitmap
{
std::string filename;
int32_t width;
int32_t height;
std::vector<pixel> data;
int32_t scaling;
bool initialized;
int32_t effective_width;
point base;
bitmap() :
initialized(false)
{}
bitmap(std::string &&filename_, int32_t width_, int32_t height_, int32_t scaling_, point base_) :
filename(filename_),
width(width_ * scaling_),
height(height_ * scaling_ + 1),
scaling(scaling_),
initialized(true),
effective_width((width + 4) / 4 * 4),
base(base_)
{
// I really don't want to plot inst10 by mistake
assert(valid(width, height));
data.resize(height * effective_width * 3, WHITE);
}
static bool valid(const int32_t width, const int32_t height);
void put_pixel(const int32_t x,const int32_t y, const pixel &p);
void write();
void draw_rectangle(const rectangle &rect, const pixel &color);
void fill_rectangle(const rectangle &rect, const pixel &color);
void draw_point(const point &p, const pixel &color);
};
std::ostream &operator<< (std::ostream &out, const pixel &p);
#endif //BITMAP_H