Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unused inSampleSize and add scaling to decoding step #359

Merged

Conversation

Raenar4k
Copy link
Contributor

I'm looking for ways to optimise memory usage in my app, and found some things in BitmapPhotoTransformer that i think can be improved.

  1. There is inSampleSize calculation being done for decoding options, but it is never used because of typo.

  2. We can skip creating another bitmap with Bitmap.createScaledBitmap if we add scaling step to decoding.

This way, if we want to scale down image while retaining original aspect ratio we skip Bitmap.createScaledBitmap, thus avoiding another bitmap allocation.

If we need to scale irregardless of aspect ratio, then createScaledBitmap will be used, but on already scaled down by biggest side version of the image, which is also a plus.

I also tried to measure performance benefit of memory usage.
I've modified sample to use .toBitmap(scaled(scaleFactor = 0.22f))
Otherwise scaleFactor pf 0.25 results in inSampleSize = 4, which wouldn't need any scaling afterwards.

Not saying this is accurate (not sure how to really measure it here), but you can see that it does have some effect.

So, sample without changes is about 180-200MB:
(uses createScaledBitmap)
https://i.imgur.com/JjtFVGf.png

Sample that first downsamples by inSampleSize and then scales by createScaledBitmap
is about 107-120MB:
https://i.imgur.com/T49QVbD.png

Sample that downsamples and scales in one step and skips allocation of createScaledBitmap:
is about 98-102MB:
https://i.imgur.com/4AI1jMU.png

@Diolor
Copy link
Member

Diolor commented Mar 16, 2019

This looks good to me. Very nice improvement @Raenar4k, thanks! 👍
@dmitry-zaitsev wanna take a look in this one too?

@Diolor Diolor self-requested a review March 16, 2019 14:23
@dmitry-zaitsev dmitry-zaitsev merged commit 7094f98 into RedApparat:master Dec 10, 2021
pulpet112 pushed a commit to infakt/Fotoapparat that referenced this pull request Apr 4, 2022
)

* Fix bitmap decoding to use inSampleSize

* Add scaling (by max side) while decoding bitmap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants