Thanks to visit codestin.com
Credit goes to github.com

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

Bitmap extensions #418

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open

Bitmap extensions #418

wants to merge 12 commits into from

Conversation

feiyanke
Copy link

@feiyanke feiyanke commented Mar 9, 2018

some useful functions of Bitmap:

Bitmap.toBytes
Bitmap.clip
Bitmap.skew
Bitmap.rotate

@googlebot
Copy link

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. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers
  • Your company has a Point of Contact who decides which employees are authorized to participate. Ask your POC to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the project maintainer to go/cla#troubleshoot.
  • The email used to register you as an authorized contributor must be the email used for the Git commit. Check your existing CLA data and verify that your email is set on your git commits.
  • The email used to register you as an authorized contributor must also be attached to your GitHub account.

@feiyanke
Copy link
Author

feiyanke commented Mar 9, 2018

I signed it!

@googlebot
Copy link

CLAs look good, thanks!

@hanggrian
Copy link
Contributor

hanggrian commented Mar 9, 2018

I'm liking toBytes, but it really should be toByteArray. Also, I'm not sure if JPG shoud be the default format over PNG. And maybe use IntRange annotation in param quality.

Copy link
Contributor

@romainguy romainguy left a 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
Copy link
Contributor

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
Copy link
Contributor

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.

Copy link
Author

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
Copy link
Contributor

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.

Copy link
Author

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
Copy link
Contributor

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).

@feiyanke
Copy link
Author

@hendraanggrian It more make sense to use toByteArray, I have fixed it and added IntRange for quality. PNG is a common use format, but I perfer JPG as it could make smaller size for most bitmaps.

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);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this change?

Copy link
Author

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
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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(
Copy link
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.

@@ -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
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().


/**
* 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
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
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
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
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.

@romainguy
Copy link
Contributor

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants