Skip to content

Commit 4d6f0c0

Browse files
authored
Minor cleanup of webp decoding mode logic (#8612)
1 parent fd0e28e commit 4d6f0c0

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

torchvision/csrc/io/image/cpu/decode_webp.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,22 @@ torch::Tensor decode_webp(
4040
TORCH_CHECK(
4141
!features.has_animation, "Animated webp files are not supported.");
4242

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

51+
// If return_rgb is false it means we return rgba - nothing else.
52+
auto return_rgb =
53+
(mode == IMAGE_READ_MODE_RGB ||
54+
(mode == IMAGE_READ_MODE_UNCHANGED && !features.has_alpha));
55+
56+
auto decoding_func = return_rgb ? WebPDecodeRGB : WebPDecodeRGBA;
57+
auto num_channels = return_rgb ? 3 : 4;
58+
5759
int width = 0;
5860
int height = 0;
5961

0 commit comments

Comments
 (0)