-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage.h
More file actions
384 lines (314 loc) · 11 KB
/
image.h
File metadata and controls
384 lines (314 loc) · 11 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright 2020 Paul Salvador Inventado and Google LLC
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
#include <iostream>
#include <memory>
#include <set>
#include <string>
#include "image_event.h"
#ifndef GRAPHICS_IMAGE_H
#define GRAPHICS_IMAGE_H
namespace cimg_library {
template <class>
class CImg;
class CImgDisplay;
} // namespace cimg_library
using namespace cimg_library;
namespace graphics {
const int kDefaultAnimationMs = 30;
/**
* Represents an RGB pixel color, where |red|, |green| and |blue|
* may be between 0 and 255, inclusive. Default color is black.
*/
class Color {
public:
explicit Color(int red = 0, int green = 0, int blue = 0);
// Copy constructor.
Color(const Color& other) {
red_ = other.Red();
green_ = other.Green();
blue_ = other.Blue();
}
// Assignment operator.
Color& operator=(const Color& other) {
red_ = other.Red();
green_ = other.Green();
blue_ = other.Blue();
return *this;
}
~Color() = default;
// Equality operator.
bool operator==(const Color& other) const {
return red_ == other.Red() && green_ == other.Green() && blue_ == other.Blue();
}
// Inequality operator.
bool operator!=(const Color& other) const {
return red_ != other.Red() || green_ != other.Green() || blue_ != other.Blue();
}
// Getters
int Red() const { return red_; }
int Green() const { return green_; }
int Blue() const { return blue_; }
// Setters
void SetRed(int red) { red_ = red; }
void SetGreen(int green) { green_ = green; }
void SetBlue(int blue) { blue_ = blue; }
private:
int red_;
int green_;
int blue_;
};
// Use by gtest.
static void PrintTo(const Color& color, std::ostream* stream) {
*stream << "Color: (" << color.Red() << "," << color.Green() << ","
<< color.Blue() << ")";
}
class Image {
public:
Image();
~Image();
/**
* Creates a new blank image of size (width, height).
*/
explicit Image(int width, int height);
// Disallow copy and assign.
Image(const Image&) = delete;
Image& operator=(const Image&) = delete;
/*
* Loads an image from a file. Returns false if the image could
* not be loaded. Note: this clears any current state, including
* pixel values, width and height.
*/
bool Load(const std::string& filename);
/*
* Resets the image to be a blank white image size |width| by |height|,
* returns false if unsuccessful (if |width| or |height| are less than 1).
*/
bool Initialize(int width, int height);
/**
* Saves the current image to the file with |filename| in bitmap
* format. Returns false if saving failed.
*/
bool SaveImageBmp(const std::string& filename) const;
/**
* Shows the current image. Returns false if the image could not be shown.
*/
bool Show() { return Show("Image"); }
/**
* Shows the image in a window with the title |title|. Returns false if
* the image could not be shown.
*/
bool Show(const std::string& title) { return ShowForMs(0, title); }
/**
* Shows the image in a window for |milliseconds| duration. Returns false if
* the image could not be shown, or true after |milliseconds| are ellapsed.
*/
bool ShowForMs(int milliseconds) { return ShowForMs(milliseconds, "Image"); }
/**
* Shows the image in a window with the title |title| for |milliseconds|
* duration. Returns false if the image could not be shown, or true after
* |milliseconds| are ellapsed.
*/
bool ShowForMs(int milliseconds, const std::string& title);
/**
* Shows the current image until the window is closed. Returns false if
* the image could not be shown.
*/
bool ShowUntilClosed() { return ShowUntilClosed("Image"); }
/**
* Shows the current image until the window is closed.
* Optional |title| for the window. Returns false if the image
* could not be shown.
*/
bool ShowUntilClosed(const std::string& title) {
return ShowUntilClosed(title, kDefaultAnimationMs);
}
bool ShowUntilClosed(const std::string& title, int animation_ms);
/**
* Refreshes the display with any update to the image. Does nothing if the
* image is not displayed.
*/
void Flush();
/**
* Hides the image if it is currently being shown.
*/
void Hide();
/**
* Returns the width of the loaded image, in pixels.
*/
int GetWidth() const { return width_; }
/**
* Returns the height of the loaded image, in pixels.
*/
int GetHeight() const { return height_; }
/**
* Gets the color at pixel at position (x, y) in the image.
* Returns (-1, -1, -1) if (x, y) is out of bounds.
*/
Color GetColor(int x, int y) const;
/**
* Returns the red component of the RGB pixel at position
* (x, y) in the image. Returns -1 if (x, y) is out of bounds.
*/
int GetRed(int x, int y) const;
/**
* Returns the green component of the RGB pixel at position
* (x, y) in the image. Returns -1 if (x, y) is out of bounds.
*/
int GetGreen(int x, int y) const;
/**
* Returns the blue component of the RGB pixel at position
* (x, y) in the image. Returns -1 if (x, y) is out of bounds.
*/
int GetBlue(int x, int y) const;
/**
* Sets the color of the RGB pixel at position (x, y)
* in the image. Returns false if (x, y) is out of bounds or
* red, green or blue are out of range [0, 255].
*/
bool SetColor(int x, int y, const Color& color);
/**
* Sets the red component of the RGB pixel at position (x, y)
* in the image. Returns false if (x, y) is out of bounds or
* |r| is out of range [0, 255].
*/
bool SetRed(int x, int y, int r);
/**
* Sets the green component of the RGB pixel at position (x, y)
* in the image. Returns false if (x, y) is out of bounds or
* |g| is out of range [0, 255].
*/
bool SetGreen(int x, int y, int g);
/**
* Sets the blue component of the RGB pixel at position (x, y)
* in the image. Returns false if (x, y) is out of bounds or
* |b| is out of range [0, 255].
*/
bool SetBlue(int x, int y, int b);
/**
* Draws a line from (x0, y0) to (x1, y1) with color |color| and optional width |thickness|.
* Returns false if params are out of bounds.
*/
bool DrawLine(int x0, int y0, int x1, int y1, const Color& color, int thickness = 1) {
return DrawLine(x0, y0, x1, y1, color.Red(), color.Green(), color.Blue(), thickness);
}
/**
* Draws a line from (x0, y0) to (x1, y1) with color specified by |red|, |green| and
* |blue| channels, and optional width |thickness|. Returns false if params are out of bounds.
*/
bool DrawLine(int x0, int y0, int x1, int y1, int red, int green, int blue, int thickness = 1);
/**
* Draws a circle centered at (x, y) with radius |radius|, and color
* |color|. Returns false if params are out of bounds.
*/
bool DrawCircle(int x, int y, int radius, const Color& color) {
return DrawCircle(x, y, radius, color.Red(), color.Green(), color.Blue());
}
/**
* Draws a circle centered at (x, y) with radius |radius|, and color
* specified by |red|, |green| and |blue| channels. Returns false if
* params are out of bounds.
*/
bool DrawCircle(int x, int y, int radius, int red, int green, int blue);
/**
* Draws a rectangle with upper left corner at (x, y) and size
* |width| by |height|, colored by |color|. Returns false if
* params are out of bounds.
*/
bool DrawRectangle(int x, int y, int width, int height, const Color& color) {
return DrawRectangle(x, y, width, height, color.Red(), color.Green(),
color.Blue());
}
/**
* Draws a rectangle with upper left corner at (x, y) and size
* |width| by |height|, colored by |red|, |green| and |blue|.
* Returns false if params are out of bounds.
*/
bool DrawRectangle(int x, int y, int width, int height, int red, int green,
int blue);
/**
* Draws the string |text| with position (x,y) at the top left corner,
* with |font_size| in pixels, colored by |color|. Returns false if the
* params are out of bounds.
*/
bool DrawText(int x, int y, const std::string& text, int font_size,
const Color& color) {
return DrawText(x, y, text, font_size, color.Red(), color.Green(),
color.Blue());
}
/**
* Draws the string |text| with position (x,y) at the top left corner,
* with |font_size| in pixels, colored by |red|, |green| and |blue|.
* Returns false if the params are out of bounds.
*/
bool DrawText(int x, int y, const std::string& text, int font_size, int red,
int green, int blue);
/**
* Adds a MouseEventListener to this image. This MouseEventListener's OnMouseEvent
* function will be called whenever the display receives left-button mouse
* events.
*/
void AddMouseEventListener(MouseEventListener& listener) {
if (mouse_listeners_.find(&listener) == mouse_listeners_.end()) {
mouse_listeners_.insert(&listener);
}
}
/**
* Removes a MouseEventListener if it was added. This MouseEventListener's
* OnMouseEvent function will no longer be called when the display receives
* mouse events.
*/
void RemoveMouseEventListener(MouseEventListener& listener) {
if (mouse_listeners_.find(&listener) != mouse_listeners_.end()) {
mouse_listeners_.erase(&listener);
}
}
/**
* Adds a AnimationEventListener to this image. This AnimationEventListener's
* OnAnimationStep function will be called whenever the time has ellapsed
* for the next animation step.
*/
void AddAnimationEventListener(AnimationEventListener& listener) {
if (animation_listeners_.find(&listener) == animation_listeners_.end()) {
animation_listeners_.insert(&listener);
}
}
/**
* Removes a AnimationEventListener if it was added. This
* AnimationEventListener's OnAnimationStep function will no longer be called
* every animation step.
*/
void RemoveAnimationEventListener(AnimationEventListener& listener) {
if (animation_listeners_.find(&listener) != animation_listeners_.end()) {
animation_listeners_.erase(&listener);
}
}
private:
friend class TestEventGenerator;
CImgDisplay* GetDisplayForTesting() {
if (!display_) return nullptr;
return display_.get();
}
void ProcessEvent();
void ProcessAnimation();
bool IsValid() const { return height_ > 0 && width_ > 0; }
bool CheckPixelInBounds(int x, int y) const;
bool CheckColorInBounds(int value) const;
bool CheckColorInBounds(const int value[]) const;
int GetPixel(int x, int y, int channel) const;
bool SetPixel(int x, int y, int channel, int value);
int width_ = 0;
int height_ = 0;
std::unique_ptr<CImg<uint8_t>> cimage_;
std::unique_ptr<CImgDisplay> display_;
int timer_ = 0;
// Mouse listeners. Unowned.
std::set<MouseEventListener*> mouse_listeners_;
// Animation listeners. Unowned.
std::set<AnimationEventListener*> animation_listeners_;
MouseEvent latest_event_ = MouseEvent(0, 0, MouseAction::kReleased);
};
} // namespace graphics
#endif // GRAPHICS_IMAGE_H