added pixbuf_fallback function
authorVladimir Nadvornik <nadvornik@suse.cz>
Fri, 27 Jun 2008 21:09:15 +0000 (21:09 +0000)
committerVladimir Nadvornik <nadvornik@suse.cz>
Fri, 27 Jun 2008 21:09:15 +0000 (21:09 +0000)
fixed thumb loader for non-image files

src/collect-io.c
src/dupe.c
src/pan-view.c
src/pixbuf_util.c
src/pixbuf_util.h
src/thumb.c
src/thumb.h
src/thumb_standard.c
src/thumb_standard.h

index 715b4e0..bb40a25 100644 (file)
@@ -223,7 +223,7 @@ static void collection_load_thumb_do(CollectionData *cd)
 
        if (!cd->thumb_loader || !g_list_find(cd->list, cd->thumb_info)) return;
 
-       pixbuf = thumb_loader_get_pixbuf(cd->thumb_loader, TRUE);
+       pixbuf = thumb_loader_get_pixbuf(cd->thumb_loader);
        collection_info_set_thumb(cd->thumb_info, pixbuf);
        g_object_unref(pixbuf);
 
index 2b52175..994ccef 100644 (file)
@@ -1264,7 +1264,7 @@ static void dupe_thumb_do(DupeWindow *dw)
        di = dw->thumb_item;
 
        if (di->pixbuf) g_object_unref(di->pixbuf);
-       di->pixbuf = thumb_loader_get_pixbuf(dw->thumb_loader, TRUE);
+       di->pixbuf = thumb_loader_get_pixbuf(dw->thumb_loader);
 
        dupe_listview_set_thumb(dw, di, NULL);
 }
index d29c472..dd6f1c9 100644 (file)
@@ -94,7 +94,7 @@ static void pan_queue_thumb_done_cb(ThumbLoader *tl, gpointer data)
                pi->queued = FALSE;
 
                if (pi->pixbuf) g_object_unref(pi->pixbuf);
-               pi->pixbuf = thumb_loader_get_pixbuf(tl, TRUE);
+               pi->pixbuf = thumb_loader_get_pixbuf(tl);
 
                rc = pi->refcount;
                image_area_changed(pw->imd, pi->x, pi->y, pi->width, pi->height);
index 8479850..fb2f1e9 100644 (file)
@@ -131,6 +131,53 @@ GdkPixbuf *pixbuf_inline(const gchar *key)
        return NULL;
 }
 
+gint pixbuf_scale_aspect(gint req_w, gint req_h, gint old_w, gint old_h,
+                                         gint *new_w, gint *new_h)
+{
+       if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
+               {
+               *new_w = req_w;
+               *new_h = (gdouble)*new_w / old_w * old_h;
+               if (*new_h < 1) *new_h = 1;
+               }
+       else
+               {
+               *new_h = req_h;
+               *new_w = (gdouble)*new_h / old_h * old_w;
+               if (*new_w < 1) *new_w = 1;
+               }
+
+       return (*new_w != old_w || *new_h != old_h);
+}
+
+GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
+{
+       GdkPixbuf *pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN); /* FIXME use different images according to FORMAT_CLASS */
+       
+       if (requested_width && requested_height)
+               {
+               gint w = gdk_pixbuf_get_width(pixbuf);
+               gint h = gdk_pixbuf_get_height(pixbuf);
+
+               if (w > requested_width || h > requested_height)
+                       {
+                       gint nw, nh;
+
+                       if (pixbuf_scale_aspect(requested_width, requested_height,
+                                                         w, h, &nw, &nh))
+                               {
+                               GdkPixbuf *tmp;
+
+                               tmp = pixbuf;
+                               pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
+                               g_object_unref(G_OBJECT(tmp));
+                               }
+                       }
+               }
+       return pixbuf;
+}
+
+
 /*
  *-----------------------------------------------------------------------------
  * misc utils
index 2535732..e483b53 100644 (file)
@@ -20,6 +20,9 @@ gboolean pixbuf_to_file_as_jpg(GdkPixbuf *pixbuf, const gchar *filename, gint qu
 
 
 GdkPixbuf *pixbuf_inline(const gchar *key);
+GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height);
+
+gint pixbuf_scale_aspect(gint req_w, gint req_h, gint old_w, gint old_h, gint *new_w, gint *new_h);
 
 #define PIXBUF_INLINE_FOLDER_CLOSED    "folder_closed"
 #define PIXBUF_INLINE_FOLDER_LOCKED    "folder_locked"
index bba2f78..d636203 100644 (file)
@@ -362,6 +362,9 @@ gint thumb_loader_start(ThumbLoader *tl, FileData *fd)
 
        if (!tl->fd) tl->fd = file_data_ref(fd);
 
+       if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
+       tl->fd->thumb_pixbuf = pixbuf_fallback(tl->fd, tl->max_w, tl->max_h);
+
        if (tl->cache_enable)
                {
                cache_path = cache_find_location(CACHE_TYPE_THUMB, tl->fd->path);
@@ -447,13 +450,13 @@ gint thumb_loader_to_pixmap(ThumbLoader *tl, GdkPixmap **pixmap, GdkBitmap **mas
 }
 #endif
 
-GdkPixbuf *thumb_loader_get_pixbuf(ThumbLoader *tl, gint with_fallback)
+GdkPixbuf *thumb_loader_get_pixbuf(ThumbLoader *tl)
 {
        GdkPixbuf *pixbuf;
 
        if (tl && tl->standard_loader)
                {
-               return thumb_loader_std_get_pixbuf((ThumbLoaderStd *)tl, with_fallback);
+               return thumb_loader_std_get_pixbuf((ThumbLoaderStd *)tl);
                }
 
        if (tl && tl->fd && tl->fd->thumb_pixbuf)
@@ -461,26 +464,9 @@ GdkPixbuf *thumb_loader_get_pixbuf(ThumbLoader *tl, gint with_fallback)
                pixbuf = tl->fd->thumb_pixbuf;
                g_object_ref(pixbuf);
                }
-       else if (with_fallback)
-               {
-               gint w, h;
-
-               pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
-               w = gdk_pixbuf_get_width(pixbuf);
-               h = gdk_pixbuf_get_height(pixbuf);
-               if ((w > tl->max_w || h > tl->max_h) &&
-                   normalize_thumb(&w, &h, tl->max_w, tl->max_h))
-                       {
-                       GdkPixbuf *tmp;
-
-                       tmp = pixbuf;
-                       pixbuf = gdk_pixbuf_scale_simple(tmp, w, h, GDK_INTERP_NEAREST);
-                       gdk_pixbuf_unref(tmp);
-                       }
-               }
        else
                {
-               pixbuf = NULL;
+               pixbuf = pixbuf_fallback(NULL, tl->max_w, tl->max_h);
                }
 
        return pixbuf;
@@ -550,8 +536,7 @@ gint thumb_from_xpm_d(const char **data, gint max_w, gint max_h, GdkPixmap **pix
        w = gdk_pixbuf_get_width(pixbuf);
        h = gdk_pixbuf_get_height(pixbuf);
 
-       if ((w > max_w || h > max_h) &&
-           normalize_thumb(&w, &h, max_w, max_h))
+       if (pixbuf_scale_aspect(w, h, max_w, max_h, &w, &h))
                {
                /* scale */
                GdkPixbuf *tmp;
