-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
...n/avif/src/main/java/com/bumptech/glide/integration/avif/AvifByteBufferBitmapDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.bumptech.glide.integration.avif; | ||
|
||
import android.graphics.Bitmap; | ||
import android.util.Log; | ||
import com.bumptech.glide.load.Options; | ||
import com.bumptech.glide.load.ResourceDecoder; | ||
import com.bumptech.glide.load.engine.Resource; | ||
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; | ||
import com.bumptech.glide.load.resource.bitmap.BitmapResource; | ||
import com.bumptech.glide.util.Preconditions; | ||
import java.nio.ByteBuffer; | ||
import javax.annotation.Nullable; | ||
import org.aomedia.avif.android.AvifDecoder; | ||
import org.aomedia.avif.android.AvifDecoder.Info; | ||
|
||
/** A Glide {@link ResourceDecoder} capable of decoding Avif images. */ | ||
public final class AvifByteBufferBitmapDecoder implements ResourceDecoder<ByteBuffer, Bitmap> { | ||
private static final String TAG = "AvifBitmapDecoder"; | ||
|
||
private final BitmapPool bitmapPool; | ||
|
||
public AvifByteBufferBitmapDecoder(BitmapPool bitmapPool) { | ||
this.bitmapPool = Preconditions.checkNotNull(bitmapPool); | ||
} | ||
|
||
private ByteBuffer maybeCopyBuffer(ByteBuffer source) { | ||
// Native calls can only access ByteBuffer if isDirect() is true. Otherwise, we would have to | ||
// make a copy into a direct ByteBuffer. | ||
if (source.isDirect()) { | ||
return source; | ||
} | ||
ByteBuffer sourceCopy = ByteBuffer.allocateDirect(source.remaining()); | ||
sourceCopy.put(source); | ||
sourceCopy.flip(); | ||
return sourceCopy; | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public Resource<Bitmap> decode(ByteBuffer source, int width, int height, Options options) { | ||
ByteBuffer sourceCopy = maybeCopyBuffer(source); | ||
Info info = new Info(); | ||
if (!AvifDecoder.getInfo(sourceCopy, sourceCopy.remaining(), info)) { | ||
if (Log.isLoggable(TAG, Log.ERROR)) { | ||
Log.e(TAG, "Requested to decode byte buffer which cannot be handled by AvifDecoder"); | ||
} | ||
return null; | ||
} | ||
Bitmap bitmap = | ||
bitmapPool.get( | ||
info.width, | ||
info.height, | ||
(info.depth == 8) ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGBA_F16); | ||
if (!AvifDecoder.decode(sourceCopy, sourceCopy.remaining(), bitmap)) { | ||
if (Log.isLoggable(TAG, Log.ERROR)) { | ||
Log.e(TAG, "Failed to decode ByteBuffer as Avif."); | ||
} | ||
bitmapPool.put(bitmap); | ||
return null; | ||
} | ||
return BitmapResource.obtain(bitmap, bitmapPool); | ||
} | ||
|
||
@Override | ||
public boolean handles(ByteBuffer source, Options options) { | ||
return AvifDecoder.isAvifImage(maybeCopyBuffer(source)); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
integration/avif/src/main/java/com/bumptech/glide/integration/avif/AvifGlideModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.bumptech.glide.integration.avif; | ||
|
||
import android.content.Context; | ||
import android.graphics.Bitmap; | ||
import androidx.annotation.NonNull; | ||
import com.bumptech.glide.Glide; | ||
import com.bumptech.glide.Registry; | ||
import com.bumptech.glide.annotation.GlideModule; | ||
import com.bumptech.glide.module.LibraryGlideModule; | ||
import java.io.InputStream; | ||
import java.nio.ByteBuffer; | ||
|
||
/** Glide support for AVIF Images. */ | ||
@GlideModule | ||
public final class AvifGlideModule extends LibraryGlideModule { | ||
|
||
@Override | ||
public void registerComponents( | ||
@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) { | ||
// Add the Avif ResourceDecoders before any of the available system decoders. This ensures that | ||
// the integration will be preferred for Avif images. | ||
AvifByteBufferBitmapDecoder byteBufferBitmapDecoder = | ||
new AvifByteBufferBitmapDecoder(glide.getBitmapPool()); | ||
registry.prepend(ByteBuffer.class, Bitmap.class, byteBufferBitmapDecoder); | ||
AvifStreamBitmapDecoder streamBitmapDecoder = | ||
new AvifStreamBitmapDecoder( | ||
registry.getImageHeaderParsers(), byteBufferBitmapDecoder, glide.getArrayPool()); | ||
registry.prepend(InputStream.class, Bitmap.class, streamBitmapDecoder); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...ation/avif/src/main/java/com/bumptech/glide/integration/avif/AvifStreamBitmapDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.bumptech.glide.integration.avif; | ||
|
||
import android.graphics.Bitmap; | ||
import com.bumptech.glide.load.ImageHeaderParser; | ||
import com.bumptech.glide.load.ImageHeaderParser.ImageType; | ||
import com.bumptech.glide.load.ImageHeaderParserUtils; | ||
import com.bumptech.glide.load.Options; | ||
import com.bumptech.glide.load.ResourceDecoder; | ||
import com.bumptech.glide.load.engine.Resource; | ||
import com.bumptech.glide.load.engine.bitmap_recycle.ArrayPool; | ||
import com.bumptech.glide.util.ByteBufferUtil; | ||
import com.bumptech.glide.util.Preconditions; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import javax.annotation.Nullable; | ||
|
||
/** A Glide {@link ResourceDecoder} capable of decoding Avif Images. */ | ||
public final class AvifStreamBitmapDecoder implements ResourceDecoder<InputStream, Bitmap> { | ||
private static final String TAG = "AvifStreamBitmapDecoder"; | ||
|
||
private final List<ImageHeaderParser> parsers; | ||
private final AvifByteBufferBitmapDecoder avifByteBufferDecoder; | ||
private final ArrayPool arrayPool; | ||
|
||
public AvifStreamBitmapDecoder( | ||
List<ImageHeaderParser> parsers, | ||
AvifByteBufferBitmapDecoder avifByteBufferDecoder, | ||
ArrayPool arrayPool) { | ||
this.parsers = parsers; | ||
this.avifByteBufferDecoder = Preconditions.checkNotNull(avifByteBufferDecoder); | ||
this.arrayPool = Preconditions.checkNotNull(arrayPool); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public Resource<Bitmap> decode(InputStream source, int width, int height, Options options) | ||
throws IOException { | ||
return avifByteBufferDecoder.decode(ByteBufferUtil.fromStream(source), width, height, options); | ||
} | ||
|
||
@Override | ||
public boolean handles(InputStream source, Options options) throws IOException { | ||
return ImageType.AVIF.equals(ImageHeaderParserUtils.getType(parsers, source, arrayPool)); | ||
} | ||
} |