2018-06-26 14:09:39 +02:00
|
|
|
#include "messages/Image.hpp"
|
2018-04-27 22:11:19 +02:00
|
|
|
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "Application.hpp"
|
2018-07-15 14:11:46 +02:00
|
|
|
#include "common/NetworkRequest.hpp"
|
2018-08-02 14:23:27 +02:00
|
|
|
#include "debug/AssertInGuiThread.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "debug/Log.hpp"
|
2018-06-28 19:46:45 +02:00
|
|
|
#include "singletons/Emotes.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "singletons/WindowManager.hpp"
|
|
|
|
#include "util/PostToThread.hpp"
|
2017-01-11 18:52:09 +01:00
|
|
|
|
2017-02-06 17:42:28 +01:00
|
|
|
#include <QBuffer>
|
|
|
|
#include <QImageReader>
|
2017-01-11 18:52:09 +01:00
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
2017-02-06 17:42:28 +01:00
|
|
|
#include <QTimer>
|
2017-06-06 21:18:05 +02:00
|
|
|
|
2017-01-11 18:52:09 +01:00
|
|
|
#include <functional>
|
2017-10-27 20:09:02 +02:00
|
|
|
#include <thread>
|
2017-01-11 18:52:09 +01:00
|
|
|
|
2017-01-18 21:30:23 +01:00
|
|
|
namespace chatterino {
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
// IMAGE2
|
|
|
|
std::atomic<bool> Image::loadedEventQueued{false};
|
2018-04-18 17:03:37 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
ImagePtr Image::fromUrl(const Url &url, qreal scale)
|
2017-01-05 16:07:20 +01:00
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
// herb sutter cache
|
|
|
|
static std::unordered_map<Url, std::weak_ptr<Image>> cache;
|
|
|
|
static std::mutex mutex;
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
|
|
|
|
auto shared = cache[url].lock();
|
|
|
|
|
|
|
|
if (!shared) {
|
|
|
|
cache[url] = shared = ImagePtr(new Image(url, scale));
|
|
|
|
} else {
|
|
|
|
Warn("same image loaded multiple times: {}", url.string);
|
|
|
|
}
|
|
|
|
|
|
|
|
return shared;
|
2017-01-05 16:07:20 +01:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
ImagePtr Image::fromOwningPixmap(std::unique_ptr<QPixmap> pixmap, qreal scale)
|
2017-01-11 18:52:09 +01:00
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
return ImagePtr(new Image(std::move(pixmap), scale));
|
2018-04-06 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
ImagePtr Image::fromNonOwningPixmap(QPixmap *pixmap, qreal scale)
|
2018-04-06 16:37:30 +02:00
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
return ImagePtr(new Image(pixmap, scale));
|
|
|
|
}
|
2018-04-06 16:37:30 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
ImagePtr Image::getEmpty()
|
|
|
|
{
|
|
|
|
static auto empty = ImagePtr(new Image);
|
|
|
|
return empty;
|
|
|
|
}
|
2018-04-06 16:37:30 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
Image::Image()
|
|
|
|
{
|
|
|
|
this->isLoaded_ = true;
|
|
|
|
this->isNull_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Image::Image(const Url &url, qreal scale)
|
|
|
|
{
|
|
|
|
this->url_ = url;
|
|
|
|
this->scale_ = scale;
|
|
|
|
|
|
|
|
if (url.string.isEmpty()) {
|
|
|
|
this->isLoaded_ = true;
|
|
|
|
this->isNull_ = true;
|
2018-04-06 16:37:30 +02:00
|
|
|
}
|
2017-01-11 18:52:09 +01:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
Image::Image(std::unique_ptr<QPixmap> owning, qreal scale)
|
2017-01-04 15:12:31 +01:00
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
this->frames_.push_back(Frame(std::move(owning)));
|
|
|
|
this->scale_ = scale;
|
|
|
|
this->isLoaded_ = true;
|
|
|
|
this->currentFramePixmap_ = this->frames_.front().getPixmap();
|
|
|
|
}
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
Image::Image(QPixmap *nonOwning, qreal scale)
|
|
|
|
{
|
|
|
|
this->frames_.push_back(Frame(nonOwning));
|
|
|
|
this->scale_ = scale;
|
|
|
|
this->isLoaded_ = true;
|
|
|
|
this->currentFramePixmap_ = this->frames_.front().getPixmap();
|
|
|
|
}
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
const Url &Image::getUrl() const
|
|
|
|
{
|
|
|
|
return this->url_;
|
|
|
|
}
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
NullablePtr<const QPixmap> Image::getPixmap() const
|
|
|
|
{
|
|
|
|
assertInGuiThread();
|
2018-04-06 17:46:12 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
if (!this->isLoaded_) {
|
|
|
|
const_cast<Image *>(this)->load();
|
|
|
|
}
|
2018-04-16 23:48:30 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
return this->currentFramePixmap_;
|
|
|
|
}
|
2018-04-16 23:48:30 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
qreal Image::getScale() const
|
|
|
|
{
|
|
|
|
return this->scale_;
|
|
|
|
}
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
bool Image::isAnimated() const
|
|
|
|
{
|
|
|
|
return this->isAnimated_;
|
|
|
|
}
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
int Image::getWidth() const
|
|
|
|
{
|
|
|
|
if (!this->isLoaded_) return 16;
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
return this->frames_.front().getPixmap()->width() * this->scale_;
|
|
|
|
}
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
int Image::getHeight() const
|
|
|
|
{
|
|
|
|
if (!this->isLoaded_) return 16;
|
2018-04-16 23:48:30 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
return this->frames_.front().getPixmap()->height() * this->scale_;
|
|
|
|
}
|
2018-04-18 17:20:33 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
bool Image::isLoaded() const
|
|
|
|
{
|
|
|
|
return this->isLoaded_;
|
|
|
|
}
|
2018-04-06 16:37:30 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
bool Image::isValid() const
|
|
|
|
{
|
|
|
|
return !this->isNull_;
|
|
|
|
}
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
bool Image::isNull() const
|
|
|
|
{
|
|
|
|
return this->isNull_;
|
|
|
|
}
|
2018-01-19 22:45:33 +01:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
void Image::load()
|
|
|
|
{
|
|
|
|
// decrease debug count
|
|
|
|
if (this->isAnimated_) {
|
|
|
|
DebugCount::decrease("animated images");
|
|
|
|
}
|
|
|
|
if (this->isLoaded_) {
|
|
|
|
DebugCount::decrease("loaded images");
|
|
|
|
}
|
2018-01-19 22:45:33 +01:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
this->isLoaded_ = false;
|
|
|
|
this->isLoading_ = true;
|
|
|
|
this->frames_.clear();
|
2018-04-18 17:03:37 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
NetworkRequest req(this->getUrl().string);
|
|
|
|
req.setCaller(&this->object_);
|
|
|
|
req.setUseQuickLoadCache(true);
|
|
|
|
req.onSuccess([this, weak = weakOf(this)](auto result) -> Outcome {
|
|
|
|
auto shared = weak.lock();
|
|
|
|
if (!shared) return Failure;
|
2018-04-18 17:10:17 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
auto &bytes = result.getData();
|
|
|
|
QByteArray copy = QByteArray::fromRawData(bytes.constData(), bytes.length());
|
2018-04-16 23:48:30 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
return this->parse(result.getData());
|
2017-10-27 20:09:02 +02:00
|
|
|
});
|
2018-07-07 13:08:57 +02:00
|
|
|
|
|
|
|
req.execute();
|
2017-02-06 17:42:28 +01:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
Outcome Image::parse(const QByteArray &data)
|
2017-02-06 17:42:28 +01:00
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
// const cast since we are only reading from it
|
|
|
|
QBuffer buffer(const_cast<QByteArray *>(&data));
|
|
|
|
buffer.open(QIODevice::ReadOnly);
|
|
|
|
QImageReader reader(&buffer);
|
2017-10-08 15:18:47 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
return this->setFrames(this->readFrames(reader));
|
2017-01-04 15:12:31 +01:00
|
|
|
}
|
2017-09-12 19:06:16 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
std::vector<Image::Frame> Image::readFrames(QImageReader &reader)
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
std::vector<Frame> frames;
|
2017-09-12 19:06:16 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
if (reader.imageCount() <= 0) {
|
|
|
|
Log("Error while reading image {}: '{}'", this->url_.string, reader.errorString());
|
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage image;
|
|
|
|
for (int index = 0; index < reader.imageCount(); ++index) {
|
|
|
|
if (reader.read(&image)) {
|
|
|
|
auto pixmap = new QPixmap(QPixmap::fromImage(image));
|
2018-01-19 22:45:33 +01:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
int duration = std::max(20, reader.nextImageDelay());
|
|
|
|
frames.push_back(Image::Frame(pixmap, duration));
|
|
|
|
}
|
2018-01-19 22:45:33 +01:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
if (frames.size() != 0) {
|
|
|
|
Log("Error while reading image {}: '{}'", this->url_.string, reader.errorString());
|
2017-09-12 19:06:16 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
return frames;
|
2017-09-12 19:06:16 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
Outcome Image::setFrames(std::vector<Frame> frames)
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->framesMutex_);
|
2017-09-12 19:06:16 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
if (frames.size() > 0) {
|
|
|
|
this->currentFramePixmap_ = frames.front().getPixmap();
|
2017-09-12 19:06:16 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
if (frames.size() > 1) {
|
|
|
|
if (!this->isAnimated_) {
|
|
|
|
getApp()->emotes->gifTimer.signal.connect([=]() { this->updateAnimation(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
this->isAnimated_ = true;
|
|
|
|
DebugCount::increase("animated images");
|
|
|
|
}
|
|
|
|
|
|
|
|
this->isLoaded_ = true;
|
|
|
|
DebugCount::increase("loaded images");
|
|
|
|
|
|
|
|
return Success;
|
2018-06-24 18:29:30 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
this->frames_ = std::move(frames);
|
|
|
|
this->queueLoadedEvent();
|
2018-06-24 18:29:30 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
return Failure;
|
2017-09-12 19:06:16 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
void Image::queueLoadedEvent()
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
if (!loadedEventQueued) {
|
|
|
|
loadedEventQueued = true;
|
|
|
|
|
|
|
|
QTimer::singleShot(250, [] {
|
|
|
|
getApp()->windows->incGeneration();
|
|
|
|
getApp()->windows->layoutChannelViews();
|
|
|
|
loadedEventQueued = false;
|
|
|
|
});
|
|
|
|
}
|
2017-09-12 19:06:16 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
void Image::updateAnimation()
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
if (this->isAnimated_) {
|
|
|
|
std::lock_guard<std::mutex> lock(this->framesMutex_);
|
2017-09-12 19:06:16 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
this->currentFrameOffset_ += GIF_FRAME_LENGTH;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
this->currentFrameIndex_ %= this->frames_.size();
|
|
|
|
if (this->currentFrameOffset_ > this->frames_[this->currentFrameIndex_].getDuration()) {
|
|
|
|
this->currentFrameOffset_ -= this->frames_[this->currentFrameIndex_].getDuration();
|
|
|
|
this->currentFrameIndex_ = (this->currentFrameIndex_ + 1) % this->frames_.size();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->currentFramePixmap_ = this->frames_[this->currentFrameIndex_].getPixmap();
|
|
|
|
}
|
2017-09-12 19:06:16 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
bool Image::operator==(const Image &other) const
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
if (this->isNull() && other.isNull()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->url_.string.isEmpty() && this->url_ == other.url_) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(this->frames_.size() == 1);
|
|
|
|
assert(other.frames_.size() == 1);
|
|
|
|
|
|
|
|
if (this->currentFramePixmap_ == other.currentFramePixmap_) {
|
|
|
|
return true;
|
2017-09-12 19:06:16 +02:00
|
|
|
}
|
2018-01-19 22:45:33 +01:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
return false;
|
2017-09-12 19:06:16 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
bool Image::operator!=(const Image &other) const
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
return !this->operator==(other);
|
2017-09-12 19:06:16 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
// FRAME
|
|
|
|
Image::Frame::Frame(QPixmap *nonOwning, int duration)
|
|
|
|
: nonOwning_(nonOwning)
|
|
|
|
, duration_(duration)
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
Image::Frame::Frame(std::unique_ptr<QPixmap> nonOwning, int duration)
|
|
|
|
: owning_(std::move(nonOwning))
|
|
|
|
, duration_(duration)
|
2017-09-12 19:06:16 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
int Image::Frame::getDuration() const
|
2018-06-24 18:29:30 +02:00
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
return this->duration_;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap *Image::Frame::getPixmap() const
|
|
|
|
{
|
|
|
|
if (this->nonOwning_) return this->nonOwning_;
|
|
|
|
|
|
|
|
return this->owning_.get();
|
2018-06-24 18:29:30 +02:00
|
|
|
}
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace chatterino
|