Qt 4.8
qimagereader.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 //#define QIMAGEREADER_DEBUG
43 
121 #include "qimagereader.h"
122 
123 #include <qbytearray.h>
124 #ifdef QIMAGEREADER_DEBUG
125 #include <qdebug.h>
126 #endif
127 #include <qfile.h>
128 #include <qfileinfo.h>
129 #include <qimage.h>
130 #include <qimageiohandler.h>
131 #include <qlist.h>
132 #include <qrect.h>
133 #include <qset.h>
134 #include <qsize.h>
135 #include <qcolor.h>
136 #include <qvariant.h>
137 
138 // factory loader
139 #include <qcoreapplication.h>
140 #include <private/qfactoryloader_p.h>
141 
142 // image handlers
143 #include <private/qbmphandler_p.h>
144 #include <private/qppmhandler_p.h>
145 #include <private/qxbmhandler_p.h>
146 #include <private/qxpmhandler_p.h>
147 #ifndef QT_NO_IMAGEFORMAT_PNG
148 #include <private/qpnghandler_p.h>
149 #endif
150 #ifndef QT_NO_IMAGEFORMAT_JPEG
151 #include <private/qjpeghandler_p.h>
152 #endif
153 #ifndef QT_NO_IMAGEFORMAT_MNG
154 #include <private/qmnghandler_p.h>
155 #endif
156 #ifndef QT_NO_IMAGEFORMAT_TIFF
157 #include <private/qtiffhandler_p.h>
158 #endif
159 #ifdef QT_BUILTIN_GIF_READER
160 #include <private/qgifhandler_p.h>
161 #endif
162 
164 
165 #ifndef QT_NO_LIBRARY
168 #endif
169 
170 enum _qt_BuiltInFormatType {
171 #ifndef QT_NO_IMAGEFORMAT_PNG
172  _qt_PngFormat,
173 #endif
174 #ifndef QT_NO_IMAGEFORMAT_JPEG
175  _qt_JpgFormat,
176 #endif
177 #ifndef QT_NO_IMAGEFORMAT_MNG
178  _qt_MngFormat,
179 #endif
180 #ifndef QT_NO_IMAGEFORMAT_TIFF
181  _qt_TifFormat,
182 #endif
183 #ifdef QT_BUILTIN_GIF_READER
184  _qt_GifFormat,
185 #endif
186  _qt_BmpFormat,
187 #ifndef QT_NO_IMAGEFORMAT_PPM
188  _qt_PpmFormat,
189  _qt_PgmFormat,
190  _qt_PbmFormat,
191 #endif
192 #ifndef QT_NO_IMAGEFORMAT_XBM
193  _qt_XbmFormat,
194 #endif
195 #ifndef QT_NO_IMAGEFORMAT_XPM
196  _qt_XpmFormat,
197 #endif
198  _qt_NumFormats,
199  _qt_NoFormat = -1
200 };
201 
203 {
204  _qt_BuiltInFormatType type;
205  const char *extension;
206 };
207 
209 #ifndef QT_NO_IMAGEFORMAT_PNG
210  {_qt_PngFormat, "png"},
211 #endif
212 #ifndef QT_NO_IMAGEFORMAT_JPEG
213  {_qt_JpgFormat, "jpg"},
214 #endif
215 #ifndef QT_NO_IMAGEFORMAT_MNG
216  {_qt_MngFormat, "mng"},
217 #endif
218 #ifndef QT_NO_IMAGEFORMAT_TIFF
219  {_qt_TifFormat, "tif"},
220 #endif
221 #ifdef QT_BUILTIN_GIF_READER
222  {_qt_GifFormat, "gif"},
223 #endif
224  {_qt_BmpFormat, "bmp"},
225 #ifndef QT_NO_IMAGEFORMAT_PPM
226  {_qt_PpmFormat, "ppm"},
227  {_qt_PgmFormat, "pgm"},
228  {_qt_PbmFormat, "pbm"},
229 #endif
230 #ifndef QT_NO_IMAGEFORMAT_XBM
231  {_qt_XbmFormat, "xbm"},
232 #endif
233 #ifndef QT_NO_IMAGEFORMAT_XPM
234  {_qt_XpmFormat, "xpm"},
235 #endif
236  {_qt_NoFormat, ""}
237 };
238 
240  const QByteArray &format,
241  bool autoDetectImageFormat,
242  bool ignoresFormatAndExtension)
243 {
244  if (!autoDetectImageFormat && format.isEmpty())
245  return 0;
246 
247  QByteArray form = format.toLower();
249 
250 #ifndef QT_NO_LIBRARY
251  // check if we have plugins that support the image format
252  QFactoryLoader *l = loader();
253  QStringList keys = l->keys();
254 #endif
256 
257 #ifdef QIMAGEREADER_DEBUG
258  qDebug() << "QImageReader::createReadHandler( device =" << (void *)device << ", format =" << format << "),"
259  << keys.size() << "plugins available: " << keys;
260 #endif
261 
262 #ifndef QT_NO_LIBRARY
263  int suffixPluginIndex = -1;
264  if (device && format.isEmpty() && autoDetectImageFormat && !ignoresFormatAndExtension) {
265  // if there's no format, see if \a device is a file, and if so, find
266  // the file suffix and find support for that format among our plugins.
267  // this allows plugins to override our built-in handlers.
268  if (QFile *file = qobject_cast<QFile *>(device)) {
269 #ifdef QIMAGEREADER_DEBUG
270  qDebug() << "QImageReader::createReadHandler: device is a file:" << file->fileName();
271 #endif
272  if (!(suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1()).isEmpty()) {
273  int index = keys.indexOf(QString::fromLatin1(suffix));
274  if (index != -1) {
275 #ifdef QIMAGEREADER_DEBUG
276  qDebug() << "QImageReader::createReadHandler: suffix recognized; the"
277  << suffix << "plugin might be able to read this";
278 #endif
279  suffixPluginIndex = index;
280  }
281  }
282  }
283  }
284 #endif // QT_NO_LIBRARY
285 
286  QByteArray testFormat = !form.isEmpty() ? form : suffix;
287 
288  if (ignoresFormatAndExtension)
289  testFormat = QByteArray();
290 
291 #ifndef QT_NO_LIBRARY
292  if (suffixPluginIndex != -1) {
293  // check if the plugin that claims support for this format can load
294  // from this device with this format.
295  const qint64 pos = device ? device->pos() : 0;
297  if (plugin && plugin->capabilities(device, testFormat) & QImageIOPlugin::CanRead) {
298  handler = plugin->create(device, testFormat);
299 #ifdef QIMAGEREADER_DEBUG
300  qDebug() << "QImageReader::createReadHandler: using the" << suffix
301  << "plugin";
302 #endif
303  }
304  if (device && !device->isSequential())
305  device->seek(pos);
306  }
307 
308  if (!handler && !testFormat.isEmpty() && !ignoresFormatAndExtension) {
309  // check if any plugin supports the format (they are not allowed to
310  // read from the device yet).
311  const qint64 pos = device ? device->pos() : 0;
312 
313  if (autoDetectImageFormat) {
314  for (int i = 0; i < keys.size(); ++i) {
315  if (i != suffixPluginIndex) {
316  QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(keys.at(i)));
317  if (plugin && plugin->capabilities(device, testFormat) & QImageIOPlugin::CanRead) {
318 #ifdef QIMAGEREADER_DEBUG
319  qDebug() << "QImageReader::createReadHandler: the" << keys.at(i) << "plugin can read this format";
320 #endif
321  handler = plugin->create(device, testFormat);
322  break;
323  }
324  }
325  }
326  } else {
327  QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(QLatin1String(testFormat)));
328  if (plugin && plugin->capabilities(device, testFormat) & QImageIOPlugin::CanRead) {
329 #ifdef QIMAGEREADER_DEBUG
330  qDebug() << "QImageReader::createReadHandler: the" << testFormat << "plugin can read this format";
331 #endif
332  handler = plugin->create(device, testFormat);
333  }
334  }
335  if (device && !device->isSequential())
336  device->seek(pos);
337  }
338 
339 #endif // QT_NO_LIBRARY
340 
341  // if we don't have a handler yet, check if we have built-in support for
342  // the format
343  if (!handler && !testFormat.isEmpty()) {
344  if (false) {
345 #ifndef QT_NO_IMAGEFORMAT_PNG
346  } else if (testFormat == "png") {
347  handler = new QPngHandler;
348 #endif
349 #ifndef QT_NO_IMAGEFORMAT_JPEG
350  } else if (testFormat == "jpg" || testFormat == "jpeg") {
351  handler = new QJpegHandler;
352 #endif
353 #ifndef QT_NO_IMAGEFORMAT_MNG
354  } else if (testFormat == "mng") {
355  handler = new QMngHandler;
356 #endif
357 #ifndef QT_NO_IMAGEFORMAT_TIFF
358  } else if (testFormat == "tif" || testFormat == "tiff") {
359  handler = new QTiffHandler;
360 #endif
361 #ifdef QT_BUILTIN_GIF_READER
362  } else if (testFormat == "gif") {
363  handler = new QGifHandler;
364 #endif
365 #ifndef QT_NO_IMAGEFORMAT_BMP
366  } else if (testFormat == "bmp") {
367  handler = new QBmpHandler;
368 #endif
369 #ifndef QT_NO_IMAGEFORMAT_XPM
370  } else if (testFormat == "xpm") {
371  handler = new QXpmHandler;
372 #endif
373 #ifndef QT_NO_IMAGEFORMAT_XBM
374  } else if (testFormat == "xbm") {
375  handler = new QXbmHandler;
376  handler->setOption(QImageIOHandler::SubType, testFormat);
377 #endif
378 #ifndef QT_NO_IMAGEFORMAT_PPM
379  } else if (testFormat == "pbm" || testFormat == "pbmraw" || testFormat == "pgm"
380  || testFormat == "pgmraw" || testFormat == "ppm" || testFormat == "ppmraw") {
381  handler = new QPpmHandler;
382  handler->setOption(QImageIOHandler::SubType, testFormat);
383 #endif
384  }
385 
386 #ifdef QIMAGEREADER_DEBUG
387  if (handler)
388  qDebug() << "QImageReader::createReadHandler: using the built-in handler for" << testFormat;
389 #endif
390  }
391 
392 #ifndef QT_NO_LIBRARY
393  if (!handler && (autoDetectImageFormat || ignoresFormatAndExtension)) {
394  // check if any of our plugins recognize the file from its contents.
395  const qint64 pos = device ? device->pos() : 0;
396  for (int i = 0; i < keys.size(); ++i) {
397  if (i != suffixPluginIndex) {
398  QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(keys.at(i)));
399  if (plugin && plugin->capabilities(device, QByteArray()) & QImageIOPlugin::CanRead) {
400  handler = plugin->create(device, testFormat);
401 #ifdef QIMAGEREADER_DEBUG
402  qDebug() << "QImageReader::createReadHandler: the" << keys.at(i) << "plugin can read this data";
403 #endif
404  break;
405  }
406  }
407  }
408  if (device && !device->isSequential())
409  device->seek(pos);
410  }
411 #endif // QT_NO_LIBRARY
412 
413  if (!handler && (autoDetectImageFormat || ignoresFormatAndExtension)) {
414  // check if any of our built-in handlers recognize the file from its
415  // contents.
416  int currentFormat = 0;
417  if (!suffix.isEmpty()) {
418  // If reading from a file with a suffix, start testing our
419  // built-in handler for that suffix first.
420  for (int i = 0; i < _qt_NumFormats; ++i) {
421  if (_qt_BuiltInFormats[i].extension == suffix) {
422  currentFormat = i;
423  break;
424  }
425  }
426  }
427 
428  QByteArray subType;
429  int numFormats = _qt_NumFormats;
430  while (device && numFormats >= 0) {
431  const _qt_BuiltInFormatStruct *formatStruct = &_qt_BuiltInFormats[currentFormat];
432 
433  const qint64 pos = device->pos();
434  switch (formatStruct->type) {
435 #ifndef QT_NO_IMAGEFORMAT_PNG
436  case _qt_PngFormat:
437  if (QPngHandler::canRead(device))
438  handler = new QPngHandler;
439  break;
440 #endif
441 #ifndef QT_NO_IMAGEFORMAT_JPEG
442  case _qt_JpgFormat:
443  if (QJpegHandler::canRead(device))
444  handler = new QJpegHandler;
445  break;
446 #endif
447 #ifndef QT_NO_IMAGEFORMAT_MNG
448  case _qt_MngFormat:
449  if (QMngHandler::canRead(device))
450  handler = new QMngHandler;
451  break;
452 #endif
453 #ifndef QT_NO_IMAGEFORMAT_TIFF
454  case _qt_TifFormat:
455  if (QTiffHandler::canRead(device))
456  handler = new QTiffHandler;
457  break;
458 #endif
459 #ifdef QT_BUILTIN_GIF_READER
460  case _qt_GifFormat:
461  if (QGifHandler::canRead(device))
462  handler = new QGifHandler;
463  break;
464 #endif
465 #ifndef QT_NO_IMAGEFORMAT_BMP
466  case _qt_BmpFormat:
467  if (QBmpHandler::canRead(device))
468  handler = new QBmpHandler;
469  break;
470 #endif
471 #ifndef QT_NO_IMAGEFORMAT_XPM
472  case _qt_XpmFormat:
473  if (QXpmHandler::canRead(device))
474  handler = new QXpmHandler;
475  break;
476 #endif
477 #ifndef QT_NO_IMAGEFORMAT_PPM
478  case _qt_PbmFormat:
479  case _qt_PgmFormat:
480  case _qt_PpmFormat:
481  if (QPpmHandler::canRead(device, &subType)) {
482  handler = new QPpmHandler;
483  handler->setOption(QImageIOHandler::SubType, subType);
484  }
485  break;
486 #endif
487 #ifndef QT_NO_IMAGEFORMAT_XBM
488  case _qt_XbmFormat:
489  if (QXbmHandler::canRead(device))
490  handler = new QXbmHandler;
491  break;
492 #endif
493  default:
494  break;
495  }
496  if (!device->isSequential())
497  device->seek(pos);
498 
499  if (handler) {
500 #ifdef QIMAGEREADER_DEBUG
501  qDebug() << "QImageReader::createReadHandler: the" << formatStruct->extension
502  << "built-in handler can read this data";
503 #endif
504  break;
505  }
506 
507  --numFormats;
508  ++currentFormat;
509  currentFormat %= _qt_NumFormats;
510  }
511  }
512 
513  if (!handler) {
514 #ifdef QIMAGEREADER_DEBUG
515  qDebug() << "QImageReader::createReadHandler: no handlers found. giving up.";
516 #endif
517  // no handler: give up.
518  return 0;
519  }
520 
521  handler->setDevice(device);
522  if (!form.isEmpty())
523  handler->setFormat(form);
524  return handler;
525 }
526 
528 {
529 public:
532 
533  // device
540  bool initHandler();
541 
542  // image options
546  int quality;
548  void getText();
549 
550  // error
553 
555 };
556 
561  : autoDetectImageFormat(true), ignoresFormatAndExtension(false)
562 {
563  device = 0;
564  deleteDevice = false;
565  handler = 0;
566  quality = -1;
568 
569  q = qq;
570 }
571 
576 {
577  if (deleteDevice)
578  delete device;
579  delete handler;
580 }
581 
586 {
587  // check some preconditions
588  if (!device || (!deleteDevice && !device->isOpen() && !device->open(QIODevice::ReadOnly))) {
591  return false;
592  }
593 
594  // probe the file extension
597  if (!format.isEmpty()) {
598  // Try the most probable extension first
599  int currentFormatIndex = extensions.indexOf(format.toLower());
600  if (currentFormatIndex > 0)
601  extensions.swap(0, currentFormatIndex);
602  }
603 
604  int currentExtension = 0;
605 
606  QFile *file = static_cast<QFile *>(device);
607  QString fileName = file->fileName();
608 
609  do {
610  file->setFileName(fileName + QLatin1Char('.')
611  + QString::fromLatin1(extensions.at(currentExtension++).constData()));
612  file->open(QIODevice::ReadOnly);
613  } while (!file->isOpen() && currentExtension < extensions.size());
614 
615  if (!device->isOpen()) {
618  file->setFileName(fileName); // restore the old file name
619  return false;
620  }
621  }
622 
623  // assign a handler
626  errorString = QLatin1String(QT_TRANSLATE_NOOP(QImageReader, "Unsupported image format"));
627  return false;
628  }
629  return true;
630 }
631 
636 {
638  return;
640  QLatin1String("\n\n"))) {
641  int index = pair.indexOf(QLatin1Char(':'));
642  if (index >= 0 && pair.indexOf(QLatin1Char(' ')) < index) {
643  text.insert(QLatin1String("Description"), pair.simplified());
644  } else {
645  QString key = pair.left(index);
646  text.insert(key, pair.mid(index + 2).simplified());
647  }
648  }
649 }
650 
656  : d(new QImageReaderPrivate(this))
657 {
658 }
659 
665  : d(new QImageReaderPrivate(this))
666 {
667  d->device = device;
668  d->format = format;
669 }
670 
678  : d(new QImageReaderPrivate(this))
679 {
680  QFile *file = new QFile(fileName);
681  d->device = file;
682  d->deleteDevice = true;
683  d->format = format;
684 }
685 
690 {
691  delete d;
692 }
693 
706 {
707  d->format = format;
708 }
709 
725 {
726  if (d->format.isEmpty()) {
727  if (!d->initHandler())
728  return QByteArray();
729  return d->handler->canRead() ? d->handler->format() : QByteArray();
730  }
731 
732  return d->format;
733 }
734 
775 {
777 }
778 
786 {
787  return d->autoDetectImageFormat;
788 }
789 
790 
806 {
807  d->ignoresFormatAndExtension = ignored;
808 }
809 
810 
820 {
822 }
823 
824 
839 {
840  if (d->device && d->deleteDevice)
841  delete d->device;
842  d->device = device;
843  d->deleteDevice = false;
844  delete d->handler;
845  d->handler = 0;
846  d->text.clear();
847 }
848 
854 {
855  return d->device;
856 }
857 
869 {
870  setDevice(new QFile(fileName));
871  d->deleteDevice = true;
872 }
873 
884 {
885  QFile *file = qobject_cast<QFile *>(d->device);
886  return file ? file->fileName() : QString();
887 }
888 
906 {
907  d->quality = quality;
908 }
909 
921 {
922  return d->quality;
923 }
924 
925 
937 {
938  if (!d->initHandler())
939  return QSize();
940 
943 
944  return QSize();
945 }
946 
963 {
964  if (!d->initHandler())
965  return QImage::Format_Invalid;
966 
969 
970  return QImage::Format_Invalid;
971 }
972 
989 {
990  d->getText();
991  return d->text.keys();
992 }
993 
1008 {
1009  d->getText();
1010  return d->text.value(key);
1011 }
1012 
1021 {
1022  d->clipRect = rect;
1023 }
1024 
1033 {
1034  return d->clipRect;
1035 }
1036 
1048 {
1049  d->scaledSize = size;
1050 }
1051 
1058 {
1059  return d->scaledSize;
1060 }
1061 
1070 {
1071  d->scaledClipRect = rect;
1072 }
1073 
1080 {
1081  return d->scaledClipRect;
1082 }
1083 
1097 {
1098  if (!d->initHandler())
1099  return;
1102 }
1103 
1117 {
1118  if (!d->initHandler())
1119  return QColor();
1121  return qvariant_cast<QColor>(d->handler->option(QImageIOHandler::BackgroundColor));
1122  return QColor();
1123 }
1124 
1137 {
1138  if (!d->initHandler())
1139  return false;
1142  return false;
1143 }
1144 
1160 {
1161  if (!d->initHandler())
1162  return false;
1163 
1164  return d->handler->canRead();
1165 }
1166 
1180 {
1181  // Because failed image reading might have side effects, we explicitly
1182  // return a null image instead of the image we've just created.
1183  QImage image;
1184  return read(&image) ? image : QImage();
1185 }
1186 
1211 {
1212  if (!image) {
1213  qWarning("QImageReader::read: cannot read into null pointer");
1214  return false;
1215  }
1216 
1217  if (!d->handler && !d->initHandler())
1218  return false;
1219 
1220  // set the handler specific options.
1223  || d->clipRect.isNull()) {
1224  // Only enable the ScaledSize option if there is no clip rect, or
1225  // if the handler also supports ClipRect.
1227  }
1228  }
1235 
1236  // read the image
1237  if (!d->handler->read(image)) {
1239  d->errorString = QLatin1String(QT_TRANSLATE_NOOP(QImageReader, "Unable to read image data"));
1240  return false;
1241  }
1242 
1243  // provide default implementations for any unsupported image
1244  // options
1248  // all features are supported by the handler; nothing to do.
1249  } else {
1250  // the image is already scaled, so apply scaled clipping.
1251  if (!d->scaledClipRect.isNull())
1252  *image = image->copy(d->scaledClipRect);
1253  }
1254  } else {
1256  // supports scaled clipping but not scaling, most
1257  // likely a broken handler.
1258  } else {
1259  if (d->scaledSize.isValid()) {
1261  }
1262  if (d->scaledClipRect.isValid()) {
1263  *image = image->copy(d->scaledClipRect);
1264  }
1265  }
1266  }
1267  } else {
1269  // in this case, there's nothing we can do. if the
1270  // plugin supports scaled size but not ClipRect, then
1271  // we have to ignore ClipRect."
1272 
1274  // nothing to do (ClipRect is ignored!)
1275  } else {
1276  // provide all workarounds.
1277  if (d->scaledClipRect.isValid()) {
1278  *image = image->copy(d->scaledClipRect);
1279  }
1280  }
1281  } else {
1283  // this makes no sense; a handler that supports
1284  // ScaledClipRect but not ScaledSize is broken, and we
1285  // can't work around it.
1286  } else {
1287  // provide all workarounds.
1288  if (d->clipRect.isValid())
1289  *image = image->copy(d->clipRect);
1290  if (d->scaledSize.isValid())
1292  if (d->scaledClipRect.isValid())
1293  *image = image->copy(d->scaledClipRect);
1294  }
1295  }
1296  }
1297 
1298  return true;
1299 }
1300 
1313 {
1314  if (!d->initHandler())
1315  return false;
1316  return d->handler->jumpToNextImage();
1317 }
1318 
1328 bool QImageReader::jumpToImage(int imageNumber)
1329 {
1330  if (!d->initHandler())
1331  return false;
1332  return d->handler->jumpToImage(imageNumber);
1333 }
1334 
1344 {
1345  if (!d->initHandler())
1346  return -1;
1347  return d->handler->loopCount();
1348 }
1349 
1360 {
1361  if (!d->initHandler())
1362  return -1;
1363  return d->handler->imageCount();
1364 }
1365 
1376 {
1377  if (!d->initHandler())
1378  return -1;
1379  return d->handler->nextImageDelay();
1380 }
1381 
1392 {
1393  if (!d->initHandler())
1394  return -1;
1395  return d->handler->currentImageNumber();
1396 }
1397 
1405 {
1406  if (!d->initHandler())
1407  return QRect();
1408  return d->handler->currentImageRect();
1409 }
1410 
1417 {
1418  return d->imageReaderError;
1419 }
1420 
1428 {
1429  if (d->errorString.isEmpty())
1430  return QLatin1String(QT_TRANSLATE_NOOP(QImageReader, "Unknown error"));
1431  return d->errorString;
1432 }
1433 
1454 {
1455  if (!d->initHandler())
1456  return false;
1457  return d->handler->supportsOption(option);
1458 }
1459 
1465 {
1466  QFile file(fileName);
1467  if (!file.open(QFile::ReadOnly))
1468  return QByteArray();
1469 
1470  return imageFormat(&file);
1471 }
1472 
1480 {
1482  QImageIOHandler *handler = createReadHandlerHelper(device, format, /* autoDetectImageFormat = */ true, false);
1483  if (handler) {
1484  if (handler->canRead())
1485  format = handler->format();
1486  delete handler;
1487  }
1488  return format;
1489 }
1490 
1531 {
1532  QSet<QByteArray> formats;
1533  for (int i = 0; i < _qt_NumFormats; ++i)
1534  formats << _qt_BuiltInFormats[i].extension;
1535 
1536 #ifndef QT_NO_LIBRARY
1537  QFactoryLoader *l = loader();
1538  QStringList keys = l->keys();
1539 
1540  for (int i = 0; i < keys.count(); ++i) {
1541  QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(keys.at(i)));
1542  if (plugin && plugin->capabilities(0, keys.at(i).toLatin1()) & QImageIOPlugin::CanRead)
1543  formats << keys.at(i).toLatin1();
1544  }
1545 #endif // QT_NO_LIBRARY
1546 
1547  QList<QByteArray> sortedFormats;
1548  for (QSet<QByteArray>::ConstIterator it = formats.constBegin(); it != formats.constEnd(); ++it)
1549  sortedFormats << *it;
1550 
1551  qSort(sortedFormats);
1552  return sortedFormats;
1553 }
1554 
T qobject_cast(QObject *object)
Definition: qobject.h:375
QColor backgroundColor() const
Returns the background color that&#39;s used when reading an image.
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
double d
Definition: qnumeric_p.h:62
QString fileName() const
Returns the name set by setFileName() or to the QFile constructors.
Definition: qfile.cpp:470
bool canRead() const
Returns true if an image can be read from the device (i.
Format
The following image formats are available in Qt.
Definition: qimage.h:91
void setFormat(const QByteArray &format)
Sets the format of the QImageIOHandler to format.
static void getText(QString &text, QTextDocumentPrivate *priv, const QString &docText, int pos, int end)
QImage copy(const QRect &rect=QRect()) const
Returns a sub-area of the image as a new image.
Definition: qimage.cpp:1410
virtual Capabilities capabilities(QIODevice *device, const QByteArray &format) const =0
Returns the capabilities on the plugin, based on the data in device and the format format...
bool isNull() const
Returns true if the rectangle is a null rectangle, otherwise returns false.
Definition: qrect.h:231
#define QImageIOHandlerFactoryInterface_iid
static QList< QByteArray > supportedImageFormats()
Returns the list of image formats supported by QImageReader.
int suffixPluginIndex
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QRect currentImageRect() const
For image formats that support animation, this function returns the rect for the current frame...
QRect scaledClipRect() const
Returns the scaled clip rect of the image.
void setDevice(QIODevice *device)
Sets QImageReader&#39;s device to device.
~QImageReader()
Destructs the QImageReader object.
#define it(className, varName)
QImageReaderPrivate * d
Definition: qimagereader.h:140
void setScaledSize(const QSize &size)
Sets the scaled size of the image to size.
bool open(OpenMode flags)
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition: qfile.cpp:1064
QImage::Format imageFormat() const
Returns the format of the image, without actually reading the image contents.
QImageReaderPrivate(QImageReader *qq)
const_iterator constEnd() const
Definition: qset.h:171
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
virtual bool read(QImage *image)=0
Read an image from the device, and stores it in image.
virtual bool jumpToNextImage()
For image formats that support animation, this function jumps to the next image.
virtual qint64 pos() const
For random-access devices, this function returns the position that data is written to or read from...
Definition: qiodevice.cpp:624
bool canRead() const
Returns true if an image can be read from the device (i.
QString text(const QString &key) const
Returns the image text associated with key.
virtual bool jumpToImage(int imageNumber)
For image formats that support animation, this function jumps to the image whose sequence number is i...
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
void setClipRect(const QRect &rect)
Sets the image clip rect (also known as the ROI, or Region Of Interest) to rect.
QByteArray toLower() const
Returns a lowercase copy of the byte array.
#define QT_TRANSLATE_NOOP(scope, x)
Marks the string literal sourceText for dynamic translation in the given context; i...
Definition: qglobal.h:2487
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool toBool() const
Returns the variant as a bool if the variant has type() Bool.
Definition: qvariant.cpp:2691
bool jumpToImage(int imageNumber)
For image formats that support animation, this function skips to the image whose sequence number is i...
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
void setQuality(int quality)
This is an image format specific function that sets the quality level of the image to quality...
The QString class provides a Unicode character string.
Definition: qstring.h:83
QImageReader::ImageReaderError imageReaderError
Q_GUI_EXPORT QString extensions()
Definition: qegl.cpp:785
QStringList keys
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
QByteArray format() const
Returns the format that is currently assigned to QImageIOHandler.
void setFormat(const QByteArray &format)
Sets the format QImageReader will use when reading images, to format.
Q_CORE_EXPORT void qDebug(const char *,...)
QMap< QString, QString > text
virtual void setOption(ImageOption option, const QVariant &value)
Sets the option option with the value value.
void setScaledClipRect(const QRect &rect)
Sets the scaled clip rect to rect.
bool decideFormatFromContent() const
Returns whether the image reader should decide which plugin to use only based on the contents of the ...
QString errorString() const
Returns a human readable description of the last error that occurred.
QStringList keys() const
bool supportsAnimation() const
Returns true if the image format supports animation; otherwise, false is returned.
int indexOf(const QRegExp &rx, int from=0) const
Returns the index position of the first exact match of rx in the list, searching forward from index p...
Definition: qstringlist.h:195
bool autoDetectImageFormat() const
Returns true if image format autodetection is enabled on this image reader; otherwise returns false...
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool isOpen() const
Returns true if the device is open; otherwise returns false.
Definition: qiodevice.cpp:530
static bool isEmpty(const char *str)
bool canRead() const
Returns true if an image can be read from the device (i.
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
ImageOption
This enum describes the different options supported by QImageIOHandler.
int currentImageNumber() const
For image formats that support animation, this function returns the sequence number of the current fr...
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499
void setFileName(const QString &fileName)
Sets the file name of QImageReader to fileName.
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
int nextImageDelay() const
For image formats that support animation, this function returns the number of milliseconds to wait un...
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
Q_CORE_EXPORT void qWarning(const char *,...)
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
bool canRead() const
Returns true if an image can be read from the device (i.
QImageReader * q
virtual int loopCount() const
For image formats that support animation, this function returns the number of times the animation sho...
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
virtual int nextImageDelay() const
For image formats that support animation, this function returns the number of milliseconds to wait un...
QList< Key > keys() const
Returns a list containing all the keys in the map in ascending order.
Definition: qmap.h:818
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
__int64 qint64
Definition: qglobal.h:942
QByteArray format() const
Returns the format QImageReader uses for reading images.
QByteArray testFormat
QSize toSize() const
Returns the variant as a QSize if the variant has type() Size ; otherwise returns an invalid QSize...
Definition: qvariant.cpp:2432
_qt_BuiltInFormatType type
void qSort(RandomAccessIterator start, RandomAccessIterator end)
Definition: qalgorithms.h:177
The QImageReader class provides a format independent interface for reading images from files or other...
Definition: qimagereader.h:62
virtual int imageCount() const
For image formats that support animation, this function returns the number of images in the animation...
virtual bool isSequential() const
Returns true if this device is sequential; otherwise returns false.
Definition: qiodevice.cpp:454
virtual QImageIOHandler * create(QIODevice *device, const QByteArray &format=QByteArray()) const =0
Creates and returns a QImageIOHandler subclass, with device and format set.
bool canRead() const
Returns true if an image can be read for the device (i.e., the image format is supported, and the device seems to contain valid data); otherwise returns false.
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
int loopCount() const
For image formats that support animation, this function returns the number of times the animation sho...
void setDevice(QIODevice *device)
Sets the device of the QImageIOHandler to device.
int indexOf(const T &t, int from=0) const
Returns the index position of the first occurrence of value in the list, searching forward from index...
Definition: qlist.h:847
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
QImageReader()
Constructs an empty QImageReader object.
bool canRead() const
Returns true if an image can be read from the device (i.
The QImageIOPlugin class defines an interface for writing an image format plugin. ...
QString simplified() const Q_REQUIRED_RESULT
Returns a string that has whitespace removed from the start and the end, and that has each sequence o...
Definition: qstring.cpp:4415
The QImageIOHandler class defines the common image I/O interface for all image formats in Qt...
static QImageIOHandler * createReadHandlerHelper(QIODevice *device, const QByteArray &format, bool autoDetectImageFormat, bool ignoresFormatAndExtension)
QIODevice * device() const
Returns the device currently assigned to QImageReader, or 0 if no device has been assigned...
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:559
QObject * instance(const QString &key) const
QSize scaledSize() const
Returns the scaled size of the image.
int key
QByteArray suffix
QRect clipRect() const
Returns the clip rect (also known as the ROI, or Region Of Interest) of the image.
void setAutoDetectImageFormat(bool enabled)
If enabled is true, image format autodetection is enabled; otherwise, it is disabled.
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
bool isEmpty() const
Returns true if the map contains no items; otherwise returns false.
Definition: qmap.h:203
ImageReaderError
This enum describes the different types of errors that can occur when reading images with QImageReade...
Definition: qimagereader.h:65
bool canRead() const
Returns true if an image can be read from the device (i.
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
bool isValid() const
Returns true if both the width and height is equal to or greater than 0; otherwise returns false...
Definition: qsize.h:123
QFactoryLoader * l
const_iterator constBegin() const
Definition: qset.h:168
virtual int currentImageNumber() const
For image formats that support animation, this function returns the sequence number of the current im...
QImageIOHandler * handler
static const _qt_BuiltInFormatStruct _qt_BuiltInFormats[]
QStringList textKeys() const
Returns the text keys for this image.
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
Definition: qiodevice.cpp:570
quint16 index
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
int quality() const
Returns the quality level of the image.
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
QSize size() const
Returns the size of the image, without actually reading the image contents.
virtual QVariant option(ImageOption option) const
Returns the value assigned to option as a QVariant.
ImageReaderError error() const
Returns the type of error that occurred last.
QImage scaled(int w, int h, Qt::AspectRatioMode aspectMode=Qt::IgnoreAspectRatio, Qt::TransformationMode mode=Qt::FastTransformation) const
Definition: qimage.h:232
bool isValid() const
Returns true if the rectangle is valid, otherwise returns false.
Definition: qrect.h:237
bool supportsOption(QImageIOHandler::ImageOption option) const
Returns true if the reader supports option; otherwise returns false.
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
QImageIOHandler * handler
bool canRead() const
Returns true if an image can be read from the device (i.
virtual bool supportsOption(ImageOption option) const
Returns true if the QImageIOHandler supports the option option; otherwise returns false...
virtual bool canRead() const
Reimplemented Function
void setBackgroundColor(const QColor &color)
Sets the background color to color.
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
bool canRead() const
Returns true if an image can be read from the device (i.
void setDecideFormatFromContent(bool ignored)
If ignored is set to true, then the image reader will ignore specified formats or file extensions and...
bool jumpToNextImage()
For image formats that support animation, this function steps over the current image, returning true if successful or false if there is no following image in the animation.
virtual bool seek(qint64 pos)
For random-access devices, this function sets the current position to pos, returning true on success...
Definition: qiodevice.cpp:659
static QString fileName(const QString &fileUrl)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
void swap(QList< T > &other)
Swaps list other with this list.
Definition: qlist.h:129
QImage read()
Reads an image from the device.
#define enabled
void clear()
Removes all items from the map.
Definition: qmap.h:444
void setFileName(const QString &name)
Sets the name of the file.
Definition: qfile.cpp:494
int imageCount() const
For image formats that support animation, this function returns the total number of images in the ani...
Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,(QImageIOHandlerFactoryInterface_iid, QLatin1String("/imageformats"))) enum _qt_BuiltInFormatType
virtual bool canRead() const =0
Returns true if an image can be read from the device (i.
virtual QRect currentImageRect() const
Returns the rect of the current image.
QString fileName() const
If the currently assigned device is a QFile, or if setFileName() has been called, this function retur...