-
Notifications
You must be signed in to change notification settings - Fork 3
/
pallete.cpp
346 lines (311 loc) · 7.96 KB
/
pallete.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
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
#include "pallete.h"
#include "vmath.h"
#include <iostream>
#include <cmath>
#include <map>
const unsigned int Pallete::WHITE = 0x00FFFFFF; //255, 255, 255
const unsigned int Pallete::RED = 0x00FF0000; //0,0, 255
const unsigned int Pallete::YELLW = 0x00FFFF00; //0, 255, 255
const unsigned int Pallete::PURPL = 0x00800080; //128, 0, 128
const unsigned int Pallete::BLACK = 0x00000000; //0, 0, 0
const float Pallete::PI0 = 3.14159265358979323846;
const float Pallete::PI3 = 6.28318531;
void Pallete::InitView(CImage *img, VIREO_COLOR color)
{
int i = 0, j = 0;
float bgcolor[3];
Pallete::fetchColor(color, bgcolor);
for(i = 0; i < img->height; i++)
{
for(j = 0; j < img->width; j++)
{
img->setPixel(j, i, bgcolor);
}
}
}
bool Pallete::fetchColor(VIREO_COLOR color, float mycolor[3])
{
unsigned char *pt;
switch(color)
{
case _vireo_white:
{
pt = (unsigned char*)&Pallete::WHITE;
mycolor[0] = (float)(*pt);
pt++;
mycolor[1] = (float)(*pt);
pt++;
mycolor[2] = (float)(*pt);
break;
}
case _vireo_red:
{
pt = (unsigned char*)&Pallete::RED;
mycolor[0] = (float)(*pt);
pt++;
mycolor[1] = (float)(*pt);
pt++;
mycolor[2] = (float)(*pt);
break;
}
case _vireo_purple:
{
pt = (unsigned char*)&Pallete::PURPL;
mycolor[0] = (float)(*pt);
pt++;
mycolor[1] = (float)(*pt);
pt++;
mycolor[2] = (float)(*pt);
break;
}
case _vireo_black:
{
pt = (unsigned char*)&Pallete::BLACK;
mycolor[0] = (float)(*pt);
pt++;
mycolor[1] = (float)(*pt);
pt++;
mycolor[2] = (float)(*pt);
break;
}
case _vireo_yellow:
{
pt = (unsigned char*)&Pallete::YELLW;
mycolor[0] = (float)(*pt);
pt++;
mycolor[1] = (float)(*pt);
pt++;
mycolor[2] = (float)(*pt);
break;
}
default:
{
pt = (unsigned char*)&Pallete::WHITE;
mycolor[0] = (float)(*pt);
pt++;
mycolor[1] = (float)(*pt);
pt++;
mycolor[2] = (float)(*pt);
}
}
return true;
}
void Pallete::display(CImage *img, const unsigned int x0, const unsigned int y0, CornType mytype)
{
int dx, dy, x, y;
float cred[3];
float cblk[3];
unsigned char *pt;
pt = (unsigned char*)&Pallete::RED;
cred[0] = (float)(*pt);
pt++;
cred[1] = (float)(*pt);
pt++;
cred[2] = (float)(*pt);
pt = (unsigned char*)&Pallete::BLACK;
cblk[0] = (float)(*pt);
pt++;
cblk[1] = (float)(*pt);
pt++;
cblk[2] = (float)(*pt);
if(mytype == _JUNC)
{
for(dx = -5; dx <= 5; dx++)
{
x = x0 + dx;
img->setPixel(x, y0, cred);
}
for(dy = -5; dy <= 5; dy++)
{
y = y0 + dy;
img->setPixel(x0, y, cred);
}
}
else if (mytype == _CORNER)
{
/**/
for(dx = -5; dx <= 5; dx++)
{
x = x0 + dx;
img->setPixel(x, y0, cred);
}
for(dy = -5; dy <= 5; dy++)
{
y = y0 + dy;
img->setPixel(x0, y, cred);
}
/**/
img->setPixel(x0, y0, cred);
}
else
{
//cout<<"hello\n";
img->setPixel(x0, y0, cblk);
}
}
void Pallete::drawEllipse(CImage *img, KeyPoint *crnt_pt, VIREO_COLOR fcolor)
{
float angle = 0, x, y, xbar, ybar;
float eigns[2];
unsigned int xi, yi, i;
float dx, dy;
float cred[3];
float cblk[3];
Pallete::fetchColor(_vireo_red, cred);
Pallete::fetchColor(fcolor, cblk);
eigns[0] = crnt_pt->e1;
eigns[1] = crnt_pt->e2;
unsigned int x0 = crnt_pt->x;
unsigned int y0 = crnt_pt->y;
unsigned int div = 100*2*Pallete::PI0*sqrt(0.5*(eigns[0]*eigns[0]+eigns[1]*eigns[1]));
float delta_alpha = Pallete::PI0/div, alpha = 0;
angle = crnt_pt->sori;
for(dx = -5; dx <= 5; dx++)
{
x = x0 + dx;
img->setPixel(x, y0, cred);
}
for(dy = -5; dy <= 5; dy++)
{
y = y0 + dy;
img->setPixel(x0, y, cred);
}
div = 2*div;
for(i = 0; i < div; i++)
{
y = eigns[1]*sin(alpha);
x = eigns[0]*cos(alpha);
xbar = x*cos(angle) + y*sin(angle);
ybar = y*cos(angle) - x*sin(angle);
xi = x0 + (unsigned int)round(xbar);
yi = y0 + (unsigned int)round(ybar);
img->setPixel(xi-1, yi, cblk);
img->setPixel(xi, yi, cblk);
img->setPixel(xi, yi-1, cblk);
alpha += delta_alpha;
}
}
void Pallete::lineto(CImage *Img, const unsigned int x0, const unsigned int y0,
const unsigned int x1, const unsigned int y1, VIREO_COLOR fcolor)
{
float sy, sx, ey, ex;
int i, height, width;
float dx, dy, absval, ratio, px, py;
int crnt_y, crnt_x;
float mycolor[3];
Pallete::fetchColor(fcolor, mycolor);
height = Img->height;
width = Img->width;
sx = x0;
sy = y0;
ex = x1;
ey = y1;
dx = ex - sx;
dy = ey - sy;
if(dx == 0)
{
absval = VMath::absx(dy);
crnt_x = ex;
for(i = 1; i < absval; i++)
{
crnt_y = ey - VMath::Sign(dy)*i;
if((crnt_x < width)&&(crnt_y < height))
{
Img->setPixel(crnt_x, crnt_y, mycolor);
Img->setPixel(crnt_x+1, crnt_y, mycolor);
}
}
return ;
}
if(dy == 0)
{
absval = VMath::absx(dx);
crnt_y = ey;
for(i = 1; i < absval; i++)
{
crnt_x = ex - VMath::Sign(dx)*i;
if((crnt_x < width)&&(crnt_y < height))
{
Img->setPixel(crnt_x, crnt_y, mycolor);
Img->setPixel(crnt_x+1, crnt_y, mycolor);
}
}
return;
}
ratio = dy/dx;
py = 0;
px = 0;
ratio = VMath::absx(ratio);
if(ratio > 1)
{
//step by y
absval = dy >= 0?dy:(-dy);
for(i = 1; i < absval; i++)
{
px = i/ratio;
crnt_x = sx + VMath::Sign(dx)*(int)round(px);
crnt_y = sy + VMath::Sign(dy)*i;
if((crnt_x < width) && (crnt_y < height))
{
Img->setPixel(crnt_x, crnt_y, mycolor);
Img->setPixel(crnt_x+1, crnt_y, mycolor);
}
}
}
else //step by x
{
absval = dx>=0?dx:(-dx);
for(i = 1; i < absval; i++)
{
py = i*ratio;
crnt_y = sy + VMath::Sign(dy)*(int)round(py);
crnt_x = sx + VMath::Sign(dx)*i;
crnt_y = crnt_y <= 0?1:crnt_y;
if((crnt_x < width)&&(crnt_y < height))
{
Img->setPixel(crnt_x, crnt_y, mycolor);
Img->setPixel(crnt_x+1, crnt_y, mycolor);
}
}
}
return ;
}
void Pallete::buildView(const char *srcfn, const char *dstfn, vector<KeyPoint*> &kps,
VIREO_COLOR fcolor, KeyPtType flt)
{
vector<KeyPoint*>::iterator itkp;
KeyPoint* crnt_kpt;
CImage *view = new CImage(srcfn);
for(itkp = kps.begin(); itkp != kps.end(); itkp++)
{
crnt_kpt = *itkp;
if(crnt_kpt->_type == flt)
{
Pallete::drawEllipse(view, crnt_kpt, fcolor);
}
}
view->save(dstfn);
delete view;
}
void Pallete::buildEdgeView(const unsigned char* edgeimg, const int width, const int height, const char *dstfn)
{
Image *myimg = new Image(width, height);
int count = 0;
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
myimg->pix[count] = edgeimg[count];
count++;
}
}
myimg->save(dstfn);
delete myimg;
myimg = NULL;
}
void Pallete::test()
{
float mycolor[3];
Pallete::fetchColor(_vireo_purple, mycolor);
cout<<"Color: "<<mycolor[0]<<"\t"<<mycolor[1]<<"\t"<<mycolor[2]<<endl;
}