Blame gdk-pixbuf/test-gdk-pixbuf.c

Packit 979760
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
Packit 979760
Packit 979760
/* GdkPixbuf library - test program
Packit 979760
 *
Packit 979760
 * Copyright (C) 1999 The Free Software Foundation
Packit 979760
 *
Packit 979760
 * Author: Federico Mena-Quintero <federico@gimp.org>
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
Packit 979760
#include "config.h"
Packit 979760
Packit 979760
#include <stdlib.h>
Packit 979760
#include "gdk-pixbuf.h"
Packit 979760
#include <glib-object.h>
Packit 979760
Packit 979760

Packit 979760
Packit 979760
static void
Packit 979760
store_pixel (guchar *pixels,
Packit 979760
	     int pixel,
Packit 979760
	     gboolean alpha)
Packit 979760
{
Packit 979760
	if (alpha) {
Packit 979760
		pixels[0] = pixel >> 24;
Packit 979760
		pixels[1] = pixel >> 16;
Packit 979760
		pixels[2] = pixel >> 8;
Packit 979760
		pixels[3] = pixel;
Packit 979760
	} else {
Packit 979760
		pixels[0] = pixel >> 16;
Packit 979760
		pixels[1] = pixel >> 8;
Packit 979760
		pixels[2] = pixel;
Packit 979760
	}
Packit 979760
}
Packit 979760
Packit 979760
static void
Packit 979760
fill_with_pixel (GdkPixbuf *pixbuf,
Packit 979760
		 int pixel)
Packit 979760
{
Packit 979760
	int x, y;
Packit 979760
	
Packit 979760
	for (x = 0; x < gdk_pixbuf_get_width (pixbuf); x++) {
Packit 979760
		for (y = 0; y < gdk_pixbuf_get_height (pixbuf); y++) {
Packit 979760
			store_pixel (gdk_pixbuf_get_pixels (pixbuf)
Packit 979760
				     + y * gdk_pixbuf_get_rowstride (pixbuf)
Packit 979760
				     + x * gdk_pixbuf_get_n_channels (pixbuf),
Packit 979760
				     pixel,
Packit 979760
				     gdk_pixbuf_get_has_alpha (pixbuf));
Packit 979760
		}
Packit 979760
	}
Packit 979760
}
Packit 979760
Packit 979760
static int
Packit 979760
load_pixel (const guchar *pixels,
Packit 979760
	    gboolean alpha)
Packit 979760
{
Packit 979760
	if (alpha)
Packit 979760
		return (((((pixels[0] << 8) | pixels[1]) << 8) | pixels[2]) << 8) | pixels[3];
Packit 979760
	else
Packit 979760
		return (((pixels[0] << 8) | pixels[1]) << 8) | pixels[2];
Packit 979760
}
Packit 979760
Packit 979760
static gboolean
Packit 979760
simple_composite_test_one (GdkInterpType type,
Packit 979760
			   int source_pixel,
Packit 979760
			   gboolean source_alpha,
Packit 979760
			   int destination_pixel,
Packit 979760
			   gboolean destination_alpha,
Packit 979760
			   int expected_result)
Packit 979760
{
Packit 979760
	GdkPixbuf *source_pixbuf;
Packit 979760
	GdkPixbuf *destination_pixbuf;
Packit 979760
	int result_pixel;
Packit 979760
Packit 979760
	source_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, source_alpha, 8, 32, 32);
Packit 979760
	destination_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, destination_alpha, 8, 32, 32);
Packit 979760
	
Packit 979760
	fill_with_pixel (source_pixbuf, source_pixel);
Packit 979760
	fill_with_pixel (destination_pixbuf, destination_pixel);
Packit 979760
Packit 979760
	gdk_pixbuf_composite (source_pixbuf, destination_pixbuf,
Packit 979760
			      0, 0, 32, 32, 0, 0, 1, 1, type, 0xFF);
Packit 979760
Packit 979760
	result_pixel = load_pixel (gdk_pixbuf_get_pixels (destination_pixbuf)
Packit 979760
				   + 16 * gdk_pixbuf_get_rowstride (destination_pixbuf)
Packit 979760
				   + 16 * gdk_pixbuf_get_n_channels (destination_pixbuf),
Packit 979760
				   destination_alpha);
Packit 979760
	  
Packit 979760
	g_object_unref (source_pixbuf);
Packit 979760
	g_object_unref (destination_pixbuf);
Packit 979760
Packit 979760
	if (result_pixel != expected_result) {
Packit 979760
		char *interpolation_type, *source_string, *destination_string, *result_string, *expected_string;
Packit 979760
Packit 979760
		switch (type) {
Packit 979760
		case GDK_INTERP_NEAREST:  interpolation_type = "GDK_INTERP_NEAREST"; break;
Packit 979760
		case GDK_INTERP_TILES:    interpolation_type = "GDK_INTERP_TILES"; break;
Packit 979760
		case GDK_INTERP_BILINEAR: interpolation_type = "GDK_INTERP_BILINEAR"; break;
Packit 979760
		case GDK_INTERP_HYPER:    interpolation_type = "GDK_INTERP_HYPER"; break;
Packit 979760
		default:                  interpolation_type = "???";
Packit 979760
		}
Packit 979760
Packit 979760
		if (source_alpha) {
Packit 979760
			source_string = g_strdup_printf ("0x%08X", source_pixel);
Packit 979760
		} else {
Packit 979760
			source_string = g_strdup_printf ("0x%06X", source_pixel);
Packit 979760
		}
Packit 979760
Packit 979760
		if (destination_alpha) {
Packit 979760
			destination_string = g_strdup_printf ("0x%08X", destination_pixel);
Packit 979760
			result_string = g_strdup_printf ("0x%08X", result_pixel);
Packit 979760
			expected_string = g_strdup_printf ("0x%08X", expected_result);
Packit 979760
		} else {
Packit 979760
			destination_string = g_strdup_printf ("0x%06X", destination_pixel);
Packit 979760
			result_string = g_strdup_printf ("0x%06X", result_pixel);
Packit 979760
			expected_string = g_strdup_printf ("0x%06X", expected_result);
Packit 979760
		}
Packit 979760
Packit 979760
		g_message ("simple_composite_test (%s): composite %s on top of %s, expected %s, got %s",
Packit 979760
			   interpolation_type,
Packit 979760
			   source_string, destination_string, expected_string, result_string);
Packit 979760
		return FALSE;
Packit 979760
	}
Packit 979760
Packit 979760
	return TRUE;
Packit 979760
}
Packit 979760
Packit 979760
static gboolean
Packit 979760
simple_composite_test_one_type (GdkInterpType type)
Packit 979760
{
Packit 979760
	gboolean success;
Packit 979760
Packit 979760
	success = TRUE;
Packit 979760
Packit 979760
	/* There are only a few trivial cases in here.
Packit 979760
	 * But these were enough to expose the problems in the old composite code.
Packit 979760
	 */
