Author Topic: framebuffer getPixels()  (Read 18557 times)

Offline haijin

  • byte
  • *
  • Posts: 37
    • View Profile
framebuffer getPixels()
« on: May 27, 2011, 09:38:36 pm »
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!

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: framebuffer getPixels()
« Reply #1 on: May 27, 2011, 09:58:32 pm »
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?

Offline haijin

  • byte
  • *
  • Posts: 37
    • View Profile
Re: framebuffer getPixels()
« Reply #2 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);

Offline MrAdam

  • int
  • **
  • Posts: 51
    • View Profile
Re: framebuffer getPixels()
« Reply #3 on: May 24, 2012, 06:05:13 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?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: framebuffer getPixels()
« Reply #4 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.

Offline MrAdam

  • int
  • **
  • Posts: 51
    • View Profile
Re: framebuffer getPixels()
« Reply #5 on: May 25, 2012, 12:00:10 am »
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:
Code: [Select]
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
« Last Edit: May 25, 2012, 12:02:52 am by MrAdam »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: framebuffer getPixels()
« Reply #6 on: May 25, 2012, 12:03:53 am »
At first glance, this...

Code: [Select]
int tmpInt = tmpPixels[i] + 0xff000000;

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

Code: [Select]
int tmpInt = tmpPixels[i];

instead.

Offline MrAdam

  • int
  • **
  • Posts: 51
    • View Profile
Re: framebuffer getPixels()
« Reply #7 on: May 25, 2012, 09:39:30 am »
At first glance, this...

Code: [Select]
int tmpInt = tmpPixels[i] + 0xff000000;

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

Code: [Select]
int tmpInt = tmpPixels[i];

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

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: framebuffer getPixels()
« Reply #8 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.

Offline MrAdam

  • int
  • **
  • Posts: 51
    • View Profile
Re: framebuffer getPixels()
« Reply #9 on: May 25, 2012, 10:02:13 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!

Offline MrAdam

  • int
  • **
  • Posts: 51
    • View Profile
Re: framebuffer getPixels()
« Reply #10 on: May 25, 2012, 02:17:18 pm »
...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?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: framebuffer getPixels()
« Reply #11 on: May 25, 2012, 02:31:37 pm »
Maybe 6 hours from now. I'm still at work...

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: framebuffer getPixels()
« Reply #12 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.

Offline MrAdam

  • int
  • **
  • Posts: 51
    • View Profile
Re: framebuffer getPixels()
« Reply #13 on: May 25, 2012, 02:41:24 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.

Offline MrAdam

  • int
  • **
  • Posts: 51
    • View Profile
Re: framebuffer getPixels()
« Reply #14 on: May 25, 2012, 02:56:37 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