forked from IcemanF1/ALTTPRCropDashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCropperMath.vb
107 lines (84 loc) · 5.23 KB
/
CropperMath.vb
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
Public Class CropperMath
Public Property DefaultCrop As New Rectangle
Public Function RemoveDefaultCrop(rect As Rectangle) As Rectangle
Return Rectangle.FromLTRB(Math.Max(rect.Left - DefaultCrop.Left, 0),
Math.Max(rect.Top - DefaultCrop.Top, 0),
Math.Max(rect.Right - DefaultCrop.Right, 0),
Math.Max(rect.Bottom - DefaultCrop.Bottom, 0)
)
End Function
Public Function RemoveDefaultCropSize(size As Size) As Size
Return New Size(Math.Max(size.Width - DefaultCrop.Left - DefaultCrop.Right, 0),
Math.Max(size.Height - DefaultCrop.Top - DefaultCrop.Bottom, 0))
End Function
Public Function AddDefaultCrop(rect As Rectangle) As Rectangle
Return Rectangle.FromLTRB(rect.Left + DefaultCrop.Left, rect.Top + DefaultCrop.Top, rect.Right + DefaultCrop.Right, rect.Bottom + DefaultCrop.Bottom)
End Function
Public Function RemoveScaling(rect As Rectangle, size As Size, scaling As Double) As Rectangle
If scaling = 1 Or scaling = 0 Then
Return rect
End If
Dim changedSize = New Size(CInt(size.Width - size.Width / scaling), CInt(size.Height - size.Height / scaling))
Return Rectangle.FromLTRB(rect.Left, rect.Top, rect.Right - changedSize.Width, rect.Bottom - changedSize.Height)
End Function
Public Function RemoveScaling(size As Size, scaling As Double) As Size
If scaling = 1 Or scaling = 0 Then
Return size
End If
Return New Size(CInt(size.Width / scaling), CInt(size.Height / scaling))
End Function
Public Function AddScaling(rect As Rectangle, size As Size, scaling As Double) As Rectangle
If scaling = 1 Or scaling = 0 Then
Return rect
End If
Dim changedSize = New Size(CInt(size.Width - size.Width / scaling), CInt(size.Height - size.Height / scaling))
Return Rectangle.FromLTRB(rect.Left, rect.Top, rect.Right + changedSize.Width, rect.Bottom + changedSize.Height)
End Function
Public Function AddScaling(size As Size, scaling As Double) As Size
If scaling = 1 Or scaling = 0 Then
Return size
End If
Return New Size(CInt(size.Width * scaling), CInt(size.Height * scaling))
End Function
Public Function AddDefaultCropSize(size As Size) As Size
Return New Size(size.Width + DefaultCrop.Left + DefaultCrop.Right,
size.Height + DefaultCrop.Top + DefaultCrop.Bottom)
End Function
Public Function AdjustCrop(crop As CropInfo, newMasterSizeWithoutDefault As Size) As CropInfo
'Let's calculate how much we differ from the original source
Dim aspectRatioScale As New PointF(CSng(newMasterSizeWithoutDefault.Width / crop.MasterSizeWithoutDefault.Width), CSng(newMasterSizeWithoutDefault.Height / crop.MasterSizeWithoutDefault.Height))
'From that, we take the smaller ratio
Dim aspectRatioChange = Math.Min(aspectRatioScale.X, aspectRatioScale.Y)
'Then by scaling the original, we get the size of the video when scaled up.
Dim scaledMasterSize As New Size(CInt(crop.MasterSizeWithoutDefault.Width * aspectRatioChange),
CInt(crop.MasterSizeWithoutDefault.Height * aspectRatioChange))
'And the remaining space is black bars.
Dim blackBarsSize As New Size(newMasterSizeWithoutDefault.Width - scaledMasterSize.Width,
newMasterSizeWithoutDefault.Height - scaledMasterSize.Height)
'These black bars are around the source. On non-even sizes, 1px bigger on one side.
Dim blackBars = Rectangle.FromLTRB(CInt(Math.Ceiling(blackBarsSize.Width / 2.0)),
CInt(Math.Ceiling(blackBarsSize.Height / 2.0)),
CInt(Math.Floor(blackBarsSize.Width / 2.0)),
CInt(Math.Floor(blackBarsSize.Height / 2.0)))
'Now we also scale the crop numbers, since they now refer to a much bigger stream.
Dim scaledCrop = Rectangle.FromLTRB(CInt(crop.CropWithoutDefault.Left * aspectRatioChange),
CInt(crop.CropWithoutDefault.Top * aspectRatioChange),
CInt(crop.CropWithoutDefault.Right * aspectRatioChange),
CInt(crop.CropWithoutDefault.Bottom * aspectRatioChange))
Return New CropInfo With {
.CropWithoutDefault = scaledCrop,
.BlackBars = blackBars,
.MasterSizeWithoutDefault = scaledMasterSize
}
End Function
End Class
Public Class CropInfo
Public Property CropWithoutDefault As Rectangle
Public Property BlackBars As New Rectangle
Public Property MasterSizeWithoutDefault As Size
Public ReadOnly Property CropWithBlackBarsWithoutDefault As Rectangle
Get
Return Rectangle.FromLTRB(CropWithoutDefault.Left + BlackBars.Left, CropWithoutDefault.Top + BlackBars.Top, CropWithoutDefault.Right + BlackBars.Right, CropWithoutDefault.Bottom + BlackBars.Bottom)
End Get
End Property
End Class