2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* pixmaps.c - code for handling pixbufs (despite the name!) */
25 /* Remove pixmaps from the cache when they haven't been accessed for
26 * this period of time (seconds).
29 #define PIXMAP_PURGE_TIME 1200
30 #define PIXMAP_THUMB_SIZE 128
31 #define PIXMAP_THUMB_TOO_OLD_TIME 5
37 #include <sys/types.h>
47 #include "gui_support.h"
58 GFSCache
*pixmap_cache
= NULL
;
59 GFSCache
*desktop_icon_cache
= NULL
;
61 static const char * bad_xpm
[] = {
79 MaskedPixmap
*im_error
;
80 MaskedPixmap
*im_unknown
;
82 MaskedPixmap
*im_appdir
;
84 MaskedPixmap
*im_dirs
;
86 GtkIconSize mount_icon_size
= -1;
88 typedef struct _ChildThumbnail ChildThumbnail
;
90 /* There is one of these for each active child process */
91 struct _ChildThumbnail
{
97 static const char *stocks
[] = {
98 ROX_STOCK_SHOW_DETAILS
,
99 ROX_STOCK_SHOW_HIDDEN
,
107 /* Static prototypes */
109 static void load_default_pixmaps(void);
110 static gint
purge(gpointer data
);
111 static MaskedPixmap
*image_from_file(const char *path
);
112 static MaskedPixmap
*image_from_desktop_file(const char *path
);
113 static MaskedPixmap
*get_bad_image(void);
114 static GdkPixbuf
*scale_pixbuf_up(GdkPixbuf
*src
, int max_w
, int max_h
);
115 static GdkPixbuf
*get_thumbnail_for(const char *path
);
116 static void thumbnail_child_done(ChildThumbnail
*info
);
117 static void child_create_thumbnail(const gchar
*path
, MIME_type
*type
);
118 static GList
*thumbs_purge_cache(Option
*option
, xmlNode
*node
, guchar
*label
);
119 static gchar
*thumbnail_path(const gchar
*path
);
120 static gchar
*thumbnail_program(MIME_type
*type
);
121 static GdkPixbuf
*extract_tiff_thumbnail(const gchar
*path
);
123 /****************************************************************
124 * EXTERNAL INTERFACE *
125 ****************************************************************/
127 void pixmaps_init(void)
129 GtkIconFactory
*factory
;
132 gtk_widget_push_colormap(gdk_rgb_get_colormap());
134 pixmap_cache
= g_fscache_new((GFSLoadFunc
) image_from_file
, NULL
, NULL
);
135 desktop_icon_cache
= g_fscache_new((GFSLoadFunc
) image_from_desktop_file
, NULL
, NULL
);
137 g_timeout_add(10000, purge
, NULL
);
139 factory
= gtk_icon_factory_new();
140 for (i
= 0; i
< G_N_ELEMENTS(stocks
); i
++)
143 GError
*error
= NULL
;
146 const gchar
*name
= stocks
[i
];
148 path
= g_strconcat(app_dir
, "/images/", name
, ".png", NULL
);
149 pixbuf
= gdk_pixbuf_new_from_file(path
, &error
);
152 g_warning("%s", error
->message
);
154 pixbuf
= gdk_pixbuf_new_from_xpm_data(bad_xpm
);
158 iset
= gtk_icon_set_new_from_pixbuf(pixbuf
);
159 g_object_unref(G_OBJECT(pixbuf
));
160 gtk_icon_factory_add(factory
, name
, iset
);
161 gtk_icon_set_unref(iset
);
163 gtk_icon_factory_add_default(factory
);
165 mount_icon_size
= gtk_icon_size_register("rox-mount-size", 14, 14);
167 load_default_pixmaps();
169 option_register_widget("thumbs-purge-cache", thumbs_purge_cache
);
172 /* Load image <appdir>/images/name.png.
173 * Always returns with a valid image.
175 MaskedPixmap
*load_pixmap(const char *name
)
178 MaskedPixmap
*retval
;
180 path
= g_strconcat(app_dir
, "/images/", name
, ".png", NULL
);
181 retval
= image_from_file(path
);
185 retval
= get_bad_image();
190 /* Create a MaskedPixmap from a GTK stock ID. Always returns
193 static MaskedPixmap
*mp_from_stock(const char *stock_id
, int size
)
195 GtkIconSet
*icon_set
;
197 MaskedPixmap
*retval
;
199 /*icon_set = gtk_icon_factory_lookup_default(stock_id);*/
200 icon_set
= gtk_style_lookup_icon_set(gtk_widget_get_default_style(),
203 return get_bad_image();
205 pixbuf
= gtk_icon_set_render_icon(icon_set
,
206 gtk_widget_get_default_style(), /* Gtk bug */
212 retval
= masked_pixmap_new(pixbuf
);
213 gdk_pixbuf_unref(pixbuf
);
218 void pixmap_make_huge(MaskedPixmap
*mp
)
223 g_return_if_fail(mp
->src_pixbuf
!= NULL
);
225 /* Limit to small size now, otherwise they get scaled up in mixed mode.
228 mp
->huge_pixbuf
= scale_pixbuf_up(mp
->src_pixbuf
,
229 SMALL_WIDTH
, SMALL_HEIGHT
);
231 if (!mp
->huge_pixbuf
)
233 mp
->huge_pixbuf
= mp
->src_pixbuf
;
234 g_object_ref(mp
->huge_pixbuf
);
237 mp
->huge_width
= gdk_pixbuf_get_width(mp
->huge_pixbuf
);
238 mp
->huge_height
= gdk_pixbuf_get_height(mp
->huge_pixbuf
);
241 void pixmap_make_small(MaskedPixmap
*mp
)
246 g_return_if_fail(mp
->src_pixbuf
!= NULL
);
248 mp
->sm_pixbuf
= scale_pixbuf(mp
->src_pixbuf
, SMALL_WIDTH
, SMALL_HEIGHT
);
252 mp
->sm_pixbuf
= mp
->src_pixbuf
;
253 g_object_ref(mp
->sm_pixbuf
);
256 mp
->sm_width
= gdk_pixbuf_get_width(mp
->sm_pixbuf
);
257 mp
->sm_height
= gdk_pixbuf_get_height(mp
->sm_pixbuf
);
260 /* Load image 'path' in the background and insert into pixmap_cache.
261 * Call callback(data, path) when done (path is NULL => error).
262 * If the image is already uptodate, or being created already, calls the
263 * callback right away.
265 void pixmap_background_thumb(const gchar
*path
, GFunc callback
, gpointer data
)
271 ChildThumbnail
*info
;
276 image
= g_fscache_lookup_full(pixmap_cache
, path
,
277 FSCACHE_LOOKUP_ONLY_NEW
, &found
);
281 /* Thumbnail is known, or being created */
283 g_object_unref(image
);
284 callback(data
, NULL
);
288 g_return_if_fail(image
== NULL
);
290 pixbuf
= get_thumbnail_for(path
);
294 struct stat info1
, info2
;
297 // Skip zero-byte files. They're either empty, or special (may cause
298 // us to hang, e.g. /proc/kmsg).
299 if (mc_stat(path
, &info1
) == 0 && info1
.st_size
== 0) {
300 callback(data
, NULL
);
304 dir
= g_path_get_dirname(path
);
306 /* If the image itself is in ~/.thumbnails, load it now
307 * (ie, don't create thumbnails for thumbnails!).
309 if (mc_stat(dir
, &info1
) != 0)
311 callback(data
, NULL
);
317 if (mc_stat(make_path(home_dir
, ".thumbnails/normal"),
319 info1
.st_dev
== info2
.st_dev
&&
320 info1
.st_ino
== info2
.st_ino
)
322 pixbuf
= rox_pixbuf_new_from_file_at_scale(path
,
323 PIXMAP_THUMB_SIZE
, PIXMAP_THUMB_SIZE
, TRUE
, NULL
);
326 g_fscache_insert(pixmap_cache
,
328 callback(data
, NULL
);
338 image
= masked_pixmap_new(pixbuf
);
339 gdk_pixbuf_unref(pixbuf
);
340 g_fscache_insert(pixmap_cache
, path
, image
, TRUE
);
341 callback(data
, (gchar
*) path
);
342 g_object_unref(G_OBJECT(image
));
346 type
= type_from_path(path
);
350 /* Add an entry, set to NULL, so no-one else tries to load this
353 g_fscache_insert(pixmap_cache
, path
, NULL
, TRUE
);
355 thumb_prog
= thumbnail_program(type
);
357 /* Only attempt to load 'images' types ourselves */
358 if (thumb_prog
== NULL
&& strcmp(type
->media_type
, "image") != 0)
360 callback(data
, NULL
);
361 return; /* Don't know how to handle this type */
369 delayed_error("fork(): %s", g_strerror(errno
));
370 callback(data
, NULL
);
376 /* We are the child process. (We are sloppy with freeing
377 memory, but since we go away very quickly, that's ok.) */
382 item
= diritem_new(g_basename(thumb_prog
));
384 diritem_restat(thumb_prog
, item
, NULL
);
385 if (item
->flags
& ITEM_FLAG_APPDIR
)
386 thumb_prog
= g_strconcat(thumb_prog
, "/AppRun",
389 execl(thumb_prog
, thumb_prog
, path
,
390 thumbnail_path(path
),
391 g_strdup_printf("%d", PIXMAP_THUMB_SIZE
),
396 child_create_thumbnail(path
, type
);
402 info
= g_new(ChildThumbnail
, 1);
403 info
->path
= g_strdup(path
);
404 info
->callback
= callback
;
406 on_child_death(child
, (CallbackFn
) thumbnail_child_done
, info
);
409 /****************************************************************
410 * INTERNAL FUNCTIONS *
411 ****************************************************************/
413 /* Create a thumbnail file for this image */
414 static void save_thumbnail(const char *pathname
, GdkPixbuf
*full
)
418 int original_width
, original_height
;
420 char *md5
, *swidth
, *sheight
, *ssize
, *smtime
, *uri
;
425 thumb
= scale_pixbuf(full
, PIXMAP_THUMB_SIZE
, PIXMAP_THUMB_SIZE
);
427 original_width
= gdk_pixbuf_get_width(full
);
428 original_height
= gdk_pixbuf_get_height(full
);
430 if (mc_stat(pathname
, &info
) != 0)
433 swidth
= g_strdup_printf("%d", original_width
);
434 sheight
= g_strdup_printf("%d", original_height
);
435 ssize
= g_strdup_printf("%" SIZE_FMT
, info
.st_size
);
436 smtime
= g_strdup_printf("%ld", (long) info
.st_mtime
);
438 path
= pathdup(pathname
);
439 uri
= g_filename_to_uri(path
, NULL
, NULL
);
441 uri
= g_strconcat("file://", path
, NULL
);
445 to
= g_string_new(home_dir
);
446 g_string_append(to
, "/.thumbnails");
447 mkdir(to
->str
, 0700);
448 g_string_append(to
, "/normal/");
449 mkdir(to
->str
, 0700);
450 g_string_append(to
, md5
);
451 name_len
= to
->len
+ 4; /* Truncate to this length when renaming */
452 g_string_append_printf(to
, ".png.ROX-Filer-%ld", (long) getpid());
456 old_mask
= umask(0077);
457 gdk_pixbuf_save(thumb
, to
->str
, "png", NULL
,
458 "tEXt::Thumb::Image::Width", swidth
,
459 "tEXt::Thumb::Image::Height", sheight
,
460 "tEXt::Thumb::Size", ssize
,
461 "tEXt::Thumb::MTime", smtime
,
462 "tEXt::Thumb::URI", uri
,
463 "tEXt::Software", PROJECT
,
467 /* We create the file ###.png.ROX-Filer-PID and rename it to avoid
468 * a race condition if two programs create the same thumb at
474 final
= g_strndup(to
->str
, name_len
);
475 if (rename(to
->str
, final
))
476 g_warning("Failed to rename '%s' to '%s': %s",
477 to
->str
, final
, g_strerror(errno
));
481 g_string_free(to
, TRUE
);
489 static gchar
*thumbnail_path(const char *path
)
495 uri
= g_filename_to_uri(path
, NULL
, NULL
);
497 uri
= g_strconcat("file://", path
, NULL
);
500 to
= g_string_new(home_dir
);
501 g_string_append(to
, "/.thumbnails");
502 mkdir(to
->str
, 0700);
503 g_string_append(to
, "/normal/");
504 mkdir(to
->str
, 0700);
505 g_string_append(to
, md5
);
506 g_string_append(to
, ".png");
512 g_string_free(to
, FALSE
);
517 /* Return a program to create thumbnails for files of this type.
518 * NULL to try to make it ourself (using gdk).
521 static gchar
*thumbnail_program(MIME_type
*type
)
529 leaf
= g_strconcat(type
->media_type
, "_", type
->subtype
, NULL
);
530 path
= choices_find_xdg_path_load(leaf
, "MIME-thumb", SITE
);
537 path
= choices_find_xdg_path_load(type
->media_type
, "MIME-thumb",
543 /* Called in a subprocess. Load path and create the thumbnail
544 * file. Parent will notice when we die.
546 static void child_create_thumbnail(const gchar
*path
, MIME_type
*type
)
548 GdkPixbuf
*image
=NULL
;
550 if(strcmp(type
->subtype
, "jpeg")==0)
551 image
=extract_tiff_thumbnail(path
);
554 image
= rox_pixbuf_new_from_file_at_scale(path
,
555 PIXMAP_THUMB_SIZE
, PIXMAP_THUMB_SIZE
, TRUE
, NULL
);
558 save_thumbnail(path
, image
);
560 /* (no need to unref, as we're about to exit) */
563 /* Called when the child process exits */
564 static void thumbnail_child_done(ChildThumbnail
*info
)
568 thumb
= get_thumbnail_for(info
->path
);
574 image
= masked_pixmap_new(thumb
);
575 g_object_unref(thumb
);
577 g_fscache_insert(pixmap_cache
, info
->path
, image
, FALSE
);
578 g_object_unref(image
);
580 info
->callback(info
->data
, info
->path
);
583 info
->callback(info
->data
, NULL
);
589 /* Check if we have an up-to-date thumbnail for this image.
590 * If so, return it. Otherwise, returns NULL.
592 static GdkPixbuf
*get_thumbnail_for(const char *pathname
)
594 GdkPixbuf
*thumb
= NULL
;
595 char *thumb_path
, *md5
, *uri
, *path
;
596 const char *ssize
, *smtime
;
600 path
= pathdup(pathname
);
601 uri
= g_filename_to_uri(path
, NULL
, NULL
);
603 uri
= g_strconcat("file://", path
, NULL
);
607 thumb_path
= g_strdup_printf("%s/.thumbnails/normal/%s.png",
611 thumb
= gdk_pixbuf_new_from_file(thumb_path
, NULL
);
615 /* Note that these don't need freeing... */
616 ssize
= gdk_pixbuf_get_option(thumb
, "tEXt::Thumb::Size");
617 /* This is optional, so don't flag an error if it is missing */
619 smtime
= gdk_pixbuf_get_option(thumb
, "tEXt::Thumb::MTime");
623 if (mc_stat(path
, &info
) != 0)
626 ttime
=(time_t) atol(smtime
);
628 if (info
.st_mtime
!= ttime
&& now
>ttime
+PIXMAP_THUMB_TOO_OLD_TIME
)
631 if (ssize
&& info
.st_size
< atol(ssize
))
637 gdk_pixbuf_unref(thumb
);
645 /* Load the image 'path' and return a pointer to the resulting
646 * MaskedPixmap. NULL on failure.
647 * Doesn't check for thumbnails (this is for small icons).
649 static MaskedPixmap
*image_from_file(const char *path
)
653 GError
*error
= NULL
;
655 pixbuf
= gdk_pixbuf_new_from_file(path
, &error
);
658 g_warning("%s\n", error
->message
);
663 image
= masked_pixmap_new(pixbuf
);
665 gdk_pixbuf_unref(pixbuf
);
670 /* Load this icon named by this .desktop file from the current theme.
673 static MaskedPixmap
*image_from_desktop_file(const char *path
)
675 GError
*error
= NULL
;
676 MaskedPixmap
*image
= NULL
;
679 icon
= get_value_from_desktop_file(path
,
680 "Desktop Entry", "Icon", &error
);
683 g_warning("Failed to parse .desktop file '%s':\n%s",
684 path
, error
->message
);
691 image
= image_from_file(icon
);
698 /* For some unknown reason, some icon names have extensions.
701 extension
= strrchr(icon
, '.');
702 if (extension
&& (strcmp(extension
, ".png") == 0
703 || strcmp(extension
, ".xpm") == 0
704 || strcmp(extension
, ".svg") == 0))
709 /* SVG reader is very noisy, so redirect stderr to stdout */
712 pixbuf
= theme_load_icon(icon
, HUGE_WIDTH
, 0, NULL
);
717 goto err
; /* Might just not be in the theme */
719 image
= masked_pixmap_new(pixbuf
);
720 g_object_unref(pixbuf
);
730 /* Scale src down to fit in max_w, max_h and return the new pixbuf.
731 * If src is small enough, then ref it and return that.
733 GdkPixbuf
*scale_pixbuf(GdkPixbuf
*src
, int max_w
, int max_h
)
737 w
= gdk_pixbuf_get_width(src
);
738 h
= gdk_pixbuf_get_height(src
);
740 if (w
<= max_w
&& h
<= max_h
)
747 float scale_x
= ((float) w
) / max_w
;
748 float scale_y
= ((float) h
) / max_h
;
749 float scale
= MAX(scale_x
, scale_y
);
750 int dest_w
= w
/ scale
;
751 int dest_h
= h
/ scale
;
753 return gdk_pixbuf_scale_simple(src
,
756 GDK_INTERP_BILINEAR
);
760 /* Scale src up to fit in max_w, max_h and return the new pixbuf.
761 * If src is that size or bigger, then ref it and return that.
763 static GdkPixbuf
*scale_pixbuf_up(GdkPixbuf
*src
, int max_w
, int max_h
)
767 w
= gdk_pixbuf_get_width(src
);
768 h
= gdk_pixbuf_get_height(src
);
770 if (w
== 0 || h
== 0 || w
>= max_w
|| h
>= max_h
)
777 float scale_x
= max_w
/ ((float) w
);
778 float scale_y
= max_h
/ ((float) h
);
779 float scale
= MIN(scale_x
, scale_y
);
781 return gdk_pixbuf_scale_simple(src
,
784 GDK_INTERP_BILINEAR
);
788 /* Return a pointer to the (static) bad image. The ref counter will ensure
789 * that the image is never freed.
791 static MaskedPixmap
*get_bad_image(void)
796 bad
= gdk_pixbuf_new_from_xpm_data(bad_xpm
);
797 mp
= masked_pixmap_new(bad
);
798 gdk_pixbuf_unref(bad
);
803 /* Called now and then to clear out old pixmaps */
804 static gint
purge(gpointer data
)
806 g_fscache_purge(pixmap_cache
, PIXMAP_PURGE_TIME
);
811 static gpointer parent_class
;
813 static void masked_pixmap_finialize(GObject
*object
)
815 MaskedPixmap
*mp
= (MaskedPixmap
*) object
;
819 g_object_unref(mp
->src_pixbuf
);
820 mp
->src_pixbuf
= NULL
;
825 g_object_unref(mp
->huge_pixbuf
);
826 mp
->huge_pixbuf
= NULL
;
830 g_object_unref(mp
->pixbuf
);
836 g_object_unref(mp
->sm_pixbuf
);
837 mp
->sm_pixbuf
= NULL
;
840 G_OBJECT_CLASS(parent_class
)->finalize(object
);
843 static void masked_pixmap_class_init(gpointer gclass
, gpointer data
)
845 GObjectClass
*object
= (GObjectClass
*) gclass
;
847 parent_class
= g_type_class_peek_parent(gclass
);
849 object
->finalize
= masked_pixmap_finialize
;
852 static void masked_pixmap_init(GTypeInstance
*object
, gpointer gclass
)
854 MaskedPixmap
*mp
= (MaskedPixmap
*) object
;
856 mp
->src_pixbuf
= NULL
;
858 mp
->huge_pixbuf
= NULL
;
860 mp
->huge_height
= -1;
866 mp
->sm_pixbuf
= NULL
;
871 static GType
masked_pixmap_get_type(void)
873 static GType type
= 0;
877 static const GTypeInfo info
=
879 sizeof (MaskedPixmapClass
),
880 NULL
, /* base_init */
881 NULL
, /* base_finalise */
882 masked_pixmap_class_init
,
883 NULL
, /* class_finalise */
884 NULL
, /* class_data */
885 sizeof(MaskedPixmap
),
890 type
= g_type_register_static(G_TYPE_OBJECT
, "MaskedPixmap",
897 MaskedPixmap
*masked_pixmap_new(GdkPixbuf
*full_size
)
900 GdkPixbuf
*src_pixbuf
, *normal_pixbuf
;
902 g_return_val_if_fail(full_size
!= NULL
, NULL
);
904 src_pixbuf
= scale_pixbuf(full_size
, HUGE_WIDTH
, HUGE_HEIGHT
);
905 g_return_val_if_fail(src_pixbuf
!= NULL
, NULL
);
907 normal_pixbuf
= scale_pixbuf(src_pixbuf
, ICON_WIDTH
, ICON_HEIGHT
);
908 g_return_val_if_fail(normal_pixbuf
!= NULL
, NULL
);
910 mp
= g_object_new(masked_pixmap_get_type(), NULL
);
912 mp
->src_pixbuf
= src_pixbuf
;
914 mp
->pixbuf
= normal_pixbuf
;
915 mp
->width
= gdk_pixbuf_get_width(normal_pixbuf
);
916 mp
->height
= gdk_pixbuf_get_height(normal_pixbuf
);
921 /* Load all the standard pixmaps. Also sets the default window icon. */
922 static void load_default_pixmaps(void)
925 GError
*error
= NULL
;
927 im_error
= mp_from_stock(GTK_STOCK_DIALOG_WARNING
,
928 GTK_ICON_SIZE_DIALOG
);
929 im_unknown
= mp_from_stock(GTK_STOCK_DIALOG_QUESTION
,
930 GTK_ICON_SIZE_DIALOG
);
932 im_dirs
= load_pixmap("dirs");
933 im_appdir
= load_pixmap("application");
935 pixbuf
= gdk_pixbuf_new_from_file(
936 make_path(app_dir
, ".DirIcon"), &error
);
941 icon_list
= g_list_append(NULL
, pixbuf
);
942 gtk_window_set_default_icon_list(icon_list
);
943 g_list_free(icon_list
);
945 g_object_unref(G_OBJECT(pixbuf
));
949 g_warning("%s\n", error
->message
);
954 /* Also purges memory cache */
955 static void purge_disk_cache(GtkWidget
*button
, gpointer data
)
962 g_fscache_purge(pixmap_cache
, 0);
964 path
= g_strconcat(home_dir
, "/.thumbnails/normal/", NULL
);
969 report_error(_("Can't delete thumbnails in %s:\n%s"),
970 path
, g_strerror(errno
));
974 while ((ent
= readdir(dir
)))
976 if (ent
->d_name
[0] == '.')
978 list
= g_list_prepend(list
,
979 g_strconcat(path
, ent
->d_name
, NULL
));
987 destroy_glist(&list
);
990 info_message(_("There are no thumbnails to delete"));
995 static GList
*thumbs_purge_cache(Option
*option
, xmlNode
*node
, guchar
*label
)
997 GtkWidget
*button
, *align
;
999 g_return_val_if_fail(option
== NULL
, NULL
);
1001 align
= gtk_alignment_new(0, 0.5, 0, 0);
1002 button
= button_new_mixed(GTK_STOCK_CLEAR
,
1003 _("Purge thumbnails disk cache"));
1004 gtk_container_add(GTK_CONTAINER(align
), button
);
1005 g_signal_connect(button
, "clicked", G_CALLBACK(purge_disk_cache
), NULL
);
1007 return g_list_append(NULL
, align
);
1011 * Based on Thierry Bousch's public domain exifdump.py.
1014 #define JPEG_FORMAT 0x201
1015 #define JPEG_FORMAT_LENGTH 0x202
1018 * Extract n-byte integer in Motorola (big-endian) format
1020 static inline long long s2n_motorola(const unsigned char *p
, int len
)
1025 for(i
=0; i
<len
; i
++)
1026 a
=(a
<<8) | (int)(p
[i
]);
1032 * Extract n-byte integer in Intel (little-endian) format
1034 static inline long long s2n_intel(const unsigned char *p
, int len
)
1039 for(i
=0; i
<len
; i
++)
1040 a
=a
| (((int) p
[i
]) << (i
*8));
1046 * Extract n-byte integer from data
1048 static int s2n(const unsigned char *dat
, int off
, int len
, char format
)
1050 const unsigned char *p
=dat
+off
;
1054 return s2n_intel(p
, len
);
1057 return s2n_motorola(p
, len
);
1064 * Load header of JPEG/Exif file and attempt to extract the embedded
1065 * thumbnail. Return NULL on failure.
1067 static GdkPixbuf
*extract_tiff_thumbnail(const gchar
*path
)
1070 unsigned char header
[256];
1073 unsigned char *data
;
1076 int thumb
=0, tlength
=0;
1077 GdkPixbuf
*buf
=NULL
;
1079 in
=fopen(path
, "rb");
1084 /* Check for Exif format */
1085 n
=fread(header
, 1, 12, in
);
1086 if(n
!=12 || strncmp((char *) header
, "\377\330\377\341", 4)!=0 ||
1087 strncmp((char *)header
+6, "Exif", 4)!=0) {
1093 length
=header
[4]*256+header
[5];
1094 data
=g_new(unsigned char, length
);
1095 n
=fread(data
, 1, length
, in
);
1096 fclose(in
); /* File no longer needed */
1102 /* Big or little endian (as 'M' or 'I') */
1105 /* Skip over main section */
1106 ifd
=s2n(data
, 4, 4, format
);
1107 entries
=s2n(data
, ifd
, 2, format
);
1109 /* Second section contains data on thumbnail */
1110 ifd
=s2n(data
, ifd
+2+12*entries
, 4, format
);
1111 entries
=s2n(data
, ifd
, 2, format
);
1113 /* Loop over the entries */
1114 for(i
=0; i
<entries
; i
++) {
1115 int entry
=ifd
+2+12*i
;
1116 int tag
=s2n(data
, entry
, 2, format
);
1117 int type
=s2n(data
, entry
+2, 2, format
);
1120 count
=s2n(data
, entry
+4, 4, format
);
1124 int val
=(int) s2n(data
, offset
, 4, format
);
1126 /* Only interested in two entries, the location of the thumbnail
1129 case JPEG_FORMAT
: thumb
=val
; break;
1130 case JPEG_FORMAT_LENGTH
: tlength
=val
; break;
1135 if(thumb
&& tlength
) {
1137 GdkPixbufLoader
*loader
;
1139 /* Don't read outside the header (some files have incorrect data) */
1140 if(thumb
+tlength
>length
)
1141 tlength
=length
-thumb
;
1143 loader
=gdk_pixbuf_loader_new();
1144 gdk_pixbuf_loader_write(loader
, data
+thumb
, tlength
, &err
);
1150 gdk_pixbuf_loader_close(loader
, &err
);
1156 buf
=gdk_pixbuf_loader_get_pixbuf(loader
);
1157 g_object_ref(buf
); /* Ref the image before we unref the loader */
1158 g_object_unref(loader
);