PDA

View Full Version : Loading corrupt jpeg images with QImage



mikeee7
1st April 2009, 15:31
Hi,

I am using QImage to scale and save images downloaded from the net using QNetworkAccessManager.

99.9% of the images can be saved correctly but for a very small fraction of images I get the following error/warning messages printed on the console:

Corrupt JPEG data: premature end of data segment
or
Corrupt JPEG data: 164 extraneous bytes before marker 0xd9

I would be interested in catching those errors and discarding the corresponding images. I wonder if anyone knows a way to do this? QImage::loadFromData() returns true even for images that produce those error messages.

Thanks,
mikeee7

talk2amulya
1st April 2009, 16:54
probably you arent able to read the images properly from net..are you relying on the finished signal from QNetworkAccessManager?

mikeee7
1st April 2009, 17:27
probably you arent able to read the images properly from net..are you relying on the finished signal from QNetworkAccessManager?

Yes, I am relying on the finished signal from QNetworkAccessManager.
Basically, I have the following code:




QByteArray data = reply->readAll();
reply->close();

// construct QImage from data
QImage img;
bool loaded = img.loadFromData(data);



My problem is that "img.loadFromData(data)" returns true in any case, even if the mentioned error messages get printed onto the console. I would be interested in finding a way to catch those error messages such that I would be able to discard the corresponding images.

talk2amulya
1st April 2009, 17:44
try using QImageReader , it has functions like canRead() and read() which might be helpful to you

mikeee7
1st April 2009, 18:17
Hi,

thanks for your help!

I've tried QImageReader as suggested but the functions canRead() and read() cannot detect the corrupt jpeg data either.

Looking at the QImage source code, one can see that QImage internally of course relies on QImageReader and thus only fails when QImageReader fails:



bool QImage::load(const QString &fileName, const char* format)
{
if (fileName.isEmpty())
return false;

QImage image = QImageReader(fileName, format).read();
if (!image.isNull()) {
operator=(image);
return true;
}
return false;
}


Any other ideas for detecting images that cause the "Corrupt JPEG data" error message?

Thanks,
mikeee7

talk2amulya
1st April 2009, 18:59
hmm..thats really odd..anyways, u can maybe try using error() and errorString() of QImageReader, if error is there, u can discard corresponding images

mikeee7
2nd April 2009, 17:06
Hi,

I tried QImageReader::error() but it does not seem to cover the "Corrupt JPEG data" error.

I found a thread covering the same problem but there is no real solution:

http://www.qtcentre.org/forum/f-qt-programming-2/t-use-qimagereader-to-test-image-files-19703.html/

It would be nice to have some additional error enums included into the QImageReader API that allow users to detect the "Corrupt JPEG data" error...

I wonder if anyone has other ideas to detect those errors except writing the data into a logfile and analyzing the logfile afterwards?

Thanks,
mikeee7

wysota
2nd April 2009, 21:24
The corruption message comes from libjpeg which is responsible for decoding the image. It succeeds to do it regardless of data corruption thus QImageReader returns true. I don't think there is anything you can do about it, unfortunately. Unless of course libjpeg can report such problems - then it'd be just a matter of slightly modifying the jpeg imageio plugin.

talk2amulya
2nd April 2009, 22:31
so wysota, do you think this should be taken up to the trolls? i mean it makes sense that if an image is corrupted, it should be reported..or is it like they know the issue and they r just playing ostrich on it?

wysota
2nd April 2009, 23:10
You can report it of course it would be worth to check first if libjpeg has any means of notifying about such corruption. If not, Trolls won't be able to do anything about it other than patching libjpeg which is doubtful they would do.

mikeee7
3rd April 2009, 12:07
Thanks for your help!

I reported the problem in the Qt tasktracker.

wysota
3rd April 2009, 13:38
Did you check if libjpeg is able to report such errors?

mikeee7
3rd April 2009, 19:46
libjpeg provides very detailed status codes, see http://libjpeg.cvs.sourceforge.net/viewvc/libjpeg/libjpeg/jerror.h?view=markup. However, qjpeghandler.cpp does not seem to check them. The corresponding function is

static bool read_jpeg_image(QIODevice *device, QImage *outImage, const QByteArray &parameters, QSize scaledSize, int inQuality )

Also, it seems that the interface provided by QImageIOHandler would need to be extended to be able to forward additional "status messages" since currently the read() and canRead() methods only return boolean values.

talk2amulya
3rd April 2009, 19:57
i always wanted to find a proper bug in Qt..it always seemed so perfect and foolproof..and here we are..good work miky :)

mikeee7
17th April 2009, 16:21
The problem discussed can now be found in the tasktracker:
http://www.qtsoftware.com/developer/task-tracker/index_html?method=entry&id=251514

luisfmss
3rd December 2010, 01:59
I'm facing the same issue, and no solutions has been reported on the Bug Tracker.
How did you solve it? Have you found a way to detect the corrupted images?
Thanks