Blame tests/test-common.c

Packit 979760
/* -*- Mode: C; c-basic-offset: 2; -*- */
Packit 979760
/* GdkPixbuf library - test loaders
Packit 979760
 *
Packit 979760
 * Copyright (C) 2014 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, write to the Free Software
Packit 979760
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
Packit 979760
 *
Packit 979760
 * Author: Matthias Clasen
Packit 979760
 */
Packit 979760
Packit 979760
#include "config.h"
Packit 979760
#include "test-common.h"
Packit 979760
#include "gdk-pixbuf/gdk-pixbuf.h"
Packit 979760
Packit 979760
#include <string.h>
Packit 979760
Packit 979760
/* Checkerboard of black and white pxels (actually (1,1,1) and (255,255,255)
Packit 979760
 * so they average to (128,128,128)) */
Packit 979760
GdkPixbuf *
Packit 979760
make_checkerboard (int width, int height)
Packit 979760
{
Packit 979760
  GdkPixbuf *checkerboard;
Packit 979760
  guint x, y;
Packit 979760
  guchar *row;   /* Pointer to start of row of pixels within the image */
Packit 979760
  guchar *pixel; /* Pointer to current pixel data in row */
Packit 979760
Packit 979760
  checkerboard = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, width, height);
Packit 979760
  g_assert_nonnull (checkerboard);
Packit 979760
Packit 979760
  for (y = 0, row = gdk_pixbuf_get_pixels (checkerboard);
Packit 979760
       y < height;
Packit 979760
       y++, row += gdk_pixbuf_get_rowstride (checkerboard))
Packit 979760
    {
Packit 979760
      for (x = 0, pixel = row;
Packit 979760
           x < width;
Packit 979760
           x++, pixel += gdk_pixbuf_get_n_channels (checkerboard))
Packit 979760
        {
Packit 979760
          pixel[0] = pixel[1] = pixel[2] = (x ^ y) & 1 ? 1 : 255;
Packit 979760
        }
Packit 979760
    }
Packit 979760
Packit 979760
  return checkerboard;
Packit 979760
}
Packit 979760
Packit 979760
/* Image where all the pixels have different colours */
Packit 979760
GdkPixbuf *
Packit 979760
make_rg (int width, int height)
Packit 979760
{
Packit 979760
  GdkPixbuf *pixbuf;
Packit 979760
  guint x, y;
Packit 979760
  guchar *row;   /* Pointer to start of row of pixels within the image */
Packit 979760
  guchar *pixel; /* Pointer to current pixel data in row */
Packit 979760
Packit 979760
  /* Make a source image whose pixels are all of different colors */
Packit 979760
  pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, width, height);
Packit 979760
  g_assert_nonnull (pixbuf);
Packit 979760
Packit 979760
  for (y = 0, row = gdk_pixbuf_get_pixels (pixbuf);
Packit 979760
       y < height;
Packit 979760
       y++, row += gdk_pixbuf_get_rowstride (pixbuf))
Packit 979760
    {
Packit 979760
      for (x = 0, pixel = row;
Packit 979760
           x < width;
Packit 979760
           x++, pixel += gdk_pixbuf_get_n_channels (pixbuf))
Packit 979760
        {
Packit 979760
          pixel[0] = x & 255; pixel[1] = y & 255;
Packit 979760
          /* If image > 256 pixels wide/high put the extra bits in the last pixel */
Packit 979760
          pixel[2] = ((x >> 8) & 15) | ((( y >> 8) & 15) << 4);
Packit 979760
        }
Packit 979760
    }
Packit 979760
Packit 979760
  return pixbuf;
