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" - that is,
0xFF 0xDF 0xFF
for JPEGs or0x47 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 givingQImage
a stream to read from, it might not save you much effort anyway?Cheers.
Edit: Or do you mean some other form of "validation"?
@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?
-
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@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.
-
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
DavidYou 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 or0x47 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 givingQImage
a stream to read from, it might not save you much effort anyway?Cheers.
Edit: Or do you mean some other form of "validation"?
-
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 or0x47 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 givingQImage
a stream to read from, it might not save you much effort anyway?Cheers.
Edit: Or do you mean some other form of "validation"?
@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-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?
@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.
-
P Perdrix has marked this topic as solved on