Skip to content

Commit

Permalink
Add logging categories to image handlers
Browse files Browse the repository at this point in the history
Change-Id: Ia0c47826d08b3f641c17d8a585f62d008a8b095b
Reviewed-by: Eirik Aavitsland <[email protected]>
  • Loading branch information
Allan Sandfeld Jensen committed Sep 30, 2020
1 parent b3db55f commit 426308a
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 28 deletions.
7 changes: 5 additions & 2 deletions src/gui/image/qimageiohandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,13 @@

#include <qbytearray.h>
#include <qimagereader.h>
#include <qloggingcategory.h>
#include <qvariant.h>

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(lcImageIo, "qt.gui.imageio")

class QIODevice;

class QImageIOHandlerPrivate
Expand Down Expand Up @@ -584,8 +587,8 @@ bool QImageIOHandler::allocateImage(QSize size, QImage::Format format, QImage *i
return false;
const qsizetype mb = szp.totalSize >> 20;
if (mb > mbLimit || (mb == mbLimit && szp.totalSize % (1 << 20))) {
qWarning("QImageIOHandler: Rejecting image as it exceeds the current "
"allocation limit of %i megabytes", mbLimit);
qCWarning(lcImageIo, "QImageIOHandler: Rejecting image as it exceeds the current "
"allocation limit of %i megabytes", mbLimit);
return false;
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/gui/image/qpnghandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <qdebug.h>
#include <qiodevice.h>
#include <qimage.h>
#include <qloggingcategory.h>
#include <qvariant.h>

#include <private/qimage_p.h> // for qt_getImageText
Expand Down Expand Up @@ -80,6 +81,8 @@

QT_BEGIN_NAMESPACE

Q_DECLARE_LOGGING_CATEGORY(lcImageIo)

// avoid going through QImage::scanLine() which calls detach
#define FAST_SCAN_LINE(data, bpl, y) (data + (y) * bpl)

Expand Down Expand Up @@ -497,7 +500,7 @@ static void read_image_scaled(QImage *outImage, png_structp png_ptr, png_infop i
extern "C" {
static void qt_png_warning(png_structp /*png_ptr*/, png_const_charp message)
{
qWarning("libpng warning: %s", message);
qCWarning(lcImageIo, "libpng warning: %s", message);
}

}
Expand Down Expand Up @@ -587,7 +590,7 @@ bool QPngHandlerPrivate::readPngHeader()
png_get_iCCP(png_ptr, info_ptr, &name, &compressionType, &profileData, &profLen);
colorSpace = QColorSpace::fromIccProfile(QByteArray((const char *)profileData, profLen));
if (!colorSpace.isValid()) {
qWarning() << "QPngHandler: Failed to parse ICC profile";
qCWarning(lcImageIo) << "QPngHandler: Failed to parse ICC profile";
} else {
QColorSpacePrivate *csD = QColorSpacePrivate::getWritable(colorSpace);
if (csD->description.isEmpty())
Expand Down Expand Up @@ -911,7 +914,7 @@ bool QPNGImageWriter::writeImage(const QImage& image, int compression_in, const
int compression = compression_in;
if (compression >= 0) {
if (compression > 9) {
qWarning("PNG: Compression %d out of range", compression);
qCWarning(lcImageIo, "PNG: Compression %d out of range", compression);
compression = 9;
}
png_set_compression_level(png_ptr, compression);
Expand Down Expand Up @@ -1204,7 +1207,7 @@ bool QPngHandler::canRead() const
bool QPngHandler::canRead(QIODevice *device)
{
if (!device) {
qWarning("QPngHandler::canRead() called with no device");
qCWarning(lcImageIo, "QPngHandler::canRead() called with no device");
return false;
}

Expand Down
9 changes: 7 additions & 2 deletions src/gui/image/qppmhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@

#ifndef QT_NO_IMAGEFORMAT_PPM

#include <qdebug.h>
#include <qimage.h>
#include <qlist.h>
#include <qloggingcategory.h>
#include <qrgba64.h>
#include <qvariant.h>

#include <ctype.h>
#include <qrgba64.h>

QT_BEGIN_NAMESPACE

Q_DECLARE_LOGGING_CATEGORY(lcImageIo)

/*****************************************************************************
PBM/PGM/PPM (ASCII and RAW) image read/write functions
*****************************************************************************/
Expand Down Expand Up @@ -473,7 +478,7 @@ bool QPpmHandler::canRead() const
bool QPpmHandler::canRead(QIODevice *device, QByteArray *subType)
{
if (!device) {
qWarning("QPpmHandler::canRead() called with no device");
qCWarning(lcImageIo, "QPpmHandler::canRead() called with no device");
return false;
}

Expand Down
9 changes: 8 additions & 1 deletion src/gui/image/qxbmhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@

#include <qimage.h>
#include <qiodevice.h>
#include <qloggingcategory.h>
#include <qvariant.h>

#include <stdio.h>
#include <ctype.h>

QT_BEGIN_NAMESPACE

Q_DECLARE_LOGGING_CATEGORY(lcImageIo)

/*****************************************************************************
X bitmap image read/write functions
*****************************************************************************/
Expand Down Expand Up @@ -293,14 +296,18 @@ bool QXbmHandler::canRead() const

bool QXbmHandler::canRead(QIODevice *device)
{
QImage image;
if (!device) {
qCWarning(lcImageIo, "QXbmHandler::canRead() called with no device");
return false;
}

// it's impossible to tell whether we can load an XBM or not when
// it's from a sequential device, as the only way to do it is to
// attempt to parse the whole image.
if (device->isSequential())
return false;

QImage image;
qint64 oldPos = device->pos();
bool success = read_xbm_image(device, &image);
device->seek(oldPos);
Expand Down
21 changes: 13 additions & 8 deletions src/gui/image/qxpmhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,22 @@

#ifndef QT_NO_IMAGEFORMAT_XPM

#include <private/qcolor_p.h>
#include <qbytearraymatcher.h>
#include <qdebug.h>
#include <qimage.h>
#include <qloggingcategory.h>
#include <qmap.h>
#include <qtextstream.h>
#include <qvariant.h>

#include <private/qcolor_p.h>

#include <algorithm>

QT_BEGIN_NAMESPACE

Q_DECLARE_LOGGING_CATEGORY(lcImageIo)

static quint64 xpmHash(const QString &str)
{
unsigned int hashValue = 0;
Expand Down Expand Up @@ -892,7 +897,7 @@ static bool read_xpm_body(

for(currentColor=0; currentColor < ncols; ++currentColor) {
if (!read_xpm_string(buf, device, source, index, state)) {
qWarning("QImage: XPM color specification missing");
qCWarning(lcImageIo, "XPM color specification missing");
return false;
}
QByteArray index;
Expand All @@ -907,15 +912,15 @@ static bool read_xpm_body(
if (i < 0)
i = tokens.indexOf("m");
if (i < 0) {
qWarning("QImage: XPM color specification is missing: %s", buf.constData());
qCWarning(lcImageIo, "XPM color specification is missing: %s", buf.constData());
return false; // no c/g/g4/m specification at all
}
QByteArray color;
while ((++i < tokens.size()) && !is_xpm_color_spec_prefix(tokens.at(i))) {
color.append(tokens.at(i));
}
if (color.isEmpty()) {
qWarning("QImage: XPM color value is missing from specification: %s", buf.constData());
qCWarning(lcImageIo, "XPM color value is missing from specification: %s", buf.constData());
return false; // no color value
}
buf = color;
Expand Down Expand Up @@ -958,7 +963,7 @@ static bool read_xpm_body(
// Read pixels
for(int y=0; y<h; y++) {
if (!read_xpm_string(buf, device, source, index, state)) {
qWarning("QImage: XPM pixels missing on image line %d", y);
qCWarning(lcImageIo, "XPM pixels missing on image line %d", y);
return false;
}
if (image.depth() == 8) {
Expand All @@ -984,7 +989,7 @@ static bool read_xpm_body(
}
// avoid uninitialized memory for malformed xpms
if (x < w) {
qWarning("QImage: XPM pixels missing on image line %d (possibly a C++ trigraph).", y);
qCWarning(lcImageIo, "XPM pixels missing on image line %d (possibly a C++ trigraph).", y);
memset(p, 0, w - x);
}
} else {
Expand All @@ -1001,7 +1006,7 @@ static bool read_xpm_body(
}
// avoid uninitialized memory for malformed xpms
if (x < w) {
qWarning("QImage: XPM pixels missing on image line %d (possibly a C++ trigraph).", y);
qCWarning(lcImageIo, "XPM pixels missing on image line %d (possibly a C++ trigraph).", y);
memset(p, 0, (w - x)*4);
}
}
Expand Down Expand Up @@ -1218,7 +1223,7 @@ bool QXpmHandler::canRead() const
bool QXpmHandler::canRead(QIODevice *device)
{
if (!device) {
qWarning("QXpmHandler::canRead() called with no device");
qCWarning(lcImageIo, "QXpmHandler::canRead() called with no device");
return false;
}

Expand Down
5 changes: 4 additions & 1 deletion src/plugins/imageformats/gif/qgifhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@

#include <qimage.h>
#include <qiodevice.h>
#include <qloggingcategory.h>
#include <qvariant.h>

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(lcGif, "qt.gui.imageio.gif")

#define Q_TRANSPARENT 0x00ffffff

// avoid going through QImage::scanLine() which calls detach
Expand Down Expand Up @@ -1121,7 +1124,7 @@ bool QGifHandler::canRead() const
bool QGifHandler::canRead(QIODevice *device)
{
if (!device) {
qWarning("QGifHandler::canRead() called with no device");
qCWarning(lcGif, "QGifHandler::canRead() called with no device");
return false;
}

Expand Down
7 changes: 5 additions & 2 deletions src/plugins/imageformats/ico/qicohandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@
#include <QtCore/qendian.h>
#include <private/qendian_p.h>
#include <QtGui/QImage>
#include <QtCore/QFile>
#include <QtCore/QBuffer>
#include <QtCore/QFile>
#include <QtCore/QLoggingCategory>
#include <qvariant.h>

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(lcIco, "qt.gui.imageio.ico")

// These next two structs represent how the icon information is stored
// in an ICO file.
typedef struct
Expand Down Expand Up @@ -771,7 +774,7 @@ bool QtIcoHandler::canRead() const
if (bCanRead)
setFormat("ico");
} else {
qWarning("QtIcoHandler::canRead() called with no device");
qCWarning(lcIco, "QtIcoHandler::canRead() called with no device");
}
return bCanRead;
}
Expand Down
12 changes: 8 additions & 4 deletions src/plugins/imageformats/jpeg/qjpeghandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <qdebug.h>
#include <qimage.h>
#include <qlist.h>
#include <qloggingcategory.h>
#include <qmath.h>
#include <qvariant.h>
#include <private/qicc_p.h>
Expand Down Expand Up @@ -75,6 +76,9 @@ extern "C" {
}

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(lcJpeg, "qt.gui.imageio.jpeg")

QT_WARNING_DISABLE_GCC("-Wclobbered")

Q_GUI_EXPORT void QT_FASTCALL qt_convert_rgb888_to_rgb32(quint32 *dst, const uchar *src, int len);
Expand All @@ -91,15 +95,15 @@ static void my_error_exit (j_common_ptr cinfo)
my_error_mgr* myerr = (my_error_mgr*) cinfo->err;
char buffer[JMSG_LENGTH_MAX];
(*cinfo->err->format_message)(cinfo, buffer);
qWarning("%s", buffer);
qCWarning(lcJpeg, "%s", buffer);
longjmp(myerr->setjmp_buffer, 1);
}

static void my_output_message(j_common_ptr cinfo)
{
char buffer[JMSG_LENGTH_MAX];
(*cinfo->err->format_message)(cinfo, buffer);
qWarning("%s", buffer);
qCWarning(lcJpeg,"%s", buffer);
}

}
Expand Down Expand Up @@ -913,7 +917,7 @@ static QImageIOHandler::Transformations exif2Qt(int exifOrientation)
case 8: // rotate 270 CW
return QImageIOHandler::TransformationRotate270;
}
qWarning("Invalid EXIF orientation");
qCWarning(lcJpeg, "Invalid EXIF orientation");
return QImageIOHandler::TransformationNone;
}

Expand Down Expand Up @@ -1071,7 +1075,7 @@ bool QJpegHandler::canRead() const
bool QJpegHandler::canRead(QIODevice *device)
{
if (!device) {
qWarning("QJpegHandler::canRead() called with no device");
qCWarning(lcJpeg, "QJpegHandler::canRead() called with no device");
return false;
}

Expand Down
8 changes: 4 additions & 4 deletions tests/auto/gui/image/qimagereader/tst_qimagereader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1462,10 +1462,10 @@ void tst_QImageReader::readFromResources_data()
<< QString("");
QTest::newRow("corrupt-colors.xpm") << QString("corrupt-colors.xpm")
<< QByteArray("xpm") << QSize(0, 0)
<< QString("QImage: XPM color specification is missing: bla9an.n#x");
<< QString("XPM color specification is missing: bla9an.n#x");
QTest::newRow("corrupt-pixels.xpm") << QString("corrupt-pixels.xpm")
<< QByteArray("xpm") << QSize(0, 0)
<< QString("QImage: XPM pixels missing on image line 3");
<< QString("XPM pixels missing on image line 3");
QTest::newRow("corrupt-pixel-count.xpm") << QString("corrupt-pixel-count.xpm")
<< QByteArray("xpm") << QSize(0, 0)
<< QString("");
Expand Down Expand Up @@ -1580,10 +1580,10 @@ void tst_QImageReader::readCorruptImage_data()
QTest::newRow("corrupt bmp") << QString("corrupt.bmp") << true << QString("") << QByteArray("bmp");
QTest::newRow("corrupt bmp (clut)") << QString("corrupt_clut.bmp") << true << QString("") << QByteArray("bmp");
QTest::newRow("corrupt xpm (colors)") << QString("corrupt-colors.xpm") << true
<< QString("QImage: XPM color specification is missing: bla9an.n#x")
<< QString("XPM color specification is missing: bla9an.n#x")
<< QByteArray("xpm");
QTest::newRow("corrupt xpm (pixels)") << QString("corrupt-pixels.xpm") << true
<< QString("QImage: XPM pixels missing on image line 3")
<< QString("XPM pixels missing on image line 3")
<< QByteArray("xpm");
QTest::newRow("corrupt xbm") << QString("corrupt.xbm") << false << QString("") << QByteArray("xbm");
QTest::newRow("corrupt svg") << QString("corrupt.svg") << true << QString("") << QByteArray("svg");
Expand Down

0 comments on commit 426308a

Please sign in to comment.