worked on Image
This commit is contained in:
+173
-195
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user