Blame tests/pixbuf-scale-two-step.c

Packit a4058c
/* -*- Mode: C; c-basic-offset: 2; -*- */
Packit a4058c
/* GdkPixbuf library - test loaders
Packit a4058c
 *
Packit a4058c
 *  Copyright (C) 2016 Martin Guy <martinwguy@gmail.com>
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
Packit a4058c
/*
Packit a4058c
 * Test the two-step scaler speed optimization in gdk-pixbuf/pixops.c
Packit a4058c
 * for result correctness. See https://bugzilla.gnome.org/show_bug.cgi?id=80925
Packit a4058c
 *
Packit a4058c
 * The two-step scaler kicks in when ceil(1/scale_x + 1) * ceil(1/scale_y + 1)
Packit a4058c
 * (the number of 2KB filters created by make_filter_table()) exceeds 1000 so
Packit a4058c
 * test cases wanting it have to do drastic image reductions, such as reducing
Packit a4058c
 * both dimensions by a factor of more that sqrt(1000) - say by 40 both ways.
Packit a4058c
 *
Packit a4058c
 * There is a global boolean in the two-step-scaler allowing us to disable the
Packit a4058c
 * two-step optimization to be able to compare the results with and without it.
Packit a4058c
 */
Packit a4058c
Packit a4058c
#include "config.h"
Packit a4058c
#include "gdk-pixbuf/gdk-pixbuf.h"
Packit a4058c
#include "test-common.h"
Packit a4058c
Packit a4058c
/* Size of images to be scaled */
Packit a4058c
#define SOURCE_WIDTH 400
Packit a4058c
#define SOURCE_HEIGHT 400
Packit a4058c
Packit a4058c
/* To trigger the two-step scaler, we need to reduce the total area by
Packit a4058c
 * more than 1000.  40x40 is 1600. */
Packit a4058c
#define SCALE_FACTOR (1.0/40.0)
Packit a4058c
Packit a4058c
/* First, some functions to make test images */
Packit a4058c
Packit a4058c
/*
Packit a4058c
 * pixdata_almost_equal(): A helper function to check whether the pixels in
Packit a4058c
 * two images are almost the same: pixel color values are allowed to differ
Packit a4058c
 * by at most one.
Packit a4058c
 */
Packit a4058c
Packit a4058c
/* Are two values the same or differ by one? */
Packit a4058c
#define within_one(a, b) \
Packit a4058c
	((a) == (b) || (a) == (b) + 1 || (a) + 1 == (b))
Packit a4058c
Packit a4058c
static gboolean
Packit a4058c
pixdata_almost_equal (GdkPixbuf *one, GdkPixbuf *two)
Packit a4058c
{
Packit a4058c
  guchar *one_row;      /* Pointer to start of row of pixels in one */
Packit a4058c
  guchar *one_pixel;    /* Pointer to current pixel data in one */
Packit a4058c
  guchar *two_row;      /* Pointer to start of row of pixels in two */
Packit a4058c
  guchar *two_pixel;    /* Pointer to current pixel data in two */
Packit a4058c
  guint x, y;
Packit a4058c
  gint width_one, height_one;
Packit a4058c
Packit a4058c
  width_one = gdk_pixbuf_get_width (one);
Packit a4058c
  height_one = gdk_pixbuf_get_height (one);
Packit a4058c
Packit a4058c
  g_assert_cmpint (height_one, >=, 0);
Packit a4058c
  g_assert_cmpint (width_one, >=, 0);
Packit a4058c
  g_assert_cmpint (gdk_pixbuf_get_height (two), >=, 0);
Packit a4058c
  g_assert_cmpint (gdk_pixbuf_get_width (two), >=, 0);
Packit a4058c
Packit a4058c
  if (width_one != gdk_pixbuf_get_width (two) ||
Packit a4058c
      height_one != gdk_pixbuf_get_height (two))
Packit a4058c
    {
Packit a4058c
      g_test_fail();
Packit a4058c
    }
Packit a4058c
Packit a4058c
  for (y = 0, one_row = gdk_pixbuf_get_pixels (one),
Packit a4058c
              two_row = gdk_pixbuf_get_pixels (two);
Packit a4058c
       y < height_one;
Packit a4058c
       y++, one_row += gdk_pixbuf_get_rowstride (one),
Packit a4058c
            two_row += gdk_pixbuf_get_rowstride (two))
Packit a4058c
    {
Packit a4058c
      for (x = 0, one_pixel = one_row, two_pixel = two_row;
Packit a4058c
	   x < width_one;
Packit a4058c
	   x++, one_pixel += gdk_pixbuf_get_n_channels (one),
Packit a4058c
	        two_pixel += gdk_pixbuf_get_n_channels (two))
Packit a4058c
        {
Packit a4058c
	  if (!(within_one(one_pixel[0], two_pixel[0]) &&
Packit a4058c
	        within_one(one_pixel[1], two_pixel[1]) &&
Packit a4058c
	        within_one(one_pixel[2], two_pixel[2])))
Packit a4058c
	    {
Packit a4058c
	      return FALSE;
Packit a4058c
	    }
Packit a4058c
        }
Packit a4058c
    }
Packit a4058c
Packit a4058c
  return TRUE;
Packit a4058c
}
Packit a4058c
Packit a4058c
/* Create a checkerboard of (1,1,1) and (255,255,255) pixels and
Packit a4058c
 * scale it to one fortieth of the image dimensions.
Packit a4058c
 * See if the results are similar enough with and without the two-step
Packit a4058c
 * optimization. */
