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.
This commit is contained in:
Tiago Conceição
2023-01-01 20:13:44 +00:00
parent 20f0de97a9
commit cd2ad6bd33
+53 -7
View File
@@ -65,6 +65,8 @@ public class Layer : BindableBase, IEquatable<Layer>, IEquatable<uint>
private Rectangle _boundingRectangle = Rectangle.Empty; private Rectangle _boundingRectangle = Rectangle.Empty;
private bool _isModified; private bool _isModified;
private uint _index; private uint _index;
private uint _resolutionX;
private uint _resolutionY;
private float _positionZ; private float _positionZ;
private float _lightOffDelay; private float _lightOffDelay;
private float _waitTimeBeforeCure; private float _waitTimeBeforeCure;
@@ -90,6 +92,38 @@ public class Layer : BindableBase, IEquatable<Layer>, IEquatable<uint>
/// </summary> /// </summary>
public FileFormat SlicerFile { get; set; } public FileFormat SlicerFile { get; set; }
/// <summary>
/// Image resolution X
/// </summary>
public uint ResolutionX
{
get => _resolutionX;
set => RaiseAndSetIfChanged(ref _resolutionX, value);
}
/// <summary>
/// Image resolution Y
/// </summary>
public uint ResolutionY
{
get => _resolutionY;
set => RaiseAndSetIfChanged(ref _resolutionY, value);
}
/// <summary>
/// Image resolution
/// </summary>
public Size Resolution
{
get => new((int)ResolutionX, (int)ResolutionY);
set
{
ResolutionX = (uint)value.Width;
ResolutionY = (uint)value.Height;
RaisePropertyChanged();
}
}
/// <summary> /// <summary>
/// Gets the number of non zero pixels on this layer image /// Gets the number of non zero pixels on this layer image
/// </summary> /// </summary>
@@ -182,12 +216,12 @@ public class Layer : BindableBase, IEquatable<Layer>, IEquatable<uint>
/// <summary> /// <summary>
/// Gets the first pixel index on this layer /// Gets the first pixel index on this layer
/// </summary> /// </summary>
public uint FirstPixelIndex => (uint)(BoundingRectangle.Y * SlicerFile.ResolutionX + BoundingRectangle.X); public uint FirstPixelIndex => (uint)(BoundingRectangle.Y * ResolutionX + BoundingRectangle.X);
/// <summary> /// <summary>
/// Gets the last pixel index on this layer /// Gets the last pixel index on this layer
/// </summary> /// </summary>
public uint LastPixelIndex => (uint)(BoundingRectangle.Bottom * SlicerFile.ResolutionX + BoundingRectangle.Right); public uint LastPixelIndex => (uint)(BoundingRectangle.Bottom * ResolutionX + BoundingRectangle.Right);
/// <summary> /// <summary>
/// Gets the first pixel <see cref="Point"/> on this layer /// Gets the first pixel <see cref="Point"/> on this layer
@@ -801,12 +835,12 @@ public class Layer : BindableBase, IEquatable<Layer>, IEquatable<uint>
CvInvoke.Imdecode(_compressedBytes, ImreadModes.Grayscale, mat); CvInvoke.Imdecode(_compressedBytes, ImreadModes.Grayscale, mat);
break; break;
case LayerCompressionCodec.Lz4: case LayerCompressionCodec.Lz4:
mat = SlicerFile.CreateMat(false); mat = new Mat(Resolution, DepthType.Cv8U, 1);
LZ4Codec.Decode(_compressedBytes.AsSpan(), mat.GetDataByteSpan()); LZ4Codec.Decode(_compressedBytes.AsSpan(), mat.GetDataByteSpan());
break; break;
case LayerCompressionCodec.GZip: case LayerCompressionCodec.GZip:
{ {
mat = SlicerFile.CreateMat(false); mat = new Mat(Resolution, DepthType.Cv8U, 1);
unsafe unsafe
{ {
fixed (byte* pBuffer = _compressedBytes) fixed (byte* pBuffer = _compressedBytes)
@@ -822,7 +856,7 @@ public class Layer : BindableBase, IEquatable<Layer>, IEquatable<uint>
} }
case LayerCompressionCodec.Deflate: case LayerCompressionCodec.Deflate:
{ {
mat = SlicerFile.CreateMat(false); mat = new Mat(Resolution, DepthType.Cv8U, 1);
unsafe unsafe
{ {
fixed (byte* pBuffer = _compressedBytes) fixed (byte* pBuffer = _compressedBytes)
@@ -837,7 +871,7 @@ public class Layer : BindableBase, IEquatable<Layer>, IEquatable<uint>
break; break;
} }
/*case LayerCompressionMethod.None: /*case LayerCompressionMethod.None:
mat = new(SlicerFile.Resolution, DepthType.Cv8U, 1); mat = new Mat(Resolution, DepthType.Cv8U, 1);
//mat.SetBytes(_compressedBytes!); //mat.SetBytes(_compressedBytes!);
_compressedBytes.CopyTo(mat.GetDataByteSpan()); _compressedBytes.CopyTo(mat.GetDataByteSpan());
break;*/ break;*/
@@ -852,8 +886,15 @@ public class Layer : BindableBase, IEquatable<Layer>, IEquatable<uint>
if (value is not null) if (value is not null)
{ {
CompressedBytes = CompressMat(value, _compressionCodec); CompressedBytes = CompressMat(value, _compressionCodec);
_resolutionX = (uint)value.Width;
_resolutionX = (uint)value.Height;
} }
else
{
_resolutionX = 0;
_resolutionY = 0;
}
GetBoundingRectangle(value, true); GetBoundingRectangle(value, true);
RaisePropertyChanged(); RaisePropertyChanged();
} }
@@ -997,6 +1038,9 @@ public class Layer : BindableBase, IEquatable<Layer>, IEquatable<uint>
SlicerFile = slicerFile; SlicerFile = slicerFile;
_index = index; _index = index;
_resolutionX = slicerFile.ResolutionX;
_resolutionY = slicerFile.ResolutionY;
//if (slicerFile is null) return; //if (slicerFile is null) return;
_positionZ = SlicerFile.GetHeightFromLayer(index); _positionZ = SlicerFile.GetHeightFromLayer(index);
@@ -1647,6 +1691,8 @@ public class Layer : BindableBase, IEquatable<Layer>, IEquatable<uint>
public void CopyImageTo(Layer layer) public void CopyImageTo(Layer layer)
{ {
if (!HaveImage) return; if (!HaveImage) return;
layer.ResolutionX = _resolutionX;
layer.ResolutionY = _resolutionY;
layer.CompressedBytes = _compressedBytes?.ToArray(); layer.CompressedBytes = _compressedBytes?.ToArray();
layer.BoundingRectangle = _boundingRectangle; layer.BoundingRectangle = _boundingRectangle;
layer.NonZeroPixelCount = _nonZeroPixelCount; layer.NonZeroPixelCount = _nonZeroPixelCount;