Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - converting byte rgb_565 array into argb or rgb array

I have Picture data in byte rgb_565 array, and I want convert it in a productive way into argb array. Right now I have found only one (little slow) way to do this:

Bitmap mPhotoPicture = BitmapFactory.decodeByteArray(imageData, 0 , imageData.length);

where imageData is my byte[] array in rgb_565, and then:

int pixels[] = new int[CameraView.PICTURE_HEIGHT*CameraView.PICTURE_WIDTH];
mPhotoPicture.getPixels(pixels, 0,PICTURE_WIDTH, 0, 0, PICTURE_WIDTH, PICTURE_HEIGHT);

The point is I believe creating a Bitmap object is exacting and not necessary in this case. Is there any other faster way to convert rgb_565 array into argb array?

I need this because making image processing on rgb_565 array seems to be a little annoying. Or maybe it is not so hard?

like image 643
Kubeczek Avatar asked Jan 12 '10 14:01

Kubeczek


1 Answers

Why don't you do it by hand? A table is in my experience fastest:

C-code:

static unsigned char rb_table[32];
static unsigned char g_table[64];

void init (void)
{
  // precalculate conversion tables:
  int i;
  for (i=0; i<32; i++)
    rb_table[i] = 255*i/31;
  for (i=0; i<64; i++)
    g_table[i] = 255*i/63;
}


void convert (unsigned int * dest, unsigned short * src, int n)
{
  // do bulk data conversion from 565 to rgb32
  int i;

  for (i=0; i<n; i++)
  {
    unsigned short color = src[i];

    unsigned int red   = rb_table[(color>>11)&31]<<16;
    unsigned int green = g_table[(color>>5)&63]<<8;
    unsigned int blue  = rb_table[color&31];

    dest[i] = red|green|blue;
  }
}
like image 87
Nils Pipenbrinck Avatar answered Sep 18 '22 16:09

Nils Pipenbrinck