tags:

views:

57

answers:

3

I have some .png files in my app. I need to load these during runtime, and get the exact colors of certain pixels from them. It's important, that I do not want to scale these pictures. I don't show them on the UI directly, they serve as maps.

Now, on Android 1.5, there's no problem with this. I put these images in the '/res/drawable' dir, load them with BitmapFactory into a Bitmap object, and use it to get the color of the desired pixels. E.g. pixel (100, 50) has the color RGB(100, 1, 100).

On Android 2.2 tho, the same procedure results varying colors (for the same pixel), so I get RGB(99, 3, 102) / RGB(101, 2, 99) / etc. for the same (100, 50) pixel. I checked the resolution of the Bitmap object, it seems that is didn't get scaled.

Could somebody explain, why I get distorted colour values?

+1  A: 

Go make yourself a bitmap thats entirely the same color of the pixel in question. Make the size of this bitmap the same resolution of the one your currrently using. Load it up and check the RGB values of the same pixel (or any pixel) you are having problems with.

This should tell you whether your problem is either scaling, which is what I think it is, or possibly a problem in the color translation.

I did it, created a bitmap with the same size, and filled it with a color (100, 22, 100), loaded it, and tried to get color values for different pixels. I got varying colors (all close to the given color), even tho the whole picture was filled with a solid color... What am I missing here?
Scythe
A: 

If you don't find an answer quickly, my pragmatist streak would ask how hard it is to parse the .png yourself, to get completely deterministic results independent of any changes in the platform.

Chris Stratton
True, but I think it's a reasonable expectation for such a modern framework to be able to load a damn .png.
Scythe
Well, it can - but the desire to have it loaded unaltered for use as data may be different than the framework implementors' apparent desire to support the best visual display (which might include dithering, or anti-aliasing or who knows what). Glad you figured out a way to do it with platform functionality though.
Chris Stratton
+1  A: 

Solved: It appears, that on Android 2.2, I have to set the correct bitmap configuration. Somehow, versions below 2.2 managed to do this (or maybe fewer configs are supported on those, and the system guessed the config correctly, don't know).

Anyways, here's the code I use now:

BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inDither=false;
        opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap mask = BitmapFactory.decodeResource(getResources(), R.drawable.picture, opt);
Scythe