Compare commits

..

36 Commits

Author SHA1 Message Date
e3603bc748 GIT_SILENT Upgrade ECM and KF5 version requirements for 5.60.0 release. 2019-07-06 13:15:48 +00:00
75ef81a109 QImage::byteCount -> QImage::sizeInByes 2019-07-04 22:26:53 +02:00
bff22e2a76 GIT_SILENT Upgrade Qt5 version requirement to 5.11.0. 2019-07-04 19:23:58 +02:00
0196444a99 GIT_SILENT Upgrade ECM and KF5 version requirements for 5.59.0 release. 2019-06-01 16:38:28 +00:00
90f340df24 GIT_SILENT Upgrade ECM and KF5 version requirements for 5.58.0 release. 2019-05-04 22:44:00 +00:00
1a9b5d6cb6 tga: don't try to read more than max_palette_size into palette 2019-05-01 01:51:42 +02:00
96b1d7e7bc tga: memset dst if read fails 2019-05-01 01:51:39 +02:00
bcce48012e tga: memset the whole palette array, not only the palette_size 2019-05-01 01:44:47 +02:00
0db5c89c5f Initialize the unread bits of _starttab
oss-fuzz #14446
2019-04-25 23:08:17 +02:00
6fea48c4ee xcf: Fix uninitialized memory use on broken documents
oss-fuzz #14312
2019-04-17 20:09:49 +02:00
645daec1ef ras: Don't overread input on malformed files 2019-04-17 20:03:52 +02:00
aaa285a3b9 xcf: layer is const in copy and merge, mark it as such 2019-04-17 17:37:28 +02:00
35e64c44d8 No & is a bit faster here 2019-04-17 17:37:28 +02:00
26b796f67d const & is a bit faster here 2019-04-17 17:37:28 +02:00
4692a34a1c QStringLiteral is a bit faster here 2019-04-17 17:37:28 +02:00
c0656c5181 GIT_SILENT Upgrade ECM and KF5 version requirements for 5.57.0 release. 2019-04-07 07:18:46 +00:00
83d1ca90d9 Fix compilation
Summary:
Seems only gcc can do a constexpr with strlen.

This fixes the build with clang, hopefully to with MSVC?

Reviewers: svuorela

Reviewed By: svuorela

Subscribers: svuorela, apol, pino, kde-frameworks-devel

Tags: #frameworks

Differential Revision: https://phabricator.kde.org/D20149
2019-03-31 22:18:16 +02:00
fd4fb6f596 ora:kra: qstrcmp -> memcmp
i..e don't check strings but memory

Makes oss-fuzz happier.

Reviewers: svuorela

Reviewed By: svuorela

Subscribers: apol, pino, security-team, rempt, kde-frameworks-devel

Tags: #frameworks

Differential Revision: https://phabricator.kde.org/D20143
2019-03-31 21:32:07 +02:00
a24ece396a autotests: Also exercise canRead 2019-03-31 20:58:26 +02:00
9fc6967f4f Fix RGBHandler::canRead
Summary:
As one can see in SGIImage::readImage the accepted images are

    _stream >> u16;
    if (u16 != 0x01da) {
        return false;
    }

    _stream >> _rle;
    if (_rle > 1) {
        return false;
    }

so not only \x01\xda\x01 but also \x01\xda\x00

Reviewers: svuorela

Reviewed By: svuorela

Subscribers: svuorela, kde-frameworks-devel

Tags: #frameworks

Differential Revision: https://phabricator.kde.org/D20145
2019-03-31 19:44:21 +02:00
bd704045e6 xcf: Don't crash with files with unsupported layer modes 2019-03-31 01:35:33 +01:00
af7a89fea7 GIT_SILENT: add gitignore 2019-03-04 07:03:36 +01:00
5989bba56a GIT_SILENT Upgrade ECM and KF5 version requirements for 5.56.0 release. 2019-03-02 13:27:12 +00:00
20100a1e0e ras: fix crash on broken files
Replace QVector::operator[] with QVector::value() since we can't know for
sure the values will be on range so use value() that gives us a 0 if the
index is not on range

oss-fuzz/13462
2019-03-01 23:33:35 +01:00
297b168a52 Use auto here too 2019-02-28 23:03:25 +01:00
f1c6c15b06 compile without foreach
Summary: compile without foreach

Reviewers: dfaure, apol

Reviewed By: apol

Subscribers: apol, kde-frameworks-devel

Tags: #frameworks

