]> git.street.me.uk Git - andy/viking.git/blame - src/thumbnails.c
Fix cycle map URL
[andy/viking.git] / src / thumbnails.c
CommitLineData
50a14534
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22/*
23 * Large (and important) sections of this file were adapted from
24 * ROX-Filer source code, Copyright (C) 2003, the ROX-Filer team,
25 * originally licensed under the GPL v2 or greater (as above).
26 *
27 */
28
45acf79e
MA
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
50a14534 33#include <stdlib.h>
45acf79e
MA
34#ifdef HAVE_UNISTD_H
35#include <unistd.h>
36#endif
50a14534
EB
37#include <errno.h>
38#include <string.h>
f83131b9
MA
39#include <glib.h>
40#include <glib/gstdio.h>
50a14534
EB
41#include "viking.h"
42#include "thumbnails.h"
5bfafde9 43#include "icons/icons.h"
50a14534
EB
44
45#ifdef __CYGWIN__
46#ifdef __CYGWIN_USE_BIG_TYPES__
47#define ST_SIZE_FMT "%lld"
48#else
49#define ST_SIZE_FMT "%ld"
50#endif
51#else
52/* FIXME -- on some systems this may need to me "lld", see ROX-Filer code */
53#define ST_SIZE_FMT "%ld"
54#endif
55
56#undef MIN /* quit yer whining, gcc */
57#undef MAX
58#include <sys/param.h> /* for realpath() */
90e25247
GB
59#ifndef MAX
60/* We need MAX macro and some system does not offer it */
61#define MAX(a,b) (((a)>(b))?(a):(b))
62#endif
50a14534 63
118b901b
MA
64#define HOME_DIR g_get_home_dir()
65
50a14534 66#ifdef WINDOWS
50a14534
EB
67#define THUMB_DIR "\\THUMBNAILS\\" /* viking maps default viking\maps */
68#define THUMB_SUB_DIR "normal\\"
50a14534
EB
69#define realpath(X,Y) _fullpath(Y,X,MAX_PATH)
70
71#else
50a14534
EB
72#define THUMB_DIR "/.thumbnails/"
73#define THUMB_SUB_DIR "normal/"
74#endif
75
76#define PIXMAP_THUMB_SIZE 128
77
adc67de1
GB
78#ifndef MAXPATHLEN
79#define MAXPATHLEN 1024
80#endif
81
50a14534
EB
82static char *md5_hash(const char *message);
83static char *pathdup(const char *path);
84static GdkPixbuf *save_thumbnail(const char *pathname, GdkPixbuf *full);
85static GdkPixbuf *child_create_thumbnail(const gchar *path);
86
87gboolean a_thumbnails_exists ( const gchar *filename )
88{
89 GdkPixbuf *pixbuf = a_thumbnails_get(filename);
90 if ( pixbuf )
91 {
92 g_object_unref ( G_OBJECT ( pixbuf ) );
93 return TRUE;
94 }
95 return FALSE;
96}
97
98GdkPixbuf *a_thumbnails_get_default ()
99{
5bfafde9 100 return gdk_pixbuf_from_pixdata ( &thumbnails_pixbuf, FALSE, NULL );
50a14534
EB
101}
102
103/* filename must be absolute. you could have a function to make sure it exists and absolutize it */
104
105void a_thumbnails_create(const gchar *filename)
106{
107 GdkPixbuf *pixbuf = a_thumbnails_get(filename);
108
109 if ( ! pixbuf )
110 pixbuf = child_create_thumbnail(filename);
111
112 if ( pixbuf )
113 g_object_unref ( G_OBJECT ( pixbuf ) );
114}
115
116GdkPixbuf *a_thumbnails_scale_pixbuf(GdkPixbuf *src, int max_w, int max_h)
117{
118 int w, h;
119
120 w = gdk_pixbuf_get_width(src);
121 h = gdk_pixbuf_get_height(src);
122
123 if (w <= max_w && h <= max_h)
124 {
125 gdk_pixbuf_ref(src);
126 return src;
127 }
128 else
129 {
130 float scale_x = ((float) w) / max_w;
131 float scale_y = ((float) h) / max_h;
132 float scale = MAX(scale_x, scale_y);
133 int dest_w = w / scale;
134 int dest_h = h / scale;
135
136 return gdk_pixbuf_scale_simple(src,
137 MAX(dest_w, 1),
138 MAX(dest_h, 1),
139 GDK_INTERP_BILINEAR);
140 }
141}
142
143static GdkPixbuf *child_create_thumbnail(const gchar *path)
144{
6d0927b1 145 GdkPixbuf *image, *tmpbuf;
50a14534
EB
146
147 image = gdk_pixbuf_new_from_file(path, NULL);
6d0927b1
GK
148 tmpbuf = gdk_pixbuf_apply_embedded_orientation(image);
149 g_object_unref(G_OBJECT(image));
150 image = tmpbuf;
50a14534
EB
151
152 if (image)
153 {
154 GdkPixbuf *thumb = save_thumbnail(path, image);
155 gdk_pixbuf_unref ( image );
156 return thumb;
157 }
158
159 return NULL;
160}
161
162static GdkPixbuf *save_thumbnail(const char *pathname, GdkPixbuf *full)
163{
164 struct stat info;
165 gchar *path;
166 int original_width, original_height;
6d0927b1 167 const gchar* orientation;
50a14534
EB
168 GString *to;
169 char *md5, *swidth, *sheight, *ssize, *smtime, *uri;
170 mode_t old_mask;
171 int name_len;
172 GdkPixbuf *thumb;
173
174 if (stat(pathname, &info) != 0)
175 return NULL;
176
177 thumb = a_thumbnails_scale_pixbuf(full, PIXMAP_THUMB_SIZE, PIXMAP_THUMB_SIZE);
178
6d0927b1
GK
179 orientation = gdk_pixbuf_get_option (full, "orientation");
180
50a14534
EB
181 original_width = gdk_pixbuf_get_width(full);
182 original_height = gdk_pixbuf_get_height(full);
183
184
185 swidth = g_strdup_printf("%d", original_width);
186 sheight = g_strdup_printf("%d", original_height);
187 ssize = g_strdup_printf(ST_SIZE_FMT, info.st_size);
188 smtime = g_strdup_printf("%ld", (long) info.st_mtime);
189
190 path = pathdup(pathname);
191 uri = g_strconcat("file://", path, NULL);
192 md5 = md5_hash(uri);
193 g_free(path);
194
195 to = g_string_new(HOME_DIR);
50a14534 196 g_string_append(to, THUMB_DIR);
50a14534 197 g_string_append(to, THUMB_SUB_DIR);
ab7f5df1 198 g_mkdir_with_parents(to->str, 0700);
50a14534
EB
199 g_string_append(to, md5);
200 name_len = to->len + 4; /* Truncate to this length when renaming */
201#ifdef WINDOWS
202 g_string_append_printf(to, ".png.Viking");
203#else
204 g_string_append_printf(to, ".png.Viking-%ld", (long) getpid());
205#endif
206
207 g_free(md5);
208
209 old_mask = umask(0077);
210 gdk_pixbuf_save(thumb, to->str, "png", NULL,
211 "tEXt::Thumb::Image::Width", swidth,
212 "tEXt::Thumb::Image::Height", sheight,
213 "tEXt::Thumb::Size", ssize,
214 "tEXt::Thumb::MTime", smtime,
215 "tEXt::Thumb::URI", uri,
216 "tEXt::Software", PROJECT,
6d0927b1 217 "tEXt::Software::Orientation", orientation ? orientation : "0",
50a14534
EB
218 NULL);
219 umask(old_mask);
220
221 /* We create the file ###.png.ROX-Filer-PID and rename it to avoid
222 * a race condition if two programs create the same thumb at
223 * once.
224 */
225 {
226 gchar *final;
227
228 final = g_strndup(to->str, name_len);
229 if (rename(to->str, final))
230 {
231 g_warning("Failed to rename '%s' to '%s': %s",
232 to->str, final, g_strerror(errno));
233 g_object_unref ( G_OBJECT(thumb) );
234 thumb = NULL; /* return NULL */
235 }
236
237 g_free(final);
238 }
239
240 g_string_free(to, TRUE);
241 g_free(swidth);
242 g_free(sheight);
243 g_free(ssize);
244 g_free(smtime);
245 g_free(uri);
246
247 return thumb;
248}
249
250
251GdkPixbuf *a_thumbnails_get(const gchar *pathname)
252{
253 GdkPixbuf *thumb = NULL;
254 char *thumb_path, *md5, *uri, *path;
255 const char *ssize, *smtime;
256 struct stat info;
257
258 path = pathdup(pathname);
259 uri = g_strconcat("file://", path, NULL);
260 md5 = md5_hash(uri);
261 g_free(uri);
262
263 thumb_path = g_strdup_printf("%s%s%s%s.png", HOME_DIR, THUMB_DIR, THUMB_SUB_DIR, md5);
264
265 g_free(md5);
266
267 thumb = gdk_pixbuf_new_from_file(thumb_path, NULL);
268 if (!thumb)
269 goto err;
270
271 /* Note that these don't need freeing... */
272 ssize = gdk_pixbuf_get_option(thumb, "tEXt::Thumb::Size");
273 if (!ssize)
274 goto err;
275
276 smtime = gdk_pixbuf_get_option(thumb, "tEXt::Thumb::MTime");
277 if (!smtime)
278 goto err;
279
280 if (stat(path, &info) != 0)
281 goto err;
282
283 if (info.st_mtime != atol(smtime) || info.st_size != atol(ssize))
284 goto err;
285
286 goto out;
287err:
288 if (thumb)
289 gdk_pixbuf_unref(thumb);
290 thumb = NULL;
291out:
292 g_free(path);
293 g_free(thumb_path);
294 return thumb;
295}
296
297/* pathdup() stuff */
298
299static char *pathdup(const char *path)
300{
301 char real[MAXPATHLEN];
302
303 g_return_val_if_fail(path != NULL, NULL);
304
305 if (realpath(path, real))
306 return g_strdup(real);
307
308 return g_strdup(path);
309}
310
311/*
312 * This code implements the MD5 message-digest algorithm.
313 * The algorithm is due to Ron Rivest. The original code was
314 * written by Colin Plumb in 1993, and put in the public domain.
315 *
316 * Modified to use glib datatypes. Put under GPL to simplify
317 * licensing for ROX-Filer. Taken from Debian's dpkg package.
318 *
319 */
320
321#define md5byte unsigned char
322
323typedef struct _MD5Context MD5Context;
324
325struct _MD5Context {
326 guint32 buf[4];
327 guint32 bytes[2];
328 guint32 in[16];
329};
330
331static void MD5Init(MD5Context *ctx);
332static void MD5Update(MD5Context *ctx, md5byte const *buf, unsigned len);
333static char *MD5Final(MD5Context *ctx);
334static void MD5Transform(guint32 buf[4], guint32 const in[16]);
335
336#if G_BYTE_ORDER == G_BIG_ENDIAN
337static void byteSwap(guint32 *buf, unsigned words)
338{
339 md5byte *p = (md5byte *)buf;
340
341 do {
342 *buf++ = (guint32)((unsigned)p[3] << 8 | p[2]) << 16 |
343 ((unsigned)p[1] << 8 | p[0]);
344 p += 4;
345 } while (--words);
346}
347#else
348#define byteSwap(buf,words)
349#endif
350
351/*
352 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
353 * initialization constants.
354 */
355static void MD5Init(MD5Context *ctx)
356{
357 ctx->buf[0] = 0x67452301;
358 ctx->buf[1] = 0xefcdab89;
359 ctx->buf[2] = 0x98badcfe;
360 ctx->buf[3] = 0x10325476;
361
362 ctx->bytes[0] = 0;
363 ctx->bytes[1] = 0;
364}
365
366/*
367 * Update context to reflect the concatenation of another buffer full
368 * of bytes.
369 */
370static void MD5Update(MD5Context *ctx, md5byte const *buf, unsigned len)
371{
372 guint32 t;
373
374 /* Update byte count */
375
376 t = ctx->bytes[0];
377 if ((ctx->bytes[0] = t + len) < t)
378 ctx->bytes[1]++; /* Carry from low to high */
379
380 t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */
381 if (t > len) {
382 memcpy((md5byte *)ctx->in + 64 - t, buf, len);
383 return;
384 }
385 /* First chunk is an odd size */
386 memcpy((md5byte *)ctx->in + 64 - t, buf, t);
387 byteSwap(ctx->in, 16);
388 MD5Transform(ctx->buf, ctx->in);
389 buf += t;
390 len -= t;
391
392 /* Process data in 64-byte chunks */
393 while (len >= 64) {
394 memcpy(ctx->in, buf, 64);
395 byteSwap(ctx->in, 16);
396 MD5Transform(ctx->buf, ctx->in);
397 buf += 64;
398 len -= 64;
399 }
400
401 /* Handle any remaining bytes of data. */
402 memcpy(ctx->in, buf, len);
403}
404
405/*
406 * Final wrapup - pad to 64-byte boundary with the bit pattern
407 * 1 0* (64-bit count of bits processed, MSB-first)
408 * Returns the newly allocated string of the hash.
409 */
410static char *MD5Final(MD5Context *ctx)
411{
412 char *retval;
413 int i;
414 int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */
415 md5byte *p = (md5byte *)ctx->in + count;
416 guint8 *bytes;
417
418 /* Set the first char of padding to 0x80. There is always room. */
419 *p++ = 0x80;
420
421 /* Bytes of padding needed to make 56 bytes (-8..55) */
422 count = 56 - 1 - count;
423
424 if (count < 0) { /* Padding forces an extra block */
425 memset(p, 0, count + 8);
426 byteSwap(ctx->in, 16);
427 MD5Transform(ctx->buf, ctx->in);
428 p = (md5byte *)ctx->in;
429 count = 56;
430 }
431 memset(p, 0, count);
432 byteSwap(ctx->in, 14);
433
434 /* Append length in bits and transform */
435 ctx->in[14] = ctx->bytes[0] << 3;
436 ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
437 MD5Transform(ctx->buf, ctx->in);
438
439 byteSwap(ctx->buf, 4);
440
441 retval = g_malloc(33);
442 bytes = (guint8 *) ctx->buf;
443 for (i = 0; i < 16; i++)
444 sprintf(retval + (i * 2), "%02x", bytes[i]);
445 retval[32] = '\0';
446
447 return retval;
448}
449
450# ifndef ASM_MD5
451
452/* The four core functions - F1 is optimized somewhat */
453
454/* #define F1(x, y, z) (x & y | ~x & z) */
455#define F1(x, y, z) (z ^ (x & (y ^ z)))
456#define F2(x, y, z) F1(z, x, y)
457#define F3(x, y, z) (x ^ y ^ z)
458#define F4(x, y, z) (y ^ (x | ~z))
459
460/* This is the central step in the MD5 algorithm. */
461#define MD5STEP(f,w,x,y,z,in,s) \
462 (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
463
464/*
465 * The core of the MD5 algorithm, this alters an existing MD5 hash to
466 * reflect the addition of 16 longwords of new data. MD5Update blocks
467 * the data and converts bytes into longwords for this routine.
468 */
469static void MD5Transform(guint32 buf[4], guint32 const in[16])
470{
471 register guint32 a, b, c, d;
472
473 a = buf[0];
474 b = buf[1];
475 c = buf[2];
476 d = buf[3];
477
478 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
479 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
480 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
481 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
482 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
483 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
484 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
485 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
486 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
487 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
488 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
489 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
490 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
491 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
492 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
493 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
494
495 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
496 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
497 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
498 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
499 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
500 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
501 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
502 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
503 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
504 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
505 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
506 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
507 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
508 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
509 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
510 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
511
512 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
513 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
514 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
515 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
516 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
517 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
518 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
519 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
520 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
521 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
522 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
523 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
524 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
525 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
526 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
527 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
528
529 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
530 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
531 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
532 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
533 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
534 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
535 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
536 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
537 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
538 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
539 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
540 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
541 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
542 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
543 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
544 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
545
546 buf[0] += a;
547 buf[1] += b;
548 buf[2] += c;
549 buf[3] += d;
550}
551
552# endif /* ASM_MD5 */
553
554static char *md5_hash(const char *message)
555{
556 MD5Context ctx;
557
558 MD5Init(&ctx);
4243132f 559 MD5Update(&ctx, (md5byte *) message, strlen(message));
50a14534
EB
560 return MD5Final(&ctx);
561}