| 1 | /* |
| 2 | Softimage PIC support for QImage. |
| 3 | |
| 4 | SPDX-FileCopyrightText: 1998 Halfdan Ingvarsson |
| 5 | SPDX-FileCopyrightText: 2007 Ruben Lopez <r.lopez@bren.es> |
| 6 | SPDX-FileCopyrightText: 2014 Alex Merry <alex.merry@kde.org> |
| 7 | |
| 8 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 9 | */ |
| 10 | |
| 11 | /* This code is based on the GIMP-PIC plugin by Halfdan Ingvarsson, |
| 12 | * and relicensed from GPL to LGPL to accommodate the KDE licensing policy |
| 13 | * with his permission. |
| 14 | */ |
| 15 | |
| 16 | #include "pic_p.h" |
| 17 | #include "rle_p.h" |
| 18 | #include "util_p.h" |
| 19 | |
| 20 | #include <QColorSpace> |
| 21 | #include <QDataStream> |
| 22 | #include <QDebug> |
| 23 | #include <QImage> |
| 24 | #include <QVariant> |
| 25 | |
| 26 | #include <algorithm> |
| 27 | #include <functional> |
| 28 | #include <qendian.h> |
| 29 | #include <utility> |
| 30 | |
| 31 | /** |
| 32 | * Reads a PIC file header from a data stream. |
| 33 | * |
| 34 | * @param s The data stream to read from. |
| 35 | * @param channels Where the read header will be stored. |
| 36 | * @returns @p s |
| 37 | * |
| 38 | * @relates PicHeader |
| 39 | */ |
| 40 | static QDataStream &(QDataStream &s, PicHeader &) |
| 41 | { |
| 42 | s.setFloatingPointPrecision(QDataStream::SinglePrecision); |
| 43 | s >> header.magic; |
| 44 | s >> header.version; |
| 45 | |
| 46 | // the comment should be truncated to the first null byte |
| 47 | char [81] = {}; |
| 48 | s.readRawData(comment, len: 80); |
| 49 | header.comment = QByteArray(comment); |
| 50 | |
| 51 | header.id.resize(size: 4); |
| 52 | const int bytesRead = s.readRawData(header.id.data(), len: 4); |
| 53 | if (bytesRead != 4) { |
| 54 | header.id.resize(size: bytesRead); |
| 55 | } |
| 56 | |
| 57 | s >> header.width; |
| 58 | s >> header.height; |
| 59 | s >> header.ratio; |
| 60 | qint16 fields; |
| 61 | s >> fields; |
| 62 | header.fields = static_cast<PicFields>(fields); |
| 63 | qint16 pad; |
| 64 | s >> pad; |
| 65 | return s; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Writes a PIC file header to a data stream. |
| 70 | * |
| 71 | * @param s The data stream to write to. |
| 72 | * @param channels The header to write. |
| 73 | * @returns @p s |
| 74 | * |
| 75 | * @relates PicHeader |
| 76 | */ |
| 77 | static QDataStream &(QDataStream &s, const PicHeader &) |
| 78 | { |
| 79 | s.setFloatingPointPrecision(QDataStream::SinglePrecision); |
| 80 | s << header.magic; |
| 81 | s << header.version; |
| 82 | |
| 83 | char [80] = {}; |
| 84 | strncpy(dest: comment, src: header.comment.constData(), n: sizeof(comment)); |
| 85 | s.writeRawData(comment, len: sizeof(comment)); |
| 86 | |
| 87 | char id[4] = {}; |
| 88 | strncpy(dest: id, src: header.id.constData(), n: sizeof(id)); |
| 89 | s.writeRawData(id, len: sizeof(id)); |
| 90 | |
| 91 | s << header.width; |
| 92 | s << header.height; |
| 93 | s << header.ratio; |
| 94 | s << quint16(header.fields); |
| 95 | s << quint16(0); |
| 96 | return s; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Reads a series of channel descriptions from a data stream. |
| 101 | * |
| 102 | * If the stream contains more than 8 channel descriptions, the status of @p s |
| 103 | * will be set to QDataStream::ReadCorruptData (note that more than 4 channels |
| 104 | * - one for each component - does not really make sense anyway). |
| 105 | * |
| 106 | * @param s The data stream to read from. |
| 107 | * @param channels The location to place the read channel descriptions; any |
| 108 | * existing entries will be cleared. |
| 109 | * @returns @p s |
| 110 | * |
| 111 | * @relates PicChannel |
| 112 | */ |
| 113 | static QDataStream &operator>>(QDataStream &s, QList<PicChannel> &channels) |
| 114 | { |
| 115 | const unsigned maxChannels = 8; |
| 116 | unsigned count = 0; |
| 117 | quint8 chained = 1; |
| 118 | channels.clear(); |
| 119 | while (chained && count < maxChannels && s.status() == QDataStream::Ok) { |
| 120 | PicChannel channel; |
| 121 | s >> chained; |
| 122 | s >> channel.size; |
| 123 | s >> channel.encoding; |
| 124 | s >> channel.code; |
| 125 | channels << channel; |
| 126 | ++count; |
| 127 | } |
| 128 | if (chained) { |
| 129 | // too many channels! |
| 130 | s.setStatus(QDataStream::ReadCorruptData); |
| 131 | } |
| 132 | return s; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Writes a series of channel descriptions to a data stream. |
| 137 | * |
| 138 | * Note that the corresponding read operation will not read more than 8 channel |
| 139 | * descriptions, although there should be no reason to have more than 4 channels |
| 140 | * anyway. |
| 141 | * |
| 142 | * @param s The data stream to write to. |
| 143 | * @param channels The channel descriptions to write. |
| 144 | * @returns @p s |
| 145 | * |
| 146 | * @relates PicChannel |
| 147 | */ |
| 148 | static QDataStream &operator<<(QDataStream &s, const QList<PicChannel> &channels) |
| 149 | { |
| 150 | Q_ASSERT(channels.size() > 0); |
| 151 | for (int i = 0; i < channels.size() - 1; ++i) { |
| 152 | s << quint8(1); // chained |
| 153 | s << channels[i].size; |
| 154 | s << quint8(channels[i].encoding); |
| 155 | s << channels[i].code; |
| 156 | } |
| 157 | s << quint8(0); // chained |
| 158 | s << channels.last().size; |
| 159 | s << quint8(channels.last().encoding); |
| 160 | s << channels.last().code; |
| 161 | return s; |
| 162 | } |
| 163 | |
| 164 | static bool readRow(QDataStream &stream, QRgb *row, quint16 width, const QList<PicChannel> &channels) |
| 165 | { |
| 166 | for (const PicChannel &channel : channels) { |
| 167 | auto readPixel = [&](QDataStream &str) -> QRgb { |
| 168 | quint8 red = 0; |
| 169 | if (channel.code & RED) { |
| 170 | str >> red; |
| 171 | } |
| 172 | quint8 green = 0; |
| 173 | if (channel.code & GREEN) { |
| 174 | str >> green; |
| 175 | } |
| 176 | quint8 blue = 0; |
| 177 | if (channel.code & BLUE) { |
| 178 | str >> blue; |
| 179 | } |
| 180 | quint8 alpha = 0; |
| 181 | if (channel.code & ALPHA) { |
| 182 | str >> alpha; |
| 183 | } |
| 184 | return qRgba(r: red, g: green, b: blue, a: alpha); |
| 185 | }; |
| 186 | auto updatePixel = [&](QRgb oldPixel, QRgb newPixel) -> QRgb { |
| 187 | return qRgba(r: qRed(rgb: (channel.code & RED) ? newPixel : oldPixel), |
| 188 | g: qGreen(rgb: (channel.code & GREEN) ? newPixel : oldPixel), |
| 189 | b: qBlue(rgb: (channel.code & BLUE) ? newPixel : oldPixel), |
| 190 | a: qAlpha(rgb: (channel.code & ALPHA) ? newPixel : oldPixel)); |
| 191 | }; |
| 192 | if (channel.encoding == MixedRLE) { |
| 193 | bool success = decodeRLEData(variant: RLEVariant::PIC, stream, dest: row, length: width, readData: readPixel, updateItem: updatePixel); |
| 194 | if (!success) { |
| 195 | qDebug() << "decodeRLEData failed" ; |
| 196 | return false; |
| 197 | } |
| 198 | } else if (channel.encoding == Uncompressed) { |
| 199 | for (quint16 i = 0; i < width; ++i) { |
| 200 | QRgb pixel = readPixel(stream); |
| 201 | row[i] = updatePixel(row[i], pixel); |
| 202 | } |
| 203 | } else { |
| 204 | // unknown encoding |
| 205 | qDebug() << "Unknown encoding" ; |
| 206 | return false; |
| 207 | } |
| 208 | } |
| 209 | if (stream.status() != QDataStream::Ok) { |
| 210 | qDebug() << "DataStream status was" << stream.status(); |
| 211 | } |
| 212 | return stream.status() == QDataStream::Ok; |
| 213 | } |
| 214 | |
| 215 | bool SoftimagePICHandler::canRead() const |
| 216 | { |
| 217 | if (!SoftimagePICHandler::canRead(device: device())) { |
| 218 | return false; |
| 219 | } |
| 220 | setFormat("pic" ); |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | bool SoftimagePICHandler::read(QImage *image) |
| 225 | { |
| 226 | if (!readChannels()) { |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | QImage::Format fmt = QImage::Format_RGB32; |
| 231 | for (const PicChannel &channel : std::as_const(t&: m_channels)) { |
| 232 | if (channel.size != 8) { |
| 233 | // we cannot read images that do not come in bytes |
| 234 | qDebug() << "Channel size was" << channel.size; |
| 235 | m_state = Error; |
| 236 | return false; |
| 237 | } |
| 238 | if (channel.code & ALPHA) { |
| 239 | fmt = QImage::Format_ARGB32; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | QImage img = imageAlloc(width: m_header.width, height: m_header.height, format: fmt); |
| 244 | if (img.isNull()) { |
| 245 | qDebug() << "Failed to allocate image, invalid dimensions?" << QSize(m_header.width, m_header.height) << fmt; |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | img.fill(pixel: qRgb(r: 0, g: 0, b: 0)); |
| 250 | |
| 251 | for (int y = 0; y < m_header.height; y++) { |
| 252 | QRgb *row = reinterpret_cast<QRgb *>(img.scanLine(y)); |
| 253 | if (!readRow(stream&: m_dataStream, row, width: m_header.width, channels: m_channels)) { |
| 254 | qDebug() << "readRow failed" ; |
| 255 | m_state = Error; |
| 256 | return false; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | *image = img; |
| 261 | m_state = Ready; |
| 262 | |
| 263 | return true; |
| 264 | } |
| 265 | |
| 266 | bool SoftimagePICHandler::write(const QImage &_image) |
| 267 | { |
| 268 | bool alpha = _image.hasAlphaChannel(); |
| 269 | QImage image; |
| 270 | #if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0) |
| 271 | auto cs = _image.colorSpace(); |
| 272 | if (cs.isValid() && cs.colorModel() == QColorSpace::ColorModel::Cmyk && _image.format() == QImage::Format_CMYK8888) { |
| 273 | image = _image.convertedToColorSpace(colorSpace: QColorSpace(QColorSpace::SRgb), format: QImage::Format_RGB32); |
| 274 | } |
| 275 | #endif |
| 276 | if (image.isNull()) { |
| 277 | image = _image.convertToFormat(f: alpha ? QImage::Format_ARGB32 : QImage::Format_RGB32); |
| 278 | } |
| 279 | |
| 280 | if (image.width() < 0 || image.height() < 0) { |
| 281 | qDebug() << "Image size invalid:" << image.width() << image.height(); |
| 282 | return false; |
| 283 | } |
| 284 | if (image.width() > 65535 || image.height() > 65535) { |
| 285 | qDebug() << "Image too big:" << image.width() << image.height(); |
| 286 | // there are only two bytes for each dimension |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | QDataStream stream(device()); |
| 291 | |
| 292 | stream << PicHeader(image.width(), image.height(), m_description); |
| 293 | |
| 294 | PicChannelEncoding encoding = m_compression ? MixedRLE : Uncompressed; |
| 295 | QList<PicChannel> channels; |
| 296 | channels << PicChannel(encoding, RED | GREEN | BLUE); |
| 297 | if (alpha) { |
| 298 | channels << PicChannel(encoding, ALPHA); |
| 299 | } |
| 300 | stream << channels; |
| 301 | |
| 302 | for (int r = 0; r < image.height(); r++) { |
| 303 | const QRgb *row = reinterpret_cast<const QRgb *>(image.constScanLine(r)); |
| 304 | |
| 305 | /* Write the RGB part of the scanline */ |
| 306 | auto rgbEqual = [](QRgb p1, QRgb p2) -> bool { |
| 307 | return qRed(rgb: p1) == qRed(rgb: p2) && qGreen(rgb: p1) == qGreen(rgb: p2) && qBlue(rgb: p1) == qBlue(rgb: p2); |
| 308 | }; |
| 309 | auto writeRgb = [](QDataStream &str, QRgb pixel) -> void { |
| 310 | str << quint8(qRed(rgb: pixel)) << quint8(qGreen(rgb: pixel)) << quint8(qBlue(rgb: pixel)); |
| 311 | }; |
| 312 | if (m_compression) { |
| 313 | encodeRLEData(variant: RLEVariant::PIC, stream, data: row, length: image.width(), itemsEqual: rgbEqual, writeItem: writeRgb); |
| 314 | } else { |
| 315 | for (int i = 0; i < image.width(); ++i) { |
| 316 | writeRgb(stream, row[i]); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /* Write the alpha channel */ |
| 321 | if (alpha) { |
| 322 | auto alphaEqual = [](QRgb p1, QRgb p2) -> bool { |
| 323 | return qAlpha(rgb: p1) == qAlpha(rgb: p2); |
| 324 | }; |
| 325 | auto writeAlpha = [](QDataStream &str, QRgb pixel) -> void { |
| 326 | str << quint8(qAlpha(rgb: pixel)); |
| 327 | }; |
| 328 | if (m_compression) { |
| 329 | encodeRLEData(variant: RLEVariant::PIC, stream, data: row, length: image.width(), itemsEqual: alphaEqual, writeItem: writeAlpha); |
| 330 | } else { |
| 331 | for (int i = 0; i < image.width(); ++i) { |
| 332 | writeAlpha(stream, row[i]); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | return stream.status() == QDataStream::Ok; |
| 338 | } |
| 339 | |
| 340 | bool SoftimagePICHandler::canRead(QIODevice *device) |
| 341 | { |
| 342 | char data[4]; |
| 343 | if (device->peek(data, maxlen: 4) != 4) { |
| 344 | return false; |
| 345 | } |
| 346 | return qFromBigEndian<qint32>(src: reinterpret_cast<uchar *>(data)) == PIC_MAGIC_NUMBER; |
| 347 | } |
| 348 | |
| 349 | bool SoftimagePICHandler::readHeader() |
| 350 | { |
| 351 | if (m_state == Ready) { |
| 352 | m_state = Error; |
| 353 | m_dataStream.setDevice(device()); |
| 354 | m_dataStream >> m_header; |
| 355 | if (m_header.isValid() && m_dataStream.status() == QDataStream::Ok) { |
| 356 | m_state = ReadHeader; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | return m_state != Error; |
| 361 | } |
| 362 | |
| 363 | bool SoftimagePICHandler::readChannels() |
| 364 | { |
| 365 | readHeader(); |
| 366 | if (m_state == ReadHeader) { |
| 367 | m_state = Error; |
| 368 | m_dataStream >> m_channels; |
| 369 | if (m_dataStream.status() == QDataStream::Ok) { |
| 370 | m_state = ReadChannels; |
| 371 | } |
| 372 | } |
| 373 | return m_state != Error; |
| 374 | } |
| 375 | |
| 376 | void SoftimagePICHandler::setOption(ImageOption option, const QVariant &value) |
| 377 | { |
| 378 | switch (option) { |
| 379 | case CompressionRatio: |
| 380 | m_compression = value.toBool(); |
| 381 | break; |
| 382 | case Description: { |
| 383 | m_description.clear(); |
| 384 | const QStringList entries = value.toString().split(QStringLiteral("\n\n" )); |
| 385 | for (const QString &entry : entries) { |
| 386 | if (entry.startsWith(QStringLiteral("Description: " ))) { |
| 387 | m_description = entry.mid(position: 13).simplified().toUtf8(); |
| 388 | } |
| 389 | } |
| 390 | break; |
| 391 | } |
| 392 | default: |
| 393 | break; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | QVariant SoftimagePICHandler::option(ImageOption option) const |
| 398 | { |
| 399 | const_cast<SoftimagePICHandler *>(this)->readHeader(); |
| 400 | switch (option) { |
| 401 | case Size: |
| 402 | if (const_cast<SoftimagePICHandler *>(this)->readHeader()) { |
| 403 | return QSize(m_header.width, m_header.height); |
| 404 | } else { |
| 405 | return QVariant(); |
| 406 | } |
| 407 | case CompressionRatio: |
| 408 | return m_compression; |
| 409 | case Description: |
| 410 | if (const_cast<SoftimagePICHandler *>(this)->readHeader()) { |
| 411 | QString descStr = QString::fromUtf8(ba: m_header.comment); |
| 412 | if (!descStr.isEmpty()) { |
| 413 | return QString(QStringLiteral("Description: " ) + descStr + QStringLiteral("\n\n" )); |
| 414 | } |
| 415 | } |
| 416 | return QString(); |
| 417 | case ImageFormat: |
| 418 | if (const_cast<SoftimagePICHandler *>(this)->readChannels()) { |
| 419 | for (const PicChannel &channel : std::as_const(t: m_channels)) { |
| 420 | if (channel.code & ALPHA) { |
| 421 | return QImage::Format_ARGB32; |
| 422 | } |
| 423 | } |
| 424 | return QImage::Format_RGB32; |
| 425 | } |
| 426 | return QVariant(); |
| 427 | default: |
| 428 | return QVariant(); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | bool SoftimagePICHandler::supportsOption(ImageOption option) const |
| 433 | { |
| 434 | return (option == CompressionRatio || option == Description || option == ImageFormat || option == Size); |
| 435 | } |
| 436 | |
| 437 | QImageIOPlugin::Capabilities SoftimagePICPlugin::capabilities(QIODevice *device, const QByteArray &format) const |
| 438 | { |
| 439 | if (format == "pic" ) { |
| 440 | return Capabilities(CanRead | CanWrite); |
| 441 | } |
| 442 | if (!format.isEmpty()) { |
| 443 | return {}; |
| 444 | } |
| 445 | if (!device->isOpen()) { |
| 446 | return {}; |
| 447 | } |
| 448 | |
| 449 | Capabilities cap; |
| 450 | if (device->isReadable() && SoftimagePICHandler::canRead(device)) { |
| 451 | cap |= CanRead; |
| 452 | } |
| 453 | if (device->isWritable()) { |
| 454 | cap |= CanWrite; |
| 455 | } |
| 456 | return cap; |
| 457 | } |
| 458 | |
| 459 | QImageIOHandler *SoftimagePICPlugin::create(QIODevice *device, const QByteArray &format) const |
| 460 | { |
| 461 | QImageIOHandler *handler = new SoftimagePICHandler(); |
| 462 | handler->setDevice(device); |
| 463 | handler->setFormat(format); |
| 464 | return handler; |
| 465 | } |
| 466 | |
| 467 | #include "moc_pic_p.cpp" |
| 468 | |