123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- using CommonLang.ByteOrder;
- using CommonLang.IO;
- using CommonUI_Unity3D.Impl;
- using System;
- using System.IO;
- using UnityEngine;
- namespace CommonUI_Unity3D.Src.M3Z
- {
-
-
-
- public enum M3ZType
- {
- M3Z_TYPE_PNG = 0x00474e50,
- M3Z_TYPE_JPG = 0x0047504a,
- M3Z_TYPE_BMP = 0x00504d42,
- M3Z_TYPE_PVR4 = 0x34525650,
- M3Z_TYPE_PVR2 = 0x32525650,
- M3Z_TYPE_PVR1 = 0x31525650,
- M3Z_TYPE_PVRA = 0x41525650,
- M3Z_TYPE_PKM1 = 0x314d4b50,
- M3Z_TYPE_PKMA = 0x414d4b50,
- M3Z_TYPE_ATC_E = 0x45435441,
- M3Z_TYPE_ATC_I = 0x49435441,
- M3Z_TYPE_ATC_RGB = 0x33435441,
- M3Z_TYPE_ETC1 = 0x31435445,
- M3Z_TYPE_ETCA = 0x41435445,
- M3Z_TYPE_RGBA8 = 0x41424752,
- M3Z_TYPE_A8 = 0x00003841,
- M3Z_TYPE_DXT1 = 0x31545844,
- M3Z_TYPE_DXT3 = 0x33545844,
- M3Z_TYPE_DXT5 = 0x35545844,
- }
- public class M3ZHeader
- {
- public const uint M3Z_HEADER = 0x5a33464d;
-
- public uint header { get; private set; }
-
- public uint version { get; private set; }
-
- public int srcWidth { get; private set; }
-
- public int srcHeight { get; private set; }
-
- public bool srcHasAlpha { get; private set; }
-
- public string extUTFData { get; private set; }
-
- public int trunkCount { get; private set; }
-
- public M3ZTrunk[] trunks { get; private set; }
- public M3ZHeader(Stream data)
- {
- this.header = LittleEdian.GetU32(data);
- if (header != M3Z_HEADER)
- {
- throw new Exception("Invalid M3Z data");
- }
- this.version = LittleEdian.GetU32(data);
- const uint v_0100 = 0x00000100;
- if (version == v_0100)
- {
- srcWidth = LittleEdian.GetS32(data);
- srcHeight = LittleEdian.GetS32(data);
- srcHasAlpha = LittleEdian.GetBool(data);
- extUTFData = LittleEdian.GetUTF(data);
- trunkCount = LittleEdian.GetS32(data);
- trunks = new M3ZTrunk[trunkCount];
- for (int i = 0; i < trunkCount; i++)
- {
- trunks[i] = new M3ZTrunk(data, version);
- }
- }
- else
- {
-
- srcWidth = LittleEdian.GetS32(data);
- srcHeight = LittleEdian.GetS32(data);
- srcHasAlpha = LittleEdian.GetBool(data);
- trunkCount = LittleEdian.GetS32(data);
- trunks = new M3ZTrunk[trunkCount];
- for (int i = 0; i < trunkCount; i++)
- {
- trunks[i] = new M3ZTrunk(data, version);
- }
- }
- }
- }
- public class M3ZTrunk
- {
-
-
-
- public M3ZType type { get; private set; }
- public uint flags { get; private set; }
-
-
-
- public bool hasAlpha { get; private set; }
-
-
-
- public int pixelW { get; private set; }
-
-
-
- public int pixelH { get; private set; }
-
-
-
- public int realPixelW { get; private set; }
-
-
-
- public int realPixelH { get; private set; }
-
-
-
- public string extUTFData { get; private set; }
-
-
-
- public int fileSize { get; private set; }
- public ICompressTextureData texData { get; private set; }
- public M3ZTrunk(Stream data, uint version)
- {
- const uint v_0100 = 0x00000100;
- if (version == v_0100)
- {
- this.type = (M3ZType)LittleEdian.GetU32(data);
- this.hasAlpha = LittleEdian.GetBool(data);
- this.pixelW = LittleEdian.GetS32(data);
- this.pixelH = LittleEdian.GetS32(data);
- this.realPixelW = LittleEdian.GetS32(data);
- this.realPixelH = LittleEdian.GetS32(data);
- this.extUTFData = LittleEdian.GetUTF(data);
- this.fileSize = LittleEdian.GetS32(data);
- }
- else
- {
- this.type = (M3ZType)LittleEdian.GetU32(data);
- this.flags = LittleEdian.GetU32(data);
- this.hasAlpha = LittleEdian.GetBool(data);
- this.pixelW = LittleEdian.GetS32(data);
- this.pixelH = LittleEdian.GetS32(data);
- this.realPixelW = pixelW;
- this.realPixelH = pixelH;
- this.fileSize = LittleEdian.GetS32(data);
- }
- switch (type)
- {
- case M3ZType.M3Z_TYPE_PVR2:
- case M3ZType.M3Z_TYPE_PVR4:
- case M3ZType.M3Z_TYPE_PVR1:
- case M3ZType.M3Z_TYPE_PVRA:
- this.texData = new PVRTexHeader();
- break;
- case M3ZType.M3Z_TYPE_ETC1:
- case M3ZType.M3Z_TYPE_ETCA:
- this.texData = new KTXTexHeader();
- break;
- case M3ZType.M3Z_TYPE_PKM1:
- case M3ZType.M3Z_TYPE_PKMA:
- this.texData = new PKMTexHeader();
- break;
- default:
- this.texData = new UnknowTexHeader();
- break;
- }
- if (texData != null)
- {
- texData.Decode(this, data);
- }
- }
- public Texture2D LoadRawTextureData()
- {
- if (texData != null)
- {
- return texData.LoadRawTextureData(this);
- }
- return null;
- }
- public static TextureFormat GetTextureFormat(M3ZType mtype)
- {
- switch (mtype)
- {
- case M3ZType.M3Z_TYPE_PNG:
- case M3ZType.M3Z_TYPE_JPG:
- case M3ZType.M3Z_TYPE_BMP:
- return TextureFormat.RGBA32;
- case M3ZType.M3Z_TYPE_PVR2:
- return TextureFormat.PVRTC_RGBA2;
- case M3ZType.M3Z_TYPE_PVR4:
- case M3ZType.M3Z_TYPE_PVR1:
- case M3ZType.M3Z_TYPE_PVRA:
- return TextureFormat.PVRTC_RGBA4;
- case M3ZType.M3Z_TYPE_PKM1:
- case M3ZType.M3Z_TYPE_PKMA:
- return TextureFormat.ETC_RGB4;
- case M3ZType.M3Z_TYPE_ATC_E:
- case M3ZType.M3Z_TYPE_ATC_I:
- case M3ZType.M3Z_TYPE_ATC_RGB:
- return TextureFormat.ATC_RGB4;
- case M3ZType.M3Z_TYPE_ETC1:
- case M3ZType.M3Z_TYPE_ETCA:
- return TextureFormat.ETC_RGB4;
- case M3ZType.M3Z_TYPE_RGBA8:
- return TextureFormat.RGBA32;
- case M3ZType.M3Z_TYPE_A8:
- return TextureFormat.Alpha8;
- case M3ZType.M3Z_TYPE_DXT1:
- return TextureFormat.DXT1;
- case M3ZType.M3Z_TYPE_DXT3:
- return TextureFormat.DXT5;
- case M3ZType.M3Z_TYPE_DXT5:
- return TextureFormat.DXT5;
- }
- return TextureFormat.RGBA32;
- }
- }
- public interface ICompressTextureData
- {
- byte[] RawData { get; }
- void Decode(M3ZTrunk trunk, Stream stream);
- void Encode(Stream stream);
- Texture2D LoadRawTextureData(M3ZTrunk trunk);
- }
- public struct UnknowTexHeader : ICompressTextureData
- {
- public byte[] rawData;
- public byte[] RawData { get { return rawData; } }
- public void Decode(M3ZTrunk trunk, Stream stream)
- {
- this.rawData = new byte[trunk.fileSize];
- IOUtil.ReadToEnd(stream, rawData, 0, rawData.Length);
- }
- public void Encode(Stream stream)
- {
- IOUtil.WriteToEnd(stream, rawData, 0, rawData.Length);
- }
- public Texture2D LoadRawTextureData(M3ZTrunk trunk)
- {
- Texture2D tex = new Texture2D(trunk.pixelW, trunk.pixelH, M3ZTrunk.GetTextureFormat(trunk.type), false, true);
- tex.LoadRawTextureData(rawData);
- tex.Apply(false, true);
- return tex;
- }
- }
- public struct PVRTexHeader : ICompressTextureData
- {
- public const int PVR_HEADER_SIZE = 52;
- public uint headerLength;
- public uint height;
- public uint width;
- public uint numMipmaps;
- public uint flags;
- public uint dataLength;
- public uint bpp;
- public uint bitmaskRed;
- public uint bitmaskGreen;
- public uint bitmaskBlue;
- public uint bitmaskAlpha;
- public uint pvrTag;
- public uint numSurfs;
- public byte[] rawData;
- public byte[] RawData { get { return rawData; } }
- public void Decode(M3ZTrunk trunk, Stream stream)
- {
- this.headerLength = LittleEdian.GetU32(stream);
- this.height = LittleEdian.GetU32(stream);
- this.width = LittleEdian.GetU32(stream);
- this.numMipmaps = LittleEdian.GetU32(stream);
- this.flags = LittleEdian.GetU32(stream);
- this.dataLength = LittleEdian.GetU32(stream);
- this.bpp = LittleEdian.GetU32(stream);
- this.bitmaskRed = LittleEdian.GetU32(stream);
- this.bitmaskGreen = LittleEdian.GetU32(stream);
- this.bitmaskBlue = LittleEdian.GetU32(stream);
- this.bitmaskAlpha = LittleEdian.GetU32(stream);
- this.pvrTag = LittleEdian.GetU32(stream);
- this.numSurfs = LittleEdian.GetU32(stream);
- this.rawData = new byte[dataLength];
- IOUtil.ReadToEnd(stream, rawData, 0, rawData.Length);
- }
- public void Encode(Stream stream)
- {
- LittleEdian.PutU32(stream, this.headerLength);
- LittleEdian.PutU32(stream, this.height);
- LittleEdian.PutU32(stream, this.width);
- LittleEdian.PutU32(stream, this.numMipmaps);
- LittleEdian.PutU32(stream, this.flags);
- LittleEdian.PutU32(stream, this.dataLength);
- LittleEdian.PutU32(stream, this.bpp);
- LittleEdian.PutU32(stream, this.bitmaskRed);
- LittleEdian.PutU32(stream, this.bitmaskGreen);
- LittleEdian.PutU32(stream, this.bitmaskBlue);
- LittleEdian.PutU32(stream, this.bitmaskAlpha);
- LittleEdian.PutU32(stream, this.pvrTag);
- LittleEdian.PutU32(stream, this.numSurfs);
- IOUtil.WriteToEnd(stream, rawData, 0, rawData.Length);
- }
- public Texture2D LoadRawTextureData(M3ZTrunk trunk)
- {
- Texture2D tex = new Texture2D(trunk.pixelW, trunk.pixelH, M3ZTrunk.GetTextureFormat(trunk.type), false, true);
- tex.LoadRawTextureData(rawData);
- tex.Apply(false, true);
- return tex;
- }
- }
- public struct PKMTexHeader : ICompressTextureData
- {
- public const int PKM_HEADER_SIZE = 16;
- public byte[] MagicNumber;
- public byte[] Version;
- public byte[] DataType;
- public ushort ExtWidth;
- public ushort ExtHeight;
- public ushort SrcWidth;
- public ushort SrcHeight;
- public byte[] rawData;
- public byte[] RawData { get { return rawData; } }
- public void Decode(M3ZTrunk trunk, Stream stream)
- {
- this.MagicNumber = IOUtil.ReadExpect(stream, 4);
- this.Version = IOUtil.ReadExpect(stream, 2);
- this.DataType = IOUtil.ReadExpect(stream, 2);
- this.ExtWidth = BigEdian.GetU16(stream);
- this.ExtHeight = BigEdian.GetU16(stream);
- this.SrcWidth = BigEdian.GetU16(stream);
- this.SrcHeight = BigEdian.GetU16(stream);
- this.rawData = new byte[trunk.fileSize - PKM_HEADER_SIZE];
- IOUtil.ReadToEnd(stream, rawData, 0, rawData.Length);
- }
- public void Encode(Stream stream)
- {
- IOUtil.WriteToEnd(stream, this.MagicNumber, 0, 4);
- IOUtil.WriteToEnd(stream, this.Version, 0, 2);
- IOUtil.WriteToEnd(stream, this.DataType, 0, 2);
- BigEdian.PutU16(stream, this.ExtWidth);
- BigEdian.PutU16(stream, this.ExtHeight);
- BigEdian.PutU16(stream, this.SrcWidth);
- BigEdian.PutU16(stream, this.SrcHeight);
- IOUtil.WriteToEnd(stream, rawData, 0, rawData.Length);
- }
- public Texture2D LoadRawTextureData(M3ZTrunk trunk)
- {
- Texture2D tex = new Texture2D(trunk.pixelW, trunk.pixelH, M3ZTrunk.GetTextureFormat(trunk.type), false, true);
- tex.LoadRawTextureData(rawData);
- tex.Apply(false, true);
- return tex;
- }
- }
- public struct KTXTexHeader : ICompressTextureData
- {
- public const int KTX_HEADER_SIZE = 52 + 12;
- public static byte[] KTX_IDENTIFIER_REF = { 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A };
- public byte[] identifier;
- public uint endianness;
- public uint glType;
- public uint glTypeSize;
- public uint glFormat;
- public uint glInternalFormat;
- public uint glBaseInternalFormat;
- public uint pixelWidth;
- public uint pixelHeight;
- public uint pixelDepth;
- public uint numberOfArrayElements;
- public uint numberOfFaces;
- public uint numberOfMipmapLevels;
- public uint bytesOfKeyValueData;
- public byte[] rawData;
- public byte[] RawData { get { return rawData; } }
- public void Decode(M3ZTrunk trunk, Stream stream)
- {
- this.identifier = IOUtil.ReadExpect(stream, 12);
- this.endianness = LittleEdian.GetU32(stream);
- this.glType = LittleEdian.GetU32(stream);
- this.glTypeSize = LittleEdian.GetU32(stream);
- this.glFormat = LittleEdian.GetU32(stream);
- this.glInternalFormat = LittleEdian.GetU32(stream);
- this.glBaseInternalFormat = LittleEdian.GetU32(stream);
- this.pixelWidth = LittleEdian.GetU32(stream);
- this.pixelHeight = LittleEdian.GetU32(stream);
- this.pixelDepth = LittleEdian.GetU32(stream);
- this.numberOfArrayElements = LittleEdian.GetU32(stream);
- this.numberOfFaces = LittleEdian.GetU32(stream);
- this.numberOfMipmapLevels = LittleEdian.GetU32(stream);
- this.bytesOfKeyValueData = LittleEdian.GetU32(stream);
- stream.Position += this.bytesOfKeyValueData;
- uint imageSize = LittleEdian.GetU32(stream);
- this.rawData = new byte[imageSize];
- IOUtil.ReadToEnd(stream, rawData, 0, rawData.Length);
- }
- public void Encode(Stream stream)
- {
- IOUtil.WriteToEnd(stream, this.identifier, 0, 12);
- LittleEdian.PutU32(stream, endianness);
- LittleEdian.PutU32(stream, glType);
- LittleEdian.PutU32(stream, glTypeSize);
- LittleEdian.PutU32(stream, glFormat);
- LittleEdian.PutU32(stream, glInternalFormat);
- LittleEdian.PutU32(stream, glBaseInternalFormat);
- LittleEdian.PutU32(stream, pixelWidth);
- LittleEdian.PutU32(stream, pixelHeight);
- LittleEdian.PutU32(stream, pixelDepth);
- LittleEdian.PutU32(stream, numberOfArrayElements);
- LittleEdian.PutU32(stream, numberOfFaces);
- LittleEdian.PutU32(stream, numberOfMipmapLevels);
- LittleEdian.PutU32(stream, bytesOfKeyValueData);
- LittleEdian.PutU32(stream, (uint)rawData.Length);
- IOUtil.WriteToEnd(stream, rawData, 0, rawData.Length);
- }
- public Texture2D LoadRawTextureData(M3ZTrunk trunk)
- {
- Texture2D tex = new Texture2D(trunk.pixelW, trunk.pixelH, M3ZTrunk.GetTextureFormat(trunk.type), false, true);
- tex.LoadRawTextureData(rawData);
- tex.Apply(false, true);
- return tex;
- }
- }
- public static class G3ZStream
- {
- public static Stream DecompressToStream(byte[] data)
- {
- int pos = 0;
- int Head = (int)LittleEdian.GetU32(data, ref pos);
- int SrcSize = (int)LittleEdian.GetU32(data, ref pos);
- byte[] dst = new byte[SrcSize];
- UnityDriver.Platform.NativeDecompressMemory(
- new ArraySegment<byte>(data, pos, data.Length - pos),
- new ArraySegment<byte>(dst, 0, dst.Length));
- return new MemoryStream(dst);
- }
- }
- }
|