| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "private/qppmhandler_p.h" |
| 5 | |
| 6 | #ifndef QT_NO_IMAGEFORMAT_PPM |
| 7 | |
| 8 | #include <qdebug.h> |
| 9 | #include <qimage.h> |
| 10 | #include <qlist.h> |
| 11 | #include <qloggingcategory.h> |
| 12 | #include <qrgba64.h> |
| 13 | #include <qvariant.h> |
| 14 | #include <private/qlocale_p.h> |
| 15 | #include <private/qtools_p.h> |
| 16 | |
| 17 | QT_BEGIN_NAMESPACE |
| 18 | |
| 19 | Q_DECLARE_LOGGING_CATEGORY(lcImageIo) |
| 20 | |
| 21 | /***************************************************************************** |
| 22 | PBM/PGM/PPM (ASCII and RAW) image read/write functions |
| 23 | *****************************************************************************/ |
| 24 | |
| 25 | static void discard_pbm_line(QIODevice *d) |
| 26 | { |
| 27 | const int buflen = 100; |
| 28 | char buf[buflen]; |
| 29 | int res = 0; |
| 30 | do { |
| 31 | res = d->readLine(data: buf, maxlen: buflen); |
| 32 | } while (res > 0 && buf[res-1] != '\n'); |
| 33 | } |
| 34 | |
| 35 | static int read_pbm_int(QIODevice *d, bool *ok, int maxDigits = -1) |
| 36 | { |
| 37 | char c; |
| 38 | int val = -1; |
| 39 | bool digit; |
| 40 | bool hasOverflow = false; |
| 41 | for (;;) { |
| 42 | if (!d->getChar(c: &c)) // end of file |
| 43 | break; |
| 44 | digit = QtMiscUtils::isAsciiDigit(c); |
| 45 | if (val != -1) { |
| 46 | if (digit) { |
| 47 | const int cValue = c - '0'; |
| 48 | if (val <= (INT_MAX - cValue) / 10) { |
| 49 | val = 10*val + cValue; |
| 50 | } else { |
| 51 | hasOverflow = true; |
| 52 | } |
| 53 | if (maxDigits > 0 && --maxDigits == 0) |
| 54 | break; |
| 55 | continue; |
| 56 | } else { |
| 57 | if (c == '#') // comment |
| 58 | discard_pbm_line(d); |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | if (digit) // first digit |
| 63 | val = c - '0'; |
| 64 | else if (ascii_isspace(c)) |
| 65 | continue; |
| 66 | else if (c == '#') |
| 67 | discard_pbm_line(d); |
| 68 | else |
| 69 | break; |
| 70 | if (maxDigits > 0 && --maxDigits == 0) |
| 71 | break; |
| 72 | } |
| 73 | if (val < 0) |
| 74 | *ok = false; |
| 75 | return hasOverflow ? -1 : val; |
| 76 | } |
| 77 | |
| 78 | static bool (QIODevice *device, char& type, int& w, int& h, int& mcc) |
| 79 | { |
| 80 | char buf[3]; |
| 81 | if (device->read(data: buf, maxlen: 3) != 3) // read P[1-6]<white-space> |
| 82 | return false; |
| 83 | |
| 84 | if (!(buf[0] == 'P' && QtMiscUtils::isAsciiDigit(c: buf[1]) && ascii_isspace(c: buf[2]))) |
| 85 | return false; |
| 86 | |
| 87 | type = buf[1]; |
| 88 | if (type < '1' || type > '6') |
| 89 | return false; |
| 90 | |
| 91 | bool ok = true; |
| 92 | w = read_pbm_int(d: device, ok: &ok); // get image width |
| 93 | h = read_pbm_int(d: device, ok: &ok); // get image height |
| 94 | |
| 95 | if (type == '1' || type == '4') |
| 96 | mcc = 1; // ignore max color component |
| 97 | else |
| 98 | mcc = read_pbm_int(d: device, ok: &ok); // get max color component |
| 99 | |
| 100 | if (!ok || w <= 0 || w > 32767 || h <= 0 || h > 32767 || mcc <= 0 || mcc > 0xffff) |
| 101 | return false; // weird P.M image |
| 102 | |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | static inline QRgb scale_pbm_color(quint16 mx, quint16 rv, quint16 gv, quint16 bv) |
| 107 | { |
| 108 | return QRgba64::fromRgba64(red: (rv * 0xffffu) / mx, green: (gv * 0xffffu) / mx, blue: (bv * 0xffffu) / mx, alpha: 0xffff).toArgb32(); |
| 109 | } |
| 110 | |
| 111 | static bool read_pbm_body(QIODevice *device, char type, int w, int h, int mcc, QImage *outImage) |
| 112 | { |
| 113 | int nbits, y; |
| 114 | qsizetype pbm_bpl; |
| 115 | bool raw; |
| 116 | |
| 117 | QImage::Format format; |
| 118 | switch (type) { |
| 119 | case '1': // ascii PBM |
| 120 | case '4': // raw PBM |
| 121 | nbits = 1; |
| 122 | format = QImage::Format_Mono; |
| 123 | break; |
| 124 | case '2': // ascii PGM |
| 125 | case '5': // raw PGM |
| 126 | nbits = 8; |
| 127 | format = QImage::Format_Grayscale8; |
| 128 | break; |
| 129 | case '3': // ascii PPM |
| 130 | case '6': // raw PPM |
| 131 | nbits = 32; |
| 132 | format = QImage::Format_RGB32; |
| 133 | break; |
| 134 | default: |
| 135 | return false; |
| 136 | } |
| 137 | raw = type >= '4'; |
| 138 | |
| 139 | if (!QImageIOHandler::allocateImage(size: QSize(w, h), format, image: outImage)) |
| 140 | return false; |
| 141 | |
| 142 | pbm_bpl = (qsizetype(w) * nbits + 7) / 8; // bytes per scanline in PBM |
| 143 | |
| 144 | if (raw) { // read raw data |
| 145 | if (nbits == 32) { // type 6 |
| 146 | pbm_bpl = mcc < 256 ? 3*w : 6*w; |
| 147 | uchar *buf24 = new uchar[pbm_bpl], *b; |
| 148 | QRgb *p; |
| 149 | QRgb *end; |
| 150 | for (y=0; y<h; y++) { |
| 151 | if (device->read(data: (char *)buf24, maxlen: pbm_bpl) != pbm_bpl) { |
| 152 | delete[] buf24; |
| 153 | return false; |
| 154 | } |
| 155 | p = (QRgb *)outImage->scanLine(y); |
| 156 | end = p + w; |
| 157 | b = buf24; |
| 158 | while (p < end) { |
| 159 | if (mcc < 256) { |
| 160 | if (mcc == 255) |
| 161 | *p++ = qRgb(r: b[0],g: b[1],b: b[2]); |
| 162 | else |
| 163 | *p++ = scale_pbm_color(mx: mcc, rv: b[0], gv: b[1], bv: b[2]); |
| 164 | b += 3; |
| 165 | } else { |
| 166 | quint16 rv = b[0] << 8 | b[1]; |
| 167 | quint16 gv = b[2] << 8 | b[3]; |
| 168 | quint16 bv = b[4] << 8 | b[5]; |
| 169 | if (mcc == 0xffff) |
| 170 | *p++ = QRgba64::fromRgba64(red: rv, green: gv, blue: bv, alpha: 0xffff).toArgb32(); |
| 171 | else |
| 172 | *p++ = scale_pbm_color(mx: mcc, rv, gv, bv); |
| 173 | b += 6; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | delete[] buf24; |
| 178 | } else if (nbits == 8 && mcc > 255) { // type 5 16bit |
| 179 | pbm_bpl = 2*w; |
| 180 | uchar *buf16 = new uchar[pbm_bpl]; |
| 181 | for (y=0; y<h; y++) { |
| 182 | if (device->read(data: (char *)buf16, maxlen: pbm_bpl) != pbm_bpl) { |
| 183 | delete[] buf16; |
| 184 | return false; |
| 185 | } |
| 186 | uchar *p = outImage->scanLine(y); |
| 187 | uchar *end = p + w; |
| 188 | uchar *b = buf16; |
| 189 | while (p < end) { |
| 190 | *p++ = (b[0] << 8 | b[1]) * 255 / mcc; |
| 191 | b += 2; |
| 192 | } |
| 193 | } |
| 194 | delete[] buf16; |
| 195 | } else { // type 4,5 |
| 196 | for (y=0; y<h; y++) { |
| 197 | uchar *p = outImage->scanLine(y); |
| 198 | if (device->read(data: (char *)p, maxlen: pbm_bpl) != pbm_bpl) |
| 199 | return false; |
| 200 | if (nbits == 8 && mcc < 255) { |
| 201 | for (qsizetype i = 0; i < pbm_bpl; i++) |
| 202 | p[i] = (p[i] * 255) / mcc; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } else { // read ascii data |
| 207 | uchar *p; |
| 208 | qsizetype n; |
| 209 | bool ok = true; |
| 210 | for (y = 0; y < h && ok; y++) { |
| 211 | p = outImage->scanLine(y); |
| 212 | n = pbm_bpl; |
| 213 | if (nbits == 1) { |
| 214 | int b; |
| 215 | int bitsLeft = w; |
| 216 | while (n-- && ok) { |
| 217 | b = 0; |
| 218 | for (int i=0; i<8; i++) { |
| 219 | if (i < bitsLeft) |
| 220 | b = (b << 1) | (read_pbm_int(d: device, ok: &ok, maxDigits: 1) & 1); |
| 221 | else |
| 222 | b = (b << 1) | (0 & 1); // pad it our self if we need to |
| 223 | } |
| 224 | bitsLeft -= 8; |
| 225 | *p++ = b; |
| 226 | } |
| 227 | } else if (nbits == 8) { |
| 228 | if (mcc == 255) { |
| 229 | while (n-- && ok) { |
| 230 | *p++ = read_pbm_int(d: device, ok: &ok); |
| 231 | } |
| 232 | } else { |
| 233 | while (n-- && ok) { |
| 234 | *p++ = (read_pbm_int(d: device, ok: &ok) & 0xffff) * 255 / mcc; |
| 235 | } |
| 236 | } |
| 237 | } else { // 32 bits |
| 238 | n /= 4; |
| 239 | int r, g, b; |
| 240 | if (mcc == 255) { |
| 241 | while (n-- && ok) { |
| 242 | r = read_pbm_int(d: device, ok: &ok); |
| 243 | g = read_pbm_int(d: device, ok: &ok); |
| 244 | b = read_pbm_int(d: device, ok: &ok); |
| 245 | *((QRgb*)p) = qRgb(r, g, b); |
| 246 | p += 4; |
| 247 | } |
| 248 | } else { |
| 249 | while (n-- && ok) { |
| 250 | r = read_pbm_int(d: device, ok: &ok); |
| 251 | g = read_pbm_int(d: device, ok: &ok); |
| 252 | b = read_pbm_int(d: device, ok: &ok); |
| 253 | *((QRgb*)p) = scale_pbm_color(mx: mcc, rv: r, gv: g, bv: b); |
| 254 | p += 4; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | if (!ok) |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | if (format == QImage::Format_Mono) { |
| 264 | outImage->setColorCount(2); |
| 265 | outImage->setColor(i: 0, c: qRgb(r: 255,g: 255,b: 255)); // white |
| 266 | outImage->setColor(i: 1, c: qRgb(r: 0,g: 0,b: 0)); // black |
| 267 | } |
| 268 | |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | static bool write_pbm_image(QIODevice *out, const QImage &sourceImage, QByteArrayView sourceFormat) |
| 273 | { |
| 274 | QByteArray str; |
| 275 | QImage image = sourceImage; |
| 276 | const QByteArrayView format = sourceFormat.left(n: 3); // ignore RAW part |
| 277 | |
| 278 | bool gray = format == "pgm" ; |
| 279 | |
| 280 | if (format == "pbm" ) { |
| 281 | image = image.convertToFormat(f: QImage::Format_Mono); |
| 282 | } else if (gray) { |
| 283 | image = image.convertToFormat(f: QImage::Format_Grayscale8); |
| 284 | } else { |
| 285 | switch (image.format()) { |
| 286 | case QImage::Format_Mono: |
| 287 | case QImage::Format_MonoLSB: |
| 288 | image = image.convertToFormat(f: QImage::Format_Indexed8); |
| 289 | break; |
| 290 | case QImage::Format_Indexed8: |
| 291 | case QImage::Format_RGB32: |
| 292 | case QImage::Format_ARGB32: |
| 293 | break; |
| 294 | default: |
| 295 | if (image.hasAlphaChannel()) |
| 296 | image = image.convertToFormat(f: QImage::Format_ARGB32); |
| 297 | else |
| 298 | image = image.convertToFormat(f: QImage::Format_RGB32); |
| 299 | break; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | if (image.depth() == 1 && image.colorCount() == 2) { |
| 304 | if (qGray(rgb: image.color(i: 0)) < qGray(rgb: image.color(i: 1))) { |
| 305 | // 0=dark/black, 1=light/white - invert |
| 306 | image.detach(); |
| 307 | for (int y=0; y<image.height(); y++) { |
| 308 | uchar *p = image.scanLine(y); |
| 309 | uchar *end = p + image.bytesPerLine(); |
| 310 | while (p < end) |
| 311 | *p++ ^= 0xff; |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | uint w = image.width(); |
| 317 | uint h = image.height(); |
| 318 | |
| 319 | str = "P\n" ; |
| 320 | str += QByteArray::number(w); |
| 321 | str += ' '; |
| 322 | str += QByteArray::number(h); |
| 323 | str += '\n'; |
| 324 | |
| 325 | switch (image.depth()) { |
| 326 | case 1: { |
| 327 | str.insert(i: 1, c: '4'); |
| 328 | if (out->write(data: str, len: str.size()) != str.size()) |
| 329 | return false; |
| 330 | w = (w+7)/8; |
| 331 | for (uint y=0; y<h; y++) { |
| 332 | uchar* line = image.scanLine(y); |
| 333 | if (w != (uint)out->write(data: (char*)line, len: w)) |
| 334 | return false; |
| 335 | } |
| 336 | } |
| 337 | break; |
| 338 | |
| 339 | case 8: { |
| 340 | str.insert(i: 1, c: gray ? '5' : '6'); |
| 341 | str.append(s: "255\n" ); |
| 342 | if (out->write(data: str, len: str.size()) != str.size()) |
| 343 | return false; |
| 344 | qsizetype bpl = qsizetype(w) * (gray ? 1 : 3); |
| 345 | uchar *buf = new uchar[bpl]; |
| 346 | if (image.format() == QImage::Format_Indexed8) { |
| 347 | const QList<QRgb> color = image.colorTable(); |
| 348 | for (uint y=0; y<h; y++) { |
| 349 | const uchar *b = image.constScanLine(y); |
| 350 | uchar *p = buf; |
| 351 | uchar *end = buf+bpl; |
| 352 | if (gray) { |
| 353 | while (p < end) { |
| 354 | uchar g = (uchar)qGray(rgb: color[*b++]); |
| 355 | *p++ = g; |
| 356 | } |
| 357 | } else { |
| 358 | while (p < end) { |
| 359 | QRgb rgb = color[*b++]; |
| 360 | *p++ = qRed(rgb); |
| 361 | *p++ = qGreen(rgb); |
| 362 | *p++ = qBlue(rgb); |
| 363 | } |
| 364 | } |
| 365 | if (bpl != (qsizetype)out->write(data: (char*)buf, len: bpl)) |
| 366 | return false; |
| 367 | } |
| 368 | } else { |
| 369 | for (uint y=0; y<h; y++) { |
| 370 | const uchar *b = image.constScanLine(y); |
| 371 | uchar *p = buf; |
| 372 | uchar *end = buf + bpl; |
| 373 | if (gray) { |
| 374 | while (p < end) |
| 375 | *p++ = *b++; |
| 376 | } else { |
| 377 | while (p < end) { |
| 378 | uchar color = *b++; |
| 379 | *p++ = color; |
| 380 | *p++ = color; |
| 381 | *p++ = color; |
| 382 | } |
| 383 | } |
| 384 | if (bpl != (qsizetype)out->write(data: (char*)buf, len: bpl)) |
| 385 | return false; |
| 386 | } |
| 387 | } |
| 388 | delete [] buf; |
| 389 | break; |
| 390 | } |
| 391 | |
| 392 | case 32: { |
| 393 | str.insert(i: 1, c: '6'); |
| 394 | str.append(s: "255\n" ); |
| 395 | if (out->write(data: str, len: str.size()) != str.size()) |
| 396 | return false; |
| 397 | qsizetype bpl = qsizetype(w) * 3; |
| 398 | uchar *buf = new uchar[bpl]; |
| 399 | for (uint y=0; y<h; y++) { |
| 400 | const QRgb *b = reinterpret_cast<const QRgb *>(image.constScanLine(y)); |
| 401 | uchar *p = buf; |
| 402 | uchar *end = buf+bpl; |
| 403 | while (p < end) { |
| 404 | QRgb rgb = *b++; |
| 405 | *p++ = qRed(rgb); |
| 406 | *p++ = qGreen(rgb); |
| 407 | *p++ = qBlue(rgb); |
| 408 | } |
| 409 | if (bpl != (qsizetype)out->write(data: (char*)buf, len: bpl)) |
| 410 | return false; |
| 411 | } |
| 412 | delete [] buf; |
| 413 | break; |
| 414 | } |
| 415 | |
| 416 | default: |
| 417 | return false; |
| 418 | } |
| 419 | |
| 420 | return true; |
| 421 | } |
| 422 | |
| 423 | QPpmHandler::QPpmHandler() |
| 424 | : state(Ready) |
| 425 | { |
| 426 | } |
| 427 | |
| 428 | bool QPpmHandler::readHeader() |
| 429 | { |
| 430 | state = Error; |
| 431 | if (!read_pbm_header(device: device(), type, w&: width, h&: height, mcc)) |
| 432 | return false; |
| 433 | state = ReadHeader; |
| 434 | return true; |
| 435 | } |
| 436 | |
| 437 | bool QPpmHandler::canRead() const |
| 438 | { |
| 439 | if (state == Ready && !canRead(device: device(), subType: &subType)) |
| 440 | return false; |
| 441 | |
| 442 | if (state != Error) { |
| 443 | setFormat(subType); |
| 444 | return true; |
| 445 | } |
| 446 | |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | bool QPpmHandler::canRead(QIODevice *device, QByteArray *subType) |
| 451 | { |
| 452 | if (!device) { |
| 453 | qCWarning(lcImageIo, "QPpmHandler::canRead() called with no device" ); |
| 454 | return false; |
| 455 | } |
| 456 | |
| 457 | char head[2]; |
| 458 | if (device->peek(data: head, maxlen: sizeof(head)) != sizeof(head)) |
| 459 | return false; |
| 460 | |
| 461 | if (head[0] != 'P') |
| 462 | return false; |
| 463 | |
| 464 | if (head[1] == '1' || head[1] == '4') { |
| 465 | if (subType) |
| 466 | *subType = "pbm" ; |
| 467 | } else if (head[1] == '2' || head[1] == '5') { |
| 468 | if (subType) |
| 469 | *subType = "pgm" ; |
| 470 | } else if (head[1] == '3' || head[1] == '6') { |
| 471 | if (subType) |
| 472 | *subType = "ppm" ; |
| 473 | } else { |
| 474 | return false; |
| 475 | } |
| 476 | return true; |
| 477 | } |
| 478 | |
| 479 | bool QPpmHandler::read(QImage *image) |
| 480 | { |
| 481 | if (state == Error) |
| 482 | return false; |
| 483 | |
| 484 | if (state == Ready && !readHeader()) { |
| 485 | state = Error; |
| 486 | return false; |
| 487 | } |
| 488 | |
| 489 | if (!read_pbm_body(device: device(), type, w: width, h: height, mcc, outImage: image)) { |
| 490 | state = Error; |
| 491 | return false; |
| 492 | } |
| 493 | |
| 494 | state = Ready; |
| 495 | return true; |
| 496 | } |
| 497 | |
| 498 | bool QPpmHandler::write(const QImage &image) |
| 499 | { |
| 500 | return write_pbm_image(out: device(), sourceImage: image, sourceFormat: subType); |
| 501 | } |
| 502 | |
| 503 | bool QPpmHandler::supportsOption(ImageOption option) const |
| 504 | { |
| 505 | return option == SubType |
| 506 | || option == Size |
| 507 | || option == ImageFormat; |
| 508 | } |
| 509 | |
| 510 | QVariant QPpmHandler::option(ImageOption option) const |
| 511 | { |
| 512 | if (option == SubType) { |
| 513 | return subType; |
| 514 | } else if (option == Size) { |
| 515 | if (state == Error) |
| 516 | return QVariant(); |
| 517 | if (state == Ready && !const_cast<QPpmHandler*>(this)->readHeader()) |
| 518 | return QVariant(); |
| 519 | return QSize(width, height); |
| 520 | } else if (option == ImageFormat) { |
| 521 | if (state == Error) |
| 522 | return QVariant(); |
| 523 | if (state == Ready && !const_cast<QPpmHandler*>(this)->readHeader()) |
| 524 | return QVariant(); |
| 525 | QImage::Format format = QImage::Format_Invalid; |
| 526 | switch (type) { |
| 527 | case '1': // ascii PBM |
| 528 | case '4': // raw PBM |
| 529 | format = QImage::Format_Mono; |
| 530 | break; |
| 531 | case '2': // ascii PGM |
| 532 | case '5': // raw PGM |
| 533 | format = QImage::Format_Grayscale8; |
| 534 | break; |
| 535 | case '3': // ascii PPM |
| 536 | case '6': // raw PPM |
| 537 | format = QImage::Format_RGB32; |
| 538 | break; |
| 539 | default: |
| 540 | break; |
| 541 | } |
| 542 | return format; |
| 543 | } |
| 544 | return QVariant(); |
| 545 | } |
| 546 | |
| 547 | void QPpmHandler::setOption(ImageOption option, const QVariant &value) |
| 548 | { |
| 549 | if (option == SubType) |
| 550 | subType = value.toByteArray().toLower(); |
| 551 | } |
| 552 | |
| 553 | QT_END_NAMESPACE |
| 554 | |
| 555 | #endif // QT_NO_IMAGEFORMAT_PPM |
| 556 | |