/
mms_attachment_image.c
331 lines (307 loc) · 11.1 KB
/
mms_attachment_image.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Copyright (C) 2013-2014 Jolla Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include "mms_attachment_image.h"
#include "mms_file_util.h"
#ifdef MMS_RESIZE_IMAGEMAGICK
# include <magick/api.h>
#endif
/* Logging */
#define MMS_LOG_MODULE_NAME mms_attachment_log
#include "mms_lib_log.h"
G_DEFINE_TYPE(MMSAttachmentImage, mms_attachment_image, MMS_TYPE_ATTACHMENT);
#define MMS_ATTACHMENT_IMAGE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \
MMS_TYPE_ATTACHMENT_IMAGE, MMSAttachmentImage))
#define MMS_ATTACHMENT_IMAGE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), \
MMS_TYPE_ATTACHMENT_IMAGE, MMSAttachmentImageClass))
int
mms_attachment_image_next_resize_step(
MMSAttachmentImage* image,
unsigned int columns,
unsigned int rows)
{
int next_step = image->resize_step + 1;
if (image->attachment.config->max_pixels > 0) {
unsigned int size = (columns/(next_step+1))*(rows/(next_step+1));
while (size > 0 && size > image->attachment.config->max_pixels) {
next_step++;
size = (columns/(next_step+1))*(rows/(next_step+1));
}
}
return next_step;
}
const char*
mms_attachment_image_prepare_filename(
MMSAttachmentImage* image)
{
if (image->resized) {
remove(image->resized);
image->attachment.file_name = image->attachment.original_file;
} else {
char* dir = g_path_get_dirname(image->attachment.original_file);
char* subdir = g_strconcat(dir, "/" MMS_RESIZE_DIR, NULL);
g_mkdir_with_parents(subdir, MMS_DIR_PERM);
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
image->resized = g_strconcat(subdir, "/",
g_basename(image->attachment.original_file), NULL);
G_GNUC_END_IGNORE_DEPRECATIONS;
g_free(dir);
g_free(subdir);
}
return image->resized;
}
static
gboolean
mms_attachment_image_resize_default(
MMSAttachmentImage* image)
{
gboolean ok = FALSE;
#ifdef MMS_RESIZE_IMAGEMAGICK
ExceptionInfo ex;
Image* src;
ImageInfo* info = CloneImageInfo(NULL);
const char* fname = mms_attachment_image_prepare_filename(image);
GetExceptionInfo(&ex);
strncpy(info->filename, image->attachment.original_file,
G_N_ELEMENTS(info->filename));
info->filename[G_N_ELEMENTS(info->filename)-1] = 0;
src = ReadImage(info, &ex);
if (src) {
if (src->magick_columns > 1 && src->magick_rows > 1) {;
const unsigned int src_cols = src->magick_columns;
const unsigned int src_rows = src->magick_rows;
const int next_step = mms_attachment_image_next_resize_step(image,
src_cols, src_rows);
const unsigned int cols = src_cols/(next_step+1);
const unsigned int rows = src_rows/(next_step+1);
Image* dest;
MMS_DEBUG("Resizing (%ux%u -> %ux%u) with ImageMagick",
src_cols, src_rows, cols, rows);
dest = ResizeImage(src, cols, rows, BoxFilter, 1.0, &ex);
if (dest) {
image->resize_step = next_step;
strncpy(info->filename, fname, G_N_ELEMENTS(info->filename));
strncpy(dest->filename, fname, G_N_ELEMENTS(dest->filename));
info->filename[G_N_ELEMENTS(info->filename)-1] = 0;
dest->filename[G_N_ELEMENTS(dest->filename)-1] = 0;
if (WriteImage(info, dest)) {
MMS_DEBUG("Resized %s with ImageMagick", fname);
ok = TRUE;
} else {
MMS_ERR("Failed to write %s", dest->filename);
}
DestroyImage(dest);
}
}
DestroyImage(src);
} else {
MMS_ERR("Failed to read %s", info->filename);
}
ClearMagickException(&ex);
DestroyExceptionInfo(&ex);
DestroyImageInfo(info);
#else
# ifdef MMS_RESIZE_QT
ok = mms_attachment_image_resize_qt(image);
# endif /* MMS_RESIZE_QT */
#endif /* MMS_RESIZE_IMAGEMAGICK */
return ok;
}
static
gboolean
mms_attachment_image_resize_type_specific(
MMSAttachmentImage* image)
{
/* If klass->fn_resize_new is not NULL, then we assume that all
* other callbacks are present as well */
gboolean ok = FALSE;
MMSAttachment* at = &image->attachment;
MMSAttachmentImageClass* klass = MMS_ATTACHMENT_IMAGE_GET_CLASS(image);
MMSAttachmentImageResize* resize;
if (klass->fn_resize_new && (resize =
klass->fn_resize_new(image, at->original_file)) != NULL) {
gboolean can_resize;
const char* fname = mms_attachment_image_prepare_filename(image);
const int next_step = mms_attachment_image_next_resize_step(image,
resize->image.width, resize->image.height);
MMSAttachmentImageSize image_size;
MMSAttachmentImageSize out_size;
image_size = resize->image;
out_size.width = image_size.width/(next_step+1);
out_size.height = image_size.height/(next_step+1);
resize->in = resize->out = out_size;
can_resize = klass->fn_resize_prepare(resize, fname);
if (!can_resize) {
klass->fn_resize_free(resize);
resize = klass->fn_resize_new(image, at->original_file);
if (!resize) return FALSE;
MMS_ASSERT(resize->image.width == image_size.width);
MMS_ASSERT(resize->image.height == image_size.height);
resize->in = image_size;
resize->out = out_size;
can_resize = klass->fn_resize_prepare(resize, fname);
}
if (can_resize) {
unsigned char* line = g_malloc(3*resize->in.width);
guint y;
if (resize->in.width == resize->out.width &&
resize->in.height == resize->out.height) {
/* Nothing to resize, image decompressor is doing all
* the job for us */
MMS_DEBUG("Decoder-assisted resize (%ux%u -> %ux%u)",
image_size.width, image_size.height,
out_size.width, out_size.height);
for (y=0;
y<resize->in.height &&
klass->fn_resize_read_line(resize, line) &&
klass->fn_resize_write_line(resize, line);
y++);
} else {
const guint nx = (resize->in.width/resize->out.width);
const guint ny = (resize->in.height/resize->out.height);
gsize bufsize = 3*resize->out.width*sizeof(guint);
guint* buf = g_malloc(bufsize);
memset(buf, 0, bufsize);
MMS_DEBUG("Resizing (%ux%u -> %ux%u)",
image_size.width, image_size.height,
out_size.width, out_size.height);
for (y=0;
y<resize->in.height &&
klass->fn_resize_read_line(resize, line);
y++) {
/* Update the resize buffer */
guint x;
guint* bufptr = buf;
const unsigned char* lineptr = line;
for (x=0; x<resize->out.width; x++) {
guint k;
for (k=0; k<nx; k++) {
bufptr[0] += (*lineptr++);
bufptr[1] += (*lineptr++);
bufptr[2] += (*lineptr++);
}
bufptr += 3;
}
if ((y % ny) == (ny-1)) {
/* Average the pixels */
unsigned char* outptr = line;
const guint denominator = nx*ny;
bufptr = buf;
for (x=0; x<resize->out.width; x++) {
(*outptr++) = (*bufptr++)/denominator;
(*outptr++) = (*bufptr++)/denominator;
(*outptr++) = (*bufptr++)/denominator;
}
/* And write the next line */
if (klass->fn_resize_write_line(resize, line)) {
memset(buf, 0, bufsize);
} else {
break;
}
}
}
g_free(buf);
}
if (klass->fn_resize_finish) {
klass->fn_resize_finish(resize);
}
if (y == resize->in.height) {
MMS_DEBUG("Resized %s", fname);
image->resize_step = next_step;
ok = TRUE;
}
g_free(line);
}
klass->fn_resize_free(resize);
}
return ok;
}
static
gboolean
mms_attachment_image_resize(
MMSAttachment* at)
{
MMSAttachmentImage* image = MMS_ATTACHMENT_IMAGE(at);
gboolean ok;
if (at->map && image->resized) {
g_mapped_file_unref(at->map);
at->map = NULL;
}
ok = mms_attachment_image_resize_type_specific(image);
if (!ok) ok = mms_attachment_image_resize_default(image);
if (ok) {
GError* error = NULL;
GMappedFile* map = g_mapped_file_new(image->resized, FALSE, &error);
if (map) {
if (at->map) g_mapped_file_unref(at->map);
at->file_name = image->resized;
at->map = map;
} else {
MMS_ERR("%s", MMS_ERRMSG(error));
g_error_free(error);
ok = FALSE;
}
}
return ok;
}
static
void
mms_attachment_image_reset(
MMSAttachment* at)
{
MMSAttachmentImage* image = MMS_ATTACHMENT_IMAGE(at);
at->file_name = at->original_file;
if (image->resize_step) {
image->resize_step = 0;
if (at->map) g_mapped_file_unref(at->map);
at->map = g_mapped_file_new(at->original_file, FALSE, NULL);
}
}
static
void
mms_attachment_image_finalize(
GObject* object)
{
MMSAttachmentImage* image = MMS_ATTACHMENT_IMAGE(object);
if (!image->attachment.config->keep_temp_files &&
!(image->attachment.flags & MMS_ATTACHMENT_KEEP_FILES)) {
mms_remove_file_and_dir(image->resized);
}
g_free(image->resized);
G_OBJECT_CLASS(mms_attachment_image_parent_class)->finalize(object);
}
static
void
mms_attachment_image_class_init(
MMSAttachmentImageClass* klass)
{
klass->attachment.fn_reset = mms_attachment_image_reset;
klass->attachment.fn_resize = mms_attachment_image_resize;
G_OBJECT_CLASS(klass)->finalize = mms_attachment_image_finalize;
}
static
void
mms_attachment_image_init(
MMSAttachmentImage* image)
{
#if defined(MMS_RESIZE_IMAGEMAGICK) || defined(MMS_RESIZE_QT)
image->attachment.flags |= MMS_ATTACHMENT_RESIZABLE;
#endif
}
/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/