Skip to content
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

Minor cleanup of webp decoding mode logic #8612

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions torchvision/csrc/io/image/cpu/decode_webp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,22 @@ torch::Tensor decode_webp(
TORCH_CHECK(
!features.has_animation, "Animated webp files are not supported.");

auto decoding_func = WebPDecodeRGB;
int num_channels = 0;
if (mode == IMAGE_READ_MODE_RGB) {
decoding_func = WebPDecodeRGB;
num_channels = 3;
} else if (mode == IMAGE_READ_MODE_RGB_ALPHA) {
decoding_func = WebPDecodeRGBA;
num_channels = 4;
} else {
// Assume mode is "unchanged"
decoding_func = features.has_alpha ? WebPDecodeRGBA : WebPDecodeRGB;
num_channels = features.has_alpha ? 4 : 3;
if (mode != IMAGE_READ_MODE_UNCHANGED && mode != IMAGE_READ_MODE_RGB &&
mode != IMAGE_READ_MODE_RGB_ALPHA) {
// Other modes aren't supported, but we don't error or even warn because we
// have generic entry points like decode_image which may support all modes,
// it just depends on the underlying decoder.
mode = IMAGE_READ_MODE_UNCHANGED;
}

// If return_rgb is false it means we return rgba - nothing else.
auto return_rgb =
(mode == IMAGE_READ_MODE_RGB ||
(mode == IMAGE_READ_MODE_UNCHANGED && !features.has_alpha));

auto decoding_func = return_rgb ? WebPDecodeRGB : WebPDecodeRGBA;
auto num_channels = return_rgb ? 3 : 4;

int width = 0;
int height = 0;

Expand Down
Loading