Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. load image report Corrupt JPEG data: premature end of data segment

load image report Corrupt JPEG data: premature end of data segment

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zhouyinlong
    wrote on last edited by
    #1
    	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

    Christian EhrlicherC 1 Reply Last reply
    0
    • Z zhouyinlong
      	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

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @zhouyinlong and what is the question? The error is clear - your jpeg is corrupt.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      Z 2 Replies Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @zhouyinlong and what is the question? The error is clear - your jpeg is corrupt.

        Z Offline
        Z Offline
        zhouyinlong
        wrote on last edited by
        #3

        @Christian-Ehrlicher The image load shows part of it and the rest of it is grayed out, but the actual picture path is normal

        1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          @zhouyinlong and what is the question? The error is clear - your jpeg is corrupt.

          Z Offline
          Z Offline
          zhouyinlong
          wrote on last edited by
          #4

          @Christian-Ehrlicher 39513e3c-b689-4c65-ae8d-b136786c9538-image.png like this

          Christian EhrlicherC 1 Reply Last reply
          0
          • Z zhouyinlong

            @Christian-Ehrlicher 39513e3c-b689-4c65-ae8d-b136786c9538-image.png like this

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            Z 1 Reply Last reply
            2
            • Christian EhrlicherC Christian Ehrlicher

              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.

              Z Offline
              Z Offline
              zhouyinlong
              wrote on last edited by
              #6

              @Christian-Ehrlicher 938db1d6-fc4c-43a0-8cdb-6a0f3c0e17a1-微信截图_20240313112043.png Local images are always normal, what causes this phenomenon and what can I do to avoid it

              C 1 Reply Last reply
              0
              • Z zhouyinlong

                @Christian-Ehrlicher 938db1d6-fc4c-43a0-8cdb-6a0f3c0e17a1-微信截图_20240313112043.png Local images are always normal, what causes this phenomenon and what can I do to avoid it

                C Offline
                C Offline
                ChrisW67
                wrote on last edited by
                #7

                @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:
                11d35bb9-94cd-410d-9b62-8fa5da7bf509-both.jpeg

                #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)
                

                3a6b1c90-f700-4dd4-848f-a9fee15472a7-both.jpeg.png

                Z 1 Reply Last reply
                3
                • C ChrisW67

                  @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:
                  11d35bb9-94cd-410d-9b62-8fa5da7bf509-both.jpeg

                  #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)
                  

                  3a6b1c90-f700-4dd4-848f-a9fee15472a7-both.jpeg.png

                  Z Offline
                  Z Offline
                  zhouyinlong
                  wrote on last edited by
                  #8

                  @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

                  C 1 Reply Last reply
                  0
                  • Z zhouyinlong

                    @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

                    C Offline
                    C Offline
                    ChrisW67
                    wrote on last edited by
                    #9

                    @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.

                    1 Reply Last reply
                    2

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved