Blame tests/pixbuf-save.c

Packit a4058c
/* -*- Mode: C; c-basic-offset: 2; -*- */
Packit a4058c
/* GdkPixbuf library - test loaders
Packit a4058c
 *
Packit a4058c
 * Copyright (C) 2013 Red Hat, Inc.
Packit a4058c
 *
Packit a4058c
 * This program is free software; you can redistribute it and/or modify
Packit a4058c
 * it under the terms of the GNU General Public License as published by
Packit a4058c
 * the Free Software Foundation; either version 2 of the License, or
Packit a4058c
 * (at your option) any later version.
Packit a4058c
 *
Packit a4058c
 * This program is distributed in the hope that it will be useful,
Packit a4058c
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a4058c
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit a4058c
 * GNU General Public License for more details.
Packit a4058c
 *
Packit a4058c
 * You should have received a copy of the GNU General Public License
Packit a4058c
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit a4058c
 *
Packit a4058c
 * Author: Matthias Clasen
Packit a4058c
 */
Packit a4058c
Packit a4058c
#include "gdk-pixbuf/gdk-pixbuf.h"
Packit a4058c
#include "test-common.h"
Packit a4058c
#include <string.h>
Packit a4058c
#include <glib/gstdio.h>
Packit a4058c
Packit a4058c
#define compare_option(p1, p2, key) \
Packit a4058c
  g_strcmp0 (gdk_pixbuf_get_option (p1, key), gdk_pixbuf_get_option (p2, key))
