Blame tests/pixbuf-stream.c

Packit 979760
/* -*- Mode: C; c-basic-offset: 2; -*- */
Packit 979760
/* GdkPixbuf library - test loaders
Packit 979760
 *
Packit 979760
 * Copyright (C) 2013 Red Hat, Inc.
Packit 979760
 *
Packit 979760
 * This program is free software; you can redistribute it and/or modify
Packit 979760
 * it under the terms of the GNU General Public License as published by
Packit 979760
 * the Free Software Foundation; either version 2 of the License, or
Packit 979760
 * (at your option) any later version.
Packit 979760
 *
Packit 979760
 * This program is distributed in the hope that it will be useful,
Packit 979760
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 979760
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 979760
 * GNU General Public License for more details.
Packit 979760
 *
Packit 979760
 * You should have received a copy of the GNU General Public License
Packit 979760
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit 979760
 *
Packit 979760
 * Author: Matthias Clasen
Packit 979760
 */
Packit 979760
Packit 979760
#include "config.h"
Packit 979760
#include "gdk-pixbuf/gdk-pixbuf.h"
Packit 979760
#include "test-common.h"
Packit 979760
#include <string.h>
Packit 979760
Packit 979760
#define compare_option(p1, p2, key) \
Packit 979760
  g_strcmp0 (gdk_pixbuf_get_option (p1, key), gdk_pixbuf_get_option (p2, key))
Packit 979760
Packit 979760
static gboolean
Packit 979760
pixbuf_equal (GdkPixbuf *p1, GdkPixbuf *p2)
Packit 979760
{
Packit 979760
  if (gdk_pixbuf_get_colorspace (p1) != gdk_pixbuf_get_colorspace (p2))
Packit 979760
    return FALSE;
Packit 979760
  if (gdk_pixbuf_get_n_channels (p1) != gdk_pixbuf_get_n_channels (p2))
Packit 979760
    return FALSE;
Packit 979760
  if (gdk_pixbuf_get_bits_per_sample (p1) != gdk_pixbuf_get_bits_per_sample (p2))
Packit 979760
    return FALSE;
Packit 979760
  if (gdk_pixbuf_get_width (p1) != gdk_pixbuf_get_width (p2))
Packit 979760
    return FALSE;
Packit 979760
  if (gdk_pixbuf_get_height (p1) != gdk_pixbuf_get_height (p2))
Packit 979760
    return FALSE;
Packit 979760
  if (gdk_pixbuf_get_rowstride (p1) != gdk_pixbuf_get_rowstride (p2))
Packit 979760
    return FALSE;
Packit 979760
  if (memcmp (gdk_pixbuf_get_pixels (p1), gdk_pixbuf_get_pixels (p2),
Packit 979760
          gdk_pixbuf_get_byte_length (p1)) != 0)
Packit 979760
    return FALSE;
Packit 979760
  if (compare_option (p1, p2, "Title") != 0)
Packit 979760
    return FALSE;
Packit 979760
  if (compare_option (p1, p2, "Artist") != 0)
Packit 979760
    return FALSE;
Packit 979760
  if (compare_option (p1, p2, "x_hot") != 0)
Packit 979760
    return FALSE;
Packit 979760
  if (compare_option (p1, p2, "y_hot") != 0)
Packit 979760
    return FALSE;
Packit 979760
  if (compare_option (p1, p2, "orientation") != 0)
Packit 979760
    return FALSE;
Packit 979760
  if (compare_option (p1, p2, "icc-profile") != 0)
Packit 979760
    return FALSE;
Packit 979760
Packit 979760
  return TRUE;
Packit 979760
}
Packit 979760
Packit 979760
static void
Packit 979760
test_stream (gconstpointer data)
Packit 979760
{
Packit 979760
  const gchar *filename = data;
Packit 979760
  const gchar *path;
Packit 979760
  GError *error = NULL;
Packit 979760
  GdkPixbuf *pixbuf, *ref;
Packit 979760
  GFile *file;
Packit 979760
  GInputStream *stream;
Packit 979760
Packit 979760
  if (!format_supported (filename))
Packit 979760
    {
Packit 979760
      g_test_skip ("format not supported");
Packit 979760
      return;
Packit 979760
    }
Packit 979760
Packit 979760
  path = g_test_get_filename (G_TEST_DIST, filename, NULL);
Packit 979760
  ref = gdk_pixbuf_new_from_file (path, &error);
Packit 979760
  g_assert_no_error (error);
Packit 979760
Packit 979760
  file = g_file_new_for_path (path);
Packit 979760
  stream = (GInputStream *)g_file_read (file, NULL, &error);
Packit 979760
  g_assert_no_error (error);
Packit 979760
Packit 979760
  pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, &error);
Packit 979760
  g_assert_no_error (error);
Packit 979760
  g_assert (pixbuf_equal (pixbuf, ref));
Packit 979760
  g_object_unref (pixbuf);
Packit 979760
  
Packit 979760
  g_object_unref (stream);
Packit 979760
  g_object_unref (file);
Packit 979760
  g_object_unref (ref);
