1
/*
2
 * Copyright © 2013 Samsung Electronics
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: Bryce Harrington <b.harrington@samsung.com>
25
 */
26

            
27
/* This test exercises scaling a png image to smaller pixel dimensions
28
 *
29
 * Currently, this exercises several of pixman's scaling filters.
30
 */
31

            
32
#include "cairo-test.h"
33

            
34
#include <stdio.h>
35
#include <stdlib.h>
36

            
37
#include <cairo.h>
38

            
39
static const char png_filename[] = "quad-color.png";
40

            
41
/* Draw an image scaled down, with antialiasing disabled */
42
static cairo_test_status_t
43
45
draw (cairo_t *cr, int width, int height, cairo_filter_t filter)
44
{
45
45
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
46
    cairo_surface_t *image;
47
    double x_scale, y_scale, scale;
48

            
49
45
    cairo_set_source_rgb (cr, 1, 1, 1);
50
45
    image = cairo_test_create_surface_from_png (ctx, png_filename);
51
45
    x_scale = width * 1.0 / cairo_image_surface_get_width (image);
52
45
    y_scale = height * 1.0 / cairo_image_surface_get_height (image);
53
45
    scale = x_scale < y_scale ? x_scale : y_scale;
54

            
55
45
    cairo_save (cr);
56
45
    cairo_scale (cr, scale, scale);
57
45
    cairo_set_source_surface (cr, image, 0, 0);
58
45
    cairo_pattern_set_filter (cairo_get_source (cr), filter);
59
45
    cairo_paint (cr);
60
45
    cairo_restore (cr);
61
45
    cairo_surface_destroy (image);
62

            
63
45
    return CAIRO_TEST_SUCCESS;
64
}
65

            
66
static cairo_test_status_t
67
9
draw_fast (cairo_t *cr, int width, int height)
68
{
69
9
  return draw (cr, width, height, CAIRO_FILTER_FAST);
70
}
71

            
72
static cairo_test_status_t
73
9
draw_good (cairo_t *cr, int width, int height)
74
{
75
9
  return draw (cr, width, height, CAIRO_FILTER_GOOD);
76
}
77

            
78
static cairo_test_status_t
79
9
draw_best (cairo_t *cr, int width, int height)
80
{
81
9
  return draw (cr, width, height, CAIRO_FILTER_BEST);
82
}
83

            
84
static cairo_test_status_t
85
9
draw_nearest (cairo_t *cr, int width, int height)
86
{
87
9
  return draw (cr, width, height, CAIRO_FILTER_NEAREST);
88
}
89

            
90
static cairo_test_status_t
91
9
draw_bilinear (cairo_t *cr, int width, int height)
92
{
93
9
  return draw (cr, width, height, CAIRO_FILTER_BILINEAR);
94
}
95

            
96
1
CAIRO_TEST (pixman_downscale_fast_96,
97
	    "Tests scaling to equivalent size using the fast filter",
98
	    "image, transform, raster, downscale", /* keywords */
99
	    NULL, /* requirements */
100
	    96, 96,
101
	    NULL, draw_fast)
102

            
103
1
CAIRO_TEST (pixman_downscale_fast_95,
104
	    "Tests downscaling by a single pixel using the fast filter",
105
	    "image, transform, raster, downscale", /* keywords */
106
	    NULL, /* requirements */
107
	    95, 95,
108
	    NULL, draw_fast)
109

            
110
1
CAIRO_TEST (pixman_downscale_fast_24,
111
	    "Tests downscaling by an even multiple using the fast filter",
112
	    "image, transform, raster, downscale", /* keywords */
113
	    NULL, /* requirements */
114
	    24, 24,
115
	    NULL, draw_fast)
116

            
117

            
118
1
CAIRO_TEST (pixman_downscale_good_96,
119
	    "Tests scaling to equivalent size using the good filter",
120
	    "image, transform, raster, downscale", /* keywords */
121
	    NULL, /* requirements */
122
	    96, 96,
123
	    NULL, draw_good)
124

            
125
1
CAIRO_TEST (pixman_downscale_good_95,
126
	    "Tests downscaling by a single pixel using the good filter",
127
	    "image, transform, raster, downscale", /* keywords */
128
	    NULL, /* requirements */
129
	    95, 95,
130
	    NULL, draw_good)
131

            
132
1
CAIRO_TEST (pixman_downscale_good_24,
133
	    "Tests downscaling by an even multiple using the good filter",
134
	    "image, transform, raster, downscale", /* keywords */
135
	    NULL, /* requirements */
136
	    24, 24,
137
	    NULL, draw_good)
138

            
139

            
140
1
CAIRO_TEST (pixman_downscale_best_96,
141
	    "Tests scaling to equivalent size using the best filter",
142
	    "image, transform, raster, downscale", /* keywords */
143
	    NULL, /* requirements */
144
	    96, 96,
145
	    NULL, draw_best)
146

            
147
1
CAIRO_TEST (pixman_downscale_best_95,
148
	    "Tests downscaling by a single pixel using the best filter",
149
	    "image, transform, raster, downscale", /* keywords */
150
	    NULL, /* requirements */
151
	    95, 95,
152
	    NULL, draw_best)
153

            
154
1
CAIRO_TEST (pixman_downscale_best_24,
155
	    "Tests downscaling by an even multiple using the best filter",
156
	    "image, transform, raster, downscale", /* keywords */
157
	    NULL, /* requirements */
158
	    24, 24,
159
	    NULL, draw_best)
160

            
161

            
162
1
CAIRO_TEST (pixman_downscale_nearest_96,
163
	    "Tests scaling to equivalent size using the nearest filter",
164
	    "image, transform, raster, downscale", /* keywords */
165
	    NULL, /* requirements */
166
	    96, 96,
167
	    NULL, draw_nearest)
168

            
169
1
CAIRO_TEST (pixman_downscale_nearest_95,
170
	    "Tests downscaling by a single pixel using the nearest filter",
171
	    "image, transform, raster, downscale", /* keywords */
172
	    NULL, /* requirements */
173
	    95, 95,
174
	    NULL, draw_nearest)
175

            
176
1
CAIRO_TEST (pixman_downscale_nearest_24,
177
	    "Tests downscaling by an even multiple using the nearest filter",
178
	    "image, transform, raster, downscale", /* keywords */
179
	    NULL, /* requirements */
180
	    24, 24,
181
	    NULL, draw_nearest)
182

            
183

            
184
1
CAIRO_TEST (pixman_downscale_bilinear_96,
185
	    "Tests scaling to equivalent size using the bilinear filter",
186
	    "image, transform, raster, downscale", /* keywords */
187
	    NULL, /* requirements */
188
	    96, 96,
189
	    NULL, draw_bilinear)
190

            
191
1
CAIRO_TEST (pixman_downscale_bilinear_95,
192
	    "Tests downscaling by a single pixel using the bilinear filter",
193
	    "image, transform, raster, downscale", /* keywords */
194
	    NULL, /* requirements */
195
	    95, 95,
196
	    NULL, draw_bilinear)
197

            
198
1
CAIRO_TEST (pixman_downscale_bilinear_24,
199
	    "Tests downscaling by an even multiple using the bilinear filter",
200
	    "image, transform, raster, downscale", /* keywords */
201
	    NULL, /* requirements */
202
	    24, 24,
203
	    NULL, draw_bilinear)