【问题标题】:Encoding/Compressing ARGB to 3 bytes将 ARGB 编码/压缩为 3 个字节
【发布时间】:2014-06-18 01:57:14
【问题描述】:

我将 3 个字节存储在将被解码为 ARGB 的 bytearray 中,这没有问题,因为我有将 bytearray 解码为 int ARGB 的代码。问题是我如何将 int ARGB 重新编码(使用相同的编码方法,而不仅仅是将 RGB 值存储在字节数组中)到 3 大小的字节数组中?我已经为此工作了 2 周,我认为我真的需要帮助。

byte[] encodedBytes = new byte[]{(byte)0x4D, (byte)0x86, (byte)0x18};
int argb = decode(encodedBytes); // 0x4D616161 // 1298227553

// byte[] encodedBytes2 = new byte[]{(byte)0x6F, (byte)0x30, (byte)0x65};
// int argb = decode(encodedBytes2); // 0x6DCF0C14 // 1842285588

使用此函数将其解码为 ARGB:

public static int decode(byte[] bytes)
{
     // alpha
     // bytes[0] = (byte) 0x4D; // 77
     int temp = bytes[0] & 0xFC;
     int alpha = temp | temp >> 6;
     // re-encode with alpha ^ alpha >> 6;

     // red
     // bytes[1] = (byte) 0x86; // -122
     temp = (bytes[0] << 6) & 0xF0;
     int red = temp | 0x3C & (bytes[1] >> 2);
     red = red | red >> 6;
     // re-encode with ?

     // green
     // bytes[2] = (byte) 0x18; // 24
     temp = 0xF0 & bytes[1] << 4;
     int green = temp | 0xC & bytes[2] >> 4;
     green = green | green >> 6;
     // re-encode with ?

     // blue
     // blue and green uses same byte,
     // bytes[2] but will not result in same color
     temp = (0x3F & bytes[2]) << 2;
     int blue = temp | temp >> 6;
     // re-encode with ?

     // Result:
     // alpha = 77
     // red = 97
     // green = 97
     // blue = 97
     // argb = 0x4D616161 // 1298227553
     int argb = (alpha << 24 ) + (red << 16) + (green << 8) + blue;
     return argb;
}

这是我将 int ARGB 编码为 bytearray 的未完成代码。

public static byte[] encode(int argb)
{
     byte[] bytes = new byte[3];

     int alpha = (argb >> 24) & 0xFF;
     int red = (argb >> 16) & 0xFF;
     int green = (argb >> 8) & 0xFF;
     int blue = argb & 0xFF;

     bytes[0] = alpha ^ alpha >> 6;
     // results to 76 instead of 77

     red = red & red >> 6;
     int temp = (bytes[0] << 6) & 0xF0;
     bytes[1] = (temp ^ red) << 2;

     // ... missing code goes here

     return bytes;
}

我只是想创建一个从 byte[]{0,0,0} 到 byte[]{(byte)0xFF,(byte)0xFF,(byte)0xFF} 的表并比较 ARGB,但我认为它会浪费。再次重复这个问题,我如何将 int ARGB 重新编码(使用相同的编码方法,而不仅仅是将 RGB 值存储在字节数组中)到 3 大小的字节数组中? 任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: java argb


    【解决方案1】:
    alpha = (argb >> 24) & 0xfc;
    red = (argb >> 16) & 0xfc;
    green = (argb >> 8) & 0xfc;
    blue = argb & 0xfc;
    bytes[0] = alpha | (red >> 6);
    bytes[1] = (red << 2) | (green >> 4);
    bytes[2] = (green << 4) | (blue >> 2);
    

    【讨论】:

    • 看那个,这正如我所愿。我想我会去制作一张桌子来搜索,谢谢。要用一堆图片来测试它。
    猜你喜欢
    • 2014-09-14
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多