将 24 位 RGB 转换为 ARGB16

发布于 2024-08-07 16:05:27 字数 254 浏览 11 评论 0原文

我必须读取 24bpp 位图 并将每个像素从 RGB24 转换为 ARGB16

我使用了以下代码,

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

但没有得到所需的输出。

任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浪菊怪哟 2024-08-14 16:05:27

打破它。让我们继续使用宏:

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

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

这里假设 alpha 只是一位。

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.

楠木可依 2024-08-14 16:05:27

由于 RGB 值可能每个都是 8 位,因此您仍然需要将它们截断为 5 位,这样它们就不会在 ARGB16 值中“重叠”。

截断它们的最简单方法可能是将它们向右移动三位。

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.

墨洒年华 2024-08-14 16:05:27

在我看来,您可能想从 r、g 和 b 中屏蔽掉不需要的位。也许是这样的:

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

编辑:哎呀,我认为迈克尔·巴丁的答案可能是正确的 - 你会想要转移,这会让你得到最重要的位。

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.

定格我的天空 2024-08-14 16:05:27

我要么使用位掩码来删除不需要的位:(颜色值和0x1F)。

将颜色值向右移动 3 位(似乎是更好的选择)。

并且是一个<< 15.你真正需要什么?您将依赖 0 位是 0 或 1 的事实来设置 alpha 位的打开或关闭。因此,如果您的 alpha 值是 0xFE,那么 alpha 位将为 0,而如果是 0x01,则为 1。

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.

完美的未来在梦里 2024-08-14 16:05:27

您可能想要 SCALE 而不是 TRUNCAT。

以 R 组件为例,24 位 RGB 中的 0xDE(222) 将变为 0x1A = (222.0/0xFF)* 16 位 RGB 中的 0x1F。

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.

吃→可爱长大的 2024-08-14 16:05:27

我有下面的代码:

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

我正在读取红色图像(255 0 0),并且我正在使用上面定义的函数(#define ARGB16(a, r, g, b) ( ((a) << ; 15) | (r>>3)|((g>>3)<<5)|((b>>3)<<10))
),但是当我使用十六进制编辑器而不是 7C00 打开文件时,输出文件显示: 1F 1F 1F ..

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 ..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文