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-08-09 18:39:46 +02:00
|
|
|
#include "debug/Benchmark.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"
|
2018-08-07 01:35:24 +02:00
|
|
|
#include "util/DebugCount.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#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-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-06 18:25:47 +02:00
|
|
|
namespace {
|
|
|
|
// Frames
|
|
|
|
Frames::Frames()
|
|
|
|
{
|
|
|
|
DebugCount::increase("images");
|
|
|
|
}
|
|
|
|
|
2018-08-10 18:56:17 +02:00
|
|
|
Frames::Frames(const QVector<Frame<QPixmap>> &frames)
|
|
|
|
: items_(frames)
|
2018-08-06 18:25:47 +02:00
|
|
|
{
|
|
|
|
DebugCount::increase("images");
|
|
|
|
if (this->animated()) DebugCount::increase("animated images");
|
|
|
|
}
|
|
|
|
|
|
|
|
Frames::~Frames()
|
|
|
|
{
|
|
|
|
DebugCount::decrease("images");
|
|
|
|
if (this->animated()) DebugCount::decrease("animated images");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Frames::advance()
|
|
|
|
{
|
2018-08-06 20:00:04 +02:00
|
|
|
this->durationOffset_ += GIF_FRAME_LENGTH;
|
2018-08-06 18:25:47 +02:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
this->index_ %= this->items_.size();
|
2018-08-06 20:00:04 +02:00
|
|
|
if (this->durationOffset_ > this->items_[this->index_].duration) {
|
|
|
|
this->durationOffset_ -= this->items_[this->index_].duration;
|
2018-08-06 18:25:47 +02:00
|
|
|
this->index_ = (this->index_ + 1) % this->items_.size();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Frames::animated() const
|
|
|
|
{
|
|
|
|
return this->items_.size() > 1;
|
|
|
|
}
|
|
|
|
|
2018-08-10 18:56:17 +02:00
|
|
|
boost::optional<QPixmap> Frames::current() const
|
2018-08-06 18:25:47 +02:00
|
|
|
{
|
2018-08-10 18:56:17 +02:00
|
|
|
if (this->items_.size() == 0) return boost::none;
|
|
|
|
return this->items_[this->index_].image;
|
2018-08-06 18:25:47 +02:00
|
|
|
}
|
|
|
|
|
2018-08-10 18:56:17 +02:00
|
|
|
boost::optional<QPixmap> Frames::first() const
|
2018-08-06 18:25:47 +02:00
|
|
|
{
|
2018-08-10 18:56:17 +02:00
|
|
|
if (this->items_.size() == 0) return boost::none;
|
|
|
|
return this->items_.front().image;
|
2018-08-06 18:25:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// functions
|
2018-08-10 18:56:17 +02:00
|
|
|
QVector<Frame<QImage>> readFrames(QImageReader &reader, const Url &url)
|
2018-08-06 18:25:47 +02:00
|
|
|
{
|
2018-08-10 18:56:17 +02:00
|
|
|
QVector<Frame<QImage>> frames;
|
2018-08-06 18:25:47 +02:00
|
|
|
|
2018-08-09 18:39:46 +02:00
|
|
|
if (reader.imageCount() == 0) {
|
2018-08-06 21:17:03 +02:00
|
|
|
Log("Error while reading image {}: '{}'", url.string,
|
|
|
|
reader.errorString());
|
2018-08-06 18:25:47 +02:00
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage image;
|
|
|
|
for (int index = 0; index < reader.imageCount(); ++index) {
|
|
|
|
if (reader.read(&image)) {
|
2018-08-09 18:39:46 +02:00
|
|
|
QPixmap::fromImage(image);
|
2018-08-06 18:25:47 +02:00
|
|
|
|
|
|
|
int duration = std::max(20, reader.nextImageDelay());
|
2018-08-10 18:56:17 +02:00
|
|
|
frames.push_back(Frame<QImage>{image, duration});
|
2018-08-06 18:25:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 18:39:46 +02:00
|
|
|
if (frames.size() == 0) {
|
2018-08-06 21:17:03 +02:00
|
|
|
Log("Error while reading image {}: '{}'", url.string,
|
|
|
|
reader.errorString());
|
2018-08-06 18:25:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
|
2018-08-09 18:39:46 +02:00
|
|
|
// parsed
|
|
|
|
template <typename Assign>
|
2018-08-10 18:56:17 +02:00
|
|
|
void assignDelayed(
|
|
|
|
std::queue<std::pair<Assign, QVector<Frame<QPixmap>>>> &queued,
|
|
|
|
std::mutex &mutex, std::atomic_bool &loadedEventQueued)
|
2018-08-09 18:39:46 +02:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (!queued.empty()) {
|
2018-08-10 18:56:17 +02:00
|
|
|
queued.front().first(queued.front().second);
|
2018-08-09 18:39:46 +02:00
|
|
|
queued.pop();
|
|
|
|
|
|
|
|
if (++i > 50) {
|
2018-08-10 18:56:17 +02:00
|
|
|
QTimer::singleShot(
|
|
|
|
3, [&] { assignDelayed(queued, mutex, loadedEventQueued); });
|
2018-08-09 18:39:46 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getApp()->windows->forceLayoutChannelViews();
|
|
|
|
loadedEventQueued = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Assign>
|
2018-08-10 18:56:17 +02:00
|
|
|
auto makeConvertCallback(const QVector<Frame<QImage>> &parsed, Assign assign)
|
2018-08-09 18:39:46 +02:00
|
|
|
{
|
2018-08-10 18:56:17 +02:00
|
|
|
return [parsed, assign] {
|
2018-08-09 18:39:46 +02:00
|
|
|
// convert to pixmap
|
2018-08-10 18:56:17 +02:00
|
|
|
auto frames = QVector<Frame<QPixmap>>();
|
2018-08-09 18:39:46 +02:00
|
|
|
std::transform(parsed.begin(), parsed.end(), std::back_inserter(frames),
|
|
|
|
[](auto &frame) {
|
2018-08-10 18:56:17 +02:00
|
|
|
return Frame<QPixmap>{
|
|
|
|
QPixmap::fromImage(frame.image), frame.duration};
|
2018-08-09 18:39:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// put into stack
|
2018-08-10 18:56:17 +02:00
|
|
|
static std::queue<std::pair<Assign, QVector<Frame<QPixmap>>>> queued;
|
2018-08-09 18:39:46 +02:00
|
|
|
static std::mutex mutex;
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2018-08-10 18:56:17 +02:00
|
|
|
queued.emplace(assign, frames);
|
2018-08-09 18:39:46 +02:00
|
|
|
|
|
|
|
static std::atomic_bool loadedEventQueued{false};
|
|
|
|
|
|
|
|
if (!loadedEventQueued) {
|
|
|
|
loadedEventQueued = true;
|
|
|
|
|
2018-08-10 18:56:17 +02:00
|
|
|
QTimer::singleShot(
|
|
|
|
100, [=] { assignDelayed(queued, mutex, loadedEventQueued); });
|
2018-08-09 18:39:46 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2018-08-06 18:25:47 +02:00
|
|
|
} // namespace
|
2017-01-18 21:30:23 +01:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
// IMAGE2
|
|
|
|
ImagePtr Image::fromUrl(const Url &url, qreal scale)
|
2017-01-05 16:07:20 +01:00
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
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 {
|
2018-08-08 19:42:14 +02:00
|
|
|
// Warn("same image loaded multiple times: {}", url.string);
|
2018-08-02 14:23:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return shared;
|
2017-01-05 16:07:20 +01:00
|
|
|
}
|
|
|
|
|
2018-08-10 18:56:17 +02:00
|
|
|
ImagePtr Image::fromPixmap(const 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()
|
2018-08-06 18:25:47 +02:00
|
|
|
: empty_(true)
|
2018-08-02 14:23:27 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Image::Image(const Url &url, qreal scale)
|
2018-08-06 18:25:47 +02:00
|
|
|
: url_(url)
|
|
|
|
, scale_(scale)
|
|
|
|
, shouldLoad_(true)
|
2018-08-02 14:23:27 +02:00
|
|
|
{
|
2017-01-11 18:52:09 +01:00
|
|
|
}
|
|
|
|
|
2018-08-10 18:56:17 +02:00
|
|
|
Image::Image(const QPixmap &pixmap, qreal scale)
|
2018-08-06 18:25:47 +02:00
|
|
|
: scale_(scale)
|
2018-08-10 18:56:17 +02:00
|
|
|
, frames_(QVector<Frame<QPixmap>>{Frame<QPixmap>{pixmap}})
|
2018-08-02 14:23:27 +02:00
|
|
|
{
|
|
|
|
}
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2018-08-06 18:25:47 +02:00
|
|
|
const Url &Image::url() const
|
2018-08-02 14:23:27 +02:00
|
|
|
{
|
|
|
|
return this->url_;
|
|
|
|
}
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2018-08-10 18:56:17 +02:00
|
|
|
boost::optional<QPixmap> Image::pixmap() const
|
2018-08-02 14:23:27 +02:00
|
|
|
{
|
|
|
|
assertInGuiThread();
|
2018-04-06 17:46:12 +02:00
|
|
|
|
2018-08-06 18:25:47 +02:00
|
|
|
if (this->shouldLoad_) {
|
|
|
|
const_cast<Image *>(this)->shouldLoad_ = false;
|
2018-08-02 14:23:27 +02:00
|
|
|
const_cast<Image *>(this)->load();
|
|
|
|
}
|
2018-04-16 23:48:30 +02:00
|
|
|
|
2018-08-06 18:25:47 +02:00
|
|
|
return this->frames_.current();
|
2018-08-02 14:23:27 +02:00
|
|
|
}
|
2018-04-16 23:48:30 +02:00
|
|
|
|
2018-08-06 18:25:47 +02:00
|
|
|
qreal Image::scale() const
|
2018-08-02 14:23:27 +02:00
|
|
|
{
|
|
|
|
return this->scale_;
|
|
|
|
}
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2018-08-10 18:56:17 +02:00
|
|
|
bool Image::isEmpty() const
|
2018-08-02 14:23:27 +02:00
|
|
|
{
|
2018-08-06 18:25:47 +02:00
|
|
|
return this->empty_;
|
2018-08-02 14:23:27 +02:00
|
|
|
}
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2018-08-06 18:25:47 +02:00
|
|
|
bool Image::animated() const
|
2018-08-02 14:23:27 +02:00
|
|
|
{
|
2018-08-06 18:25:47 +02:00
|
|
|
assertInGuiThread();
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2018-08-06 18:25:47 +02:00
|
|
|
return this->frames_.animated();
|
2018-08-02 14:23:27 +02:00
|
|
|
}
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2018-08-06 18:25:47 +02:00
|
|
|
int Image::width() const
|
2018-08-02 14:23:27 +02:00
|
|
|
{
|
2018-08-06 18:25:47 +02:00
|
|
|
assertInGuiThread();
|
2018-04-16 23:48:30 +02:00
|
|
|
|
2018-08-06 18:25:47 +02:00
|
|
|
if (auto pixmap = this->frames_.first())
|
|
|
|
return pixmap->width() * this->scale_;
|
|
|
|
else
|
|
|
|
return 16;
|
2018-08-02 14:23:27 +02:00
|
|
|
}
|
2018-04-18 17:20:33 +02:00
|
|
|
|
2018-08-06 18:25:47 +02:00
|
|
|
int Image::height() const
|
2018-08-02 14:23:27 +02:00
|
|
|
{
|
2018-08-06 18:25:47 +02:00
|
|
|
assertInGuiThread();
|
2017-10-27 20:09:02 +02:00
|
|
|
|
2018-08-06 18:25:47 +02:00
|
|
|
if (auto pixmap = this->frames_.first())
|
|
|
|
return pixmap->height() * this->scale_;
|
|
|
|
else
|
|
|
|
return 16;
|
2018-08-02 14:23:27 +02:00
|
|
|
}
|
2018-01-19 22:45:33 +01:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
void Image::load()
|
|
|
|
{
|
2018-08-06 18:25:47 +02:00
|
|
|
NetworkRequest req(this->url().string);
|
2018-08-10 18:56:17 +02:00
|
|
|
req.setExecuteConcurrently(true);
|
2018-08-02 14:23:27 +02:00
|
|
|
req.setCaller(&this->object_);
|
|
|
|
req.setUseQuickLoadCache(true);
|
2018-08-09 18:39:46 +02:00
|
|
|
req.onSuccess([that = this, weak = weakOf(this)](auto result) -> Outcome {
|
2018-08-02 14:23:27 +02:00
|
|
|
auto shared = weak.lock();
|
|
|
|
if (!shared) return Failure;
|
2018-04-18 17:10:17 +02:00
|
|
|
|
2018-08-10 18:56:17 +02:00
|
|
|
auto data = result.getData();
|
|
|
|
|
|
|
|
// const cast since we are only reading from it
|
|
|
|
QBuffer buffer(const_cast<QByteArray *>(&data));
|
|
|
|
buffer.open(QIODevice::ReadOnly);
|
|
|
|
QImageReader reader(&buffer);
|
|
|
|
auto parsed = readFrames(reader, that->url());
|
|
|
|
|
|
|
|
postToThread(makeConvertCallback(parsed, [weak](auto frames) {
|
|
|
|
if (auto shared = weak.lock()) shared->frames_ = frames;
|
|
|
|
}));
|
2018-08-02 14:23:27 +02:00
|
|
|
|
2018-08-09 17:04:15 +02:00
|
|
|
return Success;
|
2018-08-06 18:25:47 +02:00
|
|
|
});
|
2018-08-02 14:23:27 +02:00
|
|
|
|
2018-08-06 18:25:47 +02:00
|
|
|
req.execute();
|
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-10 18:56:17 +02:00
|
|
|
if (this->isEmpty() && other.isEmpty()) return true;
|
2018-08-06 18:25:47 +02:00
|
|
|
if (!this->url_.string.isEmpty() && this->url_ == other.url_) return true;
|
|
|
|
if (this->frames_.first() == other.frames_.first()) return true;
|
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
|
|
|
}
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace chatterino
|