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