Debian Codegraph @

thirdparty/thorvg/src/renderer/tvgSaver.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2021 - 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 <cstring>
24 #include "tvgCommon.h"
25 #include "tvgSaveModule.h"
26 #include "tvgPaint.h"
27
28 #ifdef THORVG_TVG_SAVER_SUPPORT
29 #include "tvgTvgSaver.h"
30 #endif
31 #ifdef THORVG_GIF_SAVER_SUPPORT
32 #include "tvgGifSaver.h"
33 #endif
34
35 /************************************************************************/
36 /* Internal Class Implementation */
37 /************************************************************************/
38
39 struct Saver::Impl
40 {
41 SaveModule* saveModule = nullptr;
42 Paint* bg = nullptr;
43
44 ~Impl()
45 {
46 delete(saveModule);
47 delete(bg);
48 }
49 };
50
51
52 static SaveModule* _find(FileType type)
53 {
54 switch(type) {
55 case FileType::Tvg: {
56 #ifdef THORVG_TVG_SAVER_SUPPORT
57 return new TvgSaver;
58 #endif
59 break;
60 }
61 case FileType::Gif: {
62 #ifdef THORVG_GIF_SAVER_SUPPORT
63 return new GifSaver;
64 #endif
65 break;
66 }
67 default: {
68 break;
69 }
70 }
71
72 #ifdef THORVG_LOG_ENABLED
73 const char *format;
74 switch(type) {
75 case FileType::Tvg: {
76 format = "TVG";
77 break;
78 }
79 case FileType::Gif: {
80 format = "GIF";
81 break;
82 }
83 default: {
84 format = "???";
85 break;
86 }
87 }
88 TVGLOG("RENDERER", "%s format is not supported", format);
89 #endif
90 return nullptr;
91 }
92
93
94 static SaveModule* _find(const string& path)
95 {
96 auto ext = path.substr(path.find_last_of(".") + 1);
97 if (!ext.compare("tvg")) {
98 return _find(FileType::Tvg);
99 } else if (!ext.compare("gif")) {
100 return _find(FileType::Gif);
101 }
102 return nullptr;
103 }
104
105
106 /************************************************************************/
107 /* External Class Implementation */
108 /************************************************************************/
109
110 Saver::Saver() : pImpl(new Impl())
111 {
112 }
113
114
115 Saver::~Saver()
116 {
117 delete(pImpl);
118 }
119
120
121 Result Saver::save(std::unique_ptr<Paint> paint, const string& path, bool compress) noexcept
122 {
123 auto p = paint.release();
124 if (!p) return Result::MemoryCorruption;
125
126 //Already on saving another resource.
127 if (pImpl->saveModule) {
128 if (P(p)->refCnt == 0) delete(p);
129 return Result::InsufficientCondition;
130 }
131
132 if (auto saveModule = _find(path)) {
133 if (saveModule->save(p, path, compress)) {
134 pImpl->saveModule = saveModule;
135 return Result::Success;
136 } else {
137 if (P(p)->refCnt == 0) delete(p);
138 delete(saveModule);
139 return Result::Unknown;
140 }
141 }
142 if (P(p)->refCnt == 0) delete(p);
143 return Result::NonSupport;
144 }
145
146
147 Result Saver::background(unique_ptr<Paint> paint) noexcept
148 {
149 delete(pImpl->bg);
150 pImpl->bg = paint.release();
151
152 return Result::Success;
153 }
154
155
156 Result Saver::save(unique_ptr<Animation> animation, const string& path, uint32_t quality, uint32_t fps) noexcept
157 {
158 auto a = animation.release();
159 if (!a) return Result::MemoryCorruption;
160
161 //animation holds the picture, it must be 1 at the bottom.
162 auto remove = PP(a->picture())->refCnt <= 1 ? true : false;
163
164 if (tvg::zero(a->totalFrame())) {
165 if (remove) delete(a);
166 return Result::InsufficientCondition;
167 }
168
169 //Already on saving another resource.
170 if (pImpl->saveModule) {
171 if (remove) delete(a);
172 return Result::InsufficientCondition;
173 }
174
175 if (auto saveModule = _find(path)) {
176 if (saveModule->save(a, pImpl->bg, path, quality, fps)) {
177 pImpl->saveModule = saveModule;
178 return Result::Success;
179 } else {
180 if (remove) delete(a);
181 delete(saveModule);
182 return Result::Unknown;
183 }
184 }
185 if (remove) delete(a);
186 return Result::NonSupport;
187 }
188
189
190 Result Saver::sync() noexcept
191 {
192 if (!pImpl->saveModule) return Result::InsufficientCondition;
193 pImpl->saveModule->close();
194 delete(pImpl->saveModule);
195 pImpl->saveModule = nullptr;
196
197 return Result::Success;
198 }
199
200
201 unique_ptr<Saver> Saver::gen() noexcept
202 {
203 return unique_ptr<Saver>(new Saver);
204 }