Packit 979760
}
Packit 979760
Packit 979760
gboolean
Packit 979760
format_supported (const gchar *filename)
Packit 979760
{
Packit 979760
  GSList *formats, *l;
Packit 979760
  gboolean retval;
Packit 979760
Packit 979760
  retval = FALSE;
Packit 979760
  formats = gdk_pixbuf_get_formats ();
Packit 979760
  for (l = formats; l; l = l->next)
Packit 979760
    {
Packit 979760
      GdkPixbufFormat *format = l->data;
Packit 979760
      char **extensions = gdk_pixbuf_format_get_extensions (format);
Packit 979760
      gint i;
Packit 979760
Packit 979760
      for (i = 0; extensions[i]; i++)
Packit 979760
        {
Packit 979760
          if (g_str_has_suffix (filename, extensions[i]))
Packit 979760
            {
Packit 979760
              retval = TRUE;
Packit 979760
              break;
Packit 979760
            }
Packit 979760
        }
Packit 979760
Packit 979760
      g_free (extensions);
Packit 979760
      if (retval)
Packit 979760
        break;
Packit 979760
    }
Packit 979760
  g_slist_free (formats);
Packit 979760
Packit 979760
  return retval;
Packit 979760
}
Packit 979760
Packit 979760
gboolean
Packit 979760
file_supported (GFile *file)
Packit 979760
{
Packit 979760
  char *uri;
Packit 979760
  gboolean result;
Packit 979760
Packit 979760
  uri = g_file_get_uri (file);
Packit 979760
Packit 979760
  result = format_supported (uri);
Packit 979760
Packit 979760
  g_free (uri);
Packit 979760
Packit 979760
  return result;
Packit 979760
}
Packit 979760
Packit 979760
gboolean
Packit 979760
skip_if_insufficient_memory (GError **err)
Packit 979760
{
Packit 979760
  if (*err && g_error_matches (*err, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY))
Packit 979760
  {
Packit 979760
      g_test_skip ((*err)->message);
Packit 979760
      g_error_free (*err);
Packit 979760
      *err = NULL;
Packit 979760
      return TRUE;
Packit 979760
  }
Packit 979760
Packit 979760
  return FALSE;
Packit 979760
}
Packit 979760
Packit 979760
gboolean
Packit 979760
pixdata_equal (GdkPixbuf  *test,
Packit 979760
               GdkPixbuf  *ref,
Packit 979760
               GError    **error)
