2 * Copyright © 2008 Chris Wilson
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 * Author: Chris Wilson <chris@chris-wilson.co.uk>
27 #include "cairo-test.h"
30 * We wish to check the optimization away of non-fractional translations
31 * for NEAREST surface patterns under a few transformations.
34 static const char png_filename[] = "romedalen.png";
36 /* A single, black pixel */
37 static const uint32_t black_pixel = 0xff000000;
39 static cairo_test_status_t
40 draw (cairo_t *cr, int width, int height)
42 const cairo_test_context_t *ctx = cairo_test_get_context (cr);
44 cairo_surface_t *surface;
45 cairo_pattern_t *pattern;
46 const cairo_matrix_t transform[] = {
48 { -1, 0, 0, 1, 8, 0 },
49 { 1, 0, 0, -1, 0, 8 },
50 { -1, 0, 0, -1, 8, 8 },
52 const cairo_matrix_t ctx_transform[] = {
54 { -1, 0, 0, 1, 14, 0 },
55 { 1, 0, 0, -1, 0, 14 },
56 { -1, 0, 0, -1, 14, 14 },
58 const double colour[][3] = {
66 surface = cairo_image_surface_create_for_data ((uint8_t *) &black_pixel,
69 pattern = cairo_pattern_create_for_surface (surface);
70 cairo_surface_destroy (surface);
72 cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST);
74 surface = cairo_test_create_surface_from_png (ctx, png_filename);
76 /* Fill background white */
77 cairo_set_source_rgb (cr, 1, 1, 1);
80 cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
82 for (k = 0; k < ARRAY_LENGTH (transform); k++) {
83 /* draw a "large" section from an image */
85 cairo_set_matrix(cr, &ctx_transform[k]);
86 cairo_rectangle (cr, 0, 0, 7, 7);
89 cairo_set_source_surface (cr, surface,
90 -cairo_image_surface_get_width (surface)/2.,
91 -cairo_image_surface_get_height (surface)/2.);
92 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
96 cairo_set_source_rgb (cr, colour[k][0], colour[k][1], colour[k][2]);
97 for (j = 4; j <= 6; j++) {
98 for (i = 4; i <= 6; i++) {
99 cairo_matrix_init_translate (&m,
102 cairo_matrix_multiply (&m, &m, &transform[k]);
103 cairo_pattern_set_matrix (pattern, &m);
104 cairo_mask (cr, pattern);
109 cairo_pattern_destroy (pattern);
110 cairo_surface_destroy (surface);
112 return CAIRO_TEST_SUCCESS;
115 CAIRO_TEST (filter_nearest_transformed,
116 "Test sample position when drawing transformed images with FILTER_NEAREST",
117 "filter, nearest", /* keywords */