-
Notifications
You must be signed in to change notification settings - Fork 5
/
lut3d.cpp
109 lines (107 loc) · 2.58 KB
/
lut3d.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
#include "lut3d.h"
const char* LUT3D::colorString[] = {"Clear",
"White",
"Black",
"Orange",
"Blue",
"Yellow",
"Pink",
"Green",
"Cyan",
"Field Green",
"Red" };
LUT3D::LUT3D()
{
size = 0x01 << (8+8+8);
lut = new char[size];
if(!lut)
{
printf("Error! LUT not allocated.\n");
exit(0);
}
for (int i = 0; i < size; ++i)
lut[i] = UNDEF;
colorValues[UNDEF] = CV_RGB(-1, -1, -1);
colorValues[WHITE] = CV_RGB(255, 255, 255);
colorValues[BLACK] = CV_RGB(0, 0, 0);
colorValues[ORANGE] = CV_RGB(255, 128, 0);
colorValues[BLUE] = CV_RGB(0, 0, 255);
colorValues[YELLOW] = CV_RGB(255, 255, 0);
colorValues[PINK] = CV_RGB(255, 0, 255);
colorValues[GREEN] = CV_RGB(0, 255, 0);
colorValues[CYAN] = CV_RGB(0, 255, 255);
colorValues[FIELD_GREEN] = CV_RGB(0, 128, 0);
colorValues[RED] = CV_RGB(255, 0, 0);
}
const char *LUT3D::getString(Color c)
{
if(c >= MAX_COLORS)
{
printf("Error: color out of bounds!\n");
return NULL;
}
return colorString[c];
}
void LUT3D::reset()
{
for (int i = 0; i < size; ++i)
{
lut[i]= UNDEF;
}
}
void LUT3D::set_bgr(int b, int g, int r, Color c)
{
#define WR(a,m) ((a>=0) && (a<=m))
if(c >= MAX_COLORS)
{
printf("Error: color out of bounds!\n");
return;
}
if(WR(b,255) && WR(g,255) && WR(r,255))
LOOKUP_BGR(b,g,r) = c;
#undef WR
}
void LUT3D::set_yuv(int y, int u, int v, Color c)
{
if(c >= MAX_COLORS)
{
printf("Error: color out of bounds yuv!\n");
return;
}
int b, g, r;
yuv2rgb(y, u, v, r, g, b);
LOOKUP_BGR(b,g,r) = c;
}
Color LUT3D::get_bgr(int b, int g, int r)
{
return (Color)LOOKUP_BGR(b,g,r);
}
CvScalar LUT3D::getScalar(Color c)
{
if(c >= MAX_COLORS)
{
printf("Error: color out of bounds scalar!\n");
return colorValues[UNDEF];
}
return colorValues[c];
}
LUT3D::~LUT3D()
{
saveLUT();
if(lut)
delete[] lut;
}
void LUT3D::saveLUT()
{
if(!lut)
return;
FILE *f = fopen("lut-bgr", "w");
for(int i=0; i<size; i++)
lut[i] += 'a';
if(!f)
return;
fprintf(f, "%s\n", lut);
for(int i=0; i<size; i++)
lut[i] -= 'a';
fclose(f);
}