forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender_tests.cpp
284 lines (242 loc) · 8.77 KB
/
render_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Aseprite Render Library
// Copyright (c) 2019-2023 Igara Studio S.A.
// Copyright (c) 2001-2018 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gtest/gtest.h>
#include "render/render.h"
#include "doc/cel.h"
#include "doc/document.h"
#include "doc/image.h"
#include "doc/layer.h"
#include "doc/palette.h"
#include "doc/primitives.h"
#include <memory>
using namespace doc;
using namespace render;
template<typename T>
class RenderAllModes : public testing::Test {
protected:
RenderAllModes() { }
};
typedef testing::Types<RgbTraits, GrayscaleTraits, IndexedTraits> ImageAllTraits;
TYPED_TEST_SUITE(RenderAllModes, ImageAllTraits);
// a b
// c d
#define EXPECT_2X2_PIXELS(image, a, b, c, d) \
EXPECT_EQ(a, get_pixel(image, 0, 0)); \
EXPECT_EQ(b, get_pixel(image, 1, 0)); \
EXPECT_EQ(c, get_pixel(image, 0, 1)); \
EXPECT_EQ(d, get_pixel(image, 1, 1))
// a b c d
// e f g h
// i j k l
// m n o p
#define EXPECT_4X4_PIXELS(image, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) \
EXPECT_EQ(a, get_pixel(image, 0, 0)); \
EXPECT_EQ(b, get_pixel(image, 1, 0)); \
EXPECT_EQ(c, get_pixel(image, 2, 0)); \
EXPECT_EQ(d, get_pixel(image, 3, 0)); \
EXPECT_EQ(e, get_pixel(image, 0, 1)); \
EXPECT_EQ(f, get_pixel(image, 1, 1)); \
EXPECT_EQ(g, get_pixel(image, 2, 1)); \
EXPECT_EQ(h, get_pixel(image, 3, 1)); \
EXPECT_EQ(i, get_pixel(image, 0, 2)); \
EXPECT_EQ(j, get_pixel(image, 1, 2)); \
EXPECT_EQ(k, get_pixel(image, 2, 2)); \
EXPECT_EQ(l, get_pixel(image, 3, 2)); \
EXPECT_EQ(m, get_pixel(image, 0, 3)); \
EXPECT_EQ(n, get_pixel(image, 1, 3)); \
EXPECT_EQ(o, get_pixel(image, 2, 3)); \
EXPECT_EQ(p, get_pixel(image, 3, 3))
TEST(Render, Basic)
{
std::shared_ptr<Document> doc = std::make_shared<Document>();
doc->sprites().add(Sprite::MakeStdSprite(ImageSpec(ColorMode::INDEXED, 2, 2)));
Image* src = doc->sprite()->root()->firstLayer()->cel(0)->image();
clear_image(src, 2);
std::unique_ptr<Image> dst(Image::create(IMAGE_INDEXED, 2, 2));
clear_image(dst.get(), 1);
EXPECT_2X2_PIXELS(dst.get(), 1, 1, 1, 1);
Render render;
render.renderSprite(dst.get(), doc->sprite(), frame_t(0));
EXPECT_2X2_PIXELS(dst.get(), 2, 2, 2, 2);
}
TYPED_TEST(RenderAllModes, CheckDefaultBackgroundMode)
{
typedef TypeParam ImageTraits;
std::shared_ptr<Document> doc = std::make_shared<Document>();
doc->sprites().add(Sprite::MakeStdSprite(ImageSpec(ImageTraits::color_mode, 2, 2)));
EXPECT_TRUE(!doc->sprite()->root()->firstLayer()->isBackground());
Image* src = doc->sprite()->root()->firstLayer()->cel(0)->image();
clear_image(src, 0);
put_pixel(src, 1, 1, 1);
std::unique_ptr<Image> dst(Image::create(ImageTraits::pixel_format, 2, 2));
clear_image(dst.get(), 1);
EXPECT_2X2_PIXELS(dst.get(), 1, 1, 1, 1);
Render render;
render.renderSprite(dst.get(), doc->sprite(), frame_t(0));
// Default background mode is to set all pixels to transparent color
EXPECT_2X2_PIXELS(dst.get(), 0, 0, 0, 1);
}
TEST(Render, DefaultBackgroundModeWithNonzeroTransparentIndex)
{
std::shared_ptr<Document> doc = std::make_shared<Document>();
doc->sprites().add(Sprite::MakeStdSprite(ImageSpec(ColorMode::INDEXED, 2, 2)));
doc->sprite()->setTransparentColor(2); // Transparent color is index 2
EXPECT_TRUE(!doc->sprite()->root()->firstLayer()->isBackground());
Image* src = doc->sprite()->root()->firstLayer()->cel(0)->image();
clear_image(src, 2);
put_pixel(src, 1, 1, 1);
std::unique_ptr<Image> dst(Image::create(IMAGE_INDEXED, 2, 2));
clear_image(dst.get(), 1);
EXPECT_2X2_PIXELS(dst.get(), 1, 1, 1, 1);
Render render;
render.renderSprite(dst.get(), doc->sprite(), frame_t(0));
EXPECT_2X2_PIXELS(dst.get(), 2, 2, 2, 1); // Indexed transparent
dst.reset(Image::create(IMAGE_RGB, 2, 2));
clear_image(dst.get(), 1);
EXPECT_2X2_PIXELS(dst.get(), 1, 1, 1, 1);
render.renderSprite(dst.get(), doc->sprite(), frame_t(0));
color_t c1 = doc->sprite()->palette(0)->entry(1);
EXPECT_NE(0, c1);
EXPECT_2X2_PIXELS(dst.get(), 0, 0, 0, c1); // RGB transparent
}
TEST(Render, CheckeredBackground)
{
std::shared_ptr<Document> doc = std::make_shared<Document>();
doc->sprites().add(Sprite::MakeStdSprite(ImageSpec(ColorMode::INDEXED, 4, 4)));
std::unique_ptr<Image> dst(Image::create(IMAGE_INDEXED, 4, 4));
clear_image(dst.get(), 0);
Render render;
BgOptions bg;
bg.type = BgType::CHECKERED;
bg.zoom = true;
bg.colorPixelFormat = IMAGE_INDEXED;
bg.color1 = 1;
bg.color2 = 2;
bg.stripeSize = gfx::Size(1, 1);
render.setBgOptions(bg);
render.renderSprite(dst.get(), doc->sprite(), frame_t(0));
EXPECT_4X4_PIXELS(dst.get(),
1, 2, 1, 2,
2, 1, 2, 1,
1, 2, 1, 2,
2, 1, 2, 1);
bg.stripeSize = gfx::Size(2, 2);
render.setBgOptions(bg);
render.renderSprite(dst.get(), doc->sprite(), frame_t(0));
EXPECT_4X4_PIXELS(dst.get(),
1, 1, 2, 2,
1, 1, 2, 2,
2, 2, 1, 1,
2, 2, 1, 1);
bg.stripeSize = gfx::Size(3, 3);
render.setBgOptions(bg);
render.renderSprite(dst.get(), doc->sprite(), frame_t(0));
EXPECT_4X4_PIXELS(dst.get(),
1, 1, 1, 2,
1, 1, 1, 2,
1, 1, 1, 2,
2, 2, 2, 1);
bg.stripeSize = gfx::Size(1, 1);
render.setProjection(Projection(PixelRatio(1, 1), Zoom(2, 1)));
render.setBgOptions(bg);
render.renderSprite(dst.get(), doc->sprite(), frame_t(0));
EXPECT_4X4_PIXELS(dst.get(),
1, 1, 2, 2,
1, 1, 2, 2,
2, 2, 1, 1,
2, 2, 1, 1);
}
TEST(Render, ZoomAndDstBounds)
{
// Create this image:
// 0 0 0
// 0 4 4
// 0 4 4
std::shared_ptr<Document> doc = std::make_shared<Document>();
doc->sprites().add(Sprite::MakeStdSprite(ImageSpec(ColorMode::INDEXED, 3, 3)));
Image* src = doc->sprite()->root()->firstLayer()->cel(0)->image();
clear_image(src, 0);
fill_rect(src, 1, 1, 2, 2, 4);
std::unique_ptr<Image> dst(Image::create(IMAGE_INDEXED, 4, 4));
clear_image(dst.get(), 0);
Render render;
BgOptions bg;
bg.type = BgType::CHECKERED;
bg.zoom = true;
bg.colorPixelFormat = IMAGE_INDEXED;
bg.color1 = 1;
bg.color2 = 2;
bg.stripeSize = gfx::Size(1, 1);
render.setBgOptions(bg);
render.renderSprite(
dst.get(), doc->sprite(), frame_t(0),
gfx::Clip(1, 1, 0, 0, 2, 2));
EXPECT_4X4_PIXELS(dst.get(),
0, 0, 0, 0,
0, 1, 2, 0,
0, 2, 4, 0,
0, 0, 0, 0);
}
TEST(Render, BugWithMultiplesOf3ZoomFactors)
{
std::shared_ptr<Document> doc = std::make_shared<Document>();
doc->sprites().add(Sprite::MakeStdSprite(ImageSpec(ColorMode::RGB, 4, 4)));
Image* src = doc->sprite()->root()->firstLayer()->cel(0)->image();
clear_image(src, 0);
draw_line(src, 0, 0, 3, 3, rgba(255, 0, 0, 255));
// Added other factors (like 1, 2, 4, etc.) too
int zooms[] = { 1, 2, 3, 4, 6, 8, 9, 12, 15, 16, 18, 21, 24, 27,
30, 32, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60,
63, 66, 69, 72, 75, 78, 81 };
for (int zoom : zooms) {
std::unique_ptr<Image> dst(Image::create(IMAGE_RGB, 4*zoom, 4*zoom));
clear_image(dst.get(), 0);
Render render;
BgOptions bg;
bg.type = BgType::CHECKERED;
bg.zoom = false;
bg.colorPixelFormat = IMAGE_RGB;
bg.color1 = rgba(128, 128, 128, 255);
bg.color2 = rgba(64, 64, 64, 255);
bg.stripeSize = gfx::Size(2, 2);
render.setBgOptions(bg);
render.setProjection(Projection(PixelRatio(1, 1), Zoom(zoom, 1)));
render.renderSprite(
dst.get(), doc->sprite(), frame_t(0),
gfx::Clip(0, 0, 0, 0, 4*zoom, 4*zoom));
for (int y=0; y<dst->height(); ++y) {
for (int x=0; x<dst->width(); ++x) {
color_t c = get_pixel(dst.get(), x, y);
if (x / zoom == y / zoom) {
EXPECT_EQ(c, rgba(255, 0, 0, 255))
<< " zoom=" << zoom << " x=" << x << " y=" << y;
}
else {
EXPECT_NE(c, rgba(255, 0, 0, 255))
<< " zoom=" << zoom << " x=" << x << " y=" << y;
int gridBg = ((x / 2) + (y / 2)) % 2;
if (gridBg == 0) {
EXPECT_EQ(c, rgba(128, 128, 128, 255))
<< " zoom=" << zoom << " x=" << x << " y=" << y;
}
else {
EXPECT_EQ(c, rgba(64, 64, 64, 255))
<< " zoom=" << zoom << " x=" << x << " y=" << y;
}
}
}
}
}
}
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}