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