-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
274 lines (233 loc) · 9.05 KB
/
main.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <memory.h>
#include <jpeglib.h>
#include <math.h>
#include <pthread.h>
#include <sys/time.h>
#include "config.h"
const uint8_t INPUT_IMAGE_COMPONENTS_NUMBER = 3u;
const uint8_t KERNEL_WIDTH = KERNEL_RADIUS * 2 + 1;
const uint8_t KERNEL_HEIGHT = KERNEL_WIDTH;
void set_decompressor_options(
struct jpeg_decompress_struct *decompressor,
struct jpeg_error_mgr *error_manager,
FILE *input_file
) {
decompressor->err = jpeg_std_error(error_manager);
jpeg_create_decompress(decompressor);
jpeg_stdio_src(decompressor, input_file);
jpeg_read_header(decompressor, TRUE);
jpeg_start_decompress(decompressor);
}
void set_compressor_options(
struct jpeg_compress_struct *compressor,
const struct jpeg_decompress_struct *decompressor,
struct jpeg_error_mgr *error_manager,
FILE *output_file
) {
compressor->err = jpeg_std_error(error_manager);
jpeg_create_compress(compressor);
jpeg_stdio_dest(compressor, output_file);
compressor->in_color_space = JCS_RGB;
compressor->jpeg_color_space = JCS_RGB;
compressor->input_components = decompressor->num_components;
compressor->num_components = decompressor->num_components;
jpeg_set_defaults(compressor);
compressor->image_width = decompressor->output_width;
compressor->image_height = decompressor->image_height;
compressor->density_unit = decompressor->density_unit;
compressor->X_density = decompressor->X_density;
compressor->Y_density = decompressor->Y_density;
jpeg_start_compress(compressor, TRUE);
}
struct pixel_components {
double red;
double green;
double blue;
};
struct kernel_wrapper {
double kernel[KERNEL_HEIGHT][KERNEL_WIDTH];
};
double gaussian(const double x, const double mu, const double sigma) {
/**
* It is ugly, I know!
* But it's math, and math is ugly :D
* The formula comes from here: https://en.wikipedia.org/wiki/Gaussian_function.
*/
return exp(-(pow((x - mu) / sigma, 2) / 2.f));
}
struct kernel_wrapper produce_gaussian_kernel(void) {
const double sigma = KERNEL_RADIUS / 2.f;
struct kernel_wrapper output;
double kernel_row[KERNEL_WIDTH];
for (size_t i = 0; i < KERNEL_WIDTH; i++) {
kernel_row[i] = gaussian(i, KERNEL_RADIUS, sigma);
}
for (size_t i = 0; i < KERNEL_HEIGHT; i++) {
for (size_t j = 0; j < KERNEL_WIDTH; j++) {
const double v = kernel_row[i] * kernel_row[j];
output.kernel[i][j] = v;
}
}
return output;
}
struct kernel_wrapper produce_mean_kernel(void) {
struct kernel_wrapper output;
for (size_t i = 0; i < KERNEL_HEIGHT; i++)
for (size_t j = 0; j < KERNEL_WIDTH; j++) {
output.kernel[i][j] = 1.f;
}
return output;
}
struct transform_row_params {
unsigned short IMAGE_WIDTH;
unsigned short IMAGE_HEIGHT;
JSAMPARRAY input_image;
JSAMPARRAY output_image;
double kernel[KERNEL_HEIGHT][KERNEL_WIDTH];
unsigned short start_row;
unsigned short num_rows;
};
void copy_kernel(double destination[KERNEL_HEIGHT][KERNEL_WIDTH], const double source[KERNEL_HEIGHT][KERNEL_WIDTH]) {
for (size_t i = 0; i < KERNEL_HEIGHT; i++) {
for (size_t j = 0; j < KERNEL_WIDTH; j++) {
destination[i][j] = source[i][j];
}
};
}
void *transform_rows(void *serialized_params) {
struct transform_row_params *params = (struct transform_row_params *)serialized_params;
for (size_t i = params->start_row; i < params->start_row + params->num_rows; i++) {
for (size_t j = 0; j < params->IMAGE_WIDTH; j++) {
struct pixel_components components_multiplication_sum = {
.red = 0.f,
.green = 0.f,
.blue = 0.f,
};
double kernel_cells_sum = 0.f;
const size_t ki_start = i > KERNEL_RADIUS ? 0 : KERNEL_RADIUS - i;
const size_t ki_end = i > params->IMAGE_HEIGHT - KERNEL_RADIUS ? params->IMAGE_HEIGHT - i + KERNEL_RADIUS : KERNEL_HEIGHT;
const size_t kj_start = j > KERNEL_RADIUS ? 0 : KERNEL_RADIUS - j;
const size_t kj_end = j > params->IMAGE_WIDTH - KERNEL_RADIUS ? params->IMAGE_WIDTH - j + KERNEL_RADIUS : KERNEL_WIDTH;
for (size_t ki = ki_start; ki < ki_end; ki++) {
for (size_t kj = kj_start; kj < kj_end; kj++) {
kernel_cells_sum += params->kernel[ki][kj];
const uint16_t red = params->input_image[i + ki - KERNEL_RADIUS][(j + kj - KERNEL_RADIUS) * INPUT_IMAGE_COMPONENTS_NUMBER + 0];
const uint16_t green = params->input_image[i + ki - KERNEL_RADIUS][(j + kj - KERNEL_RADIUS) * INPUT_IMAGE_COMPONENTS_NUMBER + 1];
const uint16_t blue = params->input_image[i + ki - KERNEL_RADIUS][(j + kj - KERNEL_RADIUS) * INPUT_IMAGE_COMPONENTS_NUMBER + 2];
components_multiplication_sum.red += red * params->kernel[ki][kj];
components_multiplication_sum.green += green * params->kernel[ki][kj];
components_multiplication_sum.blue += blue * params->kernel[ki][kj];
}
}
params->output_image[i][INPUT_IMAGE_COMPONENTS_NUMBER * j + 0] = round(components_multiplication_sum.red / kernel_cells_sum);
params->output_image[i][INPUT_IMAGE_COMPONENTS_NUMBER * j + 1] = round(components_multiplication_sum.green / kernel_cells_sum);
params->output_image[i][INPUT_IMAGE_COMPONENTS_NUMBER * j + 2] = round(components_multiplication_sum.blue / kernel_cells_sum);
}
}
return NULL;
}
int transform(
const char *input_filename,
const char *output_filename,
const double kernel[KERNEL_HEIGHT][KERNEL_WIDTH]
) {
FILE *input_file = fopen(input_filename, "rb");
if (!input_file) {
(void)fprintf(
stderr,
"🛑🙁 error opening input jpeg file '%s': %s 🙁🛑\n",
input_filename,
strerror(errno)
);
return errno;
}
FILE *output_file = fopen(output_filename, "wb");
if (!output_file) {
(void)fprintf(
stderr,
"🛑🙁 error opening output jpeg file '%s': %s 🙁🛑\n",
output_filename,
strerror(errno)
);
return errno;
}
struct jpeg_error_mgr error_manager;
struct jpeg_decompress_struct decompressor;
set_decompressor_options(&decompressor, &error_manager, input_file);
const unsigned short IMAGE_WIDTH = decompressor.image_width;
const unsigned short IMAGE_HEIGHT = decompressor.image_height;
struct jpeg_compress_struct compressor;
set_compressor_options(&compressor, &decompressor, &error_manager, output_file);
const unsigned long IMAGE_SIZE_IN_BYTES = IMAGE_HEIGHT * IMAGE_WIDTH * INPUT_IMAGE_COMPONENTS_NUMBER;
unsigned char *buffer = malloc(2 * IMAGE_SIZE_IN_BYTES + NUM_THREADS * sizeof(struct transform_row_params));
JSAMPROW write_buffer[IMAGE_HEIGHT];
for (size_t i = 0; i < IMAGE_HEIGHT; i++) {
write_buffer[i] = &buffer[i * IMAGE_WIDTH * INPUT_IMAGE_COMPONENTS_NUMBER];
}
JSAMPROW read_buffer[IMAGE_HEIGHT];
for (size_t i = 0; i < IMAGE_HEIGHT; i++) {
read_buffer[i] = &buffer[i * IMAGE_WIDTH * INPUT_IMAGE_COMPONENTS_NUMBER + IMAGE_SIZE_IN_BYTES];
}
while (decompressor.output_scanline < decompressor.output_height) {
(void)jpeg_read_scanlines(
&decompressor,
&read_buffer[decompressor.output_scanline],
decompressor.output_height - decompressor.output_scanline
);
}
pthread_t thread_ids[NUM_THREADS];
struct transform_row_params *thread_params_refs[NUM_THREADS];
const unsigned int quotient = decompressor.image_height / NUM_THREADS;
const unsigned int remainder = decompressor.image_height % NUM_THREADS;
unsigned long total_assigned_rows = 0U;
for (size_t i = 0; i < NUM_THREADS; i++) {
const unsigned long int worker_quotient = (i < remainder) ? (quotient + 1) : (quotient);
struct transform_row_params *params = (struct transform_row_params *)&buffer[2 * IMAGE_SIZE_IN_BYTES + i * sizeof(struct transform_row_params)];
params->input_image = read_buffer;
params->output_image = write_buffer;
copy_kernel(params->kernel, kernel);
params->IMAGE_HEIGHT = IMAGE_HEIGHT;
params->IMAGE_WIDTH = IMAGE_WIDTH;
params->num_rows = worker_quotient;
params->start_row = total_assigned_rows;
thread_params_refs[i] = params;
total_assigned_rows += worker_quotient;
}
struct timespec start, end;
timespec_get(&start, TIME_UTC);
for (size_t i = 0; i < NUM_THREADS; i++) {
(void)pthread_create(&thread_ids[i], NULL, transform_rows, thread_params_refs[i]);
}
for (size_t i = 0; i < NUM_THREADS; i++) {
(void)pthread_join(thread_ids[i], NULL);
}
timespec_get(&end, TIME_UTC);
unsigned long int time_in_nano_seconds = (end.tv_sec - start.tv_sec) * 1e9 + (end.tv_nsec - start.tv_nsec);
printf("total:%lu", time_in_nano_seconds);
while (compressor.next_scanline < compressor.image_height) {
(void)jpeg_write_scanlines(
&compressor,
&write_buffer[compressor.next_scanline],
compressor.image_height - compressor.next_scanline
);
}
(void)jpeg_finish_decompress(&decompressor);
jpeg_finish_compress(&compressor);
jpeg_destroy_decompress(&decompressor);
jpeg_destroy_compress(&compressor);
free(buffer);
fclose(input_file);
fclose(output_file);
return 0;
}
int main() {
return transform(
INPUT_IMAGE_FILENAME,
OUTPUT_IMAGE_FILENAME,
produce_gaussian_kernel().kernel
);
}