-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
227 lines (180 loc) · 6.85 KB
/
utils.c
File metadata and controls
227 lines (180 loc) · 6.85 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "utils.h"
img_t* parse_img(char* filename) {
img_t* image = (img_t*) malloc(sizeof(img_t));
FILE* fptr = fopen(filename, "r+");
unsigned int image_size;
int i, offset, pxl_size;
DIE(image == NULL, "Malloc failed!");
DIE(fptr == NULL, "Error opening input file");
// Skip "P"
fgetc(fptr);
// Read image type
fscanf(fptr, "%hhu\n", &(image->P));
// Manage pixel type
pxl_size = (image->P == 5) ? 1 : 3;
// Skip comment
fgets(image->comment, COMMENT_LEN, fptr);
// Read image width, height and pixel maxvalue
fscanf(fptr, "%d %d\n", &(image->width), &(image->height));
fscanf(fptr, "%hhu\n", &(image->maxval));
// Manage image data size including the size needed for padding
image_size = image->height * image->width + 2 * (image->width + image->height) + 4;
image_size *= pxl_size;
// Skip added border
offset = (image->width + 3) * pxl_size;
// Read image data
image->data = (unsigned char*) calloc(image_size, sizeof(unsigned char));
DIE(image->data == NULL, "Memory allocation failed!");
const int step = (image->width + 2) * pxl_size;
for (i = 0; i < image->height; i++, offset += step) {
fread(image->data + offset, sizeof(unsigned char), image->width * pxl_size, fptr);
}
fclose(fptr);
return image;
}
void flush_img(char* filename, img_t* image) {
FILE* fptr = fopen(filename, "w+");
int i, offset;
int pxl_size = (image->P == 5) ? 1 : 3;
DIE(fptr == NULL, "Error opening output file");
// Write image metadata
fprintf(fptr, "P%u\n", image->P);
fprintf(fptr, "%d %d\n%u\n", image->width, image->height, image->maxval);
// Write image data
offset = (image->width + 3) * pxl_size;
const int step = (image->width + 2) * pxl_size;
for (i = 0; i < image->height; i++, offset += step) {
fwrite(image->data + offset, sizeof(unsigned char), image->width * pxl_size, fptr);
}
fclose(fptr);
}
float* get_filter(char* filter) {
float* kernel = (float*) malloc(9 * sizeof(float));
DIE(kernel == NULL, "Malloc failed!");
int i;
if (!strcmp(filter, "smooth")) {
for (i = 0; i < 9; ++i) {
kernel[i] = 1.f / 9.f;
}
}
else if (!strcmp(filter, "blur")) {
for (i = 0; i < 9; ++i) {
if (i % 2 == 0) {
kernel[i] = 1.f / 16.f;
} else {
kernel[i] = 2.f / 16.f;
}
}
kernel[4] = 4.f / 16.f;
}
else if (!strcmp(filter, "sharpen")) {
for (i = 0; i < 9; ++i) {
float fact = (i % 2 == 0) ? 0.f : -2.f;
kernel[i] = (1.f / 3.f) * fact;
}
kernel[4] = 11.f / 3.f;
}
else if (!strcmp(filter, "mean")) {
for (i = 0; i < 9; ++i) {
kernel[i] = -1.f;
}
kernel[4] = 9.f;
}
else if (!strcmp(filter, "emboss")) {
for (i = 0; i < 9; ++i) {
kernel[i] = 0.f;
}
kernel[1] = -1.f;
kernel[7] = 1.f;
}
else {
DIE(1, "Received invalid filter!");
}
return kernel;
}
unsigned char* apply_filter_chunk(
unsigned char P, unsigned char maxvalue,
int rank, int nProcesses, char* filter_name,
int line_width, int buff_len,
unsigned char* img_data,
unsigned char* upper_line,
unsigned char* bottom_line) {
int pxl_size = (P == 5) ? 1 : 3;
int i, j, start, end;
unsigned char* final_data = (unsigned char*) calloc(buff_len, sizeof(unsigned char));
unsigned char* data;
int indices[9];
float* filter = get_filter(filter_name);
DIE(final_data == NULL, "Malloc failed!");
// Pad our image chunk with needing upper and / or bottom lines
if (rank == MASTER_PROC) {
// Skip the '0' padding at the beggining and manage ending point
start = line_width + pxl_size;
end = buff_len - pxl_size;
// If this is the only process, we won't have a bottom line and the ending point differs
if (nProcesses > 1) {
data = (unsigned char*) calloc(buff_len + line_width, sizeof(unsigned char));
DIE(data == NULL, "Malloc failed!");
// Add bottom missing line
memcpy(data + buff_len, bottom_line, line_width * sizeof(unsigned char));
} else {
data = (unsigned char*) calloc(buff_len, sizeof(unsigned char));
DIE(data == NULL, "Malloc failed!");
end -= line_width;
}
memcpy(data, img_data, buff_len * sizeof(unsigned char));
}
else if (rank == nProcesses - 1) {
// Skip the '0' padding at the end and manage starting point
start = pxl_size;
end = buff_len - line_width - pxl_size;
data = (unsigned char*) calloc(buff_len + line_width, sizeof(unsigned char));
DIE(data == NULL, "Malloc failed!");
// Add upper missing line
memcpy(data, upper_line, line_width * sizeof(unsigned char));
memcpy(data + line_width, img_data, buff_len * sizeof(unsigned char));
}
else {
// Manage starting and ending points
start = pxl_size;
end = buff_len - pxl_size;
data = (unsigned char*) calloc(buff_len + 2 * line_width, sizeof(unsigned char));
DIE(data == NULL, "Malloc failed!");
// Add both upper and bottom missing lines
memcpy(data, upper_line, line_width * sizeof(unsigned char));
memcpy(data + line_width, img_data, buff_len * sizeof(unsigned char));
memcpy(data + buff_len + line_width, bottom_line, line_width * sizeof(unsigned char));
}
// Manage indices offsets
// Add an additional line_width in for chunks that use an upper new line
int upper_offset = (rank == MASTER_PROC) ? 0 : line_width;
int dimension_switch = -line_width;
int offset[3] = { -pxl_size, 0, pxl_size };
// Build indices that define the square around any pixel pixel
for (i = 0; i < 9; ++i) {
// Switch lanes
if (!(i % 3) && (i)) {
dimension_switch += line_width;
}
indices[i] = dimension_switch + offset[i % 3] + upper_offset;
}
for (i = start; i < end; ++i) {
float tmp = 0.f;
// Escape '0' padding of the border
if (i % line_width < pxl_size || i % line_width >= line_width - pxl_size) {
continue;
}
// Compute filtered pixel using neighbours
for (j = 0; j < 9; ++j) {
tmp += (float) (data[i + indices[j]]) * filter[j];
}
final_data[i] = CLAMP(tmp, maxvalue);
}
free(data);
free(filter);
return final_data;
}
void destroy_img(img_t* image) {
free(image->data);
free(image);
}