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