-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocessing.c
More file actions
100 lines (85 loc) · 2.27 KB
/
processing.c
File metadata and controls
100 lines (85 loc) · 2.27 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
#include "processing.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "filter.h"
#include "image.h"
#include "threadpool.h"
typedef image_t* (*filter_fn)(image_t* img);
static const filter_fn filters[] = {
filter_scale_up2, //
filter_desaturate, //
filter_gaussian_blur, //
filter_edge_detect, //
NULL, //
};
// Fonction qui traite une image
void* process_one_image(void* arg) {
struct work_item* item = arg;
const char* fname = item->input_file;
printf("processing image: %s\n", fname);
image_t* img = image_create_from_png(fname);
if (!img) {
printf("failed to load image %s\n", fname);
goto err;
}
int i = 0;
while (filters[i]) {
filter_fn fn = filters[i];
image_t* next = fn(img);
if (!next) {
printf("failed to process image%s\n", fname);
image_destroy(img);
goto err;
}
image_destroy(img);
img = next;
i++;
}
image_save_png(img, item->output_file);
image_destroy(img);
return 0;
err:
return (void*)-1UL;
}
int process_serial(struct list* items) {
struct list_node* node = list_head(items);
while (!list_end(node)) {
unsigned long ret = (unsigned long)process_one_image(node->data);
if (ret < 0) {
return -1;
}
node = node->next;
}
return 0;
}
int process_multithread(struct list* items, int nb_thread) {
// Créer un pool de threads avec nb_thread threads
struct pool* pool = threadpool_create(nb_thread);
if (!pool) {
printf("Échec de la création du pool de threads\n");
return -1;
}
// Ajouter toutes les images à la file d'attente des tâches
struct list_node* node = list_head(items);
while (!list_end(node)) {
struct work_item* item = node->data;
threadpool_add_task(pool, process_one_image, item);
node = node->next;
}
// Attendre que le traitement soit terminé
threadpool_join(pool);
return 0;
}
struct work_item* make_work_item(const char* input_file, const char* output_dir) {
struct work_item* item = malloc(sizeof(struct work_item));
item->input_file = strdup(input_file);
item->output_file = strdup(output_dir);
return item;
}
void free_work_item(void* item) {
struct work_item* w = item;
free(w->input_file);
free(w->output_file);
free(item);
}