11

How do I interpret the returned array from build-in method getPixels for a Bitmap?

Here is my code:

public void foo() {
    int[] pixels;
    Bitmap bitmapFoo = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.test2);             
    int height = bitmapFoo.getHeight();
    int width = bitmapFoo.getWidth();

    pixels = new int[height * width];

    bitmapFoo.getPixels(pixels, 0, width, 1, 1, width - 1, height - 1);     
}

The array "pixels" gets returned with values from -988,602,635 to 1,242,635,509 and that was just from a few colors on a simple PNG file I made. How can I interpret the numbers that get returned from this method?

Edit: I realize this single integer represents a color. I just don't understand how to interpret this single integer into the RBG and alpha values that make up the color.

Thanks.

PS. If your asking yourself, "what is he trying to do?" I am trying to figure out a way to dynamically modify the color of a bitmap.

user432209
  • 20,007
  • 10
  • 56
  • 75

5 Answers5

14

You can also do the following to retrieve colors from an int :

int mColor = 0xffffffff;

int alpha = Color.alpha(mColor);
int red = Color.red(mColor);
int green = Color.green(mColor);
int blue = Color.blue(mColor);
Natie
  • 2,284
  • 3
  • 19
  • 20
11

It returns an int for the Color class.

The Color class defines methods for creating and converting color ints. Colors are represented as packed ints, made up of 4 bytes: alpha, red, green, blue. The values are unpremultiplied, meaning any transparency is stored solely in the alpha component, and not in the color components. The components are stored as follows (alpha << 24) | (red << 16) | (green << 8) | blue. Each component ranges between 0..255 with 0 meaning no contribution for that component, and 255 meaning 100% contribution. Thus opaque-black would be 0xFF000000 (100% opaque but no contributes from red, gree, blue, and opaque-white would be 0xFFFFFFFF

For example, when you use the Paint object:

Paint pRed = new Paint();
pRed.setColor(Color.RED);

setColor expects an int. Color.RED is that int value for their pre-defined meaning of "red".

Bryan Denny
  • 27,363
  • 32
  • 109
  • 125
  • Hey thanks, but I know it is a color. I should have phrased my question better, to ask what I really wanted. That's my bad and it has been edited. +1. – user432209 Nov 13 '10 at 14:21
  • @user432209 see my quoted portion. (alpha << 24) | (red << 16) | (green << 8) | blue is how the colors are packed into the int. – Bryan Denny Nov 13 '10 at 14:22
  • This is the part I don't get. (alpha << 24) | (red << 16) | (green << 8) makes no sense to me. – user432209 Nov 13 '10 at 14:24
  • 1
    It's bit logic where << means shift left and | means OR. http://en.wikipedia.org/wiki/Bitwise_operation – Bryan Denny Nov 13 '10 at 14:41
  • Each pixel is 8 bits in an RGB32 pixel. It's common to pack red, green, blue, and nowadays, alpha into a 32-bit data integer. The << moves the bits around to the 8, 16, and 24 bit positions Basically, it helps you place bits 1-8, 9-16, 17-24, and 25-32. Ever see #AARRGGBB or 0xAARRGGBB? A hex digit is 4 bits, and two hex digits are 8 bits. – Joe Plante Sep 27 '12 at 14:32
5

Even more:

int alpha=argb>>24;
int red=(argb & 0x00FF0000)>>16;
int green=(argb & 0x0000FF00)>>8;
int blue=(argb & 0x000000FF);
Community
  • 1
  • 1
JohnUopini
  • 960
  • 9
  • 24
1

If you have your alpha, red, green and blue values, your in color is equal to (alpha << 24) + (red << 16) + (green << 8) + blue.

To retrieve your alpha, red, green and blue values from an int, say argb:

int alpha=argb>>24;
int red=(argb-alpha)>>16;
int green=(argb-(alpha+red))>>8;
int blue=(argb-(alpha+red+green));
Alexis Dufrenoy
  • 11,784
  • 12
  • 82
  • 124
0

Besides that, I think it should be

bitmapFoo.getPixels(pixels, 0, width, 0, 0, width, height); 

The coordinates start from 0, right?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Phuah Yee Keat
  • 1,572
  • 1
  • 17
  • 17
  • According to the doc for [getPixels](http://developer.android.com/reference/android/graphics/Bitmap.html), Stride must be >= bitmap's width. You've used '0' for stride here. – vowel-house-might Jun 29 '12 at 10:24
  • The third parameter is stride, which equals width in the example. The paremeter list is as follows: `int[] pixels, int offset, int stride, int x, int y, int width, int height` – BVB Jun 05 '13 at 19:54