Skip to content
This repository was archived by the owner on Nov 14, 2018. It is now read-only.
7 changes: 3 additions & 4 deletions api/current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,14 @@ package androidx.graphics {
public final class BitmapKt {
ctor public BitmapKt();
method public static android.graphics.Bitmap applyCanvas(android.graphics.Bitmap, kotlin.jvm.functions.Function1<? super android.graphics.Canvas,kotlin.Unit> block);
method public static android.graphics.Bitmap! clip(android.graphics.Bitmap, int x, int y, int width, int height);
method public static android.graphics.Bitmap clip(android.graphics.Bitmap, int x, int y, int width, int height);
method public static android.graphics.Bitmap createBitmap(int width, int height, android.graphics.Bitmap.Config config = "Bitmap.Config.ARGB_8888");
method @RequiresApi(26) public static android.graphics.Bitmap createBitmap(int width, int height, android.graphics.Bitmap.Config config = "Bitmap.Config.ARGB_8888", boolean hasAlpha = "true", android.graphics.ColorSpace colorSpace = "ColorSpace.get(ColorSpace.Named.SRGB)");
method public static operator int get(android.graphics.Bitmap, int x, int y);
method public static android.graphics.Bitmap! rotate(android.graphics.Bitmap, float degrees);
method public static android.graphics.Bitmap rotate(android.graphics.Bitmap, float degrees);
method public static android.graphics.Bitmap scale(android.graphics.Bitmap, int width, int height, boolean filter = "true");
method public static operator void set(android.graphics.Bitmap, int x, int y, @ColorInt int color);
method public static android.graphics.Bitmap! skew(android.graphics.Bitmap, float kx, float ky);
method public static error.NonExistentClass toByteArray(android.graphics.Bitmap, android.graphics.Bitmap.CompressFormat format = "CompressFormat.JPEG", @IntRange(from=0L, to=100L) int quality = "100");
method public static java.io.ByteArrayInputStream toStream(android.graphics.Bitmap, android.graphics.Bitmap.CompressFormat format = "CompressFormat.JPEG", @IntRange(from=0L, to=100L) int quality = "100");
}

public final class CanvasKt {
Expand Down
13 changes: 3 additions & 10 deletions src/androidTest/java/androidx/graphics/BitmapTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,6 @@ class BitmapTest {
assertEquals(0x40302010, res[0, 0])
}

@Test fun skew() {
val src = createBitmap(10, 10)
val res = src.skew(0.5f, 2f)
assertEquals(15, res.width)
assertEquals(30, res.height)
}

@Test fun rotate() {
val src = createBitmap(10, 10)
src[3, 5] = 0x40302010
Expand All @@ -98,11 +91,11 @@ class BitmapTest {
assertEquals(0x40302010, res[4, 3])
}

@Test fun toByteArray() {
@Test fun toStream() {
val src = createBitmap(10, 10)
src[3, 5] = 0x40302010
val bytes = src.toByteArray(PNG)
val back = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
val stream = src.toStream(PNG)
val back = BitmapFactory.decodeStream(stream)
assertEquals(10, back.width)
assertEquals(10, back.height)
assertEquals(0x40302010, back[3, 5])
Expand Down
28 changes: 8 additions & 20 deletions src/main/java/androidx/graphics/Bitmap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import android.graphics.Matrix
import android.support.annotation.ColorInt
import android.support.annotation.IntRange
import android.support.annotation.RequiresApi
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream

/**
Expand Down Expand Up @@ -118,17 +119,18 @@ inline fun createBitmap(
}

/**
* Returns ByteArray compressed from this bitmap with the specified [format]
* Returns ByteArrayInputStream compressed from this bitmap with the specified [format]
* and [quality].
*
* @param format The format of bitmap.
* @param quality Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality.
* @return ByteArray
* @return ByteArrayInputStream
*/
inline fun Bitmap.toByteArray(
inline fun Bitmap.toStream(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is not what I meant. What I meant is that there shouldn't be an extension on Bitmap to compress to a byte array or a stream, etc. Bitmap.compress() already accepts an OuputStream which is generic enough to cover all use cases. It shouldn't be specialized here.

format: CompressFormat = CompressFormat.JPEG,
@IntRange(from = 0, to = 100) quality: Int = 100
) = ByteArrayOutputStream().also { compress(format, quality, it) }.toByteArray()
): ByteArrayInputStream =
ByteArrayOutputStream().also { compress(format, quality, it) }.toByteArray().inputStream()

/**
* Creates a new bitmap, clipped from this bitmap. If the specified [x], [y],
Expand All @@ -141,23 +143,9 @@ inline fun Bitmap.toByteArray(
* @param height The height.
* @return the clipped bitmap
*/
inline fun Bitmap.clip(x: Int, y: Int, width: Int, height: Int) =
inline fun Bitmap.clip(x: Int, y: Int, width: Int, height: Int): Bitmap =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type is not needed. Please rename this method to crop().

Bitmap.createBitmap(this, x, y, width, height)

/**
* Creates a new bitmap, skewed from this bitmap by [kx] and [ky],
* with a pivot point at ([px], [py]). The pivot point is the
* coordinate that should remain unchanged by the specified transformation.
*
* @param kx The skew factor of x.
* @param ky The skew factor of y.
* @param px The x coordinate of the pivot point.
* @param py The y coordinate of the pivot point.
* @return the skewed bitmap
*/
inline fun Bitmap.skew(kx: Float, ky: Float) =
createBitmap(this, 0, 0, width, height, Matrix().apply { setSkew(kx, ky) }, true)

/**
* Creates a new bitmap, rotated from this bitmap by [degrees] - the specified number of degrees,
* with a pivot point at ([px], [py]). The pivot point is the coordinate that should remain

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no pivot point in the API

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for that, i added it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the change. The pivot point should probably be set to width/2.0f and height/2.0f by default.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the default pivot point.

Expand All @@ -168,5 +156,5 @@ inline fun Bitmap.skew(kx: Float, ky: Float) =
* @param py The y coordinate of the pivot point.
* @return the rotated bitmap
*/
inline fun Bitmap.rotate(degrees: Float) =
inline fun Bitmap.rotate(degrees: Float): Bitmap =
createBitmap(this, 0, 0, width, height, Matrix().apply { setRotate(degrees) }, true)