2 * Copyright (c) 2021 - 2022 Samsung Electronics Co., Ltd. All rights reserved.
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:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
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
23 #include "tvgLoader.h"
24 #include "tvgPngLoader.h"
27 /************************************************************************/
28 /* Internal Class Implementation */
29 /************************************************************************/
32 static inline uint32_t PREMULTIPLY(uint32_t c)
35 return (c & 0xff000000) + ((((c >> 8) & 0xff) * a) & 0xff00) + ((((c & 0x00ff00ff) * a) >> 8) & 0x00ff00ff);
39 static void _premultiply(uint32_t* data, uint32_t w, uint32_t h)
42 for (uint32_t y = 0; y < h; ++y, buffer += w) {
44 for (uint32_t x = 0; x < w; ++x, ++src) {
45 *src = PREMULTIPLY(*src);
51 void PngLoader::clear()
53 lodepng_state_cleanup(&state);
55 if (freeData) free(data);
62 /************************************************************************/
63 /* External Class Implementation */
64 /************************************************************************/
66 PngLoader::PngLoader()
68 lodepng_state_init(&state);
72 PngLoader::~PngLoader()
74 if (freeData) free(data);
79 bool PngLoader::open(const string& path)
83 auto pngFile = fopen(path.c_str(), "rb");
84 if (!pngFile) return false;
89 if (fseek(pngFile, 0, SEEK_END) < 0) goto finalize;
90 if (((size = ftell(pngFile)) < 1)) goto finalize;
91 if (fseek(pngFile, 0, SEEK_SET)) goto finalize;
93 data = (unsigned char *) malloc(size);
94 if (!data) goto finalize;
98 if (fread(data, size, 1, pngFile) < 1) goto failure;
100 lodepng_state_init(&state);
102 unsigned int width, height;
103 if (lodepng_inspect(&width, &height, &state, data, size) > 0) goto failure;
105 w = static_cast<float>(width);
106 h = static_cast<float>(height);
120 bool PngLoader::open(const char* data, uint32_t size, bool copy)
124 lodepng_state_init(&state);
126 unsigned int width, height;
127 if (lodepng_inspect(&width, &height, &state, (unsigned char*)(data), size) > 0) return false;
130 this->data = (unsigned char *) malloc(size);
131 if (!this->data) return false;
132 memcpy((unsigned char *)this->data, data, size);
135 this->data = (unsigned char *) data;
139 w = static_cast<float>(width);
140 h = static_cast<float>(height);
147 bool PngLoader::read()
149 if (!data || w <= 0 || h <= 0) return false;
151 TaskScheduler::request(this);
157 bool PngLoader::close()
165 unique_ptr<Surface> PngLoader::bitmap()
169 if (!image) return nullptr;
171 auto surface = static_cast<Surface*>(malloc(sizeof(Surface)));
172 surface->buffer = (uint32_t*)(image);
176 surface->cs = SwCanvas::ARGB8888;
178 return unique_ptr<Surface>(surface);
182 void PngLoader::run(unsigned tid)
188 auto width = static_cast<unsigned>(w);
189 auto height = static_cast<unsigned>(h);
191 lodepng_decode(&image, &width, &height, &state, data, size);
193 _premultiply((uint32_t*)(image), width, height);