| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | // Qt-Security score:critical reason:data-parser |
| 4 | |
| 5 | #include "qtgahandler.h" |
| 6 | #include "qtgafile.h" |
| 7 | |
| 8 | #include <QtCore/QVariant> |
| 9 | #include <QtCore/QDebug> |
| 10 | #include <QtGui/QImage> |
| 11 | |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | |
| 14 | QTgaHandler::QTgaHandler() |
| 15 | : QImageIOHandler() |
| 16 | , tga(0) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | QTgaHandler::~QTgaHandler() |
| 21 | { |
| 22 | delete tga; |
| 23 | } |
| 24 | |
| 25 | bool QTgaHandler::canRead() const |
| 26 | { |
| 27 | if (!tga) |
| 28 | tga = new QTgaFile(device()); |
| 29 | if (tga->isValid()) |
| 30 | { |
| 31 | setFormat("tga"); |
| 32 | return true; |
| 33 | } |
| 34 | qWarning(msg: "QTgaHandler::canRead(): %s", qPrintable(tga->errorMessage())); |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | bool QTgaHandler::canRead(QIODevice *device) |
| 39 | { |
| 40 | if (!device) { |
| 41 | qWarning(msg: "QTgaHandler::canRead() called with no device"); |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | // TGA reader implementation needs a seekable QIODevice, so |
| 46 | // sequential devices are not supported |
| 47 | if (device->isSequential()) |
| 48 | return false; |
| 49 | qint64 pos = device->pos(); |
| 50 | bool isValid; |
| 51 | { |
| 52 | QTgaFile tga(device); |
| 53 | isValid = tga.isValid(); |
| 54 | } |
| 55 | device->seek(pos); |
| 56 | return isValid; |
| 57 | } |
| 58 | |
| 59 | bool QTgaHandler::read(QImage *image) |
| 60 | { |
| 61 | if (!canRead()) |
| 62 | return false; |
| 63 | *image = tga->readImage(); |
| 64 | return !image->isNull(); |
| 65 | } |
| 66 | |
| 67 | QVariant QTgaHandler::option(ImageOption option) const |
| 68 | { |
| 69 | if (option == Size && canRead()) { |
| 70 | return tga->size(); |
| 71 | } else if (option == CompressionRatio) { |
| 72 | return tga->compression(); |
| 73 | } else if (option == ImageFormat) { |
| 74 | return QImage::Format_ARGB32; |
| 75 | } |
| 76 | return QVariant(); |
| 77 | } |
| 78 | |
| 79 | void QTgaHandler::setOption(ImageOption option, const QVariant &value) |
| 80 | { |
| 81 | Q_UNUSED(option); |
| 82 | Q_UNUSED(value); |
| 83 | } |
| 84 | |
| 85 | bool QTgaHandler::supportsOption(ImageOption option) const |
| 86 | { |
| 87 | return option == CompressionRatio |
| 88 | || option == Size |
| 89 | || option == ImageFormat; |
| 90 | } |
| 91 | |
| 92 | QT_END_NAMESPACE |
| 93 |
