Rewrite the PIC image format handler

It now uses QDataStream to deal with endianness. It also supports
several QImageIOHandler options.

This comes with a more comprehensive test suite than the old code. Note
that the old test suite was incorrect as the old code wrote the floats
in the header out incorrectly (although no-one noticed because no
software seems to care about those values).

All the test PIC files in the test suite appear correct according to the
specification (by inspection with Okteta). Unfortunately, there is a
lack of other freely-available software that reads and writes PIC files
(the main application that uses them is proprietary), and so this is the
best I can do.

REVIEW: 117944
This commit is contained in:
Alex Merry
2014-03-03 12:52:40 +00:00
parent 74906dd3bc
commit 51eca9b6a8
31 changed files with 966 additions and 660 deletions

View File

@ -56,7 +56,6 @@ endmacro()
# result against the data read from the corresponding png file
kimageformats_read_tests(
pcx
pic
psd
ras
rgb
@ -101,3 +100,16 @@ endif()
if (OpenEXR_FOUND)
# FIXME: OpenEXR tests
endif()
find_package(Qt5Test ${REQUIRED_QT_VERSION} CONFIG QUIET)
if(NOT Qt5Test_FOUND)
message(STATUS "Qt5Test not found, some autotests will not be built.")
return()
endif()
add_executable(pictest pictest.cpp)
target_link_libraries(pictest Qt5::Gui Qt5::Test)
ecm_mark_as_test(pictest)
add_test(NAME kimageformats-pic COMMAND pictest)

BIN
autotests/long-runs.pic Normal file

Binary file not shown.

Binary file not shown.

BIN
autotests/pic/4x4-alpha.pic Normal file

Binary file not shown.

BIN
autotests/pic/4x4-alpha.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 98 B

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 B

Binary file not shown.

BIN
autotests/pic/long-runs.pic Normal file

Binary file not shown.

BIN
autotests/pic/long-runs.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 117 B

Binary file not shown.

277
autotests/pictest.cpp Normal file
View File

@ -0,0 +1,277 @@
/*
* Copyright 2014 Alex Merry <alex.merry@kdemail.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) version 3, or any
* later version accepted by the membership of KDE e.V. (or its
* successor approved by the membership of KDE e.V.), which shall
* act as a proxy defined in Section 6 of version 3 of the license.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <QBuffer>
#include <QFile>
#include <QFileInfo>
#include <QImage>
#include <QImageReader>
#include <QImageWriter>
#include <QTest>
#include <QUuid>
Q_DECLARE_METATYPE(QImage::Format)
class PicTests : public QObject
{
Q_OBJECT
private:
void common_data()
{
QTest::addColumn<QString>("picfile");
QTest::addColumn<QString>("pngfile");
QTest::addColumn<QString>("comment");
// whether the pic file has/should have an alpha channel
QTest::addColumn<bool>("alpha");
// the format to convert the png file to before writing
// or comparing to the read image; this can be used to
// induce loss of data (eg: make the image monochrome)
QTest::addColumn<QImage::Format>("pngformat");
QTest::addColumn<bool>("compress");
QTest::newRow("4x4 no alpha RLE")
<< QFINDTESTDATA("pic/4x4-simple-color.pic")
<< QFINDTESTDATA("pic/4x4-simple-color.png")
<< QString()
<< false
<< QImage::Format_RGB32
<< true;
QTest::newRow("4x4 no alpha raw")
<< QFINDTESTDATA("pic/4x4-simple-color-uncompressed.pic")
<< QFINDTESTDATA("pic/4x4-simple-color.png")
<< QString()
<< false
<< QImage::Format_RGB32
<< false;
QTest::newRow("Short comment")
<< QFINDTESTDATA("pic/short-comment.pic")
<< QFINDTESTDATA("pic/4x4-simple-color.png")
<< QStringLiteral("Test comment value")
<< false
<< QImage::Format_RGB32
<< true;
QTest::newRow("Long comment")
<< QFINDTESTDATA("pic/long-comment.pic")
<< QFINDTESTDATA("pic/4x4-simple-color.png")
<< QStringLiteral("Test comment value that goes right up to the end of the comment field and has no")
<< false
<< QImage::Format_RGB32
<< true;
QTest::newRow("Long run-lengths")
<< QFINDTESTDATA("pic/long-runs.pic")
<< QFINDTESTDATA("pic/long-runs.png")
<< QString()
<< false
<< QImage::Format_RGB32
<< true;
QTest::newRow("4x4 with alpha RLE")
<< QFINDTESTDATA("pic/4x4-alpha.pic")
<< QFINDTESTDATA("pic/4x4-alpha.png")
<< QString()
<< true
<< QImage::Format_ARGB32
<< true;
QTest::newRow("4x4 with alpha raw")
<< QFINDTESTDATA("pic/4x4-alpha-uncompressed.pic")
<< QFINDTESTDATA("pic/4x4-alpha.png")
<< QString()
<< true
<< QImage::Format_ARGB32
<< false;
}
private Q_SLOTS:
void initTestCase()
{
QCoreApplication::addLibraryPath(QStringLiteral(PLUGIN_DIR));
}
void testWrite_data()
{
common_data();
// NB: 4x4-simple-color only uses solid red, blue, green and white,
// so there is no actual data loss in converting to RGB16.
// This just tests that the pic plugin can deal with different
// input formats.
QTest::newRow("altered format")
<< QFINDTESTDATA("pic/4x4-simple-color.pic")
<< QFINDTESTDATA("pic/4x4-simple-color.png")
<< QString()
<< false
<< QImage::Format_RGB16
<< true;
}
void testRead_data()
{
common_data();
// TODO: test reading files with unusual channel setups
// (eg: one channel for each component)
}
void testWrite()
{
QFETCH(QString, picfile);
QFETCH(QString, pngfile);
QFETCH(QString, comment);
QFETCH(QImage::Format, pngformat);
QFETCH(bool, compress);
QImageReader pngReader(pngfile, "png");
QImage pngImage;
QVERIFY2(pngReader.read(&pngImage), qPrintable(pngReader.errorString()));
pngImage = pngImage.convertToFormat(pngformat);
QFile expFile(picfile);
QVERIFY2(expFile.open(QIODevice::ReadOnly), qPrintable(expFile.errorString()));
QByteArray expData = expFile.readAll();
QByteArray picData;
QBuffer buffer(&picData);
QImageWriter imgWriter(&buffer, "pic");
imgWriter.setText(QStringLiteral("Description"), comment);
imgWriter.setCompression(compress);
imgWriter.write(pngImage);
if (expData != picData) {
QString fileNameBase = QUuid::createUuid().toString()
.remove(QLatin1Char('{'))
.remove(QLatin1Char('}'));
QFile dumpFile(fileNameBase + QStringLiteral(".pic"));
QVERIFY2(dumpFile.open(QIODevice::WriteOnly), qPrintable(dumpFile.errorString()));
dumpFile.write(picData);
QString msg = QStringLiteral("Written data (")
+ dumpFile.fileName()
+ QStringLiteral(") differed from expected data (")
+ picfile
+ QLatin1Char(')');
QFAIL(qPrintable(msg));
}
}
void testRead()
{
QFETCH(QString, picfile);
QFETCH(QString, pngfile);
QFETCH(bool, alpha);
QFETCH(QImage::Format, pngformat);
QImageReader inputReader(picfile, "pic");
QImageReader expReader(pngfile, "png");
QImage inputImage;
QImage expImage;
QVERIFY2(expReader.read(&expImage), qPrintable(expReader.errorString()));
QVERIFY2(inputReader.read(&inputImage), qPrintable(inputReader.errorString()));
QCOMPARE(inputImage.width(), expImage.width());
QCOMPARE(inputImage.height(), expImage.height());
QCOMPARE(inputImage.hasAlphaChannel(), alpha);
QCOMPARE(inputImage.format(), alpha ? QImage::Format_ARGB32
: QImage::Format_RGB32);
expImage = expImage.convertToFormat(pngformat);
expImage = expImage.convertToFormat(alpha ? QImage::Format_ARGB32
: QImage::Format_RGB32);
if (inputImage != expImage) {
QString fileNameBase = QUuid::createUuid().toString()
.remove(QLatin1Char('{'))
.remove(QLatin1Char('}'));
QFile picDumpFile(fileNameBase + QStringLiteral("-expected.data"));
QVERIFY2(picDumpFile.open(QIODevice::WriteOnly), qPrintable(picDumpFile.errorString()));
picDumpFile.write(reinterpret_cast<const char *>(inputImage.bits()),
inputImage.byteCount());
QFile pngDumpFile(fileNameBase + QStringLiteral("-actual.data"));
QVERIFY2(pngDumpFile.open(QIODevice::WriteOnly), qPrintable(pngDumpFile.errorString()));
pngDumpFile.write(reinterpret_cast<const char *>(expImage.bits()),
expImage.byteCount());
QString msg = QStringLiteral("Read image (")
+ picDumpFile.fileName()
+ QStringLiteral(") differed from expected image (")
+ pngDumpFile.fileName()
+ QLatin1Char(')');
QFAIL(qPrintable(msg));
}
}
void testPreReadComment_data()
{
testRead_data();
}
void testPreReadComment()
{
QFETCH(QString, picfile);
QFETCH(QString, comment);
QImageReader inputReader(picfile, "pic");
QCOMPARE(inputReader.text(QStringLiteral("Description")), comment);
}
void testPreReadSize_data()
{
testRead_data();
}
void testPreReadSize()
{
QFETCH(QString, picfile);
QFETCH(QString, pngfile);
QImageReader inputReader(picfile, "pic");
QImageReader expReader(pngfile, "png");
QCOMPARE(inputReader.size(), expReader.size());
}
void testPreReadImageFormat_data()
{
testRead_data();
}
void testPreReadImageFormat()
{
QFETCH(QString, picfile);
QFETCH(bool, alpha);
QImageReader inputReader(picfile, "pic");
QCOMPARE(inputReader.imageFormat(),
alpha ? QImage::Format_ARGB32 : QImage::Format_RGB32);
}
};
QTEST_MAIN(PicTests)
#include "pictest.moc"

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 743 B

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 574 B

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.