我用的方法:

val = 0.299 * R + 0.587 * G + 0.114 * B;
image.setRGBA(val, val, val, 0);


将位图(24位RGB888和32位RGBA8888)成功转换为灰度。

Example: BMP 24-bit: 0E 0F EF (BGR) --> 51 51 51 (Grayscale) // using above method

但不能申请位图16位RGBA4444。

Example: BMP 16-bit: 'A7 36' (BGRA) --> 'CE 39' (Grayscale) // ???

有人知道吗?

最佳答案

您确定需要RGBA4444格式吗?也许您需要一种旧格式,其中绿色通道获得6位,而红色和蓝色通道获得5位(共16位)

如果是5-6-5格式-答案很简单。
做就是了
R =(R >> 3); G =(G >> 2); B =(B >> 3);将24位减少为16位。操作。

这是C语言中的示例代码

// Combine RGB into 16bits 565 representation. Assuming all inputs are in range of 0..255
static INT16  make565(int red, int green, int blue){
    return (INT16)( ((red   << 8) & 0xf800)|
                    ((green << 2) & 0x03e0)|
                    ((blue  >> 3) & 0x001f));
}


上面的方法使用与常规ARGB构造方法大致相同的结构,但是将颜色压缩为16位而不是32位,如以下示例所示:

// Combine RGB into 32bit ARGB representation. Assuming all inputs are in range of 0..255
static INT32  makeARGB(int red, int green, int blue){
    return (INT32)((red)|(green << 8)|(blue<<16)|(0xFF000000)); // Alpha is always FF
}


如果您确实需要RGBA4444,则该方法将是上述两种方法的组合

// Combine RGBA into 32bit 4444 representation. Assuming all inputs are in range of 0..255
static INT16  make4444(int red, int green, int blue, int alpha){
    return (INT32)((red>>4)|(green&0xF0)|((blue&0xF0)<<4)|((alpha&0xF0)<<8));
}

关于c++ - 如何在C++中将位图16位RGBA4444转换为16位灰度?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33050974/

10-11 22:40