[ create a new paste ] login | about

Link: http://codepad.org/6S0HGvgG    [ raw code | output | fork ]

mohit_at_codepad - C, pasted on Sep 2:
/* Fast method to convert 565 to UNORM 8 */
/* This would retain the perfect black and white by */
/* mapping all 0 to all 0 and all 1 to all 1 and almost */
/* linear mapping for the remaining */

unsigned int convert5To8bits(unsigned int in)
{
  /* assert(in <= 0x1F); */
  const unsigned int out = (in << 3) | (in >> 2);
  printf("0x%02X -> 0x%02X\n", in, out);
  return out;
}

unsigned int convert6To8bits(unsigned int in)
{
  /* assert(in <= 0x3F); */
  const unsigned int out = (in << 2) | (in >> 4);
  printf("0x%02X -> 0x%02X\n", in, out);
  return out;
}

unsigned int convert565To888bits(unsigned int in)
{
  /* assert(in <= 0xFFFF); */
  const unsigned int clr[] = {(in >> 11), ( (in >> 5) & 0x3F ), (in & 0x1F)};
  const unsigned int out =  (convert5To8bits(clr[0]) << 16) |
                            (convert6To8bits(clr[1]) << 8) |
                            (convert5To8bits(clr[2]) << 0); 
  printf("0x%04X -> 0x%06X\n", in, out);
  return out;
}

int main()
{
    unsigned int i;
    printf("Converting 5 bits to 8 bits:\n");
    for(i = 0U; i <= 0x1FU; ++i) convert5To8bits(i);

    printf("\n");
    printf("Converting 6 bits to 8 bits:\n");
    for(i = 0U; i <= 0x3FU; ++i) convert6To8bits(i);

    printf("\n");
    printf("Converting 565 to 888:\n");
    convert565To888bits(0x0000);
    printf("\n");
    convert565To888bits(0x1212);
    printf("\n");
    convert565To888bits(0xFFFF);
    return 0;
}


Output:
Converting 5 bits to 8 bits:
0x00 -> 0x00
0x01 -> 0x08
0x02 -> 0x10
0x03 -> 0x18
0x04 -> 0x21
0x05 -> 0x29
0x06 -> 0x31
0x07 -> 0x39
0x08 -> 0x42
0x09 -> 0x4A
0x0A -> 0x52
0x0B -> 0x5A
0x0C -> 0x63
0x0D -> 0x6B
0x0E -> 0x73
0x0F -> 0x7B
0x10 -> 0x84
0x11 -> 0x8C
0x12 -> 0x94
0x13 -> 0x9C
0x14 -> 0xA5
0x15 -> 0xAD
0x16 -> 0xB5
0x17 -> 0xBD
0x18 -> 0xC6
0x19 -> 0xCE
0x1A -> 0xD6
0x1B -> 0xDE
0x1C -> 0xE7
0x1D -> 0xEF
0x1E -> 0xF7
0x1F -> 0xFF

Converting 6 bits to 8 bits:
0x00 -> 0x00
0x01 -> 0x04
0x02 -> 0x08
0x03 -> 0x0C
0x04 -> 0x10
0x05 -> 0x14
0x06 -> 0x18
0x07 -> 0x1C
0x08 -> 0x20
0x09 -> 0x24
0x0A -> 0x28
0x0B -> 0x2C
0x0C -> 0x30
0x0D -> 0x34
0x0E -> 0x38
0x0F -> 0x3C
0x10 -> 0x41
0x11 -> 0x45
0x12 -> 0x49
0x13 -> 0x4D
0x14 -> 0x51
0x15 -> 0x55
0x16 -> 0x59
0x17 -> 0x5D
0x18 -> 0x61
0x19 -> 0x65
0x1A -> 0x69
0x1B -> 0x6D
0x1C -> 0x71
0x1D -> 0x75
0x1E -> 0x79
0x1F -> 0x7D
0x20 -> 0x82
0x21 -> 0x86
0x22 -> 0x8A
0x23 -> 0x8E
0x24 -> 0x92
0x25 -> 0x96
0x26 -> 0x9A
0x27 -> 0x9E
0x28 -> 0xA2
0x29 -> 0xA6
0x2A -> 0xAA
0x2B -> 0xAE
0x2C -> 0xB2
0x2D -> 0xB6
0x2E -> 0xBA
0x2F -> 0xBE
0x30 -> 0xC3
0x31 -> 0xC7
0x32 -> 0xCB
0x33 -> 0xCF
0x34 -> 0xD3
0x35 -> 0xD7
0x36 -> 0xDB
0x37 -> 0xDF
0x38 -> 0xE3
0x39 -> 0xE7
0x3A -> 0xEB
0x3B -> 0xEF
0x3C -> 0xF3
0x3D -> 0xF7
0x3E -> 0xFB
0x3F -> 0xFF

Converting 565 to 888:
0x00 -> 0x00
0x00 -> 0x00
0x00 -> 0x00
0x0000 -> 0x000000

0x02 -> 0x10
0x10 -> 0x41
0x12 -> 0x94
0x1212 -> 0x104194

0x1F -> 0xFF
0x3F -> 0xFF
0x1F -> 0xFF
0xFFFF -> 0xFFFFFF


Create a new paste based on this one


Comments: