mirror of
https://github.com/riegera2412/UVtools.git
synced 2026-07-09 01:52:32 +02:00
57b8a9dc84
- **Core:**
- (Add) Machine presets and able to load machine collection from PrusaSlicer
- (Improvement) Core: Reference EmguCV runtimes into core instead of the UI project
- **File formats:**
- **CXDLP:**
- (Add) Detection support for Halot One Pro
- (Add) Detection support for Halot One Plus
- (Add) Detection support for Halot Sky Plus
- (Add) Detection support for Halot Lite
- (Improvement) Better handling and detection of printer model when converting
- (Improvement) Discovered more fields meanings on format
- (Fix) Exposure time in format is `round(time * 10, 1)`
- (Fix) Speeds in format are in mm/s, was using mm/min before
- (Add) JXS format for Uniformation GKone [Zip+GCode]
- (Improvement) Saving and converting files now handle the file backup on Core instead on the UI, which prevents scripts and other projects lose the original file in case of error while saving
- (Fix) After load files they was flagged as requiring a full encode, preventing fast save a fresh file
- **UVtoolsCmd:**
- Bring back the commandline project
- Consult README to see the available commands and syntax
- Old terminal commands on UVtools still works for now, but consider switch to UVtoolsCmd or redirect the command using `UVtools --cmd "commands"`
- **Tools:**
- **Change print resolution:**
- (Add) Allow to change the display size to match the new printer
- (Add) Machine presets to help set both resolution and display size to a correct printer and auto set fix pixel ratio
- (Improvement) Real pixel pitch fixer due new display size information, this allow full transfers between different printers "without" invalidating the model size
- (Improvement) Better arrangement of the layout
- (Add) Infill: Option "Reinforce infill if possible", it was always on before, now default is off and configurable
- (Improvement) Always allow to export settings from tools
- **GCode:**
- (Improvement) After print the last layer, do one lift with the same layer settings before attempt a fast move to top
- (Improvement) Use the highest defined speed to send the build plate to top after finish print
- (Improvement) Append a wait sync command in the end of gcode if needed
- (Fix) When lift without a retract it still output the motor sync delay for the retract time and the wait time after retract
- **PrusaSlicer:**
- (Add) Printer: Creality Halot One Pro CL-70
- (Add) Printer: Creality Halot One Plus CL-79
- (Add) Printer: Creality Halot Sky Plus CL-92
- (Add) Printer: Creality Halot Lite CL-89L
- (Add) Printer: Creality Halot Lite CL-89L
- (Add) Printer: Creality CT133 Pro
- (Add) Printer: Creality CT-005 Pro
- (Add) Printer: Uniformation GKone
- (Add) Printer: FlashForge Foto 8.9S
- (Add) Printer: Elegoo Mars 2
- (Improvement) Rename all Creality printers
- (Fix) Creality model in print notes
730 lines
25 KiB
C#
730 lines
25 KiB
C#
/*
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE
|
|
* Version 3, 19 November 2007
|
|
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
* Everyone is permitted to copy and distribute verbatim copies
|
|
* of this license document, but changing it is not allowed.
|
|
*/
|
|
|
|
using BinarySerialization;
|
|
using Emgu.CV;
|
|
using Emgu.CV.Structure;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Drawing;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using UVtools.Core.Extensions;
|
|
using UVtools.Core.Layers;
|
|
using UVtools.Core.Operations;
|
|
|
|
namespace UVtools.Core.FileFormats;
|
|
|
|
public class CXDLPv1File : FileFormat
|
|
{
|
|
#region Constants
|
|
private const byte HEADER_SIZE = 9; // CXSW3DV2
|
|
private const string HEADER_VALUE = "CXSW3DV2";
|
|
#endregion
|
|
|
|
#region Sub Classes
|
|
#region Header
|
|
public sealed class Header
|
|
{
|
|
/// <summary>
|
|
/// Gets the size of the header
|
|
/// </summary>
|
|
[FieldOrder(0)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public uint HeaderSize { get; set; } = HEADER_SIZE;
|
|
|
|
/// <summary>
|
|
/// Gets the header name
|
|
/// </summary>
|
|
[FieldOrder(1)]
|
|
[FieldLength(HEADER_SIZE)]
|
|
[SerializeAs(SerializedType.TerminatedString)]
|
|
public string HeaderValue { get; set; } = HEADER_VALUE;
|
|
|
|
/// <summary>
|
|
/// Gets the number of records in the layer table
|
|
/// </summary>
|
|
[FieldOrder(2)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public ushort LayerCount { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets the printer resolution along X axis, in pixels. This information is critical to correctly decoding layer images.
|
|
/// </summary>
|
|
[FieldOrder(3)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public ushort ResolutionX { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets the printer resolution along Y axis, in pixels. This information is critical to correctly decoding layer images.
|
|
/// </summary>
|
|
[FieldOrder(4)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public ushort ResolutionY { get; set; }
|
|
|
|
public void Validate()
|
|
{
|
|
if (HeaderSize != HEADER_SIZE || HeaderValue != HEADER_VALUE)
|
|
{
|
|
throw new FileLoadException("Not a valid CXDLP file!");
|
|
}
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"{nameof(HeaderSize)}: {HeaderSize}, {nameof(HeaderValue)}: {HeaderValue}, {nameof(LayerCount)}: {LayerCount}, {nameof(ResolutionX)}: {ResolutionX}, {nameof(ResolutionY)}: {ResolutionY}";
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region SlicerInfo
|
|
// Address: 363407
|
|
public sealed class SlicerInfo
|
|
{
|
|
[FieldOrder(0)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public uint DisplayWidthDataSize { get; set; } = 20;
|
|
|
|
[FieldOrder(1)]
|
|
[FieldLength(nameof(DisplayWidthDataSize))]
|
|
public byte[] DisplayWidthBytes { get; set; } = null!;
|
|
|
|
[FieldOrder(2)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public uint DisplayHeightDataSize { get; set; } = 20;
|
|
|
|
[FieldOrder(3)]
|
|
[FieldLength(nameof(DisplayHeightDataSize))]
|
|
public byte[] DisplayHeightBytes { get; set; } = null!;
|
|
|
|
[FieldOrder(4)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public uint LayerHeightDataSize { get; set; } = 16;
|
|
|
|
[FieldOrder(5)]
|
|
[FieldLength(nameof(LayerHeightDataSize))]
|
|
public byte[] LayerHeightBytes { get; set; } = null!;
|
|
|
|
[FieldOrder(6)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public ushort WaitTimeBeforeCure { get; set; }
|
|
|
|
[FieldOrder(7)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public ushort ExposureTime { get; set; }
|
|
|
|
[FieldOrder(8)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public ushort BottomExposureTime { get; set; }
|
|
|
|
[FieldOrder(9)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public ushort BottomLayersCount { get; set; }
|
|
|
|
[FieldOrder(10)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public ushort BottomLiftHeight { get; set; }
|
|
|
|
[FieldOrder(11)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public ushort BottomLiftSpeed { get; set; }
|
|
|
|
[FieldOrder(12)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public ushort LiftHeight { get; set; }
|
|
|
|
[FieldOrder(13)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public ushort LiftSpeed { get; set; }
|
|
|
|
[FieldOrder(14)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public ushort RetractSpeed { get; set; }
|
|
|
|
[FieldOrder(15)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public ushort BottomLightPWM { get; set; } = 255;
|
|
|
|
[FieldOrder(16)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public ushort LightPWM { get; set; } = 255;
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"{nameof(DisplayWidthDataSize)}: {DisplayWidthDataSize}, {nameof(DisplayWidthBytes)}: {DisplayWidthBytes}, {nameof(DisplayHeightDataSize)}: {DisplayHeightDataSize}, {nameof(DisplayHeightBytes)}: {DisplayHeightBytes}, {nameof(LayerHeightDataSize)}: {LayerHeightDataSize}, {nameof(LayerHeightBytes)}: {LayerHeightBytes}, {nameof(ExposureTime)}: {ExposureTime}, {nameof(WaitTimeBeforeCure)}: {WaitTimeBeforeCure}, {nameof(BottomExposureTime)}: {BottomExposureTime}, {nameof(BottomLayersCount)}: {BottomLayersCount}, {nameof(BottomLiftHeight)}: {BottomLiftHeight}, {nameof(BottomLiftSpeed)}: {BottomLiftSpeed}, {nameof(LiftHeight)}: {LiftHeight}, {nameof(LiftSpeed)}: {LiftSpeed}, {nameof(RetractSpeed)}: {RetractSpeed}, {nameof(BottomLightPWM)}: {BottomLightPWM}, {nameof(LightPWM)}: {LightPWM}";
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Layer Def
|
|
|
|
public sealed class PreLayer
|
|
{
|
|
[FieldOrder(0)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public uint Unknown { get; set; }
|
|
|
|
public PreLayer()
|
|
{
|
|
}
|
|
|
|
public PreLayer(uint unknown)
|
|
{
|
|
Unknown = unknown;
|
|
}
|
|
}
|
|
|
|
public sealed class LayerDef
|
|
{
|
|
public static byte[] GetHeaderBytes(uint unknown, uint lineCount)
|
|
{
|
|
var bytes = new byte[8];
|
|
BitExtensions.ToBytesBigEndian(unknown, bytes);
|
|
BitExtensions.ToBytesBigEndian(lineCount, bytes, 4);
|
|
return bytes;
|
|
}
|
|
|
|
[FieldOrder(0)] [FieldEndianness(Endianness.Big)] public uint Unknown { get; set; }
|
|
[FieldOrder(1)] [FieldEndianness(Endianness.Big)] public uint LineCount { get; set; }
|
|
[FieldOrder(2)] [FieldCount(nameof(LineCount))] public LayerLine[] Lines { get; set; } = Array.Empty<LayerLine>();
|
|
[FieldOrder(3)] public PageBreak PageBreak { get; set; } = new();
|
|
|
|
public LayerDef() { }
|
|
|
|
public LayerDef(uint unknown, uint lineCount, LayerLine[] lines)
|
|
{
|
|
Unknown = unknown;
|
|
LineCount = lineCount;
|
|
Lines = lines;
|
|
}
|
|
}
|
|
|
|
public sealed class LayerLine
|
|
{
|
|
public const byte CoordinateCount = 5;
|
|
[FieldOrder(0)] [FieldCount(CoordinateCount)] public byte[] Coordinates { get; set; } = new byte[CoordinateCount];
|
|
//[FieldOrder(0)] [FieldEndianness(Endianness.Big)] [FieldBitLength(13)] public ushort StartY { get; set; }
|
|
//[FieldOrder(1)] [FieldEndianness(Endianness.Big)] [FieldBitLength(13)] public ushort EndY { get; set; }
|
|
//[FieldOrder(2)] [FieldEndianness(Endianness.Big)] [FieldBitLength(14)] public ushort StartX { get; set; }
|
|
[FieldOrder(1)] public byte Gray { get; set; }
|
|
|
|
[Ignore] public ushort StartY => (ushort)((((Coordinates[0] << 8) + Coordinates[1]) >> 3) & 0x1FFF); // 13 bits
|
|
|
|
[Ignore] public ushort EndY => (ushort)((((Coordinates[1] << 16) + (Coordinates[2] << 8) + Coordinates[3]) >> 6) & 0x1FFF); // 13 bits
|
|
|
|
[Ignore] public ushort StartX => (ushort)(((Coordinates[3] << 8) + Coordinates[4]) & 0x3FFF); // 14 bits
|
|
[Ignore] public ushort Length => (ushort)(EndY - StartY);
|
|
|
|
public static byte[] GetBytes(ushort startY, ushort endY, ushort startX, byte gray)
|
|
{
|
|
var bytes = new byte[CoordinateCount + 1];
|
|
bytes[0] = (byte)((startY >> 5) & 0xFF);
|
|
bytes[1] = (byte)(((startY << 3) + (endY >> 10)) & 0xFF);
|
|
bytes[2] = (byte)((endY >> 2) & 0xFF);
|
|
bytes[3] = (byte)(((endY << 6) + (startX >> 8)) & 0xFF);
|
|
bytes[4] = (byte)startX;
|
|
bytes[5] = gray;
|
|
return bytes;
|
|
}
|
|
|
|
public LayerLine() { }
|
|
|
|
public LayerLine(ushort startY, ushort endY, ushort startX, byte gray)
|
|
{
|
|
Coordinates[0] = (byte)((startY >> 5) & 0xFF);
|
|
Coordinates[1] = (byte)(((startY << 3) + (endY >> 10)) & 0xFF);
|
|
Coordinates[2] = (byte)((endY >> 2) & 0xFF);
|
|
Coordinates[3] = (byte)(((endY << 6) + (startX >> 8)) & 0xFF);
|
|
Coordinates[4] = (byte)startX;
|
|
/*StartY = startY;
|
|
EndY = endY;
|
|
StartX = startX;*/
|
|
Gray = gray;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"{nameof(Gray)}: {Gray}, {nameof(StartY)}: {StartY}, {nameof(EndY)}: {EndY}, {nameof(StartX)}: {StartX}, {nameof(Length)}: {Length}";
|
|
}
|
|
}
|
|
|
|
public sealed class PageBreak
|
|
{
|
|
public static byte[] Bytes => new byte[] { 0x0D, 0x0A };
|
|
|
|
[FieldOrder(0)] public byte Line { get; set; } = 0x0D;
|
|
[FieldOrder(1)] public byte Break { get; set; } = 0x0A;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Footer
|
|
public sealed class Footer
|
|
{
|
|
/// <summary>
|
|
/// Gets the size of the header
|
|
/// </summary>
|
|
[FieldOrder(0)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public uint FooterSize { get; set; } = HEADER_SIZE;
|
|
|
|
/// <summary>
|
|
/// Gets the header name
|
|
/// </summary>
|
|
[FieldOrder(1)]
|
|
[FieldLength(HEADER_SIZE)]
|
|
[SerializeAs(SerializedType.TerminatedString)]
|
|
public string FooterValue { get; set; } = HEADER_VALUE;
|
|
|
|
[FieldOrder(2)]
|
|
[FieldEndianness(Endianness.Big)]
|
|
public uint Unknown { get; set; } = 7;
|
|
|
|
public void Validate()
|
|
{
|
|
if (FooterSize != HEADER_SIZE || FooterValue != HEADER_VALUE)
|
|
{
|
|
throw new FileLoadException("Not a valid CXDLP file!");
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
public Header HeaderSettings { get; protected internal set; } = new();
|
|
public SlicerInfo SlicerInfoSettings { get; protected internal set; } = new();
|
|
public Footer FooterSettings { get; protected internal set; } = new();
|
|
|
|
public override FileFormatType FileType => FileFormatType.Binary;
|
|
|
|
public override FileExtension[] FileExtensions { get; } = {
|
|
new(typeof(CXDLPv1File), "v1.cxdlp", "Creality CXDLP v1"),
|
|
};
|
|
|
|
public override PrintParameterModifier[]? PrintParameterModifiers { get; } =
|
|
{
|
|
PrintParameterModifier.BottomLayerCount,
|
|
|
|
PrintParameterModifier.WaitTimeBeforeCure,
|
|
|
|
PrintParameterModifier.BottomExposureTime,
|
|
PrintParameterModifier.ExposureTime,
|
|
|
|
PrintParameterModifier.BottomLiftHeight,
|
|
PrintParameterModifier.BottomLiftSpeed,
|
|
PrintParameterModifier.LiftHeight,
|
|
PrintParameterModifier.LiftSpeed,
|
|
PrintParameterModifier.RetractSpeed,
|
|
|
|
PrintParameterModifier.BottomLightPWM,
|
|
PrintParameterModifier.LightPWM,
|
|
};
|
|
|
|
public override Size[]? ThumbnailsOriginalSize { get; } =
|
|
{
|
|
new(116, 116),
|
|
new(290, 290),
|
|
new(290, 290)
|
|
};
|
|
|
|
public override uint ResolutionX
|
|
{
|
|
get => HeaderSettings.ResolutionX;
|
|
set
|
|
{
|
|
HeaderSettings.ResolutionX = (ushort)value;
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override uint ResolutionY
|
|
{
|
|
get => HeaderSettings.ResolutionY;
|
|
set
|
|
{
|
|
HeaderSettings.ResolutionY = (ushort)value;
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override float DisplayWidth
|
|
{
|
|
get => float.Parse(Encoding.ASCII.GetString(SlicerInfoSettings.DisplayWidthBytes.Where(b => b != 0).ToArray()));
|
|
set
|
|
{
|
|
string str = Math.Round(value, 2).ToString(CultureInfo.InvariantCulture);
|
|
SlicerInfoSettings.DisplayWidthDataSize = (uint)(str.Length * 2);
|
|
var data = new byte[SlicerInfoSettings.DisplayWidthDataSize];
|
|
for (var i = 0; i < str.Length; i++)
|
|
{
|
|
data[i * 2 + 1] = System.Convert.ToByte(str[i]);
|
|
}
|
|
|
|
SlicerInfoSettings.DisplayWidthBytes = data;
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override float DisplayHeight
|
|
{
|
|
get => float.Parse(Encoding.ASCII.GetString(SlicerInfoSettings.DisplayHeightBytes.Where(b => b != 0).ToArray()));
|
|
set
|
|
{
|
|
string str = Math.Round(value, 2).ToString(CultureInfo.InvariantCulture);
|
|
SlicerInfoSettings.DisplayHeightDataSize = (uint)(str.Length * 2);
|
|
var data = new byte[SlicerInfoSettings.DisplayHeightDataSize];
|
|
for (var i = 0; i < str.Length; i++)
|
|
{
|
|
data[i * 2 + 1] = System.Convert.ToByte(str[i]);
|
|
}
|
|
|
|
SlicerInfoSettings.DisplayHeightBytes = data;
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override float LayerHeight
|
|
{
|
|
get => float.Parse(Encoding.ASCII.GetString(SlicerInfoSettings.LayerHeightBytes.Where(b => b != 0).ToArray()));
|
|
set
|
|
{
|
|
string str = Layer.RoundHeight(value).ToString(CultureInfo.InvariantCulture);
|
|
SlicerInfoSettings.LayerHeightDataSize = (uint)(str.Length * 2);
|
|
var data = new byte[SlicerInfoSettings.LayerHeightDataSize];
|
|
for (var i = 0; i < str.Length; i++)
|
|
{
|
|
data[i * 2 + 1] = System.Convert.ToByte(str[i]);
|
|
}
|
|
|
|
SlicerInfoSettings.LayerHeightBytes = data;
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override uint LayerCount
|
|
{
|
|
get => base.LayerCount;
|
|
set => base.LayerCount = HeaderSettings.LayerCount = (ushort)base.LayerCount;
|
|
}
|
|
|
|
public override ushort BottomLayerCount
|
|
{
|
|
get => SlicerInfoSettings.BottomLayersCount;
|
|
set => base.BottomLayerCount = SlicerInfoSettings.BottomLayersCount = value;
|
|
}
|
|
|
|
public override float BottomWaitTimeBeforeCure => WaitTimeBeforeCure;
|
|
|
|
public override float WaitTimeBeforeCure
|
|
{
|
|
get => SlicerInfoSettings.WaitTimeBeforeCure;
|
|
set => base.WaitTimeBeforeCure = SlicerInfoSettings.WaitTimeBeforeCure = (ushort)value;
|
|
}
|
|
|
|
public override float BottomExposureTime
|
|
{
|
|
get => SlicerInfoSettings.BottomExposureTime;
|
|
set => base.BottomExposureTime = SlicerInfoSettings.BottomExposureTime = (ushort)value;
|
|
}
|
|
|
|
public override float ExposureTime
|
|
{
|
|
get => SlicerInfoSettings.ExposureTime;
|
|
set => base.ExposureTime = SlicerInfoSettings.ExposureTime = (ushort)value;
|
|
}
|
|
|
|
public override float BottomLiftHeight
|
|
{
|
|
get => SlicerInfoSettings.BottomLiftHeight;
|
|
set => base.BottomLiftHeight = SlicerInfoSettings.BottomLiftHeight = (ushort)value;
|
|
}
|
|
|
|
public override float LiftHeight
|
|
{
|
|
get => SlicerInfoSettings.LiftHeight;
|
|
set => base.LiftHeight = SlicerInfoSettings.LiftHeight = (ushort)value;
|
|
}
|
|
|
|
public override float BottomLiftSpeed
|
|
{
|
|
get => SlicerInfoSettings.BottomLiftSpeed;
|
|
set => base.BottomLiftSpeed = SlicerInfoSettings.BottomLiftSpeed = (ushort)value;
|
|
}
|
|
|
|
public override float LiftSpeed
|
|
{
|
|
get => SlicerInfoSettings.LiftSpeed;
|
|
set => base.LiftSpeed = SlicerInfoSettings.LiftSpeed = (ushort)value;
|
|
}
|
|
|
|
public override float BottomRetractSpeed => RetractSpeed;
|
|
|
|
public override float RetractSpeed
|
|
{
|
|
get => SlicerInfoSettings.RetractSpeed;
|
|
set => base.RetractSpeed = SlicerInfoSettings.RetractSpeed = (ushort)value;
|
|
}
|
|
|
|
public override byte BottomLightPWM
|
|
{
|
|
get => (byte)SlicerInfoSettings.BottomLightPWM;
|
|
set => base.BottomLightPWM = (byte)(SlicerInfoSettings.BottomLightPWM = value);
|
|
}
|
|
|
|
public override byte LightPWM
|
|
{
|
|
get => (byte)SlicerInfoSettings.LightPWM;
|
|
set => base.LightPWM = (byte)(SlicerInfoSettings.LightPWM = value);
|
|
}
|
|
|
|
public override object[] Configs => new object[] { HeaderSettings, SlicerInfoSettings, FooterSettings };
|
|
|
|
#endregion
|
|
|
|
#region Constructors
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
protected override void EncodeInternally(OperationProgress progress)
|
|
{
|
|
using var outputFile = new FileStream(TemporaryOutputFileFullPath, FileMode.Create, FileAccess.Write);
|
|
|
|
if (ResolutionX == 2560 && ResolutionY == 1620)
|
|
{
|
|
MachineName = "CL-60";
|
|
}
|
|
else if (ResolutionX == 3840 && ResolutionY == 2400)
|
|
{
|
|
MachineName = "CL-89";
|
|
}
|
|
|
|
var pageBreak = PageBreak.Bytes;
|
|
|
|
Helpers.SerializeWriteFileStream(outputFile, HeaderSettings);
|
|
|
|
var previews = new byte[ThumbnailsOriginalSize!.Length][];
|
|
|
|
// Previews
|
|
Parallel.For(0, previews.Length, CoreSettings.GetParallelOptions(progress), previewIndex =>
|
|
{
|
|
var encodeLength = ThumbnailsOriginalSize[previewIndex].Area() * 2;
|
|
if (Thumbnails[previewIndex] is null)
|
|
{
|
|
previews[previewIndex] = new byte[encodeLength];
|
|
return;
|
|
}
|
|
|
|
previews[previewIndex] = EncodeImage(DATATYPE_RGB565_BE, Thumbnails[previewIndex]!);
|
|
|
|
if (encodeLength != previews[previewIndex].Length)
|
|
{
|
|
throw new FileLoadException($"Preview encode incomplete encode, expected: {previews[previewIndex].Length}, encoded: {encodeLength}");
|
|
}
|
|
});
|
|
|
|
for (int i = 0; i < ThumbnailsOriginalSize.Length; i++)
|
|
{
|
|
Helpers.SerializeWriteFileStream(outputFile, previews[i]);
|
|
outputFile.WriteBytes(pageBreak);
|
|
previews[i] = null!;
|
|
}
|
|
Helpers.SerializeWriteFileStream(outputFile, SlicerInfoSettings);
|
|
|
|
progress.Reset(OperationProgress.StatusEncodeLayers, LayerCount);
|
|
|
|
|
|
for (int layerIndex = 0; layerIndex < LayerCount; layerIndex++)
|
|
{
|
|
outputFile.WriteBytes(BitExtensions.ToBytesBigEndian(this[layerIndex].NonZeroPixelCount));
|
|
}
|
|
outputFile.WriteBytes(pageBreak);
|
|
|
|
var layerBytes = new List<byte>[LayerCount];
|
|
foreach (var batch in BatchLayersIndexes())
|
|
{
|
|
Parallel.ForEach(batch, CoreSettings.GetParallelOptions(progress), layerIndex =>
|
|
{
|
|
var layer = this[layerIndex];
|
|
using (var mat = layer.LayerMat)
|
|
{
|
|
var span = mat.GetDataByteSpan();
|
|
|
|
layerBytes[layerIndex] = new();
|
|
|
|
uint lineCount = 0;
|
|
|
|
for (int x = layer.BoundingRectangle.X; x < layer.BoundingRectangle.Right; x++)
|
|
{
|
|
int y = layer.BoundingRectangle.Y;
|
|
int startY = -1;
|
|
byte lastColor = 0;
|
|
for (; y < layer.BoundingRectangle.Bottom; y++)
|
|
{
|
|
int pos = mat.GetPixelPos(x, y);
|
|
byte color = span[pos];
|
|
|
|
if (lastColor == color && color != 0) continue;
|
|
|
|
if (startY >= 0)
|
|
{
|
|
layerBytes[layerIndex].AddRange(LayerLine.GetBytes((ushort)startY, (ushort)(y - 1),
|
|
(ushort)x, lastColor));
|
|
lineCount++;
|
|
}
|
|
|
|
startY = color == 0 ? -1 : y;
|
|
|
|
lastColor = color;
|
|
}
|
|
|
|
if (startY >= 0)
|
|
{
|
|
layerBytes[layerIndex].AddRange(LayerLine.GetBytes((ushort)startY, (ushort)(y - 1),
|
|
(ushort)x, lastColor));
|
|
lineCount++;
|
|
}
|
|
}
|
|
|
|
layerBytes[layerIndex].InsertRange(0, LayerDef.GetHeaderBytes(layer.NonZeroPixelCount, lineCount));
|
|
layerBytes[layerIndex].AddRange(pageBreak);
|
|
}
|
|
|
|
progress.LockAndIncrement();
|
|
});
|
|
|
|
foreach (var layerIndex in batch)
|
|
{
|
|
outputFile.WriteBytes(layerBytes[layerIndex].ToArray());
|
|
layerBytes[layerIndex] = null!;
|
|
}
|
|
}
|
|
|
|
|
|
Helpers.SerializeWriteFileStream(outputFile, FooterSettings);
|
|
|
|
Debug.WriteLine("Encode Results:");
|
|
Debug.WriteLine(HeaderSettings);
|
|
Debug.WriteLine(SlicerInfoSettings);
|
|
Debug.WriteLine("-End-");
|
|
}
|
|
|
|
protected override void DecodeInternally(OperationProgress progress)
|
|
{
|
|
using var inputFile = new FileStream(FileFullPath!, FileMode.Open, FileAccess.Read);
|
|
HeaderSettings = Helpers.Deserialize<Header>(inputFile);
|
|
HeaderSettings.Validate();
|
|
|
|
Debug.WriteLine(HeaderSettings);
|
|
|
|
byte[][] previews = new byte[ThumbnailsOriginalSize!.Length][];
|
|
for (int i = 0; i < ThumbnailsOriginalSize.Length; i++)
|
|
{
|
|
previews[i] = new byte[ThumbnailsOriginalSize[i].Area() * 2];
|
|
inputFile.ReadBytes(previews[i]);
|
|
inputFile.Seek(2, SeekOrigin.Current);
|
|
}
|
|
|
|
Parallel.For(0, previews.Length, CoreSettings.GetParallelOptions(progress), previewIndex =>
|
|
{
|
|
Thumbnails[previewIndex] = DecodeImage(DATATYPE_RGB565_BE, previews[previewIndex], ThumbnailsOriginalSize[previewIndex]);
|
|
previews[previewIndex] = null!;
|
|
});
|
|
|
|
|
|
SlicerInfoSettings = Helpers.Deserialize<SlicerInfo>(inputFile);
|
|
Debug.WriteLine(SlicerInfoSettings);
|
|
|
|
Init(HeaderSettings.LayerCount, DecodeType == FileDecodeType.Partial);
|
|
inputFile.Seek(LayerCount * 4 + 2, SeekOrigin.Current); // Skip pre layers
|
|
|
|
|
|
if (DecodeType == FileDecodeType.Full)
|
|
{
|
|
progress.Reset(OperationProgress.StatusDecodeLayers, LayerCount);
|
|
var linesBytes = new byte[LayerCount][];
|
|
foreach (var batch in BatchLayersIndexes())
|
|
{
|
|
foreach (var layerIndex in batch)
|
|
{
|
|
progress.ThrowIfCancellationRequested();
|
|
|
|
inputFile.Seek(4, SeekOrigin.Current);
|
|
var lineCount = BitExtensions.ToUIntBigEndian(inputFile.ReadBytes(4));
|
|
|
|
linesBytes[layerIndex] = new byte[lineCount * 6];
|
|
inputFile.ReadBytes(linesBytes[layerIndex]);
|
|
inputFile.Seek(2, SeekOrigin.Current);
|
|
|
|
}
|
|
|
|
Parallel.ForEach(batch, CoreSettings.GetParallelOptions(progress), layerIndex =>
|
|
{
|
|
using (var mat = EmguExtensions.InitMat(Resolution))
|
|
{
|
|
for (int i = 0; i < linesBytes[layerIndex].Length; i++)
|
|
{
|
|
LayerLine line = new()
|
|
{
|
|
Coordinates =
|
|
{
|
|
[0] = linesBytes[layerIndex][i++],
|
|
[1] = linesBytes[layerIndex][i++],
|
|
[2] = linesBytes[layerIndex][i++],
|
|
[3] = linesBytes[layerIndex][i++],
|
|
[4] = linesBytes[layerIndex][i++]
|
|
},
|
|
Gray = linesBytes[layerIndex][i]
|
|
};
|
|
|
|
CvInvoke.Line(mat, new Point(line.StartX, line.StartY),
|
|
new Point(line.StartX, line.EndY),
|
|
new MCvScalar(line.Gray));
|
|
}
|
|
|
|
linesBytes[layerIndex] = null!;
|
|
|
|
_layers[layerIndex] = new Layer((uint)layerIndex, mat, this);
|
|
}
|
|
|
|
progress.LockAndIncrement();
|
|
});
|
|
}
|
|
}
|
|
else // Partial read
|
|
{
|
|
inputFile.Seek(-Helpers.Serializer.SizeOf(FooterSettings), SeekOrigin.End);
|
|
}
|
|
|
|
FooterSettings = Helpers.Deserialize<Footer>(inputFile);
|
|
FooterSettings.Validate();
|
|
}
|
|
|
|
protected override void PartialSaveInternally(OperationProgress progress)
|
|
{
|
|
var offset = Helpers.Serializer.SizeOf(HeaderSettings);
|
|
foreach (var size in ThumbnailsOriginalSize!)
|
|
{
|
|
offset += size.Area() * 2 + 2; // + page break
|
|
}
|
|
|
|
using var outputFile = new FileStream(TemporaryOutputFileFullPath, FileMode.Open, FileAccess.Write);
|
|
outputFile.Seek(offset, SeekOrigin.Begin);
|
|
Helpers.SerializeWriteFileStream(outputFile, SlicerInfoSettings);
|
|
}
|
|
|
|
#endregion
|
|
} |