worked on Image

This commit is contained in:
fourtf
2018-08-06 18:25:47 +02:00
parent c2e2dfb577
commit 35d462d1f1
15 changed files with 284 additions and 302 deletions
+173 -195
View File
@@ -19,13 +19,130 @@
#include <thread>
namespace chatterino {
namespace {
// Frame
Frame::Frame(const QPixmap *nonOwning, int duration)
: nonOwning_(nonOwning)
, duration_(duration)
{
}
Frame::Frame(std::unique_ptr<QPixmap> owning, int duration)
: owning_(std::move(owning))
, duration_(duration)
{
}
int Frame::duration() const
{
return this->duration_;
}
const QPixmap *Frame::pixmap() const
{
if (this->nonOwning_) return this->nonOwning_;
return this->owning_.get();
}
// Frames
Frames::Frames()
{
DebugCount::increase("images");
}
Frames::Frames(std::vector<Frame> &&frames)
: items_(std::move(frames))
{
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()
{
this->timeOffset_ += GIF_FRAME_LENGTH;
while (true) {
this->index_ %= this->items_.size();
if (this->timeOffset_ > this->items_[this->index_].duration()) {
this->timeOffset_ -= this->items_[this->index_].duration();
this->index_ = (this->index_ + 1) % this->items_.size();
} else {
break;
}
}
}
bool Frames::animated() const
{
return this->items_.size() > 1;
}
const QPixmap *Frames::current() const
{
if (this->items_.size() == 0) return nullptr;
return this->items_[this->index_].pixmap();
}
const QPixmap *Frames::first() const
{
if (this->items_.size() == 0) return nullptr;
return this->items_.front().pixmap();
}
// functions
std::vector<Frame> readFrames(QImageReader &reader, const Url &url)
{
std::vector<Frame> frames;
if (reader.imageCount() <= 0) {
Log("Error while reading image {}: '{}'", url.string, reader.errorString());
return frames;
}
QImage image;
for (int index = 0; index < reader.imageCount(); ++index) {
if (reader.read(&image)) {
auto pixmap = std::make_unique<QPixmap>(QPixmap::fromImage(image));
int duration = std::max(20, reader.nextImageDelay());
frames.push_back(Frame(std::move(pixmap), duration));
}
}
if (frames.size() != 0) {
Log("Error while reading image {}: '{}'", url.string, reader.errorString());
}
return frames;
}
void queueLoadedEvent()
{
static auto eventQueued = false;
if (!eventQueued) {
eventQueued = true;
QTimer::singleShot(250, [] {
getApp()->windows->incGeneration();
getApp()->windows->layoutChannelViews();
eventQueued = false;
});
}
}
} // namespace
// IMAGE2
std::atomic<bool> Image::loadedEventQueued{false};
ImagePtr Image::fromUrl(const Url &url, qreal scale)
{
// herb sutter cache
static std::unordered_map<Url, std::weak_ptr<Image>> cache;
static std::mutex mutex;
@@ -59,237 +176,123 @@ ImagePtr Image::getEmpty()
}
Image::Image()
: empty_(true)
{
this->isLoaded_ = true;
this->isNull_ = true;
}
Image::Image(const Url &url, qreal scale)
: url_(url)
, scale_(scale)
, shouldLoad_(true)
{
this->url_ = url;
this->scale_ = scale;
if (url.string.isEmpty()) {
this->isLoaded_ = true;
this->isNull_ = true;
}
}
Image::Image(std::unique_ptr<QPixmap> owning, qreal scale)
: scale_(scale)
{
this->frames_.push_back(Frame(std::move(owning)));
this->scale_ = scale;
this->isLoaded_ = true;
this->currentFramePixmap_ = this->frames_.front().getPixmap();
std::vector<Frame> vec;
vec.push_back(Frame(std::move(owning)));
this->frames_ = std::move(vec);
}
Image::Image(QPixmap *nonOwning, qreal scale)
: scale_(scale)
{
this->frames_.push_back(Frame(nonOwning));
this->scale_ = scale;
this->isLoaded_ = true;
this->currentFramePixmap_ = this->frames_.front().getPixmap();
std::vector<Frame> vec;
vec.push_back(Frame(nonOwning));
this->frames_ = std::move(vec);
}
const Url &Image::getUrl() const
const Url &Image::url() const
{
return this->url_;
}
NullablePtr<const QPixmap> Image::getPixmap() const
const QPixmap *Image::pixmap() const
{
assertInGuiThread();
if (!this->isLoaded_) {
if (this->shouldLoad_) {
const_cast<Image *>(this)->shouldLoad_ = false;
const_cast<Image *>(this)->load();
}
return this->currentFramePixmap_;
return this->frames_.current();
}
qreal Image::getScale() const
qreal Image::scale() const
{
return this->scale_;
}
bool Image::isAnimated() const
bool Image::empty() const
{
return this->isAnimated_;
return this->empty_;
}
int Image::getWidth() const
bool Image::animated() const
{
if (!this->isLoaded_) return 16;
assertInGuiThread();
return this->frames_.front().getPixmap()->width() * this->scale_;
return this->frames_.animated();
}
int Image::getHeight() const
int Image::width() const
{
if (!this->isLoaded_) return 16;
assertInGuiThread();
return this->frames_.front().getPixmap()->height() * this->scale_;
if (auto pixmap = this->frames_.first())
return pixmap->width() * this->scale_;
else
return 16;
}
bool Image::isLoaded() const
int Image::height() const
{
return this->isLoaded_;
}
assertInGuiThread();
bool Image::isValid() const
{
return !this->isNull_;
}
bool Image::isNull() const
{
return this->isNull_;
if (auto pixmap = this->frames_.first())
return pixmap->height() * this->scale_;
else
return 16;
}
void Image::load()
{
// decrease debug count
if (this->isAnimated_) {
DebugCount::decrease("animated images");
}
if (this->isLoaded_) {
DebugCount::decrease("loaded images");
}
this->isLoaded_ = false;
this->isLoading_ = true;
this->frames_.clear();
NetworkRequest req(this->getUrl().string);
NetworkRequest req(this->url().string);
req.setCaller(&this->object_);
req.setUseQuickLoadCache(true);
req.onSuccess([this, weak = weakOf(this)](auto result) -> Outcome {
assertInGuiThread();
auto shared = weak.lock();
if (!shared) return Failure;
auto &bytes = result.getData();
QByteArray copy = QByteArray::fromRawData(bytes.constData(), bytes.length());
// const cast since we are only reading from it
QBuffer buffer(const_cast<QByteArray *>(&result.getData()));
buffer.open(QIODevice::ReadOnly);
QImageReader reader(&buffer);
return this->parse(result.getData());
this->frames_ = readFrames(reader, this->url());
return Success;
});
req.onError([this, weak = weakOf(this)](int) {
auto shared = weak.lock();
if (!shared) return false;
this->frames_ = std::vector<Frame>();
return false;
});
req.execute();
}
Outcome Image::parse(const QByteArray &data)
{
// const cast since we are only reading from it
QBuffer buffer(const_cast<QByteArray *>(&data));
buffer.open(QIODevice::ReadOnly);
QImageReader reader(&buffer);
return this->setFrames(this->readFrames(reader));
}
std::vector<Image::Frame> Image::readFrames(QImageReader &reader)
{
std::vector<Frame> frames;
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));
int duration = std::max(20, reader.nextImageDelay());
frames.push_back(Image::Frame(pixmap, duration));
}
}
if (frames.size() != 0) {
Log("Error while reading image {}: '{}'", this->url_.string, reader.errorString());
}
return frames;
}
Outcome Image::setFrames(std::vector<Frame> frames)
{
std::lock_guard<std::mutex> lock(this->framesMutex_);
if (frames.size() > 0) {
this->currentFramePixmap_ = frames.front().getPixmap();
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;
}
this->frames_ = std::move(frames);
this->queueLoadedEvent();
return Failure;
}
void Image::queueLoadedEvent()
{
if (!loadedEventQueued) {
loadedEventQueued = true;
QTimer::singleShot(250, [] {
getApp()->windows->incGeneration();
getApp()->windows->layoutChannelViews();
loadedEventQueued = false;
});
}
}
void Image::updateAnimation()
{
if (this->isAnimated_) {
std::lock_guard<std::mutex> lock(this->framesMutex_);
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();
}
}
bool Image::operator==(const Image &other) const
{
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;
}
if (this->empty() && other.empty()) return true;
if (!this->url_.string.isEmpty() && this->url_ == other.url_) return true;
if (this->frames_.first() == other.frames_.first()) return true;
return false;
}
@@ -299,29 +302,4 @@ bool Image::operator!=(const Image &other) const
return !this->operator==(other);
}
// FRAME
Image::Frame::Frame(QPixmap *nonOwning, int duration)
: nonOwning_(nonOwning)
, duration_(duration)
{
}
Image::Frame::Frame(std::unique_ptr<QPixmap> nonOwning, int duration)
: owning_(std::move(nonOwning))
, duration_(duration)
{
}
int Image::Frame::getDuration() const
{
return this->duration_;
}
QPixmap *Image::Frame::getPixmap() const
{
if (this->nonOwning_) return this->nonOwning_;
return this->owning_.get();
}
} // namespace chatterino
+48 -44
View File
@@ -12,6 +12,41 @@
#include "common/NullablePtr.hpp"
namespace chatterino {
namespace {
class Frame
{
public:
explicit Frame(const QPixmap *nonOwning, int duration = 1);
explicit Frame(std::unique_ptr<QPixmap> owning, int duration = 1);
const QPixmap *pixmap() const;
int duration() const;
private:
const QPixmap *nonOwning_{nullptr};
std::unique_ptr<QPixmap> owning_{};
int duration_{};
};
class Frames
{
public:
Frames();
Frames(std::vector<Frame> &&frames);
~Frames();
Frames(Frames &&other) = default;
Frames &operator=(Frames &&other) = default;
bool animated() const;
void advance();
const QPixmap *current() const;
const QPixmap *first() const;
private:
std::vector<Frame> items_;
int index_{0};
int timeOffset_{0};
};
} // namespace
class Image;
using ImagePtr = std::shared_ptr<Image>;
@@ -24,62 +59,31 @@ public:
static ImagePtr fromNonOwningPixmap(QPixmap *pixmap, qreal scale = 1);
static ImagePtr getEmpty();
const Url &getUrl() const;
NullablePtr<const QPixmap> getPixmap() const;
qreal getScale() const;
bool isAnimated() const;
int getWidth() const;
int getHeight() const;
bool isLoaded() const;
bool isError() const;
bool isValid() const;
bool isNull() const;
const Url &url() const;
const QPixmap *pixmap() const;
qreal scale() const;
bool empty() const;
int width() const;
int height() const;
bool animated() const;
bool operator==(const Image &image) const;
bool operator!=(const Image &image) const;
private:
class Frame
{
public:
QPixmap *getPixmap() const;
int getDuration() const;
Frame(QPixmap *nonOwning, int duration = 1);
Frame(std::unique_ptr<QPixmap> nonOwning, int duration = 1);
private:
QPixmap *nonOwning_;
std::unique_ptr<QPixmap> owning_;
int duration_;
};
Image();
Image(const Url &url, qreal scale);
Image(std::unique_ptr<QPixmap> owning, qreal scale);
Image(QPixmap *nonOwning, qreal scale);
void load();
Outcome parse(const QByteArray &data);
std::vector<Frame> readFrames(QImageReader &reader);
Outcome setFrames(std::vector<Frame> frames);
void updateAnimation();
void queueLoadedEvent();
Url url_;
bool isLoaded_{false};
bool isLoading_{false};
bool isAnimated_{false};
bool isError_{false};
bool isNull_ = false;
qreal scale_ = 1;
QObject object_;
std::vector<Frame> frames_;
std::mutex framesMutex_;
NullablePtr<QPixmap> currentFramePixmap_;
int currentFrameIndex_ = 0;
int currentFrameOffset_ = 0;
Url url_{};
qreal scale_{1};
bool empty_{false};
bool shouldLoad_{false};
Frames frames_{};
QObject object_{};
static std::atomic<bool> loadedEventQueued;
};
+2 -2
View File
@@ -70,11 +70,11 @@ const ImagePtr &ImageSet::getImage(float scale) const
scale = 1;
}
if (this->imageX3_->isValid() && quality == 3) {
if (!this->imageX3_->empty() && quality == 3) {
return this->imageX3_;
}
if (this->imageX2_->isValid() && quality == 2) {
if (!this->imageX2_->empty() && quality == 2) {
return this->imageX3_;
}
+1
View File
@@ -1,6 +1,7 @@
#pragma once
#include <QString>
#include <common/Common.hpp>
namespace chatterino {
+7 -10
View File
@@ -70,8 +70,8 @@ ImageElement::ImageElement(ImagePtr image, MessageElement::Flags flags)
void ImageElement::addToContainer(MessageLayoutContainer &container, MessageElement::Flags flags)
{
if (flags & this->getFlags()) {
auto size = QSize(this->image_->getWidth() * container.getScale(),
this->image_->getHeight() * container.getScale());
auto size = QSize(this->image_->width() * container.getScale(),
this->image_->height() * container.getScale());
container.addElement(
(new ImageLayoutElement(*this, this->image_, size))->setLink(this->getLink()));
@@ -83,10 +83,7 @@ EmoteElement::EmoteElement(const EmotePtr &emote, MessageElement::Flags flags)
: MessageElement(flags)
, emote_(emote)
{
auto image = emote->images.getImage1();
if (image->isValid()) {
this->textElement_.reset(new TextElement(emote->getCopyString(), MessageElement::Misc));
}
this->textElement_.reset(new TextElement(emote->getCopyString(), MessageElement::Misc));
this->setTooltip(emote->tooltip.string);
}
@@ -101,10 +98,10 @@ void EmoteElement::addToContainer(MessageLayoutContainer &container, MessageElem
if (flags & this->getFlags()) {
if (flags & MessageElement::EmoteImages) {
auto image = this->emote_->images.getImage(container.getScale());
if (!image->isValid()) return;
if (image->empty()) return;
QSize size(int(container.getScale() * image->getWidth()),
int(container.getScale() * image->getHeight()));
auto size = QSize(int(container.getScale() * image->width()),
int(container.getScale() * image->height()));
container.addElement(
(new ImageLayoutElement(*this, image, size))->setLink(this->getLink()));
@@ -123,7 +120,7 @@ TextElement::TextElement(const QString &text, MessageElement::Flags flags,
, color_(color)
, style_(style)
{
for (QString word : text.split(' ')) {
for (const auto &word : text.split(' ')) {
this->words_.push_back({word, -1});
// fourtf: add logic to store multiple spaces after message
}
@@ -91,8 +91,8 @@ void ImageLayoutElement::paint(QPainter &painter)
return;
}
auto pixmap = this->image_->getPixmap();
if (pixmap && !this->image_->isAnimated()) {
auto pixmap = this->image_->pixmap();
if (pixmap && !this->image_->animated()) {
// fourtf: make it use qreal values
painter.drawPixmap(QRectF(this->getRect()), *pixmap, QRectF());
}
@@ -104,8 +104,8 @@ void ImageLayoutElement::paintAnimated(QPainter &painter, int yOffset)
return;
}
if (this->image_->isAnimated()) {
if (auto pixmap = this->image_->getPixmap()) {
if (this->image_->animated()) {
if (auto pixmap = this->image_->pixmap()) {
auto rect = this->getRect();
rect.moveTop(rect.y() + yOffset);
painter.drawPixmap(QRectF(rect), *pixmap, QRectF());