public static DwgDrawing Load(byte[] data) { var reader = new BitReader(data); var drawing = new DwgDrawing(); drawing.FileHeader = DwgFileHeader.Parse(reader); drawing.Variables = DwgHeaderVariables.Parse(reader.FromOffset(drawing.FileHeader.HeaderVariablesLocator.Pointer), drawing.FileHeader.Version); drawing.Classes = DwgClasses.Parse(reader.FromOffset(drawing.FileHeader.ClassSectionLocator.Pointer), drawing.FileHeader.Version); // don't read the R13C3 and later unknown section drawing.FileHeader.ValidateSecondHeader(reader, drawing.Variables); drawing.ImageData = DwgImageData.Parse(reader.FromOffset(drawing.FileHeader.ImagePointer)); var objectCache = DwgObjectCache.Parse(reader.FromOffset(drawing.FileHeader.ObjectMapLocator.Pointer), drawing.FileHeader.Version); drawing.LoadObjects(reader, objectCache); return(drawing); }
internal static DwgImageData Parse(BitReader reader) { var imageData = new DwgImageData(); reader.ValidateSentinel(StartSentinel); var overallSize = reader.Read_RL(); var startOffset = reader.Offset; var imageCount = reader.Read_RC(); var headerStart = 0; var headerSize = 0; var bmpStart = 0; var bmpSize = 0; var wmfStart = 0; var wmfSize = 0; for (int i = 0; i < imageCount; i++) { var code = reader.Read_RC(); var start = reader.Read_RL(); var size = reader.Read_RL(); switch (code) { case 1: headerStart = start; headerSize = size; break; case 2: bmpStart = start; bmpSize = size; break; case 3: wmfStart = start; wmfSize = size; break; } } if (headerSize > 0) { if (reader.Offset != headerStart) { throw new DwgReadException("Unexpected image data header start."); } imageData.HeaderData = reader.ReadBytes(headerSize); } if (bmpSize > 0) { if (reader.Offset != bmpStart) { throw new DwgReadException("Unexpected BMP data start."); } imageData.BmpData = reader.ReadBytes(bmpSize); } if (wmfSize > 0) { if (reader.Offset != wmfStart) { throw new DwgReadException("Unexpected WMF data start."); } imageData.WmfData = reader.ReadBytes(wmfSize); } var endOffset = reader.Offset; var readBytes = endOffset - startOffset; if (readBytes != overallSize) { throw new DwgReadException($"Invalid count of read image data bytes. Expected: {overallSize}, Actual: {readBytes}"); } reader.ValidateSentinel(EndSentinel); return(imageData); }