简体   繁体   English

将RGB 12位数据转换为RGB 12位打包数据

[英]To convert RGB 12 bit data to RGB 12 bit packed data

I have some RGB(image) data which is 12 bit. 我有一些12位的RGB(图像)数据。 Each R,G,B has 12 bits, total 36 bits. 每个R,G,B有12位,共36位。 Now I need to club this 12 bit RGB data into a packed data format. 现在,我需要将此12位RGB数据合并为打包数据格式。 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. 目前,我有输入数据-B0-12位G0-12位R0-12位B1-12位G1-12位R1-12位.. I need to convert it to packed format as:- 我需要将其转换为打包格式为:-

Byte1 - B8 (8 bits of B0 data) 字节1-B8(B0数据的8位)

Byte2 - G4B4 (remaining 4 bits of B0 data+ first 4 bits of G0) 字节2-G4B4(剩余的B0数据的4位+ G0的前4位)

Byte3 - G8 (remaining 8 bits of G0) 字节3-G8(G0的剩余8位)

Byte4 - R8 (first 8 bits of R0) 字节4-R8(R0的前8位)

Byte5 - B4R4 (first 4 bits of B1 + last 4 bits of R0) 字节5-B4R4(B1的前4位+ R0的后4位)

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. 我必须为10位RGB输入数据做类似的事情。

  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). 我正在尝试在C程序中进行操作-我正在从R,G,B的单个12位(总共36位)中形成64位。 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. 但是在那之后,我无法提出从R,G,B数据中选择必要的位以形成字节流并将其转储到文本文件的逻辑。

Any pointers will be helpful. 任何指针都会有所帮助。

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);
}

Use bitwise & (and), | 使用按位 (和), |。 (or), and shift << , >> operators. (或),并移动<<>>运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM