Skip to content

Instantly share code, notes, and snippets.

@ajithbh
Created June 11, 2014 14:48
Show Gist options
  • Save ajithbh/1a7a14f3e80c0d5d23b5 to your computer and use it in GitHub Desktop.
Save ajithbh/1a7a14f3e80c0d5d23b5 to your computer and use it in GitHub Desktop.
RGB565 to UYV conversion table
static unsigned int uyv_table[0x10000];
void generate_rgb565_to_uyv_conv_table(void)
{
int y, u, v;
unsigned char r, g, b;
unsigned int color = 0;
for (color = 0; color < 0x10000; color++) {
r = ((color & 0xF800) >> 8) + ((color >> 13) & 0x07);
g = ((color & 0x07E0) >> 3) + ((color >> 9) & 0x03);
b = ((color & 0x001F) << 3) + ((color >> 2) & 0x07);
y = ((263 * r) + (516 * g) + (100 * b) + 16384) >> 10;
u = ((-152 * r) + (298 * g) + (450 * b) + 131072) >> 10;
v = ((450 * r) + (377 * g) + (-73 * b) + 131072) >> 10;
if (y > 235) y = 235; if (y < 16) y = 16;
if (u > 235) u = 235; if (u < 16) u = 16;
if (v > 235) v = 235; if (v < 16) v = 16;
uyv_table[color] = (v << 16) + (y << 8) + u;
}
}
void rgb565_to_uyvy(unsigned char *rbg565_buf, unsigned char *uyvy_buf, int x, int y, int w, int h, int stride)
{
register int i, j;
unsigned short int *rgb565 = NULL;
unsigned int *p = NULL;
for (i = 0; i < h; i++) {
rgb565 = (unsigned short int *) rbg565_buf + ((y + i) * stride + x);
p = (unsigned int *) uyvy_buf + ((y + i) * stride + y) >> 1);
for (j = 0; j < w; j+=2) {
*p++ = (unsigned int)(uyv_table[*rgb565] & 0xFFFFFF) + (unsigned int) ((uyv_table[*(rgb565 + 1)] << 16) & 0x0FF000000);
rgb565 += 2;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment