From 89ee2e1cde1e1b0226ed944b9abd55cee0f9b9d4 Mon Sep 17 00:00:00 2001 From: Ram Vellanki Date: Fri, 8 Jun 2018 15:37:57 -0400 Subject: [PATCH] Adding extensions for ImageDecoder (#540) * Adding extensions for decoding ImageDecoder.Source instances to Bitmaps and Drawables * Renaming ImageDecoder extensions to highlight decoding * Fixing tests to avoid relying on the file system --- api/current.txt | 6 ++ .../core/graphics/ImageDecoderTest.kt | 58 +++++++++++++++++++ .../androidx/core/graphics/ImageDecoder.kt | 52 +++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 src/androidTest/java/androidx/core/graphics/ImageDecoderTest.kt create mode 100644 src/main/java/androidx/core/graphics/ImageDecoder.kt diff --git a/api/current.txt b/api/current.txt index 061793ff..5b79660e 100644 --- a/api/current.txt +++ b/api/current.txt @@ -153,6 +153,12 @@ package androidx.core.graphics { method @RequiresApi(26) @ColorLong public static long toColorLong(int); } + public final class ImageDecoderKt { + ctor public ImageDecoderKt(); + method @RequiresApi(28) public static android.graphics.Bitmap decodeBitmap(android.graphics.ImageDecoder.Source, kotlin.jvm.functions.Function3 action); + method @RequiresApi(28) public static android.graphics.drawable.Drawable decodeDrawable(android.graphics.ImageDecoder.Source, kotlin.jvm.functions.Function3 action); + } + public final class MatrixKt { ctor public MatrixKt(); method public static android.graphics.Matrix rotationMatrix(float degrees, float px = "0.0f", float py = "0.0f"); diff --git a/src/androidTest/java/androidx/core/graphics/ImageDecoderTest.kt b/src/androidTest/java/androidx/core/graphics/ImageDecoderTest.kt new file mode 100644 index 00000000..6d252916 --- /dev/null +++ b/src/androidTest/java/androidx/core/graphics/ImageDecoderTest.kt @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package androidx.core.graphics + +import android.graphics.Bitmap +import android.graphics.ImageDecoder +import org.junit.Assert.assertEquals +import org.junit.BeforeClass +import org.junit.Test +import java.io.ByteArrayOutputStream +import java.nio.ByteBuffer + +class ImageDecoderTest { + + @Test fun decodeBitmap() { + val src = ImageDecoder.createSource(buffer) + val decodedBitmap = src.decodeBitmap { _, _ -> setTargetSize(10, 10) } + assertEquals(10, decodedBitmap.width) + assertEquals(10, decodedBitmap.height) + } + + @Test fun decodeDrawable() { + val src = ImageDecoder.createSource(buffer) + val decodedDrawable = src.decodeDrawable { _, _ -> setTargetSize(10, 10) } + assertEquals(10, decodedDrawable.intrinsicWidth) + assertEquals(10, decodedDrawable.intrinsicHeight) + } + + companion object { + private lateinit var buffer: ByteBuffer + + @BeforeClass + @JvmStatic + fun beforeClass() { + val stream = ByteArrayOutputStream().apply { + Bitmap + .createBitmap(1, 1, Bitmap.Config.ARGB_8888) + .compress(Bitmap.CompressFormat.JPEG, 100, this) + } + + buffer = ByteBuffer.wrap(stream.toByteArray()) + } + } +} diff --git a/src/main/java/androidx/core/graphics/ImageDecoder.kt b/src/main/java/androidx/core/graphics/ImageDecoder.kt new file mode 100644 index 00000000..e1802ff3 --- /dev/null +++ b/src/main/java/androidx/core/graphics/ImageDecoder.kt @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package androidx.core.graphics + +import android.graphics.Bitmap +import android.graphics.ImageDecoder +import android.graphics.ImageDecoder.ImageInfo +import android.graphics.ImageDecoder.Source +import android.graphics.drawable.Drawable +import androidx.annotation.RequiresApi + +/** + * Create a Bitmap from a Source + * + * @see ImageDecoder.decodeBitmap + */ +@RequiresApi(28) +inline fun ImageDecoder.Source.decodeBitmap( + crossinline action: ImageDecoder.(info: ImageInfo, source: Source) -> Unit +): Bitmap { + return ImageDecoder.decodeBitmap(this) { decoder, info, source -> + decoder.action(info, source) + } +} + +/** + * Create a Drawable from a Source + * + * @see ImageDecoder.decodeDrawable + */ +@RequiresApi(28) +inline fun ImageDecoder.Source.decodeDrawable( + crossinline action: ImageDecoder.(info: ImageInfo, source: Source) -> Unit +): Drawable { + return ImageDecoder.decodeDrawable(this) { decoder, info, source -> + decoder.action(info, source) + } +}