56 return device->
isReadable() ? device->
read(static_cast<char *>(buf), size) : -1;
61 return static_cast<QTiffHandler*
>(fd)->device()->
write(static_cast<char *>(buf), size);
72 device->
seek(device->
pos() + off);
104 const int height = image->
height();
105 const int width = image->
width();
107 const uint32 *originalPixel =
reinterpret_cast<const uint32*
>(image->
bits());
108 uint32 *
const generatedPixels =
reinterpret_cast<uint32*
>(generated.bits());
109 for (
int row=0; row < height; ++row) {
110 for (
int col=0; col < width; ++col) {
111 int idx = col * height + row;
112 generatedPixels[idx] = *originalPixel;
121 const int height = image->
height();
122 const int width = image->
width();
124 const int lastCol = width - 1;
125 const int lastRow = height - 1;
126 const uint32 *pixel =
reinterpret_cast<const uint32*
>(image->
bits());
127 uint32 *
const generatedBits =
reinterpret_cast<uint32*
>(generated.bits());
128 for (
int row=0; row < height; ++row) {
129 for (
int col=0; col < width; ++col) {
130 int idx = (lastCol - col) * height + (lastRow - row);
131 generatedBits[idx] = *pixel;
155 qWarning(
"QTiffHandler::canRead() called with no device");
161 int pos = device->
pos();
177 TIFF *
const tiff = TIFFClientOpen(
"foo",
194 if (!TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width)
195 || !TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height)
196 || !TIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric)) {
203 if (!TIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &bitPerSample))
205 uint16 samplesPerPixel;
206 if (!TIFFGetField(tiff, TIFFTAG_SAMPLESPERPIXEL, &samplesPerPixel))
209 bool grayscale = photometric == PHOTOMETRIC_MINISBLACK || photometric == PHOTOMETRIC_MINISWHITE;
210 if (grayscale && bitPerSample == 1 && samplesPerPixel == 1) {
214 if (photometric == PHOTOMETRIC_MINISBLACK) {
215 colortable[0] = 0xff000000;
216 colortable[1] = 0xffffffff;
218 colortable[0] = 0xffffffff;
219 colortable[1] = 0xff000000;
224 for (uint32 y=0; y<height; ++y) {
225 if (TIFFReadScanline(tiff, image->
scanLine(y), y, 0) < 0) {
232 if ((grayscale || photometric == PHOTOMETRIC_PALETTE) && bitPerSample == 8 && samplesPerPixel == 1) {
236 const uint16 tableSize = 256;
239 for (
int i = 0; i<tableSize; ++i) {
240 const int c = (photometric == PHOTOMETRIC_MINISBLACK) ? i : (255 - i);
241 qtColorTable[i] =
qRgb(c, c, c);
245 uint16 *redTable = 0;
246 uint16 *greenTable = 0;
247 uint16 *blueTable = 0;
248 if (!TIFFGetField(tiff, TIFFTAG_COLORMAP, &redTable, &greenTable, &blueTable)) {
252 if (!redTable || !greenTable || !blueTable) {
257 for (
int i = 0; i<tableSize ;++i) {
258 const int red = redTable[i] / 257;
259 const int green = greenTable[i] / 257;
260 const int blue = blueTable[i] / 257;
261 qtColorTable[i] =
qRgb(red, green, blue);
266 for (uint32 y=0; y<height; ++y) {
267 if (TIFFReadScanline(tiff, image->
scanLine(y), y, 0) < 0) {
279 const int stopOnError = 1;
280 if (TIFFReadRGBAImageOriented(tiff, width, height, reinterpret_cast<uint32 *>(image->
bits()), ORIENTATION_TOPLEFT, stopOnError)) {
281 for (uint32 y=0; y<height; ++y)
299 if (!TIFFGetField(tiff, TIFFTAG_RESOLUTIONUNIT, &resUnit))
300 resUnit = RESUNIT_INCH;
302 if (TIFFGetField(tiff, TIFFTAG_XRESOLUTION, &resX)
303 && TIFFGetField(tiff, TIFFTAG_YRESOLUTION, &resY)) {
305 case RESUNIT_CENTIMETER:
321 uint16 orientationTag;
322 if (TIFFGetField(tiff, TIFFTAG_ORIENTATION, &orientationTag)) {
325 switch (orientationTag) {
340 switch (orientationTag) {
344 *image = image->
mirrored(
true,
false);
347 *image = image->
mirrored(
true,
true);
350 *image = image->
mirrored(
false,
true);
355 transformation.
rotate(90);
357 *image = image->
mirrored(
true,
false);
363 transformation.
rotate(90);
370 transformation.
rotate(90);
372 *image = image->
mirrored(
false,
true);
378 transformation.
rotate(270);
393 if (colorTable.
size() != 256)
396 const bool increasing = (colorTable.
at(0) == 0xff000000);
397 for (
int i = 0; i < 256; ++i) {
398 if ((increasing && colorTable.
at(i) !=
qRgb(i, i, i))
399 || (!increasing && colorTable.
at(i) !=
qRgb(255 - i, 255 - i, 255 - i)))
407 if (!
device()->isWritable())
410 TIFF *
const tiff = TIFFClientOpen(
"foo",
423 const int width = image.
width();
424 const int height = image.
height();
426 if (!TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, width)
427 || !TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, height)
428 || !TIFFSetField(tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG)) {
434 bool resolutionSet =
false;
437 if ((dotPerMeterX % 100) == 0
438 && (dotPerMeterY % 100) == 0) {
439 resolutionSet = TIFFSetField(tiff, TIFFTAG_RESOLUTIONUNIT, RESUNIT_CENTIMETER)
440 && TIFFSetField(tiff, TIFFTAG_XRESOLUTION, dotPerMeterX/100.0)
441 && TIFFSetField(tiff, TIFFTAG_YRESOLUTION, dotPerMeterY/100.0);
443 resolutionSet = TIFFSetField(tiff, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH)
444 && TIFFSetField(tiff, TIFFTAG_XRESOLUTION, static_cast<float>(image.
logicalDpiX()))
445 && TIFFSetField(tiff, TIFFTAG_YRESOLUTION, static_cast<float>(image.
logicalDpiY()));
447 if (!resolutionSet) {
455 uint16 photometric = PHOTOMETRIC_MINISBLACK;
457 photometric = PHOTOMETRIC_MINISWHITE;
458 if (!TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, photometric)
460 || !TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, 1)) {
466 int chunks = (width * height / (1024 * 1024 * 16)) + 1;
467 int chunkHeight =
qMax(height / chunks, 1);
474 int chunkEnd = y + chunk.
height();
475 while (y < chunkEnd) {
476 if (TIFFWriteScanline(tiff, reinterpret_cast<uint32 *>(chunk.
scanLine(y - chunkStart)), y) != 1) {
488 uint16 photometric = PHOTOMETRIC_MINISBLACK;
490 photometric = PHOTOMETRIC_MINISWHITE;
491 if (!TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, photometric)
493 || !TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, 8)) {
498 if (!TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE)
500 || !TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, 8)) {
506 uint16 *redTable =
static_cast<uint16 *
>(
qMalloc(256 *
sizeof(uint16)));
507 uint16 *greenTable =
static_cast<uint16 *
>(
qMalloc(256 *
sizeof(uint16)));
508 uint16 *blueTable =
static_cast<uint16 *
>(
qMalloc(256 *
sizeof(uint16)));
509 if (!redTable || !greenTable || !blueTable) {
518 const int tableSize = colorTable.
size();
520 for (
int i = 0; i<tableSize; ++i) {
521 const QRgb color = colorTable.
at(i);
522 redTable[i] =
qRed(color) * 257;
523 greenTable[i] =
qGreen(color) * 257;
524 blueTable[i] =
qBlue(color) * 257;
527 const bool setColorTableSuccess = TIFFSetField(tiff, TIFFTAG_COLORMAP, redTable, greenTable, blueTable);
533 if (!setColorTableSuccess) {
541 int chunks = (width * height/ (1024 * 1024 * 16)) + 1;
542 int chunkHeight =
qMax(height / chunks, 1);
546 QImage chunk = image.
copy(0, y, width,
qMin(chunkHeight, height - y));
549 int chunkEnd = y + chunk.
height();
550 while (y < chunkEnd) {
551 if (TIFFWriteScanline(tiff, reinterpret_cast<uint32 *>(chunk.
scanLine(y - chunkStart)), y) != 1) {
561 if (!TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB)
563 || !TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, 4)
564 || !TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, 8)) {
569 int chunks = (width * height * 4 / (1024 * 1024 * 16)) + 1;
570 int chunkHeight =
qMax(height / chunks, 1);
577 int chunkEnd = y + chunk.
height();
578 while (y < chunkEnd) {
579 if (QSysInfo::ByteOrder == QSysInfo::LittleEndian)
584 if (TIFFWriteScanline(tiff, reinterpret_cast<uint32 *>(chunk.
scanLine(y - chunkStart)), y) != 1) {
607 TIFF *tiff = TIFFClientOpen(
"foo",
609 const_cast<QTiffHandler*>(
this),
621 TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width);
622 TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height);
623 imageSize =
QSize(width, height);
652 uint32 *target =
reinterpret_cast<uint32 *
>(buffer);
653 for (int32 x=0; x<width; ++x) {
654 uint32 p = target[x];
656 target[x] = (p & 0xff000000)
657 | ((p & 0x00ff0000) >> 16)
659 | ((p & 0x000000ff) << 16);
665 uint32 *target =
reinterpret_cast<uint32 *
>(buffer);
666 for (int32 x=0; x<width; ++x) {
667 uint32 p = target[x];
668 target[x] = (p & 0xff000000) >> 24
669 | (p & 0x00ff0000) << 8
670 | (p & 0x0000ff00) << 8
671 | (p & 0x000000ff) << 8;
The QVariant class acts like a union for the most common Qt data types.
Format
The following image formats are available in Qt.
QByteArray name() const
Use format() instead.
virtual qint64 size() const
For open random-access devices, this function returns the size of the device.
void setFormat(const QByteArray &format)
Sets the format of the QImageIOHandler to format.
QImage copy(const QRect &rect=QRect()) const
Returns a sub-area of the image as a new image.
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
#define QT_END_NAMESPACE
This macro expands to.
bool isReadable() const
Returns true if data can be read from the device; otherwise returns false.
QVariant option(ImageOption option) const
Returns the value assigned to option as a QVariant.
void setColorTable(const QVector< QRgb > colors)
Sets the color table used to translate color indexes to QRgb values, to the specified colors...
The QMatrix class specifies 2D transformations of a coordinate system.
Q_CORE_EXPORT void qFree(void *ptr)
bool isNull() const
Returns true if it is a null image, otherwise returns false.
The QByteArray class provides an array of bytes.
virtual qint64 pos() const
For random-access devices, this function returns the position that data is written to or read from...
Q_CORE_EXPORT void * qMalloc(size_t size)
int dotsPerMeterY() const
Returns the number of pixels that fit vertically in a physical meter.
tsize_t qtiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
static bool checkGrayscale(const QVector< QRgb > &colorTable)
toff_t qtiffSeekProc(thandle_t fd, toff_t off, int whence)
static void grayscale(const QImage &image, QImage &dest, const QRect &rect=QRect())
Format format() const
Returns the format of the image.
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Q_GUI_EXPORT_INLINE int qRed(QRgb rgb)
void qtiffUnmapProc(thandle_t, tdata_t, toff_t)
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read...
tsize_t qtiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
#define QT_BEGIN_NAMESPACE
This macro expands to.
QMatrix & rotate(qreal a)
Rotates the coordinate system the given degrees counterclockwise.
ImageOption
This enum describes the different options supported by QImageIOHandler.
static QByteArray fromRawData(const char *, int size)
Constructs a QByteArray that uses the first size bytes of the data array.
qint64 peek(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, without side effects (i.
Q_CORE_EXPORT void qWarning(const char *,...)
The QImage class provides a hardware-independent image representation that allows direct access to th...
bool canRead() const
Returns true if an image can be read from the device (i.
void setDotsPerMeterY(int)
Sets the number of pixels that fit vertically in a physical meter, to y.
QImage transformed(const QMatrix &matrix, Qt::TransformationMode mode=Qt::FastTransformation) const
Returns a copy of the image that is transformed using the given transformation matrix and transformat...
QImage mirrored(bool horizontally=false, bool vertically=true) const
Returns a mirror of the image, mirrored in the horizontal and/or the vertical direction depending on ...
Q_GUI_EXPORT_INLINE int qBlue(QRgb rgb)
const T & at(int i) const
Returns the item at index position i in the vector.
QSize size() const
Returns the size of the image, i.
void convert32BitOrderBigEndian(void *buffer, int width)
toff_t qtiffSizeProc(thandle_t fd)
int qtiffMapProc(thandle_t, tdata_t *, toff_t *)
uchar * bits()
Returns a pointer to the first pixel data.
int width() const
Returns the width of the image.
QImage convertToFormat(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) const Q_REQUIRED_RESULT
Returns a copy of the image in the given format.
void setDotsPerMeterX(int)
Sets the number of pixels that fit horizontally in a physical meter, to x.
The QImageIOHandler class defines the common image I/O interface for all image formats in Qt...
Type type() const
Returns the storage type of the value stored in the variant.
Q_GUI_EXPORT_INLINE QRgb qRgb(int r, int g, int b)
void convert32BitOrder(void *buffer, int width)
bool read(QImage *image)
Read an image from the device, and stores it in image.
bool supportsOption(ImageOption option) const
Returns true if the QImageIOHandler supports the option option; otherwise returns false...
bool isValid() const
Returns true if both the width and height is equal to or greater than 0; otherwise returns false...
int qtiffCloseProc(thandle_t)
QIODevice * device() const
Returns the device currently assigned to the QImageIOHandler.
int height() const
Returns the height of the image.
Q_GUI_EXPORT_INLINE int qGreen(QRgb rgb)
The QSize class defines the size of a two-dimensional object using integer point precision.
The QIODevice class is the base interface class of all I/O devices in Qt.
void rotate_right_mirror_horizontal(QImage *const image)
void setOption(ImageOption option, const QVariant &value)
Sets the option option with the value value.
virtual bool seek(qint64 pos)
For random-access devices, this function sets the current position to pos, returning true on success...
int size() const
Returns the number of items in the vector.
bool write(const QImage &image)
Writes the image image to the assigned device.
int dotsPerMeterX() const
Returns the number of pixels that fit horizontally in a physical meter.
QVector< QRgb > colorTable() const
Returns a list of the colors contained in the image's color table, or an empty list if the image does...
Q_DECL_CONSTEXPR int qRound(qreal d)
uchar * scanLine(int)
Returns a pointer to the pixel data at the scanline with index i.
void rotate_right_mirror_vertical(QImage *const image)