Skip to content

Commit

Permalink
Add support for animated webp detection to Glide's ImageHeaderParser.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 421923663
  • Loading branch information
sjudd authored and glide-copybara-robot committed Jan 14, 2022
1 parent afc37e8 commit 42654b2
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 24 deletions.
5 changes: 0 additions & 5 deletions checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@
<module name="StaticVariableName" />
<module name="TypeName" />

<!-- Allow common trailing comments used to describe suppressions -->
<module name="TrailingComment">
<property name="legalComment" value="^Public API.?$|^NOPMD.*$" />
</module>

<!-- Checks for imports. -->
<!-- See http://checkstyle.sourceforge.net/config_imports.html -->
<module name="AvoidStarImport"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ enum ImageType {
WEBP_A(true),
/** WebP type without alpha. */
WEBP(false),
/** All animated webps. */
ANIMATED_WEBP(true),
/** Avif type (may contain alpha). */
AVIF(true),
/** Unrecognized type. */
Expand All @@ -44,6 +46,17 @@ enum ImageType {
public boolean hasAlpha() {
return hasAlpha;
}

public boolean isWebp() {
switch (this) {
case WEBP:
case WEBP_A:
case ANIMATED_WEBP:
return true;
default:
return false;
}
}
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.load.resource.bitmap;

import static com.bumptech.glide.load.ImageHeaderParser.ImageType.ANIMATED_WEBP;
import static com.bumptech.glide.load.ImageHeaderParser.ImageType.AVIF;
import static com.bumptech.glide.load.ImageHeaderParser.ImageType.GIF;
import static com.bumptech.glide.load.ImageHeaderParser.ImageType.JPEG;
Expand Down Expand Up @@ -53,6 +54,7 @@ public final class DefaultImageHeaderParser implements ImageHeaderParser {
private static final int VP8_HEADER_TYPE_EXTENDED = 0x00000058;
// 'L'
private static final int VP8_HEADER_TYPE_LOSSLESS = 0x0000004C;
private static final int WEBP_EXTENDED_ANIMATION_FLAG = 1 << 1;
private static final int WEBP_EXTENDED_ALPHA_FLAG = 1 << 4;
private static final int WEBP_LOSSLESS_ALPHA_FLAG = 1 << 3;
// Avif-related
Expand Down Expand Up @@ -146,7 +148,13 @@ private ImageType getType(Reader reader) throws IOException {
// Skip some more length bytes and check for transparency/alpha flag.
reader.skip(4);
short flags = reader.getUInt8();
return (flags & WEBP_EXTENDED_ALPHA_FLAG) != 0 ? ImageType.WEBP_A : ImageType.WEBP;
if ((flags & WEBP_EXTENDED_ANIMATION_FLAG) != 0) {
return ANIMATED_WEBP;
} else if ((flags & WEBP_EXTENDED_ALPHA_FLAG) != 0) {
return ImageType.WEBP_A;
} else {
return ImageType.WEBP;
}
}
if ((fourthFourBytes & VP8_HEADER_TYPE_MASK) == VP8_HEADER_TYPE_LOSSLESS) {
// See chromium.googlesource.com/webm/libwebp/+/master/doc/webp-lossless-bitstream-spec.txt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ private static void calculateScaling(
} else if (imageType == ImageType.PNG || imageType == ImageType.PNG_A) {
powerOfTwoWidth = (int) Math.floor(orientedSourceWidth / (float) powerOfTwoSampleSize);
powerOfTwoHeight = (int) Math.floor(orientedSourceHeight / (float) powerOfTwoSampleSize);
} else if (imageType == ImageType.WEBP || imageType == ImageType.WEBP_A) {
} else if (imageType.isWebp()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
powerOfTwoWidth = Math.round(orientedSourceWidth / (float) powerOfTwoSampleSize);
powerOfTwoHeight = Math.round(orientedSourceHeight / (float) powerOfTwoSampleSize);
Expand Down
Loading

0 comments on commit 42654b2

Please sign in to comment.