@@ -619,25 +604,6 @@ static guchar *load_xv_thumbnail(gchar *filename, gint *widthp, gint *heightp)
 }
 #undef XV_BUFFER
 
-static gint normalize_thumb(gint *width, gint *height, gint max_w, gint max_h)
-{
-       gdouble scale;
-       gint new_w, new_h;
-
-       scale = MIN((gdouble) max_w / *width, (gdouble) max_h / *height);
-       new_w = *width * scale;
-       new_h = *height * scale;
-
-       if (new_w != *width || new_h != *height)
-               {
-               *width = new_w;
-               *height = new_h;
-               return TRUE;
-               }
-
-       return FALSE;
-}
-
 static void free_rgb_buffer(guchar *pixels, gpointer data)
 {
        g_free(pixels);
@@ -683,7 +649,7 @@ static GdkPixbuf *get_xv_thumbnail(gchar *thumb_filename, gint max_w, gint max_h
                pixbuf = gdk_pixbuf_new_from_data(rgb_data, GDK_COLORSPACE_RGB, FALSE, 8,
                                                  width, height, 3 * width, free_rgb_buffer, NULL);
 
-               if (normalize_thumb(&width, &height, max_w, max_h))
+               if (pixbuf_scale_aspect(width, height, max_w, max_h, &width, &height))
                        {
                        /* scale */
                        GdkPixbuf *tmp;
index d09152f..614efad 100644 (file)
@@ -26,7 +26,7 @@ void thumb_loader_set_cache(ThumbLoader *tl, gint enable_cache, gint local, gint
 gint thumb_loader_start(ThumbLoader *tl, FileData *fd);
 void thumb_loader_free(ThumbLoader *tl);
 
-GdkPixbuf *thumb_loader_get_pixbuf(ThumbLoader *tl, gint with_fallback);
+GdkPixbuf *thumb_loader_get_pixbuf(ThumbLoader *tl);
 
 
 #endif
index 6c62814..f65eb63 100644 (file)
@@ -376,25 +376,6 @@ static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
        g_object_unref(G_OBJECT(pixbuf));
 }
 
-static gint thumb_loader_std_scale_aspect(gint req_w, gint req_h, gint old_w, gint old_h,
-                                         gint *new_w, gint *new_h)
-{
-       if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
-               {
-               *new_w = req_w;
-               *new_h = (gdouble)*new_w / old_w * old_h;
-               if (*new_h < 1) *new_h = 1;
-               }
-       else
-               {
-               *new_h = req_h;
-               *new_w = (gdouble)*new_h / old_h * old_w;
-               if (*new_w < 1) *new_w = 1;
-               }
-
-       return (*new_w != old_w || *new_h != old_h);
-}
-
 static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf, gint shrunk)
 {
        GdkPixbuf *pixbuf_thumb = NULL;
@@ -446,7 +427,7 @@ static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf,
                                {
                                gint thumb_w, thumb_h;
 
-                               if (thumb_loader_std_scale_aspect(cache_w, cache_h, sw, sh,
+                               if (pixbuf_scale_aspect(cache_w, cache_h, sw, sh,
                                                                  &thumb_w, &thumb_h))
                                        {
                                        pixbuf_thumb = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
@@ -494,7 +475,7 @@ static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf,
                        sh = gdk_pixbuf_get_height(pixbuf);
                        }
 
-               if (thumb_loader_std_scale_aspect(tl->requested_width, tl->requested_height, sw, sh,
+               if (pixbuf_scale_aspect(tl->requested_width, tl->requested_height, sw, sh,
                                                  &thumb_w, &thumb_h))
                        {
                        result = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
@@ -667,6 +648,10 @@ gint thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd)
        if (!tl || !fd) return FALSE;
 
        thumb_loader_std_reset(tl);
+       
+       if (fd->thumb_pixbuf) g_object_unref(fd->thumb_pixbuf);
+       fd->thumb_pixbuf = pixbuf_fallback(fd, tl->requested_width, tl->requested_height);
+
 
        if (!stat_utf8(fd->path, &st)) return FALSE;
 
@@ -718,7 +703,7 @@ void thumb_loader_std_free(ThumbLoaderStd *tl)
        g_free(tl);
 }
 
-GdkPixbuf *thumb_loader_std_get_pixbuf(ThumbLoaderStd *tl, gint with_fallback)
+GdkPixbuf *thumb_loader_std_get_pixbuf(ThumbLoaderStd *tl)
 {
        GdkPixbuf *pixbuf;
 
@@ -727,32 +712,9 @@ GdkPixbuf *thumb_loader_std_get_pixbuf(ThumbLoaderStd *tl, gint with_fallback)
                pixbuf = tl->fd->thumb_pixbuf;
                g_object_ref(pixbuf);
                }
-       else if (with_fallback)
-               {
-               gint w, h;
-
-               pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
-               w = gdk_pixbuf_get_width(pixbuf);
-               h = gdk_pixbuf_get_height(pixbuf);
-
-               if (w > tl->requested_width || h > tl->requested_height)
-                       {
-                       gint nw, nh;
-
-                       if (thumb_loader_std_scale_aspect(tl->requested_width, tl->requested_height,
-                                                         w, h, &nw, &nh))
-                               {
-                               GdkPixbuf *tmp;
-
-                               tmp = pixbuf;
-                               pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
-                               g_object_unref(G_OBJECT(tmp));
-                               }
-                       }
-               }
        else
                {
-               pixbuf = NULL;
+               pixbuf = pixbuf_fallback(NULL, tl->requested_width, tl->requested_height);
                }
 
        return pixbuf;
@@ -806,7 +768,8 @@ static void thumb_loader_std_thumb_file_validate_done_cb(ThumbLoaderStd *tl, gpo
        GdkPixbuf *pixbuf;
        gint valid = FALSE;
 
-       pixbuf = thumb_loader_std_get_pixbuf(tv->tl, FALSE);
+       /* this function is called on success, so the pixbuf should not be a fallback*/
+       pixbuf = thumb_loader_std_get_pixbuf(tv->tl);
        if (pixbuf)
                {
                const gchar *uri;
@@ -967,7 +930,8 @@ static void thumb_std_maint_move_validate_cb(const gchar *path, gint valid, gpoi
        TMaintMove *tm = data;
        GdkPixbuf *pixbuf;
 
-       pixbuf = thumb_loader_std_get_pixbuf(tm->tl, FALSE);
+       /* this function is called on success, so the pixbuf should not be a fallback*/
+       pixbuf = thumb_loader_std_get_pixbuf(tm->tl);
        if (pixbuf)
                {
                const gchar *uri;
@@ -1010,6 +974,7 @@ static void thumb_std_maint_move_validate_cb(const gchar *path, gint valid, gpoi
 
                DEBUG_1("thumb move unlink: %s", tm->thumb_path);
                unlink_file(tm->thumb_path);
+               g_object_unref(pixbuf);
                }
 
        thumb_std_maint_move_step(tm);
index 898eac9..648c827 100644 (file)
@@ -71,7 +71,7 @@ void thumb_loader_std_set_cache(ThumbLoaderStd *tl, gint enable_cache, gint loca
 gint thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd);
 void thumb_loader_std_free(ThumbLoaderStd *tl);
 
-GdkPixbuf *thumb_loader_std_get_pixbuf(ThumbLoaderStd *tl, gint with_fallback);
+GdkPixbuf *thumb_loader_std_get_pixbuf(ThumbLoaderStd *tl);
 
 
 /* validates a non local thumbnail file,