[英]Encoding/Compressing ARGB to 3 bytes
I got 3 bytes stored in a bytearray which will be decoded into ARGB, there's no problem about that because I have the code for decoding the bytearray into int ARGB. 我将3个字节存储在一个字节数组中,该字节数组将被解码为ARGB,因为我有将字节数组解码为int ARGB的代码,所以这没有问题。 The problem is how do I re-encode(with the same encoding method, not just storing the RGB values in byte array) the int ARGB into the 3 sized bytearray? 问题是如何将int ARGB重新编码(使用相同的编码方法,而不仅仅是将RGB值存储在字节数组中)到3个大小的字节数组中? I've been working for this for 2 weeks and I think I really need help. 我已经为此工作了两个星期,我认为我确实需要帮助。
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
It is decoded to ARGB using this function: 使用以下功能将其解码为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;
}
This is my unfinished code for encoding the int ARGB to bytearray. 这是我将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;
}
I just thought of creating a table from byte[]{0,0,0} to byte[]{(byte)0xFF,(byte)0xFF,(byte)0xFF} and compare the ARGB but I think it would be wasteful. 我只是想创建一个从byte [] {0,0,0}到byte [] {(byte)0xFF,(byte)0xFF,(byte)0xFF}的表并比较ARGB,但我认为这很浪费。 Again, repeating the question, how do I re-encode(with the same encoding method, not just storing the RGB values in byte array) the int ARGB into the 3 sized bytearray? 再次重复这个问题,我如何将int ARGB重新编码(使用相同的编码方法,而不仅仅是将RGB值存储在字节数组中)? Any help will be greatly appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.