Packit a4058c
static void
Packit a4058c
test_old_and_new (gconstpointer data)
Packit a4058c
{
Packit a4058c
  GdkInterpType interp_type = *(GdkInterpType *) data;
Packit a4058c
  const GdkPixbuf *source;              /* Source image */
Packit a4058c
  gint width = SOURCE_WIDTH;            /* Size of source image */
Packit a4058c
  gint height = SOURCE_HEIGHT;
Packit a4058c
  GdkPixbuf *one;                       /* Version scaled by the old code */
Packit a4058c
  GdkPixbuf *two;                       /* Version scaled in two steps */
Packit a4058c
Packit a4058c
  /* Use an extreme source image, checkerboard, to exacerbate any artifacts */
Packit a4058c
  source = make_checkerboard (width, height);
Packit a4058c
Packit a4058c
  /* Scale it without and with the two-step optimization */
Packit a4058c
  g_setenv ("GDK_PIXBUF_DISABLE_TWO_STEP_SCALER", "1", TRUE);
Packit a4058c
  one = gdk_pixbuf_scale_simple (source,
Packit a4058c
				 (int) (width * SCALE_FACTOR + 0.5),
Packit a4058c
				 (int) (height * SCALE_FACTOR + 0.5),
Packit a4058c
				 interp_type);
Packit a4058c
  g_unsetenv("GDK_PIXBUF_DISABLE_TWO_STEP_SCALER");
Packit a4058c
  two = gdk_pixbuf_scale_simple (source,
Packit a4058c
				 (int) (width * SCALE_FACTOR + 0.5),
Packit a4058c
				 (int) (height * SCALE_FACTOR + 0.5),
Packit a4058c
				 interp_type);
Packit a4058c
Packit a4058c
  /* Check that the results are almost the same. An error of one color value
Packit a4058c
   * is admissible because the intermediate image's color values are stored
Packit a4058c
   * in 8-bit color values. In practice, with the checkerboard pattern all
Packit a4058c
   * pixels are (129,129,129) with the two-step scaler instead of (128,128,128)
Packit a4058c
   * while the rg pattern gives identical results.
Packit a4058c
   */
Packit a4058c
  if (!pixdata_almost_equal(one, two))
Packit a4058c
    g_test_fail();
Packit a4058c
Packit a4058c
  g_object_unref (G_OBJECT (one));
Packit a4058c
  g_object_unref (G_OBJECT (two));
Packit a4058c
  g_object_unref (G_OBJECT (source));
Packit a4058c
}
Packit a4058c
Packit a4058c
/* Crop a region out of a scaled image using gdk_pixbuf_new_subpixbuf() on a
Packit a4058c
 * scaled image and by cropping it as part of the scaling, and check that the
Packit a4058c
 * results are identical. */
Packit a4058c
static void
Packit a4058c
crop_n_compare(const GdkPixbuf *source,	/* The source image */
Packit a4058c
	       double           scale_factor,  /* is scaled by this amount */
Packit a4058c
	       gint             offset_x,      /* and from this offset in the scaled image */
Packit a4058c
	       gint             offset_y,
Packit a4058c
	       gint             width,         /* a region of this size is cropped out */
Packit a4058c
	       gint             height,
Packit a4058c
	       GdkInterpType    interp_type)
