prepare tvg v0.8 release
[platform/core/graphics/tizenvg.git] / test / testPicture.cpp
1 /*
2  * Copyright (c) 2021 - 2022 Samsung Electronics Co., Ltd. All rights reserved.
3
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22
23 #include <thorvg.h>
24 #include <string.h>
25 #include <fstream>
26 #include "catch.hpp"
27
28 using namespace tvg;
29 using namespace std;
30
31
32 TEST_CASE("Picture Creation", "[tvgPicture]")
33 {
34     auto picture = Picture::gen();
35     REQUIRE(picture);
36
37     REQUIRE(picture->identifier() == Picture::identifier());
38     REQUIRE(picture->identifier() != Shape::identifier());
39     REQUIRE(picture->identifier() != Scene::identifier());
40 }
41
42 TEST_CASE("Load SVG file", "[tvgPicture]")
43 {
44     auto picture = Picture::gen();
45     REQUIRE(picture);
46
47     //Invalid file
48     REQUIRE(picture->load("invalid.svg") == Result::InvalidArguments);
49
50     //Load Svg file
51     REQUIRE(picture->load(TEST_DIR"/logo.svg") == Result::Success);
52
53     float w, h;
54     REQUIRE(picture->size(&w, &h) == Result::Success);
55 }
56
57 TEST_CASE("Load SVG Data", "[tvgPicture]")
58 {
59     static const char* svg = "<svg height=\"1000\" viewBox=\"0 0 1000 1000\" width=\"1000\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M.10681413.09784845 1000.0527.01592069V1000.0851L.06005738 999.9983Z\" fill=\"#ffffff\" stroke-width=\"3.910218\"/><g fill=\"#252f35\"><g stroke-width=\"3.864492\"><path d=\"M256.61221 100.51736H752.8963V386.99554H256.61221Z\"/><path d=\"M201.875 100.51736H238.366478V386.99554H201.875Z\"/><path d=\"M771.14203 100.51736H807.633508V386.99554H771.14203Z\"/></g><path d=\"M420.82388 380H588.68467V422.805317H420.82388Z\" stroke-width=\"3.227\"/><path d=\"m420.82403 440.7101v63.94623l167.86079 25.5782V440.7101Z\"/><path d=\"M420.82403 523.07258V673.47362L588.68482 612.59701V548.13942Z\"/></g><g fill=\"#222f35\"><path d=\"M420.82403 691.37851 588.68482 630.5019 589 834H421Z\"/><path d=\"m420.82403 852.52249h167.86079v28.64782H420.82403v-28.64782 0 0\"/><path d=\"m439.06977 879.17031c0 0-14.90282 8.49429-18.24574 15.8161-4.3792 9.59153 0 31.63185 0 31.63185h167.86079c0 0 4.3792-22.04032 0-31.63185-3.34292-7.32181-18.24574-15.8161-18.24574-15.8161z\"/></g><g fill=\"#ffffff\"><path d=\"m280 140h15v55l8 10 8-10v-55h15v60l-23 25-23-25z\"/><path d=\"m335 140v80h45v-50h-25v10h10v30h-15v-57h18v-13z\"/></g></svg>";
60
61     auto picture = Picture::gen();
62     REQUIRE(picture);
63
64     //Negative cases
65     REQUIRE(picture->load(nullptr, 100, "") == Result::InvalidArguments);
66     REQUIRE(picture->load(svg, 0, "") == Result::InvalidArguments);
67
68     //Positive cases
69     REQUIRE(picture->load(svg, strlen(svg), "svg") == Result::Success);
70
71     float w, h;
72     REQUIRE(picture->size(&w, &h) == Result::Success);
73     REQUIRE(w == 1000);
74     REQUIRE(h == 1000);
75 }
76
77 TEST_CASE("Load RAW Data", "[tvgPicture]")
78 {
79     auto picture = Picture::gen();
80     REQUIRE(picture);
81
82     string path(TEST_DIR"/rawimage_200x300.raw");
83
84     ifstream file(path);
85     if (!file.is_open()) return;
86     auto data = (uint32_t*)malloc(sizeof(uint32_t) * (200*300));
87     file.read(reinterpret_cast<char *>(data), sizeof (uint32_t) * 200 * 300);
88     file.close();
89
90     //Negative cases
91     REQUIRE(picture->load(nullptr, 200, 300, false) == Result::InvalidArguments);
92     REQUIRE(picture->load(data, 0, 0, false) == Result::InvalidArguments);
93     REQUIRE(picture->load(data, 200, 0, false) == Result::InvalidArguments);
94     REQUIRE(picture->load(data, 0, 300, false) == Result::InvalidArguments);
95
96     //Positive cases
97     REQUIRE(picture->load(data, 200, 300, false) == Result::Success);
98     REQUIRE(picture->load(data, 200, 300, true) == Result::Success);
99
100     float w, h;
101     REQUIRE(picture->size(&w, &h) == Result::Success);
102
103     REQUIRE(w == 200);
104     REQUIRE(h == 300);
105
106     free(data);
107 }
108
109 TEST_CASE("Load PNG file from path", "[tvgPicture]")
110 {
111     auto picture = Picture::gen();
112     REQUIRE(picture);
113
114     //Invalid file
115     REQUIRE(picture->load("invalid.png") == Result::InvalidArguments);
116
117     REQUIRE(picture->load(TEST_DIR"/test.png") == Result::Success);
118
119     float w, h;
120     REQUIRE(picture->size(&w, &h) == Result::Success);
121
122     REQUIRE(w == 512);
123     REQUIRE(h == 512);
124 }
125
126 TEST_CASE("Load PNG file from data", "[tvgPicture]")
127 {
128     auto picture = Picture::gen();
129     REQUIRE(picture);
130
131     //Open file
132     ifstream file(TEST_DIR"/test.png", ios::in | ios::binary);
133     REQUIRE(file.is_open());
134     auto size = sizeof(uint32_t) * (1000*1000);
135     auto data = (char*)malloc(size);
136     file.read(data, size);
137     file.close();
138
139     REQUIRE(picture->load(data, size, "", false) == Result::Success);
140     REQUIRE(picture->load(data, size, "png", true) == Result::Success);
141
142     float w, h;
143     REQUIRE(picture->size(&w, &h) == Result::Success);
144     REQUIRE(w == 512);
145     REQUIRE(h == 512);
146
147     free(data);
148 }
149
150 TEST_CASE("Load JPG file from path", "[tvgPicture]")
151 {
152     auto picture = Picture::gen();
153     REQUIRE(picture);
154
155     //Invalid file
156     REQUIRE(picture->load("invalid.jpg") == Result::InvalidArguments);
157
158     REQUIRE(picture->load(TEST_DIR"/test.jpg") == Result::Success);
159
160     float w, h;
161     REQUIRE(picture->size(&w, &h) == Result::Success);
162
163     REQUIRE(w == 512);
164     REQUIRE(h == 512);
165 }
166
167 TEST_CASE("Load JPG file from data", "[tvgPicture]")
168 {
169     auto picture = Picture::gen();
170     REQUIRE(picture);
171
172     //Open file
173     ifstream file(TEST_DIR"/test.jpg", ios::in | ios::binary);
174     REQUIRE(file.is_open());
175     auto begin = file.tellg();
176     file.seekg(0, ios::end);
177     auto size = file.tellg() - begin;
178     auto data = (char*)malloc(size);
179     file.seekg(0, ios::beg);
180     file.read(data, size);
181     file.close();
182
183     REQUIRE(picture->load(data, size, "", false) == Result::Success);
184     REQUIRE(picture->load(data, size, "jpg", true) == Result::Success);
185
186     float w, h;
187     REQUIRE(picture->size(&w, &h) == Result::Success);
188     REQUIRE(w == 512);
189     REQUIRE(h == 512);
190
191     free(data);
192 }
193
194 TEST_CASE("Load TVG file from path", "[tvgPicture]")
195 {
196     auto picture = Picture::gen();
197     REQUIRE(picture);
198
199     //Invalid file
200     REQUIRE(picture->load("invalid.tvg") == Result::InvalidArguments);
201
202     REQUIRE(picture->load(TEST_DIR"/tag.tvg") == Result::Success);
203
204     float w, h;
205     REQUIRE(picture->size(&w, &h) == Result::Success);
206
207     REQUIRE(w == 1000);
208     REQUIRE(h == 1000);
209 }
210
211 TEST_CASE("Load TVG file from data", "[tvgPicture]")
212 {
213     auto picture = Picture::gen();
214     REQUIRE(picture);
215
216     //Open file
217     ifstream file(TEST_DIR"/tag.tvg", ios::in | ios::binary);
218     REQUIRE(file.is_open());
219     auto begin = file.tellg();
220     file.seekg(0, ios::end);
221     auto size = file.tellg() - begin;
222     auto data = (char*)malloc(size);
223     file.seekg(0, ios::beg);
224     file.read(data, size);
225     file.close();
226
227     REQUIRE(picture->load(data, size, "", false) == Result::Success);
228     REQUIRE(picture->load(data, size, "tvg", true) == Result::Success);
229
230     float w, h;
231     REQUIRE(picture->size(&w, &h) == Result::Success);
232     REQUIRE(w == 1000);
233     REQUIRE(h == 1000);
234
235     free(data);
236 }
237
238 TEST_CASE("Picture Size", "[tvgPicture]")
239 {
240     auto picture = Picture::gen();
241     REQUIRE(picture);
242
243     float w, h;
244     REQUIRE(picture->size(&w, &h) == Result::InsufficientCondition);
245
246     REQUIRE(picture->load(TEST_DIR"/logo.svg") == Result::Success);
247
248     REQUIRE(picture->size(nullptr, nullptr) == Result::Success);
249     REQUIRE(picture->size(100, 100) == Result::Success);
250     REQUIRE(picture->size(&w, &h) == Result::Success);
251     REQUIRE(w == 100);
252     REQUIRE(h == 100);
253
254     REQUIRE(picture->load(TEST_DIR"/tiger.svg") == Result::Success);
255     REQUIRE(picture->size(&w, &h) == Result::Success);
256     REQUIRE(picture->size(w, h) == Result::Success);
257 }
258
259 TEST_CASE("Picture Duplication", "[tvgPicture]")
260 {
261     auto picture = Picture::gen();
262     REQUIRE(picture);
263
264     REQUIRE(picture->load(TEST_DIR"/logo.svg") == Result::Success);
265     REQUIRE(picture->size(100, 100) == Result::Success);
266
267     auto dup = unique_ptr<Picture>((Picture*)picture->duplicate());
268     REQUIRE(dup);
269
270     float w, h;
271     REQUIRE(picture->size(&w, &h) == Result::Success);
272     REQUIRE(w == 100);
273     REQUIRE(h == 100);
274 }
275
276 TEST_CASE("Load SVG file and render", "[tvgPicture]")
277 {
278     REQUIRE(Initializer::init(CanvasEngine::Sw, 0) == Result::Success);
279
280     auto canvas = SwCanvas::gen();
281     REQUIRE(canvas);
282
283     auto buffer = new uint32_t[1000*1000];
284     if (!buffer) return;
285
286     REQUIRE(canvas->target(buffer, 1000, 1000, 1000, SwCanvas::Colorspace::ABGR8888) == Result::Success);
287
288     auto picture = Picture::gen();
289     REQUIRE(picture);
290
291     REQUIRE(picture->load(TEST_DIR"/tag.svg") == Result::Success);
292     REQUIRE(picture->size(100, 100) == Result::Success);
293
294     REQUIRE(canvas->push(move(picture)) == Result::Success);
295     REQUIRE(canvas->draw() == Result::Success);
296     REQUIRE(canvas->sync() == Result::Success);
297
298     REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
299
300     delete[] buffer;
301 }
302
303 TEST_CASE("Load PNG file and render", "[tvgPicture]")
304 {
305     REQUIRE(Initializer::init(CanvasEngine::Sw, 0) == Result::Success);
306
307     auto canvas = SwCanvas::gen();
308     REQUIRE(canvas);
309
310     uint32_t buffer[100*100];
311     REQUIRE(canvas->target(buffer, 100, 100, 100, SwCanvas::Colorspace::ABGR8888) == Result::Success);
312
313     auto picture = Picture::gen();
314     REQUIRE(picture);
315
316     REQUIRE(picture->load(TEST_DIR"/test.png") == Result::Success);
317     REQUIRE(picture->opacity(192) == Result::Success);
318     REQUIRE(picture->scale(5.0) == Result::Success);
319
320     REQUIRE(canvas->push(move(picture)) == Result::Success);
321
322     REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
323 }
324
325 TEST_CASE("Load JPG file and render", "[tvgPicture]")
326 {
327     REQUIRE(Initializer::init(CanvasEngine::Sw, 0) == Result::Success);
328
329     auto canvas = SwCanvas::gen();
330     REQUIRE(canvas);
331
332     uint32_t buffer[100*100];
333     REQUIRE(canvas->target(buffer, 100, 100, 100, SwCanvas::Colorspace::ABGR8888) == Result::Success);
334
335     auto picture = Picture::gen();
336     REQUIRE(picture);
337
338     REQUIRE(picture->load(TEST_DIR"/test.jpg") == Result::Success);
339
340     REQUIRE(canvas->push(move(picture)) == Result::Success);
341
342     REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
343 }
344
345 TEST_CASE("Load TVG file and render", "[tvgPicture]")
346 {
347     REQUIRE(Initializer::init(CanvasEngine::Sw, 0) == Result::Success);
348
349     auto canvas = SwCanvas::gen();
350     REQUIRE(canvas);
351
352     auto buffer = new uint32_t[1000*1000];
353     if (!buffer) return;
354
355     REQUIRE(canvas->target(buffer, 1000, 1000, 1000, SwCanvas::Colorspace::ABGR8888) == Result::Success);
356
357     auto pictureTag = Picture::gen();
358     REQUIRE(pictureTag);
359     REQUIRE(pictureTag->load(TEST_DIR"/tag.tvg") == Result::Success);
360     REQUIRE(canvas->push(move(pictureTag)) == Result::Success);
361
362     auto pictureTest = Picture::gen();
363     REQUIRE(pictureTest);
364     REQUIRE(pictureTest->load(TEST_DIR"/test.tvg") == Result::Success);
365     REQUIRE(canvas->push(move(pictureTest)) == Result::Success);
366
367     REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
368
369     delete[] buffer;
370 }
371
372 TEST_CASE("Load RAW file and render", "[tvgPicture]")
373 {
374     REQUIRE(Initializer::init(CanvasEngine::Sw, 0) == Result::Success);
375
376     auto canvas = SwCanvas::gen();
377     REQUIRE(canvas);
378
379     uint32_t buffer[100*100];
380     REQUIRE(canvas->target(buffer, 100, 100, 100, SwCanvas::Colorspace::ABGR8888) == Result::Success);
381
382     auto picture = Picture::gen();
383     REQUIRE(picture);
384
385     string path(TEST_DIR"/rawimage_200x300.raw");
386
387     ifstream file(path);
388     if (!file.is_open()) return;
389     auto data = (uint32_t*)malloc(sizeof(uint32_t) * (200*300));
390     if (!data) return;
391     file.read(reinterpret_cast<char *>(data), sizeof (uint32_t) * 200 * 300);
392     file.close();
393
394     REQUIRE(picture->load(data, 200, 300, false) == Result::Success);
395     REQUIRE(picture->size(100, 150) == Result::Success);
396
397     REQUIRE(canvas->push(move(picture)) == Result::Success);
398
399     REQUIRE(Initializer::term(CanvasEngine::Sw) == Result::Success);
400
401     free(data);
402 }