framebuffer getPixels()

Started by haijin, May 27, 2011, 09:38:36 PM

Previous topic - Next topic

haijin

Hi,

I am having trouble getting the right colors to generate a bitmap from the framebuffer.getPixels() method. My thought was that fb would return ints with 24bits, 1 byte per color and that, depending on the Config when creating bitmap and some previos manipulation I would get the right result. I've tried different options but none has been succesful so, I thought I better ask instead of banging my head on this...
first I tried for Config.RGB_565, doing the following conversion (and other bitwise operations combos):

                            int[] tmpPixels = fb.getPixels();
                            for (int i = 0; i < tmpPixels.length; i++){
                                int tmpInt = ((tmpPixels & 0xf8) >> 3);
                                tmpInt += ((tmpPixels & 0xfc00) >> 5);
                                tmpInt += ((tmpPixels & 0xf80000) >> 11);
                                tmpPixels = tmpInt;
                            }
                            lastImage = Bitmap.createBitmap(tmpPixels, fb.getWidth(), fb.getHeight(), Config.RGB_565);

got nice unintended predator style views, playing with that :)
also tried with a more comfortable format, Config.ARGB_8888, only adding opaque alpha:

                            int[] tmpPixels = fb.getPixels();
                            for (int i = 0; i < tmpPixels.length; i++)
                                tmpPixels = tmpPixels + 0xff000000;
                            lastImage = Bitmap.createBitmap(tmpPixels, fb.getWidth(), fb.getHeight(), Config.ARGB_8888);

and this got me a blueish tint (closer but not quite)... which actually I have also seen in some screenshot taking software... which requires to tick an option to invert red and blue... so I also tried inverting R and B (resulting in a  predator/hello kitty effect):

                            int[] tmpPixels = fb.getPixels();
                            for (int i = 0; i < tmpPixels.length; i++){
                                int tmpInt = tmpPixels + 0xff000000;
                                tmpInt &= 0xff00ff00;//remove r and b
                                int inv = ~(tmpPixels & 0x00ff00ff);//invert r and b
                                tmpInt += inv;//add r and b
                                tmpPixels = tmpInt;
                            }
                            lastImage = Bitmap.createBitmap(tmpPixels, fb.getWidth(), fb.getHeight(), Config.ARGB_8888);

any suggestions/ideas/piece of code with the solution?

cheers!

EgonOlsen

I'm not sure...I've never used this method on Android. Have you tried to put alpha at the end, i.e. rgba or bgra?

haijin

the alpha was in the right place, but not so the red and blue. Switching those generated the desired result:

                            int[] tmpPixels = fb.getPixels();
                            for (int i = 0; i < tmpPixels.length; i++){
                                int tmpInt = tmpPixels + 0xff000000;
                                int red = (tmpInt & 0x00ff0000)>>16;
                                int blue = (tmpInt & 0x000000ff)<<16;
                                tmpInt &= 0xff00ff00;
                                tmpInt += red + blue;
                                tmpPixels = tmpInt;
                            }
                            lastImage = Bitmap.createBitmap(tmpPixels, fb.getWidth(), fb.getHeight(), Config.ARGB_8888);

MrAdam

Quote from: haijin on May 27, 2011, 11:19:27 PM
the alpha was in the right place, but not so the red and blue. Switching those generated the desired result:

                            int[] tmpPixels = fb.getPixels();
                            for (int i = 0; i < tmpPixels.length; i++){
                                int tmpInt = tmpPixels + 0xff000000;
                                int red = (tmpInt & 0x00ff0000)>>16;
                                int blue = (tmpInt & 0x000000ff)<<16;
                                tmpInt &= 0xff00ff00;
                                tmpInt += red + blue;
                                tmpPixels = tmpInt;
                            }
                            lastImage = Bitmap.createBitmap(tmpPixels, fb.getWidth(), fb.getHeight(), Config.ARGB_8888);
Sorry for the bump, but Im having a bit of a problem with this, as the background is completely black
Any chance you can tell me how you fixed it?

EgonOlsen

i don't think that you can grab the camera image that way, because this grabs only the image created in the gl context. There has to be another way to get a screen shot of both combined...i guess. Worst case would be to grab the camera image in another way and combine both in your code.

MrAdam

#5
Quote from: EgonOlsen on May 24, 2012, 08:57:47 PM
i don't think that you can grab the camera image that way, because this grabs only the image created in the gl context. There has to be another way to get a screen shot of both combined...i guess. Worst case would be to grab the camera image in another way and combine both in your code.
Sorry, wasn't talking about the camera preview, already got that.
Im grabbing both and merging them with a canvas. But the frameBuffer.getPixels() is returning all black where it should be transparent after running the:
int[] tmpPixels = fb.getPixels();
                            for (int i = 0; i < tmpPixels.length; i++){
                                int tmpInt = tmpPixels[i] + 0xff000000;
                                int red = (tmpInt & 0x00ff0000)>>16;
                                int blue = (tmpInt & 0x000000ff)<<16;
                                tmpInt &= 0xff00ff00;
                                tmpInt += red + blue;
                                tmpPixels[i] = tmpInt;
                            }
                            lastImage = Bitmap.createBitmap(tmpPixels, fb.getWidth(), fb.getHeight(), Config.ARGB_8888);


Replacing "tmpInt &= 0xff00ff00" with " tmpInt &= 0x7f00ff00" makes the frameBuffer image semi-transparent.
Im not too shabby with the hex codes, but Im guessing that the alpha is overwritten. I've tried replacing the "ff"'s with "00"'s a few places to see if that did it, but to no avail

EgonOlsen

At first glance, this...


int tmpInt = tmpPixels[i] + 0xff000000;


seems to set the alpha to ff for all pixels. Try something like


int tmpInt = tmpPixels[i];


instead.

MrAdam

Quote from: EgonOlsen on May 25, 2012, 12:03:53 AM
At first glance, this...


int tmpInt = tmpPixels[i] + 0xff000000;


seems to set the alpha to ff for all pixels. Try something like


int tmpInt = tmpPixels[i];


instead.
That was my first thought as well, but that made the image 100% transparent ^^

EgonOlsen

...looks like i'm nulling the alpha values in the code that flips the image after grabbing it from the frame buffer. I'll upload a fixed version later today.

MrAdam

Quote from: EgonOlsen on May 25, 2012, 09:47:25 AM
...looks like i'm nulling the alpha values in the code that flips the image after grabbing it from the frame buffer. I'll upload a fixed version later today.
Thanks!

MrAdam

Quote from: EgonOlsen on May 25, 2012, 09:47:25 AM
...looks like i'm nulling the alpha values in the code that flips the image after grabbing it from the frame buffer. I'll upload a fixed version later today.
Not to rush you or anything, but do you know when today you might be able to look at it?

EgonOlsen

Maybe 6 hours from now. I'm still at work...

EgonOlsen

An easy hacky "fix" for now, would be to set alpha according to the background color. I.e. all black pixels are transparent, all others opaque.

MrAdam

Quote from: EgonOlsen on May 25, 2012, 02:36:36 PM
An easy hacky "fix" for now, would be to set alpha according to the background color. I.e. all black pixels are transparent, all others opaque.
True. Ill try it and see how it looks.

MrAdam

Quote from: MrAdam on May 25, 2012, 02:41:24 PM
Quote from: EgonOlsen on May 25, 2012, 02:36:36 PM
An easy hacky "fix" for now, would be to set alpha according to the background color. I.e. all black pixels are transparent, all others opaque.
True. Ill try it and see how it looks.
Looks horrible :P