/* * Copyright (c) 2021 - 2024 the ThorVG project. All rights reserved. * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include #include #include #include "config.h" #include "catch.hpp" using namespace tvg; using namespace std; TEST_CASE("Picture Creation", "[tvgPicture]") { auto picture = Picture::gen(); REQUIRE(picture); REQUIRE(picture->type() == Type::Picture); } TEST_CASE("Load RAW Data", "[tvgPicture]") { auto picture = Picture::gen(); REQUIRE(picture); ifstream file(TEST_DIR"/rawimage_200x300.raw"); if (!file.is_open()) return; auto data = (uint32_t*)malloc(sizeof(uint32_t) * (200*300)); file.read(reinterpret_cast(data), sizeof (uint32_t) * 200 * 300); file.close(); //Negative cases REQUIRE(picture->load(nullptr, 200, 300, true, false) == Result::InvalidArguments); REQUIRE(picture->load(data, 0, 0, true, false) == Result::InvalidArguments); REQUIRE(picture->load(data, 200, 0, true, false) == Result::InvalidArguments); REQUIRE(picture->load(data, 0, 300, true, false) == Result::InvalidArguments); //Positive cases REQUIRE(picture->load(data, 200, 300, true, false) == Result::Success); REQUIRE(picture->load(data, 200, 300, true, true) == Result::Success); float w, h; REQUIRE(picture->size(&w, &h) == Result::Success); REQUIRE(w == 200); REQUIRE(h == 300); free(data); } TEST_CASE("Load RAW file and render", "[tvgPicture]") { REQUIRE(Initializer::init(0) == Result::Success); auto canvas = SwCanvas::gen(); REQUIRE(canvas); uint32_t buffer[100*100]; REQUIRE(canvas->target(buffer, 100, 100, 100, ColorSpace::ABGR8888) == Result::Success); auto picture = Picture::gen(); REQUIRE(picture); ifstream file(TEST_DIR"/rawimage_200x300.raw"); if (!file.is_open()) return; auto data = (uint32_t*)malloc(sizeof(uint32_t) * (200*300)); if (!data) return; file.read(reinterpret_cast(data), sizeof (uint32_t) * 200 * 300); file.close(); REQUIRE(picture->load(data, 200, 300, true, false) == Result::Success); REQUIRE(picture->size(100, 150) == Result::Success); REQUIRE(canvas->push(std::move(picture)) == Result::Success); REQUIRE(Initializer::term() == Result::Success); free(data); } TEST_CASE("Picture Size", "[tvgPicture]") { auto picture = Picture::gen(); REQUIRE(picture); float w, h; REQUIRE(picture->size(&w, &h) == Result::InsufficientCondition); //Primary ifstream file(TEST_DIR"/rawimage_200x300.raw"); if (!file.is_open()) return; auto data = (uint32_t*)malloc(sizeof(uint32_t) * (200*300)); file.read(reinterpret_cast(data), sizeof (uint32_t) * 200 * 300); file.close(); REQUIRE(picture->load(data, 200, 300, true, false) == Result::Success); REQUIRE(picture->size(nullptr, nullptr) == Result::Success); REQUIRE(picture->size(100, 100) == Result::Success); REQUIRE(picture->size(&w, &h) == Result::Success); REQUIRE(w == 100); REQUIRE(h == 100); free(data); //Secondary ifstream file2(TEST_DIR"/rawimage_250x375.raw"); if (!file2.is_open()) return; data = (uint32_t*)malloc(sizeof(uint32_t) * (250*375)); file2.read(reinterpret_cast(data), sizeof (uint32_t) * 250 * 375); file2.close(); REQUIRE(picture->load(data, 250, 375, true, false) == Result::Success); REQUIRE(picture->size(&w, &h) == Result::Success); REQUIRE(picture->size(w, h) == Result::Success); free(data); } TEST_CASE("Picture Duplication", "[tvgPicture]") { auto picture = Picture::gen(); REQUIRE(picture); //Primary ifstream file(TEST_DIR"/rawimage_200x300.raw"); if (!file.is_open()) return; auto data = (uint32_t*)malloc(sizeof(uint32_t) * (200*300)); file.read(reinterpret_cast(data), sizeof (uint32_t) * 200 * 300); file.close(); REQUIRE(picture->load(data, 200, 300, true, false) == Result::Success); REQUIRE(picture->size(100, 100) == Result::Success); auto dup = tvg::cast(picture->duplicate()); REQUIRE(dup); float w, h; REQUIRE(picture->size(&w, &h) == Result::Success); REQUIRE(w == 100); REQUIRE(h == 100); free(data); } #ifdef THORVG_SVG_LOADER_SUPPORT TEST_CASE("Load SVG file", "[tvgPicture]") { auto picture = Picture::gen(); REQUIRE(picture); //Invalid file REQUIRE(picture->load("invalid.svg") == Result::InvalidArguments); //Load Svg file REQUIRE(picture->load(TEST_DIR"/logo.svg") == Result::Success); float w, h; REQUIRE(picture->size(&w, &h) == Result::Success); } TEST_CASE("Load SVG Data", "[tvgPicture]") { static const char* svg = ""; auto picture = Picture::gen(); REQUIRE(picture); //Negative cases REQUIRE(picture->load(nullptr, 100, "") == Result::InvalidArguments); REQUIRE(picture->load(svg, 0, "") == Result::InvalidArguments); //Positive cases REQUIRE(picture->load(svg, strlen(svg), "svg") == Result::Success); float w, h; REQUIRE(picture->size(&w, &h) == Result::Success); REQUIRE(w == 1000); REQUIRE(h == 1000); } TEST_CASE("Load SVG file and render", "[tvgPicture]") { REQUIRE(Initializer::init(0) == Result::Success); auto canvas = SwCanvas::gen(); REQUIRE(canvas); auto buffer = new uint32_t[1000*1000]; if (!buffer) return; REQUIRE(canvas->target(buffer, 1000, 1000, 1000, ColorSpace::ABGR8888) == Result::Success); auto picture = Picture::gen(); REQUIRE(picture); REQUIRE(picture->load(TEST_DIR"/tag.svg") == Result::Success); REQUIRE(picture->size(100, 100) == Result::Success); REQUIRE(canvas->push(std::move(picture)) == Result::Success); REQUIRE(canvas->draw() == Result::Success); REQUIRE(canvas->sync() == Result::Success); REQUIRE(Initializer::term() == Result::Success); delete[] buffer; } #endif #ifdef THORVG_PNG_LOADER_SUPPORT TEST_CASE("Load PNG file from path", "[tvgPicture]") { auto picture = Picture::gen(); REQUIRE(picture); //Invalid file REQUIRE(picture->load("invalid.png") == Result::InvalidArguments); REQUIRE(picture->load(TEST_DIR"/test.png") == Result::Success); float w, h; REQUIRE(picture->size(&w, &h) == Result::Success); REQUIRE(w == 512); REQUIRE(h == 512); } TEST_CASE("Load PNG file from data", "[tvgPicture]") { auto picture = Picture::gen(); REQUIRE(picture); //Open file ifstream file(TEST_DIR"/test.png", ios::in | ios::binary); REQUIRE(file.is_open()); auto size = sizeof(uint32_t) * (1000*1000); auto data = (char*)malloc(size); file.read(data, size); file.close(); REQUIRE(picture->load(data, size, "") == Result::Success); REQUIRE(picture->load(data, size, "png", "", true) == Result::Success); float w, h; REQUIRE(picture->size(&w, &h) == Result::Success); REQUIRE(w == 512); REQUIRE(h == 512); free(data); } TEST_CASE("Load PNG file and render", "[tvgPicture]") { REQUIRE(Initializer::init(0) == Result::Success); auto canvas = SwCanvas::gen(); REQUIRE(canvas); uint32_t buffer[100*100]; REQUIRE(canvas->target(buffer, 100, 100, 100, ColorSpace::ABGR8888) == Result::Success); auto picture = Picture::gen(); REQUIRE(picture); REQUIRE(picture->load(TEST_DIR"/test.png") == Result::Success); REQUIRE(picture->opacity(192) == Result::Success); REQUIRE(picture->scale(5.0) == Result::Success); REQUIRE(canvas->push(std::move(picture)) == Result::Success); REQUIRE(Initializer::term() == Result::Success); } #endif #ifdef THORVG_JPG_LOADER_SUPPORT TEST_CASE("Load JPG file from path", "[tvgPicture]") { auto picture = Picture::gen(); REQUIRE(picture); //Invalid file REQUIRE(picture->load("invalid.jpg") == Result::InvalidArguments); REQUIRE(picture->load(TEST_DIR"/test.jpg") == Result::Success); float w, h; REQUIRE(picture->size(&w, &h) == Result::Success); REQUIRE(w == 512); REQUIRE(h == 512); } TEST_CASE("Load JPG file from data", "[tvgPicture]") { auto picture = Picture::gen(); REQUIRE(picture); //Open file ifstream file(TEST_DIR"/test.jpg", ios::in | ios::binary); REQUIRE(file.is_open()); auto begin = file.tellg(); file.seekg(0, ios::end); auto size = file.tellg() - begin; auto data = (char*)malloc(size); file.seekg(0, ios::beg); file.read(data, size); file.close(); REQUIRE(picture->load(data, size, "") == Result::Success); REQUIRE(picture->load(data, size, "jpg", "", true) == Result::Success); float w, h; REQUIRE(picture->size(&w, &h) == Result::Success); REQUIRE(w == 512); REQUIRE(h == 512); free(data); } TEST_CASE("Load JPG file and render", "[tvgPicture]") { REQUIRE(Initializer::init(0) == Result::Success); auto canvas = SwCanvas::gen(); REQUIRE(canvas); uint32_t buffer[100*100]; REQUIRE(canvas->target(buffer, 100, 100, 100, ColorSpace::ABGR8888) == Result::Success); auto picture = Picture::gen(); REQUIRE(picture); REQUIRE(picture->load(TEST_DIR"/test.jpg") == Result::Success); REQUIRE(canvas->push(std::move(picture)) == Result::Success); REQUIRE(Initializer::term() == Result::Success); } #endif #ifdef THORVG_TVG_LOADER_SUPPORT TEST_CASE("Load TVG file from path", "[tvgPicture]") { auto picture = Picture::gen(); REQUIRE(picture); //Invalid file REQUIRE(picture->load("invalid.tvg") == Result::InvalidArguments); REQUIRE(picture->load(TEST_DIR"/tag.tvg") == Result::Success); float w, h; REQUIRE(picture->size(&w, &h) == Result::Success); REQUIRE(w == 1000); REQUIRE(h == 1000); } TEST_CASE("Load TVG file from data", "[tvgPicture]") { auto picture = Picture::gen(); REQUIRE(picture); //Open file ifstream file(TEST_DIR"/tag.tvg", ios::in | ios::binary); REQUIRE(file.is_open()); auto begin = file.tellg(); file.seekg(0, ios::end); auto size = file.tellg() - begin; auto data = (char*)malloc(size); file.seekg(0, ios::beg); file.read(data, size); file.close(); REQUIRE(picture->load(data, size, "") == Result::Success); REQUIRE(picture->load(data, size, "tvg", "", true) == Result::Success); float w, h; REQUIRE(picture->size(&w, &h) == Result::Success); REQUIRE(w == 1000); REQUIRE(h == 1000); free(data); } TEST_CASE("Load TVG file and render", "[tvgPicture]") { REQUIRE(Initializer::init(0) == Result::Success); auto canvas = SwCanvas::gen(); REQUIRE(canvas); auto buffer = new uint32_t[1000*1000]; if (!buffer) return; REQUIRE(canvas->target(buffer, 1000, 1000, 1000, ColorSpace::ABGR8888) == Result::Success); auto pictureTag = Picture::gen(); REQUIRE(pictureTag); REQUIRE(pictureTag->load(TEST_DIR"/tag.tvg") == Result::Success); REQUIRE(canvas->push(std::move(pictureTag)) == Result::Success); auto pictureTest = Picture::gen(); REQUIRE(pictureTest); REQUIRE(pictureTest->load(TEST_DIR"/test.tvg") == Result::Success); REQUIRE(canvas->push(std::move(pictureTest)) == Result::Success); REQUIRE(Initializer::term() == Result::Success); delete[] buffer; } #endif #ifdef THORVG_WEBP_LOADER_SUPPORT TEST_CASE("Load WEBP file from path", "[tvgPicture]") { auto picture = Picture::gen(); REQUIRE(picture); //Invalid file REQUIRE(picture->load("invalid.webp") == Result::InvalidArguments); REQUIRE(picture->load(TEST_DIR"/test.webp") == Result::Success); float w, h; REQUIRE(picture->size(&w, &h) == Result::Success); REQUIRE(w == 512); REQUIRE(h == 512); } TEST_CASE("Load WEBP file from data", "[tvgPicture]") { auto picture = Picture::gen(); REQUIRE(picture); //Open file ifstream file(TEST_DIR"/test.webp", ios::in | ios::binary); REQUIRE(file.is_open()); auto size = sizeof(uint32_t) * (1000*1000); auto data = (char*)malloc(size); file.read(data, size); file.close(); REQUIRE(picture->load(data, size, "") == Result::Success); REQUIRE(picture->load(data, size, "webp", "", true) == Result::Success); float w, h; REQUIRE(picture->size(&w, &h) == Result::Success); REQUIRE(w == 512); REQUIRE(h == 512); free(data); } TEST_CASE("Load WEBP file and render", "[tvgPicture]") { REQUIRE(Initializer::init(0) == Result::Success); auto canvas = SwCanvas::gen(); REQUIRE(canvas); uint32_t buffer[100*100]; REQUIRE(canvas->target(buffer, 100, 100, 100, ColorSpace::ABGR8888) == Result::Success); auto picture = Picture::gen(); REQUIRE(picture); REQUIRE(picture->load(TEST_DIR"/test.webp") == Result::Success); REQUIRE(picture->opacity(192) == Result::Success); REQUIRE(picture->scale(5.0) == Result::Success); REQUIRE(canvas->push(std::move(picture)) == Result::Success); REQUIRE(Initializer::term() == Result::Success); } #endif