forked from jart/hiptext
-
Notifications
You must be signed in to change notification settings - Fork 2
/
graphic.h
79 lines (65 loc) · 1.94 KB
/
graphic.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
// hiptext - Image to Text Converter
// By Justine Tunney
#ifndef HIPTEXT_GRAPHIC_H_
#define HIPTEXT_GRAPHIC_H_
#include <algorithm>
#include <utility>
#include <vector>
#include <glog/logging.h>
#include "pixel.h"
class Graphic {
public:
Graphic(int width, int height)
: width_(width),
height_(height),
pixels_(width * height) {}
Graphic(int width, int height, const Pixel& pixel)
: width_(width),
height_(height),
pixels_(width * height, pixel) {}
Graphic(int width, int height, std::vector<Pixel>&& pixels)
: width_(width),
height_(height),
pixels_(std::move(pixels)) {
CHECK(width * height == (int)pixels_.size());
}
inline int width() const { return width_; }
inline int height() const { return height_; }
inline Pixel& Get(int x, int y) {
DCHECK_GE(x, 0);
DCHECK_LT(x, width_);
DCHECK_GE(y, 0);
DCHECK_LT(y, height_);
return pixels_[y * width_ + x];
}
inline const Pixel& Get(int x, int y) const {
DCHECK_GE(x, 0);
DCHECK_LT(x, width_);
DCHECK_GE(y, 0);
DCHECK_LT(y, height_);
return pixels_[y * width_ + x];
}
inline Pixel& SafeGet(int x, int y) {
return pixels_[std::max(std::min(y, height_ - 1), 0) * width_ +
std::max(std::min(x, width_ - 1), 0)];
}
inline const Pixel& SafeGet(int x, int y) const {
return pixels_[std::max(std::min(y, height_ - 1), 0) * width_ +
std::max(std::min(x, width_ - 1), 0)];
}
Pixel GetAverageColor(int x, int y, int w, int h) const;
Graphic Copy() const { return *this; }
Graphic& Overlay(Graphic graphic, int offset_x = 0, int offset_y = 0);
Graphic& Opacify(const Pixel& background);
Graphic BilinearScale(int new_width, int new_height) const;
Graphic& Equalize();
Graphic& ToYUV();
Graphic& FromYUV();
Graphic& ToHSV();
Graphic& FromHSV();
private:
int width_;
int height_;
std::vector<Pixel> pixels_;
};
#endif // HIPTEXT_GRAPHIC_H_