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);

View File

@ -156,6 +156,10 @@ void HistorySticker::draw(Painter &p, const QRect &r, TextSelection selection, c
const auto w = _pixw;
const auto h = _pixh;
const auto &c = st::msgStickerOverlay;
const auto good = _data->goodThumbnail();
if (!lottieReady && good && !good->loaded()) {
good->load({});
}
static QPixmap empty;
if (lottieReady) {
return empty;
@ -170,6 +174,10 @@ void HistorySticker::draw(Painter &p, const QRect &r, TextSelection selection, c
// return selected
// ? blurred->pixBlurredColored(o, c, w, h)
// : blurred->pixBlurred(o, w, h);
} else if (good && good->loaded()) {
return selected
? good->pixColored(o, c, w, h)
: good->pix(o, w, h);
} else if (const auto thumbnail = _data->thumbnail()) {
return selected
? thumbnail->pixBlurredColored(o, c, w, h)

View File

@ -894,6 +894,13 @@ void FileLoadTask::process() {
MTP_string(QString()),
MTP_inputStickerSetEmpty(),
MTPMaskCoords()));
if (isAnimation) {
goodThumbnail = fullimage;
{
QBuffer buffer(&goodThumbnailBytes);
goodThumbnail.save(&buffer, "WEBP", kThumbnailQuality);
}
}
} else if (isAnimation) {
attributes.push_back(MTP_documentAttributeAnimated());
} else if (_type != SendMediaType::File) {