Skip to content

Commit

Permalink
v0.6.3.0
Browse files Browse the repository at this point in the history
* (Add) Layer remove button
* (Add) Tool: Layer removal
* (Add) Layer Repair tool: Remove empty layers
* (Add) Issues: Remove a empty layer will effectively remove the layer
* (Fix) SL1: When converting to other format in some cases the parameters on Printer Notes were not respected nor exported (#12)
* (Fix) Pixel Editor: Draw pixels was painting on wrong positions after apply, when refreshing layer some pixels disappear (Spotted by Nicholas Taylor)
  • Loading branch information
sn4k3 committed Jul 19, 2020
1 parent 9b9438d commit 97f0e38
Show file tree
Hide file tree
Showing 26 changed files with 3,243 additions and 221 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 19/07/2020 - v0.6.3.0

* (Add) Layer remove button
* (Add) Tool: Layer removal
* (Add) Layer Repair tool: Remove empty layers
* (Add) Issues: Remove a empty layer will effectively remove the layer
* (Fix) SL1: When converting to other format in some cases the parameters on Printer Notes were not respected nor exported (#12)
* (Fix) Pixel Editor: Draw pixels was painting on wrong positions after apply, when refreshing layer some pixels disappear (Spotted by Nicholas Taylor)

## 17/07/2020 - v0.6.2.3

* (Add) Issue: EmptyLayer - Detects empty layers were image is all black with 0 pixels to cure
Expand Down
20 changes: 3 additions & 17 deletions UVtools.Core/Extensions/EmguExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,31 +197,17 @@ public static byte GetByte(this Mat mat, int pos)

public static void SetByte(this Mat mat, int pixel, byte value) => SetByte(mat, pixel, new[] {value});

public static void SetByte(this Mat mat, int pixel, byte[] value)
{
/*var sw = Stopwatch.StartNew();
var span = new Span<byte>(IntPtr.Add(mat.DataPointer, pixel).ToPointer(), value.Length);
for (int i = 0; i < value.Length; i++)
{
span[i] = value[i];
}
Debug.Write(sw.ElapsedMilliseconds);
Debug.Write(" - ");
sw.Restart();*/
public static void SetByte(this Mat mat, int pixel, byte[] value) =>
Marshal.Copy(value, 0, mat.DataPointer + pixel, value.Length);
//Debug.Write(sw.ElapsedMilliseconds);
}

public static void SetByte(this Mat mat, int x, int y, byte value) =>
SetByte(mat, x, y, new[] {value});

public static void SetByte(this Mat mat, int x, int y, byte[] value) =>
SetByte(mat, y * mat.Cols + x, value);
SetByte(mat, y * mat.Step + x * mat.NumberOfChannels, value);

public static void SetBytes(this Mat mat, byte[] value)
{
public static void SetBytes(this Mat mat, byte[] value) =>
Marshal.Copy(value, 0, mat.DataPointer, value.Length);
}

}
}
29 changes: 25 additions & 4 deletions UVtools.Core/FileFormats/CWSFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ public class Slice

public override float LayerHeight => SliceSettings.Thickness;

public override uint LayerCount
{
set
{
OutputSettings.LayersNum = LayerCount;
SliceSettings.LayersNum = LayerCount;
}
}

public override ushort InitialLayerCount => SliceSettings.HeadLayersNum;

public override float InitialExposureTime => SliceSettings.HeadLayersExpoMs / 1000f;
Expand Down Expand Up @@ -207,14 +216,16 @@ public override void Encode(string fileFullPath, OperationProgress progress = nu
{
progress.Token.ThrowIfCancellationRequested();
Layer layer = this[layerIndex];
var layerImagePath = $"{Path.GetFileNameWithoutExtension(fileFullPath)}{layer.Index.ToString().PadLeft(LayerCount.ToString().Length, '0')}.png";
var layerImagePath = $"{Path.GetFileNameWithoutExtension(fileFullPath)}{layerIndex.ToString().PadLeft(LayerCount.ToString().Length, '0')}.png";
outputFile.PutFileContent(layerImagePath, layer.CompressedBytes, ZipArchiveMode.Create);
progress++;
}

UpdateGCode();
outputFile.PutFileContent($"{Path.GetFileNameWithoutExtension(fileFullPath)}.gcode", GCode.ToString(), ZipArchiveMode.Create);
}

