Fix compiler warnings on windows
[platform/core/graphics/tizenvg.git] / src / loaders / png / tvgPngLoader.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 #include <memory.h>
23 #include "tvgLoader.h"
24 #include "tvgPngLoader.h"
25
26
27 /************************************************************************/
28 /* Internal Class Implementation                                        */
29 /************************************************************************/
30
31
32 static inline uint32_t PREMULTIPLY(uint32_t c)
33 {
34     auto a = (c >> 24);
35     return (c & 0xff000000) + ((((c >> 8) & 0xff) * a) & 0xff00) + ((((c & 0x00ff00ff) * a) >> 8) & 0x00ff00ff);
36 }
37
38
39 static void _premultiply(uint32_t* data, uint32_t w, uint32_t h)
40 {
41     auto buffer = data;
42     for (uint32_t y = 0; y < h; ++y, buffer += w) {
43         auto src = buffer;
44         for (uint32_t x = 0; x < w; ++x, ++src) {
45             *src = PREMULTIPLY(*src);
46         }
47     }
48 }
49
50
51 void PngLoader::clear()
52 {
53     lodepng_state_cleanup(&state);
54
55     if (freeData) free(data);
56     data = nullptr;
57     size = 0;
58     freeData = false;
59 }
60
61
62 /************************************************************************/
63 /* External Class Implementation                                        */
64 /************************************************************************/
65
66 PngLoader::PngLoader()
67 {
68     lodepng_state_init(&state);
69 }
70
71
72 PngLoader::~PngLoader()
73 {
74     if (freeData) free(data);
75     free(image);
76 }
77
78
79 bool PngLoader::open(const string& path)
80 {
81     clear();
82
83     auto pngFile = fopen(path.c_str(), "rb");
84     if (!pngFile) return false;
85
86     auto ret = false;
87
88     //determine size
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;
92
93     data = (unsigned char *) malloc(size);
94     if (!data) goto finalize;
95
96     freeData = true;
97
98     if (fread(data, size, 1, pngFile) < 1) goto failure;
99
100     lodepng_state_init(&state);
101
102     unsigned int width, height;
103     if (lodepng_inspect(&width, &height, &state, data, size) > 0) goto failure;
104
105     w = static_cast<float>(width);
106     h = static_cast<float>(height);
107     ret = true;
108
109     goto finalize;
110
111 failure:
112     clear();
113
114 finalize:
115     fclose(pngFile);
116     return ret;
117 }
118
119
120 bool PngLoader::open(const char* data, uint32_t size, bool copy)
121 {
122     clear();
123
124     lodepng_state_init(&state);
125
126     unsigned int width, height;
127     if (lodepng_inspect(&width, &height, &state, (unsigned char*)(data), size) > 0) return false;
128
129     if (copy) {
130         this->data = (unsigned char *) malloc(size);
131         if (!this->data) return false;
132         memcpy((unsigned char *)this->data, data, size);
133         freeData = true;
134     } else {
135         this->data = (unsigned char *) data;
136         freeData = false;
137     }
138
139     w = static_cast<float>(width);
140     h = static_cast<float>(height);
141     this->size = size;
142
143     return true;
144 }
145
146
147 bool PngLoader::read()
148 {
149     if (!data || w <= 0 || h <= 0) return false;
150
151     TaskScheduler::request(this);
152
153     return true;
154 }
155
156
157 bool PngLoader::close()
158 {
159     this->done();
160     clear();
161     return true;
162 }
163
164
165 unique_ptr<Surface> PngLoader::bitmap()
166 {
167     this->done();
168
169     if (!image) return nullptr;
170
171     auto surface = static_cast<Surface*>(malloc(sizeof(Surface)));
172     surface->buffer = (uint32_t*)(image);
173     surface->stride = static_cast<uint32_t>(w);
174     surface->w = static_cast<uint32_t>(w);
175     surface->h = static_cast<uint32_t>(h);
176     surface->cs = SwCanvas::ARGB8888;
177
178     return unique_ptr<Surface>(surface);
179 }
180
181
182 void PngLoader::run(unsigned tid)
183 {
184     if (image) {
185         free(image);
186         image = nullptr;
187     }
188     auto width = static_cast<unsigned>(w);
189     auto height = static_cast<unsigned>(h);
190
191     lodepng_decode(&image, &width, &height, &state, data, size);
192
193     _premultiply((uint32_t*)(image), width, height);
194 }