Packit 979760
}
Packit 979760
Packit 979760
static void
Packit 979760
async_done_cb (GObject *source, GAsyncResult *res, gpointer data)
Packit 979760
{
Packit 979760
  GdkPixbuf *ref = data;
Packit 979760
  GdkPixbuf *pixbuf;
Packit 979760
  GError *error = NULL;
Packit 979760
Packit 979760
  pixbuf = gdk_pixbuf_new_from_stream_finish (res, &error);
Packit 979760
  g_assert_no_error (error);
Packit 979760
Packit 979760
  g_assert (pixbuf_equal (pixbuf, ref));
Packit 979760
Packit 979760
  g_object_unref (pixbuf);
Packit 979760
  g_object_unref (ref);
Packit 979760
}
Packit 979760
Packit 979760
static void
Packit 979760
test_stream_async (gconstpointer data)
Packit 979760
{
Packit 979760
  const gchar *filename = data;
Packit 979760
  const gchar *path;
Packit 979760
  GError *error = NULL;
Packit 979760
  GdkPixbuf *ref;
Packit 979760
  gchar *buffer;
Packit 979760
  gsize size;
Packit 979760
  GInputStream *stream;
Packit 979760
Packit 979760
  if (!format_supported (filename))
Packit 979760
    {
Packit 979760
      g_test_skip ("format not supported");
Packit 979760
      return;
Packit 979760
    }
Packit 979760
Packit 979760
  path = g_test_get_filename (G_TEST_DIST, filename, NULL);
Packit 979760
  ref = gdk_pixbuf_new_from_file (path, &error);
Packit 979760
  g_assert_no_error (error);
Packit 979760
Packit 979760
  g_file_get_contents (path, &buffer, &size, &error);
Packit 979760
  g_assert_no_error (error);
Packit 979760
Packit 979760
  stream = g_memory_input_stream_new_from_data (buffer, size, g_free);
Packit 979760
  gdk_pixbuf_new_from_stream_async (stream, NULL, async_done_cb, ref);
Packit 979760
  g_object_unref (stream);
Packit 979760
}
Packit 979760
Packit 979760
static void
Packit 979760
test_stream_at_scale (gconstpointer data)
Packit 979760
{
Packit 979760
  const gchar *filename = data;
Packit 979760
  const gchar *path;
Packit 979760
  GError *error = NULL;
Packit 979760
  GdkPixbuf *pixbuf, *ref;
Packit 979760
  GFile *file;
Packit 979760
  GInputStream *stream;
Packit 979760
Packit 979760
  if (!format_supported (filename))
Packit 979760
    {
Packit 979760
      g_test_skip ("format not supported");
Packit 979760
      return;
Packit 979760
    }
Packit 979760
Packit 979760
  path = g_test_get_filename (G_TEST_DIST, filename, NULL);
Packit 979760
  ref = gdk_pixbuf_new_from_file_at_scale (path, 20, 30, TRUE, &error);
Packit 979760
  g_assert_no_error (error);
Packit 979760
Packit 979760
  file = g_file_new_for_path (path);
Packit 979760
  stream = (GInputStream *)g_file_read (file, NULL, &error);
Packit 979760
  g_assert_no_error (error);
Packit 979760
Packit 979760
  pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream, 20, 30, TRUE, NULL, &error);
Packit 979760
  g_assert_no_error (error);
Packit 979760
  g_assert (pixbuf_equal (pixbuf, ref));
Packit 979760
  g_object_unref (pixbuf);
Packit 979760
  
Packit 979760
  g_object_unref (stream);
Packit 979760
  g_object_unref (file);
Packit 979760
  g_object_unref (ref);
Packit 979760
}
Packit 979760
Packit 979760
static void
Packit 979760
test_stream_at_scale_async (gconstpointer data)
Packit 979760
{
Packit 979760
  const gchar *filename = data;
Packit 979760
  const gchar *path;
Packit 979760
  GError *error = NULL;
Packit 979760
  GdkPixbuf *ref;
Packit 979760
  gchar *buffer;
Packit 979760
  gsize size;
Packit 979760
  GInputStream *stream;
Packit 979760
Packit 979760
  if (!format_supported (filename))
Packit 979760
    {
Packit 979760
      g_test_skip ("format not supported");
Packit 979760
      return;
Packit 979760
    }
Packit 979760
Packit 979760
  path = g_test_get_filename (G_TEST_DIST, filename, NULL);
Packit 979760
  ref = gdk_pixbuf_new_from_file_at_scale (path, 40, 10, FALSE, &error);
Packit 979760
  g_assert_no_error (error);
Packit 979760
Packit 979760
  g_file_get_contents (path, &buffer, &size, &error);
Packit 979760
  g_assert_no_error (error);
Packit 979760
Packit 979760
  stream = g_memory_input_stream_new_from_data (buffer, size, g_free);
Packit 979760
  gdk_pixbuf_new_from_stream_at_scale_async (stream, 40, 10, FALSE, NULL, async_done_cb, ref);
Packit 979760
  g_object_unref (stream);
Packit 979760
}
Packit 979760
Packit 979760
int
Packit 979760
main (int argc, char **argv)
Packit 979760
{
Packit 979760
  g_test_init (&argc, &argv, NULL);
Packit 979760
Packit 979760
  g_test_add_data_func ("/pixbuf/stream", "icc-profile.png", test_stream);
Packit 979760
  g_test_add_data_func ("/pixbuf/stream/async", "icc-profile.png", test_stream_async);
Packit 979760
  g_test_add_data_func ("/pixbuf/stream/scale", "icc-profile.png", test_stream_at_scale);
Packit 979760
  g_test_add_data_func ("/pixbuf/stream/scale/async", "icc-profile.png", test_stream_at_scale_async);
Packit 979760
Packit 979760
  return g_test_run ();
Packit 979760
}