-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheasy_image.h
More file actions
executable file
·279 lines (250 loc) · 8.5 KB
/
easy_image.h
File metadata and controls
executable file
·279 lines (250 loc) · 8.5 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
/*
* easy_image.h
* Copyright (C) 2011 Daniel van den Akker
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef EASY_IMAGE_INCLUDED
#define EASY_IMAGE_INCLUDED
#include <stdint.h>
#include <iostream>
#include <vector>
#include "util/ZBuffer.h"
/**
* \brief The namespace of the EasyImage class
*/
namespace img {
/**
* \brief This class represents the color of a pixel in an img::EasyImage object
*/
class Color {
// a safety warning to all of you: Do *NOT* rearrange the 'color components'
// in this class easyimage expects these three fields to be the *first* fields
// in the class AND expects them to be in *this* order if you alter the
// arrangement, the generated BMP files will contain garbage
public:
/**
* \brief The intensity of the blue color component
*/
uint8_t blue;
/**
* \brief The intensity of the green color component
*/
uint8_t green;
/**
* \brief The intensity of the red color component
*/
uint8_t red;
/**
* \brief Default Constructor
*/
Color();
/**
* \brief Constructs a Color with the given intensities
*
* \param r The red color component
* \param g The green color component
* \param b The blue color component
*
*/
Color(uint8_t r, uint8_t g, uint8_t b);
/**
* Destructor
*/
~Color();
};
/**
* \brief The exception that is thrown when an error occurs while trying to read
* an img::EasyImage from an input stream
*/
class UnsupportedFileTypeException : public std::exception {
private:
/**
* \brief Message explaining what went wrong
*/
std::string message;
public:
/**
* \brief Construct an exception with the given message
*
* \param msg The message explaining what went wrong
*
*/
UnsupportedFileTypeException(std::string const& msg);
/**
* \brief Copy Constructor
*
* \param original The exception to be copied into this object
*/
UnsupportedFileTypeException(const UnsupportedFileTypeException& original);
/**
* \brief Destructor
*/
virtual ~UnsupportedFileTypeException() throw();
/**
* \brief Assignment operator
*
* \param original The original exception to be assigned to this one
*/
UnsupportedFileTypeException& operator=(
const UnsupportedFileTypeException& original);
/**
* \brief Returns a description of the error hat occurred.
*
* \return A description of the error hat occurred.
*/
virtual const char* what() const throw();
};
/**
* \brief This class implements a 'minor' image-library that supports basic
* operations such as setting and retrieving a pixel, and drawing a line.
*/
class EasyImage {
public:
/**
* \brief Default Constructor. Creates a zero-pixel image
*/
EasyImage();
/**
* \brief Constructor: creates a new EasyImage of the specified width and
* height
*
* \param width the width of the image
* \param height the height of the image
* \param color (optional) the background color of the image
*/
EasyImage(unsigned int width, unsigned int height, Color color = Color());
/**
* \brief Copy Constructor
*
* \param img the image to be copied
*/
EasyImage(EasyImage const& img);
/**
* \brief Destructor
*/
virtual ~EasyImage();
/**
* \brief Assignment operator. Allows an easyImage to be assigned to another
* easyImage
*
* \param img The image to be assigned to this image
*/
EasyImage& operator=(EasyImage const& img);
/**
* \brief Returns the width of the image
*
* \return the width of the image
*/
unsigned int get_width() const;
/**
* \brief Returns the height of the image
* \return the height of the image
*/
unsigned int get_height() const;
/**
* \brief Function operator. This operator returns a reference to a particular
* pixel of the image.
*
* \param x the x coordinate of the pixel
* \param y the y coordinate of the pixel
*
* These assertions apply:
* assert(x>=0 && x < getWidth())
* assert(y>=0 && y < getHeight())
*/
Color& operator()(unsigned int x, unsigned int y);
/**
* \brief Function operator. This operator returns a const reference to a
* particular pixel of the image.
*
* \param x the x coordinate of the pixel
* \param y the y coordinate of the pixel
*
* These assertions apply:
* assert(x>=0 && x < getWidth())
* assert(y>=0 && y < getHeight())
*/
Color const& operator()(unsigned int x, unsigned int y) const;
/**
* \brief Fills the image with a background of a specified color. Defaults to
* black
*
* \param color The color to be assigned to each pixel
*/
void clear(Color color = Color());
/**
* \brief Draws a line from pixel (x0,y0) to pixel (x1,y1) in the specified
*color
*
* \param x0 the x coordinate of the first pixel
* \param y0 the y coordinate of the first pixel
* \param x1 the x coordinate of the second pixel
* \param y1 the y coordinate of the second pixel
* \param color the color of the line
*
* These assertions apply:
* assert(x0 < getWidth())
* assert(y0 < getHeight())
* assert(x1 < getWidth())
* assert(y1 < getHeight())
*/
void draw_line(unsigned int x0, unsigned int y0, unsigned int x1,
unsigned int y1, Color color);
void draw_zbuf_line(ZBuffer &, const unsigned int x0, const unsigned int y0, const double z0, const unsigned int x1, const unsigned int y1, const double z1, const Color &color);
private:
friend std::istream& operator>>(std::istream& in, EasyImage& image);
/**
* \brief the width of the image
*/
unsigned int width;
/**
* \brief the height of the image
*/
unsigned int height;
/**
* \brief the vector containing all pixels
*/
std::vector<Color> bitmap;
};
/**
* \brief Writes an img::EasyImage to an output stream in the BMP file format
*
* \param out the std::ostream to write the BMP file to.
* \param image the img::EasyImage to be written to the output stream
*
* \return a reference to the output stream the image was written
* to
*/
std::ostream& operator<<(std::ostream& out, EasyImage const& image);
/**
* \brief Reads an img::EasyImage from an input stream.
*
* Please note: at this point only a limited subset of BMP-file format is
*supported. In order to correctly read a BMP file it must:
* - Be an uncompressed bitmap
* - Only contain one plane
* - Use 24bits/pixel
* If the BMP file-format is not supported an UnsupportedFileTypeException is
*thrown
*
* \param in the input stream to read the bitmap from
* \param image the EasyImage object in which the bitmap must be stored
*
* \return a reference to the input stream from which the bitmap was
*read
*/
std::istream& operator>>(std::istream& in, EasyImage& image);
} // namespace img
#endif /* EASY_IMAGE_INCLUDED */