0
votes

I'm downloading image data over the network and obviously it's compressed. Inside the app I need the data to be 100% accurate to the original. So for example for a 720x720 image I need a 518400 length int array or 2073600 length byte array where the values have not been modified from the original. Since PNG compression is lossless this should be possible.I noticed that BitmapFactory applies some psychovisual effects when decoding that aren't noticeable when looking at the image, however change the byte values ever so slightly (I'm assuming this is for speed and/or better visual look on screen).

What I'm currently doing is using BitmapFactory.decode and then iterating over every pixel to create a new array from approximation and creating a new bitmap with Bitmap.create This is possible because for now the images are binary and in the original images each pixel is either (r, g, b, 255) or (0, 0, 0, 0). I can find the most prevalent colour and set all pixels with alpha values over some threshold to this RGB value. The solution is quite slow though and seems needlessly complicated.

So my question is - is there any combination of options flags for BitmapFactory.decode that returns a 100% true to the original Bitmap or do I have to create a PNG decoder myself to achieve this?

1
i have no idea what "psychovisual effects" ~ you mean... you dont have to add any extra flags and the decoded Bitmap will be 100% copy of the original - pskink
I have no idea why you think that the bitmap is being processed by decode. YOu can look at the source and see that isn't the case. - Gabe Sechan
I think you could call it antialiasing or at least the idea is similar. I'm not really knowledgeable about graphics. For a binary image the edge pixels are distorted by just a few values. An image that is binary in original and should have only 2 colour values when decoded, instead has somewhere around 10 different RGB values when looking at the raw data. These values differ ever so slightly from each other. So for example if an image should have (255, 0, 0, 255) and (0, 0, 0, 0) pixel values it has a few (254, 0, 0, 255) and some (254, 0, 0, 10) etc. I want to get rid of this effect. - JaanTohver
@pskink - running your code yields the expected results - JaanTohver
@usr2564301 - BitmapFactory.decodeByteArray(bytes, 0, bytes.size) is the only relevant line of code. Unfortunately I can't share the source data due to company policy. But thank you for the comment. I will take a closer look at the source and try to figure out what exactly is happening. - JaanTohver

1 Answers

-1
votes

Have a try with this method:

val opts = BitmapFactory.Options()
opts.inPremultiplied = false
var bitmapDecode = BitmapFactory.decodeByteArray(decode, 0, decode.size, opts)