2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 06:35:14 +00:00

Generate good thumbnail for animated stickers.

This commit is contained in:
John Preston
2019-07-04 10:20:36 +02:00
parent 7034df49e9
commit 0a63eac4f6
4 changed files with 43 additions and 7 deletions

View File

@@ -636,7 +636,10 @@ Image *DocumentData::goodThumbnail() const {
}
void DocumentData::validateGoodThumbnail() {
if (!isVideoFile() && !isAnimation() && !isWallPaper()) {
if (!isVideoFile()
&& !isAnimation()
&& !isWallPaper()
&& (!sticker() || !sticker()->animated)) {
_goodThumbnail = nullptr;
} else if (!_goodThumbnail && hasRemoteLocation()) {
_goodThumbnail = std::make_unique<Image>(
@@ -665,7 +668,10 @@ void DocumentData::setGoodThumbnailOnUpload(
}
_goodThumbnail = std::make_unique<Image>(
std::make_unique<Images::LocalFileSource>(
QString(), std::move(bytes), "JPG", std::move(image)));
QString(),
std::move(bytes),
sticker() ? "WEBP" : "JPG",
std::move(image)));
}
auto DocumentData::bigFileBaseCacheKey() const

View File

@@ -11,6 +11,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_document.h"
#include "data/data_file_origin.h"
#include "media/clip/media_clip_reader.h"
#include "lottie/lottie_animation.h"
#include "auth_session.h"
namespace Data {
@@ -19,12 +20,20 @@ namespace {
constexpr auto kGoodThumbQuality = 87;
constexpr auto kWallPaperSize = 960;
enum class FileType {
Video,
AnimatedSticker,
WallPaper,
};
QImage Prepare(
const QString &path,
QByteArray data,
bool isWallPaper) {
if (!isWallPaper) {
FileType type) {
if (type == FileType::Video) {
return Media::Clip::PrepareForSending(path, data).thumbnail;
} else if (type == FileType::AnimatedSticker) {
return Lottie::ReadThumbnail(Lottie::ReadContent(data, path));
}
const auto validateSize = [](QSize size) {
return (size.width() + size.height()) < 10'000;
@@ -64,7 +73,11 @@ void GoodThumbSource::generate(base::binary_guard &&guard) {
return;
}
const auto data = _document->data();
const auto isWallPaper = _document->isWallPaper();
const auto type = _document->isWallPaper()
? FileType::WallPaper
: _document->sticker()
? FileType::AnimatedSticker
: FileType::Video;
auto location = _document->location().isEmpty()
? nullptr
: std::make_unique<FileLocation>(_document->location());
@@ -80,11 +93,13 @@ void GoodThumbSource::generate(base::binary_guard &&guard) {
const auto filepath = (location && location->accessEnable())
? location->name()
: QString();
auto result = Prepare(filepath, data, isWallPaper);
auto result = Prepare(filepath, data, type);
auto bytes = QByteArray();
if (!result.isNull()) {
auto buffer = QBuffer(&bytes);
const auto format = (isWallPaper && result.hasAlphaChannel())
const auto format = (type == FileType::AnimatedSticker)
? "WEBP"
: (type == FileType::WallPaper && result.hasAlphaChannel())
? "PNG"
: "JPG";
result.save(&buffer, format, kGoodThumbQuality);