mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-18 22:11:32 +00:00

By choosing compress option, tvg tries to compress the data to reduce the binary size. Since the compression has the double-edges sword, we provides an option to users to select it by their demand. Basically, compression is better than non-compression. After profiling, we decided to use the encoder/decoder of Guilherme R. Lampert's. Here is the profiling result: test.tvg: 296037 -> 243411 (-17%) tiger.tvg: 54568 -> 50622 (-7%) image-embedded.tvg: 2282 -> 1231 (-46%) @Issue: https://github.com/Samsung/thorvg/issues/639 About compression method: Lempel–Ziv–Welch (LZW) encoder/decoder by Guilherme R. Lampert This is the compression scheme used by the GIF image format and the Unix 'compress' tool. Main differences from this implementation is that End Of Input (EOI) and Clear Codes (CC) are not stored in the output and the max code length in bits is 12, vs 16 in compress. EOI is simply detected by the end of the data stream, while CC happens if the dictionary gets filled. Data is written/read from bit streams, which handle byte-alignment for us in a transparent way. The decoder relies on the hardcoded data layout produced by the encoder, since no additional reconstruction data is added to the output, so they must match. The nice thing about LZW is that we can reconstruct the dictionary directly from the stream of codes generated by the encoder, so this avoids storing additional headers in the bit stream. The output code length is variable. It starts with the minimum number of bits required to store the base byte-sized dictionary and automatically increases as the dictionary gets larger (it starts at 9-bits and grows to 10-bits when code 512 is added, then 11-bits when 1024 is added, and so on). If the dictionary is filled (4096 items for a 12-bits dictionary), the whole thing is cleared and the process starts over. This is the main reason why the encoder and the decoder must match perfectly, since the lengths of the codes will not be specified with the data itself.
134 lines
3.5 KiB
C++
134 lines
3.5 KiB
C++
/*
|
|
* Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved.
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
#include "tvgCommon.h"
|
|
#include "tvgSaveModule.h"
|
|
|
|
#ifdef THORVG_TVG_SAVER_SUPPORT
|
|
#include "tvgTvgSaver.h"
|
|
#endif
|
|
|
|
/************************************************************************/
|
|
/* Internal Class Implementation */
|
|
/************************************************************************/
|
|
|
|
struct Saver::Impl
|
|
{
|
|
SaveModule* saveModule = nullptr;
|
|
~Impl()
|
|
{
|
|
if (saveModule) delete(saveModule);
|
|
}
|
|
};
|
|
|
|
|
|
static SaveModule* _find(FileType type)
|
|
{
|
|
switch(type) {
|
|
case FileType::Tvg: {
|
|
#ifdef THORVG_TVG_SAVER_SUPPORT
|
|
return new TvgSaver;
|
|
#endif
|
|
break;
|
|
}
|
|
default: {
|
|
break;
|
|
}
|
|
}
|
|
|
|
#ifdef THORVG_LOG_ENABLED
|
|
const char *format;
|
|
switch(type) {
|
|
case FileType::Tvg: {
|
|
format = "TVG";
|
|
break;
|
|
}
|
|
default: {
|
|
format = "???";
|
|
break;
|
|
}
|
|
}
|
|
TVGLOG("SAVER", "%s format is not supported", format);
|
|
#endif
|
|
return nullptr;
|
|
}
|
|
|
|
|
|
static SaveModule* _find(const string& path)
|
|
{
|
|
auto ext = path.substr(path.find_last_of(".") + 1);
|
|
if (!ext.compare("tvg")) {
|
|
return _find(FileType::Tvg);
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
|
|
/************************************************************************/
|
|
/* External Class Implementation */
|
|
/************************************************************************/
|
|
|
|
Saver::Saver() : pImpl(new Impl())
|
|
{
|
|
}
|
|
|
|
|
|
Saver::~Saver()
|
|
{
|
|
delete(pImpl);
|
|
}
|
|
|
|
|
|
Result Saver::save(std::unique_ptr<Paint> paint, const string& path, bool compress) noexcept
|
|
{
|
|
//Already on saving an other resource.
|
|
if (pImpl->saveModule) return Result::InsufficientCondition;
|
|
|
|
auto p = paint.release();
|
|
if (!p) return Result::MemoryCorruption;
|
|
|
|
if (auto saveModule = _find(path)) {
|
|
if (saveModule->save(p, path, compress)) {
|
|
pImpl->saveModule = saveModule;
|
|
return Result::Success;
|
|
} else {
|
|
return Result::Unknown;
|
|
}
|
|
}
|
|
return Result::NonSupport;
|
|
}
|
|
|
|
|
|
Result Saver::sync() noexcept
|
|
{
|
|
if (!pImpl->saveModule) return Result::InsufficientCondition;
|
|
pImpl->saveModule->close();
|
|
delete(pImpl->saveModule);
|
|
pImpl->saveModule = nullptr;
|
|
|
|
return Result::Success;
|
|
}
|
|
|
|
|
|
unique_ptr<Saver> Saver::gen() noexcept
|
|
{
|
|
return unique_ptr<Saver>(new Saver);
|
|
}
|