Fix compiler warnings on windows
[platform/core/graphics/tizenvg.git] / src / loaders / jpg / tvgJpgLoader.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
23 #include <memory.h>
24 #include "tvgLoader.h"
25 #include "tvgJpgLoader.h"
26
27 /************************************************************************/
28 /* Internal Class Implementation                                        */
29 /************************************************************************/
30
31 void JpgLoader::clear()
32 {
33     jpgdDelete(decoder);
34     if (freeData) free(data);
35     decoder = nullptr;
36     data = nullptr;
37     freeData = false;
38 }
39
40
41 /************************************************************************/
42 /* External Class Implementation                                        */
43 /************************************************************************/
44
45
46 JpgLoader::~JpgLoader()
47 {
48     jpgdDelete(decoder);
49     if (freeData) free(data);
50     free(image);
51 }
52
53
54 bool JpgLoader::open(const string& path)
55 {
56     clear();
57
58     int width, height;
59     decoder = jpgdHeader(path.c_str(), &width, &height);
60     if (!decoder) return false;
61
62     w = static_cast<float>(width);
63     h = static_cast<float>(height);
64
65     return true;
66 }
67
68
69 bool JpgLoader::open(const char* data, uint32_t size, bool copy)
70 {
71     clear();
72
73     if (copy) {
74         this->data = (char *) malloc(size);
75         if (!this->data) return false;
76         memcpy((char *)this->data, data, size);
77         freeData = true;
78     } else {
79         this->data = (char *) data;
80         freeData = false;
81     }
82
83     int width, height;
84     decoder = jpgdHeader(this->data, size, &width, &height);
85     if (!decoder) return false;
86
87     w = static_cast<float>(width);
88     h = static_cast<float>(height);
89
90     return true;
91 }
92
93
94
95 bool JpgLoader::read()
96 {
97     if (!decoder || w <= 0 || h <= 0) return false;
98
99     TaskScheduler::request(this);
100
101     return true;
102 }
103
104
105 bool JpgLoader::close()
106 {
107     this->done();
108     clear();
109     return true;
110 }
111
112
113 unique_ptr<Surface> JpgLoader::bitmap()
114 {
115     this->done();
116
117     if (!image) return nullptr;
118
119     auto surface = static_cast<Surface*>(malloc(sizeof(Surface)));
120     surface->buffer = (uint32_t*)(image);
121     surface->stride = static_cast<uint32_t>(w);
122     surface->w = static_cast<uint32_t>(w);
123     surface->h = static_cast<uint32_t>(h);
124     surface->cs = SwCanvas::ARGB8888;
125
126     return unique_ptr<Surface>(surface);
127 }
128
129
130 void JpgLoader::run(unsigned tid)
131 {
132     if (image) {
133         free(image);
134         image = nullptr;
135     }
136     image = jpgdDecompress(decoder);
137 }