Skip to content

Instantly share code, notes, and snippets.

@takeshich
Last active October 17, 2018 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takeshich/0c0b1496bbd5ea2b77ca46dc10b1ee73 to your computer and use it in GitHub Desktop.
Save takeshich/0c0b1496bbd5ea2b77ca46dc10b1ee73 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace Mono.Samples.TexturedCube
{
class KTX
{
public struct KTXHeader
{
public Byte[] identifier;
public Byte[] endianness;
public UInt32 glType;
public UInt32 glTypeSize;
public UInt32 glFormat;
public UInt32 glInternalFormat;
public UInt32 glBaseInternalFormat;
public UInt32 pixelWidth;
public UInt32 pixelHeight;
public UInt32 pixelDepth;
public UInt32 numberOfArrayElements;
public UInt32 numberOfFaces;
public UInt32 numberOfMipmapLevels;
public UInt32 bytesOfKeyValueData;
}
public struct KTXMetadata
{
public UInt32? keyAndValueByteSize;
public Byte[] keyAndValue;
public Byte[] valuePadding;
}
public static byte[] Load(Stream rawdata, out KTXHeader ktxheader,out KTXMetadata ktxmetadata)
{
byte[] imagedata;
using (var ms = new MemoryStream())
{
rawdata.CopyTo(ms);
int Length = ms.ToArray().Length;
ms.Seek(0, SeekOrigin.Begin);
//ヘッダの読み込み
//イメージデータの取得
using (var br = new BinaryReader(ms))
{
ktxheader.identifier = br.ReadBytes(12);
ktxheader.endianness = br.ReadBytes(4);
ktxheader.glType = br.ReadUInt32();
ktxheader.glTypeSize = br.ReadUInt32();
ktxheader.glFormat = br.ReadUInt32();
ktxheader.glInternalFormat = br.ReadUInt32();
ktxheader.glBaseInternalFormat = br.ReadUInt32();
ktxheader.pixelWidth = br.ReadUInt32();
ktxheader.pixelHeight = br.ReadUInt32();
ktxheader.pixelDepth = br.ReadUInt32();
ktxheader.numberOfArrayElements = br.ReadUInt32();
ktxheader.numberOfFaces = br.ReadUInt32();
ktxheader.numberOfMipmapLevels = br.ReadUInt32();
ktxheader.bytesOfKeyValueData = br.ReadUInt32();
if (ktxheader.bytesOfKeyValueData != 0)
{
ktxmetadata.keyAndValueByteSize = br.ReadUInt32();
ktxmetadata.keyAndValue = br.ReadBytes((int)ktxmetadata.keyAndValueByteSize);
ktxmetadata.valuePadding = br.ReadBytes(3 - (((int)ktxmetadata.keyAndValueByteSize + 3) % 4));
}
else
{
ktxmetadata.keyAndValueByteSize = null;
ktxmetadata.keyAndValue = null;
ktxmetadata.valuePadding = null;
}
//headersize(64) +bytesOfKeyValueData
int datasize = Length - 64 + (int)ktxheader.bytesOfKeyValueData;
imagedata = new byte[datasize];
imagedata = br.ReadBytes(datasize);
}
}
return imagedata;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment