diff options
Diffstat (limited to 'UVtools.Core/FileFormats/GR1File.cs')
-rw-r--r-- | UVtools.Core/FileFormats/GR1File.cs | 96 |
1 files changed, 42 insertions, 54 deletions
diff --git a/UVtools.Core/FileFormats/GR1File.cs b/UVtools.Core/FileFormats/GR1File.cs index 808b588..c55167e 100644 --- a/UVtools.Core/FileFormats/GR1File.cs +++ b/UVtools.Core/FileFormats/GR1File.cs @@ -354,9 +354,9 @@ namespace UVtools.Core.FileFormats #endregion #region Methods - protected override void EncodeInternally(string fileFullPath, OperationProgress progress) + protected override void EncodeInternally(OperationProgress progress) { - using var outputFile = new FileStream(fileFullPath, FileMode.Create, FileAccess.Write); + using var outputFile = new FileStream(FileFullPath, FileMode.Create, FileAccess.Write); var pageBreak = PageBreak.Bytes; Helpers.SerializeWriteFileStream(outputFile, HeaderSettings); @@ -465,13 +465,13 @@ namespace UVtools.Core.FileFormats - protected override void DecodeInternally(string fileFullPath, OperationProgress progress) + protected override void DecodeInternally(OperationProgress progress) { - using var inputFile = new FileStream(fileFullPath, FileMode.Open, FileAccess.Read); + using var inputFile = new FileStream(FileFullPath, FileMode.Open, FileAccess.Read); HeaderSettings = Helpers.Deserialize<Header>(inputFile); if (HeaderSettings.HeaderValue != Header.HEADER_VALUE) { - throw new FileLoadException("Not a valid Makerbase file!", fileFullPath); + throw new FileLoadException("Not a valid Makerbase file!", FileFullPath); } byte[][] previews = new byte[ThumbnailsOriginalSize.Length][]; @@ -491,77 +491,65 @@ namespace UVtools.Core.FileFormats SlicerInfoSettings = Helpers.Deserialize<SlicerInfo>(inputFile); - LayerManager.Init(SlicerInfoSettings.LayerCount); + LayerManager.Init(SlicerInfoSettings.LayerCount, DecodeType == FileDecodeType.Partial); - progress.Reset(OperationProgress.StatusDecodeLayers, LayerCount); - - var range = Enumerable.Range(0, (int)LayerCount); - - var linesBytes = new byte[LayerCount][]; - foreach (var batch in BatchLayersIndexes()) + if (DecodeType == FileDecodeType.Full) { - progress.Token.ThrowIfCancellationRequested(); - - foreach (var layerIndex in batch) + progress.Reset(OperationProgress.StatusDecodeLayers, LayerCount); + var linesBytes = new byte[LayerCount][]; + foreach (var batch in BatchLayersIndexes()) { - var lineCount = BitExtensions.ToUIntBigEndian(inputFile.ReadBytes(4)); + progress.Token.ThrowIfCancellationRequested(); - linesBytes[layerIndex] = new byte[lineCount * 6]; - inputFile.ReadBytes(linesBytes[layerIndex]); - inputFile.Seek(2, SeekOrigin.Current); + foreach (var layerIndex in batch) + { + var lineCount = BitExtensions.ToUIntBigEndian(inputFile.ReadBytes(4)); - progress.Token.ThrowIfCancellationRequested(); - } + linesBytes[layerIndex] = new byte[lineCount * 6]; + inputFile.ReadBytes(linesBytes[layerIndex]); + inputFile.Seek(2, SeekOrigin.Current); - Parallel.ForEach(batch, CoreSettings.ParallelOptions, layerIndex => - { - if (progress.Token.IsCancellationRequested) return; - using var mat = EmguExtensions.InitMat(Resolution); + progress.Token.ThrowIfCancellationRequested(); + } - for (int i = 0; i < linesBytes[layerIndex].Length; i++) + Parallel.ForEach(batch, CoreSettings.ParallelOptions, layerIndex => { - var startY = BitExtensions.ToUShortBigEndian(linesBytes[layerIndex][i++], linesBytes[layerIndex][i++]); - var endY = BitExtensions.ToUShortBigEndian(linesBytes[layerIndex][i++], linesBytes[layerIndex][i++]); - var startX = BitExtensions.ToUShortBigEndian(linesBytes[layerIndex][i++], linesBytes[layerIndex][i]); + if (progress.Token.IsCancellationRequested) return; + using var mat = EmguExtensions.InitMat(Resolution); - CvInvoke.Line(mat, new Point(startX, startY), new Point(startX, endY), EmguExtensions.WhiteColor); - } + for (int i = 0; i < linesBytes[layerIndex].Length; i++) + { + var startY = BitExtensions.ToUShortBigEndian(linesBytes[layerIndex][i++], linesBytes[layerIndex][i++]); + var endY = BitExtensions.ToUShortBigEndian(linesBytes[layerIndex][i++], linesBytes[layerIndex][i++]); + var startX = BitExtensions.ToUShortBigEndian(linesBytes[layerIndex][i++], linesBytes[layerIndex][i]); - linesBytes[layerIndex] = null; + CvInvoke.Line(mat, new Point(startX, startY), new Point(startX, endY), + EmguExtensions.WhiteColor); + } - this[layerIndex] = new Layer((uint)layerIndex, mat, this); + linesBytes[layerIndex] = null; - progress.LockAndIncrement(); - }); + this[layerIndex] = new Layer((uint)layerIndex, mat, this); + + progress.LockAndIncrement(); + }); + } + } + else // Partial read + { + inputFile.Seek(-Helpers.Serializer.SizeOf(HeaderSettings), SeekOrigin.End); } HeaderSettings = Helpers.Deserialize<Header>(inputFile); if (HeaderSettings.HeaderValue != Header.HEADER_VALUE) { - throw new FileLoadException("Not a valid Makerbase file!", fileFullPath); + throw new FileLoadException("Not a valid Makerbase file!", FileFullPath); } } - public override void SaveAs(string filePath = null, OperationProgress progress = null) + protected override void PartialSaveInternally(OperationProgress progress) { - if (RequireFullEncode) - { - if (!string.IsNullOrEmpty(filePath)) - { - FileFullPath = filePath; - } - Encode(FileFullPath, progress); - return; - } - - - if (!string.IsNullOrEmpty(filePath)) - { - File.Copy(FileFullPath, filePath, true); - FileFullPath = filePath; - } - using var outputFile = new FileStream(FileFullPath, FileMode.Open, FileAccess.Write); outputFile.Seek(SlicerInfoAddress, SeekOrigin.Begin); Helpers.SerializeWriteFileStream(outputFile, SlicerInfoSettings); |