2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
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.
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.
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
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).
40 #include <glib/gstdio.h>
42 #include "thumbnails.h"
43 #include "icons/icons.h"
46 #ifdef __CYGWIN_USE_BIG_TYPES__
47 #define ST_SIZE_FMT "%lld"
49 #define ST_SIZE_FMT "%ld"
52 /* FIXME -- on some systems this may need to me "lld", see ROX-Filer code */
53 #define ST_SIZE_FMT "%ld"
56 #undef MIN /* quit yer whining, gcc */
58 #include <sys/param.h> /* for realpath() */
60 /* We need MAX macro and some system does not offer it */
61 #define MAX(a,b) (((a)>(b))?(a):(b))
64 #define HOME_DIR g_get_home_dir()
67 #define THUMB_DIR "\\THUMBNAILS\\" /* viking maps default viking\maps */
68 #define THUMB_SUB_DIR "normal\\"
69 #define realpath(X,Y) _fullpath(Y,X,MAX_PATH)
72 #define THUMB_DIR "/.thumbnails/"
73 #define THUMB_SUB_DIR "normal/"
76 #define PIXMAP_THUMB_SIZE 128
79 #define MAXPATHLEN 1024
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);
87 gboolean a_thumbnails_exists ( const gchar *filename )
89 GdkPixbuf *pixbuf = a_thumbnails_get(filename);
92 g_object_unref ( G_OBJECT ( pixbuf ) );
98 GdkPixbuf *a_thumbnails_get_default ()
100 return gdk_pixbuf_from_pixdata ( &thumbnails_pixbuf, FALSE, NULL );
103 /* filename must be absolute. you could have a function to make sure it exists and absolutize it */
105 void a_thumbnails_create(const gchar *filename)
107 GdkPixbuf *pixbuf = a_thumbnails_get(filename);
110 pixbuf = child_create_thumbnail(filename);
113 g_object_unref ( G_OBJECT ( pixbuf ) );
116 GdkPixbuf *a_thumbnails_scale_pixbuf(GdkPixbuf *src, int max_w, int max_h)
120 w = gdk_pixbuf_get_width(src);
121 h = gdk_pixbuf_get_height(src);
123 if (w <= max_w && h <= max_h)
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;
136 return gdk_pixbuf_scale_simple(src,
139 GDK_INTERP_BILINEAR);
143 static GdkPixbuf *child_create_thumbnail(const gchar *path)
145 GdkPixbuf *image, *tmpbuf;
147 image = gdk_pixbuf_new_from_file(path, NULL);
151 tmpbuf = gdk_pixbuf_apply_embedded_orientation(image);
152 g_object_unref(G_OBJECT(image));
157 GdkPixbuf *thumb = save_thumbnail(path, image);
158 gdk_pixbuf_unref ( image );
165 static GdkPixbuf *save_thumbnail(const char *pathname, GdkPixbuf *full)
169 int original_width, original_height;
170 const gchar* orientation;
172 char *md5, *swidth, *sheight, *ssize, *smtime, *uri;
177 if (stat(pathname, &info) != 0)
180 thumb = a_thumbnails_scale_pixbuf(full, PIXMAP_THUMB_SIZE, PIXMAP_THUMB_SIZE);
182 orientation = gdk_pixbuf_get_option (full, "orientation");
184 original_width = gdk_pixbuf_get_width(full);
185 original_height = gdk_pixbuf_get_height(full);
188 swidth = g_strdup_printf("%d", original_width);
189 sheight = g_strdup_printf("%d", original_height);
190 ssize = g_strdup_printf(ST_SIZE_FMT, info.st_size);
191 smtime = g_strdup_printf("%ld", (long) info.st_mtime);
193 path = pathdup(pathname);
194 uri = g_strconcat("file://", path, NULL);
198 to = g_string_new(HOME_DIR);
199 g_string_append(to, THUMB_DIR);
200 g_string_append(to, THUMB_SUB_DIR);
201 g_mkdir_with_parents(to->str, 0700);
202 g_string_append(to, md5);
203 name_len = to->len + 4; /* Truncate to this length when renaming */
205 g_string_append_printf(to, ".png.Viking");
207 g_string_append_printf(to, ".png.Viking-%ld", (long) getpid());
212 old_mask = umask(0077);
213 gdk_pixbuf_save(thumb, to->str, "png", NULL,
214 "tEXt::Thumb::Image::Width", swidth,
215 "tEXt::Thumb::Image::Height", sheight,
216 "tEXt::Thumb::Size", ssize,
217 "tEXt::Thumb::MTime", smtime,
218 "tEXt::Thumb::URI", uri,
219 "tEXt::Software", PROJECT,
220 "tEXt::Software::Orientation", orientation ? orientation : "0",
224 /* We create the file ###.png.ROX-Filer-PID and rename it to avoid
225 * a race condition if two programs create the same thumb at
231 final = g_strndup(to->str, name_len);
232 if (rename(to->str, final))
234 g_warning("Failed to rename '%s' to '%s': %s",
235 to->str, final, g_strerror(errno));
236 g_object_unref ( G_OBJECT(thumb) );
237 thumb = NULL; /* return NULL */
243 g_string_free(to, TRUE);
254 GdkPixbuf *a_thumbnails_get(const gchar *pathname)
256 GdkPixbuf *thumb = NULL;
257 char *thumb_path, *md5, *uri, *path;
258 const char *ssize, *smtime;
261 path = pathdup(pathname);
262 uri = g_strconcat("file://", path, NULL);
266 thumb_path = g_strdup_printf("%s%s%s%s.png", HOME_DIR, THUMB_DIR, THUMB_SUB_DIR, md5);
270 thumb = gdk_pixbuf_new_from_file(thumb_path, NULL);
274 /* Note that these don't need freeing... */
275 ssize = gdk_pixbuf_get_option(thumb, "tEXt::Thumb::Size");
279 smtime = gdk_pixbuf_get_option(thumb, "tEXt::Thumb::MTime");
283 if (stat(path, &info) != 0)
286 if (info.st_mtime != atol(smtime) || info.st_size != atol(ssize))
292 gdk_pixbuf_unref(thumb);
300 /* pathdup() stuff */
302 static char *pathdup(const char *path)
304 char real[MAXPATHLEN];
306 g_return_val_if_fail(path != NULL, NULL);
308 if (realpath(path, real))
309 return g_strdup(real);
311 return g_strdup(path);
315 * This code implements the MD5 message-digest algorithm.
316 * The algorithm is due to Ron Rivest. The original code was
317 * written by Colin Plumb in 1993, and put in the public domain.
319 * Modified to use glib datatypes. Put under GPL to simplify
320 * licensing for ROX-Filer. Taken from Debian's dpkg package.
324 #define md5byte unsigned char
326 typedef struct _MD5Context MD5Context;
334 static void MD5Init(MD5Context *ctx);
335 static void MD5Update(MD5Context *ctx, md5byte const *buf, unsigned len);
336 static char *MD5Final(MD5Context *ctx);
337 static void MD5Transform(guint32 buf[4], guint32 const in[16]);
339 #if G_BYTE_ORDER == G_BIG_ENDIAN
340 static void byteSwap(guint32 *buf, unsigned words)
342 md5byte *p = (md5byte *)buf;
345 *buf++ = (guint32)((unsigned)p[3] << 8 | p[2]) << 16 |
346 ((unsigned)p[1] << 8 | p[0]);
351 #define byteSwap(buf,words)
355 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
356 * initialization constants.
358 static void MD5Init(MD5Context *ctx)
360 ctx->buf[0] = 0x67452301;
361 ctx->buf[1] = 0xefcdab89;
362 ctx->buf[2] = 0x98badcfe;
363 ctx->buf[3] = 0x10325476;
370 * Update context to reflect the concatenation of another buffer full
373 static void MD5Update(MD5Context *ctx, md5byte const *buf, unsigned len)
377 /* Update byte count */
380 if ((ctx->bytes[0] = t + len) < t)
381 ctx->bytes[1]++; /* Carry from low to high */
383 t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */
385 memcpy((md5byte *)ctx->in + 64 - t, buf, len);
388 /* First chunk is an odd size */
389 memcpy((md5byte *)ctx->in + 64 - t, buf, t);
390 byteSwap(ctx->in, 16);
391 MD5Transform(ctx->buf, ctx->in);
395 /* Process data in 64-byte chunks */
397 memcpy(ctx->in, buf, 64);
398 byteSwap(ctx->in, 16);
399 MD5Transform(ctx->buf, ctx->in);
404 /* Handle any remaining bytes of data. */
405 memcpy(ctx->in, buf, len);
409 * Final wrapup - pad to 64-byte boundary with the bit pattern
410 * 1 0* (64-bit count of bits processed, MSB-first)
411 * Returns the newly allocated string of the hash.
413 static char *MD5Final(MD5Context *ctx)
417 int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */
418 md5byte *p = (md5byte *)ctx->in + count;
421 /* Set the first char of padding to 0x80. There is always room. */
424 /* Bytes of padding needed to make 56 bytes (-8..55) */
425 count = 56 - 1 - count;
427 if (count < 0) { /* Padding forces an extra block */
428 memset(p, 0, count + 8);
429 byteSwap(ctx->in, 16);
430 MD5Transform(ctx->buf, ctx->in);
431 p = (md5byte *)ctx->in;
435 byteSwap(ctx->in, 14);
437 /* Append length in bits and transform */
438 ctx->in[14] = ctx->bytes[0] << 3;
439 ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
440 MD5Transform(ctx->buf, ctx->in);
442 byteSwap(ctx->buf, 4);
444 retval = g_malloc(33);
445 bytes = (guint8 *) ctx->buf;
446 for (i = 0; i < 16; i++)
447 sprintf(retval + (i * 2), "%02x", bytes[i]);
455 /* The four core functions - F1 is optimized somewhat */
457 /* #define F1(x, y, z) (x & y | ~x & z) */
458 #define F1(x, y, z) (z ^ (x & (y ^ z)))
459 #define F2(x, y, z) F1(z, x, y)
460 #define F3(x, y, z) (x ^ y ^ z)
461 #define F4(x, y, z) (y ^ (x | ~z))
463 /* This is the central step in the MD5 algorithm. */
464 #define MD5STEP(f,w,x,y,z,in,s) \
465 (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
468 * The core of the MD5 algorithm, this alters an existing MD5 hash to
469 * reflect the addition of 16 longwords of new data. MD5Update blocks
470 * the data and converts bytes into longwords for this routine.
472 static void MD5Transform(guint32 buf[4], guint32 const in[16])
474 register guint32 a, b, c, d;
481 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
482 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
483 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
484 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
485 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
486 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
487 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
488 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
489 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
490 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
491 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
492 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
493 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
494 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
495 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
496 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
498 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
499 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
500 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
501 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
502 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
503 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
504 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
505 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
506 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
507 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
508 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
509 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
510 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
511 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
512 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
513 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
515 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
516 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
517 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
518 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
519 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
520 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
521 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
522 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
523 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
524 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
525 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
526 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
527 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
528 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
529 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
530 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
532 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
533 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
534 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
535 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
536 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
537 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
538 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
539 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
540 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
541 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
542 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
543 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
544 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
545 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
546 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
547 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
555 # endif /* ASM_MD5 */
557 static char *md5_hash(const char *message)
562 MD5Update(&ctx, (md5byte *) message, strlen(message));
563 return MD5Final(&ctx);