public static byte[] LZ77_Compress(byte[] data, bool header = false) { ByteArrayOutputStream res = new ByteArrayOutputStream(); if (header) { res.writeUInt(0x37375A4C); //LZ77 } res.writeInt((data.Length << 8) | 0x10); byte[] tempBuffer = new byte[16]; //Current byte to compress. int current = 0; while (current < data.Length) { int tempBufferCursor = 0; byte blockFlags = 0; for (int i = 0; i < 8; i++) { //Not sure if this is needed. The DS probably ignores this data. if (current >= data.Length) { tempBuffer[tempBufferCursor++] = 0; continue; } int searchPos = 0; int searchLen = 0; LZ77_Compress_Search(data, current, out searchPos, out searchLen); int searchDisp = current - searchPos - 1; if (searchLen > 2) //We found a big match, let's write a compressed block. { blockFlags |= (byte)(1 << (7 - i)); tempBuffer[tempBufferCursor++] = (byte)((((searchLen - 3) & 0xF) << 4) + ((searchDisp >> 8) & 0xF)); tempBuffer[tempBufferCursor++] = (byte)(searchDisp & 0xFF); current += searchLen; } else { tempBuffer[tempBufferCursor++] = data[current++]; } } res.writeByte(blockFlags); for (int i = 0; i < tempBufferCursor; i++) { res.writeByte(tempBuffer[i]); } } return(res.getArray()); }
private byte[] NCL_PaletteToByteArray(Color[] pal) { ByteArrayOutputStream oo = new ByteArrayOutputStream(); for (int i = 0; i < pal.Length; i++) { Color c = pal[i]; byte r = (byte)(c.R >> 3); byte g = (byte)(c.G >> 3); byte b = (byte)(c.B >> 3); ushort val = 0; val |= r; val |= (ushort)(g << 5); val |= (ushort)(b << 10); oo.writeUShort(val); } return(oo.getArray()); }