/* * ----------------- * | 0 | 4 | blockNum * | 4 | 4 | 0 * --------------- * | 8 + N*8 + 0 | 4 | uncompressedSize * | 8 + N*8 + 4 | 4 | compressedSize * --------------- * blockData * --------------- */ public static int Encode(int blockSize, byte [] src, int srcOffset, int srcLength, byte [] dest, int destOffset, int destLength) { int blockNum = (src.Length + blockSize - 1) / blockSize; int headerSize = 8 + blockNum * 8; FastLoaderUtil.GetByteArrayFromInt(blockNum, dest, destOffset + 0); FastLoaderUtil.GetByteArrayFromInt(0, dest, destOffset + 4); int sizeSum = headerSize; int currentDestOffset = destOffset + headerSize; for (int i = 0; i < blockNum; ++i) { // write to data int originSize = blockSize; if (blockSize + (i * blockSize) > srcLength) { originSize = srcLength - (i * blockSize); } int compressedSize = LZ4Codec.Encode(src, srcOffset + (i * blockSize), originSize, dest, currentDestOffset, destLength - currentDestOffset + destOffset); // write to header block FastLoaderUtil.GetByteArrayFromInt(originSize, dest, destOffset + 8 + (i * 8) + 0); FastLoaderUtil.GetByteArrayFromInt(compressedSize, dest, destOffset + 8 + (i * 8) + 4); currentDestOffset += compressedSize; sizeSum += compressedSize; } return(sizeSum); }
private void WriteTextureToStream(Stream stream, Texture2D texture, bool isCompress, int blockSize) { byte[] rawData = texture.GetRawTextureData(); byte[] writeData = null; int writeDataSize = 0; if (isCompress) { writeData = this.lz4Buffer; writeDataSize = Lz4Util.Encode(blockSize, rawData, 0, rawData.Length, writeData, 0, writeData.Length); Debug.Log("Write " + rawData.Length + "->" + writeDataSize); } else { writeData = rawData; writeDataSize = rawData.Length; } byte[] fileData = new byte[writeDataSize + 32]; int flag = TextureLoaderUtil.GetFlagInt(isCompress, (texture.mipmapCount > 1), (texture.filterMode != FilterMode.Point)); // write header System.Array.Copy(FastLoaderUtil.FastTextureHeader, 0, fileData, 0, FastLoaderUtil.FastTextureHeader.Length); FastLoaderUtil.GetByteArrayFromInt(texture.width, fileData, 8); FastLoaderUtil.GetByteArrayFromInt(texture.height, fileData, 12); FastLoaderUtil.GetByteArrayFromInt((int)texture.format, fileData, 16); FastLoaderUtil.GetByteArrayFromInt(flag, fileData, 20); FastLoaderUtil.GetByteArrayFromInt(writeDataSize, fileData, 24); FastLoaderUtil.GetByteArrayFromInt(rawData.Length, fileData, 28); System.Array.Copy(writeData, 0, fileData, 32, writeDataSize); stream.Write(fileData, 0, fileData.Length); // calc crc32 byte[] crc32 = new byte[4]; FastLoaderUtil.GetByteArrayFromInt(Crc32.GetValue(fileData, 0, fileData.Length), crc32, 0); stream.Write(crc32, 0, crc32.Length); Debug.Log("format " + texture.format + "::" + (int)texture.format); }