Packit a4058c
{
Packit a4058c
  GdkPixbuf *whole_scaled;     /* The whole image scaled but not cropped */
Packit a4058c
  GdkPixbuf *cropped;          /* The scaled-then-cropped result */
Packit a4058c
  GdkPixbuf *scaled;           /* The cropped-while-scaled result */
Packit a4058c
  guint new_width, new_height; /* Size of whole scaled image */
Packit a4058c
Packit a4058c
  /* First, scale the whole image and crop it */
Packit a4058c
  new_width = (int) (gdk_pixbuf_get_width (source) * scale_factor + 0.5);
Packit a4058c
  new_height = (int) (gdk_pixbuf_get_height (source) * scale_factor + 0.5);
Packit a4058c
  g_assert_cmpint (new_width, ==, 10);
Packit a4058c
  g_assert_cmpint (new_height, ==, 10);
Packit a4058c
Packit a4058c
  whole_scaled = gdk_pixbuf_scale_simple (source, new_width, new_height, interp_type);
Packit a4058c
  g_assert_nonnull (whole_scaled);
Packit a4058c
  cropped = gdk_pixbuf_new_subpixbuf (whole_scaled, offset_x, offset_y, width, height);
Packit a4058c
  g_assert_nonnull (cropped);
Packit a4058c
Packit a4058c
  /* Now crop and scale it in one go */
Packit a4058c
  scaled = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, width, height);
Packit a4058c
  g_assert_nonnull (scaled);
Packit a4058c
  gdk_pixbuf_scale (source, scaled,
Packit a4058c
		    0, 0,                              /* dest_[xy] */
Packit a4058c
		    width, height,                     /* dest_width/height */
Packit a4058c
		    -1.0 * offset_x, -1.0 * offset_y,
Packit a4058c
		    scale_factor, scale_factor,
Packit a4058c
		    interp_type);
Packit a4058c
Packit a4058c
  /* and check that the results are the same.
Packit a4058c
   * We can't use pixdata_equal() because it fails if rowstride is different
Packit a4058c
   * and gdk_pixbuf_new_subpixbuf() reuses whole_scaled's image data with its
Packit a4058c
   * larger rowstride. */
Packit a4058c
  {
Packit a4058c
    guchar *scaled_row;     /* Pointer to start of row of pixels in scaled */
Packit a4058c
    guchar *scaled_pixel;   /* Pointer to current pixel data in scaled */
Packit a4058c
    guchar *cropped_row;    /* Pointer to start of row of pixels in cropped */
Packit a4058c
    guchar *cropped_pixel;  /* Pointer to current pixel data in cropped */
Packit a4058c
    guint x, y;
Packit a4058c
    gint scaled_width, scaled_height;
Packit a4058c
Packit a4058c
    scaled_width = gdk_pixbuf_get_width (scaled);
Packit a4058c
    scaled_height = gdk_pixbuf_get_height (scaled);
Packit a4058c
Packit a4058c
    g_assert (scaled_width > 0);
Packit a4058c
    g_assert (scaled_height > 0);
Packit a4058c
Packit a4058c
    if (scaled_width != gdk_pixbuf_get_width (cropped) ||
Packit a4058c
	scaled_height != gdk_pixbuf_get_height (cropped))
Packit a4058c
      {
Packit a4058c
        g_test_fail();
Packit a4058c
      }
Packit a4058c
Packit a4058c
    for (y = 0, scaled_row = gdk_pixbuf_get_pixels (scaled),
Packit a4058c
	       cropped_row = gdk_pixbuf_get_pixels (cropped);
Packit a4058c
	 y < scaled_height;
Packit a4058c
	 y++, scaled_row += gdk_pixbuf_get_rowstride (scaled),
Packit a4058c
	      cropped_row += gdk_pixbuf_get_rowstride (cropped))
Packit a4058c
      {
Packit a4058c
        for (x = 0, scaled_pixel = scaled_row, cropped_pixel = cropped_row;
Packit a4058c
	    x < scaled_width;
Packit a4058c
	    x++, scaled_pixel += gdk_pixbuf_get_n_channels (scaled),
Packit a4058c
	         cropped_pixel += gdk_pixbuf_get_n_channels (cropped))
Packit a4058c
	  {
Packit a4058c
	    if (!(scaled_pixel[0] == cropped_pixel[0] &&
Packit a4058c
	          scaled_pixel[1] == cropped_pixel[1] &&
Packit a4058c
	          scaled_pixel[2] == cropped_pixel[2]))
Packit a4058c
	    {
Packit a4058c
	      g_test_fail();
Packit a4058c
	    }
Packit a4058c
	  }
Packit a4058c
      }
Packit a4058c
  }
