-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.c
191 lines (176 loc) · 5.03 KB
/
image.c
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
#include "image.h"
#define GET_R(px) (((px) & 0xFF000000) >> 24)
#define GET_G(px) (((px) & 0xFF0000) >> 16)
#define GET_B(px) (((px) & 0xFF00) >> 8)
static float _expo;
float* imgScratch;
Uint32 lerp(Uint32 c1, Uint32 c2, double k)
{
double red = ((c1 & 0xFF000000) >> 24) * (1 - k) + ((c2 & 0xFF000000) >> 24) * k;
double grn = ((c1 & 0xFF0000) >> 16) * (1 - k) + ((c2 & 0xFF0000) >> 16) * k;
double blu = ((c1 & 0xFF00) >> 8) * (1 - k) + ((c2 & 0xFF00) >> 8) * k;
return ((Uint32) red << 24) | ((Uint32) grn << 16) | ((Uint32) blu << 8) | 0xFF;
}
//comparator returns -1 if lhs < rhs, 1 if lhs > rhs
static int pixelCompare(const void* lhsRaw, const void* rhsRaw)
{
float lhs = iters[*((int*) lhsRaw)];
float rhs = iters[*((int*) rhsRaw)];
if(lhs == rhs)
return 0;
if(lhs < 0)
return 1;
if(rhs < 0)
return -1;
if(lhs < rhs)
return -1;
if(lhs > rhs)
return 1;
return 0;
}
void handleNonColored()
{
for(int i = 0; i < winw * winh; i++)
{
if(iters[i] == -1)
frameBuf[i] = 0xFF;
else if(iters[i] < 0)
frameBuf[i] = 0x999999FF;
}
}
static int floatCmp(const void* lhs, const void* rhs)
{
float l = *((float*) lhs);
float r = *((float*) rhs);
if(l < r)
return -1;
if(l > r)
return 1;
return 0;
}
float getPercentileValue(float* buf, int w, int h, float proportion)
{
assert(proportion >= 0.0);
int count = 0;
for(int i = 0; i < w * h; i++)
{
if(buf[i] > 0)
{
imgScratch[count++] = buf[i];
}
}
assert(count > 2);
qsort(imgScratch, count, sizeof(float), floatCmp);
int index = count * proportion;
if(index >= count)
index = count - 1;
return imgScratch[index];
}
static double expoMapFunc(double val)
{
return pow(val, _expo) - 1;
}
static double logMapFunc(double val)
{
return log(val + 2) / log(1.05);
}
static void applyCyclicMapping(Image* im, Mapping func)
{
handleNonColored();
int minVal = INT_MAX;
for(int i = 0; i < winw * winh; i++)
{
if(iters[i] > 0 && iters[i] < minVal)
minVal = iters[i];
}
for(int i = 0; i < winw * winh; i++)
{
if(iters[i] >= 0)
imgScratch[i] = func(iters[i] * iterScale);
else
imgScratch[i] = -1.0;
}
double scale = 0.05; //(im->period * im->cycles) / (cap - minMapped);
double perSegment = im->period / im->cycles / im->numColors;
for(int i = 0; i < winw * winh; i++)
{
//subtract 1 so that effective min value maps to 0 (origin in color cycle)
double val = imgScratch[i] * scale;
if(val >= 0)
{
int segment = val / perSegment;
double lerpK = (val - segment * perSegment) / perSegment;
int lowColorIndex = segment % im->numColors;
int highColorIndex = (segment + 1) % im->numColors;
//lerp between low and high color
frameBuf[i] = lerp(im->palette[lowColorIndex], im->palette[highColorIndex], lerpK);
}
}
}
void colorExpoCyclic(Image* im, double expo)
{
_expo = expo;
applyCyclicMapping(im, expoMapFunc);
}
void colorLogCyclic(Image* im)
{
applyCyclicMapping(im, logMapFunc);
}
void colorHist(Image* im)
{
//temporary equal weights for all colors, then do weighted hist coloring
double* weights = alloca(im->numColors * sizeof(double));
for(int i = 0; i < im->numColors; i++)
weights[i] = 1;
colorHistWeighted(im, weights);
}
void colorHistWeighted(Image* im, double* weights)
{
//histogram proportion (i.e. quarter of all is 0.25) multiplied by
int* pixelList = (int*) imgScratch;
for(int i = 0; i < winw * winh; i++)
pixelList[i] = i;
//sort pixelList (iters indices) according to the iter values
qsort(pixelList, winw * winh, sizeof(int), pixelCompare);
if(iters[pixelList[0]] == -1)
{
//something wrong with computation, but fill screen with black and return
for(int i = 0; i < winw * winh; i++)
frameBuf[i] = 0xFF;
return;
}
//get # of diverged pixels
int diverged = winw * winh;
while(iters[pixelList[diverged - 1]] < 0)
diverged--;
int* colorOffsets = alloca(im->numColors * sizeof(int));
double* normalWeights = alloca(im->numColors * sizeof(double));
double accum = 0;
//divide each color weight by sum of all weights
for(int i = 0; i < im->numColors - 1; i++)
accum += weights[i];
for(int i = 0; i < im->numColors - 1; i++)
normalWeights[i] = weights[i] / accum;
normalWeights[im->numColors] = 1;
//determine number of pixels in each "segment"
accum = 0;
for(int i = 0; i < im->numColors; i++)
{
colorOffsets[i] = diverged * accum;
accum += normalWeights[i];
}
int lowerColor = 0;
int firstOfValue = 0; //index of first first pixel encountered with ith value
for(int i = 0; i < diverged; i++)
{
if(iters[pixelList[i]] != iters[pixelList[firstOfValue]])
firstOfValue = i;
while(colorOffsets[lowerColor + 1] <= firstOfValue)
lowerColor++;
int colorLo = colorOffsets[lowerColor];
int colorHi = colorOffsets[lowerColor + 1];
frameBuf[pixelList[i]] = lerp(im->palette[lowerColor], im->palette[lowerColor + 1],
(double) (firstOfValue - colorLo) / (colorHi - colorLo));
}
handleNonColored();
}