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