Skip to content
This repository was archived by the owner on Nov 14, 2018. It is now read-only.

Commit 89ee2e1

Browse files
RamV13romainguy
authored andcommitted
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
1 parent c803919 commit 89ee2e1

3 files changed

Lines changed: 116 additions & 0 deletions

File tree

api/current.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ package androidx.core.graphics {
153153
method @RequiresApi(26) @ColorLong public static long toColorLong(int);
154154
}
155155

156+
public final class ImageDecoderKt {
157+
ctor public ImageDecoderKt();
158+
method @RequiresApi(28) public static android.graphics.Bitmap decodeBitmap(android.graphics.ImageDecoder.Source, kotlin.jvm.functions.Function3<? super android.graphics.ImageDecoder,? super android.graphics.ImageDecoder.ImageInfo,? super android.graphics.ImageDecoder.Source,kotlin.Unit> action);
159+
method @RequiresApi(28) public static android.graphics.drawable.Drawable decodeDrawable(android.graphics.ImageDecoder.Source, kotlin.jvm.functions.Function3<? super android.graphics.ImageDecoder,? super android.graphics.ImageDecoder.ImageInfo,? super android.graphics.ImageDecoder.Source,kotlin.Unit> action);
160+
}
161+
156162
public final class MatrixKt {
157163
ctor public MatrixKt();
158164
method public static android.graphics.Matrix rotationMatrix(float degrees, float px = "0.0f", float py = "0.0f");
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (C) 2018 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package androidx.core.graphics
18+
19+
import android.graphics.Bitmap
20+
import android.graphics.ImageDecoder
21+
import org.junit.Assert.assertEquals
22+
import org.junit.BeforeClass
23+
import org.junit.Test
24+
import java.io.ByteArrayOutputStream
25+
import java.nio.ByteBuffer
26+
27+
class ImageDecoderTest {
28+
29+
@Test fun decodeBitmap() {
30+
val src = ImageDecoder.createSource(buffer)
31+
val decodedBitmap = src.decodeBitmap { _, _ -> setTargetSize(10, 10) }
32+
assertEquals(10, decodedBitmap.width)
33+
assertEquals(10, decodedBitmap.height)
34+
}
35+
36+
@Test fun decodeDrawable() {
37+
val src = ImageDecoder.createSource(buffer)
38+
val decodedDrawable = src.decodeDrawable { _, _ -> setTargetSize(10, 10) }
39+
assertEquals(10, decodedDrawable.intrinsicWidth)
40+
assertEquals(10, decodedDrawable.intrinsicHeight)
41+
}
42+
43+
companion object {
44+
private lateinit var buffer: ByteBuffer
45+
46+
@BeforeClass
47+
@JvmStatic
48+
fun beforeClass() {
49+
val stream = ByteArrayOutputStream().apply {
50+
Bitmap
51+
.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
52+
.compress(Bitmap.CompressFormat.JPEG, 100, this)
53+
}
54+
55+
buffer = ByteBuffer.wrap(stream.toByteArray())
56+
}
57+
}
58+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (C) 2018 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package androidx.core.graphics
18+
19+
import android.graphics.Bitmap
20+
import android.graphics.ImageDecoder
21+
import android.graphics.ImageDecoder.ImageInfo
22+
import android.graphics.ImageDecoder.Source
23+
import android.graphics.drawable.Drawable
24+
import androidx.annotation.RequiresApi
25+
26+
/**
27+
* Create a Bitmap from a Source
28+
*
29+
* @see ImageDecoder.decodeBitmap
30+
*/
31+
@RequiresApi(28)
32+
inline fun ImageDecoder.Source.decodeBitmap(
33+
crossinline action: ImageDecoder.(info: ImageInfo, source: Source) -> Unit
34+
): Bitmap {
35+
return ImageDecoder.decodeBitmap(this) { decoder, info, source ->
36+
decoder.action(info, source)
37+
}
38+
}
39+
40+
/**
41+
* Create a Drawable from a Source
42+
*
43+
* @see ImageDecoder.decodeDrawable
44+
*/
45+
@RequiresApi(28)
46+
inline fun ImageDecoder.Source.decodeDrawable(
47+
crossinline action: ImageDecoder.(info: ImageInfo, source: Source) -> Unit
48+
): Drawable {
49+
return ImageDecoder.decodeDrawable(this) { decoder, info, source ->
50+
decoder.action(info, source)
51+
}
52+
}

0 commit comments

Comments
 (0)