mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-09 14:13:43 +00:00
savers: provides a background setting.
Allow users to set a custom background with a saver. API: - Result Saver::background(std::unique_ptr<Paint> paint);
This commit is contained in:
parent
5129b05023
commit
27843d2557
7 changed files with 44 additions and 22 deletions
|
@ -1804,6 +1804,15 @@ class TVG_API Saver final
|
||||||
public:
|
public:
|
||||||
~Saver();
|
~Saver();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Sets the base background content for the saved image.
|
||||||
|
*
|
||||||
|
* @param[in] paint The paint to be drawn as the background image for the saving paint.
|
||||||
|
*
|
||||||
|
* @note Experimental API
|
||||||
|
*/
|
||||||
|
Result background(std::unique_ptr<Paint> paint) noexcept;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Exports the given @p paint data to the given @p path
|
* @brief Exports the given @p paint data to the given @p path
|
||||||
*
|
*
|
||||||
|
|
|
@ -34,7 +34,7 @@ public:
|
||||||
virtual ~SaveModule() {}
|
virtual ~SaveModule() {}
|
||||||
|
|
||||||
virtual bool save(Paint* paint, const string& path, bool compress) = 0;
|
virtual bool save(Paint* paint, const string& path, bool compress) = 0;
|
||||||
virtual bool save(Animation* animation, const string& path, uint32_t quality, uint32_t fps) = 0;
|
virtual bool save(Animation* animation, Paint* bg, const string& path, uint32_t quality, uint32_t fps) = 0;
|
||||||
virtual bool close() = 0;
|
virtual bool close() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -37,9 +37,12 @@
|
||||||
struct Saver::Impl
|
struct Saver::Impl
|
||||||
{
|
{
|
||||||
SaveModule* saveModule = nullptr;
|
SaveModule* saveModule = nullptr;
|
||||||
|
Paint* bg = nullptr;
|
||||||
|
|
||||||
~Impl()
|
~Impl()
|
||||||
{
|
{
|
||||||
delete(saveModule);
|
delete(saveModule);
|
||||||
|
delete(bg);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -139,7 +142,16 @@ Result Saver::save(std::unique_ptr<Paint> paint, const string& path, bool compre
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Result Saver::save(std::unique_ptr<Animation> animation, const string& path, uint32_t quality, uint32_t fps) noexcept
|
Result Saver::background(unique_ptr<Paint> paint) noexcept
|
||||||
|
{
|
||||||
|
delete(pImpl->bg);
|
||||||
|
pImpl->bg = paint.release();
|
||||||
|
|
||||||
|
return Result::Success;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Result Saver::save(unique_ptr<Animation> animation, const string& path, uint32_t quality, uint32_t fps) noexcept
|
||||||
{
|
{
|
||||||
auto a = animation.release();
|
auto a = animation.release();
|
||||||
if (!a) return Result::MemoryCorruption;
|
if (!a) return Result::MemoryCorruption;
|
||||||
|
@ -156,7 +168,7 @@ Result Saver::save(std::unique_ptr<Animation> animation, const string& path, uin
|
||||||
}
|
}
|
||||||
|
|
||||||
if (auto saveModule = _find(path)) {
|
if (auto saveModule = _find(path)) {
|
||||||
if (saveModule->save(a, path, quality, fps)) {
|
if (saveModule->save(a, pImpl->bg, path, quality, fps)) {
|
||||||
pImpl->saveModule = saveModule;
|
pImpl->saveModule = saveModule;
|
||||||
return Result::Success;
|
return Result::Success;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -42,13 +42,7 @@ void GifSaver::run(unsigned tid)
|
||||||
|
|
||||||
buffer = (uint32_t*)realloc(buffer, sizeof(uint32_t) * w * h);
|
buffer = (uint32_t*)realloc(buffer, sizeof(uint32_t) * w * h);
|
||||||
canvas->target(buffer, w, w, h, tvg::SwCanvas::ABGR8888);
|
canvas->target(buffer, w, w, h, tvg::SwCanvas::ABGR8888);
|
||||||
|
canvas->push(cast(bg));
|
||||||
//base white background
|
|
||||||
auto bg = Shape::gen();
|
|
||||||
bg->appendRect(0, 0, vsize[0], vsize[1]);
|
|
||||||
bg->fill(255, 255, 255);
|
|
||||||
canvas->push(std::move(bg));
|
|
||||||
|
|
||||||
canvas->push(cast(animation->picture()));
|
canvas->push(cast(animation->picture()));
|
||||||
|
|
||||||
//use the default fps
|
//use the default fps
|
||||||
|
@ -81,6 +75,8 @@ void GifSaver::run(unsigned tid)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!GifEnd(&writer)) TVGERR("GIF_SAVER", "Failed gif encoding");
|
if (!GifEnd(&writer)) TVGERR("GIF_SAVER", "Failed gif encoding");
|
||||||
|
|
||||||
|
this->bg = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -98,6 +94,9 @@ bool GifSaver::close()
|
||||||
{
|
{
|
||||||
this->done();
|
this->done();
|
||||||
|
|
||||||
|
delete(bg);
|
||||||
|
bg = nullptr;
|
||||||
|
|
||||||
delete(animation);
|
delete(animation);
|
||||||
animation = nullptr;
|
animation = nullptr;
|
||||||
|
|
||||||
|
@ -118,7 +117,7 @@ bool GifSaver::save(TVG_UNUSED Paint* paint, TVG_UNUSED const string& path, TVG_
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool GifSaver::save(Animation* animation, const string& path, TVG_UNUSED uint32_t quality, uint32_t fps)
|
bool GifSaver::save(Animation* animation, Paint* bg, const string& path, TVG_UNUSED uint32_t quality, uint32_t fps)
|
||||||
{
|
{
|
||||||
close();
|
close();
|
||||||
|
|
||||||
|
@ -140,6 +139,7 @@ bool GifSaver::save(Animation* animation, const string& path, TVG_UNUSED uint32_
|
||||||
if (!this->path) return false;
|
if (!this->path) return false;
|
||||||
|
|
||||||
this->animation = animation;
|
this->animation = animation;
|
||||||
|
if (bg) this->bg = bg->duplicate();
|
||||||
this->fps = static_cast<float>(fps);
|
this->fps = static_cast<float>(fps);
|
||||||
|
|
||||||
TaskScheduler::request(this);
|
TaskScheduler::request(this);
|
||||||
|
|
|
@ -34,6 +34,7 @@ class GifSaver : public SaveModule, public Task
|
||||||
private:
|
private:
|
||||||
uint32_t* buffer = nullptr;
|
uint32_t* buffer = nullptr;
|
||||||
Animation* animation = nullptr;
|
Animation* animation = nullptr;
|
||||||
|
Paint* bg = nullptr;
|
||||||
char *path = nullptr;
|
char *path = nullptr;
|
||||||
float vsize[2] = {0.0f, 0.0f};
|
float vsize[2] = {0.0f, 0.0f};
|
||||||
float fps = 0.0f;
|
float fps = 0.0f;
|
||||||
|
@ -44,7 +45,7 @@ public:
|
||||||
~GifSaver();
|
~GifSaver();
|
||||||
|
|
||||||
bool save(Paint* paint, const string& path, bool compress) override;
|
bool save(Paint* paint, const string& path, bool compress) override;
|
||||||
bool save(Animation* animation, const string& path, uint32_t quality, uint32_t fps) override;
|
bool save(Animation* animation, Paint* bg, const string& path, uint32_t quality, uint32_t fps) override;
|
||||||
bool close() override;
|
bool close() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -780,15 +780,14 @@ bool TvgSaver::close()
|
||||||
{
|
{
|
||||||
this->done();
|
this->done();
|
||||||
|
|
||||||
if (paint) {
|
|
||||||
delete(paint);
|
delete(paint);
|
||||||
paint = nullptr;
|
paint = nullptr;
|
||||||
}
|
|
||||||
if (path) {
|
|
||||||
free(path);
|
free(path);
|
||||||
path = nullptr;
|
path = nullptr;
|
||||||
}
|
|
||||||
buffer.reset();
|
buffer.reset();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -822,7 +821,7 @@ bool TvgSaver::save(Paint* paint, const string& path, bool compress)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool TvgSaver::save(TVG_UNUSED Animation* animation, TVG_UNUSED const string& path, TVG_UNUSED uint32_t quality, TVG_UNUSED uint32_t fps)
|
bool TvgSaver::save(TVG_UNUSED Animation* animation, TVG_UNUSED Paint* bg, TVG_UNUSED const string& path, TVG_UNUSED uint32_t quality, TVG_UNUSED uint32_t fps)
|
||||||
{
|
{
|
||||||
TVGLOG("TVG_SAVER", "Animation is not supported.");
|
TVGLOG("TVG_SAVER", "Animation is not supported.");
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -35,6 +35,7 @@ class TvgSaver : public SaveModule, public Task
|
||||||
private:
|
private:
|
||||||
Array<TvgBinByte> buffer;
|
Array<TvgBinByte> buffer;
|
||||||
Paint* paint = nullptr;
|
Paint* paint = nullptr;
|
||||||
|
Paint* bg = nullptr;
|
||||||
char *path = nullptr;
|
char *path = nullptr;
|
||||||
uint32_t headerSize;
|
uint32_t headerSize;
|
||||||
float vsize[2] = {0.0f, 0.0f};
|
float vsize[2] = {0.0f, 0.0f};
|
||||||
|
@ -71,7 +72,7 @@ public:
|
||||||
~TvgSaver();
|
~TvgSaver();
|
||||||
|
|
||||||
bool save(Paint* paint, const string& path, bool compress) override;
|
bool save(Paint* paint, const string& path, bool compress) override;
|
||||||
bool save(Animation* animation, const string& path, uint32_t quality, uint32_t fps) override;
|
bool save(Animation* animation, Paint* bg, const string& path, uint32_t quality, uint32_t fps) override;
|
||||||
bool close() override;
|
bool close() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue