Skip to content

Commit

Permalink
Create a decoder with support for parsing the Draco header
Browse files Browse the repository at this point in the history
  • Loading branch information
B3zaleel committed Jul 14, 2024
1 parent cec1284 commit b6960a6
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Draco/IO/DracoDecoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace Draco.IO;

public class DracoDecoder
{
public DracoHeader? Header { get; private set; }

public void Decode(string path)
{
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
Decode(new BinaryReader(fs));
}

public void Decode(Stream stream)
{
Decode(new BinaryReader(stream));
}

public void Decode(BinaryReader binaryReader)
{
using var buffer = new DecoderBuffer(binaryReader);
Header = ParseHeader(buffer);
buffer.BitStream_Version = Header.Version;
}

private static DracoHeader ParseHeader(DecoderBuffer buffer)
{
var dracoMagic = buffer.ReadASCIIBytes(Constants.DracoMagic.Length);
if (dracoMagic != Constants.DracoMagic)
{
throw new InvalidDataException("Invalid Draco file.");
}
var majorVersion = buffer.ReadByte();
var minorVersion = buffer.ReadByte();
var encoderType = buffer.ReadByte();
var encoderMethod = buffer.ReadByte();
var flags = buffer.ReadUInt16();

return new DracoHeader(
majorVersion: majorVersion,
minorVersion: minorVersion,
encoderType: encoderType,
encoderMethod: encoderMethod,
flags: flags
);
}
}

0 comments on commit b6960a6

Please sign in to comment.