Debian Codegraph @

thirdparty/thorvg/src/loaders/external_png/tvgPngLoader.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2020 - 2024 the 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
29 void PngLoader::clear()
30 {
31 png_image_free(image);
32 free(image);
33 image = nullptr;
34 }
35
36
37 /************************************************************************/
38 /* External Class Implementation */
39 /************************************************************************/
40
41 PngLoader::PngLoader() : ImageLoader(FileType::Png)
42 {
43 image = static_cast<png_imagep>(calloc(1, sizeof(png_image)));
44 image->version = PNG_IMAGE_VERSION;
45 image->opaque = NULL;
46 }
47
48 PngLoader::~PngLoader()
49 {
50 clear();
51 free((void*)surface.buf32);
52 }
53
54
55 bool PngLoader::open(const string& path)
56 {
57 image->opaque = NULL;
58
59 if (!png_image_begin_read_from_file(image, path.c_str())) return false;
60
61 w = (float)image->width;
62 h = (float)image->height;
63
64 return true;
65 }
66
67
68 bool PngLoader::open(const char* data, uint32_t size, bool copy)
69 {
70 #ifdef THORVG_FILE_IO_SUPPORT
71 image->opaque = NULL;
72
73 if (!png_image_begin_read_from_memory(image, data, size)) return false;
74
75 w = (float)image->width;
76 h = (float)image->height;
77
78 return true;
79 #else
80 return false;
81 #endif
82 }
83
84
85 bool PngLoader::read()
86 {
87 if (!LoadModule::read()) return true;
88
89 if (w == 0 || h == 0) return false;
90
91 if (ImageLoader::cs == ColorSpace::ARGB8888 || ImageLoader::cs == ColorSpace::ARGB8888S) {
92 image->format = PNG_FORMAT_BGRA;
93 surface.cs = ColorSpace::ARGB8888S;
94 } else {
95 image->format = PNG_FORMAT_RGBA;
96 surface.cs = ColorSpace::ABGR8888S;
97 }
98
99 auto buffer = static_cast<png_bytep>(malloc(PNG_IMAGE_SIZE((*image))));
100 if (!png_image_finish_read(image, NULL, buffer, 0, NULL)) {
101 free(buffer);
102 return false;
103 }
104
105 //setup the surface
106 surface.buf32 = reinterpret_cast<uint32_t*>(buffer);
107 surface.stride = (uint32_t)w;
108 surface.w = (uint32_t)w;
109 surface.h = (uint32_t)h;
110 surface.channelSize = sizeof(uint32_t);
111 //TODO: we can acquire a pre-multiplied image. See "png_structrp"
112 surface.premultiplied = false;
113
114 clear();
115
116 return true;
117 }