【问题标题】:How to convert bitmap 16-bit RGBA4444 to 16-bit Grayscale in C++?如何在 C++ 中将位图 16 位 RGBA4444 转换为 16 位灰度?
【发布时间】:2015-10-10 06:20:06
【问题描述】:

我用的方法:

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) // ???

有人知道怎么做吗?

【问题讨论】:

    标签: c++ image-processing bitmap


    【解决方案1】:

    您确定需要 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));
    }
    

    【讨论】:

    • 我需要的是如何将RGB位图转换为灰度,不要将m位转换为n位。示例:BMP 16 位:A7 36 (BGRA) --> CE 39 (灰度)。 BMP 24 位:0E 0F EF (BGR) --> 51 51 51 (灰度)
    • 那么为什么不从 ARGB4444 中提取 R、G、B 的 4 位值,将每个值左移 4 位以将它们转换为完整字节,然后使用您的公式:val = 0.299 * R + 0.587 * G + 0.114 * B?
    猜你喜欢
    • 2015-08-11
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多