Mercurial > geeqie
annotate src/thumb.c @ 699:9873d695a9c1
Do not use glib posix wrappers since they were introduced in 2.6
and we want 2.4 compatibility.
author | zas_ |
---|---|
date | Tue, 20 May 2008 20:11:29 +0000 |
parents | a3218946bd2d |
children | 179a7224dde1 |
rev | line source |
---|---|
1 | 1 /* |
196 | 2 * Geeqie |
9 | 3 * (C) 2004 John Ellis |
475 | 4 * Copyright (C) 2008 The Geeqie Team |
1 | 5 * |
6 * Author: John Ellis | |
7 * | |
9 | 8 * This software is released under the GNU General Public License (GNU GPL). |
9 * Please read the included file COPYING for more information. | |
10 * This software comes with no warranty of any kind, use at your own risk! | |
1 | 11 */ |
12 | |
9 | 13 |
281 | 14 #include "main.h" |
9 | 15 #include "thumb.h" |
1 | 16 |
9 | 17 #include "cache.h" |
18 #include "image-load.h" | |
586 | 19 #include "filedata.h" |
9 | 20 #include "pixbuf_util.h" |
21 #include "thumb_standard.h" | |
22 #include "ui_fileops.h" | |
1 | 23 |
9 | 24 #include <utime.h> |
25 | |
26 | |
27 static void thumb_loader_error_cb(ImageLoader *il, gpointer data); | |
28 static void thumb_loader_setup(ThumbLoader *tl, gchar *path); | |
29 | |
30 static gint normalize_thumb(gint *width, gint *height, gint max_w, gint max_h); | |
31 static GdkPixbuf *get_xv_thumbnail(gchar *thumb_filename, gint max_w, gint max_h); | |
32 | |
1 | 33 |
34 /* | |
35 *----------------------------------------------------------------------------- | |
36 * thumbnail routines: creation, caching, and maintenance (public) | |
37 *----------------------------------------------------------------------------- | |
38 */ | |
39 | |
9 | 40 static gint thumb_loader_save_to_cache(ThumbLoader *tl) |
1 | 41 { |
9 | 42 gchar *cache_dir; |
43 gint success = FALSE; | |
44 mode_t mode = 0755; | |
1 | 45 |
9 | 46 if (!tl || !tl->pixbuf) return FALSE; |
1 | 47 |
9 | 48 cache_dir = cache_get_location(CACHE_TYPE_THUMB, tl->path, FALSE, &mode); |
1 | 49 |
9 | 50 if (cache_ensure_dir_exists(cache_dir, mode)) |
1 | 51 { |
52 gchar *cache_path; | |
9 | 53 gchar *pathl; |
54 | |
55 cache_path = g_strconcat(cache_dir, "/", filename_from_path(tl->path), | |
283 | 56 GQ_CACHE_EXT_THUMB, NULL); |
1 | 57 |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
58 DEBUG_1("Saving thumb: %s", cache_path); |
9 | 59 |
60 pathl = path_from_utf8(cache_path); | |
61 success = pixbuf_to_file_as_png(tl->pixbuf, pathl); | |
62 if (success) | |
1 | 63 { |
9 | 64 struct utimbuf ut; |
65 /* set thumb time to that of source file */ | |
66 | |
67 ut.actime = ut.modtime = filetime(tl->path); | |
68 if (ut.modtime > 0) | |
1 | 69 { |
9 | 70 utime(pathl, &ut); |
1 | 71 } |
72 } | |
73 else | |
74 { | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
75 DEBUG_1("Saving failed: %s", pathl); |
1 | 76 } |
9 | 77 |
78 g_free(pathl); | |
79 g_free(cache_path); | |
80 } | |
81 | |
82 g_free(cache_dir); | |
83 | |
84 return success; | |
85 } | |
1 | 86 |
9 | 87 static gint thumb_loader_mark_failure(ThumbLoader *tl) |
88 { | |
89 gchar *cache_dir; | |
90 gint success = FALSE; | |
91 mode_t mode = 0755; | |
442 | 92 |
9 | 93 if (!tl) return FALSE; |
94 | |
95 cache_dir = cache_get_location(CACHE_TYPE_THUMB, tl->path, FALSE, &mode); | |
1 | 96 |
9 | 97 if (cache_ensure_dir_exists(cache_dir, mode)) |
98 { | |
99 gchar *cache_path; | |
100 gchar *pathl; | |
101 FILE *f; | |
102 | |
103 cache_path = g_strconcat(cache_dir, "/", filename_from_path(tl->path), | |
283 | 104 GQ_CACHE_EXT_THUMB, NULL); |
9 | 105 |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
106 DEBUG_1("marking thumb failure: %s", cache_path); |
9 | 107 |
108 pathl = path_from_utf8(cache_path); | |
109 f = fopen(pathl, "w"); | |
110 if (f) | |
1 | 111 { |
9 | 112 struct utimbuf ut; |
113 | |
512
f9bf33be53ff
Remove whitespace between function name and first parenthesis for the sake of consistency.
zas_
parents:
507
diff
changeset
|
114 fclose(f); |
1 | 115 |
9 | 116 ut.actime = ut.modtime = filetime(tl->path); |
117 if (ut.modtime > 0) | |
1 | 118 { |
9 | 119 utime(pathl, &ut); |
1 | 120 } |
121 | |
9 | 122 success = TRUE; |
123 } | |
124 | |
125 g_free(pathl); | |
126 g_free(cache_path); | |
127 } | |
128 | |
129 g_free(cache_dir); | |
130 return success; | |
131 } | |
132 | |
133 static void thumb_loader_percent_cb(ImageLoader *il, gdouble percent, gpointer data) | |
134 { | |
135 ThumbLoader *tl = data; | |
136 | |
137 tl->percent_done = percent; | |
138 | |
139 if (tl->func_progress) tl->func_progress(tl, tl->data); | |
140 } | |
141 | |
142 static void thumb_loader_done_cb(ImageLoader *il, gpointer data) | |
143 { | |
144 ThumbLoader *tl = data; | |
145 GdkPixbuf *pixbuf; | |
146 gint pw, ph; | |
147 gint save; | |
148 | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
149 DEBUG_1("thumb done: %s", tl->path); |
1 | 150 |
9 | 151 pixbuf = image_loader_get_pixbuf(tl->il); |
152 if (!pixbuf) | |
153 { | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
154 DEBUG_1("...but no pixbuf: %s", tl->path); |
9 | 155 thumb_loader_error_cb(tl->il, tl); |
156 return; | |
157 } | |
158 | |
159 pw = gdk_pixbuf_get_width(pixbuf); | |
160 ph = gdk_pixbuf_get_height(pixbuf); | |
161 | |
162 if (tl->cache_hit && pw != tl->max_w && ph != tl->max_h) | |
163 { | |
164 /* requested thumbnail size may have changed, load original */ | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
165 DEBUG_1("thumbnail size mismatch, regenerating: %s", tl->path); |
9 | 166 tl->cache_hit = FALSE; |
167 | |
168 thumb_loader_setup(tl, tl->path); | |
1 | 169 |
9 | 170 if (!image_loader_start(tl->il, thumb_loader_done_cb, tl)) |
171 { | |
172 image_loader_free(tl->il); | |
173 tl->il = NULL; | |
174 | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
175 DEBUG_1("regeneration failure: %s", tl->path); |
9 | 176 thumb_loader_error_cb(tl->il, tl); |
177 } | |
178 return; | |
179 } | |
1 | 180 |
9 | 181 /* scale ?? */ |
182 | |
183 if (pw > tl->max_w || ph > tl->max_h) | |
184 { | |
185 gint w, h; | |
186 | |
14
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
187 if (((double)tl->max_w / pw) < ((double)tl->max_h / ph)) |
9 | 188 { |
189 w = tl->max_w; | |
14
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
190 h = (double)w / pw * ph; |
9 | 191 if (h < 1) h = 1; |
1 | 192 } |
193 else | |
194 { | |
9 | 195 h = tl->max_h; |
14
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
196 w = (double)h / ph * pw; |
9 | 197 if (w < 1) w = 1; |
1 | 198 } |
199 | |
333 | 200 tl->pixbuf = gdk_pixbuf_scale_simple(pixbuf, w, h, (GdkInterpType)options->thumbnails.quality); |
9 | 201 save = TRUE; |
1 | 202 } |
203 else | |
204 { | |
9 | 205 tl->pixbuf = pixbuf; |
206 gdk_pixbuf_ref(tl->pixbuf); | |
14
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
207 save = il->shrunk; |
9 | 208 } |
209 | |
210 /* save it ? */ | |
211 if (tl->cache_enable && save) | |
212 { | |
213 thumb_loader_save_to_cache(tl); | |
214 } | |
215 | |
216 if (tl->func_done) tl->func_done(tl, tl->data); | |
217 } | |
218 | |
219 static void thumb_loader_error_cb(ImageLoader *il, gpointer data) | |
220 { | |
221 ThumbLoader *tl = data; | |
222 | |
223 /* if at least some of the image is available, go to done_cb */ | |
224 if (image_loader_get_pixbuf(tl->il) != NULL) | |
225 { | |
226 thumb_loader_done_cb(il, data); | |
227 return; | |
1 | 228 } |
9 | 229 |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
230 DEBUG_1("thumb error: %s", tl->path); |
9 | 231 |
232 image_loader_free(tl->il); | |
233 tl->il = NULL; | |
234 | |
235 if (tl->func_error) tl->func_error(tl, tl->data); | |
236 } | |
237 | |
238 static gint thumb_loader_done_delay_cb(gpointer data) | |
239 { | |
240 ThumbLoader *tl = data; | |
241 | |
242 tl->idle_done_id = -1; | |
243 | |
244 if (tl->func_done) tl->func_done(tl, tl->data); | |
245 | |
246 return FALSE; | |
247 } | |
248 | |
249 static void thumb_loader_delay_done(ThumbLoader *tl) | |
250 { | |
251 if (tl->idle_done_id == -1) tl->idle_done_id = g_idle_add(thumb_loader_done_delay_cb, tl); | |
252 } | |
253 | |
254 static void thumb_loader_setup(ThumbLoader *tl, gchar *path) | |
255 { | |
256 image_loader_free(tl->il); | |
138 | 257 tl->il = image_loader_new(file_data_new_simple(path)); |
9 | 258 |
333 | 259 if (options->thumbnails.fast) |
14
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
260 { |
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
261 /* this will speed up jpegs by up to 3x in some cases */ |
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
262 image_loader_set_requested_size(tl->il, tl->max_w, tl->max_h); |
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
263 } |
9 | 264 |
265 image_loader_set_error_func(tl->il, thumb_loader_error_cb, tl); | |
266 if (tl->func_progress) image_loader_set_percent_func(tl->il, thumb_loader_percent_cb, tl); | |
267 } | |
268 | |
269 void thumb_loader_set_callbacks(ThumbLoader *tl, | |
270 ThumbLoaderFunc func_done, | |
271 ThumbLoaderFunc func_error, | |
272 ThumbLoaderFunc func_progress, | |
273 gpointer data) | |
274 { | |
275 if (!tl) return; | |
276 | |
277 if (tl->standard_loader) | |
278 { | |
279 thumb_loader_std_set_callbacks((ThumbLoaderStd *)tl, | |
280 (ThumbLoaderStdFunc) func_done, | |
281 (ThumbLoaderStdFunc) func_error, | |
282 (ThumbLoaderStdFunc) func_progress, | |
283 data); | |
284 return; | |
285 } | |
286 | |
287 tl->func_done = func_done; | |
288 tl->func_error = func_error; | |
289 tl->func_progress = func_progress; | |
290 | |
291 tl->data = data; | |
292 } | |
293 | |
294 void thumb_loader_set_cache(ThumbLoader *tl, gint enable_cache, gint local, gint retry_failed) | |
295 { | |
442 | 296 if (!tl) return; |
9 | 297 |
298 if (tl->standard_loader) | |
299 { | |
300 thumb_loader_std_set_cache((ThumbLoaderStd *)tl, enable_cache, local, retry_failed); | |
301 return; | |
302 } | |
303 | |
304 tl->cache_enable = enable_cache; | |
305 #if 0 | |
306 tl->cache_local = local; | |
307 tl->cache_retry = retry_failed; | |
308 #endif | |
1 | 309 } |
310 | |
311 | |
9 | 312 gint thumb_loader_start(ThumbLoader *tl, const gchar *path) |
313 { | |
314 gchar *cache_path = NULL; | |
1 | 315 |
9 | 316 if (!tl) return FALSE; |
1 | 317 |
9 | 318 if (tl->standard_loader) |
1 | 319 { |
9 | 320 return thumb_loader_std_start((ThumbLoaderStd *)tl, path); |
321 } | |
322 | |
323 if (!tl->path && !path) return FALSE; | |
1 | 324 |
9 | 325 if (!tl->path) tl->path = g_strdup(path); |
1 | 326 |
9 | 327 if (tl->cache_enable) |
328 { | |
329 cache_path = cache_find_location(CACHE_TYPE_THUMB, tl->path); | |
330 | |
331 if (cache_path) | |
1 | 332 { |
9 | 333 if (cache_time_valid(cache_path, tl->path)) |
334 { | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
335 DEBUG_1("Found in cache:%s", tl->path); |
1 | 336 |
9 | 337 if (filesize(cache_path) == 0) |
1 | 338 { |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
339 DEBUG_1("Broken image mark found:%s", cache_path); |
9 | 340 g_free(cache_path); |
341 return FALSE; | |
1 | 342 } |
9 | 343 |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
344 DEBUG_1("Cache location:%s", cache_path); |
9 | 345 } |
346 else | |
347 { | |
348 g_free(cache_path); | |
349 cache_path = NULL; | |
1 | 350 } |
351 } | |
9 | 352 } |
353 | |
333 | 354 if (!cache_path && options->thumbnails.use_xvpics) |
9 | 355 { |
356 tl->pixbuf = get_xv_thumbnail(tl->path, tl->max_w, tl->max_h); | |
357 if (tl->pixbuf) | |
358 { | |
359 thumb_loader_delay_done(tl); | |
360 return TRUE; | |
361 } | |
362 } | |
363 | |
364 if (cache_path) | |
365 { | |
366 thumb_loader_setup(tl, cache_path); | |
367 g_free(cache_path); | |
368 tl->cache_hit = TRUE; | |
369 } | |
370 else | |
371 { | |
372 thumb_loader_setup(tl, tl->path); | |
373 } | |
374 | |
375 if (!image_loader_start(tl->il, thumb_loader_done_cb, tl)) | |
376 { | |
377 /* try from original if cache attempt */ | |
378 if (tl->cache_hit) | |
379 { | |
380 tl->cache_hit = FALSE; | |
694 | 381 log_printf("%s", _("Thumbnail image in cache failed to load, trying to recreate.\n")); |
9 | 382 |
383 thumb_loader_setup(tl, tl->path); | |
384 if (image_loader_start(tl->il, thumb_loader_done_cb, tl)) return TRUE; | |
385 } | |
386 /* mark failed thumbnail in cache with 0 byte file */ | |
387 if (tl->cache_enable) | |
388 { | |
389 thumb_loader_mark_failure(tl); | |
390 } | |
442 | 391 |
9 | 392 image_loader_free(tl->il); |
393 tl->il = NULL; | |
394 return FALSE; | |
395 } | |
396 | |
397 return TRUE; | |
398 } | |
399 | |
400 #if 0 | |
401 gint thumb_loader_to_pixmap(ThumbLoader *tl, GdkPixmap **pixmap, GdkBitmap **mask) | |
402 { | |
403 if (!tl || !tl->pixbuf) return -1; | |
404 | |
405 gdk_pixbuf_render_pixmap_and_mask(tl->pixbuf, pixmap, mask, 128); | |
406 | |
407 return thumb_loader_get_space(tl); | |
408 } | |
409 #endif | |
410 | |
411 GdkPixbuf *thumb_loader_get_pixbuf(ThumbLoader *tl, gint with_fallback) | |
412 { | |
413 GdkPixbuf *pixbuf; | |
414 | |
415 if (tl && tl->standard_loader) | |
416 { | |
417 return thumb_loader_std_get_pixbuf((ThumbLoaderStd *)tl, with_fallback); | |
418 } | |
419 | |
420 if (tl && tl->pixbuf) | |
421 { | |
422 pixbuf = tl->pixbuf; | |
423 g_object_ref(pixbuf); | |
424 } | |
425 else if (with_fallback) | |
426 { | |
427 gint w, h; | |
428 | |
429 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN); | |
430 w = gdk_pixbuf_get_width(pixbuf); | |
431 h = gdk_pixbuf_get_height(pixbuf); | |
432 if ((w > tl->max_w || h > tl->max_h) && | |
433 normalize_thumb(&w, &h, tl->max_w, tl->max_h)) | |
434 { | |
435 GdkPixbuf *tmp; | |
436 | |
437 tmp = pixbuf; | |
438 pixbuf = gdk_pixbuf_scale_simple(tmp, w, h, GDK_INTERP_NEAREST); | |
439 gdk_pixbuf_unref(tmp); | |
440 } | |
1 | 441 } |
9 | 442 else |
443 { | |
444 pixbuf = NULL; | |
445 } | |
446 | |
447 return pixbuf; | |
448 } | |
449 | |
450 #if 0 | |
451 gint thumb_loader_get_space(ThumbLoader *tl) | |
452 { | |
453 if (!tl) return 0; | |
454 | |
455 if (tl->pixbuf) return (tl->max_w - gdk_pixbuf_get_width(tl->pixbuf)); | |
456 | |
457 return tl->max_w; | |
458 } | |
459 #endif | |
460 | |
461 ThumbLoader *thumb_loader_new(gint width, gint height) | |
462 { | |
463 ThumbLoader *tl; | |
464 | |
333 | 465 if (options->thumbnails.spec_standard) |
9 | 466 { |
467 return (ThumbLoader *)thumb_loader_std_new(width, height); | |
468 } | |
469 | |
470 tl = g_new0(ThumbLoader, 1); | |
471 tl->standard_loader = FALSE; | |
472 tl->path = NULL; | |
333 | 473 tl->cache_enable = options->thumbnails.enable_caching; |
9 | 474 tl->cache_hit = FALSE; |
475 tl->percent_done = 0.0; | |
476 tl->max_w = width; | |
477 tl->max_h = height; | |
478 | |
479 tl->il = NULL; | |
480 | |
481 tl->idle_done_id = -1; | |
482 | |
483 return tl; | |
1 | 484 } |
485 | |
9 | 486 void thumb_loader_free(ThumbLoader *tl) |
487 { | |
488 if (!tl) return; | |
489 | |
490 if (tl->standard_loader) | |
491 { | |
492 thumb_loader_std_free((ThumbLoaderStd *)tl); | |
493 return; | |
494 } | |
495 | |
496 if (tl->pixbuf) gdk_pixbuf_unref(tl->pixbuf); | |
497 image_loader_free(tl->il); | |
498 g_free(tl->path); | |
499 | |
500 if (tl->idle_done_id != -1) g_source_remove(tl->idle_done_id); | |
501 | |
502 g_free(tl); | |
503 } | |
504 | |
505 #if 0 | |
506 gint thumb_from_xpm_d(const char **data, gint max_w, gint max_h, GdkPixmap **pixmap, GdkBitmap **mask) | |
507 { | |
508 GdkPixbuf *pixbuf; | |
509 gint w, h; | |
510 | |
511 pixbuf = gdk_pixbuf_new_from_xpm_data(data); | |
512 w = gdk_pixbuf_get_width(pixbuf); | |
513 h = gdk_pixbuf_get_height(pixbuf); | |
514 | |
515 if ((w > max_w || h > max_h) && | |
516 normalize_thumb(&w, &h, max_w, max_h)) | |
517 { | |
518 /* scale */ | |
519 GdkPixbuf *tmp; | |
520 | |
521 tmp = pixbuf; | |
522 pixbuf = gdk_pixbuf_scale_simple(tmp, w, h, GDK_INTERP_NEAREST); | |
523 gdk_pixbuf_unref(tmp); | |
524 } | |
525 | |
526 gdk_pixbuf_render_pixmap_and_mask(pixbuf, pixmap, mask, 128); | |
527 gdk_pixbuf_unref(pixbuf); | |
528 | |
529 return w; | |
530 } | |
531 #endif | |
532 | |
1 | 533 /* |
534 *----------------------------------------------------------------------------- | |
535 * xvpics thumbnail support, read-only (private) | |
536 *----------------------------------------------------------------------------- | |
537 */ | |
538 | |
539 /* | |
540 * xvpics code originally supplied by: | |
541 * "Diederen Damien" <D.Diederen@student.ulg.ac.be> | |
542 * | |
543 * Note: Code has been modified to fit the style of the other code, and to use | |
544 * a few more glib-isms. | |
9 | 545 * 08-28-2000: Updated to return a gdk_pixbuf, Imlib is dieing a death here. |
1 | 546 */ |
547 | |
548 #define XV_BUFFER 2048 | |
549 static guchar *load_xv_thumbnail(gchar *filename, gint *widthp, gint *heightp) | |
550 { | |
551 FILE *file; | |
552 gchar buffer[XV_BUFFER]; | |
553 guchar *data; | |
554 gint width, height, depth; | |
555 | |
556 file = fopen(filename, "rt"); | |
557 if(!file) return NULL; | |
558 | |
559 fgets(buffer, XV_BUFFER, file); | |
560 if(strncmp(buffer, "P7 332", 6) != 0) | |
561 { | |
562 fclose(file); | |
563 return NULL; | |
564 } | |
565 | |
516 | 566 while (fgets(buffer, XV_BUFFER, file) && buffer[0] == '#') /* do_nothing() */; |
1 | 567 |
568 if(sscanf(buffer, "%d %d %d", &width, &height, &depth) != 3) | |
569 { | |
570 fclose(file); | |
571 return NULL; | |
572 } | |
573 | |
574 data = g_new(guchar, width * height); | |
575 fread(data, 1, width * height, file); | |
576 | |
577 fclose(file); | |
578 *widthp = width; | |
579 *heightp = height; | |
580 return data; | |
581 } | |
582 #undef XV_BUFFER | |
583 | |
9 | 584 static gint normalize_thumb(gint *width, gint *height, gint max_w, gint max_h) |
1 | 585 { |
9 | 586 gdouble scale; |
587 gint new_w, new_h; | |
588 | |
589 scale = MIN((gdouble) max_w / *width, (gdouble) max_h / *height); | |
590 new_w = *width * scale; | |
591 new_h = *height * scale; | |
592 | |
593 if (new_w != *width || new_h != *height) | |
1 | 594 { |
9 | 595 *width = new_w; |
596 *height = new_h; | |
597 return TRUE; | |
1 | 598 } |
9 | 599 |
600 return FALSE; | |
1 | 601 } |
602 | |
9 | 603 static void free_rgb_buffer(guchar *pixels, gpointer data) |
604 { | |
605 g_free(pixels); | |
606 } | |
607 | |
608 static GdkPixbuf *get_xv_thumbnail(gchar *thumb_filename, gint max_w, gint max_h) | |
1 | 609 { |
610 gint width, height; | |
611 gchar *thumb_name; | |
612 gchar *tmp_string; | |
613 gchar *last_slash; | |
614 guchar *packed_data; | |
615 | |
9 | 616 tmp_string = path_from_utf8(thumb_filename); |
1 | 617 last_slash = strrchr(tmp_string, '/'); |
9 | 618 if(!last_slash) return NULL; |
1 | 619 *last_slash++ = '\0'; |
620 | |
621 thumb_name = g_strconcat(tmp_string, "/.xvpics/", last_slash, NULL); | |
622 packed_data = load_xv_thumbnail(thumb_name, &width, &height); | |
623 g_free(tmp_string); | |
624 g_free(thumb_name); | |
625 | |
626 if(packed_data) | |
627 { | |
628 guchar *rgb_data; | |
9 | 629 GdkPixbuf *pixbuf; |
1 | 630 gint i; |
631 | |
632 rgb_data = g_new(guchar, width * height * 3); | |
517 | 633 for (i = 0; i < width * height; i++) |
1 | 634 { |
635 rgb_data[i * 3 + 0] = (packed_data[i] >> 5) * 36; | |
636 rgb_data[i * 3 + 1] = ((packed_data[i] & 28) >> 2) * 36; | |
637 rgb_data[i * 3 + 2] = (packed_data[i] & 3) * 85; | |
638 } | |
9 | 639 g_free(packed_data); |
1 | 640 |
9 | 641 pixbuf = gdk_pixbuf_new_from_data(rgb_data, GDK_COLORSPACE_RGB, FALSE, 8, |
642 width, height, 3 * width, free_rgb_buffer, NULL); | |
643 | |
644 if (normalize_thumb(&width, &height, max_w, max_h)) | |
645 { | |
646 /* scale */ | |
647 GdkPixbuf *tmp; | |
648 | |
649 tmp = pixbuf; | |
650 pixbuf = gdk_pixbuf_scale_simple(tmp, width, height, GDK_INTERP_NEAREST); | |
651 gdk_pixbuf_unref(tmp); | |
652 } | |
442 | 653 |
9 | 654 return pixbuf; |
1 | 655 } |
656 | |
9 | 657 return NULL; |
1 | 658 } |