-
Notifications
You must be signed in to change notification settings - Fork 1
/
imagemagick.c
82 lines (63 loc) · 1.66 KB
/
imagemagick.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
// gcc imagemagick.c -lm `pkg-config --cflags --libs MagickWand` -o magick
#include <stdio.h>
#include <assert.h>
#include <MagickWand/MagickWand.h>
void ThrowWandException(MagickWand* wand)
{
char* description;
ExceptionType severity;
description = MagickGetException(wand,&severity);
(void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description);
description=(char *) MagickRelinquishMemory(description);
}
int main(int argc, char **argv)
{
MagickWand* m_wand = NULL;
MagickBooleanType status;
int width, height;
if (argc < 2) {
printf("usage: ./magick image\n");
return 0;
}
MagickWandGenesis();
m_wand = NewMagickWand();
assert(m_wand != NULL);
status = MagickReadImage(m_wand, argv[1]);
if (status == MagickFalse) {
ThrowWandException(m_wand);
return 1;
}
// Get the image's width and height
width = MagickGetImageWidth(m_wand);
height = MagickGetImageHeight(m_wand);
printf("Image size %dx%d\n", width, height);
// Resize
width = 256;
height = 256;
status = MagickResizeImage(m_wand, width, height, LanczosFilter);
if (status == MagickFalse) {
ThrowWandException(m_wand);
return 1;
}
// Remove color profiles for interoperability with other hashing tools
status = MagickProfileImage(m_wand, "*", NULL, 0);
if (status == MagickFalse) {
ThrowWandException(m_wand);
return 1;
}
// Grayscale
status = MagickTransformImageColorspace(m_wand, GRAYColorspace);
if (status == MagickFalse) {
ThrowWandException(m_wand);
return 1;
}
// Save to file
MagickWriteImage(m_wand, "magick_result.jpg");
// Clean up
if(m_wand) {
m_wand = DestroyMagickWand(m_wand);
m_wand = NULL;
}
MagickWandTerminus();
return 0;
}