forked from bakape/thumbnailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thumbnailer.c
188 lines (167 loc) · 5.08 KB
/
thumbnailer.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
#include "thumbnailer.h"
#include <magick/pixel_cache.h>
#include <stdio.h>
#include <string.h>
int thumbnail(struct Buffer* src, struct Thumbnail* thumb,
const struct Options opts, ExceptionInfo* ex)
{
ImageInfo* info = NULL;
Image *img = NULL, *sampled = NULL, *scaled = NULL, *oriented = NULL;
double scale;
int err = 0;
// Read image
info = CloneImageInfo(NULL);
GetExceptionInfo(ex);
// Read only the first frame/page of GIFs and PDFs
info->subimage = 0;
info->subrange = 1;
// If width and height are already defined, then a frame from ffmpeg has
// been passed
if (src->width && src->height) {
strcpy(info->magick, "RGBA");
char* buf = malloc(128);
int over = snprintf(buf, 128, "%lux%lu", src->width, src->height);
if (over > 0) {
buf = realloc(buf, 128 + (size_t)over);
sprintf(buf, "%lux%lu", src->width, src->height);
}
info->size = buf;
info->depth = 8;
}
img = BlobToImage(info, src->data, src->size, ex);
if (!img) {
goto end;
}
src->width = img->columns;
src->height = img->rows;
// Validate dimensions
if (strcmp(img->magick, "PDF")) {
const unsigned long maxW = opts.maxSrcDims.width;
const unsigned long maxH = opts.maxSrcDims.height;
if (maxW && img->columns > maxW) {
err = 2;
goto end;
}
if (maxH && img->rows > maxH) {
err = 3;
goto end;
}
}
// Rotate image based on EXIF metadata, if needed
if (img->orientation > TopLeftOrientation) {
oriented = AutoOrientImage(img, img->orientation, ex);
if (!oriented) {
goto end;
}
DestroyImage(img);
img = oriented;
oriented = NULL;
}
// Strip EXIF metadata, if any. Fail silently.
DeleteImageProfile(img, "EXIF");
const unsigned long thumbW = opts.thumbDims.width;
const unsigned long thumbH = opts.thumbDims.height;
// Image already fits thumbnail
if (img->columns <= thumbW && img->rows <= thumbH) {
thumb->img.width = img->columns;
thumb->img.height = img->rows;
err = writeThumb(img, thumb, opts, ex);
goto end;
}
// Maintain aspect ratio
if (img->columns >= img->rows) {
scale = (double)(img->columns) / (double)(thumbW);
} else {
scale = (double)(img->rows) / (double)(thumbH);
}
thumb->img.width = (unsigned long)((double)img->columns / scale);
thumb->img.height = (unsigned long)((double)img->rows / scale);
// Subsample to 4 times the thumbnail size. A decent enough compromise
// between quality and performance for images around the thumbnail size
// and much bigger ones.
sampled = SampleImage(img, thumb->img.width * 4, thumb->img.height * 4, ex);
if (!sampled) {
goto end;
}
// Scale to thumbnail size
scaled = ResizeImage(
sampled, thumb->img.width, thumb->img.height, BoxFilter, 1, ex);
if (!scaled) {
goto end;
}
err = writeThumb(scaled, thumb, opts, ex);
end:
if (img) {
DestroyImage(img);
}
if (info) {
DestroyImageInfo(info);
}
if (sampled) {
DestroyImage(sampled);
}
if (scaled) {
DestroyImage(scaled);
}
if (oriented) {
DestroyImage(oriented);
}
if (!err) {
return thumb->img.data == NULL;
}
return err;
}
// Convert thumbnail to appropriate file type and write to buffer
static int writeThumb(Image* img, struct Thumbnail* thumb,
const struct Options opts, ExceptionInfo* ex)
{
ImageInfo* info = CloneImageInfo(NULL);
char* format = NULL;
bool needPNG = false;
if (strcmp(img->magick, "JPEG")) {
int err = hasTransparency(img, &needPNG, ex);
if (err) {
DestroyImageInfo(info);
return err;
}
}
if (needPNG) {
format = "PNG";
info->quality = 105;
thumb->isPNG = true;
} else {
format = "JPEG";
info->quality = opts.JPEGCompression;
}
strcpy(info->magick, format);
strcpy(img->magick, format);
thumb->img.data = ImageToBlob(info, img, &thumb->img.size, ex);
DestroyImageInfo(info);
return 0;
}
// Iterates over all pixels and checks, if any transparency present
static int hasTransparency(
const Image* const img, bool* needPNG, ExceptionInfo* ex)
{
// No alpha channel
if (!img->matte) {
return 0;
}
// Transparent pixels are most likely to also be in the first row, so
// retrieve one row at a time. It is also more performant to retrieve entire
// rows instead of individual pixels.
for (unsigned long i = 0; i < img->rows; i++) {
const PixelPacket* packets
= AcquireImagePixels(img, 0, i, img->columns, 1, ex);
if (!packets) {
return 1;
}
for (unsigned long j = 0; j < img->columns; j++) {
if (packets[j].opacity > 0) {
*needPNG = true;
return 0;
}
}
}
return 0;
}