2016-02-07 185 views
1

我等待將圖像從內存(jpegs和tiffs)加載到gdk_pixbuf_loader_write()中。我有加載jpeg的樣本/測試代碼很好,但無法顯示tiff圖像。 tiff圖像與其他觀衆(gnome等的眼睛)打開很好,所以tiff是有效的;我也使用imagemagick從jpegs創建tiff,這些也無法加載/顯示在我的示例代碼中。無法使用gdk_pixbuf_loader加載TIFF圖像

在我的示例代碼在後臺運行strace與TIFF圖像,我可以看到gdkpixbuf裝載機OK打開:

open("/usr/lib64/gdk-pixbuf-2.0/2.10.0/loaders.cache", O_RDONLY) = 7 
... 
open("/usr/lib64/gdk-pixbuf-2.0/2.10.0/loaders/libpixbufloader-tiff.so", O_RDONLY|O_CLOEXEC) = 7 

有沒有什麼具體的,我需要用TIFF文件的時候處理?樣本測試程序如下。

#include <sys/types.h> 
#include <sys/stat.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#include <glib.h> 
#include <gdk-pixbuf/gdk-pixbuf.h> 
#include <gtk/gtk.h> 

void 
on_destroy (GtkWidget * widget G_GNUC_UNUSED, 
     gpointer user_data G_GNUC_UNUSED) 
{ 
    gtk_main_quit(); 
} 

int 
main (int argc, char *argv[]) 
{ 
    FILE *f; 
    struct stat st; 

    guint8* buffer = NULL; 
    gsize length = 0; 
    GdkPixbufLoader *loader = NULL; 
    GdkPixbuf *pixbuf; 
    GtkWidget *window; 
    GtkWidget *image; 
    GError** err_ = NULL; 

    if (argc == 1) { 
     printf("no files\n"); 
    exit(1); 
    } 
    memset(&st, sizeof(st), 0); 
    stat(argv[1], &st); 
    if (st.st_size == 0) { 
     printf("%s is empty\n", argv[1]); 
    exit(1); 
    } 

    buffer = malloc(st.st_size); 
    memset(buffer, 0, st.st_size); 

    gtk_init (&argc, &argv); 

    f = fopen (argv[1], "rb"); 
    if ((length = fread (buffer, 1, st.st_size, f)) != st.st_size) { 
     printf("failed to read all data from %s\n", argv[1]); 
    exit(1); 
    } 
    fclose (f); 

    if (argc == 3) { 
     printf("requesting loader for mimetype=%s on %s\n", argv[2], argv[1]); 
     loader = gdk_pixbuf_loader_new_with_mime_type(argv[2], err_); 
     if (err_ && *err_) { 
      g_error_free(*err_); 
      *err_ = NULL; 
      printf("no known loader for explicit mimetype, defaulting\n"); 
      loader = gdk_pixbuf_loader_new(); 
     } 
    } 
    else 
    { 
     loader = gdk_pixbuf_loader_new(); 
    } 
    gdk_pixbuf_loader_write (loader, buffer, length, NULL); 
    pixbuf = gdk_pixbuf_loader_get_pixbuf (loader); 
    gdk_pixbuf_loader_close(loader, err_); 

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL); 
    image = gtk_image_new_from_pixbuf (pixbuf); 
    gtk_container_add (GTK_CONTAINER (window), image); 
    gtk_widget_show_all (GTK_WIDGET (window)); 
    g_signal_connect (window, "destroy", G_CALLBACK (on_destroy), NULL); 
    gtk_main(); 

    free(buffer); 
    return 0; 
} 

回答

0

我正在爲我們的條形碼掃描儀驅動程序開發示例演示應用程序。所以我也遇到這個問題。我叫「gdk_pixbuf_loader_get_pixbuf」

gdk_pixbuf_loader_close(loader,&error); 
GdkPixbuf* pixbuf = gdk_pixbuf_loader_get_pixbuf(loader); 

也很檢查if (pixbuf == NULL){以避免空指針的崩潰是一個好主意之前設法解決這個問題,通過關閉pixbuffer。

在'tiff'中,您需要調用gdk_pixbuf_loader_close,否則它將無法工作。 當你關閉它時,你不能再使用同一個裝載器。