-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathImage.cpp
81 lines (60 loc) · 1.4 KB
/
Image.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
#include "Image.h"
#include "Color.h"
#include <vector>
#include <algorithm>
namespace Gif2UnicornHat {
Image::Image(Dimension width, Dimension height, Color background /*=Color()*/)
: width_(width),
height_(height),
canvas_(width*height, background)
{
}
Image::Dimension Image::width() const
{
return width_;
}
Image::Dimension Image::height() const
{
return height_;
}
Image Image::scale2x() const
{
Image img(width_*2, height_*2);
for (Dimension x = 0; x < width_; ++x) {
for (Dimension y = 0; y < height_; ++y) {
// Each pixel in the source turns into 4 in the target.
const Color px = (*this)[x][y];
img[2*x][2*y] = px;
img[2*x][2*y+1] = px;
img[2*x+1][2*y] = px;
img[2*x+1][2*y+1] = px;
}
}
return img;
}
void Image::fill(const Color& c)
{
std::fill(begin(canvas_), end(canvas_), c);
}
Image::AccessorProxy::AccessorProxy(Image* image, Dimension x)
: image_(image),
x_(x)
{
}
Color& Image::AccessorProxy::operator[] (Dimension y)
{
return image_->canvas_[y*image_->width()+x_];
}
const Color& Image::AccessorProxy::operator[] (Dimension y) const
{
return image_->canvas_[y*image_->width()+x_];
}
Image::AccessorProxy Image::operator[] (Dimension x)
{
return AccessorProxy(this, x);
}
const Image::AccessorProxy Image::operator[] (Dimension x) const
{
return AccessorProxy(const_cast<Image*>(this), x);
}
}