-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreen.h
More file actions
131 lines (116 loc) · 2.88 KB
/
Screen.h
File metadata and controls
131 lines (116 loc) · 2.88 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
128
129
130
131
#pragma once
#include "stdafx.h"
#include "opencv2\opencv.hpp"
using std::vector;
using cv::Mat;
using cv::Vec3b;
typedef unsigned char uchar;
struct Color {
private:
Real r = 0, g = 0, b = 0;
public:
Color() {}
Color(Real _r, Real _g, Real _b)
: r(_r), g(_g), b(_b) {
}
Vec3b value() {
return Vec3b(
std::min((int)b, 255),
std::min((int)g, 255),
std::min((int)r, 255));
}
Color scale(Real k) const {
return Color(k*r, k*g, k*b);
}
static Color fromHSV(Real, Real, Real);
static Color gray(Real g) {
return Color(g, g, g);
}
inline Real red() const { return r; }
inline Real green() const { return g; }
inline Real blue() const { return b; }
inline void operator+=(const Color& c) {
r += c.red();
g += c.green();
b += c.blue();
}
inline Color filter(Real _r, Real _g, Real _b) const {
return Color(
red() * _r,
green() * _g,
blue() * _b);
}
inline Color filter(const std::vector<Real> &k) const {
return filter(k[0], k[1], k[2]);
}
inline Color filter(const std::vector<Real> &k, Real w) const {
return filter(k[0] / w, k[1] / w, k[2] / w);
}
inline Color gammaCorrection(Real gamma) {
return Color(
255. * pow(r / 255., gamma),
255. * pow(g / 255., gamma),
255. * pow(b / 255., gamma));
}
};
struct Point {
Real x, y;
Point(Real x_, Real y_) : x(x_), y(y_) {}
};
class Screen {
private:
Real top, bottom, left, right;
int width, height;
// granularity
Real dx, dy;
Mat bitmap;
public:
// status
Color color = Color();
bool antialiasing = false;
public:
Screen(Real t, Real b, Real l, Real r, int w, int h) :
top(t), bottom(b), left(l), right(r), width(w), height(h) {
bitmap = Mat(cv::Size(width, height), CV_8UC3);
for (int w = 0; w < width; w++) {
for (int h = 0; h < height; h++) {
bitmap.at<Vec3b>(h, w) = color.value();
}
}
dx = (right - left) / width;
dy = (top - bottom) / height;
}
Screen(int w, int h) : width(w), height(h) {
bitmap = Mat(cv::Size(width, height), CV_8UC3);
for (int w = 0; w < width; w++) {
for (int h = 0; h < height; h++) {
bitmap.at<Vec3b>(h, w) = color.value();
}
}
}
int getWidth() const { return width; }
int getHeight() const { return height; }
int getX(Real x) const { return lround((x - left) / dx); }
int getY(Real y) const { return lround((y - bottom) / dy); }
Real getDX() const { return dx; }
Real getDY() const { return dy; }
void drawPixel(int i, int j);
void drawPoint(Point p);
void clear(Color);
void show();
void writeImage(const char *filename);
void drawLine(Point, Point);
void drawPolygon(const std::vector<Point> &);
private:
void coreDrawLine();
void coreDrawPixel(int i, int j);
};
#pragma region constants
namespace Colors {
const Color black = Color(0, 0, 0);
const Color red = Color(255, 0, 0);
const Color white = Color(255, 255, 255);
const Color green = Color(0, 255, 0);
const Color blue = Color(0, 0, 255);
}
#pragma endregion