2

I am trying to make a very simple tile engine. However, using the Bitmap.getpixel(x,y) is not always matching the color correctly. It seems to be doing fine with 0xFFFFFFFF, 0xFF000000, 0xFF189600, and 0xFF18FF00, but has a problem with 0xFF186600. I tried changing it to multiple different similar colors, but it still doesn't seem to be reading it correctly. I am comparing with a simple switch statement. Here is the code for my method

public void LoadLevel(Canvas canvas, int levelName)
{

        Bitmap level = BitmapFactory.decodeResource(getResources(), levelName);
        Bitmap startTile = BitmapFactory.decodeResource(getResources(), R.drawable.starttile);

        canvas.drawColor(Color.WHITE);

        int drawX = 0;
        int drawY = 0;

        for(int y = 0; y < level.getHeight(); y++)
        {
            for(int x = 0; x < level.getWidth(); x++)
            {
                switch(level.getPixel(x, y))
                {
                    case 0xFF000000: break;

                    case 0xFFFFFFFF: 
                        canvas.drawBitmap(startTile, drawX, drawY, null); 
                        break;

                    case 0xFF189600: 
                        canvas.drawBitmap(startTile, drawX, drawY, null); 
                        break;

                    case 0xFF18FF00: 
                        canvas.drawBitmap(startTile, drawX, drawY, null); 
                        break;

                    case 0xFF186600: 
                        canvas.drawBitmap(startTile, drawX, drawY, null); 
                        break;
                }

                Log.d("Color", Integer.toString(level.getPixel(x, y)));

                drawX += 128;
            }
            drawX = 0;
            drawY += 128;
        }
}

According to the log, the color is "-15177472". I am not sure what color that actually is though... So I am not sure if -15177472 == 0xFF186600

What am I doing incorrectly to not get the pixel? Is android changing the image? Are there safe colors I am suppose to use?

Jonathan Spooner
  • 7,682
  • 2
  • 34
  • 41
NullMan
  • 43
  • 7
  • 0xff39ff00 seems to work, but the color is too similar to the other ones. This is getting very annoying. – NullMan Nov 28 '11 at 01:35
  • It seems to be "safe" with colors like ffff00, ff00ff, 0000ff, even 00aeff. But not 999900, 666600, etc. I am not sure why, but this is how it seems to be. Really badly inaccurate. Wow. – NullMan Nov 28 '11 at 02:46

1 Answers1

0

-15177472 is 0xFF186900. Note the 9 digit instead of the 6 you were looking for.

Where are you getting these expected values from, and how are you loading the bitmaps? If you are creating the bitmaps in a drawing program, and then loading them in your Android app, you may find that they are being decompressed as 16bpp, in which case there will be some loss of accuracy when converting pixel values back to 32bpp for getPixel().

Graham Borland
  • 60,055
  • 21
  • 138
  • 179