Differential Revision: https://phabricator.kde.org/D19317
2019-02-28 23:02:41 +01:00
156bac5e54 ras: protect the palette QVector too
oss-fuzz/13068
2019-02-13 23:50:36 +01:00
d79c11d280 ras: tweak max file check
better to do - 32 than + 32 otherwise we may overflow

oss-fuzz/13017
2019-02-11 22:57:33 +01:00
aeec934839 xcf: Fix uninitialized memory use on broken documents
oss-fuzz/12871
2019-02-08 23:27:03 +01:00
0c4f2f8e62 add const, helps understand the function better 2019-02-08 23:07:56 +01:00
4a8da73f0e ras: tweak max size that "fits" in a QVector
oss-fuzz/12951
2019-02-07 22:14:22 +01:00
039d7d8fbe ras: don't assert because we try to allicate a huge vector
oss-fuzz/12915
2019-02-06 22:06:58 +01:00
b072484dbb ras: Protect against divide by zero
oss-fuzz/12905
2019-02-05 19:51:24 +01:00
bad90cea4b xcf: Don't divide by 0
oss-fuzz/12815
2019-02-03 14:06:33 +01:00
a51cbd865f tga: fail gracefully if readRawData errors
oss-fuzz/12818
2019-02-03 13:49:11 +01:00
1a31500e55 ras: fail gracefully on height*width*bpp > length
oss-fuzz/12822
2019-02-03 13:38:44 +01:00
16 changed files with 182 additions and 78 deletions

21
.gitignore vendored Normal file
View File

@ -0,0 +1,21 @@
# Ignore the following files
*~
*.[oa]
*.diff
*.kate-swp
*.kdev4
.kdev_include_paths
*.kdevelop.pcs
*.moc
*.moc.cpp
*.orig
*.user
.*.swp
.swp.*
Doxyfile
Makefile
avail
random_seed
/build*/
CMakeLists.txt.user*
*.unc-backup*

View File

@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.5)
project(KImageFormats)
include(FeatureSummary)
find_package(ECM 5.55.0 NO_MODULE)
find_package(ECM 5.60.0 NO_MODULE)
set_package_properties(ECM PROPERTIES TYPE REQUIRED DESCRIPTION "Extra CMake Modules." URL "https://projects.kde.org/projects/kdesupport/extra-cmake-modules")
feature_summary(WHAT REQUIRED_PACKAGES_NOT_FOUND FATAL_ON_MISSING_REQUIRED_PACKAGES)
@ -17,7 +17,7 @@ include(KDECMakeSettings)
include(CheckIncludeFiles)
set(REQUIRED_QT_VERSION 5.10.0)
set(REQUIRED_QT_VERSION 5.11.0)
find_package(Qt5Gui ${REQUIRED_QT_VERSION} REQUIRED NO_MODULE)
find_package(KF5Archive)
@ -45,7 +45,7 @@ set_package_properties(OpenEXR PROPERTIES
TYPE OPTIONAL
PURPOSE "Required for the QImage plugin for OpenEXR images"
)
add_definitions(-DQT_NO_FOREACH)
add_subdirectory(src)
if (BUILD_TESTING)
add_subdirectory(autotests)

View File

@ -210,11 +210,11 @@ private Q_SLOTS:
QFile picDumpFile(fileNameBase + QStringLiteral("-expected.data"));
QVERIFY2(picDumpFile.open(QIODevice::WriteOnly), qPrintable(picDumpFile.errorString()));
picDumpFile.write(reinterpret_cast<const char *>(inputImage.bits()),
inputImage.byteCount());
inputImage.sizeInBytes());
QFile pngDumpFile(fileNameBase + QStringLiteral("-actual.data"));
QVERIFY2(pngDumpFile.open(QIODevice::WriteOnly), qPrintable(pngDumpFile.errorString()));
pngDumpFile.write(reinterpret_cast<const char *>(expImage.bits()),
expImage.byteCount());
expImage.sizeInBytes());
QString msg = QStringLiteral("Read image (")
+ picDumpFile.fileName()
+ QStringLiteral(") differed from expected image (")

View File

