1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
/*
* 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.
*/
// https://github.com/cbiffle/catibo/blob/master/doc/cbddlp-ctb.adoc
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using BinarySerialization;
using Emgu.CV;
using Emgu.CV.CvEnum;
using UVtools.Core.Extensions;
using UVtools.Core.Operations;
namespace UVtools.Core.FileFormats
{
public class MDLPFile : FileFormat
{
#region Constants
private const uint SlicerInfoAddress = 4 + 7 + 290 * 290 * 2 + 116 * 116 * 2 + 4;
#endregion
#region Sub Classes
#region Header
public sealed class Header
{
public const byte HEADER_SIZE = 7;
public const string HEADER_VALUE = "MKSDLP";
[FieldOrder(0)]
[FieldEndianness(Endianness.Big)]
public uint HeaderSize { get; set; } = HEADER_SIZE;
/// <summary>
/// Gets the file tag = MKSDLP
/// </summary>
[FieldOrder(1)]
[FieldLength(HEADER_SIZE)]
[SerializeAs(SerializedType.TerminatedString)]
public string HeaderValue { get; set; } = HEADER_VALUE;
public void Validate()
{
if (HeaderSize != HEADER_SIZE || HeaderValue != HEADER_VALUE)
{
throw new FileLoadException("Not a valid Makerbase mdlp file!");
}
}
}
public sealed class SlicerInfo
{
// 290 * 290 * 2 + 116 * 116 * 2 + 4
//[FieldOrder(0)] [FieldLength(195116)] public byte[] PreviewData { get; set; }
[FieldOrder(0)] [FieldEndianness(Endianness.Big)] public ushort LayerCount { get; set; }
[FieldOrder(1)] [FieldEndianness(Endianness.Big)] public ushort ResolutionX { get; set; }
[FieldOrder(2)] [FieldEndianness(Endianness.Big)] public ushort ResolutionY { get; set; }
[FieldOrder(3)] [FieldEndianness(Endianness.Big)] public uint DisplayWidthDataSize { get; set; } = 6;
[FieldOrder(4)] [FieldLength(nameof(DisplayWidthDataSize))] public byte[] DisplayWidth { get; set; }
[FieldOrder(5)] [FieldEndianness(Endianness.Big)] public uint DisplayHeightDataSize { get; set; } = 6;
[FieldOrder(6)] [FieldLength(nameof(DisplayHeightDataSize))] public byte[] DisplayHeight { get; set; }
[FieldOrder(7)] [FieldEndianness(Endianness.Big)] public uint LayerHeightDataSize { get; set; } = 6;
[FieldOrder(8)] [FieldLength(nameof(LayerHeightDataSize))] public byte[] LayerHeight { get; set; }
[FieldOrder(9)] [FieldEndianness(Endianness.Big)] public ushort ExposureTime { get; set; }
[FieldOrder(10)] [FieldEndianness(Endianness.Big)] public ushort LightOffDelay { get; set; }
[FieldOrder(11)] [FieldEndianness(Endianness.Big)] public ushort BottomExposureTime { get; set; }
[FieldOrder(12)] [FieldEndianness(Endianness.Big)] public ushort BottomLayers { get; set; }
/* [FieldOrder(13)] [FieldEndianness(Endianness.Big)] public ushort BottomLiftHeight { get; set; }
[FieldOrder(14)] [FieldEndianness(Endianness.Big)] public ushort BottomLiftSpeed { get; set; }
[FieldOrder(15)] [FieldEndianness(Endianness.Big)] public ushort LiftHeight { get; set; }
[FieldOrder(16)] [FieldEndianness(Endianness.Big)] public ushort LiftSpeed { get; set; }
[FieldOrder(17)] [FieldEndianness(Endianness.Big)] public ushort RetractSpeed { get; set; }
[FieldOrder(18)] [FieldEndianness(Endianness.Big)] public ushort BottomLightPWM { get; set; }
[FieldOrder(19)] [FieldEndianness(Endianness.Big)] public ushort LightPWM { get; set; }*/
}
#endregion
#region LayerDef
public sealed class LayerDef
{
[FieldOrder(0)] [FieldEndianness(Endianness.Big)] public uint LineCount { get; set; }
[FieldOrder(1)] [FieldCount(nameof(LineCount))] public LayerLine[] Lines { get; set; }
[FieldOrder(2)] public PageBreak PageBreak { get; set; } = new();
public LayerDef(uint lineCount, LayerLine[] lines)
{
LineCount = lineCount;
Lines = lines;
}
}
public sealed class LayerLine
{
[FieldOrder(0)] [FieldEndianness(Endianness.Big)] public ushort StartY { get; set; }
[FieldOrder(1)] [FieldEndianness(Endianness.Big)] public ushort EndY { get; set; }
[FieldOrder(2)] [FieldEndianness(Endianness.Big)] public ushort StartX { get; set; }
public LayerLine()
{ }
public LayerLine(ushort startY, ushort endY, ushort startX)
{
StartY = startY;
EndY = endY;
StartX = startX;
}
}
public sealed class PageBreak
{
[FieldOrder(0)] [FieldEndianness(Endianness.Big)] public byte Line { get; set; } = 0x0D;
[FieldOrder(1)] [FieldEndianness(Endianness.Big)] public byte Break { get; set; } = 0x0A;
}
#endregion
#endregion
#region Properties
public Header HeaderSettings { get; protected internal set; } = new();
public SlicerInfo SlicerInfoSettings { get; protected internal set; } = new();
public override FileFormatType FileType => FileFormatType.Binary;
public override FileExtension[] FileExtensions { get; } = {
new ("mdlp", "Makerbase MDLP v1"),
};
public override PrintParameterModifier[] PrintParameterModifiers { get; } =
{
PrintParameterModifier.BottomLayerCount,
PrintParameterModifier.BottomExposureSeconds,
PrintParameterModifier.ExposureSeconds,
//PrintParameterModifier.BottomLightOffDelay,
PrintParameterModifier.LightOffDelay,
/*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)
};
public override uint ResolutionX
{
get => SlicerInfoSettings.ResolutionX;
set
{
SlicerInfoSettings.ResolutionX = (ushort) value;
RaisePropertyChanged();
}
}
public override uint ResolutionY
{
get => SlicerInfoSettings.ResolutionY;
set
{
SlicerInfoSettings.ResolutionY = (ushort) value;
RaisePropertyChanged();
}
}
public override float DisplayWidth
{
get => float.Parse(SlicerInfoSettings.DisplayWidth.Where(b => b != 0).Aggregate(string.Empty, (current, b) => current + System.Convert.ToChar(b)));
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] = System.Convert.ToByte(str[i]);
}
SlicerInfoSettings.DisplayWidth = data;
RaisePropertyChanged();
}
}
public override float DisplayHeight
{
get => float.Parse(SlicerInfoSettings.DisplayHeight.Where(b => b != 0).Aggregate(string.Empty, (current, b) => current + System.Convert.ToChar(b)));
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] = System.Convert.ToByte(str[i]);
}
SlicerInfoSettings.DisplayHeight = data;
RaisePropertyChanged();
}
}
public override bool MirrorDisplay { get; set; }
public override byte AntiAliasing
{
get => 1;
set { }
}
public override float LayerHeight
{
get => float.Parse(SlicerInfoSettings.LayerHeight.Where(b => b != 0).Aggregate(string.Empty, (current, b) => current + System.Convert.ToChar(b)));
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] = System.Convert.ToByte(str[i]);
}
SlicerInfoSettings.LayerHeight = data;
RaisePropertyChanged();
}
}
public override uint LayerCount
{
get => base.LayerCount;
set => base.LayerCount = SlicerInfoSettings.LayerCount = (ushort)base.LayerCount;
}
public override ushort BottomLayerCount
{
get => SlicerInfoSettings.BottomLayers;
set => base.BottomLayerCount = SlicerInfoSettings.BottomLayers = 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 BottomLightOffDelay
{
get => LightOffDelay;
set { }
}
public override float LightOffDelay
{
get => SlicerInfoSettings.LightOffDelay;
set => base.LightOffDelay = SlicerInfoSettings.LightOffDelay = (ushort)value;
}
public override object[] Configs => new[] { (object)HeaderSettings, SlicerInfoSettings };
#endregion
#region Constructors
#endregion
#region Methods
protected override void EncodeInternally(string fileFullPath, OperationProgress progress)
{
using var outputFile = new FileStream(fileFullPath, FileMode.Create, FileAccess.Write);
var pageBreak = new PageBreak();
Helpers.SerializeWriteFileStream(outputFile, HeaderSettings);
byte[][] previews = new byte[2][];
for (int i = 0; i < ThumbnailsOriginalSize.Length; i++)
{
previews[i] = new byte[ThumbnailsOriginalSize[i].Area() * 2];
}
// Previews
Parallel.For(0, previews.Length, previewIndex =>
{
if (progress.Token.IsCancellationRequested) return;
if (Thumbnails[previewIndex] is null) return;
var span = Thumbnails[previewIndex].GetPixelSpanByte();
int index = 0;
for (int i = 0; i < span.Length; i += 3)
{
byte b = span[i];
byte g = span[i + 1];
byte r = span[i + 2];
ushort rgb15 = (ushort)(((r >> 3) << 11) | ((g >> 2) << 5) | ((b >> 3) << 0));
previews[previewIndex][index++] = (byte)(rgb15 >> 8);
previews[previewIndex][index++] = (byte)(rgb15 & 0xff);
}
if (index != previews[previewIndex].Length)
{
throw new FileLoadException($"Preview encode incomplete encode, expected: {previews[previewIndex].Length}, encoded: {index}");
}
});
for (int i = 0; i < ThumbnailsOriginalSize.Length; i++)
{
Helpers.SerializeWriteFileStream(outputFile, previews[i]);
Helpers.SerializeWriteFileStream(outputFile, pageBreak);
}
Helpers.SerializeWriteFileStream(outputFile, SlicerInfoSettings);
progress.Reset(OperationProgress.StatusEncodeLayers, LayerCount);
var layerDefs = new LayerDef[LayerCount];
Parallel.For(0, LayerCount, layerIndex =>
{
if (progress.Token.IsCancellationRequested) return;
List<LayerLine> layerLines = new();
var layer = this[layerIndex];
using var mat = layer.LayerMat;
var span = mat.GetPixelSpanByte();
for (int x = layer.BoundingRectangle.X; x < layer.BoundingRectangle.Right; x++)
{
int y = layer.BoundingRectangle.Y;
int startY = -1;
for (; y < layer.BoundingRectangle.Bottom; y++)
{
int pos = mat.GetPixelPos(x, y);
if (span[pos] < 128) // Black pixel
{
if(startY == -1) continue; // Keep ignoring
layerLines.Add(new LayerLine((ushort) startY, (ushort) (y-1), (ushort) x));
startY = -1;
}
else // White pixel
{
if (startY >= 0) continue; // Keep sum
startY = y;
}
}
if (startY >= 0)
{
layerLines.Add(new LayerLine((ushort)startY, (ushort)(y - 1), (ushort) x));
}
}
layerDefs[layerIndex] = new LayerDef((uint) layerLines.Count, layerLines.ToArray());
progress.LockAndIncrement();
});
progress.Reset(OperationProgress.StatusWritingFile, LayerCount);
for (int layerIndex = 0; layerIndex < LayerCount; layerIndex++)
{
progress.Token.ThrowIfCancellationRequested();
Helpers.SerializeWriteFileStream(outputFile, layerDefs[layerIndex]);
progress++;
}
Helpers.SerializeWriteFileStream(outputFile, HeaderSettings);
Debug.WriteLine("Encode Results:");
Debug.WriteLine(HeaderSettings);
Debug.WriteLine("-End-");
}
protected override void DecodeInternally(string fileFullPath, OperationProgress progress)
{
using var inputFile = new FileStream(fileFullPath, FileMode.Open, FileAccess.Read);
//HeaderSettings = Helpers.ByteToType<CbddlpFile.Header>(InputFile);
//HeaderSettings = Helpers.Serializer.Deserialize<Header>(InputFile.ReadBytes(Helpers.Serializer.SizeOf(typeof(Header))));
HeaderSettings = Helpers.Deserialize<Header>(inputFile);
HeaderSettings.Validate();
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, previewIndex =>
{
var mat = new Mat(ThumbnailsOriginalSize[previewIndex], DepthType.Cv8U, 3);
var span = mat.GetPixelSpanByte();
int spanIndex = 0;
for (int i = 0; i < previews[previewIndex].Length; i += 2)
{
ushort rgb15 = (ushort)((ushort)(previews[previewIndex][i + 0] << 8) | previews[previewIndex][i + 1]);
byte r = (byte)((rgb15 >> 11) << 3);
byte g = (byte)((rgb15 >> 5) << 2);
byte b = (byte)((rgb15 >> 0) << 3);
span[spanIndex++] = b;
span[spanIndex++] = g;
span[spanIndex++] = r;
}
Thumbnails[previewIndex] = mat;
});
SlicerInfoSettings = Helpers.Deserialize<SlicerInfo>(inputFile);
LayerManager.Init(SlicerInfoSettings.LayerCount);
progress.ItemCount = LayerCount;
LayerDef[] layerDefs = new LayerDef[LayerCount];
for (int layerIndex = 0; layerIndex < LayerCount; layerIndex++)
{
progress.Token.ThrowIfCancellationRequested();
layerDefs[layerIndex] = Helpers.Deserialize<LayerDef>(inputFile);
progress++;
}
progress.Reset(OperationProgress.StatusDecodeLayers, LayerCount);
Parallel.For(0, LayerCount, layerIndex =>
{
if (progress.Token.IsCancellationRequested) return;
using var mat = EmguExtensions.InitMat(Resolution);
foreach (var line in layerDefs[layerIndex].Lines)
{
CvInvoke.Line(mat, new Point(line.StartX, line.StartY), new Point(line.StartX, line.EndY), EmguExtensions.WhiteByte);
}
this[layerIndex] = new Layer((uint) layerIndex, mat, this);
progress.LockAndIncrement();
});
HeaderSettings = Helpers.Deserialize<Header>(inputFile);
HeaderSettings.Validate();
}
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);
FileFullPath = filePath;
}
using var outputFile = new FileStream(FileFullPath, FileMode.Open, FileAccess.Write);
outputFile.Seek(SlicerInfoAddress, SeekOrigin.Begin);
Helpers.SerializeWriteFileStream(outputFile, SlicerInfoSettings);
}
#endregion
}
}
|