views:

443

answers:

2

Hello,

I have some RGB(image) data which is 12 bit. Each R,G,B has 12 bits, total 36 bits. Now i need to club this 12 bit RGB data into a packed data format. I have tried to mention the packing as below:-

At present I have input data as - B0 - 12 bits G0 - 12 bits R0 - 12 bits B1 - 12 bits G1 - 12 bits R1 - 12 bits .. so on. I need to convert it to packed format as:-

Byte1 - B8 (8 bits of B0 data)

Byte2 - G4B4 (remaining 4 bits of B0 data+ first 4 bits of G0)

Byte3 - G8 (remaining 8 bits of G0)

Byte4 - R8 (first 8 bits of R0)

Byte5 - B4R4 (first 4 bits of B1 + last 4 bits of R0)

I have to write these individual bytes to a file in text format. one byte below another.

Similar thing i have to do for a 10 bit RGB input data.

1.) Is there any tool/software to get the conversion of data i am looking to get done.

2.) I am trying to do it in a C program - I am forming a 64 bit from the individual 12 bits of R,G,B (total 36 bits). But after that i am not able to come up with a logic to pick the necessary bits from a R,G,B data to form a byte stream, and to dump them to a text file.

Any pointers will be helpful.

Thank you.

-AD

A: 

Use bitwise & (and), | (or), and shift <<, >> operators.

Karl Voigtland
@Karl: I know, i have to use those bit wise operators, but final logic is not falling in place for the entire image, hence looking for pointers.
goldenmean
Maybe you could post what you were able to come up with?
Karl Voigtland
+1  A: 

This is pretty much untested, super messy code I whipped together to give you a start. It's probably not packing the bytes exactly as you want, but you should get the general idea.

Apologies for the quick and nasty code, only had a couple of minutes, hope it's of some help anyway.

#include <stdio.h>

typedef struct
{
    unsigned short B;
    unsigned short G;
    unsigned short R;

} UnpackedRGB;

UnpackedRGB test[] = 
{
    {0x0FFF, 0x000, 0x0EEE},
    {0x000, 0x0FEF, 0xDEF},
    {0xFED, 0xDED, 0xFED},
    {0x111, 0x222, 0x333},
    {0xA10, 0xB10, 0xC10}
};

UnpackedRGB buffer = {0, 0, 0};

int main(int argc, char** argv)
{   
    int numSourcePixels = sizeof(test)/sizeof(UnpackedRGB);

    /* round up to the last byte */
    int destbytes = ((numSourcePixels * 45)+5)/10; 

    unsigned char* dest = (unsigned char*)malloc(destbytes); 
    unsigned char* currentDestByte = dest;

    UnpackedRGB *pixel1;
    UnpackedRGB *pixel2;
    int ixSource;
    for (ixSource = 0; ixSource < numSourcePixels; ixSource += 2)
    {      
        pixel1 = &test[ixSource];
        pixel2 = ((ixSource + 1) < numSourcePixels ? &test[ixSource] : &buffer);

        *currentDestByte++ = (0x0FF) & pixel1->B;
        *currentDestByte++ = ((0xF00 & pixel1->B) >> 8) | (0x0F & pixel1->G);
        *currentDestByte++ = ((0xFF0 & pixel1->G) >> 4);
        *currentDestByte++ = (0x0FF & pixel1->R);
        *currentDestByte++ = ((0xF00 & pixel1->R) >> 8) | (0x0F & pixel2->B);

        if ((ixSource + 1) >= numSourcePixels)
        {
            break;
        }

        *currentDestByte++ = ((0xFF0 & pixel2->B) >> 4);
        *currentDestByte++ = (0x0FF & pixel2->G);
        *currentDestByte++ = ((0xF00 & pixel2->G) >> 8) | (0x0F & pixel2->R);
        *currentDestByte++ = (0xFF0 & pixel2->R);
    }

    FILE* outfile = fopen("output.bin", "w");
    fwrite(dest, 1, destbytes,outfile);
    fclose(outfile);
}
Andrew Barrett
@Andrew: Thanks for taking effort in coming up with code so quick! I am going through it. It seems to be pointing me in the right direction. Will sure let u know about the results once i implement the same myself.
goldenmean