From b6960a643850c6d72d2d3d385ff9a991dee47db8 Mon Sep 17 00:00:00 2001 From: Bezaleel Olakunori Date: Sun, 14 Jul 2024 10:21:36 +0200 Subject: [PATCH] Create a decoder with support for parsing the Draco header --- src/Draco/IO/DracoDecoder.cs | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/Draco/IO/DracoDecoder.cs diff --git a/src/Draco/IO/DracoDecoder.cs b/src/Draco/IO/DracoDecoder.cs new file mode 100644 index 0000000..8b1ced1 --- /dev/null +++ b/src/Draco/IO/DracoDecoder.cs @@ -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 + ); + } +}