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. Checking that a file is a jpeg or png
Forum Updated to NodeBB v4.3 + New Features

Checking that a file is a jpeg or png

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 1.6k Views 3 Watching
  • 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.
  • PerdrixP Offline
    PerdrixP Offline
    Perdrix
    wrote on last edited by
    #1

    Given a LARGE jpeg or png file is there a way to check that it is what it purports to be without building a QImage from it?

    I will create a QImage from it (later) if it is a valid file of the specified type ...

    Thanks
    David

    Chris KawaC Paul ColbyP 2 Replies Last reply
    0
    • Paul ColbyP Paul Colby

      You can always read the first couple of bytes, and check that they match the JPEG / GIF "magic bytes" - that is, 0xFF 0xDF 0xFF for JPEGs or 0x47 0x49 0x46 (aka "GIF") for GIFs.

      See https://en.wikipedia.org/wiki/List_of_file_signatures for some common "magic bytes", including GIF and JPEG.

      That said, it wouldn't surprise me if QImage stops parsing very quickly if those bytes are not one of the supported formats, so depending on why you're checking, if you're giving QImage a stream to read from, it might not save you much effort anyway?

      Cheers.

      Edit: Or do you mean some other form of "validation"?

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

      @Paul-Colby said in Checking that a file is a jpeg or png:

      You can always read the first couple of bytes, and check that they match the JPEG / GIF "magic bytes"

      But why not use QMimeDatabase for it?

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

      Paul ColbyP 1 Reply Last reply
      3
      • PerdrixP Perdrix

        Given a LARGE jpeg or png file is there a way to check that it is what it purports to be without building a QImage from it?

        I will create a QImage from it (later) if it is a valid file of the specified type ...

        Thanks
        David

        Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Perdrix You can't tell if a file has valid data without reading it. Both formats have a standard header, so you can read just a couple bytes to see what format they claim to be. If you read a couple bytes more you can get additional info like the dimensions, bit depth etc., so you can validate for example if the size of the file matches what's specified in the header, but to know if the actual image data is corrupted or not you will have to try to load it.

        1 Reply Last reply
        0
        • PerdrixP Perdrix

          Given a LARGE jpeg or png file is there a way to check that it is what it purports to be without building a QImage from it?

          I will create a QImage from it (later) if it is a valid file of the specified type ...

          Thanks
          David

          Paul ColbyP Offline
          Paul ColbyP Offline
          Paul Colby
          wrote on last edited by Paul Colby
          #3

          You can always read the first couple of bytes, and check that they match the JPEG / GIF "magic bytes" - that is, 0xFF 0xDF 0xFF for JPEGs or 0x47 0x49 0x46 (aka "GIF") for GIFs.

          See https://en.wikipedia.org/wiki/List_of_file_signatures for some common "magic bytes", including GIF and JPEG.

          That said, it wouldn't surprise me if QImage stops parsing very quickly if those bytes are not one of the supported formats, so depending on why you're checking, if you're giving QImage a stream to read from, it might not save you much effort anyway?

          Cheers.

          Edit: Or do you mean some other form of "validation"?

          Christian EhrlicherC 1 Reply Last reply
          0
          • Paul ColbyP Paul Colby

            You can always read the first couple of bytes, and check that they match the JPEG / GIF "magic bytes" - that is, 0xFF 0xDF 0xFF for JPEGs or 0x47 0x49 0x46 (aka "GIF") for GIFs.

            See https://en.wikipedia.org/wiki/List_of_file_signatures for some common "magic bytes", including GIF and JPEG.

            That said, it wouldn't surprise me if QImage stops parsing very quickly if those bytes are not one of the supported formats, so depending on why you're checking, if you're giving QImage a stream to read from, it might not save you much effort anyway?

            Cheers.

            Edit: Or do you mean some other form of "validation"?

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

            @Paul-Colby said in Checking that a file is a jpeg or png:

            You can always read the first couple of bytes, and check that they match the JPEG / GIF "magic bytes"

            But why not use QMimeDatabase for it?

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

            Paul ColbyP 1 Reply Last reply
            3
            • Christian EhrlicherC Christian Ehrlicher

              @Paul-Colby said in Checking that a file is a jpeg or png:

              You can always read the first couple of bytes, and check that they match the JPEG / GIF "magic bytes"

              But why not use QMimeDatabase for it?

              Paul ColbyP Offline
              Paul ColbyP Offline
              Paul Colby
              wrote on last edited by
              #5

              @Christian-Ehrlicher said in Checking that a file is a jpeg or png:

              But why not use QMimeDatabase for it?

              Nice! I didn't know about that one :) Great suggestion.

              1 Reply Last reply
              0
              • PerdrixP Perdrix has marked this topic as solved on

              • Login

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