2 * Copyright (c) 2020-2021 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
22 #include "tvgLoader.h"
24 #ifdef THORVG_SVG_LOADER_SUPPORT
25 #include "tvgSvgLoader.h"
28 #ifdef THORVG_PNG_LOADER_SUPPORT
29 #include "tvgPngLoader.h"
32 #ifdef THORVG_TVG_LOADER_SUPPORT
33 #include "tvgTvgLoader.h"
36 #ifdef THORVG_JPG_LOADER_SUPPORT
37 #include "tvgJpgLoader.h"
40 #include "tvgRawLoader.h"
42 /************************************************************************/
43 /* Internal Class Implementation */
44 /************************************************************************/
46 static LoadModule* _find(FileType type)
50 #ifdef THORVG_TVG_LOADER_SUPPORT
56 #ifdef THORVG_SVG_LOADER_SUPPORT
66 #ifdef THORVG_PNG_LOADER_SUPPORT
72 #ifdef THORVG_JPG_LOADER_SUPPORT
82 #ifdef THORVG_LOG_ENABLED
101 case FileType::Jpg: {
110 TVGLOG("LOADER", "%s format is not supported", format);
116 static LoadModule* _findByPath(const string& path)
118 auto ext = path.substr(path.find_last_of(".") + 1);
119 if (!ext.compare("tvg")) return _find(FileType::Tvg);
120 if (!ext.compare("svg")) return _find(FileType::Svg);
121 if (!ext.compare("png")) return _find(FileType::Png);
122 if (!ext.compare("jpg")) return _find(FileType::Jpg);
127 static LoadModule* _findByType(const string& mimeType)
129 if (mimeType.empty()) return nullptr;
131 auto type = FileType::Unknown;
133 if (mimeType == "tvg") type = FileType::Tvg;
134 else if (mimeType == "svg" || mimeType == "svg+xml") type = FileType::Svg;
135 else if (mimeType == "raw") type = FileType::Raw;
136 else if (mimeType == "png") type = FileType::Png;
137 else if (mimeType == "jpg" || mimeType == "jpeg") type = FileType::Jpg;
139 TVGLOG("LOADER", "Given mimetype is unknown = \"%s\".", mimeType.c_str());
147 /************************************************************************/
148 /* External Class Implementation */
149 /************************************************************************/
152 bool LoaderMgr::init()
160 bool LoaderMgr::term()
168 shared_ptr<LoadModule> LoaderMgr::loader(const string& path, bool* invalid)
172 if (auto loader = _findByPath(path)) {
173 if (loader->open(path)) return shared_ptr<LoadModule>(loader);
181 shared_ptr<LoadModule> LoaderMgr::loader(const char* data, uint32_t size, const string& mimeType, bool copy)
183 //Try first with the given MimeType
184 if (auto loader = _findByType(mimeType)) {
185 if (loader->open(data, size, copy)) {
186 return shared_ptr<LoadModule>(loader);
188 TVGLOG("LOADER", "Given mimetype \"%s\" seems incorrect or not supported. Will try again with other types.", mimeType.c_str());
193 //Abnormal MimeType. Try with the candidates in the order
194 for (int i = 0; i < static_cast<int>(FileType::Unknown); i++) {
195 auto loader = _find(static_cast<FileType>(i));
197 if (loader->open(data, size, copy)) return shared_ptr<LoadModule>(loader);
205 shared_ptr<LoadModule> LoaderMgr::loader(const uint32_t *data, uint32_t w, uint32_t h, bool copy)
207 //function is dedicated for raw images only
208 auto loader = new RawLoader;
209 if (loader->open(data, w, h, copy)) return shared_ptr<LoadModule>(loader);