Compare commits

...

8 Commits

Author SHA1 Message Date
e2aaf89ec5 Update version to 6.7.0 2024-09-06 14:21:04 +02:00
989a5c70d6 Update version to 6.6.0 2024-09-06 13:28:36 +02:00
8588c053b6 XCF: fix crash 2024-08-27 21:52:16 +00:00
145dedf360 README update
Added some information about plugins and their behaviors (when they do particular things).
2024-08-26 15:18:57 +00:00
2405a09e36 RGB: added options support
- Added support for `Size` and `Format` options and slightly improved format detection from canRead().
- Removed conversion to ARGB32 on load (improved performace with RGBA images).
- Added result checks on writing.

With this MR, all plugins have minimal support for options.
2024-08-25 21:00:08 +00:00
d02dcb064b PCX: added options support
- Added support for ```Size``` and ```Format``` options and slightly improved format detection from canRead().
- Added PCXHEADER::isValid() method to consolidate header consistency checks in one place.
2024-08-17 06:40:29 +00:00
0590c6b49d Update version to 6.6.0 2024-08-09 12:58:58 +02:00
eb46f0f421 Fix crash on malformed files
Co-authored-by: Mirco Miranda <mircomir@gmail.com>
2024-08-05 22:15:06 +00:00
9 changed files with 557 additions and 171 deletions

View File

@ -1,11 +1,11 @@
cmake_minimum_required(VERSION 3.16)
set(KF_VERSION "6.5.0") # handled by release scripts
set(KF_DEP_VERSION "6.5.0") # handled by release scripts
set(KF_VERSION "6.7.0") # handled by release scripts
set(KF_DEP_VERSION "6.6.0") # handled by release scripts
project(KImageFormats VERSION ${KF_VERSION})
include(FeatureSummary)
find_package(ECM 6.5.0 NO_MODULE)
find_package(ECM 6.6.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)

161
README.md
View File

