Skip to content
This repository has been archived by the owner on Mar 4, 2023. It is now read-only.

Commit

Permalink
Use png_get_io_ptr instead of a threadsafe mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Skycoder42 committed Feb 28, 2019
1 parent 082bb27 commit 04269e1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 44 deletions.
2 changes: 1 addition & 1 deletion examples/apng/sample/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Widget::Widget(QWidget *parent) :
auto m1 = new QMovie(QStringLiteral(":/data/sample.apng"), "apng", this);
m1->start();
ui->label_movie_apng->setMovie(m1);
auto m2 = new QMovie(QStringLiteral(":/data/sample.png"), "apng", this);
auto m2 = new QMovie(QStringLiteral("/home/sky/Programming/QtLibraries/QtApng/tests/auto/apng/apngPlugin/sample-5.apng"), "apng", this);
m2->start();
ui->label_movie_png->setMovie(m2);
}
Expand Down
46 changes: 7 additions & 39 deletions src/plugins/imageformats/apng/apngreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,14 @@
#include <QThread>
#include <QtEndian>

QMutex ApngReader::_readerMutex;
QHash<png_structp, ApngReader*> ApngReader::_readers;

ApngReader::ApngReader(QObject *parent) :
QObject{parent}
{}

ApngReader::~ApngReader()
{
if(_png) {
{
QMutexLocker lock(&_readerMutex);
_readers.remove(_png);
}
if(_png)
png_destroy_read_struct(&_png, &_info, nullptr);
}

if (_frame.rows)
delete[] _frame.rows;
Expand Down Expand Up @@ -62,11 +54,7 @@ bool ApngReader::init(QIODevice *device)
return false;
}

{
QMutexLocker lock(&_readerMutex);
_readers.insert(_png, this);
}
png_set_progressive_read_fn(_png, nullptr, &ApngReader::info_fn, &ApngReader::row_fn, &ApngReader::end_fn);
png_set_progressive_read_fn(_png, this, &ApngReader::info_fn, &ApngReader::row_fn, &ApngReader::end_fn);

//set png jump position
if (setjmp(png_jmpbuf(_png))) {
Expand Down Expand Up @@ -129,11 +117,7 @@ quint32 ApngReader::plays() const

void ApngReader::info_fn(png_structp png_ptr, png_infop info_ptr)
{
ApngReader *reader;
{
QMutexLocker lock(&_readerMutex);
reader = _readers[png_ptr];
}
auto reader = reinterpret_cast<ApngReader*>(png_get_io_ptr(png_ptr));
Frame &frame = reader->_frame;

//init png reading
Expand Down Expand Up @@ -187,22 +171,14 @@ void ApngReader::info_fn(png_structp png_ptr, png_infop info_ptr)
void ApngReader::row_fn(png_structp png_ptr, png_bytep new_row, png_uint_32 row_num, int pass)
{
Q_UNUSED(pass)
ApngReader *reader;
{
QMutexLocker lock(&_readerMutex);
reader = _readers[png_ptr];
}
auto reader = reinterpret_cast<ApngReader*>(png_get_io_ptr(png_ptr));
png_progressive_combine_row(png_ptr, reader->_frame.rows[row_num], new_row);
}

void ApngReader::end_fn(png_structp png_ptr, png_infop info_ptr)
{
Q_UNUSED(info_ptr);
ApngReader *reader;
{
QMutexLocker lock(&_readerMutex);
reader = _readers[png_ptr];
}
auto reader = reinterpret_cast<ApngReader*>(png_get_io_ptr(png_ptr));
Frame &frame = reader->_frame;

if(!reader->_animated) {
Expand All @@ -223,11 +199,7 @@ void ApngReader::end_fn(png_structp png_ptr, png_infop info_ptr)
void ApngReader::frame_info_fn(png_structp png_ptr, png_uint_32 frame_num)
{
Q_UNUSED(frame_num);
ApngReader *reader;
{
QMutexLocker lock(&_readerMutex);
reader = _readers[png_ptr];
}
auto reader = reinterpret_cast<ApngReader*>(png_get_io_ptr(png_ptr));
auto info_ptr = reader->_info;
Frame &frame = reader->_frame;

Expand All @@ -243,11 +215,7 @@ void ApngReader::frame_info_fn(png_structp png_ptr, png_uint_32 frame_num)

void ApngReader::frame_end_fn(png_structp png_ptr, png_uint_32 frame_num)
{
ApngReader *reader;
{
QMutexLocker lock(&_readerMutex);
reader = _readers[png_ptr];
}
auto reader = reinterpret_cast<ApngReader*>(png_get_io_ptr(png_ptr));
Frame &frame = reader->_frame;
auto &image = reader->_lastImg;

Expand Down
4 changes: 0 additions & 4 deletions src/plugins/imageformats/apng/apngreader_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <QIODevice>
#include <png.h>
#include <QImage>
#include <QMutex>

#ifndef PNG_APNG_SUPPORTED
#error libpng with APNG patch is required
Expand Down Expand Up @@ -45,9 +44,6 @@ class ApngReader : public QObject
quint32 plays() const;

private:
static QMutex _readerMutex;
static QHash<png_structp, ApngReader*> _readers;

QIODevice *_device = nullptr;

png_structp _png = nullptr;
Expand Down

0 comments on commit 04269e1

Please sign in to comment.