2016-06-24 10:37:29 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
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
|