320 if (!TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width)
321 || !TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height)
322 || !TIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &
photometric)) {
326 size = QSize(width, height);
328 uint16_t orientationTag;
329 if (TIFFGetField(tiff, TIFFTAG_ORIENTATION, &orientationTag))
330 transformation = exif2Qt(orientationTag);
333 uint16_t bitPerSample;
334 if (!TIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &bitPerSample))
336 uint16_t samplesPerPixel;
337 if (!TIFFGetField(tiff, TIFFTAG_SAMPLESPERPIXEL, &samplesPerPixel))
339 uint16_t sampleFormat;
340 if (!TIFFGetField(tiff, TIFFTAG_SAMPLEFORMAT, &sampleFormat))
341 sampleFormat = SAMPLEFORMAT_VOID;
344 uint16_t planarConfig;
345 if (!TIFFGetField(tiff, TIFFTAG_PLANARCONFIG, &planarConfig))
346 planarConfig = PLANARCONFIG_CONTIG;
347 separatePlanes = planarConfig == PLANARCONFIG_SEPARATE && samplesPerPixel > 1;
351 if (
grayscale && bitPerSample == 1 && samplesPerPixel == 1)
352 format = QImage::Format_Mono;
353 else if (
photometric == PHOTOMETRIC_MINISBLACK && bitPerSample == 8 && samplesPerPixel == 1)
354 format = QImage::Format_Grayscale8;
356 format = QImage::Format_Grayscale16;
357 else if ((
grayscale ||
photometric == PHOTOMETRIC_PALETTE) && bitPerSample == 8 && samplesPerPixel == 1)
358 format = QImage::Format_Indexed8;
359 else if (samplesPerPixel < 4) {
360 bool regular = (samplesPerPixel != 2) && (
photometric == PHOTOMETRIC_RGB ||
photometric == PHOTOMETRIC_MINISBLACK);
361 if (bitPerSample == 16 && regular)
362 format = floatingPoint ? QImage::Format_RGBX16FPx4 : QImage::Format_RGBX64;
364 format = QImage::Format_RGBX32FPx4;
366 format = QImage::Format_RGB32;
369 uint16_t *extrasamples;
374 bool premultiplied =
true;
375 bool gotField = TIFFGetField(tiff, TIFFTAG_EXTRASAMPLES, &count, &extrasamples);
376 if (!gotField || !count || extrasamples[0] == EXTRASAMPLE_UNSPECIFIED)
377 premultiplied =
false;
379 if (bitPerSample == 16 &&
photometric == PHOTOMETRIC_RGB) {
381 if (gotField && count && extrasamples[0] == EXTRASAMPLE_UNASSALPHA)
382 premultiplied =
false;
384 format = floatingPoint ? QImage::Format_RGBA16FPx4_Premultiplied : QImage::Format_RGBA64_Premultiplied;
386 format = floatingPoint ? QImage::Format_RGBA16FPx4 : QImage::Format_RGBA64;
388 if (gotField && count && extrasamples[0] == EXTRASAMPLE_UNASSALPHA)
389 premultiplied =
false;
391 format = QImage::Format_RGBA32FPx4_Premultiplied;
393 format = QImage::Format_RGBA32FPx4;
394 }
else if (samplesPerPixel == 4 && bitPerSample == 8 &&
photometric == PHOTOMETRIC_SEPARATED) {
396 const bool gotInkSetField = TIFFGetField(tiff, TIFFTAG_INKSET, &inkSet);
397 if (!gotInkSetField || inkSet == INKSET_CMYK) {
398 format = QImage::Format_CMYK8888;
405 format = QImage::Format_ARGB32_Premultiplied;
407 format = QImage::Format_ARGB32;
453 if (!QImageIOHandler::allocateImage(size, format, image))
457 if (TIFFIsTiled(tiff) && TIFFTileSize64(tiff) > uint64_t(image->sizeInBytes()))
460 const quint32 width = size.width();
461 const quint32 height = size.height();
464 if (format == QImage::Format_Mono || format == QImage::Format_Indexed8) {
465 if (format == QImage::Format_Mono) {
466 QList<QRgb> colortable(2);
467 if (photometric == PHOTOMETRIC_MINISBLACK) {
468 colortable[0] = 0xff000000;
469 colortable[1] = 0xffffffff;
471 colortable[0] = 0xffffffff;
472 colortable[1] = 0xff000000;
474 image->setColorTable(colortable);
475 }
else if (format == QImage::Format_Indexed8) {
476 const uint16_t tableSize = 256;
477 QList<QRgb> qtColorTable(tableSize);
479 for (
int i = 0; i<tableSize; ++i) {
480 const int c = (photometric == PHOTOMETRIC_MINISBLACK) ? i : (255 - i);
481 qtColorTable[i] = qRgb(c, c, c);
485 uint16_t *redTable = 0;
486 uint16_t *greenTable = 0;
487 uint16_t *blueTable = 0;
488 if (!TIFFGetField(tiff, TIFFTAG_COLORMAP, &redTable, &greenTable, &blueTable))
490 if (!redTable || !greenTable || !blueTable)
493 for (
int i = 0; i<tableSize ;++i) {
495 const int red = redTable[i] >> 8;
496 const int green = greenTable[i] >> 8;
497 const int blue = blueTable[i] >> 8;
498 qtColorTable[i] = qRgb(red, green, blue);
501 image->setColorTable(qtColorTable);
505 bool format8bit = (format == QImage::Format_Mono || format == QImage::Format_Indexed8 || format == QImage::Format_Grayscale8);
506 bool format16bit = (format == QImage::Format_Grayscale16);
507 bool formatCmyk32bit = (format == QImage::Format_CMYK8888);
508 bool format64bit = (format == QImage::Format_RGBX64 || format == QImage::Format_RGBA64 || format == QImage::Format_RGBA64_Premultiplied);
509 bool format64fp = (format == QImage::Format_RGBX16FPx4 || format == QImage::Format_RGBA16FPx4 || format == QImage::Format_RGBA16FPx4_Premultiplied);
510 bool format128fp = (format == QImage::Format_RGBX32FPx4 || format == QImage::Format_RGBA32FPx4 || format == QImage::Format_RGBA32FPx4_Premultiplied);
513 if (format8bit || format16bit || formatCmyk32bit || format64bit || format64fp || format128fp) {
518 int bytesPerPixel = image->depth() / 8;
519 if (format == QImage::Format_RGBX64 || format == QImage::Format_RGBX16FPx4)
520 bytesPerPixel = photometric == PHOTOMETRIC_RGB ? 6 : 2;
521 else if (format == QImage::Format_RGBX32FPx4)
522 bytesPerPixel = photometric == PHOTOMETRIC_RGB ? 12 : 4;
524 if (TIFFIsTiled(tiff)) {
525 quint32 tileWidth, tileLength;
526 if (!TIFFGetField(tiff, TIFFTAG_TILEWIDTH, &tileWidth)
527 || !TIFFGetField(tiff, TIFFTAG_TILELENGTH, &tileLength)
528 || !tileWidth || !tileLength || tileWidth % 16 || tileLength % 16)
532 quint32 byteWidth = (format == QImage::Format_Mono) ? (width + 7)/8 : (width * bytesPerPixel);
533 quint32 byteTileWidth = (format == QImage::Format_Mono) ? tileWidth/8 : (tileWidth * bytesPerPixel);
534 tmsize_t byteTileSize = TIFFTileSize(tiff);
535 if (byteTileSize > image->sizeInBytes() || byteTileSize / tileLength < byteTileWidth)
537 uchar *buf = (uchar *)_TIFFmalloc(byteTileSize);
540 for (quint32 y = 0; y < height; y += tileLength) {
541 for (quint32 x = 0; x < width; x += tileWidth) {
542 if (TIFFReadTile(tiff, buf, x, y, 0, 0) < 0) {
546 quint32 linesToCopy = qMin(tileLength, height - y);
547 quint32 byteOffset = (format == QImage::Format_Mono) ? x/8 : (x * bytesPerPixel);
548 quint32 widthToCopy = qMin(byteTileWidth, byteWidth - byteOffset);
549 for (quint32 i = 0; i < linesToCopy; i++) {
550 ::memcpy(image->scanLine(y + i) + byteOffset, buf + (i * byteTileWidth), widthToCopy);
556 if (image->bytesPerLine() < TIFFScanlineSize(tiff))
558 for (uint32_t y=0; y<height; ++y) {
559 if (TIFFReadScanline(tiff, image->scanLine(y), y, 0) < 0)
563 if (format == QImage::Format_RGBX64 || format == QImage::Format_RGBX16FPx4) {
564 if (photometric == PHOTOMETRIC_RGB)
568 }
else if (format == QImage::Format_RGBX32FPx4) {
569 if (photometric == PHOTOMETRIC_RGB)
575 const int stopOnError = 1;
576 if (TIFFReadRGBAImageOriented(tiff, width, height,
reinterpret_cast<uint32_t *>(image->bits()),
577 qt2Exif(transformation), stopOnError))
579 for (uint32_t y=0; y<height; ++y)
580 convert32BitOrder(image->scanLine(y), width);
590 if (!TIFFGetField(tiff, TIFFTAG_RESOLUTIONUNIT, &resUnit))
591 resUnit = RESUNIT_INCH;
593 constexpr float maxRes = 20000000;
594 if (TIFFGetField(tiff, TIFFTAG_XRESOLUTION, &resX)
595 && TIFFGetField(tiff, TIFFTAG_YRESOLUTION, &resY)
596 && resX > 0 && resX <= maxRes && resY > 0 && resY <= maxRes) {
599 case RESUNIT_CENTIMETER:
600 image->setDotsPerMeterX(qRound(resX * 100));
601 image->setDotsPerMeterY(qRound(resY * 100));
604 image->setDotsPerMeterX(qRound(resX * (100 / 2.54)));
605 image->setDotsPerMeterY(qRound(resY * (100 / 2.54)));
616 if (TIFFGetField(tiff, TIFFTAG_ICCPROFILE, &count, &profile)) {
617 QByteArray iccProfile(
reinterpret_cast<
const char *>(profile), count);
618 image->setColorSpace(QColorSpace::fromIccProfile(iccProfile));
679 const auto tiff = d->openInternal(
"wB", device());
684 const auto writeScanline = [&](
const void *line,
int y) {
685 return TIFFWriteScanline(tiff.get(),
const_cast<
void*>(line), y);
688 const auto setField = [&] (uint32_t tag,
auto&&...args) {
689 return TIFFSetField(tiff.get(), tag,
std::forward<
decltype(args)>(args)...);
692 const int width = image.width();
693 const int height = image.height();
695 if (!setField(TIFFTAG_IMAGEWIDTH, width)
696 || !setField(TIFFTAG_IMAGELENGTH, height)
697 || !setField(TIFFTAG_COMPRESSION, toLibTiffCompression(d->compression))
698 || !setField(TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG))
704 bool resolutionSet =
false;
705 const int dotPerMeterX = image.dotsPerMeterX();
706 const int dotPerMeterY = image.dotsPerMeterY();
707 if ((dotPerMeterX % 100) == 0
708 && (dotPerMeterY % 100) == 0) {
709 resolutionSet = setField(TIFFTAG_RESOLUTIONUNIT, RESUNIT_CENTIMETER)
710 && setField(TIFFTAG_XRESOLUTION, dotPerMeterX/100.0)
711 && setField(TIFFTAG_YRESOLUTION, dotPerMeterY/100.0);
713 resolutionSet = setField(TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH)
714 && setField(TIFFTAG_XRESOLUTION,
static_cast<
float>(image.logicalDpiX()))
715 && setField(TIFFTAG_YRESOLUTION,
static_cast<
float>(image.logicalDpiY()));
721 if (!setField(TIFFTAG_ORIENTATION,
qt2Exif(d->transformation)))
725 const QByteArray iccProfile = image.colorSpace().iccProfile();
726 if (!iccProfile.isEmpty()) {
727 const auto size =
static_cast<uint32_t>(iccProfile.size());
728 if (!q20::cmp_equal(size, iccProfile.size())
729 || !setField(TIFFTAG_ICCPROFILE, size, iccProfile.data()))
736 const QImage::Format format = image.format();
737 if (format == QImage::Format_Mono || format == QImage::Format_MonoLSB) {
738 uint16_t photometric = PHOTOMETRIC_MINISBLACK;
739 if (image.colorTable().value(0) == 0xffffffff)
740 photometric = PHOTOMETRIC_MINISWHITE;
741 if (!setField(TIFFTAG_PHOTOMETRIC, photometric)
742 || !setField(TIFFTAG_BITSPERSAMPLE, 1)
743 || !setField(TIFFTAG_ROWSPERSTRIP, defaultStripSize(tiff)))
749 const int chunks =
int(image.sizeInBytes() / (1024 * 1024 * 16)) + 1;
750 const int chunkHeight = qMax(height / chunks, 1);
754 QImage chunk = image.copy(0, y, width, qMin(chunkHeight, height - y)).convertToFormat(QImage::Format_Mono);
757 int chunkEnd = y + chunk.height();
758 while (y < chunkEnd) {
759 if (writeScanline(chunk.scanLine(y - chunkStart), y) != 1)
764 }
else if (format == QImage::Format_Indexed8
765 || format == QImage::Format_Grayscale8
766 || format == QImage::Format_Grayscale16
767 || format == QImage::Format_Alpha8) {
768 QList<QRgb> colorTable = effectiveColorTable(image);
769 bool isGrayscale = checkGrayscale(colorTable);
771 uint16_t photometric = PHOTOMETRIC_MINISBLACK;
772 if (colorTable.at(0) == 0xffffffff)
773 photometric = PHOTOMETRIC_MINISWHITE;
774 if (!setField(TIFFTAG_PHOTOMETRIC, photometric)
775 || !setField(TIFFTAG_BITSPERSAMPLE, image.depth())
776 || !setField(TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT)
777 || !setField(TIFFTAG_ROWSPERSTRIP, defaultStripSize(tiff)))
782 if (!setField(TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE)
783 || !setField(TIFFTAG_BITSPERSAMPLE, 8)
784 || !setField(TIFFTAG_ROWSPERSTRIP, defaultStripSize(tiff)))
789 constexpr qsizetype colorMapSize = 256;
790 const int tableSize =
int(qMin(colorTable.size(), colorMapSize));
791 QVarLengthArray<uint16_t> redTable(colorMapSize, 0);
792 QVarLengthArray<uint16_t> greenTable(colorMapSize, 0);
793 QVarLengthArray<uint16_t> blueTable(colorMapSize, 0);
796 for (
int i = 0; i<tableSize; ++i) {
797 const QRgb color = colorTable.at(i);
798 redTable[i] = qRed(color) * 257;
799 greenTable[i] = qGreen(color) * 257;
800 blueTable[i] = qBlue(color) * 257;
803 if (!setField(TIFFTAG_COLORMAP, redTable.data(), greenTable.data(), blueTable.data()))
808 for (
int y = 0; y < height; ++y) {
809 if (writeScanline(image.scanLine(y), y) != 1)
812 }
else if (format == QImage::Format_RGBX64 || format == QImage::Format_RGBX16FPx4) {
813 if (!setField(TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB)
814 || !setField(TIFFTAG_SAMPLESPERPIXEL, 3)
815 || !setField(TIFFTAG_BITSPERSAMPLE, 16)
816 || !setField(TIFFTAG_SAMPLEFORMAT,
817 format == QImage::Format_RGBX64
819 : SAMPLEFORMAT_IEEEFP)
820 || !setField(TIFFTAG_ROWSPERSTRIP, defaultStripSize(tiff)))
824 std::unique_ptr<quint16[]> rgb48line(
new quint16[width * 3]);
825 for (
int y = 0; y < height; ++y) {
826 const quint16 *srcLine =
reinterpret_cast<
const quint16 *>(image.constScanLine(y));
827 for (
int x = 0; x < width; ++x) {
828 rgb48line[x * 3 + 0] = srcLine[x * 4 + 0];
829 rgb48line[x * 3 + 1] = srcLine[x * 4 + 1];
830 rgb48line[x * 3 + 2] = srcLine[x * 4 + 2];
833 if (writeScanline(rgb48line.get(), y) != 1)
836 }
else if (format == QImage::Format_RGBA64
837 || format == QImage::Format_RGBA64_Premultiplied) {
838 const bool premultiplied = image.format() != QImage::Format_RGBA64;
839 const uint16_t extrasamples = premultiplied ? EXTRASAMPLE_ASSOCALPHA : EXTRASAMPLE_UNASSALPHA;
840 if (!setField(TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB)
841 || !setField(TIFFTAG_SAMPLESPERPIXEL, 4)
842 || !setField(TIFFTAG_BITSPERSAMPLE, 16)
843 || !setField(TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT)
844 || !setField(TIFFTAG_EXTRASAMPLES, 1, &extrasamples)
845 || !setField(TIFFTAG_ROWSPERSTRIP, defaultStripSize(tiff)))
849 for (
int y = 0; y < height; ++y) {
850 if (writeScanline(image.scanLine(y), y) != 1)
853 }
else if (format == QImage::Format_RGBX32FPx4) {
854 if (!setField(TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB)
855 || !setField(TIFFTAG_SAMPLESPERPIXEL, 3)
856 || !setField(TIFFTAG_BITSPERSAMPLE, 32)
857 || !setField(TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP)
858 || !setField(TIFFTAG_ROWSPERSTRIP, defaultStripSize(tiff)))
862 std::unique_ptr<
float[]> line(
new float[width * 3]);
863 for (
int y = 0; y < height; ++y) {
864 const float *srcLine =
reinterpret_cast<
const float *>(image.constScanLine(y));
865 for (
int x = 0; x < width; ++x) {
866 line[x * 3 + 0] = srcLine[x * 4 + 0];
867 line[x * 3 + 1] = srcLine[x * 4 + 1];
868 line[x * 3 + 2] = srcLine[x * 4 + 2];
871 if (writeScanline(line.get(), y) != 1)
874 }
else if (format == QImage::Format_RGBA16FPx4 || format == QImage::Format_RGBA32FPx4
875 || format == QImage::Format_RGBA16FPx4_Premultiplied
876 || format == QImage::Format_RGBA32FPx4_Premultiplied) {
877 const bool premultiplied = image.format() != QImage::Format_RGBA16FPx4 && image.format() != QImage::Format_RGBA32FPx4;
878 const uint16_t extrasamples = premultiplied ? EXTRASAMPLE_ASSOCALPHA : EXTRASAMPLE_UNASSALPHA;
879 if (!setField(TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB)
880 || !setField(TIFFTAG_SAMPLESPERPIXEL, 4)
881 || !setField(TIFFTAG_BITSPERSAMPLE, image.depth() == 64 ? 16 : 32)
882 || !setField(TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP)
883 || !setField(TIFFTAG_EXTRASAMPLES, 1, &extrasamples)
884 || !setField(TIFFTAG_ROWSPERSTRIP, defaultStripSize(tiff)))
888 for (
int y = 0; y < height; ++y) {
889 if (writeScanline(image.scanLine(y), y) != 1)
892 }
else if (format == QImage::Format_CMYK8888) {
893 if (!setField(TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED)
894 || !setField(TIFFTAG_SAMPLESPERPIXEL, 4)
895 || !setField(TIFFTAG_BITSPERSAMPLE, 8)
896 || !setField(TIFFTAG_INKSET, INKSET_CMYK)
897 || !setField(TIFFTAG_ROWSPERSTRIP, defaultStripSize(tiff)))
902 for (
int y = 0; y < image.height(); ++y) {
903 if (writeScanline(image.scanLine(y), y) != 1)
906 }
else if (!image.hasAlphaChannel()) {
907 if (!setField(TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB)
908 || !setField(TIFFTAG_SAMPLESPERPIXEL, 3)
909 || !setField(TIFFTAG_BITSPERSAMPLE, 8)
910 || !setField(TIFFTAG_ROWSPERSTRIP, defaultStripSize(tiff)))
915 const int chunks =
int(image.sizeInBytes() / (1024 * 1024 * 16)) + 1;
916 const int chunkHeight = qMax(height / chunks, 1);
920 const QImage chunk = image.copy(0, y, width, qMin(chunkHeight, height - y)).convertToFormat(QImage::Format_RGB888);
923 int chunkEnd = y + chunk.height();
924 while (y < chunkEnd) {
925 if (writeScanline(chunk.scanLine(y - chunkStart), y) != 1)
931 const bool premultiplied = image.format() != QImage::Format_ARGB32
932 && image.format() != QImage::Format_RGBA8888;
933 const uint16_t extrasamples = premultiplied ? EXTRASAMPLE_ASSOCALPHA : EXTRASAMPLE_UNASSALPHA;
934 if (!setField(TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB)
935 || !setField(TIFFTAG_SAMPLESPERPIXEL, 4)
936 || !setField(TIFFTAG_BITSPERSAMPLE, 8)
937 || !setField(TIFFTAG_EXTRASAMPLES, 1, &extrasamples)
938 || !setField(TIFFTAG_ROWSPERSTRIP, defaultStripSize(tiff)))
943 const int chunks =
int(image.sizeInBytes() / (1024 * 1024 * 16)) + 1;
944 const int chunkHeight = qMax(height / chunks, 1);
946 const QImage::Format format = premultiplied ? QImage::Format_RGBA8888_Premultiplied
947 : QImage::Format_RGBA8888;
950 const QImage chunk = image.copy(0, y, width, qMin(chunkHeight, height - y)).convertToFormat(format);
953 int chunkEnd = y + chunk.height();
954 while (y < chunkEnd) {
955 if (writeScanline(chunk.scanLine(y - chunkStart), y) != 1)