-
Notifications
You must be signed in to change notification settings - Fork 0
/
brightness.c
143 lines (113 loc) · 3.65 KB
/
brightness.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
#include "brightness.h"
void adjust_brightness(gsl_matrix *m, pixel_point *tp) {
int row = tp->row;
int col = tp->col;
if (gsl_matrix_get(m, row, col) == Light) {
if (!in_lamp_range(tp)) {
gsl_matrix_set(m, row, col, Shadow);
} else {
gsl_matrix_set(m, row, col, 1 - (distance(tp) / MAX_LAMP_RANGE));
}
}
}
void remove_single_light_points(gsl_matrix *m) {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (gsl_matrix_get(m, row, col) == Light) {
int is_single = TRUE;
pixel_point tp = {row, col};
pixel_vector cv = get_circle_vector(&tp, 1);
for (int i = 0; i < cv.size; i++) {
if (gsl_matrix_get(m, cv.data[i]->row, cv.data[i]->col) == Light) {
is_single = FALSE;
break;
}
}
if (is_single) {
gsl_matrix_set(m, row, col, Shadow);
}
adjust_brightness(m, &tp);
free(cv.data);
}
}
}
}
gsl_matrix *get_smooth_shadow_matrix(gsl_matrix *m) {
gsl_matrix *m2 = gsl_matrix_alloc(ROWS, COLS);
gsl_matrix_memcpy(m2, m);
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (gsl_matrix_get(m, row, col) == Shadow) {
pixel_point current_point = {row, col};
pixel_vector shadow_cricle = get_circle_vector(¤t_point, 1);
int light_pixels = 0;
int all_pixels = 0;
for (int i = 0; i < shadow_cricle.size; i++) {
if (gsl_matrix_get(m, shadow_cricle.data[i]->row,
shadow_cricle.data[i]->col) != SeenBlock) {
all_pixels++;
}
if (gsl_matrix_get(m, shadow_cricle.data[i]->row,
shadow_cricle.data[i]->col) == Light) {
light_pixels++;
}
free(shadow_cricle.data[i]);
}
free(shadow_cricle.data);
gsl_matrix_set(m2, row, col, (double)light_pixels / all_pixels);
}
}
}
return m2;
}
void apply_brightness_logic(gsl_matrix *m, pixel_vector *lv,
pixel_type base_point_value) {
for (int k = 0; k < lv->size; k++) {
int row = lv->data[k]->row;
int col = lv->data[k]->col;
pixel_type current_value = gsl_matrix_get(m, row, col);
if (k == 0) {
if (base_point_value == Empty) {
gsl_matrix_set(m, lv->data[0]->row, lv->data[0]->col, Light);
}
if (base_point_value == Block) {
gsl_matrix_set(m, lv->data[0]->row, lv->data[0]->col, SeenBlock);
}
continue;
}
if (current_value == Block) {
gsl_matrix_set(m, row, col, SeenBlock);
continue;
}
int pre_row = lv->data[k - 1]->row;
int pre_col = lv->data[k - 1]->col;
free(lv->data[k - 1]);
pixel_type previous_value = gsl_matrix_get(m, pre_row, pre_col);
if (current_value == Empty) {
if (previous_value == Light) {
gsl_matrix_set(m, row, col, Light);
} else {
gsl_matrix_set(m, row, col, Shadow);
}
}
}
if (lv->size >= 1)
free(lv->data[lv->size - 1]);
}
void set_brightness(gsl_matrix *m) {
int longest_r = get_longest_radius();
for (int r = 1; r <= longest_r; r++) {
pixel_point light_point = {lamp_row, lamp_col};
pixel_vector cv = get_circle_vector(&light_point, r);
for (int i = 0; i < cv.size; i++) {
pixel_vector lv = get_line_vector(cv.data[i]);
pixel_type base_point_value =
gsl_matrix_get(m, cv.data[i]->row, cv.data[i]->col);
free(cv.data[i]);
if (base_point_value == Empty)
apply_brightness_logic(m, &lv, base_point_value);
free(lv.data);
}
free(cv.data);
}
}