-
Notifications
You must be signed in to change notification settings - Fork 562
Bitmap extensions #418
base: master
Are you sure you want to change the base?
Bitmap extensions #418
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here (e.g. What to do if you already signed the CLAIndividual signers
Corporate signers
|
I signed it! |
CLAs look good, thanks! |
I'm liking |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comments, but also missing from this PR:
- running the updateApi task
- tests
* @param quality Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. | ||
* @return ByteArray | ||
*/ | ||
fun Bitmap.toBytes(format:CompressFormat = JPEG, quality: Int=100):ByteArray |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Compressing a bitmap can be done in many ways and compressing into a byte array is just a highly specific use case. I'd rather remove this method.
* @param height The height. | ||
* @return the clipped bitmap | ||
*/ | ||
fun Bitmap.clip(x:Int, y:Int, width: Int, height: Int):Bitmap |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this method is necessary. It doesn't save much over the Bitmap.createBitmap()
call for an operation that might not be very common. At least it should be inline
, better documented and the return type doesn't need to be specified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clip
is of more clear meaning than createBitmap
for bitmap operation. I added inline
for all methods and updated docs.
* @param py The y coordinate of the pivot point. | ||
* @return the skewed bitmap | ||
*/ | ||
fun Bitmap.skew(kx:Float, ky:Float, px:Float = 0f, py:Float = 0f):Bitmap |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a common usage, please remove.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I perfer add this method in order to let operations of bitmap more complete
* @param py The y coordinate of the pivot point. | ||
* @return the rotated bitmap | ||
*/ | ||
fun Bitmap.rotate(degrees:Int, px: Float, py: Float):Bitmap |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not entirely convinced by how common this use case is. (Also same comments that I've mentioned on clip()
apply here).
@hendraanggrian It more make sense to use |
2. add inline for functions
api/current.txt
Outdated
@@ -23,6 +23,7 @@ package androidx.content { | |||
|
|||
public final class ContextKt { | |||
ctor public ContextKt(); | |||
method public static java.util.List<java.lang.String> getPermissions(android.content.Context); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, it's a mistake, I have already removed it
* @param py The y coordinate of the pivot point. | ||
* @return the skewed bitmap | ||
*/ | ||
inline fun Bitmap.skew(kx:Float, ky:Float, px:Float = 0f, py:Float = 0f):Bitmap |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove, this is not useful
* @param py The y coordinate of the pivot point. | ||
* @return the rotated bitmap | ||
*/ | ||
inline fun Bitmap.rotate(degrees:Float, px: Float, py: Float) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure this is usefuul
* @param height The height. | ||
* @return the clipped bitmap | ||
*/ | ||
inline fun Bitmap.clip(x:Int, y:Int, width: Int, height: Int) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should run klint, your formatting is inconsistent (x:Int
vs with: Int
for instance)
I would rather call this method crop()
* @param quality Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. | ||
* @return ByteArray | ||
*/ | ||
inline fun Bitmap.toByteArray(format:CompressFormat = CompressFormat.JPEG, @IntRange(from = 0, to = 100) quality: Int=100) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still don't find compressing to a byte array useful. Compressing directly into a stream seems the more common case
*/ | ||
inline fun Bitmap.toByteArray( | ||
inline fun Bitmap.toStream( |
There was a problem hiding this comment.
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.
@@ -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 = |
There was a problem hiding this comment.
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()
.
|
||
/** | ||
* 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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
I am not against those two extensions, they are actually kind of useful, but they don't really leverage any feature of Kotlin, beyond being simple extensions. rotate with a default pivot point would. |
some useful functions of Bitmap: