public static RawTextureInfo Read(byte[] data) { using (var ms = new MemoryStream(data)) { using (var br = new BinaryReader(ms)) { DDSHeaderStruct header = new DDSHeaderStruct(); if (ReadHeader(br, ref header)) { RawTextureInfo info = new RawTextureInfo(); info.Width = (int)header.width; info.Height = (int)header.height; info.HasMips = header.mipmapcount > 1; switch (header.pixelformat.fourcc) { case FOURCC_NO_COMPRESSION: if (header.pixelformat.rgbbitcount == 8) { info.Format = TextureFormat.Alpha8; } else if (header.pixelformat.rgbbitcount == 32) { info.Format = TextureFormat.RGBA32; } else { Debug.LogError("Invalid texture format fourcc = " + header.pixelformat.fourcc + " rgbbitcount = " + header.pixelformat.rgbbitcount); } break; case FOURCC_DXT1: info.Format = TextureFormat.DXT1; break; case FOURCC_DXT5: info.Format = TextureFormat.DXT5; break; default: Debug.LogError("Unrecognized texture format fourcc = " + header.pixelformat.fourcc); break; } info.RawData = new byte[ms.Length - ms.Position]; ms.Read(info.RawData, 0, info.RawData.Length); return(info); } } } return(null); }
private static bool ReadHeader(BinaryReader reader, ref DDSHeaderStruct header) { byte[] signature = reader.ReadBytes(4); if (!(signature[0] == 'D' && signature[1] == 'D' && signature[2] == 'S' && signature[3] == ' ')) { return(false); } header.size = reader.ReadUInt32(); if (header.size != 124) { return(false); } //convert the data header.flags = reader.ReadUInt32(); header.height = reader.ReadUInt32(); header.width = reader.ReadUInt32(); header.sizeorpitch = reader.ReadUInt32(); header.depth = reader.ReadUInt32(); header.mipmapcount = reader.ReadUInt32(); header.alphabitdepth = reader.ReadUInt32(); header.reserved = new uint[10]; for (int i = 0; i < 10; i++) { header.reserved[i] = reader.ReadUInt32(); } //pixelfromat header.pixelformat.size = reader.ReadUInt32(); header.pixelformat.flags = reader.ReadUInt32(); header.pixelformat.fourcc = reader.ReadUInt32(); header.pixelformat.rgbbitcount = reader.ReadUInt32(); header.pixelformat.rbitmask = reader.ReadUInt32(); header.pixelformat.gbitmask = reader.ReadUInt32(); header.pixelformat.bbitmask = reader.ReadUInt32(); header.pixelformat.alphabitmask = reader.ReadUInt32(); //caps header.ddscaps.caps1 = reader.ReadUInt32(); header.ddscaps.caps2 = reader.ReadUInt32(); header.ddscaps.caps3 = reader.ReadUInt32(); header.ddscaps.caps4 = reader.ReadUInt32(); header.texturestage = reader.ReadUInt32(); return(true); }