forked from EdSalisbury/jigsaw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.cpp
305 lines (263 loc) · 6.76 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
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
/**********************************************************
* Jigsaw *
* A jigsaw puzzle creator/solver/game *
* by Ed Salisbury ([email protected]), *
* Matt Goralczyk ([email protected]), *
* and James Brayton ([email protected]) *
* (c)2012, Some Rights Reserved *
**********************************************************/
/*
* License:
* Except where otherwise noted, this work is licensed under Creative Commons
* Attribution ShareAlike 3.0.
*
* You are free:
* ~ to Share -- to copy, distribute and transmit the work
* ~ to Remix -- to adapt the work
*
* Under the following conditions:
* ~ Attribution. You must attribute the work in the manner specified by the
* author or licensor (but not in any way that suggests that they endorse
* you or your use of the work).
* ~ Share Alike. If you alter, transform, or build upon this work, you may
* distribute the resulting work only under the same, similar or a
* compatible license.
* ~ For any reuse or distribution, you must make clear to others the license
* terms of this work. The best way to do this is with a link to the
* license's web page (http://creativecommons.org/licenses/by-sa/3.0/)
* ~ Any of the above conditions can be waived if you get permission from the
* copyright holder.
* ~ Nothing in this license impairs or restricts the author's moral rights.
*/
#include "image.h"
#include <iostream>
#include <cstdlib>
Image::Image()
{
ilInit();
mWidth = 0;
mHeight = 0;
}
Image::Image(UINT width, UINT height)
{
mWidth = width;
mHeight = height;
this->Init();
}
Image::~Image()
{
this->Destroy();
}
UINT Image::Width() const
{
return this->mWidth;
}
UINT Image::Height() const
{
return this->mHeight;
}
bool Image::Load(std::string filename)
{
// Prepare for image loading
ILuint image_name;
ilGenImages(1, &image_name);
ilBindImage(image_name);
// Load the image
if (!ilLoadImage(filename.c_str()))
return false;
// Get image data
mWidth = ilGetInteger(IL_IMAGE_WIDTH);
mHeight = ilGetInteger(IL_IMAGE_HEIGHT);
ILubyte* data = ilGetData();
// Determine if this image has alpha components
bool alpha = false;
if (ilGetInteger(IL_IMAGE_FORMAT) == IL_RGBA)
{
alpha = true;
}
// Initialize the data vector
this->Init();
// Load the image data into the data vector
for (UINT y = 0 ; y < mHeight ; ++y)
{
for (UINT x = 0 ; x < mWidth ; ++x)
{
UINT pos;
if (alpha)
{
pos = (y * mWidth + x) * 4;
}
else
{
pos = (y * mWidth + x) * 3;
}
mData[x][y].Red(data[pos + 0]);
mData[x][y].Green(data[pos + 1]);
mData[x][y].Blue(data[pos + 2]);
if (alpha)
mData[x][y].Alpha(data[pos + 3]);
else
mData[x][y].Alpha(255);
}
}
// Cleanup
ilDeleteImages(1, &image_name);
return true;
}
void Image::ColorReplace(Color& color1, Color& color2)
{
for (UINT y = 0 ; y < mHeight ; ++y)
{
for (UINT x = 0 ; x < mWidth ; ++x)
{
if (mData[x][y] == color1)
{
mData[x][y] = color2;
}
}
}
}
void Image::Opacify()
{
for (UINT y = 0 ; y < mHeight ; ++y)
{
for (UINT x = 0 ; x < mWidth ; ++x)
{
if (mData[x][y].Alpha() != 255)
{
mData[x][y].Red(255);
mData[x][y].Green(255);
mData[x][y].Blue(255);
mData[x][y].Alpha(255);
}
}
}
}
void Image::Fill(Color color)
{
for (UINT y = 0 ; y < mHeight ; ++y)
{
for (UINT x = 0 ; x < mWidth ; ++x)
{
mData[x][y] = color;
}
}
}
void Image::CopyRect(Image& src, UINT src_x, UINT dest_x, UINT width, UINT src_y, UINT dest_y, UINT height)
{
UINT x_offset = dest_x - src_x;
UINT y_offset = dest_y - src_y;
for (UINT y = src_y ; y < height ; ++y)
{
for (UINT x = src_x ; x < width ; ++x)
{
mData[x + x_offset][y + y_offset] = src.mData[x][y];
}
}
}
void Image::Init()
{
mData.resize(mWidth);
for (UINT x = 0 ; x < mWidth ; ++x)
{
mData[x].resize(mHeight);
}
for (UINT y = 0 ; y < mHeight ; ++y)
{
for (UINT x = 0 ; x < mWidth ; ++x)
{
mData[x][y].Red(0);
mData[x][y].Green(0);
mData[x][y].Blue(0);
mData[x][y].Alpha(0);
}
}
}
void Image::Destroy()
{
}
void Image::Plot(UINT x, UINT y, Color color)
{
if (x >= mWidth || y >= mHeight)
{
return;
}
mData[x][y] = color;
}
void Image::DrawLine(UINT startx, UINT starty, UINT endx, UINT endy, Color color)
{
// Implement Bresenham's Line Algorithm
int dx = abs(endx - startx);
int dy = abs(endy - starty);
int sx;
int sy;
if (startx < endx)
sx = 1;
else
sx = -1;
if (starty < endy)
sy = 1;
else
sy = -1;
int err = dx - dy;
bool done = false;
while (!done)
{
Plot(startx, starty, color);
if (startx == endx && starty == endy)
done = true;
else
{
int e2 = 2 * err;
if (e2 > -dy)
{
err -= dy;
startx += sx;
}
if (e2 < dx)
{
err += dx;
starty += sy;
}
}
}
}
bool Image::Save(std::string filename)
{
// Prepare for image saving
ILuint image_name;
ilGenImages(1, &image_name);
ilBindImage(image_name);
// Copy color data to the image
ILubyte* data = new ILubyte[mWidth * mHeight * 4];
for (UINT y = 0 ; y < mHeight ; ++y)
{
for (UINT x = 0 ; x < mWidth ; ++x)
{
UINT pos = (y * mWidth + x) * 4;
data[pos + 0] = (ILubyte)mData[x][mHeight - y - 1].Red();
data[pos + 1] = (ILubyte)mData[x][mHeight - y - 1].Green();
data[pos + 2] = (ILubyte)mData[x][mHeight - y - 1].Blue();
data[pos + 3] = (ILubyte)mData[x][mHeight - y - 1].Alpha();
}
}
ilTexImage(mWidth, mHeight, 1, 4, IL_RGBA, IL_UNSIGNED_BYTE, data);
delete[] data;
ilEnable(IL_FILE_OVERWRITE);
ilSaveImage(filename.c_str());
// Cleanup
ilDeleteImages(1, &image_name);
return true;
}
void Image::Width(const UINT width)
{
mWidth = width;
}
void Image::Height(const UINT height)
{
mHeight = height;
}
Color Image::GetColor(UINT x, UINT y)
{
return (mData[x][y]);
}