Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'Rotate stuff
- Dim tempColor As Color = Color.Red
- Function isSameColor(ByVal colorA As Color, ByVal colorB As Color)
- If colorA.R = colorB.R And colorA.G = colorB.G And colorA.B = colorB.B Then
- Return True
- End If
- Return False
- End Function
- Sub fillOutside(ByRef img As Bitmap, Optional ByVal loc As Point = Nothing)
- If loc = Nothing Then
- loc = New Point(0, 0)
- End If
- If isSameColor(img.GetPixel(loc.X, loc.Y), bgColor) Then : img.SetPixel(loc.X, loc.Y, tempColor)
- Else : Exit Sub
- End If
- If loc.X > 0 Then : fillOutside(img, New Point(loc.X - 1, loc.Y))
- End If
- If loc.X < img.Width - 1 Then : fillOutside(img, New Point(loc.X + 1, loc.Y))
- End If
- If loc.Y > 0 Then : fillOutside(img, New Point(loc.X, loc.Y - 1))
- End If
- If loc.Y < img.Height - 1 Then : fillOutside(img, New Point(loc.X, loc.Y + 1))
- End If
- End Sub
- Structure PolarPoint
- Public radius As Double
- Public theta As Double
- End Structure
- Function toPolarPoint(ByVal curPoint As Point, ByVal centerPoint As Point) As PolarPoint
- Dim newX, newY As Integer
- newX = curPoint.X - centerPoint.X
- newY = centerPoint.Y - curPoint.Y 'Coordinate system have a y that increases upwards, but bitmaps have a y that increases downwards
- Dim r, t As Double
- r = Math.Sqrt(newX ^ 2 + newY ^ 2)
- If newX = 0 And newY = 0 Then : t = 0
- ElseIf newX = 0 And newY = 0 Then : t = 0
- ElseIf newX > 0 And newY = 0 Then : t = 0
- ElseIf newX < 0 And newY = 0 Then : t = Math.PI
- ElseIf newX = 0 And newY > 0 Then : t = Math.PI / 2
- ElseIf newX = 0 And newY < 0 Then : t = 3 * Math.PI / 2
- ElseIf newX > 0 And newY > 0 Then : t = Math.Atan(newY / newX)
- ElseIf newX < 0 And newY > 0 Then : t = (Math.PI) - Math.Atan(-newY / newX)
- ElseIf newX < 0 And newY < 0 Then : t = (Math.PI) + Math.Atan(newY / newX)
- ElseIf newX > 0 And newY < 0 Then : t = (2 * Math.PI) + Math.Atan(newY / newX)
- End If
- Dim tempPP As PolarPoint
- tempPP.radius = r
- tempPP.theta = t
- Return tempPP
- End Function
- Function getOuterPixels(ByVal img As Bitmap) As Point()
- Dim borderImg As New Bitmap(img.Width + 2, img.Height + 2)
- Dim borderG As Graphics = Graphics.FromImage(borderImg)
- borderG.FillRectangle(New SolidBrush(bgColor), 0, 0, borderImg.Width, borderImg.Height)
- borderG.DrawImage(img, 1, 1, img.Width, img.Height)
- Dim tempImg As Bitmap = borderImg
- fillOutside(tempImg) 'Colors all the outside white area with tempColor pixels
- Dim outerPixels As New List(Of Point)
- For y As Integer = 0 To tempImg.Height - 1
- For x As Integer = 0 To tempImg.Width - 1
- If isSameColor(tempImg.GetPixel(x, y), charColor) Then 'Checks if a pixel is beside a tempColor pixel
- Dim notAdded As Boolean = True
- If x > 0 Then
- If isSameColor(tempImg.GetPixel(x - 1, y), tempColor) And notAdded Then
- outerPixels.Add(New Point(x, y))
- notAdded = False
- End If
- End If
- If x < tempImg.Width - 1 Then
- If isSameColor(tempImg.GetPixel(x + 1, y), tempColor) And notAdded Then
- outerPixels.Add(New Point(x, y))
- notAdded = False
- End If
- End If
- If y > 0 Then
- If isSameColor(tempImg.GetPixel(x, y - 1), tempColor) And notAdded Then
- outerPixels.Add(New Point(x, y))
- notAdded = False
- End If
- End If
- If y < tempImg.Height - 1 Then
- If isSameColor(tempImg.GetPixel(x, y + 1), tempColor) And notAdded Then
- outerPixels.Add(New Point(x, y))
- notAdded = False
- End If
- End If
- End If
- Next
- Next
- Return outerPixels.ToArray
- End Function
- Function toCartesianPoint(ByVal curPoint As PolarPoint, ByVal centerPoint As Point) As Point
- Dim newX, newY As Integer
- newX = CInt(Math.Round(centerPoint.X + (curPoint.radius * Math.Cos(curPoint.theta))))
- newY = CInt(Math.Round(centerPoint.Y - (curPoint.radius * Math.Sin(curPoint.theta))))
- Return New Point(newX, newY)
- End Function
- Function rotateImg(ByVal img As Bitmap) As Bitmap
- Dim input As Bitmap = img.Clone
- 'Step 1: Get the minimum and maximum X coordinate
- Dim center As Point = New Point(CInt(Math.Floor(img.Width / 2)), CInt(Math.Floor(img.Height / 2)))
- Dim outerPixels As Point() = getOuterPixels(Input)
- Dim outerPixelsPolar(outerPixels.Count - 1) As PolarPoint
- For i As Integer = 0 To outerPixels.Count - 1
- outerPixelsPolar(i) = toPolarPoint(outerPixels(i), center)
- Next
- Dim minWidth As Integer = Math.Ceiling(Math.Sqrt(img.Width ^ 2 + img.Height ^ 2))
- Dim minTheta As Double = 0 'The corresponding value of theta in which the minWidth is reached
- For thetaOffset As Double = -Math.PI To Math.PI Step (Math.PI / 180)
- Dim minX As Integer = img.Width
- Dim maxX As Integer = -img.Width
- For i As Integer = 0 To outerPixelsPolar.Count - 1
- Dim tempWidth As Integer = CInt(Math.Round(outerPixelsPolar(i).radius * Math.Cos(outerPixelsPolar(i).theta + thetaOffset)))
- minX = Math.Min(tempWidth, minX)
- maxX = Math.Max(tempWidth, maxX)
- Next
- Dim finalWidth As Integer = (-minX) + 1 + maxX
- If finalWidth < minWidth Then
- minWidth = finalWidth
- minTheta = thetaOffset
- End If
- Next
- 'Get points on four corners
- Dim cornerPoints As Point() = {New Point(0, 0), New Point(img.Width, 0), New Point(0, img.Height), New Point(img.Width, img.Height)}
- 'Randomize angle
- Dim angle As Decimal = minTheta * Math.PI / 180
- 'Shifts the points to make rotation axis about the middle
- Dim xHalf As Single = img.Width / 2
- Dim yHalf As Single = img.Height / 2
- For i As Integer = 0 To cornerPoints.Length - 1
- cornerPoints(i).X -= xHalf
- cornerPoints(i).Y -= yHalf
- Next i
- 'Editing corners to new location based on the angle
- Dim sin As Single = Math.Sin(angle)
- Dim cos As Single = Math.Cos(angle)
- Dim myX, myY As Single
- For i As Integer = 0 To 3
- myX = cornerPoints(i).X
- myY = cornerPoints(i).Y
- cornerPoints(i).X = myX * cos + myY * sin
- cornerPoints(i).Y = -myX * sin + myY * cos
- Next i
- 'Shift the points again back to original offset
- For i As Integer = 0 To 3
- cornerPoints(i).X += xHalf
- cornerPoints(i).Y += yHalf
- Next i
- 'Output
- Dim output As New Bitmap(img.Width, img.Height)
- Dim g As Graphics = Graphics.FromImage(output)
- 'Remove last corner: Only 3 needed for DrawImage
- ReDim Preserve cornerPoints(2)
- g.DrawImage(img, cornerPoints)
- Return output
- End Function
Add Comment
Please, Sign In to add comment