Packit 979760
Packit 979760
	/* Non-alpha into non-alpha. */
Packit 979760
	success &= simple_composite_test_one (type, 0x000000, FALSE, 0x000000, FALSE, 0x000000);
Packit 979760
	success &= simple_composite_test_one (type, 0x000000, FALSE, 0xFFFFFF, FALSE, 0x000000);
Packit 979760
	success &= simple_composite_test_one (type, 0xFF0000, FALSE, 0x000000, FALSE, 0xFF0000);
Packit 979760
	success &= simple_composite_test_one (type, 0x00FF00, FALSE, 0x000000, FALSE, 0x00FF00);
Packit 979760
	success &= simple_composite_test_one (type, 0x0000FF, FALSE, 0x000000, FALSE, 0x0000FF);
Packit 979760
	success &= simple_composite_test_one (type, 0x000000, FALSE, 0xFF0000, FALSE, 0x000000);
Packit 979760
	success &= simple_composite_test_one (type, 0x000000, FALSE, 0x00FF00, FALSE, 0x000000);
Packit 979760
	success &= simple_composite_test_one (type, 0x000000, FALSE, 0x0000FF, FALSE, 0x000000);
Packit 979760
	success &= simple_composite_test_one (type, 0x00FF00, FALSE, 0xFFFFFF, FALSE, 0x00FF00);
