162 bool autoDetectImageFormat,
163 bool ignoresFormatAndExtension)
165 if (!autoDetectImageFormat && format.isEmpty())
169 QImageIOHandler *handler =
nullptr;
172 qCDebug(lcImageReader) <<
"Finding read handler for" << device <<
"and format" << format;
174#if QT_CONFIG(imageformatplugin)
175 Q_CONSTINIT
static QBasicMutex mutex;
176 const auto locker = qt_scoped_lock(mutex);
178 typedef QMultiMap<
int, QString> PluginKeyMap;
181 auto l = QImageReaderWriterHelpers::pluginLoader();
182 const PluginKeyMap keyMap = l->keyMap();
184 qCDebug(lcImageReader) << keyMap.uniqueKeys().size() <<
"plugins available:" << keyMap.values();
186 int testFormatPluginIndex = -1;
189 if (device && format.isEmpty() && autoDetectImageFormat && !ignoresFormatAndExtension) {
191 if (QFile *file = qobject_cast<QFile *>(device)) {
192 suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1();
193 qCDebug(lcImageReader) <<
"Resolved format" << suffix <<
"from file name suffix";
197 QByteArray testFormat = !form.isEmpty() ? form : suffix;
198 if (ignoresFormatAndExtension)
199 testFormat = QByteArray();
201#if QT_CONFIG(imageformatplugin)
202 if (!testFormat.isEmpty()) {
205 qCDebug(lcImageReader) <<
"Checking if any plugins have explicitly declared support"
206 <<
"for the format" << testFormat;
207 const qint64 pos = device ? device->pos() : 0;
208 const auto keyList = keyMap.keys(QLatin1StringView(testFormat));
209 for (
int testIndex : keyList) {
210 QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(testIndex));
211 if (plugin && plugin->capabilities(device, testFormat) & QImageIOPlugin::CanRead) {
212 qCDebug(lcImageReader) << plugin <<
"can read the format" << testFormat;
213 handler = plugin->create(device, testFormat);
214 testFormatPluginIndex = testIndex;
218 if (device && !device->isSequential())
225 if (!handler && !testFormat.isEmpty()) {
226 qCDebug(lcImageReader) <<
"Checking if any built in handlers recognize the format"
229#ifndef QT_NO_IMAGEFORMAT_PNG
230 }
else if (testFormat ==
"png") {
231 handler =
new QPngHandler;
233#ifndef QT_NO_IMAGEFORMAT_BMP
234 }
else if (testFormat ==
"bmp") {
235 handler =
new QBmpHandler;
236 }
else if (testFormat ==
"dib") {
237 handler =
new QBmpHandler(QBmpHandler::DibFormat);
239#ifndef QT_NO_IMAGEFORMAT_XPM
240 }
else if (testFormat ==
"xpm") {
241 handler =
new QXpmHandler;
243#ifndef QT_NO_IMAGEFORMAT_XBM
244 }
else if (testFormat ==
"xbm") {
245 handler =
new QXbmHandler;
246 handler->setOption(QImageIOHandler::SubType, testFormat);
248#ifndef QT_NO_IMAGEFORMAT_PPM
249 }
else if (testFormat ==
"pbm" || testFormat ==
"pbmraw" || testFormat ==
"pgm"
250 || testFormat ==
"pgmraw" || testFormat ==
"ppm" || testFormat ==
"ppmraw") {
251 handler =
new QPpmHandler;
252 handler->setOption(QImageIOHandler::SubType, testFormat);
257 qCDebug(lcImageReader) <<
"Using the built-in handler for format" << testFormat;
260#if QT_CONFIG(imageformatplugin)
261 if (!handler && !testFormat.isEmpty() && autoDetectImageFormat) {
264 qCDebug(lcImageReader) <<
"Checking if any plugins recognize the format" << testFormat;
266 const qint64 pos = device ? device->pos() : 0;
268 const int keyCount = keyMap.size();
269 for (
int i = 0; i < keyCount; ++i) {
270 if (i != testFormatPluginIndex) {
271 QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(i));
272 if (plugin && plugin->capabilities(device, testFormat) & QImageIOPlugin::CanRead) {
273 qCDebug(lcImageReader) << plugin <<
"can read the format" << testFormat;
274 handler = plugin->create(device, testFormat);
279 if (device && !device->isSequential())
284 if (!handler && !form.isEmpty() && !ignoresFormatAndExtension) {
286 qCDebug(lcImageReader) <<
"No handler found for explicitly requested format" << form;
290 if (handler && device && !suffix.isEmpty()) {
291 Q_ASSERT(qobject_cast<QFile *>(device));
294 const qint64 pos = device->pos();
295 handler->setDevice(device);
297 handler->setFormat(form);
298 bool canRead = handler->canRead();
304 qCDebug(lcImageReader) << handler <<
"claimed support for" << suffix
305 <<
"but could not read the file";
312#if QT_CONFIG(imageformatplugin)
313 if (!handler && (autoDetectImageFormat || ignoresFormatAndExtension)) {
315 qCDebug(lcImageReader) <<
"Checking if any plugins recognize the format"
316 <<
"based on the contents in" << device;
317 const qint64 pos = device ? device->pos() : 0;
318 const int keyCount = keyMap.size();
319 for (
int i = 0; i < keyCount; ++i) {
320 if (i != testFormatPluginIndex) {
321 QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(i));
322 if (plugin && plugin->capabilities(device, QByteArray()) & QImageIOPlugin::CanRead) {
323 handler = plugin->create(device, testFormat);
324 qCDebug(lcImageReader) << plugin <<
"can read the data in" << device;
329 if (device && !device->isSequential())
334 if (!handler && (autoDetectImageFormat || ignoresFormatAndExtension)) {
337 qCDebug(lcImageReader) <<
"Checking if any built in handlers recognize the format"
338 <<
"based on the contents in" << device;
339 int currentFormat = 0;
340 if (!suffix.isEmpty()) {
343 for (
int i = 0; i < _qt_NumFormats; ++i) {
344 if (_qt_BuiltInFormats[i].extension == suffix) {
352 int numFormats = _qt_NumFormats;
353 while (device && numFormats >= 0) {
354 const qint64 pos = device->pos();
355 switch (currentFormat) {
356#ifndef QT_NO_IMAGEFORMAT_PNG
358 if (QPngHandler::canRead(device))
359 handler =
new QPngHandler;
362#ifndef QT_NO_IMAGEFORMAT_BMP
364 if (QBmpHandler::canRead(device))
365 handler =
new QBmpHandler;
368#ifndef QT_NO_IMAGEFORMAT_XPM
370 if (QXpmHandler::canRead(device))
371 handler =
new QXpmHandler;
374#ifndef QT_NO_IMAGEFORMAT_PPM
378 if (QPpmHandler::canRead(device, &subType)) {
379 handler =
new QPpmHandler;
380 handler->setOption(QImageIOHandler::SubType, subType);
384#ifndef QT_NO_IMAGEFORMAT_XBM
386 if (QXbmHandler::canRead(device))
387 handler =
new QXbmHandler;
393 if (!device->isSequential())
397 qCDebug(lcImageReader,
"The %s built-in handler can read this data",
398 _qt_BuiltInFormats[currentFormat].extension);
404 if (currentFormat >= _qt_NumFormats)
410 qCDebug(lcImageReader,
"No handlers found. Giving up.");
415 handler->setDevice(device);
417 handler->setFormat(form);
496 imageReaderError = QImageReader::DeviceError;
497 errorString = QImageReader::tr(
"Invalid device");
503 Q_ASSERT(qobject_cast<QFile*>(device) !=
nullptr);
504 QFile *file =
static_cast<QFile *>(device);
506 if (file->error() == QFileDevice::ResourceError) {
508 imageReaderError = QImageReader::DeviceError;
509 errorString = file->errorString();
513 QString fileName = file->fileName();
514 bool fileIsOpen =
false;
517 QList<QByteArray> extensions = QImageReader::supportedImageFormats();
518 if (!format.isEmpty()) {
520 int currentFormatIndex = extensions.indexOf(format.toLower());
521 if (currentFormatIndex > 0)
522 extensions.swapItemsAt(0, currentFormatIndex);
525 int currentExtension = 0;
527 file->setFileName(fileName + u'.'
528 + QLatin1StringView(extensions.at(currentExtension++).constData()));
529 fileIsOpen = file->open(QIODevice::ReadOnly);
530 }
while (!fileIsOpen && currentExtension < extensions.size());
534 imageReaderError = QImageReader::FileNotFoundError;
535 errorString = QImageReader::tr(
"File not found");
536 file->setFileName(fileName);
542 if ((handler = createReadHandlerHelper(device, format, autoDetectImageFormat, ignoresFormatAndExtension)) ==
nullptr) {
543 imageReaderError = QImageReader::UnsupportedFormatError;
544 errorString = QImageReader::tr(
"Unsupported image format");
1192bool QImageReader::read(QImage *image)
1195 qWarning(
"QImageReader::read: cannot read into null pointer");
1199 if (!d->initHandler())
1202 QSize scaledSize = d->scaledSize;
1203 if ((scaledSize.width() <= 0 && scaledSize.height() > 0) ||
1204 (scaledSize.height() <= 0 && scaledSize.width() > 0)) {
1207 if (
const QSize originalSize = size(); !originalSize.isEmpty()) {
1208 if (scaledSize.width() <= 0) {
1209 const auto ratio = qreal(scaledSize.height()) / originalSize.height();
1210 if (
const qreal newWidth = originalSize.width() * ratio; newWidth < INT_MAX)
1211 scaledSize.setWidth(qMax(1, qRound(newWidth)));
1213 const auto ratio = qreal(scaledSize.width()) / originalSize.width();
1214 if (
const qreal newHeight = originalSize.height() * ratio; newHeight < INT_MAX)
1215 scaledSize.setHeight(qMax(1, qRound(newHeight)));
1218 if (scaledSize.isEmpty()) {
1219 d->imageReaderError = InvalidDataError;
1220 d->errorString = QImageReader::tr(
"Scaled size out of range");
1226 const bool supportScaledSize = supportsOption(QImageIOHandler::ScaledSize) && scaledSize.isValid();
1227 const bool supportClipRect = supportsOption(QImageIOHandler::ClipRect) && !d->clipRect.isNull();
1228 const bool supportScaledClipRect = supportsOption(QImageIOHandler::ScaledClipRect) && !d->scaledClipRect.isNull();
1231 if (supportScaledSize) {
1232 if (supportClipRect || d->clipRect.isNull()) {
1235 d->handler->setOption(QImageIOHandler::ScaledSize, scaledSize);
1238 if (supportClipRect)
1239 d->handler->setOption(QImageIOHandler::ClipRect, d->clipRect);
1240 if (supportScaledClipRect)
1241 d->handler->setOption(QImageIOHandler::ScaledClipRect, d->scaledClipRect);
1242 if (supportsOption(QImageIOHandler::Quality))
1243 d->handler->setOption(QImageIOHandler::Quality, d->quality);
1246 QString filename = fileName();
1247 if (Q_TRACE_ENABLED(QImageReader_read_before_reading)) {
1248 Q_TRACE(QImageReader_read_before_reading,
this, filename.isEmpty() ? u"unknown"_s : filename);
1251 const bool result = d->handler->read(image);
1253 Q_TRACE(QImageReader_read_after_reading,
this, result);
1256 d->imageReaderError = InvalidDataError;
1257 d->errorString = QImageReader::tr(
"Unable to read image data");
1262 const auto scaleImage = [&] {
1263 if (QImageData::isWithinIOAllocationLimit(scaledSize.width(), scaledSize.height(),
1264 image->format(),
"QImageReader")) {
1265 *image = image->scaled(scaledSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
1266 if (!image->isNull())
1270 d->imageReaderError = InvalidDataError;
1271 d->errorString = QImageReader::tr(
"Unable to scale image");
1277 if (supportClipRect) {
1278 if (supportScaledSize) {
1279 if (supportScaledClipRect) {
1283 if (!d->scaledClipRect.isNull())
1284 *image = image->copy(d->scaledClipRect);
1287 if (supportScaledClipRect) {
1291 if (scaledSize.isValid() && !scaleImage())
1293 if (d->scaledClipRect.isValid()) {
1294 *image = image->copy(d->scaledClipRect);
1299 if (supportScaledSize && d->clipRect.isNull()) {
1300 if (supportScaledClipRect) {
1304 if (d->scaledClipRect.isValid()) {
1305 *image = image->copy(d->scaledClipRect);
1309 if (supportScaledClipRect) {
1315 if (d->clipRect.isValid())
1316 *image = image->copy(d->clipRect);
1317 if (scaledSize.isValid() && !scaleImage())
1319 if (d->scaledClipRect.isValid())
1320 *image = image->copy(d->scaledClipRect);
1326 static bool disableNxImageLoading = !qEnvironmentVariableIsEmpty(
"QT_HIGHDPI_DISABLE_2X_IMAGE_LOADING");
1327 if (!disableNxImageLoading) {
1328 const QByteArray suffix = QFileInfo(filename).baseName().right(3).toLatin1();
1329 if (suffix.size() == 3 && suffix[0] ==
'@' && suffix[1] >=
'2' && suffix[1] <=
'9' && suffix[2] ==
'x')
1330 image->setDevicePixelRatio(suffix[1] -
'0');
1332 if (autoTransform())
1333 qt_imageTransform(*image, transformation());