Packit 979760
{
Packit 979760
  if (gdk_pixbuf_get_colorspace (test) != gdk_pixbuf_get_colorspace (ref))
Packit 979760
    {
Packit 979760
      g_set_error (error, GDK_PIXBUF_ERROR, 0, "Image colorspace is %d but should be %d",
Packit 979760
                   gdk_pixbuf_get_colorspace (test), gdk_pixbuf_get_colorspace (ref));
Packit 979760
      return FALSE;
Packit 979760
    }
Packit 979760
Packit 979760
  if (gdk_pixbuf_get_n_channels (test) != gdk_pixbuf_get_n_channels (ref))
Packit 979760
    {
Packit 979760
      g_set_error (error, GDK_PIXBUF_ERROR, 0,
Packit 979760
                   "has %u channels but should have %u",
Packit 979760
                   gdk_pixbuf_get_n_channels (test), gdk_pixbuf_get_n_channels (ref));
Packit 979760
      return FALSE;
Packit 979760
    }
Packit 979760
Packit 979760
  if (gdk_pixbuf_get_bits_per_sample (test) != gdk_pixbuf_get_bits_per_sample (ref))
Packit 979760
    {
Packit 979760
      g_set_error (error, GDK_PIXBUF_ERROR, 0,
Packit 979760
                   "Image is %u bits per sample but should be %u bits per sample",
Packit 979760
                   gdk_pixbuf_get_bits_per_sample (test), gdk_pixbuf_get_bits_per_sample (ref));
Packit 979760
      return FALSE;
Packit 979760
    }
Packit 979760
Packit 979760
  if (gdk_pixbuf_get_width (test) != gdk_pixbuf_get_width (ref) ||
Packit 979760
      gdk_pixbuf_get_height (test) != gdk_pixbuf_get_height (ref))
Packit 979760
    {
Packit 979760
      g_set_error (error, GDK_PIXBUF_ERROR, 0,
Packit 979760
                   "Image size is %dx%d but should be %dx%d",
Packit 979760
                   gdk_pixbuf_get_width (test), gdk_pixbuf_get_height (test),
Packit 979760
                   gdk_pixbuf_get_width (ref), gdk_pixbuf_get_height (ref));
Packit 979760
      return FALSE;
Packit 979760
    }
Packit 979760
Packit 979760
  if (gdk_pixbuf_get_rowstride (test) != gdk_pixbuf_get_rowstride (ref))
Packit 979760
    {
Packit 979760
      g_set_error (error, GDK_PIXBUF_ERROR, 0,
Packit 979760
                   "Image rowstrides is %u bytes but should be %u bytes",
Packit 979760
                   gdk_pixbuf_get_rowstride (test), gdk_pixbuf_get_rowstride (ref));
Packit 979760
      return FALSE;
Packit 979760
    }
Packit 979760
Packit 979760
  if (memcmp (gdk_pixbuf_get_pixels (test), gdk_pixbuf_get_pixels (ref),
Packit 979760
          gdk_pixbuf_get_byte_length (test)) != 0)
Packit 979760
    {
Packit 979760
      gint x, y, width, height, n_channels, rowstride;
Packit 979760
      const guchar *test_pixels, *ref_pixels;
Packit 979760
Packit 979760
      rowstride = gdk_pixbuf_get_rowstride (test);
Packit 979760
      n_channels = gdk_pixbuf_get_n_channels (test);
Packit 979760
      width = gdk_pixbuf_get_width (test);
Packit 979760
      height = gdk_pixbuf_get_height (test);
Packit 979760
      test_pixels = gdk_pixbuf_get_pixels (test);
Packit 979760
      ref_pixels = gdk_pixbuf_get_pixels (ref);
Packit 979760
Packit 979760
      g_assert_cmpint (width, >=, 0);
Packit 979760
      g_assert_cmpint (height, >=, 0);
Packit 979760
      g_assert_cmpint (n_channels, >=, 0);
Packit 979760
Packit 979760
      for (y = 0; y < height; y++)
Packit 979760
        {
Packit 979760
          for (x = 0; x < width; x++)
Packit 979760
            {
Packit 979760
              if (memcmp (&test_pixels[x * n_channels], &ref_pixels[x * n_channels], n_channels) != 0)
Packit 979760
                {
Packit 979760
                  if (n_channels == 4)
Packit 979760
                    {
Packit 979760
                      g_set_error (error, GDK_PIXBUF_ERROR, 0, "Image data at %ux%u is #%02X%02X%02X%02X, but should be #%02X%02X%02X%02X",
Packit 979760
                                   x, y,
Packit 979760
                                   test_pixels[x * n_channels + 0], test_pixels[x * n_channels + 1], test_pixels[x * n_channels + 2], test_pixels[x * n_channels + 3],
Packit 979760
                                   ref_pixels[x * n_channels + 0], ref_pixels[x * n_channels + 1], ref_pixels[x * n_channels + 2], ref_pixels[x * n_channels + 3]);
Packit 979760
                    }
Packit 979760
                  else if (n_channels == 3)
Packit 979760
                    {
Packit 979760
                      g_set_error (error, GDK_PIXBUF_ERROR, 0, "Image data at %ux%u is #%02X%02X%02X, but should be #%02X%02X%02X",
Packit 979760
                                   x, y,
Packit 979760
                                   test_pixels[x * n_channels + 0], test_pixels[x * n_channels + 1], test_pixels[x * n_channels + 2],
Packit 979760
                                   ref_pixels[x * n_channels + 0], ref_pixels[x * n_channels + 1], ref_pixels[x * n_channels + 2]);
Packit 979760
                    }
Packit 979760
                  else
Packit 979760
                    {
Packit 979760
                      g_set_error (error, GDK_PIXBUF_ERROR, 0, "Image data differ at %ux%u", x, y);
Packit 979760
                    }
Packit 979760
                  return FALSE;
Packit 979760
                }
Packit 979760
            }
Packit 979760
          test_pixels += rowstride;
Packit 979760
          ref_pixels += rowstride;
Packit 979760
        }
Packit 979760
    }
Packit 979760
Packit 979760
  return TRUE;
Packit 979760
}
Packit 979760
Packit 979760
static int
Packit 979760
compare_files (gconstpointer a, gconstpointer b)
Packit 979760
{
Packit 979760
  GFile *file1 = G_FILE (a);
Packit 979760
  GFile *file2 = G_FILE (b);
Packit 979760
  char *uri1, *uri2;
Packit 979760
  int result;
Packit 979760
Packit 979760
  uri1 = g_file_get_uri (file1);
Packit 979760
  uri2 = g_file_get_uri (file2);
Packit 979760
Packit 979760
  result = strcmp (uri1, uri2);
Packit 979760
Packit 979760
  g_free (uri1);
Packit 979760
  g_free (uri2);
Packit 979760
Packit 979760
  return result;
Packit 979760
}
Packit 979760
Packit 979760
void
Packit 979760
add_test_for_all_images (const gchar   *prefix,
Packit 979760
                         GFile         *base,
Packit 979760
                         GFile         *file,
Packit 979760
                         GTestDataFunc  test_func,
Packit 979760
                         AddTestFunc    add_test_func)
