-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathroboimage.cpp
More file actions
251 lines (229 loc) · 6.44 KB
/
roboimage.cpp
File metadata and controls
251 lines (229 loc) · 6.44 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
/* This class deals with RoboCup images. It is far from complete. For one,
it only deals with the actual image and not the joint values.
*/
#include "roboimage.h"
/* Constructor. Takes a width and height.
@param wd width of our images
@param ht height
*/
RoboImage::RoboImage(int wd, int ht)
{
width = wd;
height = ht;
yImg = new int*[wd];
uImg = new int*[wd];
vImg = new int*[wd];
for (int i = 0; i < wd; i++) {
yImg[i] = new int[ht];
vImg[i] = new int[ht];
uImg[i] = new int[ht];
}
}
/* Read an image. Only mostly (see above).
Reading bytes in QT is kind of weird (or at least the way I figured out is weird).
We read in one byte chunks - this is done by reading into a byte array of size 1.
Then we need to make sure that QT interprets our numbers the way we want them,
meaning between 0 and 255. This actually takes a fair amount of care. See the
code for more.
@param filename the image filename
*/
void RoboImage::read(QString filename)
{
QFile file(filename);
QTextStream out(stdout);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
out << "The file would not open properly" << "\n";
return;
}
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; x += 2)
{
QByteArray temp;
// read a single byte
temp = file.read(1);
// now put that where we want it
yImg[x][y] = temp[0];
// sometimes it gets interpreted as a negative number, so make it positive
while (yImg[x][y] < 0)
{
yImg[x][y] = 256 + yImg[x][y];
}
// sometimes it gets interpreted as a number larger than 255, so reduce it
yImg[x][y] = yImg[x][y] % 256;
temp = file.read(1);
uImg[x][y] = temp[0];
while (uImg[x][y] < 0)
{
uImg[x][y] = 256 + uImg[x][y];
}
uImg[x][y] = uImg[x][y] % 256;
uImg[x+1][y] = uImg[x][y];
temp = file.read(1);
yImg[x+1][y] = temp[0];
temp = file.read(1);
while (yImg[x+1][y] < 0)
{
yImg[x+1][y] = 256 + yImg[x+1][y];
}
yImg[x+1][y] = yImg[x+1][y] % 256;
vImg[x][y] = temp[0];
while (vImg[x][y] < 0)
{
vImg[x][y] = 256 + vImg[x][y];
}
vImg[x][y] = vImg[x][y] % 256;
vImg[x+1][y] = vImg[x][y];
}
}
file.close();
}
/* Used for returning bitmapped images. Never really got this to work properly.
To Do: fix
*/
QImage RoboImage::fast()
{
ColorZone cz();
return bmp();
}
/* Get R information for the specified pixel
@param x x value
@param y y value
@return amount of red in that pixel
*/
int RoboImage::getRed(int x, int y)
{
ColorSpace c;
c.setYuv(yImg[x][y], uImg[x][y], vImg[x][y]);
return c.getRb();
}
/* Get G information for the specified pixel
@param x x value
@param y y value
@return amount of green in that pixel
*/
int RoboImage::getGreen(int x, int y)
{
ColorSpace c;
c.setYuv(yImg[x][y], uImg[x][y], vImg[x][y]);
return c.getGb();
}
/* Get B information for the specified pixel
@param x x value
@param y y value
@return amount of blue in that pixel
*/
int RoboImage::getBlue(int x, int y)
{
ColorSpace c;
c.setYuv(yImg[x][y], uImg[x][y], vImg[x][y]);
return c.getBb();
}
/* Get H information for the specified pixel
@param x x value
@param y y value
@return amount of Hue in that pixel
*/
int RoboImage::getH(int x, int y)
{
ColorSpace c;
c.setYuv(yImg[x][y], uImg[x][y], vImg[x][y]);
return c.getHb();
}
/* Get S information for the specified pixel
@param x x value
@param y y value
@return amount of saturation in that pixel
*/
int RoboImage::getS(int x, int y)
{
ColorSpace c;
c.setYuv(yImg[x][y], uImg[x][y], vImg[x][y]);
return c.getSb();
}
/* Get Z(v) information for the specified pixel
@param x x value
@param y y value
@return amount of z in that pixel
*/
int RoboImage::getZ(int x, int y)
{
ColorSpace c;
c.setYuv(yImg[x][y], uImg[x][y], vImg[x][y]);
return c.getZb();
}
/* Return a bitmapped image of the image. Doesn't really work.
To Do: fix
*/
QImage RoboImage::bmp()
{
QImage img(width, height, QImage::Format_RGB32);
img.fill(0);
ColorSpace c;
DisplayModes dm = Color;
ColorZone cz;
QTextStream out(stdout);
for (int j = 0; j < height; ++j)
for (int i = 0; i < width; ++i)
{
c.setYuv(yImg[i][j], uImg[i][j], vImg[i][j]);
if (cz.within(c))
{
int r, g, b;
switch (dm)
{
case Color:
r = c.getRb();
g = c.getGb();
b = c.getBb();
//out << i << " " << j << " " << r << " " << g << " " << b << "\n";
break;
case Y:
r = g = b = yImg[i][j];
break;
case U:
r = g = b = uImg[i][j];
break;
case V:
r = g = b = vImg[i][j];
break;
case Red:
r = g = b = c.getRb();
break;
case Green:
r = g = b = c.getGb();
break;
case Blue:
r = g = b = c.getBb();
break;
case Hue:
if (c.getS() >= 0.25f && c.getY() >= 0.2f)
{
ColorSpace h = ColorSpace();
h.setHsz(c.getH(), c.getS(), 0.875f);
r = h.getRb();
g = h.getGb();
b = h.getBb();
}
else
r = g = b = 0;
break;
case Saturation:
r = g = b = c.getSb();
break;
case Value:
r = g = b = c.getZb();
break;
default:
r = g = b = 0;
break;
}
QRgb value = qRgb(r, g, b);
//value = Qt::cyan;
//out << " " << value.red() << " " << value.green() << " " << value.blue() << "\n";
img.setPixel(i, j, value);
}
}
return img;
}