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