AfterEncode();
}

public override void Decode(string fileFullPath, OperationProgress progress = null)
Expand Down Expand Up @@ -292,7 +303,7 @@ public override void Decode(string fileFullPath, OperationProgress progress = nu
}


LayerManager = new LayerManager(OutputSettings.LayersNum);
LayerManager = new LayerManager(OutputSettings.LayersNum, this);

var gcode = GCode.ToString();
float currentHeight = 0;
Expand Down Expand Up @@ -447,6 +458,16 @@ void UpdateLayers()

public override void SaveAs(string filePath = null, OperationProgress progress = null)
{
if (RequireFullEncode)
{
if (!string.IsNullOrEmpty(filePath))
{
FileFullPath = filePath;
}
Encode(FileFullPath, progress);
return;
}

if (!string.IsNullOrEmpty(filePath))
{
File.Copy(FileFullPath, filePath, true);
Expand Down Expand Up @@ -486,12 +507,12 @@ public override void SaveAs(string filePath = null, OperationProgress progress =
}
outputFile.PutFileContent($"{Path.GetFileNameWithoutExtension(FileFullPath)}.gcode", GCode.ToString(), ZipArchiveMode.Update);

foreach (var layer in this)
/*foreach (var layer in this)
{
if (!layer.IsModified) continue;
outputFile.PutFileContent(layer.Filename, layer.CompressedBytes, ZipArchiveMode.Update);
layer.IsModified = false;
}
}*/
}

//Decode(FileFullPath, progress);
Expand Down
17 changes: 14 additions & 3 deletions UVtools.Core/FileFormats/ChituboxFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ public byte[] Read(byte[] input)

public override byte ThumbnailsCount { get; } = 2;

public override System.Drawing.Size[] ThumbnailsOriginalSize { get; } = {new System.Drawing.Size(400, 300), new System.Drawing.Size(200, 125)};
public override Size[] ThumbnailsOriginalSize { get; } = {new Size(400, 300), new Size(200, 125)};

public override uint ResolutionX => HeaderSettings.ResolutionX;

Expand All @@ -946,6 +946,15 @@ public byte[] Read(byte[] input)

public override float LayerHeight => HeaderSettings.LayerHeightMilimeter;

public override uint LayerCount
{
set
{
HeaderSettings.LayerCount = LayerCount;
HeaderSettings.OverallHeightMilimeter = TotalHeight;
}
}

public override ushort InitialLayerCount => (ushort)HeaderSettings.BottomLayersCount;

public override float InitialExposureTime => HeaderSettings.BottomExposureSeconds;
Expand Down Expand Up @@ -1137,6 +1146,8 @@ public override void Encode(string fileFullPath, OperationProgress progress = nu
outputFile.Seek(0, SeekOrigin.Begin);
Helpers.SerializeWriteFileStream(outputFile, HeaderSettings);

AfterEncode();

Debug.WriteLine("Encode Results:");
Debug.WriteLine(HeaderSettings);
Debug.WriteLine(Previews[0]);
Expand Down Expand Up @@ -1249,7 +1260,7 @@ public override void Decode(string fileFullPath, OperationProgress progress = nu
}
}

LayerManager = new LayerManager(HeaderSettings.LayerCount);
LayerManager = new LayerManager(HeaderSettings.LayerCount, this);

progress.Reset(OperationProgress.StatusDecodeLayers, LayerCount);

Expand Down Expand Up @@ -1392,7 +1403,7 @@ void UpdateLayers()

public override void SaveAs(string filePath = null, OperationProgress progress = null)
{
if (LayerManager.IsModified)
if (RequireFullEncode)
{
if (!string.IsNullOrEmpty(filePath))
{
Expand Down
45 changes: 29 additions & 16 deletions UVtools.Core/FileFormats/ChituboxZipFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ public class Header

public override float LayerHeight => HeaderSettings.LayerHeight;

public override uint LayerCount
{
set
{
HeaderSettings.LayerCount = LayerCount;
}
}

public override ushort InitialLayerCount => HeaderSettings.BottomLayerCount;

public override float InitialExposureTime => HeaderSettings.BottomLayerExposureTime;
Expand Down Expand Up @@ -164,14 +172,6 @@ public override void Encode(string fileFullPath, OperationProgress progress = nu
base.Encode(fileFullPath, progress);
using (ZipArchive outputFile = ZipFile.Open(fileFullPath, ZipArchiveMode.Create))
{
for(uint layerIndex = 0; layerIndex < LayerCount; layerIndex++)
{
progress.Token.ThrowIfCancellationRequested();
Layer layer = this[layerIndex];
outputFile.PutFileContent($"{layerIndex+1}.png", layer.CompressedBytes, ZipArchiveMode.Create);
progress++;
}

if (Thumbnails.Length > 0 && !ReferenceEquals(Thumbnails[0], null))
{
using (Stream stream = outputFile.CreateEntry("preview.png").Open())
Expand All @@ -196,7 +196,17 @@ public override void Encode(string fileFullPath, OperationProgress progress = nu

UpdateGCode();
outputFile.PutFileContent("run.gcode", GCode.ToString(), ZipArchiveMode.Create);

for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++)
{
progress.Token.ThrowIfCancellationRequested();
Layer layer = this[layerIndex];
outputFile.PutFileContent($"{layerIndex + 1}.png", layer.CompressedBytes, ZipArchiveMode.Create);
progress++;
}
}

AfterEncode();
}

public override void Decode(string fileFullPath, OperationProgress progress = null)
Expand Down Expand Up @@ -253,7 +263,7 @@ public override void Decode(string fileFullPath, OperationProgress progress = nu
}
}

LayerManager = new LayerManager(HeaderSettings.LayerCount);
LayerManager = new LayerManager(HeaderSettings.LayerCount, this);

progress.ItemCount = LayerCount;

Expand Down Expand Up @@ -433,6 +443,16 @@ void UpdateLayers()

public override void SaveAs(string filePath = null, OperationProgress progress = null)
{
if (RequireFullEncode)
{
if (!string.IsNullOrEmpty(filePath))
{
FileFullPath = filePath;
}
Encode(FileFullPath, progress);
return;
}

if (!string.IsNullOrEmpty(filePath))
{
File.Copy(FileFullPath, filePath, true);
Expand All @@ -451,13 +471,6 @@ public override void SaveAs(string filePath = null, OperationProgress progress =
}

outputFile.PutFileContent("run.gcode", GCode.ToString(), ZipArchiveMode.Update);

foreach (var layer in this)
{
if (!layer.IsModified) continue;
outputFile.PutFileContent(layer.Filename, layer.CompressedBytes, ZipArchiveMode.Update);
layer.IsModified = false;
}
}

//Decode(FileFullPath, progress);
Expand Down
28 changes: 18 additions & 10 deletions UVtools.Core/FileFormats/FileFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,16 @@ public byte CreatedThumbnailsCount {
public Mat[] Thumbnails { get; set; }
public LayerManager LayerManager { get; set; }

private bool _haveModifiedLayers;

/// <summary>
/// Gets if any layer got modified
/// Gets or sets if modifications require a full encode to save
/// </summary>
public bool ModifiedLayers => LayerManager.IsModified;
public bool RequireFullEncode
{
get => _haveModifiedLayers || LayerManager.IsModified;
set => _haveModifiedLayers = value;
} // => LayerManager.IsModified;

public abstract uint ResolutionX { get; }

Expand All @@ -277,8 +283,12 @@ public byte CreatedThumbnailsCount {

public float TotalHeight => LayerCount == 0 ? 0 : this[LayerCount - 1].PositionZ; //(float)Math.Round(LayerCount * LayerHeight, 2);

public uint LayerCount => LayerManager?.Count ?? 0;

public virtual uint LayerCount
{
get => LayerManager?.Count ?? 0;
set => throw new NotImplementedException();
}

public abstract ushort InitialLayerCount { get; }

public abstract float InitialExposureTime { get; }
Expand Down Expand Up @@ -464,21 +474,19 @@ public virtual void Encode(string fileFullPath, OperationProgress progress = nul
for (var i = 0; i < Thumbnails.Length; i++)
{
if (ReferenceEquals(Thumbnails[i], null)) continue;
if(Thumbnails[i].Size == ThumbnailsOriginalSize[i]) continue;
CvInvoke.Resize(Thumbnails[i], Thumbnails[i], new Size(ThumbnailsOriginalSize[i].Width, ThumbnailsOriginalSize[i].Height));
}

if(ReferenceEquals(progress, null)) progress = new OperationProgress();
progress.Reset(OperationProgress.StatusEncodeLayers, LayerCount);
}

/*public virtual void BeginEncode(string fileFullPath)
public void AfterEncode()
{
LayerManager.Desmodify();
RequireFullEncode = false;
}
public abstract void InsertLayerImageEncode(Image<L8> image, uint layerIndex);
public abstract void EndEncode();*/

public virtual void Decode(string fileFullPath, OperationProgress progress = null)
{
Expand Down
5 changes: 3 additions & 2 deletions UVtools.Core/FileFormats/IFileFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public interface IFileFormat
/// <summary>
/// Gets the number of layers present in this file
/// </summary>
uint LayerCount { get; }
uint LayerCount { get; set; }

/// <summary>
/// Gets the number of initial layer count
Expand Down Expand Up @@ -236,7 +236,8 @@ public interface IFileFormat
/// <param name="fileFullPath">Output file</param>
/// <param name="progress"></param>
void Encode(string fileFullPath, OperationProgress progress = null);

void AfterEncode();

/*
/// <summary>
/// Begin encode to an output file
Expand Down
2 changes: 1 addition & 1 deletion UVtools.Core/FileFormats/ImageFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public override void Decode(string fileFullPath, OperationProgress progress = nu
CvInvoke.CvtColor(ImageMat, ImageMat, ColorConversion.Bgr2Gray);
}

LayerManager = new LayerManager(1);
LayerManager = new LayerManager(1, this);
this[0] = new Layer(0, ImageMat, Path.GetFileName(fileFullPath));
}

Expand Down
15 changes: 13 additions & 2 deletions UVtools.Core/FileFormats/PHZFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,15 @@ public byte[] Read(byte[] input)

public override float LayerHeight => HeaderSettings.LayerHeightMilimeter;

public override uint LayerCount
{
set
{
HeaderSettings.LayerCount = LayerCount;
HeaderSettings.OverallHeightMilimeter = TotalHeight;
}
}

public override ushort InitialLayerCount => (ushort)HeaderSettings.BottomLayersCount;

public override float InitialExposureTime => HeaderSettings.BottomExposureSeconds;
Expand Down Expand Up @@ -880,6 +889,8 @@ public override void Encode(string fileFullPath, OperationProgress progress = nu
outputFile.Seek(0, SeekOrigin.Begin);
Helpers.SerializeWriteFileStream(outputFile, HeaderSettings);

AfterEncode();

Debug.WriteLine("Encode Results:");
Debug.WriteLine(HeaderSettings);
Debug.WriteLine(Previews[0]);
Expand Down Expand Up @@ -967,7 +978,7 @@ public override void Decode(string fileFullPath, OperationProgress progress = nu
progress.Token.ThrowIfCancellationRequested();
}

LayerManager = new LayerManager(HeaderSettings.LayerCount);
LayerManager = new LayerManager(HeaderSettings.LayerCount, this);

progress.Reset(OperationProgress.StatusDecodeLayers, HeaderSettings.LayerCount);

Expand Down Expand Up @@ -1107,7 +1118,7 @@ void UpdateLayers()

public override void SaveAs(string filePath = null, OperationProgress progress = null)
{
if (LayerManager.IsModified)
if (RequireFullEncode)
{
if (!string.IsNullOrEmpty(filePath))
{
Expand Down
Loading

0 comments on commit 97f0e38

Please sign in to comment.