From cd2ad6bd3355ca3df0d7cfd1f026fad1221d3c9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Concei=C3=A7=C3=A3o?= Date: Sun, 1 Jan 2023 20:13:44 +0000 Subject: [PATCH] Add Resolution properties to Layer Each layer can now be aware of it own resolution. This fix the problem where we can't fix file resolution from layers when using compression codec other than png. --- UVtools.Core/Layers/Layer.cs | 60 +++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/UVtools.Core/Layers/Layer.cs b/UVtools.Core/Layers/Layer.cs index ffbdd0e..7b99a52 100644 --- a/UVtools.Core/Layers/Layer.cs +++ b/UVtools.Core/Layers/Layer.cs @@ -65,6 +65,8 @@ public class Layer : BindableBase, IEquatable, IEquatable private Rectangle _boundingRectangle = Rectangle.Empty; private bool _isModified; private uint _index; + private uint _resolutionX; + private uint _resolutionY; private float _positionZ; private float _lightOffDelay; private float _waitTimeBeforeCure; @@ -90,6 +92,38 @@ public class Layer : BindableBase, IEquatable, IEquatable /// public FileFormat SlicerFile { get; set; } + /// + /// Image resolution X + /// + public uint ResolutionX + { + get => _resolutionX; + set => RaiseAndSetIfChanged(ref _resolutionX, value); + } + + /// + /// Image resolution Y + /// + public uint ResolutionY + { + get => _resolutionY; + set => RaiseAndSetIfChanged(ref _resolutionY, value); + } + + /// + /// Image resolution + /// + public Size Resolution + { + get => new((int)ResolutionX, (int)ResolutionY); + set + { + ResolutionX = (uint)value.Width; + ResolutionY = (uint)value.Height; + RaisePropertyChanged(); + } + } + /// /// Gets the number of non zero pixels on this layer image /// @@ -182,12 +216,12 @@ public class Layer : BindableBase, IEquatable, IEquatable /// /// Gets the first pixel index on this layer /// - public uint FirstPixelIndex => (uint)(BoundingRectangle.Y * SlicerFile.ResolutionX + BoundingRectangle.X); + public uint FirstPixelIndex => (uint)(BoundingRectangle.Y * ResolutionX + BoundingRectangle.X); /// /// Gets the last pixel index on this layer /// - public uint LastPixelIndex => (uint)(BoundingRectangle.Bottom * SlicerFile.ResolutionX + BoundingRectangle.Right); + public uint LastPixelIndex => (uint)(BoundingRectangle.Bottom * ResolutionX + BoundingRectangle.Right); /// /// Gets the first pixel on this layer @@ -801,12 +835,12 @@ public class Layer : BindableBase, IEquatable, IEquatable CvInvoke.Imdecode(_compressedBytes, ImreadModes.Grayscale, mat); break; case LayerCompressionCodec.Lz4: - mat = SlicerFile.CreateMat(false); + mat = new Mat(Resolution, DepthType.Cv8U, 1); LZ4Codec.Decode(_compressedBytes.AsSpan(), mat.GetDataByteSpan()); break; case LayerCompressionCodec.GZip: { - mat = SlicerFile.CreateMat(false); + mat = new Mat(Resolution, DepthType.Cv8U, 1); unsafe { fixed (byte* pBuffer = _compressedBytes) @@ -822,7 +856,7 @@ public class Layer : BindableBase, IEquatable, IEquatable } case LayerCompressionCodec.Deflate: { - mat = SlicerFile.CreateMat(false); + mat = new Mat(Resolution, DepthType.Cv8U, 1); unsafe { fixed (byte* pBuffer = _compressedBytes) @@ -837,7 +871,7 @@ public class Layer : BindableBase, IEquatable, IEquatable break; } /*case LayerCompressionMethod.None: - mat = new(SlicerFile.Resolution, DepthType.Cv8U, 1); + mat = new Mat(Resolution, DepthType.Cv8U, 1); //mat.SetBytes(_compressedBytes!); _compressedBytes.CopyTo(mat.GetDataByteSpan()); break;*/ @@ -852,8 +886,15 @@ public class Layer : BindableBase, IEquatable, IEquatable if (value is not null) { CompressedBytes = CompressMat(value, _compressionCodec); + _resolutionX = (uint)value.Width; + _resolutionX = (uint)value.Height; } - + else + { + _resolutionX = 0; + _resolutionY = 0; + } + GetBoundingRectangle(value, true); RaisePropertyChanged(); } @@ -997,6 +1038,9 @@ public class Layer : BindableBase, IEquatable, IEquatable SlicerFile = slicerFile; _index = index; + + _resolutionX = slicerFile.ResolutionX; + _resolutionY = slicerFile.ResolutionY; //if (slicerFile is null) return; _positionZ = SlicerFile.GetHeightFromLayer(index); @@ -1647,6 +1691,8 @@ public class Layer : BindableBase, IEquatable, IEquatable public void CopyImageTo(Layer layer) { if (!HaveImage) return; + layer.ResolutionX = _resolutionX; + layer.ResolutionY = _resolutionY; layer.CompressedBytes = _compressedBytes?.ToArray(); layer.BoundingRectangle = _boundingRectangle; layer.NonZeroPixelCount = _nonZeroPixelCount;