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
|
/*
* 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 System;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV;
using UVtools.Core.Extensions;
using UVtools.Core.FileFormats;
namespace UVtools.Core.Operations
{
[Serializable]
public sealed class OperationChangeResolution : Operation
{
#region Members
private uint _newResolutionX;
private uint _newResolutionY;
private bool _fixRatio;
#endregion
#region Subclasses
public class Resolution
{
public uint ResolutionX { get; }
public uint ResolutionY { get; }
public string Name { get; }
public bool IsEmpty => ResolutionX == 0 && ResolutionY == 0;
public Resolution(uint resolutionX, uint resolutionY, string name = null)
{
ResolutionX = resolutionX;
ResolutionY = resolutionY;
Name = name;
}
public override string ToString()
{
if(IsEmpty) return string.Empty;
var str = $"{ResolutionX} x {ResolutionY}";
if (!string.IsNullOrEmpty(Name))
{
str += $" ({Name})";
}
return str;
}
}
#endregion
#region Overrides
public override Enumerations.LayerRangeSelection StartLayerRangeSelection => Enumerations.LayerRangeSelection.None;
public override bool CanROI => false;
public override string Title => "Change print resolution";
public override string Description =>
"Crops or resizes all layer images to fit an alternate print resolution.\n" +
"Useful to make files printable on a different printer than they were originally sliced for without the need to re-slice.\n\n" +
"NOTE: Please ensure that the actual model will fit within the new print resolution. The operation will be aborted if it will result in any of the actual model being clipped.\n" +
"Only use this tool if both source and target printer have the same pixel pitch spec, otherwise the model size will be invalidated and result in a different size than the originally sliced for. " +
"As alternative is possible to resize the model to match the new pixel pitch.";
public override string ConfirmationText =>
"change print resolution " +
$"from {SlicerFile.ResolutionX}x{SlicerFile.ResolutionY} " +
$"to {NewResolutionX}x{NewResolutionY}?";
public override string ProgressTitle =>
$"Changing print resolution from ({SlicerFile.ResolutionX}x{SlicerFile.ResolutionY}) to ({NewResolutionX}x{NewResolutionY})";
public override string ProgressAction => "Changed layers";
public override string ValidateInternally()
{
var sb = new StringBuilder();
if (SlicerFile.ResolutionX == NewResolutionX && SlicerFile.ResolutionY == NewResolutionY)
{
sb.AppendLine($"The new resolution must be different from current resolution ({SlicerFile.ResolutionX} x {SlicerFile.ResolutionY}).");
}
var finalBoundsWidth = FinalBoundsWidth;
var finalBoundsHeight = FinalBoundsHeight;
if (NewResolutionX < finalBoundsWidth || NewResolutionY < finalBoundsHeight)
{
sb.AppendLine($"The new resolution ({NewResolutionX} x {NewResolutionY}) is not large enough to hold the model volume ({finalBoundsWidth} x {finalBoundsHeight}), continuing operation would clip the model.");
sb.AppendLine("To fix this, try to rotate the object and/or resize to fit on this new resolution.");
}
return sb.ToString();
}
public override string ToString()
{
var result = $"{_newResolutionX} x {_newResolutionY} [Fix ratio: {_fixRatio}]";
if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
return result;
}
#endregion
#region Properties
public uint NewResolutionX
{
get => _newResolutionX;
set
{
if(!RaiseAndSetIfChanged(ref _newResolutionX, value)) return;
RaisePropertyChanged(nameof(NewRatioX));
RaisePropertyChanged(nameof(NewFixedRatioX));
RaisePropertyChanged(nameof(FinalBoundsWidth));
}
}
public uint NewResolutionY
{
get => _newResolutionY;
set
{
RaiseAndSetIfChanged(ref _newResolutionY, value);
RaisePropertyChanged(nameof(NewRatioY));
RaisePropertyChanged(nameof(NewFixedRatioY));
RaisePropertyChanged(nameof(FinalBoundsHeight));
}
}
public double NewRatioX => Math.Round((double)SlicerFile.ResolutionX / _newResolutionX, 2);
public double NewRatioY => Math.Round((double)SlicerFile.ResolutionY / _newResolutionY, 2);
public double NewFixedRatioX => Math.Round((double)_newResolutionX / SlicerFile.ResolutionX, 2);
public double NewFixedRatioY => Math.Round((double)_newResolutionY / SlicerFile.ResolutionY, 2);
public bool FixRatio
{
get => _fixRatio;
set
{
if(!RaiseAndSetIfChanged(ref _fixRatio, value)) return;
RaisePropertyChanged(nameof(FinalBoundsWidth));
RaisePropertyChanged(nameof(FinalBoundsHeight));
}
}
public uint FinalBoundsWidth => (uint)(_fixRatio ? SlicerFile.BoundingRectangle.Width * NewFixedRatioX : SlicerFile.BoundingRectangle.Width);
public uint FinalBoundsHeight => (uint)(_fixRatio ? SlicerFile.BoundingRectangle.Height * NewFixedRatioY : SlicerFile.BoundingRectangle.Height);
#endregion
#region Constructor
public OperationChangeResolution() { }
public OperationChangeResolution(FileFormat slicerFile) : base(slicerFile)
{ }
public override void InitWithSlicerFile()
{
base.InitWithSlicerFile();
if(_newResolutionX <= 0) _newResolutionX = SlicerFile.ResolutionX;
if(_newResolutionY <= 0) _newResolutionY = SlicerFile.ResolutionY;
}
#endregion
#region Methods
public static Resolution[] GetResolutions()
{
return new [] {
//new Resolution(0, 0, string.Empty),
new Resolution(854, 480, "FWVGA"),
new Resolution(960, 1708),
new Resolution(1080, 1920, "FHD"),
new Resolution(1440, 2560, "QHD"),
new Resolution(1600, 2560, "WQXGA"),
new Resolution(1620, 2560, "WQXGA"),
new Resolution(1920, 1080, "FHD"),
new Resolution(2160, 3840, "4K UHD"),
new Resolution(2531, 1410, "QHD"),
new Resolution(2560, 1440, "QHD"),
new Resolution(2560, 1600, "WQXGA"),
new Resolution(2560, 1620, "WQXGA"),
new Resolution(3840, 2160, "4K UHD"),
new Resolution(3840, 2400, "WQUXGA"),
new Resolution(4920, 2880, "5K UHD"),
new Resolution(5448, 3064, "6K"),
new Resolution(7680, 4320, "8K UHD"),
};
}
public static Resolution[] Presets => GetResolutions();
protected override bool ExecuteInternally(OperationProgress progress)
{
progress.ItemCount = SlicerFile.LayerCount;
var newSize = new Size((int) NewResolutionX, (int) NewResolutionY);
var finalBoundsWidth = FinalBoundsWidth;
var finalBoundsHeight = FinalBoundsHeight;
var finalBounds = new Size((int)finalBoundsWidth, (int)finalBoundsHeight);
Parallel.For(0, SlicerFile.LayerCount, CoreSettings.ParallelOptions, layerIndex =>
{
if (progress.Token.IsCancellationRequested) return;
using var mat = SlicerFile[layerIndex].LayerMat;
if (mat.Size != newSize)
{
using var matDst = EmguExtensions.InitMat(newSize);
var newFixedRatioX = NewFixedRatioX;
var newFixedRatioY = NewFixedRatioY;
if (_fixRatio && (newFixedRatioX != 1.0 || newFixedRatioY != 1.0))
{
CvInvoke.Resize(mat, mat, SlicerFile.Resolution.Multiply(newFixedRatioX, newFixedRatioY));
}
mat.CopyCenterToCenter(finalBounds, matDst);
SlicerFile[layerIndex].LayerMat = matDst;
}
progress.LockAndIncrement();
});
progress.Token.ThrowIfCancellationRequested();
SlicerFile.ResolutionX = NewResolutionX;
SlicerFile.ResolutionY = NewResolutionY;
return !progress.Token.IsCancellationRequested;
}
/*public override bool Execute(Mat mat, params object[] arguments)
{
//mat.Transform(1, 1, newResolutionX-mat.Width+roi.Width, newResolutionY-mat.Height - roi.Height/2, new Size((int) newResolutionX, (int) newResolutionY));
using var matRoi = new Mat(mat, VolumeBonds);
using var matDst = new Mat(new Size((int)NewResolutionX, (int)NewResolutionY), mat.Depth, mat.NumberOfChannels);
using var matDstRoi = new Mat(matDst,
new Rectangle((int)(NewResolutionX / 2 - VolumeBonds.Width / 2),
(int)NewResolutionY / 2 - VolumeBonds.Height / 2,
VolumeBonds.Width, VolumeBonds.Height));
matRoi.CopyTo(matDstRoi);
return true;
}*/
#endregion
#region Equality
private bool Equals(OperationChangeResolution other)
{
return _newResolutionX == other._newResolutionX && _newResolutionY == other._newResolutionY && _fixRatio == other._fixRatio;
}
public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) || obj is OperationChangeResolution other && Equals(other);
}
public override int GetHashCode()
{
return HashCode.Combine(_newResolutionX, _newResolutionY, _fixRatio);
}
#endregion
}
}
|