private bool LoadTextureFormatDataFromBuffer() { #if NATIVE_PLUGIN_LOAD textureData.width = FastLoad_Texture_GetWidth(); textureData.heght = FastLoad_Texture_GetHeight(); textureData.format = FastLoad_Texture_GetFormat(); textureData.flags = FastLoad_Texture_GetFlags(); textureData.compressedSize = 0; textureData.dataSize = FastLoad_Texture_GetBodySize(); textureData.rawData = FastLoad_Texture_GetRawData(); #else if (!FastLoaderUtil.CheckHeader(this.bufferData, 0, FastLoaderUtil.FastTextureHeader)) { return(false); } textureData.width = FastLoaderUtil.GetIntFromByteArray(this.bufferData, 8); textureData.heght = FastLoaderUtil.GetIntFromByteArray(this.bufferData, 12); textureData.format = FastLoaderUtil.GetIntFromByteArray(this.bufferData, 16); textureData.flags = FastLoaderUtil.GetIntFromByteArray(this.bufferData, 20); textureData.compressedSize = FastLoaderUtil.GetIntFromByteArray(this.bufferData, 24); textureData.dataSize = FastLoaderUtil.GetIntFromByteArray(this.bufferData, 28); if (textureData.compress) { Lz4Util.Decode(this.bufferData, 32, textureData.compressedSize, this.uncompressedBuffer, 0, textureData.dataSize); textureData.rawData = Marshal.UnsafeAddrOfPinnedArrayElement(this.uncompressedBuffer, 0); } else { textureData.rawData = Marshal.UnsafeAddrOfPinnedArrayElement(this.bufferData, 32); } #endif return(true); }
/* * ----------------- * | 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); }
public static int Decode(byte [] src, int srcOffset, int srcLength, byte [] dest, int destOffset, int destLength) { int blockNum = FastLoaderUtil.GetIntFromByteArray(src, srcOffset + 0); int headerSize = 8 + blockNum * 8; int currentDestOffset = destOffset; int currentSrcOffset = srcOffset + headerSize; int destLengthSum = 0; for (int i = 0; i < blockNum; ++i) { int originSize = FastLoaderUtil.GetIntFromByteArray(src, srcOffset + 8 + (i * 8) + 0); int compressedSize = FastLoaderUtil.GetIntFromByteArray(src, srcOffset + 8 + (i * 8) + 4); LZ4Codec.Decode(src, currentSrcOffset, compressedSize, dest, currentDestOffset, originSize); currentSrcOffset += compressedSize; currentDestOffset += originSize; destLengthSum += originSize; } return(destLengthSum); }