Packit a4058c
Packit a4058c
  g_object_unref (G_OBJECT (whole_scaled));
Packit a4058c
  g_object_unref (G_OBJECT (cropped));
Packit a4058c
  g_object_unref (G_OBJECT (scaled));
Packit a4058c
}
Packit a4058c
Packit a4058c
/* Check that offsets work.
Packit a4058c
 * We should be able to crop a region of an image using the scaler using
Packit a4058c
 * negative offsets. */
Packit a4058c
static void
Packit a4058c
test_offset (gconstpointer data)
Packit a4058c
{
Packit a4058c
  GdkInterpType interp_type = *(GdkInterpType *) data;
Packit a4058c
  const GdkPixbuf *source;                     /* Source image */
Packit a4058c
  gint swidth = SOURCE_WIDTH;                  /* Size of source image */
Packit a4058c
  gint sheight = SOURCE_HEIGHT;
Packit a4058c
  gint dwidth = (swidth * SCALE_FACTOR + 0.5); /* Size of scaled image */
Packit a4058c
  gint dheight = (sheight * SCALE_FACTOR + 0.5);
Packit a4058c
Packit a4058c
  source = make_rg (swidth, sheight);
Packit a4058c
Packit a4058c
  /* Check that the scaler correctly crops out an image half the size of the
Packit a4058c
   * original from each corner and from the middle */
Packit a4058c
  crop_n_compare (source, SCALE_FACTOR, 0,          0,           dwidth / 2, dheight / 2, interp_type);
Packit a4058c
  crop_n_compare (source, SCALE_FACTOR, 0,          dheight / 2, dwidth / 2, dheight / 2, interp_type);
Packit a4058c
  crop_n_compare (source, SCALE_FACTOR, dwidth / 2, 0,           dwidth / 2, dheight / 2, interp_type);
Packit a4058c
  crop_n_compare (source, SCALE_FACTOR, dwidth / 2, dheight / 2, dwidth / 2, dheight / 2, interp_type);
Packit a4058c
  crop_n_compare (source, SCALE_FACTOR, dwidth / 4, dheight / 4, dwidth / 2, dheight / 2, interp_type);
Packit a4058c
Packit a4058c
  g_object_unref (G_OBJECT (source));
Packit a4058c
}
Packit a4058c
Packit a4058c
/* Test the dest_x and dest_y fields by making a copy of an image by
Packit a4058c
 * scaling 1:1 and using dest to copy the four quadrants separately.
Packit a4058c
 *
Packit a4058c
 * When scaled, images are:
Packit a4058c
 * 1) scaled by the scale factors with respect to the top-left corner
Packit a4058c
 * 2) translated by the offsets (negative to shift the image left/up in its
Packit a4058c
 *    frame)
Packit a4058c
 * 3) a region of size dest_width x dest-height starting at (dest_x,dest_y)
Packit a4058c
 *    in the scaled-and-offset image is copied to (dest_x,dest_y) in the
Packit a4058c
 *    destination image. See the illustration at
Packit a4058c
 *    https://developer.gnome.org/gdk-pixbuf/2.22/gdk-pixbuf-scaling.html#gdk-pixbuf-composite */
Packit a4058c
static void
Packit a4058c
test_dest (gconstpointer data)
Packit a4058c
{
Packit a4058c
  GdkInterpType interp_type = *(GdkInterpType *) data;
Packit a4058c
  const GdkPixbuf *source;             /* Source image */
Packit a4058c
  GdkPixbuf *scaled;                   /* Scaled whole image */
Packit a4058c
  GdkPixbuf *copy;                     /* Destination image */
Packit a4058c
  gint swidth = SOURCE_WIDTH;          /* Size of source image */
Packit a4058c
  gint sheight = SOURCE_HEIGHT;
Packit a4058c
  gint dwidth = swidth * SCALE_FACTOR; /* Size of scaled image */
Packit a4058c
  gint dheight = sheight * SCALE_FACTOR;
Packit a4058c
Packit a4058c
  source = make_checkerboard (swidth, sheight);
Packit a4058c
Packit a4058c
  copy = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, dwidth, dheight);
Packit a4058c
  g_assert_nonnull (copy);
Packit a4058c
Packit a4058c
  /* Copy the four quadrants separately */
