mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-08-07 16:11:13 +00:00
example:refactor to remove naked malloc and RAII warning
This commit is contained in:
parent
23115b9092
commit
89a72b6e9c
@ -4,15 +4,19 @@
|
|||||||
#include<iostream>
|
#include<iostream>
|
||||||
#include<vector>
|
#include<vector>
|
||||||
#include<array>
|
#include<array>
|
||||||
#include<cstdlib>
|
#include<libgen.h>
|
||||||
|
|
||||||
class GifBuilder {
|
class GifBuilder {
|
||||||
public:
|
public:
|
||||||
GifBuilder(const std::string &fileName , const uint32_t width,
|
explicit GifBuilder(const std::string &fileName , const uint32_t width,
|
||||||
const uint32_t height, const uint32_t delay = 2)
|
const uint32_t height, const uint32_t delay = 2)
|
||||||
{
|
{
|
||||||
GifBegin(&handle, fileName.c_str(), width, height, delay);
|
GifBegin(&handle, fileName.c_str(), width, height, delay);
|
||||||
}
|
}
|
||||||
|
~GifBuilder()
|
||||||
|
{
|
||||||
|
GifEnd(&handle);
|
||||||
|
}
|
||||||
void addFrame(rlottie::Surface &s, uint32_t delay = 2)
|
void addFrame(rlottie::Surface &s, uint32_t delay = 2)
|
||||||
{
|
{
|
||||||
argbTorgba(s);
|
argbTorgba(s);
|
||||||
@ -56,10 +60,7 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void commit()
|
|
||||||
{
|
|
||||||
GifEnd(&handle);
|
|
||||||
}
|
|
||||||
private:
|
private:
|
||||||
GifWriter handle;
|
GifWriter handle;
|
||||||
};
|
};
|
||||||
@ -71,18 +72,15 @@ public:
|
|||||||
auto player = rlottie::Animation::loadFromFile(fileName);
|
auto player = rlottie::Animation::loadFromFile(fileName);
|
||||||
if (!player) return help();
|
if (!player) return help();
|
||||||
|
|
||||||
uint32_t* buffer = (uint32_t *) malloc(w * h * 4);
|
auto buffer = std::make_unique<uint32_t[]>(w * h);
|
||||||
size_t frameCount = player->totalFrame();
|
size_t frameCount = player->totalFrame();
|
||||||
|
|
||||||
GifBuilder builder(baseName.data(), w, h);
|
GifBuilder builder(baseName.data(), w, h);
|
||||||
for (size_t i = 0; i < frameCount ; i++) {
|
for (size_t i = 0; i < frameCount ; i++) {
|
||||||
rlottie::Surface surface(buffer, w, h, w * 4);
|
rlottie::Surface surface(buffer.get(), w, h, w * 4);
|
||||||
player->renderSync(i, surface);
|
player->renderSync(i, surface);
|
||||||
builder.addFrame(surface);
|
builder.addFrame(surface);
|
||||||
}
|
}
|
||||||
builder.commit();
|
|
||||||
|
|
||||||
free(buffer);
|
|
||||||
return result();
|
return result();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user