Packit 979760
	success &= simple_composite_test_one (type, 0xFFFFFF, FALSE, 0xFFFFFF, FALSE, 0xFFFFFF);
Packit 979760
Packit 979760
	/* Alpha into non-alpha. */
Packit 979760
	success &= simple_composite_test_one (type, 0x00000000, TRUE, 0x000000, FALSE, 0x000000);
Packit 979760
	success &= simple_composite_test_one (type, 0x00000000, TRUE, 0xFFFFFF, FALSE, 0xFFFFFF);
Packit 979760
	success &= simple_composite_test_one (type, 0x0000007F, TRUE, 0xFFFFFF, FALSE, 0x808080);
Packit 979760
	success &= simple_composite_test_one (type, 0x00000080, TRUE, 0xFFFFFF, FALSE, 0x7F7F7F);
Packit 979760
	success &= simple_composite_test_one (type, 0x000000FF, TRUE, 0xFFFFFF, FALSE, 0x000000);
Packit 979760
	success &= simple_composite_test_one (type, 0x000000FF, TRUE, 0xFFFFFF, FALSE, 0x000000);
Packit 979760
	success &= simple_composite_test_one (type, 0xFF0000FF, TRUE, 0x000000, FALSE, 0xFF0000);
Packit 979760
	success &= simple_composite_test_one (type, 0x00FF00FF, TRUE, 0x000000, FALSE, 0x00FF00);
Packit 979760
	success &= simple_composite_test_one (type, 0x0000FFFF, TRUE, 0x000000, FALSE, 0x0000FF);
Packit 979760
	success &= simple_composite_test_one (type, 0x00000000, TRUE, 0xFF0000, FALSE, 0xFF0000);
Packit 979760
	success &= simple_composite_test_one (type, 0x00000000, TRUE, 0x00FF00, FALSE, 0x00FF00);
Packit 979760
	success &= simple_composite_test_one (type, 0x00000000, TRUE, 0x0000FF, FALSE, 0x0000FF);
Packit 979760
	success &= simple_composite_test_one (type, 0x00FF0080, TRUE, 0xFFFFFF, FALSE, 0x7FFF7F);
Packit 979760
	success &= simple_composite_test_one (type, 0xFFFFFFFF, TRUE, 0xFFFFFF, FALSE, 0xFFFFFF);
Packit 979760
Packit 979760
	/* Non-alpha into alpha. */
Packit 979760
	success &= simple_composite_test_one (type, 0x000000, FALSE, 0x00000000, TRUE, 0x000000FF);
Packit 979760
	success &= simple_composite_test_one (type, 0x000000, FALSE, 0xFFFFFFFF, TRUE, 0x000000FF);
Packit 979760
	success &= simple_composite_test_one (type, 0xFF0000, FALSE, 0x00000000, TRUE, 0xFF0000FF);
Packit 979760
	success &= simple_composite_test_one (type, 0x00FF00, FALSE, 0x00000000, TRUE, 0x00FF00FF);
Packit 979760
	success &= simple_composite_test_one (type, 0x0000FF, FALSE, 0x00000000, TRUE, 0x0000FFFF);
Packit 979760
	success &= simple_composite_test_one (type, 0x000000, FALSE, 0xFF0000FF, TRUE, 0x000000FF);
Packit 979760
	success &= simple_composite_test_one (type, 0x000000, FALSE, 0x00FF00FF, TRUE, 0x000000FF);
