6

I'm currently writing Bitmaps to a png file and also reading them back to a Bitmap. I'm looking for ways to improve the speed at which writing and reading happens. The images need to be lossless since I'm reading them back to edit them.

The place where I see the worst performance is the actual BitmapFactory.decode(...).

Few questions:
1. Is there a faster solution to read/write from file to a Bitmap using NDK?
2. Is there a better library to decode a Bitmap faster?
3. What is the best way to store and read a Bitmap?

Jona
  • 13,325
  • 15
  • 86
  • 129
  • Some thoughts: 1. Where are you trying to **write to**? 2. Probably not. Decode is very expensive. 3. There are many compression solutions, but I personally like 9-patches. – hwrdprkns Apr 29 '12 at 23:13
  • I updated my question to mention to file. Well, 9-patch would definitely not work. These images are drawings that can't be 9-patched. There has to be some type of NDK library that decodes a bit faster? :P – Jona Apr 29 '12 at 23:23
  • Eh, I would think that if you have a JPEG image that's as compressed as it's going to get, unless you know something about the image beforehand... – hwrdprkns Apr 30 '12 at 06:41
  • Thanks for the tip, I'm thinking that by compressing to JPEG and than decompressing back again to a Bitmap you start to loose quality on the image no? Or is it always restored properly? That is assuming using the least compression. – Jona Apr 30 '12 at 13:01
  • That's correct. Well, you should lose quality the first time but after that you should be able to keep the same quality standard. Maybe you could do some introspection as to if the file is a jpeg already. I'm sure you know that compressing compressed files makes them bigger. – hwrdprkns Apr 30 '12 at 14:17
  • Thanks for your replies. I should have mentioned before that the images must be lossless since I'm editing those images back and forth. – Jona Apr 30 '12 at 16:27

1 Answers1

6

Trying to resolve the best/fastest possible way to read/write image to file came down to using plain old BitmapFactory. I have tried using NDK to do the encoding/decoding but that really didn't make a difference.

Essentially the format to use was lossless PNG since I didn't want to loose any quality after editing an image.

The main concept from all this was that I needed to understand was how long encoding took versus decoding. The encoding numbers where in the upper 300-600ms, depending on image size, and decoding was just fast, around 10-23ms.

After understanding all that I just created a worker thread that I passed images needing encoding and let it do the work without affecting the user experience. The image was kept cached in memory just in case it was needed right away before it was completely encoded and saved to file.

Jona
  • 13,325
  • 15
  • 86
  • 129