Blame tests/pixbuf-short-gif-write.c

Packit a4058c
#include "config.h"
Packit a4058c
#include "gdk-pixbuf/gdk-pixbuf.h"
Packit a4058c
#include <glib.h>
Packit a4058c
Packit a4058c
/*
Packit a4058c
 * Reads count_bytes from the channel and write them to the loader.
Packit a4058c
 * Returns number of bytes written.
Packit a4058c
 * count_bytes = G_MAXSIZE means read as many bytes as possible.
Packit a4058c
 */
Packit a4058c
static gsize
Packit a4058c
loader_write_from_channel (GdkPixbufLoader *loader,
Packit a4058c
			   GIOChannel      *channel,
Packit a4058c
			   gsize            count_bytes)
Packit a4058c
{
Packit a4058c
    guchar* buffer = NULL;
Packit a4058c
    gsize bytes_read = 0;
Packit a4058c
    GIOStatus read_status;
Packit a4058c
    GError* error = NULL;
Packit a4058c
    gboolean ret;
Packit a4058c
    
Packit a4058c
    if(count_bytes < G_MAXSIZE) {
Packit a4058c
        //read no more than 'count_bytes' bytes
Packit a4058c
        buffer = g_malloc (count_bytes);
Packit a4058c
        read_status = g_io_channel_read_chars (channel, (gchar*)buffer, count_bytes, &bytes_read, NULL);
Packit a4058c
    } else {
Packit a4058c
        //read up to end
Packit a4058c
        read_status = g_io_channel_read_to_end (channel, (gchar**)&buffer, &bytes_read, NULL);
Packit a4058c
    }
Packit a4058c
    g_assert (read_status == G_IO_STATUS_NORMAL || read_status == G_IO_STATUS_EOF);
Packit a4058c
Packit a4058c
    ret = gdk_pixbuf_loader_write(loader, buffer, bytes_read, &error);
Packit a4058c
    g_assert_no_error (error);
Packit a4058c
    g_assert (ret);
Packit a4058c
    g_free(buffer);
Packit a4058c
    return bytes_read;
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
test_short_gif_write (void)
Packit a4058c
{
Packit a4058c
    GdkPixbufLoader *loader;
Packit a4058c
    GIOChannel* channel = g_io_channel_new_file (g_test_get_filename (G_TEST_DIST, "test-animation.gif", NULL), "r", NULL);
Packit a4058c
Packit a4058c
    g_assert (channel != NULL);
Packit a4058c
    g_io_channel_set_encoding (channel, NULL, NULL);
Packit a4058c
Packit a4058c
    loader = gdk_pixbuf_loader_new_with_type ("gif", NULL);
Packit a4058c
    g_assert (loader != NULL);
Packit a4058c
Packit a4058c
    loader_write_from_channel (loader, channel, 10);
Packit a4058c
    loader_write_from_channel (loader, channel, G_MAXSIZE);
Packit a4058c
Packit a4058c
    g_io_channel_unref (channel);
Packit a4058c
Packit a4058c
    gdk_pixbuf_loader_close (loader, NULL);
Packit a4058c
    g_object_unref (loader);
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
test_load_first_frame (void)
Packit a4058c
{
Packit a4058c
    GIOChannel* channel;
Packit a4058c
    gboolean has_frame = FALSE;
Packit a4058c
    GError *error = NULL;
Packit a4058c
    GdkPixbuf *pixbuf;
Packit a4058c
    GdkPixbufLoader *loader;
Packit a4058c
Packit a4058c
    channel = g_io_channel_new_file (g_test_get_filename (G_TEST_DIST, "1_partyanimsm2.gif", NULL), "r", NULL);
Packit a4058c
    g_assert (channel != NULL);
Packit a4058c
    g_io_channel_set_encoding (channel, NULL, NULL);
Packit a4058c
Packit a4058c
    loader = gdk_pixbuf_loader_new_with_type ("gif", NULL);
Packit a4058c
    g_assert (loader != NULL);
Packit a4058c
Packit a4058c
    while (!has_frame) {
Packit a4058c
        GdkPixbufAnimation *animation;
Packit a4058c
Packit a4058c
        loader_write_from_channel (loader, channel, 4096);
Packit a4058c
        animation = gdk_pixbuf_loader_get_animation (loader);
Packit a4058c
        if (animation) {
Packit a4058c
            GdkPixbufAnimationIter *iter = gdk_pixbuf_animation_get_iter (animation, NULL);
Packit a4058c
            if (!gdk_pixbuf_animation_iter_on_currently_loading_frame (iter))
Packit a4058c
                has_frame = TRUE;
Packit a4058c
            g_object_unref (iter);
Packit a4058c
        }
Packit a4058c
    }
Packit a4058c
Packit a4058c
    g_io_channel_unref (channel);
Packit a4058c
Packit a4058c
    gdk_pixbuf_loader_close (loader, &error);
Packit a4058c
    g_assert_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INCOMPLETE_ANIMATION);
Packit a4058c
    g_clear_error (&error);
Packit a4058c
    pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
Packit a4058c
    g_assert (pixbuf);
Packit a4058c
    g_assert_cmpint (gdk_pixbuf_get_width (pixbuf), ==, 660);
Packit a4058c
    g_assert_cmpint (gdk_pixbuf_get_height (pixbuf), ==, 666);
Packit a4058c
    g_object_unref (loader);
Packit a4058c
}
Packit a4058c
Packit a4058c
int main (int argc, char *argv[])
Packit a4058c
{
Packit a4058c
  g_test_init (&argc, &argv, NULL);
Packit a4058c
Packit a4058c
  g_test_add_func ("/animation/short_gif_write", test_short_gif_write);
Packit a4058c
  g_test_add_func ("/animation/load_first_frame", test_load_first_frame);
Packit a4058c
Packit a4058c
  return g_test_run ();
Packit a4058c
}