PDA

View Full Version : QImageReader fails inconsistently with "Unable to read image data".



mlr
18th June 2011, 00:44
I am using the QImageReader to read in all of the images in a directory. However it is sometimes unable to read certain images, even if it has read them successfully in the past. The error message is "Unable to read image data". Once there is an error on one file in a directory there tends to be an error on all subsequent files. Can someone explain what is going on? Here is my code:



QStringList filters;
foreach (QByteArray format, QImageReader::supportedImageFormats())
filters += "*." + format;
dir.setSorting(QDir::Time | QDir::Reversed);

foreach (QFileInfo fileInfo, dir.entryInfoList(filters, QDir::Files))
{
QString filename = fileInfo.fileName();
QString pathname = fileInfo.absoluteFilePath();

if (!imageReader)
{
imageReader = new QImageReader(pathname);
}
else
{
imageReader->setFileName(pathname);
}
QImage image = imageReader->read();
if (!image.isNull())
{
Image* myImage = new Image(image);
myImage->s_pathname(pathname);
myImage->s_timestamp (fileInfo.lastModified());
QList<Image *> *imglist = g_imageList();
imglist->append(myImage);
}
else
{
/*
** Sometimes get the error "Unable to read image data" which is the
** "InvalidDataError". But it is inconsistent. The same file is
** successfully read on other occasions.
** When it fails with one file tends to fail with all other files from that
** directory. (filename is always ok).
*/
QString errstr = imageReader->errorString();
QString path = imageReader->fileName();
}
}

wysota
18th June 2011, 00:54
It seems like a resouce problem, maybe with not enough RAM available? Or some system policy?

ChrisW67
18th June 2011, 03:48
Possibly related... If imagelist is NULL (line 11) then you allocate one (13) but don't set the file name before trying to read at line 19.

stampede
18th June 2011, 09:22
I think constructor at line 13 takes file name as argument.

wysota
18th June 2011, 10:23
Looking at the code again, the first thing I'd do is to get rid of all those unnecessary pointers.

ChrisW67
18th June 2011, 10:33
Oh yes, my bad.

mlr
20th June 2011, 20:39
It does appear to be a resource issue. I am going to try scaling the QImages after I read them, deleting the larger QImage and saving the scaled image and see if that resolves the problem.

wysota
20th June 2011, 22:18
QImageReader has the capability of reading the images already scaled down, at least for some formats.

mlr
20th June 2011, 23:20
The issue is resolved. After reading in the image with the QImageReader I scale it to make it the dimensions I will draw it at and delete the original image. The resource issue no longer occurs in this situation and all of the images get drawn. But the performance is very slow where there are 100s of large images (4288 x 2848) so I want to see if I can just read in the thumbnail to begin with and not have to read in all of those large images.

wysota
21st June 2011, 00:02
Read what I said again -- you can load the image already scaled. Performance will be much better then. See QImageReader::setScaledSize().

mlr
21st June 2011, 00:06
Reading the image in scaled down form performs better than scaling down after reading.

ChrisW67
21st June 2011, 02:05
A 4288 x 2848 image with 24-bits per pixel is ~36MB per image uncompressed in memory (48MB if there's an alpha channel). Loading lots of these is certainly a rapid way to consume memory.