-
Notifications
You must be signed in to change notification settings - Fork 3
/
imageProcessing.c
185 lines (152 loc) · 5.46 KB
/
imageProcessing.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
#error THIS FILE IS NOT CURRENTLY USED!
#include "imageProcessing.h"
#include "rectangle.h" // for SUPER_SAMPLING
#include <string.h> //for memcpy()
#include <math.h> // for exp()
static uint8_t clamp(float d)
{
if (d < 0) d = 0;
if (d > 255) d = 255;
return d;
}
//convert light energy to perceived brightness
float convert(float color)
{
// if (color < 0.8)
// return 1 - exp(-2*color);
// else
// return 1/16.0*color + 0.75;
return 1 - exp(-1.5*color);
//return 5/8.0* pow(color, 1/3.0);
}
void convert2(float *r, float *g, float *b)
{
float luminance = 0.2126 * (*r) + 0.7152* (*g) + 0.0722 * (*b);
float luminance_perceptive = convert(luminance);
(*r) *= luminance_perceptive/luminance;
(*g) *= luminance_perceptive/luminance;
(*b) *= luminance_perceptive/luminance;
}
int isBlack(const uint8_t *data, int width, int height, int x, int y)
{
if (x < 0) x = 0;
if (x >= width) x = width - 1;
if (y < 0) y = 0;
if (y >= height) y = height-1;
int idx = (y * width + x ) * 3;
return (data[idx] == 0 && data[idx+1] == 0 && data[idx+2] == 0);
}
typedef struct Pixel {
int r,g,b;
} Pixel;
Pixel getPixelData(const uint8_t *data, int width, int height, int x, int y)
{
if (x < 0) x = 0;
if (x >= width) x = width - 1;
if (y < 0) y = 0;
if (y >= height) y = height-1;
int idx = (y * width + x ) * 3;
Pixel res = { .r = data[idx], .g = data[idx+1], .b = data[idx+2] };
return res;
}
void mark(uint8_t *data, int width, int height, int x, int y, int r, int g, int b)
{
if (x < 0) x = 0;
if (x >= width) x = width - 1;
if (y < 0) y = 0;
if (y >= height) y = height-1;
int idx = (y*width + x)*3;
data[idx ] = r;
data[idx+1] = g;
data[idx+2] = b;
}
int hasBlackNeighbor(uint8_t *data, int width, int height, int x, int y)
{
for (int dx = x-1; dx <= x+1; dx++)
for (int dy = y-1; dy <= y+1; dy++)
if (isBlack(data, width, height, dx, dy))
return 1;
return 0;
}
int hasNonBlackNeighbor(uint8_t *data, int width, int height, int x, int y)
{
for (int dx = x-1; dx <= x+1; dx++)
for (int dy = y-1; dy <= y+1; dy++)
if (!isBlack(data, width, height, dx, dy))
return 1;
return 0;
}
void selectiveDilate(uint8_t *data, int width, int height)
{
uint8_t *dataTmp = malloc(width*height*3);
memcpy(dataTmp, data, width*height*3);
//memset(dataTmp, 0, width*height*3);
//int x = 10;
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
{
/*
if ( (x+y) % 2 == 0)
{
printf("(%d, %d)\n", x, y);
mark(data, width, height, x, y, 0, 255, 0);
}*/
/*if (isBlack(data, width, height, x, y))
mark(data, width, height, x, y, 0, 255, 0);
else */if (hasBlackNeighbor(data, width, height, x, y) &&
hasNonBlackNeighbor(data, width, height, x, y))
{
//int neighbors[9] = {0,0,0, 0,0,0, 0,0,0};
Pixel brightest = {.r = 0, .g = 0, .b = 0};
for (int dx = -1; dx <= 1; dx++)
for (int dy = -1; dy <= 1; dy++)
{
Pixel p = getPixelData(data, width, height, x+dx, y+dy);
if ( p.r + p.g + p.b > brightest.r + brightest.g + brightest.b)
brightest = p;
}
dataTmp[ (y * width + x) * 3 + 0] = brightest.r;
dataTmp[ (y * width + x) * 3 + 1] = brightest.g;
dataTmp[ (y * width + x) * 3 + 2] = brightest.b;
//int numNeighbors = 0;
//mark(dataTmp, width, height, x, y, 0, 0, 255);
}
}
memcpy(data, dataTmp, width*height*3);
free(dataTmp);
}
void subsampleAndConvertToPerceptive(Vector3* lights, uint8_t *dataOut, int width, int height)
{
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
{
float sum_r = 0;
float sum_g = 0;
float sum_b = 0;
float count = 0;
for (int dx = 0; dx < SUPER_SAMPLING; dx++)
for (int dy = 0; dy < SUPER_SAMPLING; dy++)
{
float r = lights[(y*SUPER_SAMPLING+dy)* (width * SUPER_SAMPLING) + (x*SUPER_SAMPLING+dx)].s[0];
float g = lights[(y*SUPER_SAMPLING+dy)* (width * SUPER_SAMPLING) + (x*SUPER_SAMPLING+dx)].s[1];
float b = lights[(y*SUPER_SAMPLING+dy)* (width * SUPER_SAMPLING) + (x*SUPER_SAMPLING+dx)].s[2];
if (r != 0.0f && g != 0.0f && b != 0.0f)
{
sum_r += r;
sum_g += g;
sum_b += b;
count +=1;
}
}
sum_r/= count;
sum_g/=count;
sum_b/=count;
convert2(&sum_r, &sum_g, &sum_b);
/* dataOut[(y * width + x) * 3 + 0] = clamp( convert(sum_r / count ) * 255);
dataOut[(y * width + x) * 3 + 1] = clamp( convert(sum_g / count ) * 255);
dataOut[(y * width + x) * 3 + 2] = clamp( convert(sum_b / count ) * 255);*/
dataOut[(y * width + x) * 3 + 0] = clamp( sum_r * 255);
dataOut[(y * width + x) * 3 + 1] = clamp( sum_g * 255);
dataOut[(y * width + x) * 3 + 2] = clamp( sum_b * 255);
}
}