diff options
Diffstat (limited to 'src/plugins/imageformats/ico')
-rw-r--r-- | src/plugins/imageformats/ico/CMakeLists.txt | 19 | ||||
-rw-r--r-- | src/plugins/imageformats/ico/ico.pro | 10 | ||||
-rw-r--r-- | src/plugins/imageformats/ico/main.cpp | 59 | ||||
-rw-r--r-- | src/plugins/imageformats/ico/main.h | 59 | ||||
-rw-r--r-- | src/plugins/imageformats/ico/qicohandler.cpp | 119 | ||||
-rw-r--r-- | src/plugins/imageformats/ico/qicohandler.h | 46 |
6 files changed, 81 insertions, 231 deletions
diff --git a/src/plugins/imageformats/ico/CMakeLists.txt b/src/plugins/imageformats/ico/CMakeLists.txt new file mode 100644 index 0000000000..c9cd0f0d40 --- /dev/null +++ b/src/plugins/imageformats/ico/CMakeLists.txt @@ -0,0 +1,19 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## QICOPlugin Plugin: +##################################################################### + +qt_internal_add_plugin(QICOPlugin + OUTPUT_NAME qico + PLUGIN_TYPE imageformats + SOURCES + main.cpp + qicohandler.cpp qicohandler.h + LIBRARIES + Qt::Core + Qt::CorePrivate + Qt::Gui + NO_UNITY_BUILD +) diff --git a/src/plugins/imageformats/ico/ico.pro b/src/plugins/imageformats/ico/ico.pro deleted file mode 100644 index c8bb37eff2..0000000000 --- a/src/plugins/imageformats/ico/ico.pro +++ /dev/null @@ -1,10 +0,0 @@ -TARGET = qico - -HEADERS += main.h qicohandler.h -SOURCES += main.cpp qicohandler.cpp -OTHER_FILES += ico.json -QT += core-private - -PLUGIN_TYPE = imageformats -PLUGIN_CLASS_NAME = QICOPlugin -load(qt_plugin) diff --git a/src/plugins/imageformats/ico/main.cpp b/src/plugins/imageformats/ico/main.cpp index b00d8c7fd3..d5199fa18b 100644 --- a/src/plugins/imageformats/ico/main.cpp +++ b/src/plugins/imageformats/ico/main.cpp @@ -1,46 +1,25 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only -#include "main.h" +#include <qimageiohandler.h> +#include <qdebug.h> + +#ifdef QT_NO_IMAGEFORMAT_ICO +#undef QT_NO_IMAGEFORMAT_ICO +#endif +#include "qicohandler.h" QT_BEGIN_NAMESPACE +class QICOPlugin : public QImageIOPlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "ico.json") +public: + Capabilities capabilities(QIODevice *device, const QByteArray &format) const override; + QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const override; +}; + QImageIOPlugin::Capabilities QICOPlugin::capabilities(QIODevice *device, const QByteArray &format) const { if (format == "ico" || format == "cur") @@ -67,3 +46,5 @@ QImageIOHandler *QICOPlugin::create(QIODevice *device, const QByteArray &format) } QT_END_NAMESPACE + +#include "main.moc" diff --git a/src/plugins/imageformats/ico/main.h b/src/plugins/imageformats/ico/main.h deleted file mode 100644 index b5875183c1..0000000000 --- a/src/plugins/imageformats/ico/main.h +++ /dev/null @@ -1,59 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <qimageiohandler.h> -#include <qdebug.h> - -#ifdef QT_NO_IMAGEFORMAT_ICO -#undef QT_NO_IMAGEFORMAT_ICO -#endif -#include "qicohandler.h" - -QT_BEGIN_NAMESPACE - -class QICOPlugin : public QImageIOPlugin -{ - Q_OBJECT - Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "ico.json") -public: - Capabilities capabilities(QIODevice *device, const QByteArray &format) const override; - QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const override; -}; - -QT_END_NAMESPACE diff --git a/src/plugins/imageformats/ico/qicohandler.cpp b/src/plugins/imageformats/ico/qicohandler.cpp index c8e31dceac..18b39766f5 100644 --- a/src/plugins/imageformats/ico/qicohandler.cpp +++ b/src/plugins/imageformats/ico/qicohandler.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only /*! \class QtIcoHandler @@ -50,12 +14,17 @@ #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") + +namespace { + // These next two structs represent how the icon information is stored // in an ICO file. typedef struct @@ -95,6 +64,8 @@ typedef struct { // BMP information header } BMP_INFOHDR ,*LPBMP_INFOHDR; #define BMP_INFOHDR_SIZE 40 +} + class ICOReader { public: @@ -103,9 +74,9 @@ public: QImage iconAt(int index); static bool canRead(QIODevice *iodev); - static QVector<QImage> read(QIODevice *device); + static QList<QImage> read(QIODevice *device); - static bool write(QIODevice *device, const QVector<QImage> &images); + static bool write(QIODevice *device, const QList<QImage> &images); bool readIconEntry(int index, ICONDIRENTRY * iconEntry); @@ -205,9 +176,7 @@ bool ICOReader::canRead(QIODevice *iodev) ICONDIR ikonDir; if (readIconDir(iodev, &ikonDir)) { - qint64 readBytes = ICONDIR_SIZE; if (readIconDirEntry(iodev, &ikonDir.idEntries[0])) { - readBytes += ICONDIRENTRY_SIZE; // ICO format does not have a magic identifier, so we read 6 different values, which will hopefully be enough to identify the file. if ( ikonDir.idReserved == 0 && (ikonDir.idType == 1 || ikonDir.idType == 2) @@ -356,7 +325,7 @@ void ICOReader::read1BitBMP(QImage & image) if (iod) { int h = image.height(); - int bpl = image.bytesPerLine(); + qsizetype bpl = image.bytesPerLine(); while (--h >= 0) { if (iod->read((char*)image.scanLine(h),bpl) != bpl) { @@ -405,7 +374,7 @@ void ICOReader::read8BitBMP(QImage & image) if (iod) { int h = icoAttrib.h; - int bpl = image.bytesPerLine(); + qsizetype bpl = image.bytesPerLine(); while (--h >= 0) { if (iod->read((char *)image.scanLine(h), bpl) != bpl) { @@ -425,7 +394,7 @@ void ICOReader::read16_24_32BMP(QImage & image) QRgb *p; QRgb *end; uchar *buf = new uchar[image.bytesPerLine()]; - int bpl = ((icoAttrib.w*icoAttrib.nbits+31)/32)*4; + qsizetype bpl = ((qsizetype(icoAttrib.w)*icoAttrib.nbits+31)/32)*4; uchar *b; while (--h >= 0) { @@ -465,7 +434,9 @@ QImage ICOReader::iconAt(int index) static const uchar pngMagicData[] = { 137, 80, 78, 71, 13, 10, 26, 10 }; - iod->seek(iconEntry.dwImageOffset); + if (!iod->seek(iconEntry.dwImageOffset) + || iconEntry.dwBytesInRes > iod->bytesAvailable()) + return img; const QByteArray pngMagic = QByteArray::fromRawData((const char*)pngMagicData, sizeof(pngMagicData)); const bool isPngImage = (iod->read(pngMagic.size()) == pngMagic); @@ -491,8 +462,12 @@ QImage ICOReader::iconAt(int index) case 4: icoAttrib.depth = 8; break; - default: + case 1: icoAttrib.depth = 1; + break; + default: + return img; + break; } if (icoAttrib.depth == 32) // there's no colormap icoAttrib.ncolors = 0; @@ -517,13 +492,14 @@ QImage ICOReader::iconAt(int index) else if (icoAttrib.ncolors > 0) format = QImage::Format_Indexed8; - QImage image(icoAttrib.w, icoAttrib.h, format); - if (!image.isNull()) { + QImage image; + const QSize size(icoAttrib.w, icoAttrib.h); + if (QImageIOHandler::allocateImage(size, format, &image)) { findColorInfo(image); if (!image.isNull()) { readBMP(image); if (!image.isNull()) { - if (icoAttrib.depth == 32) { + if (icoAttrib.nbits == 32) { img = std::move(image).convertToFormat(QImage::Format_ARGB32_Premultiplied); } else { QImage mask(image.width(), image.height(), QImage::Format_Mono); @@ -535,8 +511,6 @@ QImage ICOReader::iconAt(int index) if (!mask.isNull()) { img = image; img.setAlphaChannel(mask); - // (Luckily, it seems that setAlphaChannel() does not ruin the alpha values - // of partially transparent pixels in those icons that have that) } } } @@ -561,9 +535,9 @@ QImage ICOReader::iconAt(int index) \sa write() */ -QVector<QImage> ICOReader::read(QIODevice *device) +QList<QImage> ICOReader::read(QIODevice *device) { - QVector<QImage> images; + QList<QImage> images; ICOReader reader(device); const int N = reader.count(); @@ -587,18 +561,18 @@ QVector<QImage> ICOReader::read(QIODevice *device) \sa read() */ -bool ICOReader::write(QIODevice *device, const QVector<QImage> &images) +bool ICOReader::write(QIODevice *device, const QList<QImage> &images) { bool retValue = false; - if (images.count()) { + if (images.size()) { qint64 origOffset = device->pos(); ICONDIR id; id.idReserved = 0; id.idType = 1; - id.idCount = images.count(); + id.idCount = images.size(); ICONDIRENTRY * entries = new ICONDIRENTRY[id.idCount]; BMP_INFOHDR * bmpHeaders = new BMP_INFOHDR[id.idCount]; @@ -615,13 +589,7 @@ bool ICOReader::write(QIODevice *device, const QVector<QImage> &images) } QImage maskImage(image.width(), image.height(), QImage::Format_Mono); image = image.convertToFormat(QImage::Format_ARGB32); - - if (image.hasAlphaChannel()) { - maskImage = image.createAlphaMask(); - } else { - maskImage.fill(0xff); - } - maskImage = maskImage.convertToFormat(QImage::Format_Mono); + maskImage.fill(Qt::color1); int nbits = 32; int bpl_bmp = ((image.width()*nbits+31)/32)*4; @@ -671,7 +639,7 @@ bool ICOReader::write(QIODevice *device, const QVector<QImage> &images) *b++ = qRed(*p); *b++ = qAlpha(*p); if (qAlpha(*p) > 0) // Even mostly transparent pixels must not be masked away - maskImage.setPixel(x, y, Qt::color1); // (i.e. createAlphaMask() takes away too much) + maskImage.setPixel(x, y, 0); p++; x++; } @@ -679,7 +647,6 @@ bool ICOReader::write(QIODevice *device, const QVector<QImage> &images) } delete[] buf; - maskImage.invertPixels(); // seems as though it needs this // NOTE! !! The mask is only flipped vertically - not horizontally !! for (y = maskImage.height() - 1; y >= 0; y--) buffer.write((char*)maskImage.scanLine(y), maskImage.bytesPerLine()); @@ -768,6 +735,8 @@ bool QtIcoHandler::supportsOption(ImageOption option) const */ bool QtIcoHandler::canRead() const { + if (knownCanRead) + return true; bool bCanRead = false; QIODevice *device = QImageIOHandler::device(); if (device) { @@ -775,8 +744,9 @@ bool QtIcoHandler::canRead() const if (bCanRead) setFormat("ico"); } else { - qWarning("QtIcoHandler::canRead() called with no device"); + qCWarning(lcIco, "QtIcoHandler::canRead() called with no device"); } + knownCanRead = bCanRead; return bCanRead; } @@ -813,22 +783,11 @@ bool QtIcoHandler::read(QImage *image) bool QtIcoHandler::write(const QImage &image) { QIODevice *device = QImageIOHandler::device(); - QVector<QImage> imgs; + QList<QImage> imgs; imgs.append(image); return ICOReader::write(device, imgs); } -#if QT_DEPRECATED_SINCE(5, 13) -/*! - * Return the common identifier of the format. - * For ICO format this will return "ico". - */ -QByteArray QtIcoHandler::name() const -{ - return "ico"; -} -#endif - /*! \reimp */ diff --git a/src/plugins/imageformats/ico/qicohandler.h b/src/plugins/imageformats/ico/qicohandler.h index 328dfce47e..61c3eea465 100644 --- a/src/plugins/imageformats/ico/qicohandler.h +++ b/src/plugins/imageformats/ico/qicohandler.h @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QTICOHANDLER_H #define QTICOHANDLER_H @@ -54,10 +18,6 @@ public: bool read(QImage *image) override; bool write(const QImage &image) override; -#if QT_DEPRECATED_SINCE(5, 13) - QByteArray name() const override; -#endif - int imageCount() const override; bool jumpToImage(int imageNumber) override; bool jumpToNextImage() override; @@ -70,7 +30,7 @@ public: private: int m_currentIconIndex; ICOReader *m_pICOReader; - + mutable bool knownCanRead = false; }; QT_END_NAMESPACE |