Skip to content
0.2.0-dev Download ZIP
110 lines ·3.4 KB Raw
1/*
2 * Copyright (c) 2020 - 2026 ThorVG project. 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 "tvgPngLoader.h"
24
25/************************************************************************/
26/* Internal Class Implementation */
27/************************************************************************/
28
29void PngLoader::clear()
30{
31 png_image_free(image);
32 tvg::free(image);
33 image = nullptr;
34}
35
36/************************************************************************/
37/* External Class Implementation */
38/************************************************************************/
39
40PngLoader::PngLoader() : BitmapLoader(FileType::Png)
41{
42 image = tvg::calloc<png_image>(1, sizeof(png_image));
43 image->version = PNG_IMAGE_VERSION;
44 image->opaque = nullptr;
45}
46
47PngLoader::~PngLoader()
48{
49 clear();
50 tvg::free(surface.buf32);
51}
52
53Result PngLoader::open(const char* path, TVG_UNUSED const LoaderOps& ops)
54{
55#ifdef THORVG_FILE_IO_SUPPORT
56 image->opaque = nullptr;
57
58 if (!png_image_begin_read_from_file(image, path)) return Result::InvalidArguments;
59
60 w = (float)image->width;
61 h = (float)image->height;
62
63 return Result::Success;
64#else
65 return Result::NonSupport;
66#endif
67}
68
69Result PngLoader::open(const char* data, uint32_t size, TVG_UNUSED const LoaderOps& ops)
70{
71 image->opaque = nullptr;
72
73 if (!png_image_begin_read_from_memory(image, data, size)) return Result::InvalidArguments;
74
75 w = (float)image->width;
76 h = (float)image->height;
77
78 return Result::Success;
79}
80
81
82bool PngLoader::read()
83{
84 if (!Loader::read()) return true;
85
86 if (w == 0 || h == 0) return false;
87
88 ColorSpace cs;
89
90 // TODO: we can acquire a pre-multiplied image. See "png_structrp"
91 if (BitmapLoader::cs == ColorSpace::ARGB8888 || BitmapLoader::cs == ColorSpace::ARGB8888S) {
92 image->format = PNG_FORMAT_BGRA;
93 cs = ColorSpace::ARGB8888S;
94 } else {
95 image->format = PNG_FORMAT_RGBA;
96 cs = ColorSpace::ABGR8888S;
97 }
98
99 auto buffer = tvg::malloc<png_byte>(PNG_IMAGE_SIZE((*image)));
100 if (!png_image_finish_read(image, NULL, buffer, 0, NULL)) {
101 tvg::free(buffer);
102 return false;
103 }
104
105 surface.setup(reinterpret_cast<pixel_t*>(buffer), (uint32_t)w, (uint32_t)w, (uint32_t)h, sizeof(uint32_t), cs);
106
107 clear();
108
109 return true;
110}