Guest User

Untitled

a guest
Jan 21st, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.60 KB | None | 0 0
  1. 'Rotate stuff
  2.  
  3.  
  4. Dim tempColor As Color = Color.Red
  5. Function isSameColor(ByVal colorA As Color, ByVal colorB As Color)
  6. If colorA.R = colorB.R And colorA.G = colorB.G And colorA.B = colorB.B Then
  7. Return True
  8. End If
  9. Return False
  10. End Function
  11.  
  12. Sub fillOutside(ByRef img As Bitmap, Optional ByVal loc As Point = Nothing)
  13. If loc = Nothing Then
  14. loc = New Point(0, 0)
  15. End If
  16. If isSameColor(img.GetPixel(loc.X, loc.Y), bgColor) Then : img.SetPixel(loc.X, loc.Y, tempColor)
  17. Else : Exit Sub
  18. End If
  19. If loc.X > 0 Then : fillOutside(img, New Point(loc.X - 1, loc.Y))
  20. End If
  21. If loc.X < img.Width - 1 Then : fillOutside(img, New Point(loc.X + 1, loc.Y))
  22. End If
  23. If loc.Y > 0 Then : fillOutside(img, New Point(loc.X, loc.Y - 1))
  24. End If
  25. If loc.Y < img.Height - 1 Then : fillOutside(img, New Point(loc.X, loc.Y + 1))
  26. End If
  27. End Sub
  28.  
  29. Structure PolarPoint
  30. Public radius As Double
  31. Public theta As Double
  32. End Structure
  33.  
  34. Function toPolarPoint(ByVal curPoint As Point, ByVal centerPoint As Point) As PolarPoint
  35. Dim newX, newY As Integer
  36. newX = curPoint.X - centerPoint.X
  37. newY = centerPoint.Y - curPoint.Y 'Coordinate system have a y that increases upwards, but bitmaps have a y that increases downwards
  38. Dim r, t As Double
  39. r = Math.Sqrt(newX ^ 2 + newY ^ 2)
  40. If newX = 0 And newY = 0 Then : t = 0
  41. ElseIf newX = 0 And newY = 0 Then : t = 0
  42. ElseIf newX > 0 And newY = 0 Then : t = 0
  43. ElseIf newX < 0 And newY = 0 Then : t = Math.PI
  44. ElseIf newX = 0 And newY > 0 Then : t = Math.PI / 2
  45. ElseIf newX = 0 And newY < 0 Then : t = 3 * Math.PI / 2
  46. ElseIf newX > 0 And newY > 0 Then : t = Math.Atan(newY / newX)
  47. ElseIf newX < 0 And newY > 0 Then : t = (Math.PI) - Math.Atan(-newY / newX)
  48. ElseIf newX < 0 And newY < 0 Then : t = (Math.PI) + Math.Atan(newY / newX)
  49. ElseIf newX > 0 And newY < 0 Then : t = (2 * Math.PI) + Math.Atan(newY / newX)
  50. End If
  51. Dim tempPP As PolarPoint
  52. tempPP.radius = r
  53. tempPP.theta = t
  54. Return tempPP
  55. End Function
  56.  
  57. Function getOuterPixels(ByVal img As Bitmap) As Point()
  58.  
  59. Dim borderImg As New Bitmap(img.Width + 2, img.Height + 2)
  60. Dim borderG As Graphics = Graphics.FromImage(borderImg)
  61. borderG.FillRectangle(New SolidBrush(bgColor), 0, 0, borderImg.Width, borderImg.Height)
  62. borderG.DrawImage(img, 1, 1, img.Width, img.Height)
  63. Dim tempImg As Bitmap = borderImg
  64.  
  65. fillOutside(tempImg) 'Colors all the outside white area with tempColor pixels
  66. Dim outerPixels As New List(Of Point)
  67. For y As Integer = 0 To tempImg.Height - 1
  68. For x As Integer = 0 To tempImg.Width - 1
  69. If isSameColor(tempImg.GetPixel(x, y), charColor) Then 'Checks if a pixel is beside a tempColor pixel
  70. Dim notAdded As Boolean = True
  71. If x > 0 Then
  72. If isSameColor(tempImg.GetPixel(x - 1, y), tempColor) And notAdded Then
  73. outerPixels.Add(New Point(x, y))
  74. notAdded = False
  75. End If
  76. End If
  77. If x < tempImg.Width - 1 Then
  78. If isSameColor(tempImg.GetPixel(x + 1, y), tempColor) And notAdded Then
  79. outerPixels.Add(New Point(x, y))
  80. notAdded = False
  81. End If
  82. End If
  83. If y > 0 Then
  84. If isSameColor(tempImg.GetPixel(x, y - 1), tempColor) And notAdded Then
  85. outerPixels.Add(New Point(x, y))
  86. notAdded = False
  87. End If
  88. End If
  89. If y < tempImg.Height - 1 Then
  90. If isSameColor(tempImg.GetPixel(x, y + 1), tempColor) And notAdded Then
  91. outerPixels.Add(New Point(x, y))
  92. notAdded = False
  93. End If
  94. End If
  95. End If
  96. Next
  97. Next
  98. Return outerPixels.ToArray
  99. End Function
  100.  
  101. Function toCartesianPoint(ByVal curPoint As PolarPoint, ByVal centerPoint As Point) As Point
  102. Dim newX, newY As Integer
  103. newX = CInt(Math.Round(centerPoint.X + (curPoint.radius * Math.Cos(curPoint.theta))))
  104. newY = CInt(Math.Round(centerPoint.Y - (curPoint.radius * Math.Sin(curPoint.theta))))
  105. Return New Point(newX, newY)
  106. End Function
  107.  
  108. Function rotateImg(ByVal img As Bitmap) As Bitmap
  109. Dim input As Bitmap = img.Clone
  110.  
  111. 'Step 1: Get the minimum and maximum X coordinate
  112. Dim center As Point = New Point(CInt(Math.Floor(img.Width / 2)), CInt(Math.Floor(img.Height / 2)))
  113. Dim outerPixels As Point() = getOuterPixels(Input)
  114. Dim outerPixelsPolar(outerPixels.Count - 1) As PolarPoint
  115. For i As Integer = 0 To outerPixels.Count - 1
  116. outerPixelsPolar(i) = toPolarPoint(outerPixels(i), center)
  117. Next
  118. Dim minWidth As Integer = Math.Ceiling(Math.Sqrt(img.Width ^ 2 + img.Height ^ 2))
  119. Dim minTheta As Double = 0 'The corresponding value of theta in which the minWidth is reached
  120. For thetaOffset As Double = -Math.PI To Math.PI Step (Math.PI / 180)
  121. Dim minX As Integer = img.Width
  122. Dim maxX As Integer = -img.Width
  123. For i As Integer = 0 To outerPixelsPolar.Count - 1
  124. Dim tempWidth As Integer = CInt(Math.Round(outerPixelsPolar(i).radius * Math.Cos(outerPixelsPolar(i).theta + thetaOffset)))
  125. minX = Math.Min(tempWidth, minX)
  126. maxX = Math.Max(tempWidth, maxX)
  127. Next
  128. Dim finalWidth As Integer = (-minX) + 1 + maxX
  129. If finalWidth < minWidth Then
  130. minWidth = finalWidth
  131. minTheta = thetaOffset
  132. End If
  133. Next
  134.  
  135. 'Get points on four corners
  136. Dim cornerPoints As Point() = {New Point(0, 0), New Point(img.Width, 0), New Point(0, img.Height), New Point(img.Width, img.Height)}
  137. 'Randomize angle
  138. Dim angle As Decimal = minTheta * Math.PI / 180
  139. 'Shifts the points to make rotation axis about the middle
  140. Dim xHalf As Single = img.Width / 2
  141. Dim yHalf As Single = img.Height / 2
  142. For i As Integer = 0 To cornerPoints.Length - 1
  143. cornerPoints(i).X -= xHalf
  144. cornerPoints(i).Y -= yHalf
  145. Next i
  146. 'Editing corners to new location based on the angle
  147. Dim sin As Single = Math.Sin(angle)
  148. Dim cos As Single = Math.Cos(angle)
  149. Dim myX, myY As Single
  150. For i As Integer = 0 To 3
  151. myX = cornerPoints(i).X
  152. myY = cornerPoints(i).Y
  153. cornerPoints(i).X = myX * cos + myY * sin
  154. cornerPoints(i).Y = -myX * sin + myY * cos
  155. Next i
  156. 'Shift the points again back to original offset
  157. For i As Integer = 0 To 3
  158. cornerPoints(i).X += xHalf
  159. cornerPoints(i).Y += yHalf
  160. Next i
  161. 'Output
  162. Dim output As New Bitmap(img.Width, img.Height)
  163. Dim g As Graphics = Graphics.FromImage(output)
  164. 'Remove last corner: Only 3 needed for DrawImage
  165. ReDim Preserve cornerPoints(2)
  166. g.DrawImage(img, cornerPoints)
  167. Return output
  168.  
  169. End Function
Add Comment
Please, Sign In to add comment