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