src/gui/image/qimagereader.cpp
changeset 30 5dc02b23752f
parent 18 2f34d5167611
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
   142 #include <private/qpnghandler_p.h>
   142 #include <private/qpnghandler_p.h>
   143 #endif
   143 #endif
   144 
   144 
   145 QT_BEGIN_NAMESPACE
   145 QT_BEGIN_NAMESPACE
   146 
   146 
   147 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
   147 #ifndef QT_NO_LIBRARY
   148 Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
   148 Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
   149                           (QImageIOHandlerFactoryInterface_iid, QLatin1String("/imageformats")))
   149                           (QImageIOHandlerFactoryInterface_iid, QLatin1String("/imageformats")))
   150 #endif
   150 #endif
   151 
   151 
   152 enum _qt_BuiltInFormatType {
   152 enum _qt_BuiltInFormatType {
   203         return 0;
   203         return 0;
   204 
   204 
   205     QByteArray form = format.toLower();
   205     QByteArray form = format.toLower();
   206     QImageIOHandler *handler = 0;
   206     QImageIOHandler *handler = 0;
   207 
   207 
   208 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
   208 #ifndef QT_NO_LIBRARY
   209     // check if we have plugins that support the image format
   209     // check if we have plugins that support the image format
   210     QFactoryLoader *l = loader();
   210     QFactoryLoader *l = loader();
   211     QStringList keys = l->keys();
   211     QStringList keys = l->keys();
   212 #endif
   212 #endif
   213     QByteArray suffix;
   213     QByteArray suffix;
   215 #ifdef QIMAGEREADER_DEBUG
   215 #ifdef QIMAGEREADER_DEBUG
   216     qDebug() << "QImageReader::createReadHandler( device =" << (void *)device << ", format =" << format << "),"
   216     qDebug() << "QImageReader::createReadHandler( device =" << (void *)device << ", format =" << format << "),"
   217              << keys.size() << "plugins available: " << keys;
   217              << keys.size() << "plugins available: " << keys;
   218 #endif
   218 #endif
   219 
   219 
   220 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
   220 #ifndef QT_NO_LIBRARY
   221     int suffixPluginIndex = -1;
   221     int suffixPluginIndex = -1;
   222     if (device && format.isEmpty() && autoDetectImageFormat && !ignoresFormatAndExtension) {
   222     if (device && format.isEmpty() && autoDetectImageFormat && !ignoresFormatAndExtension) {
   223         // if there's no format, see if \a device is a file, and if so, find
   223         // if there's no format, see if \a device is a file, and if so, find
   224         // the file suffix and find support for that format among our plugins.
   224         // the file suffix and find support for that format among our plugins.
   225         // this allows plugins to override our built-in handlers.
   225         // this allows plugins to override our built-in handlers.
   244     QByteArray testFormat = !form.isEmpty() ? form : suffix;
   244     QByteArray testFormat = !form.isEmpty() ? form : suffix;
   245 
   245 
   246     if (ignoresFormatAndExtension)
   246     if (ignoresFormatAndExtension)
   247         testFormat = QByteArray();
   247         testFormat = QByteArray();
   248 
   248 
   249 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
   249 #ifndef QT_NO_LIBRARY
   250     if (suffixPluginIndex != -1) {
   250     if (suffixPluginIndex != -1) {
   251         // check if the plugin that claims support for this format can load
   251         // check if the plugin that claims support for this format can load
   252         // from this device with this format.
   252         // from this device with this format.
   253         const qint64 pos = device ? device->pos() : 0;
   253         const qint64 pos = device ? device->pos() : 0;
   254         QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(QString::fromLatin1(suffix)));
   254         QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(QString::fromLatin1(suffix)));
   329         if (handler)
   329         if (handler)
   330             qDebug() << "QImageReader::createReadHandler: using the built-in handler for" << testFormat;
   330             qDebug() << "QImageReader::createReadHandler: using the built-in handler for" << testFormat;
   331 #endif
   331 #endif
   332     }
   332     }
   333 
   333 
   334 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
   334 #ifndef QT_NO_LIBRARY
   335     if (!handler && (autoDetectImageFormat || ignoresFormatAndExtension)) {
   335     if (!handler && (autoDetectImageFormat || ignoresFormatAndExtension)) {
   336         // check if any of our plugins recognize the file from its contents.
   336         // check if any of our plugins recognize the file from its contents.
   337         const qint64 pos = device ? device->pos() : 0;
   337         const qint64 pos = device ? device->pos() : 0;
   338         for (int i = 0; i < keys.size(); ++i) {
   338         for (int i = 0; i < keys.size(); ++i) {
   339             if (i != suffixPluginIndex) {
   339             if (i != suffixPluginIndex) {
   348             }
   348             }
   349         }
   349         }
   350         if (device && !device->isSequential())
   350         if (device && !device->isSequential())
   351             device->seek(pos);
   351             device->seek(pos);
   352     }
   352     }
   353 #endif
   353 #endif // QT_NO_LIBRARY
   354 
   354 
   355     if (!handler && (autoDetectImageFormat || ignoresFormatAndExtension)) {
   355     if (!handler && (autoDetectImageFormat || ignoresFormatAndExtension)) {
   356         // check if any of our built-in handlers recognize the file from its
   356         // check if any of our built-in handlers recognize the file from its
   357         // contents.
   357         // contents.
   358         int currentFormat = 0;
   358         int currentFormat = 0;
   501     \internal
   501     \internal
   502 */
   502 */
   503 bool QImageReaderPrivate::initHandler()
   503 bool QImageReaderPrivate::initHandler()
   504 {
   504 {
   505     // check some preconditions
   505     // check some preconditions
   506     if (!device || (!deleteDevice && !device->isOpen())) {
   506     if (!device || (!deleteDevice && !device->isOpen() && !device->open(QIODevice::ReadOnly))) {
   507         imageReaderError = QImageReader::DeviceError;
   507         imageReaderError = QImageReader::DeviceError;
   508         errorString = QLatin1String(QT_TRANSLATE_NOOP(QImageReader, "Invalid device"));
   508         errorString = QLatin1String(QT_TRANSLATE_NOOP(QImageReader, "Invalid device"));
   509         return false;
   509         return false;
   510     }
   510     }
   511 
   511 
  1412 {
  1412 {
  1413     QSet<QByteArray> formats;
  1413     QSet<QByteArray> formats;
  1414     for (int i = 0; i < _qt_NumFormats; ++i)
  1414     for (int i = 0; i < _qt_NumFormats; ++i)
  1415         formats << _qt_BuiltInFormats[i].extension;
  1415         formats << _qt_BuiltInFormats[i].extension;
  1416 
  1416 
  1417 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
  1417 #ifndef QT_NO_LIBRARY
  1418     QFactoryLoader *l = loader();
  1418     QFactoryLoader *l = loader();
  1419     QStringList keys = l->keys();
  1419     QStringList keys = l->keys();
  1420 
  1420 
  1421     for (int i = 0; i < keys.count(); ++i) {
  1421     for (int i = 0; i < keys.count(); ++i) {
  1422         QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(keys.at(i)));
  1422         QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(keys.at(i)));