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
|
// Calvin Morrison Copyright 2012
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <libgen.h>
#include <wand/MagickWand.h>
#include <wand/composite.h>
#include <wand/convert.h>
#include "thpool.h"
#define ThrowWandException(wand) { char *description; ExceptionType severity; description=MagickGetException(wand,&severity); (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); description=(char *) MagickRelinquishMemory(description); exit(-1); }
MagickWand *background;
char *output_folder = NULL;
void convert_image(char *file) {
MagickWand *mask = NewMagickWand();
char *output_name = malloc(256);
if(MagickReadImage(mask, file) == MagickFalse) {
ThrowWandException(mask);
return;
}
// convert \\\( -composite -compose Difference $output_dir/Masks/$setname/Background.png {} \\\) \\\( -contrast-stretch 90%x0% \\\) \\\( -threshold 30% \\\) $output_dir/Masks/$setname/Masks/{/}
MagickCompositeImage(mask, background, DifferenceCompositeOp, 0, 0);
// MagickContrastStretchImage(mask, 90000, 0);
MagickAutoLevelImage(mask);
MagickThresholdImage(mask, 30000);
sprintf(output_name, "%s%s", output_folder, basename(file));
if(MagickWriteImages(mask, output_name, MagickTrue) == MagickFalse) {
ThrowWandException(mask);
}
mask = DestroyMagickWand(mask);
printf("output written to: %s \n", output_name);
free(output_name);
free(file);
}
int main( int argc, char **argv){
char *usage = "Usage: generate-mask -b <Background filename> -i <image list > -o <outputFolderName>";
char *background_file = NULL;
char *image_list = NULL;
int c;
opterr = 0;
while ((c = getopt (argc, argv, "b:i:o:hv")) != -1)
switch (c) {
case 'b':
background_file = optarg;
break;
case 'i':
image_list = optarg;
break;
case 'o':
output_folder = optarg;
break;
case 'v':
break;
case 'h':
puts(usage);
exit(1);
break;
default:
break;
}
if( background_file == NULL || image_list == NULL || output_folder == NULL ) {
puts(usage);
exit(1);
}
MagickBooleanType status;
MagickWandGenesis();
background = NewMagickWand();
status=MagickReadImage(background, background_file);
if (status == MagickFalse) {
puts("background could not load error");
exit(0);
}
thpool_t* threadpool; /* make a new thread pool structure */
threadpool=thpool_init(4); /* initialise it to 4 number of threads */
char filename[256];
char *temp;
FILE *f = fopen ( image_list, "r" );
if ( f != NULL ) {
while ( fgets ( filename, sizeof(filename), f ) != NULL ) {
temp = strchr(filename, '\n');
if (temp != NULL) *temp = '\0';
char *filename_r = malloc(256);
strncpy(filename_r, filename, sizeof(filename));
printf("add work: %s \n", filename);
thpool_add_work(threadpool, (void*)convert_image, (void*)filename_r);
}
fclose ( f );
}
else {
exit(0);
}
puts("Will kill threadpool");
thpool_destroy(threadpool);
MagickWandTerminus();
return 0;
}
|