Packit a4058c
Packit a4058c
static gboolean
Packit a4058c
pixbuf_equal (GdkPixbuf *p1, GdkPixbuf *p2)
Packit a4058c
{
Packit a4058c
  if (!pixdata_equal (p1, p2, NULL))
Packit a4058c
    return FALSE;
Packit a4058c
  if (compare_option (p1, p2, "Title") != 0)
Packit a4058c
    return FALSE;
Packit a4058c
  if (compare_option (p1, p2, "Artist") != 0)
Packit a4058c
    return FALSE;
Packit a4058c
  if (compare_option (p1, p2, "x_hot") != 0)
Packit a4058c
    return FALSE;
Packit a4058c
  if (compare_option (p1, p2, "y_hot") != 0)
Packit a4058c
    return FALSE;
Packit a4058c
  if (compare_option (p1, p2, "orientation") != 0)
Packit a4058c
    return FALSE;
Packit a4058c
  if (compare_option (p1, p2, "icc-profile") != 0)
Packit a4058c
    return FALSE;
Packit a4058c
Packit a4058c
  return TRUE;
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
test_save_roundtrip (void)
Packit a4058c
{
Packit a4058c
  GError *error = NULL;
Packit a4058c
  GdkPixbuf *ref;
Packit a4058c
  GdkPixbuf *pixbuf;
Packit a4058c
Packit a4058c
  if (!format_supported ("png"))
Packit a4058c
    {
Packit a4058c
      g_test_skip ("format not supported");
Packit a4058c
      return;
Packit a4058c
    }
Packit a4058c
Packit a4058c
  ref = gdk_pixbuf_new_from_file (g_test_get_filename (G_TEST_DIST, "test-image.png", NULL), &error);
Packit a4058c
  g_assert_no_error (error);
Packit a4058c
Packit a4058c
  gdk_pixbuf_save (ref, "pixbuf-save-roundtrip", "png", &error, NULL);
Packit a4058c
  g_assert_no_error (error);
Packit a4058c
Packit a4058c
  pixbuf = gdk_pixbuf_new_from_file ("pixbuf-save-roundtrip", &error);
Packit a4058c
  g_assert_no_error (error);
Packit a4058c
Packit a4058c
  g_assert (pixbuf_equal (pixbuf, ref));
Packit a4058c
Packit a4058c
  g_object_unref (pixbuf);
Packit a4058c
  g_object_unref (ref);
Packit a4058c
Packit a4058c
  g_unlink ("pixbuf-save-roundtrip");
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
test_save_ico (void)
Packit a4058c
{
Packit a4058c
  GError *error = NULL;
Packit a4058c
  GdkPixbuf *ref, *ref2;
Packit a4058c
  GdkPixbuf *pixbuf;
Packit a4058c
Packit a4058c
  if (!format_supported ("ico") || !format_supported ("png"))
Packit a4058c
    {
Packit a4058c
      g_test_skip ("format not supported");
Packit a4058c
      return;
Packit a4058c
    }
Packit a4058c
Packit a4058c
  ref = gdk_pixbuf_new_from_file (g_test_get_filename (G_TEST_DIST, "test-image.png", NULL), &error);
Packit a4058c
  g_assert_no_error (error);
Packit a4058c
Packit a4058c
  ref2 = gdk_pixbuf_scale_simple (ref, 256, 256, GDK_INTERP_NEAREST);
Packit a4058c
  g_object_unref (ref);
Packit a4058c
  ref = ref2;
Packit a4058c
Packit a4058c
  gdk_pixbuf_save (ref, "pixbuf-save-roundtrip", "ico", &error, NULL);
Packit a4058c
  g_assert_no_error (error);
Packit a4058c
Packit a4058c
  pixbuf = gdk_pixbuf_new_from_file ("pixbuf-save-roundtrip", &error);
Packit a4058c
  g_assert_no_error (error);
Packit a4058c
Packit a4058c
  g_assert (pixbuf_equal (pixbuf, ref));
Packit a4058c
Packit a4058c
  g_object_unref (pixbuf);
Packit a4058c
  g_object_unref (ref);
Packit a4058c
Packit a4058c
  g_unlink ("pixbuf-save-roundtrip");
Packit a4058c
}
Packit a4058c
Packit a4058c
static void
Packit a4058c
test_save_options (void)
Packit a4058c
{
Packit a4058c
  GdkPixbuf *ref;
Packit a4058c
  GdkPixbuf *pixbuf, *pixbuf2;
Packit a4058c
  GError *error = NULL;
Packit a4058c
Packit a4058c
  if (!format_supported ("png"))
Packit a4058c
    {
Packit a4058c
      g_test_skip ("format not supported");
Packit a4058c
      return;
Packit a4058c
    }
Packit a4058c
Packit a4058c
  ref = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 10, 10);
Packit a4058c
  gdk_pixbuf_fill (ref, 0xff00ff00);
Packit a4058c
Packit a4058c
  gdk_pixbuf_save (ref, "pixbuf-save-options", "png", &error,
Packit a4058c
                   "tEXt::option1", "Some text to transport via option",
Packit a4058c
                   "tEXt::long-option-name123456789123456789123456789", "",
Packit a4058c
#ifdef PNG_iTXt_SUPPORTED
Packit a4058c
                   "tEXt::3", "αβγδ",
Packit a4058c
#endif
Packit a4058c
                   NULL);
Packit a4058c
  g_assert_no_error (error);
Packit a4058c
Packit a4058c
  pixbuf = gdk_pixbuf_new_from_file ("pixbuf-save-options", &error);
Packit a4058c
  g_assert_no_error (error);
Packit a4058c
Packit a4058c
  g_assert_cmpstr (gdk_pixbuf_get_option (pixbuf, "tEXt::option1"), ==, "Some text to transport via option");
Packit a4058c
  g_assert_cmpstr (gdk_pixbuf_get_option (pixbuf, "tEXt::long-option-name123456789123456789123456789"), ==, "");
Packit a4058c
#ifdef PNG_iTXt_SUPPORTED
Packit a4058c
  g_assert_cmpstr (gdk_pixbuf_get_option (pixbuf, "tEXt::3"), ==, "αβγδ");
Packit a4058c
#endif
Packit a4058c
Packit a4058c
  pixbuf2 = gdk_pixbuf_copy (pixbuf);
Packit a4058c
  g_assert_null (gdk_pixbuf_get_option (pixbuf2, "tEXt::option1"));
Packit a4058c
  gdk_pixbuf_copy_options (pixbuf, pixbuf2);
Packit a4058c
  g_assert_cmpstr (gdk_pixbuf_get_option (pixbuf2, "tEXt::option1"), ==, "Some text to transport via option");
Packit a4058c
  g_assert_true (gdk_pixbuf_remove_option (pixbuf2, "tEXt::option1"));
Packit a4058c
  g_assert_null (gdk_pixbuf_get_option (pixbuf2, "tEXt::option1"));
Packit a4058c
  g_assert_false (gdk_pixbuf_remove_option (pixbuf2, "tEXt::option1"));
Packit a4058c
#ifdef PNG_iTXt_SUPPORTED
Packit a4058c
  gdk_pixbuf_remove_option (pixbuf2, "tEXt::3");
Packit a4058c
#endif
Packit a4058c
  gdk_pixbuf_remove_option (pixbuf2, "tEXt::long-option-name123456789123456789123456789");
Packit a4058c
  g_assert_false (gdk_pixbuf_remove_option (pixbuf2, "tEXt::option1"));
Packit a4058c
Packit a4058c
  g_object_unref (pixbuf2);
Packit a4058c
  g_object_unref (pixbuf);
Packit a4058c
  g_object_unref (ref);
Packit a4058c
}
Packit a4058c
Packit a4058c
int
Packit a4058c
main (int argc, char **argv)
Packit a4058c
{
Packit a4058c
  g_test_init (&argc, &argv, NULL);
Packit a4058c
Packit a4058c
  g_test_add_func ("/pixbuf/save/roundtrip", test_save_roundtrip);
Packit a4058c
  g_test_add_func ("/pixbuf/save/options", test_save_options);
Packit a4058c
  g_test_add_func ("/pixbuf/save/ico", test_save_ico);
Packit a4058c
Packit a4058c
  return g_test_run ();
Packit a4058c
}