Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
55227815d5 | |||
64d51ed610 | |||
2ca57c9c59 | |||
f7fd14d418 | |||
c9aa1ff629 | |||
91d3bd5227 | |||
bb66367bc8 | |||
14770318a3 | |||
9b1fafe29b | |||
fa673b5df8 | |||
e96b43aef5 | |||
64f3303ef0 | |||
63056c52f9 | |||
2997f7ae8d | |||
0b4741f4b7 | |||
bc52c03981 | |||
c1c57d9a11 |
@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.16)
|
||||
project(KImageFormats)
|
||||
|
||||
include(FeatureSummary)
|
||||
find_package(ECM 5.102.0 NO_MODULE)
|
||||
find_package(ECM 5.107.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)
|
||||
|
||||
|
BIN
autotests/read/pcx/ccbug_463951.pcx
Normal file
BIN
autotests/read/pcx/ccbug_463951.png
Normal file
After Width: | Height: | Size: 8.5 KiB |
BIN
autotests/read/pcx/mono.pcx
Normal file
BIN
autotests/read/pcx/mono.png
Normal file
After Width: | Height: | Size: 24 KiB |
Before Width: | Height: | Size: 5.2 KiB After Width: | Height: | Size: 4.9 KiB |
Before Width: | Height: | Size: 983 B After Width: | Height: | Size: 4.0 KiB |
Before Width: | Height: | Size: 94 KiB |
BIN
autotests/read/psd/birthday.tif
Normal file
Before Width: | Height: | Size: 189 KiB After Width: | Height: | Size: 189 KiB |
Before Width: | Height: | Size: 117 KiB After Width: | Height: | Size: 114 KiB |
@ -159,17 +159,22 @@ int main(int argc, char **argv)
|
||||
QTextStream(stdout) << "* Run on RANDOM ACCESS device\n";
|
||||
}
|
||||
for (const QFileInfo &fi : lstImgDir) {
|
||||
if (!fi.suffix().compare("png", Qt::CaseInsensitive)) {
|
||||
if (!fi.suffix().compare("png", Qt::CaseInsensitive) || !fi.suffix().compare("tif", Qt::CaseInsensitive)) {
|
||||
continue;
|
||||
}
|
||||
int suffixPos = fi.filePath().count() - suffix.count();
|
||||
QString inputfile = fi.filePath();
|
||||
QString expfile = fi.filePath().replace(suffixPos, suffix.count(), QStringLiteral("png"));
|
||||
QString fmt = QStringLiteral("png");
|
||||
QString expfile = fi.filePath().replace(suffixPos, suffix.count(), fmt);
|
||||
if (!QFile::exists(expfile)) { // try with tiff
|
||||
fmt = QStringLiteral("tif");
|
||||
expfile = fi.filePath().replace(suffixPos, suffix.count(), fmt);
|
||||
}
|
||||
QString expfilename = QFileInfo(expfile).fileName();
|
||||
|
||||
std::unique_ptr<QIODevice> inputDevice(seq ? new SequentialFile(inputfile) : new QFile(inputfile));
|
||||
QImageReader inputReader(inputDevice.get(), format);
|
||||
QImageReader expReader(expfile, "png");
|
||||
QImageReader expReader(expfile, fmt.toLatin1());
|
||||
|
||||
QImage inputImage;
|
||||
QImage expImage;
|
||||
|
Before Width: | Height: | Size: 4.0 KiB After Width: | Height: | Size: 4.0 KiB |
Before Width: | Height: | Size: 4.0 KiB After Width: | Height: | Size: 4.0 KiB |
35
src/imageformats/fastmath_p.h
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
Approximated math functions used into conversions.
|
||||
|
||||
SPDX-FileCopyrightText: Edward Kmett
|
||||
SPDX-FileCopyrightText: 2023 Mirco Miranda <mircomir@outlook.com>
|
||||
|
||||
SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
#ifndef FASTMATH_P_H
|
||||
#define FASTMATH_P_H
|
||||
|
||||
#include <QtGlobal>
|
||||
|
||||
/*!
|
||||
* \brief fastPow
|
||||
* Based on Edward Kmett code released into the public domain.
|
||||
* See also: https://github.com/ekmett/approximate
|
||||
*/
|
||||
inline double fastPow(double x, double y)
|
||||
{
|
||||
union {
|
||||
double d;
|
||||
qint32 i[2];
|
||||
} u = {x};
|
||||
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
|
||||
u.i[1] = qint32(y * (u.i[1] - 1072632447) + 1072632447);
|
||||
u.i[0] = 0;
|
||||
#else // never tested
|
||||
u.i[0] = qint32(y * (u.i[0] - 1072632447) + 1072632447);
|
||||
u.i[1] = 0;
|
||||
#endif
|
||||
return u.d;
|
||||
}
|
||||
|
||||
#endif // FASTMATH_P_H
|
@ -449,6 +449,14 @@ bool HEIFHandler::ensureDecoder()
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((heif_image_handle_get_width(handle) == 0) || (heif_image_handle_get_height(handle) == 0)) {
|
||||
m_parseState = ParseHeicError;
|
||||
heif_image_handle_release(handle);
|
||||
heif_context_free(ctx);
|
||||
qWarning() << "HEIC image has zero dimension";
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool hasAlphaChannel = heif_image_handle_has_alpha_channel(handle);
|
||||
const int bit_depth = heif_image_handle_get_luma_bits_per_pixel(handle);
|
||||
heif_chroma chroma;
|
||||
|
@ -230,7 +230,7 @@ PCXHEADER::PCXHEADER()
|
||||
s >> *this;
|
||||
}
|
||||
|
||||
static void readLine(QDataStream &s, QByteArray &buf, const PCXHEADER &header)
|
||||
static bool readLine(QDataStream &s, QByteArray &buf, const PCXHEADER &header)
|
||||
{
|
||||
quint32 i = 0;
|
||||
quint32 size = buf.size();
|
||||
@ -257,9 +257,11 @@ static void readLine(QDataStream &s, QByteArray &buf, const PCXHEADER &header)
|
||||
buf[i++] = byte;
|
||||
}
|
||||
}
|
||||
|
||||
return (s.status() == QDataStream::Ok);
|
||||
}
|
||||
|
||||
static void readImage1(QImage &img, QDataStream &s, const PCXHEADER &header)
|
||||
static bool readImage1(QImage &img, QDataStream &s, const PCXHEADER &header)
|
||||
{
|
||||
QByteArray buf(header.BytesPerLine, 0);
|
||||
|
||||
@ -268,16 +270,18 @@ static void readImage1(QImage &img, QDataStream &s, const PCXHEADER &header)
|
||||
|
||||
if (img.isNull()) {
|
||||
qWarning() << "Failed to allocate image, invalid dimensions?" << QSize(header.width(), header.height());
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int y = 0; y < header.height(); ++y) {
|
||||
if (s.atEnd()) {
|
||||
img = QImage();
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!readLine(s, buf, header)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
readLine(s, buf, header);
|
||||
uchar *p = img.scanLine(y);
|
||||
unsigned int bpl = qMin((quint16)((header.width() + 7) / 8), header.BytesPerLine);
|
||||
for (unsigned int x = 0; x < bpl; ++x) {
|
||||
@ -288,9 +292,11 @@ static void readImage1(QImage &img, QDataStream &s, const PCXHEADER &header)
|
||||
// Set the color palette
|
||||
img.setColor(0, qRgb(0, 0, 0));
|
||||
img.setColor(1, qRgb(255, 255, 255));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void readImage4(QImage &img, QDataStream &s, const PCXHEADER &header)
|
||||
static bool readImage4(QImage &img, QDataStream &s, const PCXHEADER &header)
|
||||
{
|
||||
QByteArray buf(header.BytesPerLine * 4, 0);
|
||||
QByteArray pixbuf(header.width(), 0);
|
||||
@ -299,17 +305,18 @@ static void readImage4(QImage &img, QDataStream &s, const PCXHEADER &header)
|
||||
img.setColorCount(16);
|
||||
if (img.isNull()) {
|
||||
qWarning() << "Failed to allocate image, invalid dimensions?" << QSize(header.width(), header.height());
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int y = 0; y < header.height(); ++y) {
|
||||
if (s.atEnd()) {
|
||||
img = QImage();
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
pixbuf.fill(0);
|
||||
readLine(s, buf, header);
|
||||
if (!readLine(s, buf, header)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
quint32 offset = i * header.BytesPerLine;
|
||||
@ -333,9 +340,11 @@ static void readImage4(QImage &img, QDataStream &s, const PCXHEADER &header)
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
img.setColor(i, header.ColorMap.color(i));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void readImage8(QImage &img, QDataStream &s, const PCXHEADER &header)
|
||||
static bool readImage8(QImage &img, QDataStream &s, const PCXHEADER &header)
|
||||
{
|
||||
QByteArray buf(header.BytesPerLine, 0);
|
||||
|
||||
@ -344,21 +353,21 @@ static void readImage8(QImage &img, QDataStream &s, const PCXHEADER &header)
|
||||
|
||||
if (img.isNull()) {
|
||||
qWarning() << "Failed to allocate image, invalid dimensions?" << QSize(header.width(), header.height());
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int y = 0; y < header.height(); ++y) {
|
||||
if (s.atEnd()) {
|
||||
img = QImage();
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
readLine(s, buf, header);
|
||||
if (!readLine(s, buf, header)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uchar *p = img.scanLine(y);
|
||||
|
||||
if (!p) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned int bpl = qMin(header.BytesPerLine, (quint16)header.width());
|
||||
@ -367,10 +376,21 @@ static void readImage8(QImage &img, QDataStream &s, const PCXHEADER &header)
|
||||
}
|
||||
}
|
||||
|
||||
quint8 flag;
|
||||
// by specification, the extended palette starts at file.size() - 769
|
||||
quint8 flag = 0;
|
||||
if (auto device = s.device()) {
|
||||
if (device->isSequential()) {
|
||||
while (flag != 12 && s.status() == QDataStream::Ok) {
|
||||
s >> flag;
|
||||
// qDebug() << "Palette Flag: " << flag;
|
||||
}
|
||||
}
|
||||
else {
|
||||
device->seek(device->size() - 769);
|
||||
s >> flag;
|
||||
}
|
||||
}
|
||||
|
||||
// qDebug() << "Palette Flag: " << flag;
|
||||
if (flag == 12 && (header.Version == 5 || header.Version == 2)) {
|
||||
// Read the palette
|
||||
quint8 r;
|
||||
@ -381,9 +401,11 @@ static void readImage8(QImage &img, QDataStream &s, const PCXHEADER &header)
|
||||
img.setColor(i, qRgb(r, g, b));
|
||||
}
|
||||
}
|
||||
|
||||
return (s.status() == QDataStream::Ok);
|
||||
}
|
||||
|
||||
static void readImage24(QImage &img, QDataStream &s, const PCXHEADER &header)
|
||||
static bool readImage24(QImage &img, QDataStream &s, const PCXHEADER &header)
|
||||
{
|
||||
QByteArray r_buf(header.BytesPerLine, 0);
|
||||
QByteArray g_buf(header.BytesPerLine, 0);
|
||||
@ -393,27 +415,34 @@ static void readImage24(QImage &img, QDataStream &s, const PCXHEADER &header)
|
||||
|
||||
if (img.isNull()) {
|
||||
qWarning() << "Failed to allocate image, invalid dimensions?" << QSize(header.width(), header.height());
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int y = 0; y < header.height(); ++y) {
|
||||
if (s.atEnd()) {
|
||||
img = QImage();
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
readLine(s, r_buf, header);
|
||||
readLine(s, g_buf, header);
|
||||
readLine(s, b_buf, header);
|
||||
if (!readLine(s, r_buf, header)) {
|
||||
return false;
|
||||
}
|
||||
if (!readLine(s, g_buf, header)) {
|
||||
return false;
|
||||
}
|
||||
if (!readLine(s, b_buf, header)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint *p = (uint *)img.scanLine(y);
|
||||
for (int x = 0; x < header.width(); ++x) {
|
||||
p[x] = qRgb(r_buf[x], g_buf[x], b_buf[x]);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void writeLine(QDataStream &s, QByteArray &buf)
|
||||
static bool writeLine(QDataStream &s, QByteArray &buf)
|
||||
{
|
||||
quint32 i = 0;
|
||||
quint32 size = buf.size();
|
||||
@ -439,15 +468,26 @@ static void writeLine(QDataStream &s, QByteArray &buf)
|
||||
|
||||
s << data;
|
||||
}
|
||||
return (s.status() == QDataStream::Ok);
|
||||
}
|
||||
|
||||
static void writeImage1(QImage &img, QDataStream &s, PCXHEADER &header)
|
||||
static bool writeImage1(QImage &img, QDataStream &s, PCXHEADER &header)
|
||||
{
|
||||
if (img.format() != QImage::Format_Mono) {
|
||||
img = img.convertToFormat(QImage::Format_Mono);
|
||||
}
|
||||
if (img.isNull() || img.colorCount() < 1) {
|
||||
return false;
|
||||
}
|
||||
auto rgb = img.color(0);
|
||||
auto minIsBlack = (qRed(rgb) + qGreen(rgb) + qBlue(rgb)) / 3 < 127;
|
||||
|
||||
header.Bpp = 1;
|
||||
header.NPlanes = 1;
|
||||
header.BytesPerLine = img.bytesPerLine();
|
||||
if (header.BytesPerLine == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
s << header;
|
||||
|
||||
@ -458,18 +498,24 @@ static void writeImage1(QImage &img, QDataStream &s, PCXHEADER &header)
|
||||
|
||||
// Invert as QImage uses reverse palette for monochrome images?
|
||||
for (int i = 0; i < header.BytesPerLine; ++i) {
|
||||
buf[i] = ~p[i];
|
||||
buf[i] = minIsBlack ? p[i] : ~p[i];
|
||||
}
|
||||
|
||||
writeLine(s, buf);
|
||||
if (!writeLine(s, buf)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void writeImage4(QImage &img, QDataStream &s, PCXHEADER &header)
|
||||
static bool writeImage4(QImage &img, QDataStream &s, PCXHEADER &header)
|
||||
{
|
||||
header.Bpp = 1;
|
||||
header.NPlanes = 4;
|
||||
header.BytesPerLine = header.width() / 8;
|
||||
if (header.BytesPerLine == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
header.ColorMap.setColor(i, img.color(i));
|
||||
@ -499,16 +545,22 @@ static void writeImage4(QImage &img, QDataStream &s, PCXHEADER &header)
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
writeLine(s, buf[i]);
|
||||
if (!writeLine(s, buf[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void writeImage8(QImage &img, QDataStream &s, PCXHEADER &header)
|
||||
static bool writeImage8(QImage &img, QDataStream &s, PCXHEADER &header)
|
||||
{
|
||||
header.Bpp = 8;
|
||||
header.NPlanes = 1;
|
||||
header.BytesPerLine = img.bytesPerLine();
|
||||
if (header.BytesPerLine == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
s << header;
|
||||
|
||||
@ -521,7 +573,9 @@ static void writeImage8(QImage &img, QDataStream &s, PCXHEADER &header)
|
||||
buf[i] = p[i];
|
||||
}
|
||||
|
||||
writeLine(s, buf);
|
||||
if (!writeLine(s, buf)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Write palette flag
|
||||
@ -532,13 +586,25 @@ static void writeImage8(QImage &img, QDataStream &s, PCXHEADER &header)
|
||||
for (int i = 0; i < 256; ++i) {
|
||||
s << RGB::from(img.color(i));
|
||||
}
|
||||
|
||||
return (s.status() == QDataStream::Ok);
|
||||
}
|
||||
|
||||
static void writeImage24(QImage &img, QDataStream &s, PCXHEADER &header)
|
||||
static bool writeImage24(QImage &img, QDataStream &s, PCXHEADER &header)
|
||||
{
|
||||
header.Bpp = 8;
|
||||
header.NPlanes = 3;
|
||||
header.BytesPerLine = header.width();
|
||||
if (header.BytesPerLine == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (img.format() != QImage::Format_ARGB32 && img.format() != QImage::Format_RGB32) {
|
||||
img = img.convertToFormat(QImage::Format_RGB32);
|
||||
}
|
||||
if (img.isNull()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
s << header;
|
||||
|
||||
@ -547,7 +613,7 @@ static void writeImage24(QImage &img, QDataStream &s, PCXHEADER &header)
|
||||
QByteArray b_buf(header.width(), 0);
|
||||
|
||||
for (int y = 0; y < header.height(); ++y) {
|
||||
uint *p = (uint *)img.scanLine(y);
|
||||
auto p = (QRgb*)img.scanLine(y);
|
||||
|
||||
for (int x = 0; x < header.width(); ++x) {
|
||||
QRgb rgb = *p++;
|
||||
@ -556,10 +622,18 @@ static void writeImage24(QImage &img, QDataStream &s, PCXHEADER &header)
|
||||
b_buf[x] = qBlue(rgb);
|
||||
}
|
||||
|
||||
writeLine(s, r_buf);
|
||||
writeLine(s, g_buf);
|
||||
writeLine(s, b_buf);
|
||||
if (!writeLine(s, r_buf)) {
|
||||
return false;
|
||||
}
|
||||
if (!writeLine(s, g_buf)) {
|
||||
return false;
|
||||
}
|
||||
if (!writeLine(s, b_buf)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
PCXHandler::PCXHandler()
|
||||
@ -588,46 +662,30 @@ bool PCXHandler::read(QImage *outImage)
|
||||
|
||||
s >> header;
|
||||
|
||||
if (header.Manufacturer != 10 || s.atEnd()) {
|
||||
if (header.Manufacturer != 10 || header.BytesPerLine == 0 || s.atEnd()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// int w = header.width();
|
||||
// int h = header.height();
|
||||
|
||||
// qDebug() << "Manufacturer: " << header.Manufacturer;
|
||||
// qDebug() << "Version: " << header.Version;
|
||||
// qDebug() << "Encoding: " << header.Encoding;
|
||||
// qDebug() << "Bpp: " << header.Bpp;
|
||||
// qDebug() << "Width: " << w;
|
||||
// qDebug() << "Height: " << h;
|
||||
// qDebug() << "Window: " << header.XMin << "," << header.XMax << ","
|
||||
// << header.YMin << "," << header.YMax << endl;
|
||||
// qDebug() << "BytesPerLine: " << header.BytesPerLine;
|
||||
// qDebug() << "NPlanes: " << header.NPlanes;
|
||||
|
||||
auto ok = false;
|
||||
QImage img;
|
||||
|
||||
if (header.Bpp == 1 && header.NPlanes == 1) {
|
||||
readImage1(img, s, header);
|
||||
ok = readImage1(img, s, header);
|
||||
} else if (header.Bpp == 1 && header.NPlanes == 4) {
|
||||
readImage4(img, s, header);
|
||||
ok = readImage4(img, s, header);
|
||||
} else if (header.Bpp == 8 && header.NPlanes == 1) {
|
||||
readImage8(img, s, header);
|
||||
ok = readImage8(img, s, header);
|
||||
} else if (header.Bpp == 8 && header.NPlanes == 3) {
|
||||
readImage24(img, s, header);
|
||||
ok = readImage24(img, s, header);
|
||||
}
|
||||
|
||||
// qDebug() << "Image Bytes: " << img.numBytes();
|
||||
// qDebug() << "Image Bytes Per Line: " << img.bytesPerLine();
|
||||
// qDebug() << "Image Depth: " << img.depth();
|
||||
if (img.isNull() || !ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!img.isNull()) {
|
||||
img.setDotsPerMeterX(qRound(header.HDpi / 25.4 * 1000));
|
||||
img.setDotsPerMeterY(qRound(header.YDpi / 25.4 * 1000));
|
||||
*outImage = img;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool PCXHandler::write(const QImage &image)
|
||||
@ -644,12 +702,6 @@ bool PCXHandler::write(const QImage &image)
|
||||
return false;
|
||||
}
|
||||
|
||||
// qDebug() << "Width: " << w;
|
||||
// qDebug() << "Height: " << h;
|
||||
// qDebug() << "Depth: " << img.depth();
|
||||
// qDebug() << "BytesPerLine: " << img.bytesPerLine();
|
||||
// qDebug() << "Color Count: " << img.colorCount();
|
||||
|
||||
PCXHEADER header;
|
||||
|
||||
header.Manufacturer = 10;
|
||||
@ -659,22 +711,23 @@ bool PCXHandler::write(const QImage &image)
|
||||
header.YMin = 0;
|
||||
header.XMax = w - 1;
|
||||
header.YMax = h - 1;
|
||||
header.HDpi = 300;
|
||||
header.YDpi = 300;
|
||||
header.HDpi = qRound(image.dotsPerMeterX() * 25.4 / 1000);
|
||||
header.YDpi = qRound(image.dotsPerMeterY() * 25.4 / 1000);
|
||||
header.Reserved = 0;
|
||||
header.PaletteInfo = 1;
|
||||
|
||||
auto ok = false;
|
||||
if (img.depth() == 1) {
|
||||
writeImage1(img, s, header);
|
||||
ok = writeImage1(img, s, header);
|
||||
} else if (img.depth() == 8 && img.colorCount() <= 16) {
|
||||
writeImage4(img, s, header);
|
||||
ok = writeImage4(img, s, header);
|
||||
} else if (img.depth() == 8) {
|
||||
writeImage8(img, s, header);
|
||||
} else if (img.depth() == 32) {
|
||||
writeImage24(img, s, header);
|
||||
ok = writeImage8(img, s, header);
|
||||
} else if (img.depth() >= 24) {
|
||||
ok = writeImage24(img, s, header);
|
||||
}
|
||||
|
||||
return true;
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool PCXHandler::canRead(QIODevice *device)
|
||||
|
@ -3,14 +3,14 @@
|
||||
|
||||
SPDX-FileCopyrightText: 2003 Ignacio Castaño <castano@ludicon.com>
|
||||
SPDX-FileCopyrightText: 2015 Alex Merry <alex.merry@kde.org>
|
||||
SPDX-FileCopyrightText: 2022 Mirco Miranda <mircomir@outlook.com>
|
||||
SPDX-FileCopyrightText: 2022-2023 Mirco Miranda <mircomir@outlook.com>
|
||||
|
||||
SPDX-License-Identifier: LGPL-2.0-or-later
|
||||
*/
|
||||
|
||||
/*
|
||||
* This code is based on Thacher Ulrich PSD loading code released
|
||||
* into the public domain. See: http://tulrich.com/geekstuff/
|
||||
* The early version of this code was based on Thacher Ulrich PSD loading code
|
||||
* released into the public domain. See: http://tulrich.com/geekstuff/
|
||||
*/
|
||||
|
||||
/*
|
||||
@ -21,7 +21,6 @@
|
||||
/*
|
||||
* Limitations of the current code:
|
||||
* - 32-bit float image are converted to 16-bit integer image.
|
||||
* NOTE: Qt 6.2 allow 32-bit float images (RGB only)
|
||||
* - Other color spaces cannot directly be read due to lack of QImage support for
|
||||
* color spaces other than RGB (and Grayscale). Where possible, a conversion
|
||||
* to RGB is done:
|
||||
@ -33,6 +32,7 @@
|
||||
* color management engine (e.g. LittleCMS).
|
||||
*/
|
||||
|
||||
#include "fastmath_p.h"
|
||||
#include "psd_p.h"
|
||||
#include "util_p.h"
|
||||
|
||||
@ -51,7 +51,7 @@ typedef quint8 uchar;
|
||||
* This should not be a problem because the Qt's QColorSpace supports the linear
|
||||
* sRgb colorspace.
|
||||
*
|
||||
* Using linear conversion, the loading speed is improved by 4x. Anyway, if you are using
|
||||
* Using linear conversion, the loading speed is slightly improved. Anyway, if you are using
|
||||
* an software that discard color info, you should comment it.
|
||||
*
|
||||
* At the time I'm writing (07/2022), Gwenview and Krita supports linear sRgb but KDE
|
||||
@ -733,9 +733,9 @@ static QImage::Format imageFormat(const PSDHeader &header, bool alpha)
|
||||
switch(header.color_mode) {
|
||||
case CM_RGB:
|
||||
if (header.depth == 16 || header.depth == 32)
|
||||
format = header.channel_count < 4 || !alpha ? QImage::Format_RGBX64 : QImage::Format_RGBA64;
|
||||
format = header.channel_count < 4 || !alpha ? QImage::Format_RGBX64 : QImage::Format_RGBA64_Premultiplied;
|
||||
else
|
||||
format = header.channel_count < 4 || !alpha ? QImage::Format_RGB888 : QImage::Format_RGBA8888;
|
||||
format = header.channel_count < 4 || !alpha ? QImage::Format_RGB888 : QImage::Format_RGBA8888_Premultiplied;
|
||||
break;
|
||||
case CM_MULTICHANNEL: // Treat MCH as CMYK (number of channel check is done in IsSupported())
|
||||
case CM_CMYK: // Photoshop supports CMYK/MCH 8-bits and 16-bits only
|
||||
@ -830,6 +830,43 @@ inline void planarToChunchyFloat(uchar *target, const char *source, qint32 width
|
||||
}
|
||||
}
|
||||
|
||||
enum class PremulConversion {
|
||||
PS2P, // Photoshop premul to qimage premul (required by RGB)
|
||||
PS2A, // Photoshop premul to unassociated alpha (required by RGB, CMYK and L* components of LAB)
|
||||
PSLab2A // Photoshop premul to unassociated alpha (required by a* and b* components of LAB)
|
||||
};
|
||||
|
||||
template<class T>
|
||||
inline void premulConversion(char *stride, qint32 width, qint32 ac, qint32 cn, const PremulConversion &conv)
|
||||
{
|
||||
auto s = reinterpret_cast<T *>(stride);
|
||||
auto max = qint64(std::numeric_limits<T>::max());
|
||||
|
||||
for (qint32 c = 0; c < ac; ++c) {
|
||||
if (conv == PremulConversion::PS2P) {
|
||||
for (qint32 x = 0; x < width; ++x) {
|
||||
auto xcn = x * cn;
|
||||
auto alpha = *(s + xcn + ac);
|
||||
*(s + xcn + c) = *(s + xcn + c) + alpha - max;
|
||||
}
|
||||
} else if (conv == PremulConversion::PS2A || (conv == PremulConversion::PSLab2A && c == 0)) {
|
||||
for (qint32 x = 0; x < width; ++x) {
|
||||
auto xcn = x * cn;
|
||||
auto alpha = *(s + xcn + ac);
|
||||
if (alpha > 0)
|
||||
*(s + xcn + c) = ((*(s + xcn + c) + alpha - max) * max + alpha / 2) / alpha;
|
||||
}
|
||||
} else if (conv == PremulConversion::PSLab2A) {
|
||||
for (qint32 x = 0; x < width; ++x) {
|
||||
auto xcn = x * cn;
|
||||
auto alpha = *(s + xcn + ac);
|
||||
if (alpha > 0)
|
||||
*(s + xcn + c) = ((*(s + xcn + c) + (alpha - max + 1) / 2) * max + alpha / 2) / alpha;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline void monoInvert(uchar *target, const char* source, qint32 bytes)
|
||||
{
|
||||
auto s = reinterpret_cast<const quint8*>(source);
|
||||
@ -839,12 +876,25 @@ inline void monoInvert(uchar *target, const char* source, qint32 bytes)
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void rawChannelsCopy(uchar *target, qint32 targetChannels, const char *source, qint32 sourceChannels, qint32 width)
|
||||
{
|
||||
auto s = reinterpret_cast<const T *>(source);
|
||||
auto t = reinterpret_cast<T *>(target);
|
||||
for (qint32 c = 0, cs = std::min(targetChannels, sourceChannels); c < cs; ++c) {
|
||||
for (qint32 x = 0; x < width; ++x) {
|
||||
t[x * targetChannels + c] = s[x * sourceChannels + c];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void cmykToRgb(uchar *target, qint32 targetChannels, const char *source, qint32 sourceChannels, qint32 width, bool alpha = false)
|
||||
{
|
||||
auto s = reinterpret_cast<const T*>(source);
|
||||
auto t = reinterpret_cast<T*>(target);
|
||||
auto max = double(std::numeric_limits<T>::max());
|
||||
auto invmax = 1.0 / max; // speed improvements by ~10%
|
||||
|
||||
if (sourceChannels < 4) {
|
||||
qDebug() << "cmykToRgb: image is not a valid CMYK!";
|
||||
@ -853,10 +903,10 @@ inline void cmykToRgb(uchar *target, qint32 targetChannels, const char *source,
|
||||
|
||||
for (qint32 w = 0; w < width; ++w) {
|
||||
auto ps = s + sourceChannels * w;
|
||||
auto C = 1 - *(ps + 0) / max;
|
||||
auto M = 1 - *(ps + 1) / max;
|
||||
auto Y = 1 - *(ps + 2) / max;
|
||||
auto K = 1 - *(ps + 3) / max;
|
||||
auto C = 1 - *(ps + 0) * invmax;
|
||||
auto M = 1 - *(ps + 1) * invmax;
|
||||
auto Y = 1 - *(ps + 2) * invmax;
|
||||
auto K = 1 - *(ps + 3) * invmax;
|
||||
|
||||
auto pt = t + targetChannels * w;
|
||||
*(pt + 0) = T(std::min(max - (C * (1 - K) + K) * max + 0.5, max));
|
||||
@ -881,8 +931,9 @@ inline double gammaCorrection(double linear)
|
||||
#ifdef PSD_FAST_LAB_CONVERSION
|
||||
return linear;
|
||||
#else
|
||||
// NOTE: pow() slow down the performance by a 4 factor :(
|
||||
return (linear > 0.0031308 ? 1.055 * std::pow(linear, 1.0 / 2.4) - 0.055 : 12.92 * linear);
|
||||
// Replacing fastPow with std::pow the conversion time is 2/3 times longer: using fastPow
|
||||
// there are minimal differences in the conversion that are not visually noticeable.
|
||||
return (linear > 0.0031308 ? 1.055 * fastPow(linear, 1.0 / 2.4) - 0.055 : 12.92 * linear);
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -892,6 +943,7 @@ inline void labToRgb(uchar *target, qint32 targetChannels, const char *source, q
|
||||
auto s = reinterpret_cast<const T*>(source);
|
||||
auto t = reinterpret_cast<T*>(target);
|
||||
auto max = double(std::numeric_limits<T>::max());
|
||||
auto invmax = 1.0 / max;
|
||||
|
||||
if (sourceChannels < 3) {
|
||||
qDebug() << "labToRgb: image is not a valid LAB!";
|
||||
@ -900,14 +952,14 @@ inline void labToRgb(uchar *target, qint32 targetChannels, const char *source, q
|
||||
|
||||
for (qint32 w = 0; w < width; ++w) {
|
||||
auto ps = s + sourceChannels * w;
|
||||
auto L = (*(ps + 0) / max) * 100.0;
|
||||
auto A = (*(ps + 1) / max) * 255.0 - 128.0;
|
||||
auto B = (*(ps + 2) / max) * 255.0 - 128.0;
|
||||
auto L = (*(ps + 0) * invmax) * 100.0;
|
||||
auto A = (*(ps + 1) * invmax) * 255.0 - 128.0;
|
||||
auto B = (*(ps + 2) * invmax) * 255.0 - 128.0;
|
||||
|
||||
// converting LAB to XYZ (D65 illuminant)
|
||||
auto Y = (L + 16.0) / 116.0;
|
||||
auto X = A / 500.0 + Y;
|
||||
auto Z = Y - B / 200.0;
|
||||
auto Y = (L + 16.0) * (1.0 / 116.0);
|
||||
auto X = A * (1.0 / 500.0) + Y;
|
||||
auto Z = Y - B * (1.0 / 200.0);
|
||||
|
||||
// NOTE: use the constants of the illuminant of the target RGB color space
|
||||
X = finv(X) * 0.9504; // D50: * 0.9642
|
||||
@ -1057,7 +1109,15 @@ static bool LoadPSD(QDataStream &stream, const PSDHeader &header, QImage &img)
|
||||
QByteArray rawStride;
|
||||
rawStride.resize(raw_count);
|
||||
|
||||
if (header.color_mode == CM_CMYK || header.color_mode == CM_LABCOLOR || header.color_mode == CM_MULTICHANNEL) {
|
||||
// clang-format off
|
||||
// checks the need of color conversion (that requires random access to the image)
|
||||
auto randomAccess = (header.color_mode == CM_CMYK) ||
|
||||
(header.color_mode == CM_LABCOLOR) ||
|
||||
(header.color_mode == CM_MULTICHANNEL) ||
|
||||
(header.color_mode != CM_INDEXED && img.hasAlphaChannel());
|
||||
// clang-format on
|
||||
|
||||
if (randomAccess) {
|
||||
// In order to make a colorspace transformation, we need all channels of a scanline
|
||||
QByteArray psdScanline;
|
||||
psdScanline.resize(qsizetype(header.width * std::min(header.depth, quint16(16)) * header.channel_count + 7) / 8);
|
||||
@ -1077,31 +1137,56 @@ static bool LoadPSD(QDataStream &stream, const PSDHeader &header, QImage &img)
|
||||
auto scanLine = reinterpret_cast<unsigned char*>(psdScanline.data());
|
||||
if (header.depth == 8) {
|
||||
planarToChunchy<quint8>(scanLine, rawStride.data(), header.width, c, header.channel_count);
|
||||
}
|
||||
else if (header.depth == 16) {
|
||||
} else if (header.depth == 16) {
|
||||
planarToChunchy<quint16>(scanLine, rawStride.data(), header.width, c, header.channel_count);
|
||||
}
|
||||
else if (header.depth == 32) { // Not currently used
|
||||
} else if (header.depth == 32) {
|
||||
planarToChunchyFloat<quint32>(scanLine, rawStride.data(), header.width, c, header.channel_count);
|
||||
}
|
||||
}
|
||||
|
||||
// Convert premultiplied data to unassociated data
|
||||
if (img.hasAlphaChannel()) {
|
||||
if (header.color_mode == CM_CMYK) {
|
||||
if (header.depth == 8)
|
||||
premulConversion<quint8>(psdScanline.data(), header.width, 4, header.channel_count, PremulConversion::PS2A);
|
||||
else if (header.depth == 16)
|
||||
premulConversion<quint16>(psdScanline.data(), header.width, 4, header.channel_count, PremulConversion::PS2A);
|
||||
}
|
||||
if (header.color_mode == CM_LABCOLOR) {
|
||||
if (header.depth == 8)
|
||||
premulConversion<quint8>(psdScanline.data(), header.width, 3, header.channel_count, PremulConversion::PSLab2A);
|
||||
else if (header.depth == 16)
|
||||
premulConversion<quint16>(psdScanline.data(), header.width, 3, header.channel_count, PremulConversion::PSLab2A);
|
||||
}
|
||||
if (header.color_mode == CM_RGB) {
|
||||
if (header.depth == 8)
|
||||
premulConversion<quint8>(psdScanline.data(), header.width, 3, header.channel_count, PremulConversion::PS2P);
|
||||
else if (header.depth == 16 || header.depth == 32)
|
||||
premulConversion<quint16>(psdScanline.data(), header.width, 3, header.channel_count, PremulConversion::PS2P);
|
||||
}
|
||||
}
|
||||
|
||||
// Conversion to RGB
|
||||
if (header.color_mode == CM_CMYK || header.color_mode == CM_MULTICHANNEL) {
|
||||
if (header.depth == 8)
|
||||
cmykToRgb<quint8>(img.scanLine(y), imgChannels, psdScanline.data(), header.channel_count, header.width, alpha);
|
||||
else
|
||||
else if (header.depth == 16)
|
||||
cmykToRgb<quint16>(img.scanLine(y), imgChannels, psdScanline.data(), header.channel_count, header.width, alpha);
|
||||
}
|
||||
if (header.color_mode == CM_LABCOLOR) {
|
||||
if (header.depth == 8)
|
||||
labToRgb<quint8>(img.scanLine(y), imgChannels, psdScanline.data(), header.channel_count, header.width, alpha);
|
||||
else
|
||||
else if (header.depth == 16)
|
||||
labToRgb<quint16>(img.scanLine(y), imgChannels, psdScanline.data(), header.channel_count, header.width, alpha);
|
||||
}
|
||||
if (header.color_mode == CM_RGB) {
|
||||
if (header.depth == 8)
|
||||
rawChannelsCopy<quint8>(img.scanLine(y), imgChannels, psdScanline.data(), header.channel_count, header.width);
|
||||
else if (header.depth == 16 || header.depth == 32)
|
||||
rawChannelsCopy<quint16>(img.scanLine(y), imgChannels, psdScanline.data(), header.channel_count, header.width);
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// Linear read (no position jumps): optimized code usable only for the colorspaces supported by QImage
|
||||
for (qint32 c = 0; c < channel_num; ++c) {
|
||||
for (qint32 y = 0, h = header.height; y < h; ++y) {
|
||||
@ -1114,14 +1199,11 @@ static bool LoadPSD(QDataStream &stream, const PSDHeader &header, QImage &img)
|
||||
auto scanLine = img.scanLine(y);
|
||||
if (header.depth == 1) { // Bitmap
|
||||
monoInvert(scanLine, rawStride.data(), std::min(rawStride.size(), img.bytesPerLine()));
|
||||
}
|
||||
else if (header.depth == 8) { // 8-bits images: Indexed, Grayscale, RGB/RGBA
|
||||
} else if (header.depth == 8) { // 8-bits images: Indexed, Grayscale, RGB/RGBA
|
||||
planarToChunchy<quint8>(scanLine, rawStride.data(), header.width, c, imgChannels);
|
||||
}
|
||||
else if (header.depth == 16) { // 16-bits integer images: Grayscale, RGB/RGBA
|
||||
} else if (header.depth == 16) { // 16-bits integer images: Grayscale, RGB/RGBA
|
||||
planarToChunchy<quint16>(scanLine, rawStride.data(), header.width, c, imgChannels);
|
||||
}
|
||||
else if (header.depth == 32) { // 32-bits float images: Grayscale, RGB/RGBA (coverted to equivalent integer 16-bits)
|
||||
} else if (header.depth == 32) { // 32-bits float images: Grayscale, RGB/RGBA (coverted to equivalent integer 16-bits)
|
||||
planarToChunchyFloat<quint32>(scanLine, rawStride.data(), header.width, c, imgChannels);
|
||||
}
|
||||
}
|
||||
@ -1264,6 +1346,9 @@ bool PSDHandler::canRead(QIODevice *device)
|
||||
if (header.color_mode == CM_CMYK || header.color_mode == CM_LABCOLOR || header.color_mode == CM_MULTICHANNEL) {
|
||||
return false;
|
||||
}
|
||||
if (header.color_mode == CM_RGB && header.channel_count > 3) {
|
||||
return false; // supposing extra channel as alpha
|
||||
}
|
||||
}
|
||||
|
||||
return IsSupported(header);
|
||||
|
@ -45,7 +45,6 @@ const auto supported_formats = QSet<QByteArray>{
|
||||
"dcs", "dc2", "dcr", "dng", "drf", "dxo",
|
||||
"eip", "erf",
|
||||
"fff",
|
||||
"hdr",
|
||||
"iiq",
|
||||
"k25", "kc2", "kdc",
|
||||
"mdc", "mef", "mfw", "mos", "mrw",
|
||||
|
@ -7,7 +7,6 @@
|
||||
"dcs", "dc2", "dcr", "dng", "drf", "dxo",
|
||||
"eip", "erf",
|
||||
"fff",
|
||||
"hdr",
|
||||
"iiq",
|
||||
"k25", "kdc", "kc2",
|
||||
"mdc", "mef", "mfw", "mos", "mrw",
|
||||
@ -27,7 +26,6 @@
|
||||
"image/x-kodak-dcs", "image/x-dc2", "image/x-kodak-dcr", "image/x-adobe-dng", "image/x-drf", "image/x-dxo",
|
||||
"image/x-epson-eip", "image/x-epson-erf",
|
||||
"image/x-fff",
|
||||
"image/x-hdr",
|
||||
"image/x-iiq",
|
||||
"image/x-kodak-k25", "image/x-kodak-kdc", "image/x-kodak-kc2",
|
||||
"image/x-minolta-mdc", "image/x-mamiya-mef", "image/x-mfw", "image/x-aptus-mos", "image/x-minolta-mrw",
|
||||
|
@ -672,11 +672,16 @@ bool SGIImage::writeImage(const QImage &image)
|
||||
_dim = 3, _zsize = 3;
|
||||
}
|
||||
|
||||
if (img.format() == QImage::Format_ARGB32) {
|
||||
auto hasAlpha = img.hasAlphaChannel();
|
||||
if (hasAlpha) {
|
||||
_dim = 3, _zsize++;
|
||||
}
|
||||
|
||||
if (hasAlpha && img.format() != QImage::Format_ARGB32) {
|
||||
img = img.convertToFormat(QImage::Format_ARGB32);
|
||||
} else if (!hasAlpha && img.format() != QImage::Format_RGB32) {
|
||||
img = img.convertToFormat(QImage::Format_RGB32);
|
||||
}
|
||||
if (img.isNull()) {
|
||||
// qDebug() << "can't convert image to depth 32";
|
||||
return false;
|
||||
@ -685,7 +690,7 @@ bool SGIImage::writeImage(const QImage &image)
|
||||
const int w = img.width();
|
||||
const int h = img.height();
|
||||
|
||||
if (w > 65536 || h > 65536) {
|
||||
if (w > 65535 || h > 65535) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -712,12 +717,6 @@ bool SGIImage::writeImage(const QImage &image)
|
||||
rle_size += _rlevector[i]->size();
|
||||
}
|
||||
|
||||
// qDebug() << "minimum intensity: " << _pixmin;
|
||||
// qDebug() << "maximum intensity: " << _pixmax;
|
||||
// qDebug() << "saved scanlines: " << _numrows - _rlemap.size();
|
||||
// qDebug() << "total savings: " << (verbatim_size - rle_size) << " bytes";
|
||||
// qDebug() << "compression: " << (rle_size * 100.0 / verbatim_size) << '%';
|
||||
|
||||
if (verbatim_size <= rle_size) {
|
||||
writeVerbatim(img);
|
||||
} else {
|
||||
|
@ -428,8 +428,20 @@ bool TGAHandler::write(const QImage &image)
|
||||
QDataStream s(device());
|
||||
s.setByteOrder(QDataStream::LittleEndian);
|
||||
|
||||
const QImage &img = image;
|
||||
const bool hasAlpha = (img.format() == QImage::Format_ARGB32);
|
||||
QImage img(image);
|
||||
const bool hasAlpha = img.hasAlphaChannel();
|
||||
if (hasAlpha && img.format() != QImage::Format_ARGB32) {
|
||||
img = img.convertToFormat(QImage::Format_ARGB32);
|
||||
} else if (!hasAlpha && img.format() != QImage::Format_RGB32) {
|
||||
img = img.convertToFormat(QImage::Format_RGB32);
|
||||
}
|
||||
if (img.isNull()) {
|
||||
qDebug() << "TGAHandler::write: image conversion to 32 bits failed!";
|
||||
return false;
|
||||
}
|
||||
static constexpr quint8 originTopLeft = TGA_ORIGIN_UPPER + TGA_ORIGIN_LEFT; // 0x20
|
||||
static constexpr quint8 alphaChannel8Bits = 0x08;
|
||||
|
||||
for (int i = 0; i < 12; i++) {
|
||||
s << targaMagic[i];
|
||||
}
|
||||
@ -438,11 +450,12 @@ bool TGAHandler::write(const QImage &image)
|
||||
s << quint16(img.width()); // width
|
||||
s << quint16(img.height()); // height
|
||||
s << quint8(hasAlpha ? 32 : 24); // depth (24 bit RGB + 8 bit alpha)
|
||||
s << quint8(hasAlpha ? 0x24 : 0x20); // top left image (0x20) + 8 bit alpha (0x4)
|
||||
s << quint8(hasAlpha ? originTopLeft + alphaChannel8Bits : originTopLeft); // top left image (0x20) + 8 bit alpha (0x8)
|
||||
|
||||
for (int y = 0; y < img.height(); y++) {
|
||||
auto ptr = reinterpret_cast<QRgb *>(img.scanLine(y));
|
||||
for (int x = 0; x < img.width(); x++) {
|
||||
const QRgb color = img.pixel(x, y);
|
||||
auto color = *(ptr + x);
|
||||
s << quint8(qBlue(color));
|
||||
s << quint8(qGreen(color));
|
||||
s << quint8(qRed(color));
|
||||
|