convert 24bit RGB to ARGB16

和自甴很熟 提交于 2019-12-25 00:37:59

问题


I have to read a 24bpp Bitmap and convert each pixel from RGB24 to ARGB16.

I used the following code,

#define ARGB16(a, r, g, b) ( ((a) << 15) | (r)|((g)<<5)|((b)<<10))

But I am not getting the required Output.

Any help would be highly appreciated.


回答1:


Break it up. Let's continue to use macros:

#define TRUNCATE(x)  ((x) >> 3)

#define ARGB16(a,r,g,b) ((a << 15) | (TRUNCATE(r) << 10) | (TRUNCATE(g) << 5) | TRUNCATE(b)))

This assumes the alpha is just a single bit.




回答2:


Since the RGB values are probably 8 bits each, you still need to truncate them to five bits so they won't "overlap" in the ARGB16 value.

The easiest way to truncate them is probably to bitshift them to the right by three places.




回答3:


It looks to me like you probably want to mask off the bits you don't need from r, g and b. Maybe something like this:

#define ARGB16(a, r, g, b) ( ((a) << 15) | (r>>3)|((g>>3)<<5)|((b>>3)<<10))

Edit: whoops, I think the answer by Michael Buddingh is probably right - you'll want to shift off, this gets you the most significant bits.




回答4:


I would either use a bitmask to get rid of the bits you don't need: (colorvalue & 0x1F).

Either shift the color value to the right 3 bits (seems like the better option).

And is a << 15 really what you need? You would be relying on the fact that the 0 bit is 0 or 1 to set the alpha bit on or off. So if your alpha value is 0xFE then the alpha bit would 0, whereas if it were 0x01 it would be 1.




回答5:


You might to want to SCALE rather than TRUNCAT.

Take R component for example , 0xDE(222) in 24bit RGB will become 0x1A = (222.0/0xFF)*0x1F in 16bit RGB.




回答6:


i have the code bellow :

    FILE *inFile;
BmpHeader header;
BmpImageInfo info;
Rgb *palette;
int i = 0;

    inFile = fopen( "red.bmp", "rb" );

fread(&header, 1, sizeof(BmpHeader), inFile);
fread(&info, 1, sizeof(BmpImageInfo), inFile);

palette = (Rgb*)malloc(sizeof(Rgb) * info.numColors);

fread(palette, sizeof(Rgb), info.numColors, inFile);

unsigned char buffer[info.width*info.height];

FILE *outFile = fopen( "red.a", "wb" );
Rgb *pixel = (Rgb*) malloc( sizeof(Rgb) );

int read, j;

for( j=info.height; j>0; j-- ) 
{
    for( i=0; i<info.width; i++ ) 
    {
        fread(pixel, 1, sizeof(Rgb), inFile);
        buffer[i] = ARGB16(0, pixel->red, pixel->green, pixel->blue);
    }
}

    fwrite(buffer, 1, sizeof(buffer), outFile);

and i am reading a red image(255 0 0), and i am using the function defined by you above(#define ARGB16(a, r, g, b) ( ((a) << 15) | (r>>3)|((g>>3)<<5)|((b>>3)<<10)) ), but the output file shows me : 1F 1F 1F when i am opening the file with a hexaeditor instead of 7C00 ..



来源:https://stackoverflow.com/questions/1553502/convert-24bit-rgb-to-argb16

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!