1
/*
2
 * Copyright © 2008 Chris Wilson
3
 *
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use, copy,
8
 * modify, merge, publish, distribute, sublicense, and/or sell copies
9
 * of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 *
24
 * Author: Chris Wilson <chris@chris-wilson.co.uk>
25
 */
26

            
27
#include "cairo-test.h"
28

            
29
/*
30
 * We wish to check the optimization away of non-fractional translations
31
 * for NEAREST surface patterns under a few transformations.
32
 */
33

            
34
static const char png_filename[] = "romedalen.png";
35

            
36
/* A single, black pixel */
37
static const uint32_t black_pixel = 0xff000000;
38

            
39
static cairo_test_status_t
40
3
draw (cairo_t *cr, int width, int height)
41
{
42
3
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
43
    unsigned int i, j, k;
44
    cairo_surface_t *surface;
45
    cairo_pattern_t *pattern;
46
3
    const cairo_matrix_t transform[] = {
47
	{  1, 0, 0,  1,  0, 0 },
48
	{ -1, 0, 0,  1,  8, 0 },
49
	{  1, 0, 0, -1,  0, 8 },
50
	{ -1, 0, 0, -1,  8, 8 },
51
    };
52
3
    const cairo_matrix_t ctx_transform[] = {
53
	{  1, 0, 0,  1,   0,  0 },
54
	{ -1, 0, 0,  1,  14,  0 },
55
	{  1, 0, 0, -1,   0, 14 },
56
	{ -1, 0, 0, -1,  14, 14 },
57
    };
58
3
    const double colour[][3] = {
59
	{0, 0, 0},
60
	{1, 0, 0},
61
	{0, 1, 0},
62
	{0, 0, 1},
63
    };
64
    cairo_matrix_t m;
65

            
66
3
    surface = cairo_image_surface_create_for_data ((uint8_t *) &black_pixel,
67
						   CAIRO_FORMAT_ARGB32,
68
						   1, 1, 4);
69
3
    pattern = cairo_pattern_create_for_surface (surface);
70
3
    cairo_surface_destroy (surface);
71

            
72
3
    cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST);
73

            
74
3
    surface = cairo_test_create_surface_from_png (ctx, png_filename);
75

            
76
    /* Fill background white */
77
3
    cairo_set_source_rgb (cr, 1, 1, 1);
78
3
    cairo_paint (cr);
79

            
80
3
    cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
81

            
82
15
    for (k = 0; k < ARRAY_LENGTH (transform); k++) {
83
	/* draw a "large" section from an image */
84
12
	cairo_save (cr); {
85
12
	    cairo_set_matrix(cr, &ctx_transform[k]);
86
12
	    cairo_rectangle (cr, 0, 0, 7, 7);
87
12
	    cairo_clip (cr);
88

            
89
24
	    cairo_set_source_surface (cr, surface,
90
12
				      -cairo_image_surface_get_width (surface)/2.,
91
12
				      -cairo_image_surface_get_height (surface)/2.);
92
12
	    cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
93
12
	    cairo_paint (cr);
94
12
	} cairo_restore (cr);
95

            
96
12
	cairo_set_source_rgb (cr, colour[k][0], colour[k][1], colour[k][2]);
97
48
	for (j = 4; j <= 6; j++) {
98
144
	    for (i = 4; i <= 6; i++) {
99
108
		cairo_matrix_init_translate (&m,
100
108
					     -(2*(i-4) + .1*i),
101
108
					     -(2*(j-4) + .1*j));
102
108
		cairo_matrix_multiply (&m, &m, &transform[k]);
103
108
		cairo_pattern_set_matrix (pattern, &m);
104
108
		cairo_mask (cr, pattern);
105
	    }
106
	}
107
    }
108

            
109
3
    cairo_pattern_destroy (pattern);
110
3
    cairo_surface_destroy (surface);
111

            
112
3
    return CAIRO_TEST_SUCCESS;
113
}
114

            
115
1
CAIRO_TEST (filter_nearest_transformed,
116
	    "Test sample position when drawing transformed images with FILTER_NEAREST",
117
	    "filter, nearest", /* keywords */
118
	    NULL,
119
	    14, 14,
120
	    NULL, draw)