Packit a4058c
  gdk_pixbuf_scale ((const GdkPixbuf *) source, copy,
Packit a4058c
		    0, 0,                    /* dest_[xy] */
Packit a4058c
		    dwidth / 2, dheight / 2, /* dest_width/height */
Packit a4058c
		    0.0, 0.0,                /* offset_[xy] */
Packit a4058c
		    SCALE_FACTOR,  SCALE_FACTOR,
Packit a4058c
		    interp_type);
Packit a4058c
  gdk_pixbuf_scale ((const GdkPixbuf *) source, copy,
Packit a4058c
		    dwidth / 2, 0,           /* dest_[xy] */
Packit a4058c
		    dwidth / 2, dheight / 2, /* dest_width/height */
Packit a4058c
		    0.0, 0.0,                /* offset_[xy] */
Packit a4058c
		    SCALE_FACTOR,  SCALE_FACTOR,
Packit a4058c
		    interp_type);
Packit a4058c
  gdk_pixbuf_scale ((const GdkPixbuf *)source, copy,
Packit a4058c
		    0, dheight / 2,          /* dest_[xy] */
Packit a4058c
		    dwidth / 2, dheight / 2, /* dest_width/height */
Packit a4058c
		    0.0, 0.0,                /* offset_[xy] */
Packit a4058c
		    SCALE_FACTOR,  SCALE_FACTOR,
Packit a4058c
		    interp_type);
Packit a4058c
  gdk_pixbuf_scale ((const GdkPixbuf *)source, copy,
Packit a4058c
		    dwidth / 2, dheight / 2, /* dest_[xy] */
Packit a4058c
		    dwidth / 2, dheight / 2, /* dest_width/height */
Packit a4058c
		    0.0, 0.0,                /* offset_[xy] */
Packit a4058c
		    SCALE_FACTOR,  SCALE_FACTOR,
Packit a4058c
		    interp_type);
Packit a4058c
Packit a4058c
  scaled = gdk_pixbuf_scale_simple (source, dwidth, dheight, interp_type);
Packit a4058c
  g_assert_nonnull (scaled);
Packit a4058c
Packit a4058c
  /* Compare the all-at-once and the composite images */
Packit a4058c
  {
Packit a4058c
    GError *error = NULL;
Packit a4058c
    if (!pixdata_equal(scaled, copy, &error))
Packit a4058c
      g_test_fail();
Packit a4058c
  }
Packit a4058c
Packit a4058c
  g_object_unref (G_OBJECT (source));
Packit a4058c
  g_object_unref (G_OBJECT (copy));
Packit a4058c
  g_object_unref (G_OBJECT (scaled));
Packit a4058c
}
Packit a4058c
Packit a4058c
int
Packit a4058c
main (int argc, char **argv)
Packit a4058c
{
Packit a4058c
  GdkInterpType tiles = GDK_INTERP_TILES;
Packit a4058c
  GdkInterpType bilinear = GDK_INTERP_BILINEAR;
Packit a4058c
  GdkInterpType hyper = GDK_INTERP_HYPER;
Packit a4058c
  g_test_init (&argc, &argv, NULL);
Packit a4058c
Packit a4058c
  g_test_add_data_func ("/pixbuf/scale/two-step/tiles", &tiles, test_old_and_new);
Packit a4058c
  g_test_add_data_func ("/pixbuf/scale/two-step/bilinear", &bilinear, test_old_and_new);
Packit a4058c
  g_test_add_data_func ("/pixbuf/scale/two-step/hyper", &hyper, test_old_and_new);
Packit a4058c
Packit a4058c
  g_test_add_data_func ("/pixbuf/scale/two-step/offset/tiles", &tiles, test_offset);
Packit a4058c
  g_test_add_data_func ("/pixbuf/scale/two-step/offset/bilinear", &bilinear, test_offset);
Packit a4058c
  g_test_add_data_func ("/pixbuf/scale/two-step/offset/hyper", &hyper, test_offset);
Packit a4058c
Packit a4058c
  g_test_add_data_func ("/pixbuf/scale/two-step/dest/tiles", &tiles, test_dest);
Packit a4058c
  g_test_add_data_func ("/pixbuf/scale/two-step/dest/bilinear", &bilinear, test_dest);
Packit a4058c
  g_test_add_data_func ("/pixbuf/scale/two-step/dest/hyper", &hyper, test_dest);
Packit a4058c
Packit a4058c
  return g_test_run ();
Packit a4058c
}