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
24 #include "tvgLoader.h"
25 #include "tvgTvgLoader.h"
29 /************************************************************************/
30 /* Internal Class Implementation */
31 /************************************************************************/
34 void TvgLoader::clear()
36 if (copy) free((char*)data);
43 interpreter = nullptr;
48 /* WARNING: Header format shall not change! */
49 bool TvgLoader::readHeader()
51 if (!ptr) return false;
53 //Make sure the size is large enough to hold the header
54 if (size < TVG_HEADER_SIZE) return false;
57 if (memcmp(ptr, TVG_HEADER_SIGNATURE, TVG_HEADER_SIGNATURE_LENGTH)) return false;
58 ptr += TVG_HEADER_SIGNATURE_LENGTH;
61 char version[TVG_HEADER_VERSION_LENGTH + 1];
62 memcpy(version, ptr, TVG_HEADER_VERSION_LENGTH);
63 version[TVG_HEADER_VERSION_LENGTH - 1] = '\0';
64 ptr += TVG_HEADER_VERSION_LENGTH;
65 this->version = atoi(version);
66 if (this->version > THORVG_VERSION_NUMBER()) {
67 TVGLOG("TVG", "This TVG file expects a higher version(%d) of ThorVG symbol(%d)", this->version, THORVG_VERSION_NUMBER());
77 if (*ptr & TVG_HEAD_FLAG_COMPRESSED) compressed = true;
78 ptr += TVG_HEADER_RESERVED_LENGTH;
80 //5. Compressed Size if any
84 //TVG_HEADER_UNCOMPRESSED_SIZE
85 memcpy(&uncompressedSize, p, sizeof(uint32_t));
88 //TVG_HEADER_COMPRESSED_SIZE
89 memcpy(&compressedSize, p, sizeof(uint32_t));
92 //TVG_HEADER_COMPRESSED_SIZE_BITS
93 memcpy(&compressedSizeBits, p, sizeof(uint32_t));
96 ptr += TVG_HEADER_COMPRESS_SIZE;
98 //Decide the proper Tvg Binary Interpreter based on the current file version
99 if (this->version >= 0) interpreter = new TvgBinInterpreter;
105 /************************************************************************/
106 /* External Class Implementation */
107 /************************************************************************/
109 TvgLoader::~TvgLoader()
115 bool TvgLoader::open(const string &path)
120 f.open(path, ifstream::in | ifstream::binary | ifstream::ate);
122 if (!f.is_open()) return false;
125 f.seekg(0, ifstream::beg);
128 data = (char*)malloc(size);
135 if (!f.read((char*)data, size))
150 bool TvgLoader::open(const char *data, uint32_t size, bool copy)
155 this->data = (char*)malloc(size);
156 if (!this->data) return false;
157 memcpy((char*)this->data, data, size);
158 } else this->data = data;
160 this->ptr = this->data;
168 bool TvgLoader::resize(Paint* paint, float w, float h)
170 if (!paint) return false;
172 auto sx = w / this->w;
173 auto sy = h / this->h;
176 auto scale = sx < sy ? sx : sy;
180 float tx = 0, ty = 0;
181 auto sw = this->w * scale;
182 auto sh = this->h * scale;
183 if (sw > sh) ty -= (h - sh) * 0.5f;
184 else tx -= (w - sw) * 0.5f;
185 paint->translate(-tx, -ty);
191 bool TvgLoader::read()
193 if (!ptr || size == 0) return false;
195 TaskScheduler::request(this);
201 bool TvgLoader::close()
209 void TvgLoader::run(unsigned tid)
211 if (root) root.reset();
213 auto data = const_cast<char*>(ptr);
216 data = (char*) lzwDecode((uint8_t*) data, compressedSize, compressedSizeBits, uncompressedSize);
217 root = interpreter->run(data, data + uncompressedSize);
220 root = interpreter->run(data, this->data + size);
227 unique_ptr<Paint> TvgLoader::paint()
230 if (root) return move(root);