2016-06-24 10:37:29 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-06-24 10:37:29 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-06-24 10:37:29 +00:00
|
|
|
*/
|
|
|
|
#include "media/media_clip_qtgif.h"
|
|
|
|
|
|
|
|
namespace Media {
|
|
|
|
namespace Clip {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
QtGifReaderImplementation::QtGifReaderImplementation(FileLocation *location, QByteArray *data) : ReaderImplementation(location, data) {
|
|
|
|
}
|
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
ReaderImplementation::ReadResult QtGifReaderImplementation::readFramesTill(TimeMs frameMs, TimeMs systemMs) {
|
2016-07-14 11:20:46 +00:00
|
|
|
if (!_frame.isNull() && _frameTime > frameMs) {
|
2016-07-12 18:04:34 +00:00
|
|
|
return ReadResult::Success;
|
2016-07-10 19:44:55 +00:00
|
|
|
}
|
2016-07-12 18:04:34 +00:00
|
|
|
auto readResult = readNextFrame();
|
2016-07-14 11:20:46 +00:00
|
|
|
if (readResult != ReadResult::Success || _frameTime > frameMs) {
|
2016-07-12 18:04:34 +00:00
|
|
|
return readResult;
|
2016-07-10 19:44:55 +00:00
|
|
|
}
|
2016-07-12 18:04:34 +00:00
|
|
|
readResult = readNextFrame();
|
2016-07-14 11:20:46 +00:00
|
|
|
if (_frameTime <= frameMs) {
|
|
|
|
_frameTime = frameMs + 5; // keep up
|
2016-07-10 19:44:55 +00:00
|
|
|
}
|
2016-07-12 18:04:34 +00:00
|
|
|
return readResult;
|
2016-07-10 19:44:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
TimeMs QtGifReaderImplementation::frameRealTime() const {
|
2016-07-12 12:28:07 +00:00
|
|
|
return _frameRealTime;
|
|
|
|
}
|
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
TimeMs QtGifReaderImplementation::framePresentationTime() const {
|
|
|
|
return qMax(_frameTime, 0LL);
|
2016-07-10 19:44:55 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 18:04:34 +00:00
|
|
|
ReaderImplementation::ReadResult QtGifReaderImplementation::readNextFrame() {
|
2016-06-24 10:37:29 +00:00
|
|
|
if (_reader) _frameDelay = _reader->nextImageDelay();
|
2016-07-12 18:04:34 +00:00
|
|
|
if (_framesLeft < 1) {
|
|
|
|
if (_mode == Mode::Normal) {
|
2016-07-30 10:03:44 +00:00
|
|
|
return ReadResult::EndOfFile;
|
2016-07-12 18:04:34 +00:00
|
|
|
} else if (!jumpToStart()) {
|
|
|
|
return ReadResult::Error;
|
|
|
|
}
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_frame = QImage(); // QGifHandler always reads first to internal QImage and returns it
|
|
|
|
if (!_reader->read(&_frame) || _frame.isNull()) {
|
2016-07-12 18:04:34 +00:00
|
|
|
return ReadResult::Error;
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
--_framesLeft;
|
2016-07-10 19:44:55 +00:00
|
|
|
_frameTime += _frameDelay;
|
2016-07-12 12:28:07 +00:00
|
|
|
_frameRealTime += _frameDelay;
|
2016-07-12 18:04:34 +00:00
|
|
|
return ReadResult::Success;
|
2016-06-24 10:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool QtGifReaderImplementation::renderFrame(QImage &to, bool &hasAlpha, const QSize &size) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!_frame.isNull());
|
2016-06-24 10:37:29 +00:00
|
|
|
if (size.isEmpty() || size == _frame.size()) {
|
|
|
|
int32 w = _frame.width(), h = _frame.height();
|
|
|
|
if (to.width() == w && to.height() == h && to.format() == _frame.format()) {
|
|
|
|
if (to.byteCount() != _frame.byteCount()) {
|
|
|
|
int bpl = qMin(to.bytesPerLine(), _frame.bytesPerLine());
|
|
|
|
for (int i = 0; i < h; ++i) {
|
|
|
|
memcpy(to.scanLine(i), _frame.constScanLine(i), bpl);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
memcpy(to.bits(), _frame.constBits(), _frame.byteCount());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
to = _frame.copy();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
to = _frame.scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
|
|
|
}
|
|
|
|
hasAlpha = _frame.hasAlphaChannel();
|
|
|
|
_frame = QImage();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
TimeMs QtGifReaderImplementation::durationMs() const {
|
2016-07-12 12:28:07 +00:00
|
|
|
return 0; // not supported
|
|
|
|
}
|
|
|
|
|
2017-03-09 09:47:45 +00:00
|
|
|
bool QtGifReaderImplementation::start(Mode mode, TimeMs &positionMs) {
|
|
|
|
if (mode == Mode::Inspecting) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-12 18:04:34 +00:00
|
|
|
_mode = mode;
|
2016-06-24 10:37:29 +00:00
|
|
|
return jumpToStart();
|
|
|
|
}
|
|
|
|
|
2017-03-09 09:47:45 +00:00
|
|
|
QtGifReaderImplementation::~QtGifReaderImplementation() = default;
|
2016-06-24 10:37:29 +00:00
|
|
|
|
|
|
|
bool QtGifReaderImplementation::jumpToStart() {
|
|
|
|
if (_reader && _reader->jumpToImage(0)) {
|
|
|
|
_framesLeft = _reader->imageCount();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-09 09:47:45 +00:00
|
|
|
_reader = nullptr;
|
2016-06-24 10:37:29 +00:00
|
|
|
initDevice();
|
2017-03-09 09:47:45 +00:00
|
|
|
_reader = std::make_unique<QImageReader>(_device);
|
2016-08-30 05:24:16 +00:00
|
|
|
#ifndef OS_MAC_OLD
|
2016-06-24 10:37:29 +00:00
|
|
|
_reader->setAutoTransform(true);
|
2016-08-30 05:24:16 +00:00
|
|
|
#endif // OS_MAC_OLD
|
2016-06-24 10:37:29 +00:00
|
|
|
if (!_reader->canRead() || !_reader->supportsAnimation()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_framesLeft = _reader->imageCount();
|
|
|
|
if (_framesLeft < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace Clip
|
|
|
|
} // namespace Media
|