@ -1,6 +1,6 @@
# KImageFormats
Plugins to allow QImage to support extra file formats.
Plugins to allow `QImage` to support extra file formats.
## Introduction
@ -28,9 +28,9 @@ The following image formats have read and write support:
- AV1 Image File Format (avif)
- Encapsulated PostScript (eps)
- High Efficiency Image File Format (heif). Can be enabled with the KIMAGEFORMATS_HEIF build option.
- High Efficiency Image File Format (heif)
- JPEG XL (jxl)
- JPEG XR (jxr). Can be enabled with the KIMAGEFORMATS_JXR build option.
- JPEG XR (jxr)
- OpenEXR (exr)
- Personal Computer Exchange (pcx)
- Quite OK Image format (qoi)
@ -40,7 +40,7 @@ The following image formats have read and write support:
## Contributing
See the QImageIOPlugin documentation for information on how to write a
See the [`QImageIOPlugin`](https://doc.qt.io/qt-6/qimageioplugin.html) documentation for information on how to write a
new plugin.
The main difference between this framework and the qimageformats module
@ -68,3 +68,156 @@ This framework is licensed under the
The CMake code in this framework is licensed under the
[BSD license](http://opensource.org/licenses/BSD-3-Clause).
## Plugin status
The current implementation of a plugin may not be complete or may have limitations
of various kinds. Typically the limitations are on maximum size and color depth.
The various plugins are also limited by the formats natively supported by Qt.
For example, native support for CMYK images is only available since Qt 6.8.
### HDR images
HDR images are supported via floating point image formats from EXR, HDR, JXR,
PFM and PSD plugins.
It is important to note that in the past these plugins stripped away HDR
information, returning SDR images.
HDR images return R, G and B values outside the range 0.0 - 1.0.
While Qt painters handles HDR data correctly, some older programs may display
strange artifacts if they do not use a tone mapping operator (or at least a
clamp). This is not a plugin issue.
### Metadata
Metadata support is implemented in all formats that support it. In particular,
in addition to the classic `"Description"`, `"Author"`, `"Copyright"`, etc... where
possible, XMP data is supported via the `"XML:com.adobe.xmp"` key.
Please note that only the most common metadata is supported.
### ICC profile support
ICC support is fully implemented in all formats that support it. When saving,
some formats convert the image using color profiles according to
specifications. In particular, HDR formats almost always convert to linear
RGB.
### Maximum image size
Where possible, plugins support large images. By convention, many of the
large image plugins are limited to a maximum of 300,000 x 300,000 pixels.
Anyway, all plugins are also limited by the
`QImageIOReader::allocationLimit()`. Below are the maximum sizes for each
plugin ('n/a' means no limit, i.e. the limit depends on the format encoding).
- ANI: n/a
- AVIF: 32,768 x 32,768 pixels, in any case no larger than 256 megapixels
- EXR: 300,000 x 300,000 pixels
- HDR: n/a (large image)
- HEIF: n/a
- JXL: 65,535 x 65,535 pixels, in any case no larger than 256 megapixels
- JXR: n/a
- PCX: 65,535 x 65,535 pixels
- PFM: n/a (large image)
- PIC: 65,535 x 65,535 pixels
- PSD: 300,000 x 300,000 pixels
- PXR: 65,535 x 65,535 pixels
- QOI: 300,000 x 300,000 pixels
- RAS: n/a (large image)
- RAW: n/a (depends on the RAW format loaded)
- RGB: 65,535 x 65,535 pixels
- TGA: 65,535 x 65,535 pixels
- XCF: 300,000 x 300,000 pixels
### Sequential and random access devices
All plugins work fine on random access devices while only some work on
sequential access devices.
Some plugins, such as PSD, allow reading RGB images on sequential access
devices, but cannot do the same for Lab files.
**Important: some plugins use `QIODevice` transactions and/or
`QIODevice::ungetChar()`. Therefore, the device used to read the image must not
have any active transactions.**
### Memory usage
Qt has added many image formats over time. In older plugins, to support new
formats, `QImage` conversion functions have been used, causing memory
consumption proportional to the size of the image to be saved.
Normally this is not a source of problems because the affected plugins
are limited to maximum images of 2GiB or less.
On plugins for formats that support large images, progressive conversion has
been used or the maximum size of the image that can be saved has been limited.
### Non-RGB formats
PSD plugin loads CMYK, Lab and Multichannel images and converts them to RGB
without using the ICC profile.
JXR and PSD plugins natively support 4-channel CMYK images when compiled
with Qt 6.8+.
### The HEIF plugin
**This plugin is disabled by default. It can be enabled with the
`KIMAGEFORMATS_HEIF` build option in the cmake file.**
### The EXR plugin
The following defines can be defined in cmake to modify the behavior of the plugin:
- `EXR_CONVERT_TO_SRGB`: the linear data is converted to sRGB on read to accommodate programs that do not support color profiles.
- `EXR_DISABLE_XMP_ATTRIBUTE`: disables the stores XMP values in a non-standard attribute named "xmp". Note that Gimp reads the "xmp" attribute and Darktable writes it as well.
### The HDR plugin
The following defines can be defined in cmake to modify the behavior of the plugin:
- `HDR_HALF_QUALITY`: on read, a 16-bit float image is returned instead of a 32-bit float one.
### The JXL plugin
**The current version of the plugin limits the image size to 256 megapixels
according to feature level 5 of the JXL stream encoding.**
### The JXR plugin
**This plugin is disabled by default. It can be enabled with the
`KIMAGEFORMATS_JXR` build option in the cmake file.**
The following defines can be defined in cmake to modify the behavior of the plugin:
- `JXR_DENY_FLOAT_IMAGE`: disables the use of float images and consequently any HDR data will be lost.
- `JXR_DISABLE_DEPTH_CONVERSION`: remove the neeeds of additional memory by disabling the conversion between different color depths (e.g. RGBA64bpp to RGBA32bpp) at the cost of reduced compatibility.
- `JXR_DISABLE_BGRA_HACK`: Windows displays and opens JXR files correctly out of the box. Unfortunately it doesn't seem to open (P)RGBA @32bpp files as it only wants (P)BGRA32bpp files (a format not supported by Qt). Only for this format an hack is activated to guarantee total compatibility of the plugin with Windows.
- `JXR_ENABLE_ADVANCED_METADATA`: enable metadata support (e.g. XMP). Some distributions use an incomplete JXR library that does not allow reading metadata, causing compilation errors.
### The PSD plugin
PSD support has the following limitations:
- Only images saved by Photoshop using compatibility mode enabled (Photoshop default) can be decoded.
- Multichannel images are treated as CMY/CMYK and are only loaded if they have 3 or more channels.
- Duotone images are treated as grayscale images.
- Extra channels other than alpha are discarded.
The following defines can be defined in cmake to modify the behavior of the plugin:
- `PSD_FAST_LAB_CONVERSION`: the LAB image is converted to linear sRGB instead of sRGB which significantly increases performance.
- `PSD_NATIVE_CMYK_SUPPORT_DISABLED`: disable native support for CMYK images when compiled with Qt 6.8+
### The RAW plugin
Loading RAW images always requires a conversion. To allow the user to
choose how to convert the image, it was chosen to use the quality parameter
to act on the converter. The quality parameter can be used with values from
0 to 100 (0 = fast, 100 = maximum quality) or by setting flags to
selectively change the conversion (see also [raw_p.h](./src/imageformats/raw_p.h)).
The default setting tries to balance quality and conversion speed.
### The XCF plugin
XCF support has the following limitations:
- XCF format up to [version 12](https://testing.developer.gimp.org/core/standards/xcf/#version-history) (no support for GIMP 3).
- The returned image is always 8-bit.
- Cannot read zlib compressed files.
- The rendered image may be slightly different (colors/transparencies) than in GIMP.

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

View File

@ -67,6 +67,40 @@ public:
{
return (Encoding == 1);
}
/*!
* \brief isValid
* Checks if the header data are valid for the PCX.
* \note Put here the header sanity checks.
* \return True if the header is a valid PCX header, otherwise false.
*/
inline bool isValid() const
{
return Manufacturer == 10 && BytesPerLine != 0;
}
/*!
* \brief isSupported
* \return True if the header is valid and the PCX format is supported by the plugin. Otherwise false.
*/
inline bool isSupported() const
{
return isValid() && format() != QImage::Format_Invalid;
}
inline QImage::Format format() const
{
auto fmt = QImage::Format_Invalid;
if (Bpp == 1 && NPlanes == 1) {
fmt = QImage::Format_Mono;
} else if (Bpp == 1 && NPlanes == 4) {
fmt = QImage::Format_Indexed8;
} else if (Bpp == 4 && NPlanes == 1) {
fmt = QImage::Format_Indexed8;
} else if (Bpp == 8 && NPlanes == 1) {
fmt = QImage::Format_Indexed8;
} else if (Bpp == 8 && NPlanes == 3) {
fmt = QImage::Format_RGB32;
}
return fmt;
}
quint8 Manufacturer; // Constant Flag, 10 = ZSoft .pcx
quint8 Version; // Version information·
@ -100,6 +134,8 @@ public:
// found only in PB IV/IV Plus
quint16 VScreenSize; // Vertical screen size in pixels. New field
// found only in PB IV/IV Plus
quint8 unused[54];
};
#pragma pack(pop)
@ -173,9 +209,8 @@ static QDataStream &operator>>(QDataStream &s, PCXHEADER &ph)
ph.VScreenSize = vscreensize;
// Skip the rest of the header
quint8 byte;
for (auto i = 0; i < 54; ++i) {
s >> byte;
for (size_t i = 0, n = sizeof(ph.unused); i < n; ++i) {
s >> ph.unused[i];
}
return s;
@ -213,9 +248,8 @@ static QDataStream &operator<<(QDataStream &s, const PCXHEADER &ph)
s << ph.HScreenSize;
s << ph.VScreenSize;
quint8 byte = 0;
for (int i = 0; i < 54; ++i) {
s << byte;
for (size_t i = 0, n = sizeof(ph.unused); i < n; ++i) {
s << ph.unused[i];
}
return s;
@ -230,6 +264,34 @@ PCXHEADER::PCXHEADER()
s >> *this;
}
bool peekHeader(QIODevice *d, PCXHEADER& h)
{
qint64 pos = 0;
if (!d->isSequential()) {
pos = d->pos();
}
auto ok = false;
{ // datastream is destroyed before working on device
QDataStream ds(d);
ds.setByteOrder(QDataStream::LittleEndian);
ds >> h;
ok = ds.status() == QDataStream::Ok && h.isValid();
}
if (!d->isSequential()) {
return d->seek(pos) && ok;
}
// sequential device undo
auto head = reinterpret_cast<char*>(&h);
auto readBytes = sizeof(h);
while (readBytes > 0) {
d->ungetChar(head[readBytes-- - 1]);
}
return ok;
}
static bool readLine(QDataStream &s, QByteArray &buf, const PCXHEADER &header)
{
quint32 i = 0;
@ -265,7 +327,7 @@ static bool readImage1(QImage &img, QDataStream &s, const PCXHEADER &header)
{
QByteArray buf(header.BytesPerLine, 0);
img = imageAlloc(header.width(), header.height(), QImage::Format_Mono);
img = imageAlloc(header.width(), header.height(), header.format());
img.setColorCount(2);
if (img.isNull()) {
@ -301,14 +363,14 @@ static bool readImage4(QImage &img, QDataStream &s, const PCXHEADER &header)
QByteArray buf(header.BytesPerLine * 4, 0);
QByteArray pixbuf(header.width(), 0);
img = imageAlloc(header.width(), header.height(), QImage::Format_Indexed8);
img = imageAlloc(header.width(), header.height(), header.format());
img.setColorCount(16);
if (img.isNull()) {
qWarning() << "Failed to allocate image, invalid dimensions?" << QSize(header.width(), header.height());
return false;
}
if (header.BytesPerLine < (header.width() / 8)) {
if (header.BytesPerLine < (header.width() + 7) / 8) {
qWarning() << "PCX image has invalid BytesPerLine value";
return false;
}
@ -353,7 +415,7 @@ static bool readImage4v2(QImage &img, QDataStream &s, const PCXHEADER &header)
{
QByteArray buf(header.BytesPerLine, 0);
img = imageAlloc(header.width(), header.height(), QImage::Format_Indexed8);
img = imageAlloc(header.width(), header.height(), header.format());
img.setColorCount(16);
if (img.isNull()) {
@ -394,7 +456,7 @@ static bool readImage8(QImage &img, QDataStream &s, const PCXHEADER &header)
{
QByteArray buf(header.BytesPerLine, 0);
img = imageAlloc(header.width(), header.height(), QImage::Format_Indexed8);
img = imageAlloc(header.width(), header.height(), header.format());
img.setColorCount(256);
if (img.isNull()) {
@ -457,7 +519,7 @@ static bool readImage24(QImage &img, QDataStream &s, const PCXHEADER &header)
QByteArray g_buf(header.BytesPerLine, 0);
QByteArray b_buf(header.BytesPerLine, 0);
img = imageAlloc(header.width(), header.height(), QImage::Format_RGB32);
img = imageAlloc(header.width(), header.height(), header.format());
if (img.isNull()) {
qWarning() << "Failed to allocate image, invalid dimensions?" << QSize(header.width(), header.height());
@ -685,7 +747,18 @@ static bool writeImage24(QImage &img, QDataStream &s, PCXHEADER &header)
return true;
}
class PCXHandlerPrivate
{
public:
PCXHandlerPrivate() {}
~PCXHandlerPrivate() {}
PCXHEADER m_header;
};
PCXHandler::PCXHandler()
: QImageIOHandler()
, d(new PCXHandlerPrivate)
{
}
@ -707,11 +780,14 @@ bool PCXHandler::read(QImage *outImage)
return false;
}
PCXHEADER header;
auto&& header = d->m_header;
s >> header;
if (header.Manufacturer != 10 || header.BytesPerLine == 0 || s.atEnd()) {
if (s.status() != QDataStream::Ok || s.atEnd()) {
return false;
}
if (!header.isSupported()) {
return false;
}
@ -781,6 +857,46 @@ bool PCXHandler::write(const QImage &image)
return ok;
}
bool PCXHandler::supportsOption(ImageOption option) const
{
if (option == QImageIOHandler::Size) {
return true;
}
if (option == QImageIOHandler::ImageFormat) {
return true;
}
return false;
}
QVariant PCXHandler::option(ImageOption option) const
{
QVariant v;
if (option == QImageIOHandler::Size) {
auto&& header = d->m_header;
if (header.isSupported()) {
v = QVariant::fromValue(QSize(header.width(), header.height()));
} else if (auto dev = device()) {
if (peekHeader(dev, header) && header.isSupported()) {
v = QVariant::fromValue(QSize(header.width(), header.height()));
}
}
}
if (option == QImageIOHandler::ImageFormat) {
auto&& header = d->m_header;
if (header.isSupported()) {
v = QVariant::fromValue(header.format());
} else if (auto dev = device()) {
if (peekHeader(dev, header) && header.isSupported()) {
v = QVariant::fromValue(header.format());
}
}
}
return v;
}
bool PCXHandler::canRead(QIODevice *device)
{
if (!device) {
@ -788,30 +904,11 @@ bool PCXHandler::canRead(QIODevice *device)
return false;
}
qint64 oldPos = device->pos();
char head[1];
qint64 readBytes = device->read(head, sizeof(head));
if (readBytes != sizeof(head)) {
if (device->isSequential()) {
while (readBytes > 0) {
device->ungetChar(head[readBytes-- - 1]);
}
} else {
device->seek(oldPos);
}
PCXHEADER header;
if (!peekHeader(device, header)) {
return false;
}
if (device->isSequential()) {
while (readBytes > 0) {
device->ungetChar(head[readBytes-- - 1]);
}
} else {
device->seek(oldPos);
}
return qstrncmp(head, "\012", 1) == 0;
return header.isSupported();
}
QImageIOPlugin::Capabilities PCXPlugin::capabilities(QIODevice *device, const QByteArray &format) const

View File

@ -9,7 +9,9 @@
#define KIMG_PCX_P_H
#include <QImageIOPlugin>
#include <QScopedPointer>
class PCXHandlerPrivate;
class PCXHandler : public QImageIOHandler
{
public:
@ -19,7 +21,13 @@ 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);
private:
const QScopedPointer<PCXHandlerPrivate> d;
};
class PCXPlugin : public QImageIOPlugin

View File

@ -22,6 +22,8 @@
#include "rgb_p.h"
#include "util_p.h"
#include <cstring>
#include <QList>
#include <QMap>
@ -72,15 +74,25 @@ private:
uint _offset;
};
class SGIImage
class SGIImagePrivate
{
public:
SGIImage(QIODevice *device);
~SGIImage();
SGIImagePrivate();
~SGIImagePrivate();
bool readImage(QImage &);
bool writeImage(const QImage &);
bool isValid() const;
bool isSupported() const;
bool peekHeader(QIODevice *device);
QSize size() const;
QImage::Format format() const;
void setDevice(QIODevice *device);
private:
enum {
NORMAL,
@ -91,16 +103,19 @@ private:
QIODevice *_dev;
QDataStream _stream;
quint8 _rle;
quint8 _bpc;
quint16 _dim;
quint16 _xsize;
quint16 _ysize;
quint16 _zsize;
quint32 _pixmin;
quint32 _pixmax;
quint16 _magic = 0;
quint8 _rle = 0;
quint8 _bpc = 0;
quint16 _dim = 0;
quint16 _xsize = 0;
quint16 _ysize = 0;
quint16 _zsize = 0;
quint32 _pixmin = 0;
quint32 _pixmax = 0;
char _imagename[80];
quint32 _colormap;
quint32 _colormap = 0;
quint8 _unused[404];
quint32 _unused32 = 0;
quint32 *_starttab;
quint32 *_lengthtab;
@ -112,24 +127,28 @@ private:
bool readData(QImage &);
bool getRow(uchar *dest);
bool readHeader();
void writeHeader();
void writeRle();
void writeVerbatim(const QImage &);
static bool readHeader(QDataStream &ds, SGIImagePrivate *sgi);
bool writeHeader();
bool writeRle();
bool writeVerbatim(const QImage &);
bool scanData(const QImage &);
uint compact(uchar *, uchar *);
uchar intensity(uchar);
};
SGIImage::SGIImage(QIODevice *io)
: _starttab(nullptr)
SGIImagePrivate::SGIImagePrivate()
: _dev(nullptr)
, _starttab(nullptr)
, _lengthtab(nullptr)
{
_dev = io;
_stream.setDevice(_dev);
std::memset(_imagename, 0, sizeof(_imagename));
std::memset(_unused, 0, sizeof(_unused));
}
SGIImage::~SGIImage()
SGIImagePrivate::~SGIImagePrivate()
{
delete[] _starttab;
delete[] _lengthtab;
@ -137,7 +156,13 @@ SGIImage::~SGIImage()
///////////////////////////////////////////////////////////////////////////////
bool SGIImage::getRow(uchar *dest)
void SGIImagePrivate::setDevice(QIODevice *device)
{
_dev = device;
_stream.setDevice(_dev);
}
bool SGIImagePrivate::getRow(uchar *dest)
{
int n;
int i;
@ -180,7 +205,7 @@ bool SGIImage::getRow(uchar *dest)
return i == _xsize;
}
bool SGIImage::readData(QImage &img)
bool SGIImagePrivate::readData(QImage &img)
{
QRgb *c;
quint32 *start = _starttab;
@ -258,66 +283,9 @@ bool SGIImage::readData(QImage &img)
return true;
}
bool SGIImage::readImage(QImage &img)
bool SGIImagePrivate::readImage(QImage &img)
{
qint8 u8;
qint16 u16;
qint32 u32;
// qDebug() << "reading rgb ";
// magic
_stream >> u16;
if (u16 != 0x01da) {
return false;
}
// verbatim/rle
_stream >> _rle;
// qDebug() << (_rle ? "RLE" : "verbatim");
if (_rle > 1) {
return false;
}
// bytes per channel
_stream >> _bpc;
// qDebug() << "bytes per channel: " << int(_bpc);
if (_bpc == 1) {
;
} else if (_bpc == 2) {
// qDebug() << "dropping least significant byte";
} else {
return false;
}
// number of dimensions
_stream >> _dim;
// qDebug() << "dimensions: " << _dim;
if (_dim < 1 || _dim > 3) {
return false;
}
_stream >> _xsize >> _ysize >> _zsize >> _pixmin >> _pixmax >> u32;
// qDebug() << "x: " << _xsize;
// qDebug() << "y: " << _ysize;
// qDebug() << "z: " << _zsize;
// name
_stream.readRawData(_imagename, 80);
_imagename[79] = '\0';
_stream >> _colormap;
// qDebug() << "colormap: " << _colormap;
if (_colormap != NORMAL) {
return false; // only NORMAL supported
}
for (int i = 0; i < 404; i++) {
_stream >> u8;
}
if (_dim == 1) {
// qDebug() << "1-dimensional images aren't supported yet";
if (!readHeader() || !isSupported()) {
return false;
}
@ -325,19 +293,13 @@ bool SGIImage::readImage(QImage &img)
return false;
}
img = imageAlloc(_xsize, _ysize, QImage::Format_RGB32);
img = imageAlloc(size(), format());
if (img.isNull()) {
qWarning() << "Failed to allocate image, invalid dimensions?" << QSize(_xsize, _ysize);
return false;
}
if (_zsize == 0) {
return false;
}
if (_zsize == 2 || _zsize == 4) {
img = img.convertToFormat(QImage::Format_ARGB32);
} else if (_zsize > 4) {
if (_zsize > 4) {
// qDebug() << "using first 4 of " << _zsize << " channels";
// Only let this continue if it won't cause a int overflow later
// this is most likely a broken file anyway
@ -435,7 +397,7 @@ QList<const RLEData *> RLEMap::vector()
return v;
}
uchar SGIImage::intensity(uchar c)
uchar SGIImagePrivate::intensity(uchar c)
{
if (c < _pixmin) {
_pixmin = c;
@ -446,7 +408,7 @@ uchar SGIImage::intensity(uchar c)
return c;
}
uint SGIImage::compact(uchar *d, uchar *s)
uint SGIImagePrivate::compact(uchar *d, uchar *s)
{
uchar *dest = d;
uchar *src = s;
@ -489,7 +451,7 @@ uint SGIImage::compact(uchar *d, uchar *s)
return dest - d;
}
bool SGIImage::scanData(const QImage &img)
bool SGIImagePrivate::scanData(const QImage &img)
{
quint32 *start = _starttab;
QByteArray lineguard(_xsize * 2, 0);
@ -575,13 +537,127 @@ bool SGIImage::scanData(const QImage &img)
return true;
}
void SGIImage::writeHeader()
bool SGIImagePrivate::isValid() const
{
_stream << quint16(0x01da);
// File signature/magic number
if (_magic != 0x01da) {
return false;
}
// Compression, 0 = Uncompressed, 1 = RLE compressed
if (_rle > 1) {
return false;
}
// Bytes per pixel, 1 = 8 bit, 2 = 16 bit
if (_bpc != 1 && _bpc != 2) {
return false;
}
// Image dimension, 3 for RGBA image
if (_dim < 1 || _dim > 3) {
return false;
}
// Number channels in the image file, 4 for RGBA image
if (_zsize < 1) {
return false;
}
return true;
}
bool SGIImagePrivate::isSupported() const
{
if (!isValid()) {
return false;
}
if (_colormap != NORMAL) {
return false; // only NORMAL supported
}
if (_dim == 1) {
return false;
}
return true;
}
bool SGIImagePrivate::peekHeader(QIODevice *device)
{
qint64 pos = 0;
if (!device->isSequential()) {
pos = device->pos();
}
auto ok = false;
QByteArray header;
{ // datastream is destroyed before working on device
header = device->read(512);
QDataStream ds(header);
ok = SGIImagePrivate::readHeader(ds, this) && isValid();
}
if (!device->isSequential()) {
return device->seek(pos) && ok;
}
// sequential device undo
auto head = header.data();
auto readBytes = header.size();
while (readBytes > 0) {
device->ungetChar(head[readBytes-- - 1]);
}
return ok;
}
QSize SGIImagePrivate::size() const
{
return QSize(_xsize, _ysize);
}
QImage::Format SGIImagePrivate::format() const
{
if (_zsize == 2 || _zsize == 4) {
return QImage::Format_ARGB32;
}
return QImage::Format_RGB32;
}
bool SGIImagePrivate::readHeader()
{
return readHeader(_stream, this);
}
bool SGIImagePrivate::readHeader(QDataStream &ds, SGIImagePrivate *sgi)
{
// magic
ds >> sgi->_magic;
// verbatim/rle
ds >> sgi->_rle;
// bytes per channel
ds >> sgi->_bpc;
// number of dimensions
ds >> sgi->_dim;
ds >> sgi->_xsize >> sgi->_ysize >> sgi->_zsize >> sgi->_pixmin >> sgi->_pixmax >> sgi->_unused32;
// name
ds.readRawData(sgi->_imagename, 80);
sgi->_imagename[79] = '\0';
ds >> sgi->_colormap;
for (size_t i = 0; i < sizeof(_unused); i++) {
ds >> sgi->_unused[i];
}
return ds.status() == QDataStream::Ok;
}
bool SGIImagePrivate::writeHeader()
{
_stream << _magic;
_stream << _rle << _bpc << _dim;
_stream << _xsize << _ysize << _zsize;
_stream << _pixmin << _pixmax;
_stream << quint32(0);
_stream << _unused32;
for (int i = 0; i < 80; i++) {
_imagename[i] = '\0';
@ -589,16 +665,20 @@ void SGIImage::writeHeader()
_stream.writeRawData(_imagename, 80);
_stream << _colormap;
for (int i = 0; i < 404; i++) {
_stream << quint8(0);
for (size_t i = 0; i < sizeof(_unused); i++) {
_stream << _unused[i];
}
return _stream.status() == QDataStream::Ok;
}
void SGIImage::writeRle()
bool SGIImagePrivate::writeRle()
{
_rle = 1;
// qDebug() << "writing RLE data";
writeHeader();
if (!writeHeader()) {
return false;
}
uint i;
// write start table
@ -615,13 +695,16 @@ void SGIImage::writeRle()
for (i = 0; (int)i < _rlevector.size(); i++) {
const_cast<RLEData *>(_rlevector[i])->write(_stream);
}
return _stream.status() == QDataStream::Ok;
}
void SGIImage::writeVerbatim(const QImage &img)
bool SGIImagePrivate::writeVerbatim(const QImage &img)
{
_rle = 0;
// qDebug() << "writing verbatim data";
writeHeader();
if (!writeHeader()) {
return false;
}
const QRgb *c;
unsigned x;
@ -635,7 +718,7 @@ void SGIImage::writeVerbatim(const QImage &img)
}
if (_zsize == 1) {
return;
return _stream.status() == QDataStream::Ok;
}
if (_zsize != 2) {
@ -654,7 +737,7 @@ void SGIImage::writeVerbatim(const QImage &img)
}
if (_zsize == 3) {
return;
return _stream.status() == QDataStream::Ok;
}
}
@ -664,9 +747,11 @@ void SGIImage::writeVerbatim(const QImage &img)
_stream << quint8(qAlpha(*c++));
}
}
return _stream.status() == QDataStream::Ok;
}
bool SGIImage::writeImage(const QImage &image)
bool SGIImagePrivate::writeImage(const QImage &image)
{
// qDebug() << "writing "; // TODO add filename
QImage img = image;
@ -698,6 +783,7 @@ bool SGIImage::writeImage(const QImage &image)
return false;
}
_magic = 0x01da;
_bpc = 1;
_xsize = w;
_ysize = h;
@ -722,16 +808,16 @@ bool SGIImage::writeImage(const QImage &image)
}
if (verbatim_size <= rle_size) {
writeVerbatim(img);
} else {
writeRle();
return writeVerbatim(img);
}
return true;
return writeRle();
}
///////////////////////////////////////////////////////////////////////////////
RGBHandler::RGBHandler()
: QImageIOHandler()
, d(new SGIImagePrivate)
{
}
@ -746,14 +832,54 @@ bool RGBHandler::canRead() const
bool RGBHandler::read(QImage *outImage)
{
SGIImage sgi(device());
return sgi.readImage(*outImage);
d->setDevice(device());
return d->readImage(*outImage);
}
bool RGBHandler::write(const QImage &image)
{
SGIImage sgi(device());
return sgi.writeImage(image);
d->setDevice(device());
return d->writeImage(image);
}
bool RGBHandler::supportsOption(ImageOption option) const
{
if (option == QImageIOHandler::Size) {
return true;
}
if (option == QImageIOHandler::ImageFormat) {
return true;
}
return false;
}
QVariant RGBHandler::option(ImageOption option) const
{
QVariant v;
if (option == QImageIOHandler::Size) {
auto &&sgi = d;
if (sgi->isSupported()) {
v = QVariant::fromValue(sgi->size());
} else if (auto dev = device()) {
if (d->peekHeader(dev) && sgi->isSupported()) {
v = QVariant::fromValue(sgi->size());
}
}
}
if (option == QImageIOHandler::ImageFormat) {
auto &&sgi = d;
if (sgi->isSupported()) {
v = QVariant::fromValue(sgi->format());
} else if (auto dev = device()) {
if (d->peekHeader(dev) && sgi->isSupported()) {
v = QVariant::fromValue(sgi->format());
}
}
}
return v;
}
bool RGBHandler::canRead(QIODevice *device)
@ -763,20 +889,8 @@ bool RGBHandler::canRead(QIODevice *device)
return false;
}
const qint64 oldPos = device->pos();
const QByteArray head = device->readLine(64);
int readBytes = head.size();
if (device->isSequential()) {
while (readBytes > 0) {
device->ungetChar(head[readBytes-- - 1]);
}
} else {
device->seek(oldPos);
}
return head.size() >= 4 && head.startsWith("\x01\xda") && (head[2] == 0 || head[2] == 1) && (head[3] == 1 || head[3] == 2);
SGIImagePrivate sgi;
return sgi.peekHeader(device) && sgi.isSupported();
}
///////////////////////////////////////////////////////////////////////////////

View File

@ -9,7 +9,9 @@
#define KIMG_RGB_P_H
#include <QImageIOPlugin>
#include <QScopedPointer>
class SGIImagePrivate;
class RGBHandler : public QImageIOHandler
{
public:
@ -19,7 +21,13 @@ 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);
private:
QScopedPointer<SGIImagePrivate> d;
};
class RGBPlugin : public QImageIOPlugin

View File

@ -1974,6 +1974,12 @@ static bool convertFloatTo16Bit(uchar *output, quint64 outputSize, uchar *input)
*/
bool XCFImageFormat::loadLevel(QDataStream &xcf_io, Layer &layer, qint32 bpp, const GimpPrecision precision)
{
auto bpc = bytesPerChannel(precision);
if ((bpc == 0) || (bpp % bpc)) {
qCDebug(XCFPLUGIN) << "XCF: the stream seems corrupted";
return false;
}
qint32 width;
qint32 height;