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