last day (15 days later) » 

7:04 PM
0
Q: BitmapFactory.decodeStream() increasing image size?

John SardinhaI'm trying to take an image, resize it, and save it again, but resolving an image with BitmapFactory.decodeStream() and saving it to an OutputStream again seems to be increasing images' size, while I'd expect it to remain the same. Logger.log(TAG, "Original bitmap inputstream size: ${inputStream...

 
Does the bitmap have a lower bit-depth than the jpeg?
 
How do I check? @FedericoklezCulloca
 
Download the images to a pc and then, depending on what OS you use, right click -> properties may have that information somewhere
 
I found a tag called "Bits per sample", which the value is 8, I assume that's the depth. Just checked the default config on BitmapFactory.Options() and it's Bitmap.Config.ARGB_8888, which is documented with "Each pixel is stored on 4 bytes." So does this make each pixel with the fixed size of 32 bits where the actual data just takes 8?
 
What you 'discovered' is quite normal behaviour. BitmapFactory makes a Bitmap from a jpg or png. Every program stores jp's different as the have different compression algoritms and take different default values and at conversions as you do the amount of bytes may change. You could try to load the saved jpg again and then save again. Or better save a bitmap as jpg 30%, load it again and save as 100%. You will see quite a difference in file size.
 
7:04 PM
How can we then resolve the image using its depth? The only available config in Bitmap.Config that uses 8 bits per pixel is ALPHA_8 which just saves alpha values.
 
It is unclear to me what you mean by resolving the image using its dept. If the obtained bitmap is ok then bitmap factory will have used its dept in the right way. Once you have a bitmap all info of the source is gone except for resolution . .. and the picture.
 
I mean, if this image's depth is 1 byte, and if I can get that depth inspecting its exif properties, then I can specify decodeStream() to use an 8 bit depth instead of 32. I assume this is what's causing the resolved bitmap to be ~2.5x larger than the original file.
 
But you already found out that ALPHA_8 is the only 8 bit compress possibility hence even if you want to save in 8 bit format you are out of luck under Android. You could use the 565 to keep it small. Well.. using BitmapFactory.
I dont think the exif header has info about the jpgs bit-depth. Jpg's come without exif header too.
 
So you're saying that Android's image API doesn't allow for resolving an image in the exact schema that it's composed with, i.e. so that by saving it again it would have the same exact size? @blackapps
 
Indeed. You understood what i said. Maybe saving bitmap with 100% reloading and then saving again with 100% would deliver nearly the same amount of bytes in the jpg files. (You will have checked that by now i suppose).
Is the original jpg greyscale?
 
7:04 PM
No, it's an unfiltered, unedited picture from a phone's camera.
 
I would like to have a look. Can you put such an image on the internet and post a link here?
 
The second parameter to compress() is a quality level, in the range 0..100. It is not a "percent of original size".
 
@blackapps Also just learned that there's some ambiguity to the depth, when referenced as sample depth it's the amount of bits per color, so that being 8 for this image it means it's 32, so it should actually be ARGB_8888. Here's an image that's 2.9MB, 33MB after decodeStream() and 3.6MB after .compress() send.firefox.com/download/050694fe9f5ec2ee/…
@greeble31 The max quality should be the original quality of bitmap. What's the difference?
 
Thanks. DSC_0032.JPG is 24 bits per pixel according IrfanView. (Like my Android pictures). Not 8. Size 2.917.799 bytes. So it resuls that the different compression algotitms cause the change in file size.
 
Right, makes sense since there's no alpha scale, but there's still no option in Bitmap.Cconfig for RGB_888
 
7:04 PM
Well that is the default value. It will be 24 bits too.
 
@JohnSardinha You should think of quality as a subjective value. With quality, you're essentially choosing a rough output bits-per-pixel. A compression algorithm has no way of knowing how "compressed" an input bitmap was prior to decompression. It's not a repeatable transform (except perhaps in certain edge cases), in the sense that you shouldn't expect compress(decompress(compress(bitmap))) to equal compress(bitmap).
 
But every JPG is by definition somewhat compressed, so compress(JPEG, 100, bitmap) would always somehow attempt to decompress. the method's documentation states: "0 meaning compress for small size, 100 meaning compress for max quality.", I wouldn't interpret this as quality being subjective, I would interpret as quality is related to max potential quality, which is the original received data.
 
"0 meaning compress for small size, 100 meaning compress for max quality.", That is not good formulated. It should be: "0 meaning compress for the lowest quality, 100 meaning compress for max quality.", But in practice its the same as "0 meaning compress for small size, 100 meaning compress for maximum size.",
 
Max quality being the original quality of the input image, meaning it shouldn't try to attempt any decompression, simply not compress.
 
Nope, it simply doesn't work that way.
 
7:04 PM
How exactly does it work?
 
7:30 PM
I would refer you to the wikipedia article for a detailed discussion. Now, you're kinda driving at two different things in your question. I think I can answer the first directly: decodeStream() does some basic file-format detection, and if it is JPEG, runs the JPEG decompression algorithm on the input data.
Your second question, about the increase in size with quality of 100 -- if that's really bothering you, you should go into more detail and/or ask another question about the behavior you need. After all, you already have the original, compressed image (in the InputStream), so it's not necessary to try to re-create it via an expensive compression computation.
 

last day (15 days later) »