1

我等待将图像从内存(jpeg 和 tiff)加载到 gdk_pixbuf_loader_write()。我有示例/测试代码可以很好地加载 jpeg 但无法显示 tiff 图像。tiff 图像可以在其他查​​看器(gnome 之眼等)中正常打开,因此 tiff 有效;我还使用 imagemagick 从 jpeg 创建 tiff,这些也无法在我的示例代码中加载/显示。

使用 tiff 图像在我的示例代码上运行 strace,我可以看到 gdkpixbuf 加载程序打开正常:

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;
}
4

1 回答 1

0

I'm developing a sample demonstrating application for our barcode scanner driver. So I also encounter this issue. I managed to solve this issue, by closing the pixbuffer before calling 'gdk_pixbuf_loader_get_pixbuf'

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

also it's a good idea to check if (pixbuf == NULL ){to avoid crashes with null pointers.

In 'tiff' you need to call gdk_pixbuf_loader_close otherwise it will not work. And when you close it, you can't use the same loader again.

于 2016-10-26T06:20:46.900 回答