-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
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
Add support for saving WebP images #61770
Conversation
I like the proposed API with default parameters, +1. It makes for concise code and I'll never need to remember what |
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.
I'm approving this on a proposal standpoint, because webp is already one of godot accepted formats and is used widely for (Godot) lossless images.
It's open to technical review.
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.
Looks good to me overall, still have to do a closer review.
core/io/image.h
Outdated
@@ -34,6 +34,7 @@ | |||
#include "core/io/resource.h" | |||
#include "core/math/color.h" | |||
#include "core/math/rect2.h" | |||
#include "modules/modules_enabled.gen.h" |
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.
Not needed anymore.
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.
Discussed in PR review meeting, we agreed with @reduz that the API should be:
save_webp(String path, bool lossy = false, float lossy_quality = 0.75)
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.
Looks good 👍
Thanks! |
Godot already has code to save WebP images, but it's not exposed to the user. This PR refactors this code, adding and exposing APIs for saving WebP images.
The bulk of the saving code already existed in
image_loader_webp.cpp
, it has been moved to a new filewebp_common.cpp
, which is used by both the loader and the new fileresource_saver_webp.cpp
. This mimics the structure of the PNG code.In addition to moving this code, this PR also fixes a bug @lyuma and @Macksaur discovered where Godot was adding an extra
WEBP
prefix that made the saved file not be a valid WebP file. This bug wasn't affecting us before because Godot only used this code to save stuff internally and then we stripped that prefix when loading internally, but it's technically a pre-existing bug that resulted in compressed textures being invalid WebPs and 4 bytes bigger than they needed to be.The exposed methods are in the
Image
class. They mimic the API for saving PNG files, except that WebP has both lossy and lossless so there is an extra parameter for the quality setting. Passing intrue
for thelossy
parameter will save lossy, and the quality can be set to a value between0.0
and1.0
. Users can writeimage.save_webp("res://test.webp")
to save a lossless WebP file, orimage.save_webp("res://test.webp", true)
to save a lossy WebP file at 75% quality, orimage.save_webp("res://test.webp", true, 0.5)
to save a lossy WebP file at 50% quality.Special thanks to @lyuma and @Macksaur. My work on this was sponsored by The Mirror.