-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace imghdr #650
Replace imghdr #650
Conversation
Also copy own check functions from `utils.files` over.
This is equivalent to the functionality originally provided by `add_image_format`. This implementation has the advantage that it allows uf us to add this validation for ALL types not natively supported (including types supported by qtimageformates).
This also includes removing jp2, as this check is included in `utils.imageheaer`.
Added wrong function to `_registry`, resulting in dynamic check not actually be done.
A few comments on this implementation of image detection:
|
@jcjgraf thanks a lot for the effort that went into this, this is really nice, I hope to take a closer look during this week! Concerning the Copyright header, you are probably right, but I wouldn't bother in this PR. It may make sense to simplify this overall and remove the years and just keep a small notice at the top of each file. Discussion in a new issue? @buzzingwires thanks a bunch for your comments and details! 😊 We actually did start a discussion on this in #579 . The reason for pursuing this path was initially, that just copying |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is absolutely amazing work, thanks again! I only left a few nitpicks, so besides the points you mentioned and including the magic bytes described by @buzzingwires, this is basically ready to go.
It is really cool to see how much cleaner this makes the handling of filetypes, together with the Qt support, and the error / warning messages. And the documentation is amazing.
Initially, I thought (hoped) that this is not needed. But it seems that for certain types it is required. That is e.g. the case when the first 32 bytes are not sufficient to detect type.
Version 1 is undetectable
Seeking relative to the end of file seems to not work for some files, resulting in an exception. This was fixed.
Assuming a certain seek is not a good idea. Also, apparently, the prefix of `_width` and `_height` is optional (QImageWriter does not produce one).
It would be even cleaner to omit the else bloc and set `check = check_verified` in the then block, and register `check` instead. But python has some recursion issue with that.
@buzzingwires Thanks for your really helpful comment! And sorry for the late reply... I know that some of the formats are probably used by literally nobody (anymore). But since these formats (including
Your correct, thanks! I have added a check for it.
XBM is indeed a funky file definition; it is a valid C source file🙈 I have also added a check for it.
Indeed. There are two version of TGA. In version 2 they have added a signature to the footer of the file. I have added support for that.
I am not really sure if it is works the effort to check for dynamic set bytes. This seems rather complex and is in the end not more that a heuristic (well, that is also the case for some other types, that have a rather "badly" chosen signature 😂), that may lead to false positives. |
From python 3.11 on try induces zero-overhead when no exception is raised in the try. This is much more efficient than using suppress. The try is placed inside for loop to ensure that a check that raises does not affect other checks.
Could also be useful in other unit tests in the future.
This should have been changed as port of karlch#650. This plugin is no longer needed for jp2 support, but jp2 is natively supported, given that the qt-imageformats module is installed.
replaces #646
Since imghdr is getting deprecated from the standard library (see #579), this PR replaces that module by a own (and thereby, legally completely decoupled independent (unlike in #646)), implementation of the magic byte file type detection.
Some advantages of this approach, over using the package:
utils/files
andplugin/imageformats
plugin/imageformats
has a specific and well defined purposeAnd something that I like particularly well:
The runtime validation of check functions, (used to be done for all checks that were registered using
utils/files/add_image_formats
), is now done for all image types, except the ones natively supported by QT. This way, clients get a notification when they try to open an image of a type that:plugins/imageformats
, without having that module installedThis should prevent confusions as in Unable to open WebP image format #619, and previous similar issues.
Steps
utils/imagheader
for status supported typesutils/imageheader
plugins/imageformats
I have already added a basic tests that verifies the detection of all filetypes which
QPixmap
is capable of writing (/creating) (as you suggested in the other PR). Unfortunately, it is capable of creating less types than what it is able to read. I have to think about ways to create and test the other types.So, code-wise this PR is done, and if time allows, you may have a look at it. I will spend some more time for testing.
In what way does it make sense that copyright in the header of the new file is required to start at 2017? Should it not start in the year the file was first added to the project (i.e. 2023 in this case)?
fixes #579
replaces #646