Packit 979760
	success &= simple_composite_test_one (type, 0x000000, FALSE, 0x0000FFFF, TRUE, 0x000000FF);
Packit 979760
	success &= simple_composite_test_one (type, 0x00FF00, FALSE, 0xFFFFFF00, TRUE, 0x00FF00FF);
Packit 979760
	success &= simple_composite_test_one (type, 0xFFFFFF, FALSE, 0xFFFFFFFF, TRUE, 0xFFFFFFFF);
Packit 979760
Packit 979760
	/* Alpha into alpha. */
Packit 979760
	success &= simple_composite_test_one (type, 0x00000000, TRUE, 0x00000000, TRUE, 0x00000000);
Packit 979760
	success &= simple_composite_test_one (type, 0x00000000, TRUE, 0xFFFFFFFF, TRUE, 0xFFFFFFFF);
Packit 979760
	success &= simple_composite_test_one (type, 0x0000007F, TRUE, 0xFFFFFFFF, TRUE, 0x808080FF);
Packit 979760
	success &= simple_composite_test_one (type, 0x00000080, TRUE, 0xFFFFFFFF, TRUE, 0x7F7F7FFF);
Packit 979760
	success &= simple_composite_test_one (type, 0x000000FF, TRUE, 0xFFFFFFFF, TRUE, 0x000000FF);
Packit 979760
	success &= simple_composite_test_one (type, 0xFF0000FF, TRUE, 0x00000000, TRUE, 0xFF0000FF);
Packit 979760
	success &= simple_composite_test_one (type, 0x00FF00FF, TRUE, 0x00000000, TRUE, 0x00FF00FF);
Packit 979760
	success &= simple_composite_test_one (type, 0x0000FFFF, TRUE, 0x00000000, TRUE, 0x0000FFFF);
Packit 979760
	success &= simple_composite_test_one (type, 0x00000000, TRUE, 0xFF0000FF, TRUE, 0xFF0000FF);
Packit 979760
	success &= simple_composite_test_one (type, 0x00000000, TRUE, 0x00FF00FF, TRUE, 0x00FF00FF);
Packit 979760
	success &= simple_composite_test_one (type, 0x00000000, TRUE, 0x0000FFFF, TRUE, 0x0000FFFF);
Packit 979760
	success &= simple_composite_test_one (type, 0x00FF0080, TRUE, 0xFFFFFF00, TRUE, 0x00FF0080);
Packit 979760
	success &= simple_composite_test_one (type, 0xFF000080, TRUE, 0x00FF0040, TRUE, 0xCC32009F);
Packit 979760
	success &= simple_composite_test_one (type, 0xFFFFFFFF, TRUE, 0xFFFFFFFF, TRUE, 0xFFFFFFFF);
Packit 979760
Packit 979760
	return success;
Packit 979760
}
Packit 979760
Packit 979760
static gboolean
Packit 979760
simple_composite_test (void)
Packit 979760
{
Packit 979760
	gboolean success;
Packit 979760
Packit 979760
	success = TRUE;
Packit 979760
Packit 979760
	success &= simple_composite_test_one_type (GDK_INTERP_NEAREST);
Packit 979760
	success &= simple_composite_test_one_type (GDK_INTERP_TILES);
Packit 979760
	success &= simple_composite_test_one_type (GDK_INTERP_BILINEAR);
Packit 979760
	success &= simple_composite_test_one_type (GDK_INTERP_HYPER);
Packit 979760
Packit 979760
	return success;
Packit 979760
}
Packit 979760
Packit 979760
int
Packit 979760
main (int argc, char **argv)
Packit 979760
{
Packit 979760
	int result;
Packit 979760
Packit 979760
	result = EXIT_SUCCESS;
Packit 979760
Packit 979760
	/* Run some tests. */
Packit 979760
	if (!simple_composite_test ()) {
Packit 979760
		result = EXIT_FAILURE;
Packit 979760
	}
Packit 979760
Packit 979760
	return result;
Packit 979760
}