load image report Corrupt JPEG data: premature end of data segment
-
QImageReader reader(frontfile.c_str()); QSize displaySize(mImageWidth, mImageHeight); //scale image size reader.setScaledSize(displaySize); QImage mFrontImage = reader.read(); QPixmap mPixImage = QPixmap::fromImage(mFrontImage);
report : Corrupt JPEG data: premature end of data segment
-
@zhouyinlong and what is the question? The error is clear - your jpeg is corrupt.
-
@Christian-Ehrlicher The image load shows part of it and the rest of it is grayed out, but the actual picture path is normal
-
@Christian-Ehrlicher
like this
-
So use a non-corrupt image. When the jpeg library tells you the image can not be loaded then I don't see what you can do except providing a non corrupt one.
-
@Christian-Ehrlicher
Local images are always normal, what causes this phenomenon and what can I do to avoid it
-
@zhouyinlong said in load image report Corrupt JPEG data: premature end of data segment:
what causes this phenomenon and what can I do to avoid it
Corrupt inputs cause corrupt output and warnings about it.
You fix it by providing valid inputs.
There is no magic.Here's some test data generated by ImageMagick and deliberately broken. Note that the "bad.jpeg" looks to have the correct dimensions because the header part of the file is intact.
$ convert -geometry 200x200 plasma: good.jpeg $ ls -l good.jpeg -rw-rw-r-- 1 chrisw chrisw 22115 Mar 13 16:48 good.jpeg $ head --bytes=10000 good.jpeg > bad.jpeg $ identify *.jpeg bad.jpeg JPEG 200x200 200x200+0+0 8-bit sRGB 10000B 0.000u 0:00.000 good.jpeg JPEG 200x200 200x200+0+0 8-bit sRGB 22115B 0.000u 0:00.000 $ display bad.jpeg display-im6.q16: Premature end of JPEG file `bad.jpeg' @ warning/jpeg.c/JPEGWarningHandler/389. display-im6.q16: Corrupt JPEG data: premature end of data segment `bad.jpeg' @ warning/jpeg.c/JPEGWarningHandler/389.
The ImageMagick display program gives similar corruption message to Qt but does its best to display what made sense (as does Qt). Good and bad inputs side-by-side:
#include <QApplication> #include <QImage> #include <QImageReader> #include <QPixmap> #include <QDebug> #include <string> int main(int argc, char **argv) { QApplication app(argc, argv); std::string frontfile("good.jpeg"); // std::string frontfile("bad.jpeg"); int mImageWidth(100); int mImageHeight(100); QImageReader reader(frontfile.c_str()); QSize displaySize(mImageWidth, mImageHeight); //scale image size reader.setScaledSize(displaySize); QImage mFrontImage = reader.read(); qDebug() << mFrontImage; QPixmap mPixImage = QPixmap::fromImage(mFrontImage); qDebug() << mPixImage; frontfile += ".png"; mPixImage.save(frontfile.c_str()); return 0; }
Outputs:
# For good.jpeg $ ./test QImage(QSize(100, 100),format=QImage::Format_RGB32,depth=32,devicePixelRatio=1,bytesPerLine=400,sizeInBytes=40000) QPixmap(QSize(100, 100),depth=32,devicePixelRatio=1,cacheKey=0x100000064) # for bad.jpeg $ ./test qt.gui.imageio.jpeg: Corrupt JPEG data: premature end of data segment QImage(QSize(100, 100),format=QImage::Format_RGB32,depth=32,devicePixelRatio=1,bytesPerLine=400,sizeInBytes=40000) QPixmap(QSize(100, 100),depth=32,devicePixelRatio=1,cacheKey=0x100000064)
-
@ChrisW67 I understand that you mean that the reason for this problem is that there is something wrong with the picture itself. Is this error output for the picture you have problems with every time? When loading the same picture, only part of the picture is displayed occasionally, and I can display it correctly in most cases? Is this problem caused by the decoding process
-
@zhouyinlong Intermittent behaviour would have been good to mention in your first post. All your example images show an image corrupted from the same place.
If you give Qt the same image data every time then you get the same result every time. If it sometimes works and sometimes does not then you have to consider that the input image is changing (long before any more abstract causes). For example, you are grabbing the image over the network and truncating it accidentally by mishandling the transfer, or you are trampling over a buffer with out-of-bounds memory accesses.
We cannot see your actual code or input image.