mirror of
https://invent.kde.org/frameworks/kimageformats.git
synced 2025-07-14 19:04:17 -04:00
Compare commits
10 Commits
v5.111.0-r
...
v5.116.0
Author | SHA1 | Date | |
---|---|---|---|
106279d32e | |||
f5962442ca | |||
7cc4cb8d0c | |||
2bf3a859fc | |||
3ce3ab2364 | |||
c09b88b7c6 | |||
8cc25c39b4 | |||
43c80793ac | |||
e90bca4924 | |||
bfc73ca260 |
@ -2,9 +2,11 @@
|
||||
# SPDX-License-Identifier: CC0-1.0
|
||||
|
||||
include:
|
||||
- https://invent.kde.org/sysadmin/ci-utilities/raw/master/gitlab-templates/linux.yml
|
||||
- https://invent.kde.org/sysadmin/ci-utilities/raw/master/gitlab-templates/linux-static.yml
|
||||
- https://invent.kde.org/sysadmin/ci-utilities/raw/master/gitlab-templates/android.yml
|
||||
- https://invent.kde.org/sysadmin/ci-utilities/raw/master/gitlab-templates/freebsd.yml
|
||||
- https://invent.kde.org/sysadmin/ci-utilities/raw/master/gitlab-templates/windows.yml
|
||||
- https://invent.kde.org/sysadmin/ci-utilities/raw/master/gitlab-templates/windows-static.yml
|
||||
- project: sysadmin/ci-utilities
|
||||
file:
|
||||
- /gitlab-templates/linux.yml
|
||||
- /gitlab-templates/linux-static.yml
|
||||
- /gitlab-templates/android.yml
|
||||
- /gitlab-templates/freebsd.yml
|
||||
- /gitlab-templates/windows.yml
|
||||
- /gitlab-templates/windows-static.yml
|
||||
|
@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.16)
|
||||
project(KImageFormats)
|
||||
|
||||
include(FeatureSummary)
|
||||
find_package(ECM 5.111.0 NO_MODULE)
|
||||
find_package(ECM 5.116.0 NO_MODULE)
|
||||
set_package_properties(ECM PROPERTIES TYPE REQUIRED DESCRIPTION "Extra CMake Modules." URL "https://commits.kde.org/extra-cmake-modules")
|
||||
feature_summary(WHAT REQUIRED_PACKAGES_NOT_FOUND FATAL_ON_MISSING_REQUIRED_PACKAGES)
|
||||
|
||||
|
@ -99,6 +99,12 @@ if (LibHeif_FOUND)
|
||||
kimageformats_write_tests(FUZZ 1
|
||||
heif-nodatacheck-lossless
|
||||
)
|
||||
|
||||
if (LibHeif_VERSION VERSION_GREATER_EQUAL "1.17.0")
|
||||
kimageformats_read_tests(FUZZ 1
|
||||
hej2
|
||||
)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if (LibJXL_FOUND AND LibJXLThreads_FOUND)
|
||||
|
BIN
autotests/read/hej2/rgb_kcolorchooser.hej2
Normal file
BIN
autotests/read/hej2/rgb_kcolorchooser.hej2
Normal file
Binary file not shown.
BIN
autotests/read/hej2/rgb_kcolorchooser.png
Normal file
BIN
autotests/read/hej2/rgb_kcolorchooser.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 27 KiB |
BIN
autotests/read/hej2/rgba_kolourpaint.hej2
Normal file
BIN
autotests/read/hej2/rgba_kolourpaint.hej2
Normal file
Binary file not shown.
BIN
autotests/read/hej2/rgba_kolourpaint.png
Normal file
BIN
autotests/read/hej2/rgba_kolourpaint.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
@ -16,9 +16,34 @@
|
||||
|
||||
#include <cfloat>
|
||||
|
||||
/*
|
||||
Quality range - compression/subsampling
|
||||
100 - lossless RGB compression
|
||||
< KIMG_AVIF_QUALITY_BEST, 100 ) - YUV444 color subsampling
|
||||
< KIMG_AVIF_QUALITY_HIGH, KIMG_AVIF_QUALITY_BEST ) - YUV422 color subsampling
|
||||
< 0, KIMG_AVIF_QUALITY_HIGH ) - YUV420 color subsampling
|
||||
< 0, KIMG_AVIF_QUALITY_LOW ) - lossy compression of alpha channel
|
||||
*/
|
||||
|
||||
#ifndef KIMG_AVIF_DEFAULT_QUALITY
|
||||
#define KIMG_AVIF_DEFAULT_QUALITY 68
|
||||
#endif
|
||||
|
||||
#ifndef KIMG_AVIF_QUALITY_BEST
|
||||
#define KIMG_AVIF_QUALITY_BEST 90
|
||||
#endif
|
||||
|
||||
#ifndef KIMG_AVIF_QUALITY_HIGH
|
||||
#define KIMG_AVIF_QUALITY_HIGH 80
|
||||
#endif
|
||||
|
||||
#ifndef KIMG_AVIF_QUALITY_LOW
|
||||
#define KIMG_AVIF_QUALITY_LOW 51
|
||||
#endif
|
||||
|
||||
QAVIFHandler::QAVIFHandler()
|
||||
: m_parseState(ParseAvifNotParsed)
|
||||
, m_quality(52)
|
||||
, m_quality(KIMG_AVIF_DEFAULT_QUALITY)
|
||||
, m_container_width(0)
|
||||
, m_container_height(0)
|
||||
, m_rawAvifData(AVIF_DATA_EMPTY)
|
||||
@ -519,9 +544,17 @@ bool QAVIFHandler::write(const QImage &image)
|
||||
}
|
||||
}
|
||||
|
||||
if (m_quality > 100) {
|
||||
m_quality = 100;
|
||||
} else if (m_quality < 0) {
|
||||
m_quality = KIMG_AVIF_DEFAULT_QUALITY;
|
||||
}
|
||||
|
||||
#if AVIF_VERSION < 1000000
|
||||
int maxQuantizer = AVIF_QUANTIZER_WORST_QUALITY * (100 - qBound(0, m_quality, 100)) / 100;
|
||||
int minQuantizer = 0;
|
||||
int maxQuantizerAlpha = 0;
|
||||
#endif
|
||||
avifResult res;
|
||||
|
||||
bool save_grayscale; // true - monochrome, false - colors
|
||||
@ -567,13 +600,15 @@ bool QAVIFHandler::write(const QImage &image)
|
||||
break;
|
||||
}
|
||||
|
||||
// quality settings
|
||||
#if AVIF_VERSION < 1000000
|
||||
// deprecated quality settings
|
||||
if (maxQuantizer > 20) {
|
||||
minQuantizer = maxQuantizer - 20;
|
||||
if (maxQuantizer > 40) { // we decrease quality of alpha channel here
|
||||
maxQuantizerAlpha = maxQuantizer - 40;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (save_grayscale && !image.hasAlphaChannel()) { // we are going to save grayscale image without alpha channel
|
||||
if (save_depth > 8) {
|
||||
@ -646,8 +681,8 @@ bool QAVIFHandler::write(const QImage &image)
|
||||
QImage tmpcolorimage = image.convertToFormat(tmpformat);
|
||||
|
||||
avifPixelFormat pixel_format = AVIF_PIXEL_FORMAT_YUV420;
|
||||
if (maxQuantizer < 20) {
|
||||
if (maxQuantizer < 10) {
|
||||
if (m_quality >= KIMG_AVIF_QUALITY_HIGH) {
|
||||
if (m_quality >= KIMG_AVIF_QUALITY_BEST) {
|
||||
pixel_format = AVIF_PIXEL_FORMAT_YUV444; // best quality
|
||||
} else {
|
||||
pixel_format = AVIF_PIXEL_FORMAT_YUV422; // high quality
|
||||
@ -807,6 +842,8 @@ bool QAVIFHandler::write(const QImage &image)
|
||||
avifRWData raw = AVIF_DATA_EMPTY;
|
||||
avifEncoder *encoder = avifEncoderCreate();
|
||||
encoder->maxThreads = qBound(1, QThread::idealThreadCount(), 64);
|
||||
|
||||
#if AVIF_VERSION < 1000000
|
||||
encoder->minQuantizer = minQuantizer;
|
||||
encoder->maxQuantizer = maxQuantizer;
|
||||
|
||||
@ -814,6 +851,17 @@ bool QAVIFHandler::write(const QImage &image)
|
||||
encoder->minQuantizerAlpha = AVIF_QUANTIZER_LOSSLESS;
|
||||
encoder->maxQuantizerAlpha = maxQuantizerAlpha;
|
||||
}
|
||||
#else
|
||||
encoder->quality = m_quality;
|
||||
|
||||
if (image.hasAlphaChannel()) {
|
||||
if (m_quality >= KIMG_AVIF_QUALITY_LOW) {
|
||||
encoder->qualityAlpha = 100;
|
||||
} else {
|
||||
encoder->qualityAlpha = 100 - (KIMG_AVIF_QUALITY_LOW - m_quality) / 2;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
encoder->speed = 6;
|
||||
|
||||
@ -870,7 +918,7 @@ void QAVIFHandler::setOption(ImageOption option, const QVariant &value)
|
||||
if (m_quality > 100) {
|
||||
m_quality = 100;
|
||||
} else if (m_quality < 0) {
|
||||
m_quality = 52;
|
||||
m_quality = KIMG_AVIF_DEFAULT_QUALITY;
|
||||
}
|
||||
return;
|
||||
default:
|
||||
|
@ -22,6 +22,7 @@ size_t HEIFHandler::m_initialized_count = 0;
|
||||
bool HEIFHandler::m_plugins_queried = false;
|
||||
bool HEIFHandler::m_heif_decoder_available = false;
|
||||
bool HEIFHandler::m_heif_encoder_available = false;
|
||||
bool HEIFHandler::m_hej2_decoder_available = false;
|
||||
|
||||
extern "C" {
|
||||
static struct heif_error heifhandler_write_callback(struct heif_context * /* ctx */, const void *data, size_t size, void *userdata)
|
||||
@ -59,12 +60,25 @@ HEIFHandler::HEIFHandler()
|
||||
|
||||
bool HEIFHandler::canRead() const
|
||||
{
|
||||
if (m_parseState == ParseHeicNotParsed && !canRead(device())) {
|
||||
if (m_parseState == ParseHeicNotParsed) {
|
||||
QIODevice *dev = device();
|
||||
if (dev) {
|
||||
const QByteArray header = dev->peek(28);
|
||||
|
||||
if (HEIFHandler::isSupportedBMFFType(header)) {
|
||||
setFormat("heif");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (HEIFHandler::isSupportedHEJ2(header)) {
|
||||
setFormat("hej2");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_parseState != ParseHeicError) {
|
||||
setFormat("heif");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -300,17 +314,6 @@ bool HEIFHandler::write_helper(const QImage &image)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool HEIFHandler::canRead(QIODevice *device)
|
||||
{
|
||||
if (!device) {
|
||||
qWarning("HEIFHandler::canRead() called with no device");
|
||||
return false;
|
||||
}
|
||||
|
||||
const QByteArray header = device->peek(28);
|
||||
return HEIFHandler::isSupportedBMFFType(header);
|
||||
}
|
||||
|
||||
bool HEIFHandler::isSupportedBMFFType(const QByteArray &header)
|
||||
{
|
||||
if (header.size() < 28) {
|
||||
@ -350,6 +353,22 @@ bool HEIFHandler::isSupportedBMFFType(const QByteArray &header)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool HEIFHandler::isSupportedHEJ2(const QByteArray &header)
|
||||
{
|
||||
if (header.size() < 28) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *buffer = header.constData();
|
||||
if (qstrncmp(buffer + 4, "ftyp", 4) == 0) {
|
||||
if (qstrncmp(buffer + 8, "j2ki", 4) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
QVariant HEIFHandler::option(ImageOption option) const
|
||||
{
|
||||
if (option == Quality) {
|
||||
@ -425,7 +444,7 @@ bool HEIFHandler::ensureDecoder()
|
||||
}
|
||||
|
||||
const QByteArray buffer = device()->readAll();
|
||||
if (!HEIFHandler::isSupportedBMFFType(buffer)) {
|
||||
if (!HEIFHandler::isSupportedBMFFType(buffer) && !HEIFHandler::isSupportedHEJ2(buffer)) {
|
||||
m_parseState = ParseHeicError;
|
||||
return false;
|
||||
}
|
||||
@ -814,6 +833,9 @@ bool HEIFHandler::isHeifDecoderAvailable()
|
||||
}
|
||||
#endif
|
||||
|
||||
#if LIBHEIF_HAVE_VERSION(1, 13, 0)
|
||||
m_hej2_decoder_available = heif_have_decoder_for_format(heif_compression_JPEG2000);
|
||||
#endif
|
||||
m_heif_encoder_available = heif_have_encoder_for_format(heif_compression_HEVC);
|
||||
m_heif_decoder_available = heif_have_decoder_for_format(heif_compression_HEVC);
|
||||
m_plugins_queried = true;
|
||||
@ -839,6 +861,9 @@ bool HEIFHandler::isHeifEncoderAvailable()
|
||||
}
|
||||
#endif
|
||||
|
||||
#if LIBHEIF_HAVE_VERSION(1, 13, 0)
|
||||
m_hej2_decoder_available = heif_have_decoder_for_format(heif_compression_JPEG2000);
|
||||
#endif
|
||||
m_heif_decoder_available = heif_have_decoder_for_format(heif_compression_HEVC);
|
||||
m_heif_encoder_available = heif_have_encoder_for_format(heif_compression_HEVC);
|
||||
m_plugins_queried = true;
|
||||
@ -853,6 +878,34 @@ bool HEIFHandler::isHeifEncoderAvailable()
|
||||
return m_heif_encoder_available;
|
||||
}
|
||||
|
||||
bool HEIFHandler::isHej2DecoderAvailable()
|
||||
{
|
||||
QMutexLocker locker(&getHEIFHandlerMutex());
|
||||
|
||||
if (!m_plugins_queried) {
|
||||
#if LIBHEIF_HAVE_VERSION(1, 13, 0)
|
||||
if (m_initialized_count == 0) {
|
||||
heif_init(nullptr);
|
||||
}
|
||||
#endif
|
||||
|
||||
m_heif_encoder_available = heif_have_encoder_for_format(heif_compression_HEVC);
|
||||
m_heif_decoder_available = heif_have_decoder_for_format(heif_compression_HEVC);
|
||||
#if LIBHEIF_HAVE_VERSION(1, 13, 0)
|
||||
m_hej2_decoder_available = heif_have_decoder_for_format(heif_compression_JPEG2000);
|
||||
#endif
|
||||
m_plugins_queried = true;
|
||||
|
||||
#if LIBHEIF_HAVE_VERSION(1, 13, 0)
|
||||
if (m_initialized_count == 0) {
|
||||
heif_deinit();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return m_hej2_decoder_available;
|
||||
}
|
||||
|
||||
void HEIFHandler::startHeifLib()
|
||||
{
|
||||
#if LIBHEIF_HAVE_VERSION(1, 13, 0)
|
||||
@ -901,6 +954,15 @@ QImageIOPlugin::Capabilities HEIFPlugin::capabilities(QIODevice *device, const Q
|
||||
}
|
||||
return format_cap;
|
||||
}
|
||||
|
||||
if (format == "hej2") {
|
||||
Capabilities format_cap;
|
||||
if (HEIFHandler::isHej2DecoderAvailable()) {
|
||||
format_cap |= CanRead;
|
||||
}
|
||||
return format_cap;
|
||||
}
|
||||
|
||||
if (!format.isEmpty()) {
|
||||
return {};
|
||||
}
|
||||
@ -909,8 +971,16 @@ QImageIOPlugin::Capabilities HEIFPlugin::capabilities(QIODevice *device, const Q
|
||||
}
|
||||
|
||||
Capabilities cap;
|
||||
if (device->isReadable() && HEIFHandler::canRead(device) && HEIFHandler::isHeifDecoderAvailable()) {
|
||||
cap |= CanRead;
|
||||
if (device->isReadable()) {
|
||||
const QByteArray header = device->peek(28);
|
||||
|
||||
if (HEIFHandler::isSupportedBMFFType(header) && HEIFHandler::isHeifDecoderAvailable()) {
|
||||
cap |= CanRead;
|
||||
}
|
||||
|
||||
if (HEIFHandler::isSupportedHEJ2(header) && HEIFHandler::isHej2DecoderAvailable()) {
|
||||
cap |= CanRead;
|
||||
}
|
||||
}
|
||||
|
||||
if (device->isWritable() && HEIFHandler::isHeifEncoderAvailable()) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
{
|
||||
"Keys": [ "heif", "heic" ],
|
||||
"MimeTypes": [ "image/heif", "image/heif" ]
|
||||
"Keys": [ "heif", "heic", "hej2" ],
|
||||
"MimeTypes": [ "image/heif", "image/heif", "image/hej2k" ]
|
||||
}
|
||||
|
@ -24,17 +24,18 @@ public:
|
||||
bool read(QImage *image) override;
|
||||
bool write(const QImage &image) override;
|
||||
|
||||
static bool canRead(QIODevice *device);
|
||||
|
||||
QVariant option(ImageOption option) const override;
|
||||
void setOption(ImageOption option, const QVariant &value) override;
|
||||
bool supportsOption(ImageOption option) const override;
|
||||
|
||||
static bool isHeifDecoderAvailable();
|
||||
static bool isHeifEncoderAvailable();
|
||||
static bool isHej2DecoderAvailable();
|
||||
|
||||
static bool isSupportedBMFFType(const QByteArray &header);
|
||||
static bool isSupportedHEJ2(const QByteArray &header);
|
||||
|
||||
private:
|
||||
static bool isSupportedBMFFType(const QByteArray &header);
|
||||
bool ensureParsed() const;
|
||||
bool ensureDecoder();
|
||||
|
||||
@ -57,6 +58,7 @@ private:
|
||||
static bool m_plugins_queried;
|
||||
static bool m_heif_decoder_available;
|
||||
static bool m_heif_encoder_available;
|
||||
static bool m_hej2_decoder_available;
|
||||
|
||||
static QMutex &getHEIFHandlerMutex();
|
||||
};
|
||||
|
@ -88,10 +88,6 @@ static QDataStream &operator>>(QDataStream &s, TgaHeader &head)
|
||||
s >> head.height;
|
||||
s >> head.pixel_size;
|
||||
s >> head.flags;
|
||||
/*qDebug() << "id_length: " << head.id_length << " - colormap_type: " << head.colormap_type << " - image_type: " << head.image_type;
|
||||
qDebug() << "colormap_index: " << head.colormap_index << " - colormap_length: " << head.colormap_length << " - colormap_size: " << head.colormap_size;
|
||||
qDebug() << "x_origin: " << head.x_origin << " - y_origin: " << head.y_origin << " - width:" << head.width << " - height:" << head.height << " - pixelsize:
|
||||
" << head.pixel_size << " - flags: " << head.flags;*/
|
||||
return s;
|
||||
}
|
||||
|
||||
@ -117,6 +113,10 @@ static bool IsSupported(const TgaHeader &head)
|
||||
if (head.pixel_size != 8 && head.pixel_size != 16 && head.pixel_size != 24 && head.pixel_size != 32) {
|
||||
return false;
|
||||
}
|
||||
// If the colormap_type field is set to zero, indicating that no color map exists, then colormap_size, colormap_index and colormap_length should be set to zero.
|
||||
if (head.colormap_type == 0 && (head.colormap_size != 0 || head.colormap_index != 0 || head.colormap_length != 0)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -170,10 +170,57 @@ struct TgaHeaderInfo {
|
||||
}
|
||||
};
|
||||
|
||||
static QImage::Format imageFormat(const TgaHeader &head)
|
||||
{
|
||||
auto format = QImage::Format_Invalid;
|
||||
if (IsSupported(head)) {
|
||||
// Bits 0-3 are the numbers of alpha bits (can be zero!)
|
||||
const int numAlphaBits = head.flags & 0xf;
|
||||
// However alpha exists only in the 32 bit format.
|
||||
if ((head.pixel_size == 32) && (head.flags & 0xf)) {
|
||||
if (numAlphaBits <= 8) {
|
||||
format = QImage::Format_ARGB32;
|
||||
}
|
||||
}
|
||||
else {
|
||||
format = QImage::Format_RGB32;
|
||||
}
|
||||
}
|
||||
return format;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief peekHeader
|
||||
* Reads the header but does not change the position in the device.
|
||||
*/
|
||||
static bool peekHeader(QIODevice *device, TgaHeader &header)
|
||||
{
|
||||
qint64 oldPos = device->pos();
|
||||
QByteArray head = device->read(TgaHeader::SIZE);
|
||||
int readBytes = head.size();
|
||||
|
||||
if (device->isSequential()) {
|
||||
for (int pos = readBytes - 1; pos >= 0; --pos) {
|
||||
device->ungetChar(head[pos]);
|
||||
}
|
||||
} else {
|
||||
device->seek(oldPos);
|
||||
}
|
||||
|
||||
if (readBytes < TgaHeader::SIZE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QDataStream stream(head);
|
||||
stream.setByteOrder(QDataStream::LittleEndian);
|
||||
stream >> header;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool LoadTGA(QDataStream &s, const TgaHeader &tga, QImage &img)
|
||||
{
|
||||
// Create image.
|
||||
img = imageAlloc(tga.width, tga.height, QImage::Format_RGB32);
|
||||
img = imageAlloc(tga.width, tga.height, imageFormat(tga));
|
||||
if (img.isNull()) {
|
||||
qWarning() << "Failed to allocate image, invalid dimensions?" << QSize(tga.width, tga.height);
|
||||
return false;
|
||||
@ -181,21 +228,7 @@ static bool LoadTGA(QDataStream &s, const TgaHeader &tga, QImage &img)
|
||||
|
||||
TgaHeaderInfo info(tga);
|
||||
|
||||
// Bits 0-3 are the numbers of alpha bits (can be zero!)
|
||||
const int numAlphaBits = tga.flags & 0xf;
|
||||
// However alpha exists only in the 32 bit format.
|
||||
if ((tga.pixel_size == 32) && (tga.flags & 0xf)) {
|
||||
img = imageAlloc(tga.width, tga.height, QImage::Format_ARGB32);
|
||||
if (img.isNull()) {
|
||||
qWarning() << "Failed to allocate image, invalid dimensions?" << QSize(tga.width, tga.height);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (numAlphaBits > 8) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
uint pixel_size = (tga.pixel_size / 8);
|
||||
qint64 size = qint64(tga.width) * qint64(tga.height) * pixel_size;
|
||||
|
||||
@ -391,23 +424,25 @@ bool TGAHandler::read(QImage *outImage)
|
||||
{
|
||||
// qDebug() << "Loading TGA file!";
|
||||
|
||||
QDataStream s(device());
|
||||
s.setByteOrder(QDataStream::LittleEndian);
|
||||
|
||||
// Read image header.
|
||||
auto d = device();
|
||||
TgaHeader tga;
|
||||
s >> tga;
|
||||
s.device()->seek(TgaHeader::SIZE + tga.id_length);
|
||||
|
||||
// Check image file format.
|
||||
if (s.atEnd()) {
|
||||
if (!peekHeader(d, tga) || !IsSupported(tga)) {
|
||||
// qDebug() << "This TGA file is not valid.";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check supported file types.
|
||||
if (!IsSupported(tga)) {
|
||||
// qDebug() << "This TGA file is not supported.";
|
||||
if (d->isSequential()) {
|
||||
d->read(TgaHeader::SIZE + tga.id_length);
|
||||
} else {
|
||||
d->seek(TgaHeader::SIZE + tga.id_length);
|
||||
}
|
||||
|
||||
QDataStream s(d);
|
||||
s.setByteOrder(QDataStream::LittleEndian);
|
||||
|
||||
// Check image file format.
|
||||
if (s.atEnd()) {
|
||||
// qDebug() << "This TGA file is not valid.";
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -468,6 +503,42 @@ bool TGAHandler::write(const QImage &image)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TGAHandler::supportsOption(ImageOption option) const
|
||||
{
|
||||
if (option == QImageIOHandler::Size) {
|
||||
return true;
|
||||
}
|
||||
if (option == QImageIOHandler::ImageFormat) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
QVariant TGAHandler::option(ImageOption option) const
|
||||
{
|
||||
QVariant v;
|
||||
|
||||
if (option == QImageIOHandler::Size) {
|
||||
if (auto d = device()) {
|
||||
TgaHeader header;
|
||||
if (peekHeader(d, header) && IsSupported(header)) {
|
||||
v = QVariant::fromValue(QSize(header.width, header.height));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (option == QImageIOHandler::ImageFormat) {
|
||||
if (auto d = device()) {
|
||||
TgaHeader header;
|
||||
if (peekHeader(d, header) && IsSupported(header)) {
|
||||
v = QVariant::fromValue(imageFormat(header));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
bool TGAHandler::canRead(QIODevice *device)
|
||||
{
|
||||
if (!device) {
|
||||
@ -491,10 +562,12 @@ bool TGAHandler::canRead(QIODevice *device)
|
||||
return false;
|
||||
}
|
||||
|
||||
QDataStream stream(head);
|
||||
stream.setByteOrder(QDataStream::LittleEndian);
|
||||
TgaHeader tga;
|
||||
stream >> tga;
|
||||
if (!peekHeader(device, tga)) {
|
||||
qWarning("TGAHandler::canRead() error while reading the header");
|
||||
return false;
|
||||
}
|
||||
|
||||
return IsSupported(tga);
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,9 @@ public:
|
||||
bool read(QImage *image) override;
|
||||
bool write(const QImage &image) override;
|
||||
|
||||
bool supportsOption(QImageIOHandler::ImageOption option) const override;
|
||||
QVariant option(QImageIOHandler::ImageOption option) const override;
|
||||
|
||||
static bool canRead(QIODevice *device);
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user