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
|
#include "thumbnail.h"
#include <magick/api.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
/* http://www.graphicsmagick.org/api/api.html */
int thumbnail_create(const char *const src, const char *const dst)
{
int ret = -1;
const size_t slen = strlen(src), dlen = strlen(dst);
ImageInfo *info = NULL;
Image *i = NULL, *t = NULL;
ExceptionInfo exc;
InitializeMagick(NULL);
GetExceptionInfo(&exc);
if (slen >= sizeof i->filename)
{
fprintf(stderr, "%s: src maximum length exceeded (%zu, max %zu)\n",
__func__, slen, sizeof i->filename - 1);
goto end;
}
else if (dlen >= sizeof t->filename)
{
fprintf(stderr, "%s: dst maximum length exceeded (%zu, max %zu)\n",
__func__, slen, sizeof t->filename - 1);
goto end;
}
else if (!(info = CloneImageInfo(NULL)))
{
fprintf(stderr, "%s: CloneImageInfo failed\n", __func__);
goto end;
}
strcpy(info->filename, src);
info->adjoin = MagickTrue;
if (!(i = ReadImage(info, &exc)))
{
if (exc.severity == MissingDelegateError)
/* Non-image file format. */
ret = 1;
else
fprintf(stderr, "%s: ReadImage failed: "
"reason: %s, description: %s\n",
__func__, exc.reason, exc.description);
goto end;
}
const unsigned long y = i->rows > THUMBNAIL_HEIGHT ?
THUMBNAIL_HEIGHT : i->rows,
x = y * i->columns / i->rows;
if (!(t = ResizeImage(i, x, y, PointFilter, 1, &exc)))
{
fprintf(stderr, "%s: ResizeImage failed, "
"reason: %s, description: %s\n",
__func__, exc.reason, exc.description);
goto end;
}
else if (WriteImages(info, t, dst, &exc) != MagickPass)
{
fprintf(stderr, "%s: WriteImages failed, "
"reason: %s, description: %s\n",
__func__, exc.reason, exc.description);
goto end;
}
ret = 0;
end:
DestroyImageInfo(info);
DestroyImage(i);
DestroyImage(t);
DestroyExceptionInfo(&exc);
DestroyMagick();
return ret;
}
bool thumbnail_configured(void)
{
return true;
}
int thumbnail_dim(const char *const path, struct thumbnail_dim *const d)
{
int ret = -1;
const size_t slen = strlen(path);
ImageInfo *info = NULL;
Image *i = NULL;
ExceptionInfo exc;
InitializeMagick(NULL);
GetExceptionInfo(&exc);
if (slen >= sizeof i->filename)
{
fprintf(stderr, "%s: src maximum length exceeded (%zu, max %zu)\n",
__func__, slen, sizeof i->filename - 1);
goto end;
}
else if (!(info = CloneImageInfo(NULL)))
{
fprintf(stderr, "%s: CloneImageInfo failed\n", __func__);
goto end;
}
strcpy(info->filename, path);
info->adjoin = MagickTrue;
if (!(i = ReadImage(info, &exc)))
{
if (exc.severity == MissingDelegateError)
/* Non-image file format. */
ret = 1;
else
fprintf(stderr, "%s: ReadImage failed: "
"reason: %s, description: %s\n",
__func__, exc.reason, exc.description);
goto end;
}
*d = (const struct thumbnail_dim)
{
.w = i->columns,
.h = i->rows
};
ret = 0;
end:
DestroyImageInfo(info);
DestroyImage(i);
DestroyExceptionInfo(&exc);
DestroyMagick();
return ret;
}
|