@ -35,8 +35,8 @@ static void writeImageData(const char *name, const QString &filename, const QIma
{
QFile file(filename);
if (file.open(QIODevice::WriteOnly)) {
qint64 written = file.write(reinterpret_cast<const char *>(image.bits()), image.byteCount());
if (written == image.byteCount()) {
qint64 written = file.write(reinterpret_cast<const char *>(image.bits()), image.sizeInBytes());
if (written == image.sizeInBytes()) {
QTextStream(stdout) << " " << name
<< " written to " << filename << "\n";
} else {
@ -127,7 +127,8 @@ int main(int argc, char ** argv)
<< "Starting basic read tests for "
<< suffix << " images *********\n";
foreach (QFileInfo fi, imgdir.entryInfoList()) {
const QFileInfoList lstImgDir = imgdir.entryInfoList();
for (const QFileInfo &fi : lstImgDir) {
int suffixPos = fi.filePath().count() - suffix.count();
QString inputfile = fi.filePath();
QString expfile = fi.filePath().replace(suffixPos, suffix.count(), QStringLiteral("png"));
@ -147,6 +148,14 @@ int main(int argc, char ** argv)
++failed;
continue;
}
if (!inputReader.canRead()) {
QTextStream(stdout) << "FAIL : " << fi.fileName()
<< ": failed can read: "
<< inputReader.errorString()
<< "\n";
++failed;
continue;
}
if (!inputReader.read(&inputImage)) {
QTextStream(stdout) << "FAIL : " << fi.fileName()
<< ": failed to load: "

View File

@ -73,8 +73,8 @@ int main(int argc, char ** argv)
QTextStream(stdout) << "********* "
<< "Starting basic write tests for "
<< suffix << " images *********\n";
foreach (QFileInfo fi, imgdir.entryInfoList()) {
const QFileInfoList lstImgDir = imgdir.entryInfoList();
for (const QFileInfo &fi : lstImgDir) {
int suffixPos = fi.filePath().count() - suffix.count();
QString pngfile = fi.filePath().replace(suffixPos, suffix.count(), QStringLiteral("png"));
QString pngfilename = QFileInfo(pngfile).fileName();

View File

@ -82,7 +82,7 @@ void K_IStream::clear()
* format into the normal 32 bit pixel format. Process is from the
* ILM code.
*/
QRgb RgbaToQrgba(struct Imf::Rgba imagePixel)
QRgb RgbaToQrgba(struct Imf::Rgba &imagePixel)
{
float r, g, b, a;

View File

@ -18,6 +18,9 @@
#include <QIODevice>
#include <QFile>
static constexpr char s_magic[] = "application/x-krita";
static constexpr int s_magic_size = sizeof(s_magic) - 1; // -1 to remove the last \0
KraHandler::KraHandler()
{
}
@ -36,7 +39,7 @@ bool KraHandler::read(QImage *image)
KZip zip(device());
if (!zip.open(QIODevice::ReadOnly)) return false;
const KArchiveEntry *entry = zip.directory()->entry(QLatin1String("mergedimage.png"));
const KArchiveEntry *entry = zip.directory()->entry(QStringLiteral("mergedimage.png"));
if (!entry || !entry->isFile()) return false;
const KZipFileEntry* fileZipEntry = static_cast<const KZipFileEntry*>(entry);
@ -55,7 +58,7 @@ bool KraHandler::canRead(QIODevice *device)
char buff[57];
if (device->peek(buff, sizeof(buff)) == sizeof(buff))
return qstrcmp(buff + 0x26, "application/x-krita") == 0;
return memcmp(buff + 0x26, s_magic, s_magic_size) == 0;
return false;
}

View File

@ -17,6 +17,9 @@
#include <kzip.h>
static constexpr char s_magic[] = "image/openraster";
static constexpr int s_magic_size = sizeof(s_magic) - 1; // -1 to remove the last \0
OraHandler::OraHandler()
{
}
@ -35,7 +38,7 @@ bool OraHandler::read(QImage *image)
KZip zip(device());
if (!zip.open(QIODevice::ReadOnly)) return false;
const KArchiveEntry *entry = zip.directory()->entry(QLatin1String("mergedimage.png"));
const KArchiveEntry *entry = zip.directory()->entry(QStringLiteral("mergedimage.png"));
if (!entry || !entry->isFile()) return false;
const KZipFileEntry* fileZipEntry = static_cast<const KZipFileEntry*>(entry);
@ -54,7 +57,7 @@ bool OraHandler::canRead(QIODevice *device)
char buff[54];
if (device->peek(buff, sizeof(buff)) == sizeof(buff))
return qstrcmp(buff + 0x26, "image/openraster") == 0;
return memcmp(buff + 0x26, s_magic, s_magic_size) == 0;
return false;
}

View File

@ -23,7 +23,7 @@ public:
quint8 g;
quint8 b;
static RGB from(const QRgb &color)
static RGB from(const QRgb color)
{
RGB c;
c.r = qRed(color);
@ -169,7 +169,7 @@ static QDataStream &operator>>(QDataStream &s, PCXHEADER &ph)
return s;
}
static QDataStream &operator<<(QDataStream &s, const RGB &rgb)
static QDataStream &operator<<(QDataStream &s, const RGB rgb)
{
s << rgb.r << rgb.g << rgb.b;

View File

@ -173,9 +173,9 @@ static QDataStream &operator<< (QDataStream &s, const QList<PicChannel> &channel
return s;
}
static bool readRow(QDataStream &stream, QRgb *row, quint16 width, QList<PicChannel> channels)
static bool readRow(QDataStream &stream, QRgb *row, quint16 width, const QList<PicChannel> &channels)
{
Q_FOREACH(const PicChannel &channel, channels) {
for(const PicChannel &channel : channels) {
auto readPixel = [&] (QDataStream &str) -> QRgb {
quint8 red = 0;
if (channel.code & RED) {
@ -242,7 +242,7 @@ bool SoftimagePICHandler::read(QImage *image)
}
QImage::Format fmt = QImage::Format_RGB32;
Q_FOREACH(const PicChannel &channel, m_channels) {
for (const PicChannel &channel : qAsConst(m_channels)) {
if (channel.size != 8) {
// we cannot read images that do not come in bytes
qDebug() << "Channel size was" << channel.size;
@ -388,8 +388,8 @@ void SoftimagePICHandler::setOption(ImageOption option, const QVariant &value)
break;
case Description: {
m_description.clear();
QStringList entries = value.toString().split(QStringLiteral("\n\n"));
Q_FOREACH(const QString entry, entries) {
const QStringList entries = value.toString().split(QStringLiteral("\n\n"));
for (const QString &entry : entries) {
if (entry.startsWith(QStringLiteral("Description: "))) {
m_description = entry.mid(13).simplified().toUtf8();
}
@ -425,7 +425,7 @@ QVariant SoftimagePICHandler::option(ImageOption option) const
return QString();
case ImageFormat:
if (const_cast<SoftimagePICHandler*>(this)->readChannels()) {
Q_FOREACH (const PicChannel &channel, m_channels) {
for (const PicChannel &channel : qAsConst(m_channels)) {
if (channel.code & ALPHA) {
return QImage::Format_ARGB32;
}

View File

@ -102,6 +102,13 @@ static bool IsSupported(const RasHeader &head)
static bool LoadRAS(QDataStream &s, const RasHeader &ras, QImage &img)
{
s.device()->seek(RasHeader::SIZE);
// QVector uses some extra space for stuff, hence the 32 here suggested by thiago
if (ras.ColorMapLength > std::numeric_limits<int>::max() - 32) {
qWarning() << "LoadRAS() unsupported image color map length in file header" << ras.ColorMapLength;
return false;
}
// Read palette if needed.
QVector<quint8> palette(ras.ColorMapLength);
if (ras.ColorMapType == 1) {
@ -110,19 +117,36 @@ static bool LoadRAS(QDataStream &s, const RasHeader &ras, QImage &img)
}
}
const int bpp = ras.Depth / 8;
if (ras.Height == 0) {
return false;
}
if (bpp == 0) {
return false;
}
if (ras.Length / ras.Height / bpp < ras.Width) {
qWarning() << "LoadRAS() mistmatch between height and width" << ras.Width << ras.Height << ras.Length << ras.Depth;
return false;
}
// QVector uses some extra space for stuff, hence the 32 here suggested by thiago
if (ras.Length > std::numeric_limits<int>::max() - 32) {
qWarning() << "LoadRAS() unsupported image length in file header" << ras.Length;
return false;
}
// each line must be a factor of 16 bits, so they may contain padding
// this will be 1 if padding required, 0 otherwise
int paddingrequired = (ras.Width * (ras.Depth / 8) % 2);
const int paddingrequired = (ras.Width * bpp % 2);
// qDebug() << "paddingrequired: " << paddingrequired;
// don't trust ras.Length
QVector<quint8> input(ras.Length);
int i = 0;
while (! s.atEnd()) {
while (! s.atEnd() && i < input.size()) {
s >> input[i];
// I guess we need to find out if we're at the end of a line
if (paddingrequired && i != 0 && !(i % (ras.Width * (ras.Depth / 8)))) {
if (paddingrequired && i != 0 && !(i % (ras.Width * bpp))) {
s >> input[i];
}
i++;
@ -140,9 +164,9 @@ static bool LoadRAS(QDataStream &s, const RasHeader &ras, QImage &img)
quint8 red, green, blue;
for (quint32 y = 0; y < ras.Height; y++) {
for (quint32 x = 0; x < ras.Width; x++) {
red = palette[(int)input[y * ras.Width + x]];
green = palette[(int)input[y * ras.Width + x] + (ras.ColorMapLength / 3)];
blue = palette[(int)input[y * ras.Width + x] + 2 * (ras.ColorMapLength / 3)];
red = palette.value((int)input[y * ras.Width + x]);
green = palette.value((int)input[y * ras.Width + x] + (ras.ColorMapLength / 3));
blue = palette.value((int)input[y * ras.Width + x] + 2 * (ras.ColorMapLength / 3));
img.setPixel(x, y, qRgb(red, green, blue));
}
}

View File

@ -336,6 +336,9 @@ bool SGIImage::readImage(QImage &img)
_stream >> _starttab[l];
_starttab[l] -= 512 + _numrows * 2 * sizeof(quint32);
}
for (; l < _numrows; l++) {
_starttab[l] = 0;
}
_lengthtab = new quint32[_numrows];
for (l = 0; l < _numrows; l++) {
@ -709,7 +712,7 @@ bool RGBHandler::canRead(QIODevice *device)
device->seek(oldPos);
}
return head.size() >= 4 && head.startsWith("\x01\xda\x01") && (head[3] == 1 || head[3] == 2);
return head.size() >= 4 && head.startsWith("\x01\xda") && (head[2] == 0 || head[2] == 1) && (head[3] == 1 || head[3] == 2);
}
///////////////////////////////////////////////////////////////////////////////

View File

@ -201,13 +201,20 @@ static bool LoadTGA(QDataStream &s, const TgaHeader &tga, QImage &img)
}
// Read palette.
char palette[768];
static const int max_palette_size = 768;
char palette[max_palette_size];
if (info.pal) {
// @todo Support palettes in other formats!
const int size = 3 * tga.colormap_length;
const int dataRead = s.readRawData(palette, size);
if (dataRead < size) {
memset(&palette[dataRead], 0, size - dataRead);
const int palette_size = 3 * tga.colormap_length;
if (palette_size > max_palette_size) {
return false;
}
const int dataRead = s.readRawData(palette, palette_size);
if (dataRead < 0) {
return false;
}
if (dataRead < max_palette_size) {
memset(&palette[dataRead], 0, max_palette_size - dataRead);
}
}
@ -253,13 +260,24 @@ static bool LoadTGA(QDataStream &s, const TgaHeader &tga, QImage &img)
} else {
// Raw pixels.
count *= pixel_size;
s.readRawData(dst, count);
const int dataRead = s.readRawData(dst, count);
if (dataRead < 0) {
free(image);
return false;
}
if ((uint)dataRead < count) {
memset(&dst[dataRead], 0, count - dataRead);
}
dst += count;
}
}
} else {
// Read raw image.
const int dataRead = s.readRawData((char *)image, size);
if (dataRead < 0) {
free(image);
return false;
}
if (dataRead < size) {
memset(&image[dataRead], 0, size - dataRead);
}

View File

@ -167,11 +167,11 @@ private:
//! The bottom-most layer is copied into the final QImage by this
//! routine.
typedef void (*PixelCopyOperation)(Layer &layer, uint i, uint j, int k, int l,
typedef void (*PixelCopyOperation)(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n);
//! Higher layers are merged into the final QImage by this routine.
typedef void (*PixelMergeOperation)(Layer &layer, uint i, uint j, int k, int l,
typedef void (*PixelMergeOperation)(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n);
//! Layer mode static data.
@ -201,37 +201,37 @@ private:
int data_length, qint32 bpp);
static void copyLayerToImage(XCFImage &xcf_image);
static void copyRGBToRGB(Layer &layer, uint i, uint j, int k, int l,
static void copyRGBToRGB(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n);
static void copyGrayToGray(Layer &layer, uint i, uint j, int k, int l,
static void copyGrayToGray(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n);
static void copyGrayToRGB(Layer &layer, uint i, uint j, int k, int l,
static void copyGrayToRGB(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n);
static void copyGrayAToRGB(Layer &layer, uint i, uint j, int k, int l,
static void copyGrayAToRGB(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n);
static void copyIndexedToIndexed(Layer &layer, uint i, uint j, int k, int l,
static void copyIndexedToIndexed(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n);
static void copyIndexedAToIndexed(Layer &layer, uint i, uint j, int k, int l,
static void copyIndexedAToIndexed(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n);
static void copyIndexedAToRGB(Layer &layer, uint i, uint j, int k, int l,
static void copyIndexedAToRGB(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n);
static void mergeLayerIntoImage(XCFImage &xcf_image);
static void mergeRGBToRGB(Layer &layer, uint i, uint j, int k, int l,
static void mergeRGBToRGB(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n);
static void mergeGrayToGray(Layer &layer, uint i, uint j, int k, int l,
static void mergeGrayToGray(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n);
static void mergeGrayAToGray(Layer &layer, uint i, uint j, int k, int l,
static void mergeGrayAToGray(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n);
static void mergeGrayToRGB(Layer &layer, uint i, uint j, int k, int l,
static void mergeGrayToRGB(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n);
static void mergeGrayAToRGB(Layer &layer, uint i, uint j, int k, int l,
static void mergeGrayAToRGB(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n);
static void mergeIndexedToIndexed(Layer &layer, uint i, uint j, int k, int l,
static void mergeIndexedToIndexed(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n);
static void mergeIndexedAToIndexed(Layer &layer, uint i, uint j, int k, int l,
static void mergeIndexedAToIndexed(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n);
static void mergeIndexedAToRGB(Layer &layer, uint i, uint j, int k, int l,
static void mergeIndexedAToRGB(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n);
static void initializeRandomTable();
@ -244,6 +244,12 @@ bool XCFImageFormat::random_table_initialized;
QVector<QRgb> XCFImageFormat::grayTable;
template <typename T, size_t N>
constexpr size_t countof(T(&)[N])
{
return N;
}
const XCFImageFormat::LayerModes XCFImageFormat::layer_modes[] = {
{true}, // NORMAL_MODE
{true}, // DISSOLVE_MODE
@ -270,7 +276,7 @@ const XCFImageFormat::LayerModes XCFImageFormat::layer_modes[] = {
};
//! Change a QRgb value's alpha only.
inline QRgb qRgba(const QRgb &rgb, int a)
inline QRgb qRgba(const QRgb rgb, int a)
{
return ((a & 0xff) << 24 | (rgb & RGB_MASK));
}
@ -709,6 +715,10 @@ bool XCFImageFormat::loadLayerProperties(QDataStream &xcf_io, Layer &layer)
case PROP_MODE:
property >> layer.mode;
if (layer.mode >= countof(layer_modes)) {
qWarning() << "Found layer with unsupported mode" << layer.mode << "Defaulting to mode 0";
layer.mode = 0;
}
break;
case PROP_TATTOO:
@ -901,7 +911,7 @@ void XCFImageFormat::setPalette(XCFImage &xcf_image, QImage &image)
void XCFImageFormat::assignImageBytes(Layer &layer, uint i, uint j)
{
QImage &image = layer.image_tiles[j][i];
uchar *tile = layer.tile;
const uchar *tile = layer.tile;
const int width = image.width();
const int height = image.height();
const int bytesPerLine = image.bytesPerLine();
@ -1063,6 +1073,16 @@ bool XCFImageFormat::loadLevel(QDataStream &xcf_io, Layer &layer, qint32 bpp)
xcf_io >> width >> height >> offset;
if (offset == 0) {
// offset 0 with rowsxcols != 0 is probably an error since it means we have tiles
// without data but just clear the bits for now instead of returning false
for (uint j = 0; j < layer.nrows; j++) {
for (uint i = 0; i < layer.ncols; i++) {
layer.image_tiles[j][i].fill(Qt::transparent);
if (layer.type == GRAYA_GIMAGE || layer.type == INDEXEDA_GIMAGE) {
layer.alpha_tiles[j][i].fill(Qt::transparent);
}
}
}
return true;
}
@ -1623,7 +1643,7 @@ void XCFImageFormat::copyLayerToImage(XCFImage &xcf_image)
* \param m x pixel of destination image.
* \param n y pixel of destination image.
*/
void XCFImageFormat::copyRGBToRGB(Layer &layer, uint i, uint j, int k, int l,
void XCFImageFormat::copyRGBToRGB(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n)
{
QRgb src = layer.image_tiles[j][i].pixel(k, l);
@ -1654,7 +1674,7 @@ void XCFImageFormat::copyRGBToRGB(Layer &layer, uint i, uint j, int k, int l,
* \param m x pixel of destination image.
* \param n y pixel of destination image.
*/
void XCFImageFormat::copyGrayToGray(Layer &layer, uint i, uint j, int k, int l,
void XCFImageFormat::copyGrayToGray(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n)
{
int src = layer.image_tiles[j][i].pixelIndex(k, l);
@ -1674,7 +1694,7 @@ void XCFImageFormat::copyGrayToGray(Layer &layer, uint i, uint j, int k, int l,
* \param m x pixel of destination image.
* \param n y pixel of destination image.
*/
void XCFImageFormat::copyGrayToRGB(Layer &layer, uint i, uint j, int k, int l,
void XCFImageFormat::copyGrayToRGB(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n)
{
QRgb src = layer.image_tiles[j][i].pixel(k, l);
@ -1695,7 +1715,7 @@ void XCFImageFormat::copyGrayToRGB(Layer &layer, uint i, uint j, int k, int l,
* \param m x pixel of destination image.
* \param n y pixel of destination image.
*/
void XCFImageFormat::copyGrayAToRGB(Layer &layer, uint i, uint j, int k, int l,
void XCFImageFormat::copyGrayAToRGB(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n)
{
QRgb src = layer.image_tiles[j][i].pixel(k, l);
@ -1723,7 +1743,7 @@ void XCFImageFormat::copyGrayAToRGB(Layer &layer, uint i, uint j, int k, int l,
* \param m x pixel of destination image.
* \param n y pixel of destination image.
*/
void XCFImageFormat::copyIndexedToIndexed(Layer &layer, uint i, uint j, int k, int l,
void XCFImageFormat::copyIndexedToIndexed(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n)
{
int src = layer.image_tiles[j][i].pixelIndex(k, l);
@ -1741,7 +1761,7 @@ void XCFImageFormat::copyIndexedToIndexed(Layer &layer, uint i, uint j, int k, i
* \param m x pixel of destination image.
* \param n y pixel of destination image.
*/
void XCFImageFormat::copyIndexedAToIndexed(Layer &layer, uint i, uint j, int k, int l,
void XCFImageFormat::copyIndexedAToIndexed(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n)
{
uchar src = layer.image_tiles[j][i].pixelIndex(k, l);
@ -1776,7 +1796,7 @@ void XCFImageFormat::copyIndexedAToIndexed(Layer &layer, uint i, uint j, int k,
* \param m x pixel of destination image.
* \param n y pixel of destination image.
*/
void XCFImageFormat::copyIndexedAToRGB(Layer &layer, uint i, uint j, int k, int l,
void XCFImageFormat::copyIndexedAToRGB(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n)
{
QRgb src = layer.image_tiles[j][i].pixel(k, l);
@ -1913,7 +1933,7 @@ void XCFImageFormat::mergeLayerIntoImage(XCFImage &xcf_image)
* \param m x pixel of destination image.
* \param n y pixel of destination image.
*/
void XCFImageFormat::mergeRGBToRGB(Layer &layer, uint i, uint j, int k, int l,
void XCFImageFormat::mergeRGBToRGB(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n)
{
QRgb src = layer.image_tiles[j][i].pixel(k, l);
@ -2214,7 +2234,7 @@ void XCFImageFormat::mergeRGBToRGB(Layer &layer, uint i, uint j, int k, int l,
uchar new_r, new_g, new_b, new_a;
new_a = dst_a + INT_MULT(OPAQUE_OPACITY - dst_a, src_a);
float src_ratio = (float)src_a / new_a;
const float src_ratio = new_a == 0 ? 1.0 : (float)src_a / new_a;
float dst_ratio = 1.0 - src_ratio;
new_r = (uchar)(src_ratio * src_r + dst_ratio * dst_r + EPSILON);
@ -2239,7 +2259,7 @@ void XCFImageFormat::mergeRGBToRGB(Layer &layer, uint i, uint j, int k, int l,
* \param m x pixel of destination image.
* \param n y pixel of destination image.
*/
void XCFImageFormat::mergeGrayToGray(Layer &layer, uint i, uint j, int k, int l,
void XCFImageFormat::mergeGrayToGray(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n)
{
int src = layer.image_tiles[j][i].pixelIndex(k, l);
@ -2257,7 +2277,7 @@ void XCFImageFormat::mergeGrayToGray(Layer &layer, uint i, uint j, int k, int l,
* \param m x pixel of destination image.
* \param n y pixel of destination image.
*/
void XCFImageFormat::mergeGrayAToGray(Layer &layer, uint i, uint j, int k, int l,
void XCFImageFormat::mergeGrayAToGray(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n)
{
int src = qGray(layer.image_tiles[j][i].pixel(k, l));
@ -2373,7 +2393,7 @@ void XCFImageFormat::mergeGrayAToGray(Layer &layer, uint i, uint j, int k, int l
uchar new_a = OPAQUE_OPACITY;
float src_ratio = (float)src_a / new_a;
const float src_ratio = new_a == 0 ? 1.0 : (float)src_a / new_a;
float dst_ratio = 1.0 - src_ratio;
uchar new_g = (uchar)(src_ratio * src + dst_ratio * dst + EPSILON);
@ -2394,7 +2414,7 @@ void XCFImageFormat::mergeGrayAToGray(Layer &layer, uint i, uint j, int k, int l
* \param m x pixel of destination image.
* \param n y pixel of destination image.
*/
void XCFImageFormat::mergeGrayToRGB(Layer &layer, uint i, uint j, int k, int l,
void XCFImageFormat::mergeGrayToRGB(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n)
{
QRgb src = layer.image_tiles[j][i].pixel(k, l);
@ -2415,7 +2435,7 @@ void XCFImageFormat::mergeGrayToRGB(Layer &layer, uint i, uint j, int k, int l,
* \param m x pixel of destination image.
* \param n y pixel of destination image.
*/
void XCFImageFormat::mergeGrayAToRGB(Layer &layer, uint i, uint j, int k, int l,
void XCFImageFormat::mergeGrayAToRGB(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n)
{
int src = qGray(layer.image_tiles[j][i].pixel(k, l));
@ -2546,7 +2566,7 @@ void XCFImageFormat::mergeGrayAToRGB(Layer &layer, uint i, uint j, int k, int l,
uchar new_a = dst_a + INT_MULT(OPAQUE_OPACITY - dst_a, src_a);
float src_ratio = (float)src_a / new_a;
const float src_ratio = new_a == 0 ? 1.0 : (float)src_a / new_a;
float dst_ratio = 1.0 - src_ratio;
uchar new_g = (uchar)(src_ratio * src + dst_ratio * dst + EPSILON);
@ -2569,7 +2589,7 @@ void XCFImageFormat::mergeGrayAToRGB(Layer &layer, uint i, uint j, int k, int l,
* \param m x pixel of destination image.
* \param n y pixel of destination image.
*/
void XCFImageFormat::mergeIndexedToIndexed(Layer &layer, uint i, uint j, int k, int l,
void XCFImageFormat::mergeIndexedToIndexed(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n)
{
int src = layer.image_tiles[j][i].pixelIndex(k, l);
@ -2587,7 +2607,7 @@ void XCFImageFormat::mergeIndexedToIndexed(Layer &layer, uint i, uint j, int k,
* \param m x pixel of destination image.
* \param n y pixel of destination image.
*/
void XCFImageFormat::mergeIndexedAToIndexed(Layer &layer, uint i, uint j, int k, int l,
void XCFImageFormat::mergeIndexedAToIndexed(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n)
{
uchar src = layer.image_tiles[j][i].pixelIndex(k, l);
@ -2619,7 +2639,7 @@ void XCFImageFormat::mergeIndexedAToIndexed(Layer &layer, uint i, uint j, int k,
* \param m x pixel of destination image.
* \param n y pixel of destination image.
*/
void XCFImageFormat::mergeIndexedAToRGB(Layer &layer, uint i, uint j, int k, int l,
void XCFImageFormat::mergeIndexedAToRGB(const Layer &layer, uint i, uint j, int k, int l,
QImage &image, int m, int n)
{
QRgb src = layer.image_tiles[j][i].pixel(k, l);

View File

@ -64,11 +64,13 @@ int main(int argc, char **argv)
if (parser.isSet(listformats)) {
QTextStream out(stdout);
out << "Input formats:\n";
foreach (const QByteArray &fmt, QImageReader::supportedImageFormats()) {
const auto lstReaderSupportedFormats = QImageReader::supportedImageFormats();
for (const QByteArray &fmt : lstReaderSupportedFormats) {
out << " " << fmt << '\n';
}
out << "Output formats:\n";
foreach (const QByteArray &fmt, QImageWriter::supportedImageFormats()) {
const auto lstWriterSupportedFormats = QImageWriter::supportedImageFormats();
for (const QByteArray &fmt : lstWriterSupportedFormats) {
out << " " << fmt << '\n';
}
return 0;

View File

@ -72,7 +72,8 @@ int main(int argc, char **argv)
if (parser.isSet(listformats)) {
QTextStream out(stdout);
out << "File formats:\n";
foreach (const QByteArray &fmt, QImageReader::supportedImageFormats()) {
const auto lstSupportedFormats = QImageReader::supportedImageFormats();
for (const auto &fmt : lstSupportedFormats) {
out << " " << fmt << '\n';
}
return 0;
@ -115,8 +116,8 @@ int main(int argc, char **argv)
}
img = img.convertToFormat(qformat);
}
qint64 written = output.write(reinterpret_cast<const char *>(img.bits()), img.byteCount());
if (written != img.byteCount()) {
qint64 written = output.write(reinterpret_cast<const char *>(img.bits()), img.sizeInBytes());
if (written != img.sizeInBytes()) {
QTextStream(stderr) << "Could not write image data to " << files.at(1)
<< ":" << output.errorString() << "\n";
return 5;