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