Packit 979760
{
Packit 979760
  GFileEnumerator *enumerator;
Packit 979760
  GFileInfo *info;
Packit 979760
  GList *l, *files;
Packit 979760
  GError *error = NULL;
Packit 979760
Packit 979760
Packit 979760
  if (g_file_query_file_type (file, 0, NULL) != G_FILE_TYPE_DIRECTORY)
Packit 979760
    {
Packit 979760
      gchar *test_path;
Packit 979760
      gchar *relative_path;
Packit 979760
Packit 979760
      if (base)
Packit 979760
        relative_path = g_file_get_relative_path (base, file);
Packit 979760
      else
Packit 979760
        relative_path = g_file_get_path (file);
Packit 979760
Packit 979760
      test_path = g_strconcat (prefix, "/", relative_path, NULL);
Packit 979760
      
Packit 979760
      g_test_add_data_func_full (test_path, g_object_ref (file), test_func, g_object_unref);
Packit 979760
      return;
Packit 979760
    }
Packit 979760
Packit 979760
Packit 979760
  enumerator = g_file_enumerate_children (file, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
Packit 979760
  g_assert_no_error (error);
Packit 979760
  files = NULL;
Packit 979760
Packit 979760
  while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
Packit 979760
    {
Packit 979760
      GFile *next_file = g_file_get_child (file, g_file_info_get_name (info));
Packit 979760
Packit 979760
      if (add_test_func == NULL || add_test_func (next_file))
Packit 979760
        {
Packit 979760
          files = g_list_prepend (files, g_object_ref (next_file));
Packit 979760
        }
Packit 979760
Packit 979760
      g_object_unref (next_file);
Packit 979760
      g_object_unref (info);
Packit 979760
    }
Packit 979760
  
Packit 979760
  g_assert_no_error (error);
Packit 979760
  g_object_unref (enumerator);
Packit 979760
Packit 979760
  files = g_list_sort (files, compare_files);
Packit 979760
Packit 979760
  for (l = files; l; l = l->next)
Packit 979760
    {
Packit 979760
      add_test_for_all_images (prefix, base, l->data, test_func, add_test_func);
Packit 979760
    }
Packit 979760
Packit 979760
  g_list_free_full (files, g_object_unref);
Packit 979760
}