簡體   English   中英

將ARGB編碼/壓縮為3個字節

[英]Encoding/Compressing ARGB to 3 bytes

我將3個字節存儲在一個字節數組中,該字節數組將被解碼為ARGB,因為我有將字節數組解碼為int ARGB的代碼,所以這沒有問題。 問題是如何將int ARGB重新編碼(使用相同的編碼方法,而不僅僅是將RGB值存儲在字節數組中)到3個大小的字節數組中? 我已經為此工作了兩個星期,我認為我確實需要幫助。

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值存儲在字節數組中)? 任何幫助將不勝感激。 謝謝。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM