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
|
namespace Rotor {
class pixeltables{
//handy pixel arithmetic lookup tables as nested arrays
//so - pixels.add[0x78][0x66]; will give the precalculated result of adding with saturation
// pixels.mono_weights[0][0x100]; will give the red component to convert to mono
public:
pixeltables(){
add=new uint8_t*[256];
multiply=new uint8_t*[256];
for (int i=0;i<256;i++){
add[i]=new uint8_t[256];
multiply[i]=new uint8_t[256];
for (int j=0;j<256;j++){
add[i][j]=(uint8_t)min(i+j,0xFF);
multiply[i][j]=(uint8_t)((((float)i)/255.0f)*(((float)j)/255.0f)*255.0f);
}
}
mono_weights=new uint8_t*[3];
float weights[3]={0.2989, 0.5870, 0.1140};
for (int i=0;i<3;i++) {
mono_weights[i]=new uint8_t[256];
for (int j=0;j<256;j++){
mono_weights[i][j]=(uint8_t)(((float)j)*weights[i]);
}
}
}
virtual ~pixeltables(){
for (int i=0;i<256;i++){
delete[] add[i];
delete[] multiply[i];
}
delete[] add;
delete[] multiply;
for (int i=0;i<3;i++) {
delete[] mono_weights[i];
}
delete[] mono_weights;
}
uint8_t **add;
uint8_t **multiply;
uint8_t **mono_weights;
};
static pixeltables pixels;
class Image{
public:
Image(){
zero();
};
Image(int _w,int _h){
zero();
setup(_w,_h);
};
~Image() {
free();
};
void free(){
if (RGBdata&&ownsRGBdata) delete[] RGBdata;
if (Adata&&ownsAdata) delete[] Adata;
if (Zdata&&ownsZdata) delete[] Zdata;
zero();
}
void zero(){
RGBdata=nullptr;
Adata=nullptr;
Zdata=nullptr;
w=0;
h=0;
ownsRGBdata=ownsAdata=ownsZdata=false;
}
bool setup(int _w,int _h){ //set up with internal data
if (w!=_w||h!=_h||!ownsRGBdata||!ownsAdata||!ownsZdata){
free();
w=_w;
h=_h;
RGBdata=new uint8_t[w*h*3];
Adata=new uint8_t[w*h];
Zdata=new uint16_t[w*h];
ownsRGBdata=ownsAdata=ownsZdata=true;
return true;
}
else return false;
}
bool setup_fromRGB(int _w,int _h,uint8_t *pRGBdata){ //possibility of just resetting pointer?
if (w!=_w||h!=_h||ownsRGBdata||!ownsAdata||!ownsZdata){
free();
w=_w;
h=_h;
RGBdata=pRGBdata;
Adata=new uint8_t[w*h];
Zdata=new uint16_t[w*h];
ownsRGBdata=false;
ownsAdata=ownsZdata=true;
return true;
}
return false;
}
Image* clone(){
Image *t=new Image(w,h);
for (int i=0;i<w*h*3;i++) {
t->RGBdata[i]=RGBdata[i];
}
return t;
}
Image & operator+=(const Image &other) {
if (other.w!=w||other.h!=h) {
cerr<<"Rotor: cannot add images with different sizes! (wanted "<<w<<"x"<<h<<", got "<<other.w<<"x"<<other.h<<")"<<endl;
}
else {
for (int i=0;i<w*h*3;i++){
//calculate with tables
RGBdata[i]=pixels.add[RGBdata[i]][other.RGBdata[i]];
}
}
return *this;
}
Image & operator*=(const Image &other) {
if (other.w!=w||other.h!=h) {
cerr<<"Rotor: cannot multiply images with different sizes! (wanted "<<w<<"x"<<h<<", got "<<other.w<<"x"<<other.h<<")"<<endl;
}
else {
for (int i=0;i<w*h*3;i++){
//calculate with tables
uint8_t p1=RGBdata[i];
uint8_t p2=other.RGBdata[i];
RGBdata[i]=pixels.multiply[RGBdata[i]][other.RGBdata[i]];
}
}
return *this;
}
Image & add_wrap(const Image &other) {
if (other.w!=w||other.h!=h) {
cerr<<"Rotor: cannot add images with different sizes! (wanted "<<w<<"x"<<h<<", got "<<other.w<<"x"<<other.h<<")"<<endl;
}
else {
for (int i=0;i<w*h*3;i++){
//creates rainbow overload
RGBdata[i]=(unsigned char)(((int)other.RGBdata[i]+(int)RGBdata[i]));
}
}
return *this;
}
//scalar operations allocate a new image.
//maybe this could not be the case if the data is owned by this image?
//need to look into auto_ptr
Image & operator*=(const float &amount) {
uint8_t *LUT=new uint8_t[256];
for (int i=0;i<256;i++) {
LUT[i]=(uint8_t)min(0xFF,max(0,(int)(i*amount)));
}
for (int i=0;i<w*h*3;i++){
//calculate with table
RGBdata[i]=LUT[RGBdata[i]];
}
delete[] LUT;
return *this;
}
Image * operator*(const float &amount) {
Image *other=new Image(w,h);
uint8_t *LUT=new uint8_t[256];
for (int i=0;i<256;i++) {
LUT[i]=(uint8_t)min(0xFF,max(0,(int)(i*amount)));
}
for (int i=0;i<w*h*3;i++){
other->RGBdata[i]=LUT[RGBdata[i]];
}
delete[] LUT;
return other;
}
Image * operator+(const float &amount) {
Image *other=new Image(w,h);
uint8_t *LUT=new uint8_t[256];
for (int i=0;i<256;i++) {
LUT[i]=(uint8_t)min(0xFF,max(0,(int)(i+(amount*255.0f))));
}
for (int i=0;i<w*h*3;i++){
other->RGBdata[i]=LUT[RGBdata[i]];
}
delete[] LUT;
return other;
}
Image * operator-(const float &amount) {
Image *other=new Image(w,h);
uint8_t *LUT=new uint8_t[256];
for (int i=0;i<256;i++) {
LUT[i]=(uint8_t)min(0xFF,max(0,(int)(i-(amount*255.0f))));
}
for (int i=0;i<w*h*3;i++){
other->RGBdata[i]=LUT[RGBdata[i]];
}
delete[] LUT;
return other;
}
Image * operator/(const float &amount) {
Image *other=new Image(w,h);
uint8_t *LUT=new uint8_t[256];
for (int i=0;i<256;i++) {
LUT[i]=(uint8_t)min(0xFF,max(0,(int)(i/amount)));
}
for (int i=0;i<w*h*3;i++){
other->RGBdata[i]=LUT[RGBdata[i]];
}
delete[] LUT;
return other;
}
uint8_t *RGBdata;
uint8_t *Adata;
uint16_t *Zdata;
int h,w;
bool ownsRGBdata,ownsAdata,ownsZdata; //better done through auto_ptr?
};
}
|