TizzyT

LandWaterGenerator -TizzyT

Nov 23rd, 2017
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 8.61 KB | None | 0 0
  1.     Public Function PerlinNoise(ByVal RNG As Random, ByVal X As Integer, ByVal Y As Integer, ByVal Level As Integer) As Bitmap
  2.         If X Mod 2 = 1 OrElse Y Mod 2 = 1 Then Throw New Exception("Demensions must be multiple of 2")
  3.         Dim Noise As New Func(Of Integer, Integer, Random, Bitmap)(Function(X2, Y2, RNG2)
  4.                                                                        Dim bm As New Bitmap(X2, Y2, PixelFormat.Format32bppArgb)
  5.                                                                        Dim lbm As New LockBitmap(bm)
  6.                                                                        lbm.LockBits()
  7.                                                                        lbm.FillNoise(RNG)
  8.                                                                        lbm.UnlockBits()
  9.                                                                        Return bm
  10.                                                                    End Function)
  11.         Dim XX As Integer = X : Dim YY As Integer = Y
  12.         Dim bmp As Bitmap = Noise(X, Y, RNG)
  13.         Dim matrix As New ColorMatrix With {.Matrix33 = 2 / 3}
  14.         Dim attributes As New ImageAttributes()
  15.         attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
  16.         attributes.SetWrapMode(WrapMode.TileFlipXY)
  17.         Using gfx As Graphics = Graphics.FromImage(bmp)
  18.             gfx.InterpolationMode = InterpolationMode.HighQualityBicubic
  19.             gfx.CompositingMode = CompositingMode.SourceOver
  20.             gfx.CompositingQuality = CompositingQuality.HighQuality
  21.             gfx.PixelOffsetMode = PixelOffsetMode.HighQuality
  22.             gfx.SmoothingMode = SmoothingMode.HighQuality
  23.             XX /= 2 : YY /= 2
  24.             While XX > 0 AndAlso YY > 0 AndAlso Level + 1 > 0
  25.                 Using bm As Bitmap = Noise(XX, YY, RNG)
  26.                     gfx.DrawImage(bm, New Rectangle(0, 0, X, Y), 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel, attributes)
  27.                 End Using
  28.                 XX /= 2 : YY /= 2 : Level -= 1
  29.             End While
  30.             'Return bmp
  31.         End Using
  32.         bmp = ResizeImage(bmp, X, Y, 1, 0.66)
  33.         Dim lr As New LockBitmap(bmp)
  34.         lr.LockBits()
  35.         lr.Threshold(154)
  36.         lr.UnlockBits()
  37.         bmp = ResizeImage(bmp, X, Y, 1, 0.66)
  38.         Return bmp
  39.     End Function
  40.  
  41.     Public Function ResizeImage(IMG As Image,
  42.                             W As Integer,
  43.                             H As Integer,
  44.                             Optional Opacity As Single = 1.0,
  45.                             Optional Blur As Single = 0.0,
  46.                             Optional CompositingMode As CompositingMode = CompositingMode.SourceOver,
  47.                             Optional CompositingQuality As CompositingQuality = CompositingQuality.HighQuality,
  48.                             Optional InterpolationMode As InterpolationMode = InterpolationMode.HighQualityBicubic,
  49.                             Optional SmoothingMode As SmoothingMode = SmoothingMode.HighQuality,
  50.                             Optional PixelOffsetMode As PixelOffsetMode = PixelOffsetMode.HighQuality,
  51.                             Optional WrapMode As WrapMode = WrapMode.TileFlipXY) As Bitmap
  52.         If W < 1 Then W = 1 : If H < 1 Then H = 1
  53.         Dim destRect As New Rectangle(0, 0, W, H), destImage As New Bitmap(W, H)
  54.         destImage.SetResolution(IMG.HorizontalResolution, IMG.VerticalResolution)
  55.         Using g = Graphics.FromImage(destImage) : With g
  56.                 .CompositingMode = CompositingMode
  57.                 .CompositingQuality = CompositingQuality
  58.                 .InterpolationMode = InterpolationMode
  59.                 .SmoothingMode = SmoothingMode
  60.                 .PixelOffsetMode = PixelOffsetMode : End With
  61.             Using WM = New ImageAttributes()
  62.                 If Opacity < 1.0 Then WM.SetColorMatrix(New ColorMatrix With {.Matrix33 = Opacity}, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
  63.                 WM.SetWrapMode(WrapMode)
  64.                 g.DrawImage(IMG, destRect, 0, 0, IMG.Width, IMG.Height, GraphicsUnit.Pixel, WM)
  65.             End Using
  66.         End Using
  67.         If Blur > 0.0 AndAlso Blur <= 1.0 Then Return ResizeImage(ResizeImage(destImage, W - (W * Blur), H - (H * Blur)), W, H)
  68.         Return destImage
  69.     End Function
  70.  
  71. Imports System.Drawing.Imaging
  72. Imports System.Runtime.InteropServices
  73. Public Class LockBitmap
  74.     Private source As Bitmap = Nothing
  75.     Private Iptr As IntPtr = IntPtr.Zero
  76.     Private bitmapData As BitmapData = Nothing
  77.     Public Property Pixels() As Byte()
  78.         Get
  79.             Return m_Pixels
  80.         End Get
  81.         Set(value As Byte())
  82.             m_Pixels = value
  83.         End Set
  84.     End Property
  85.     Private m_Pixels As Byte()
  86.     Public Property Depth() As Integer
  87.         Get
  88.             Return m_Depth
  89.         End Get
  90.         Private Set(value As Integer)
  91.             m_Depth = value
  92.         End Set
  93.     End Property
  94.     Private m_Depth As Integer
  95.     Public Property Width() As Integer
  96.         Get
  97.             Return m_Width
  98.         End Get
  99.         Private Set(value As Integer)
  100.             m_Width = value
  101.         End Set
  102.     End Property
  103.     Private m_Width As Integer
  104.     Public Property Height() As Integer
  105.         Get
  106.             Return m_Height
  107.         End Get
  108.         Private Set(value As Integer)
  109.             m_Height = value
  110.         End Set
  111.     End Property
  112.     Private m_Height As Integer
  113.     Public Sub New(source As Bitmap)
  114.         Me.source = source
  115.     End Sub
  116.     Public Sub LockBits()
  117.         Try
  118.             Width = source.Width
  119.             Height = source.Height
  120.             Dim PixelCount As Integer = Width * Height
  121.             Dim rect As New Rectangle(0, 0, Width, Height)
  122.             Depth = Image.GetPixelFormatSize(source.PixelFormat)
  123.             'If Depth <> 16 Then Throw New ArgumentException("Only 8, 24 and 32 bpp images are supported.")
  124.             bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite, source.PixelFormat)
  125.             Dim [step] As Integer = Depth / 8
  126.             Pixels = New Byte(PixelCount * [step] - 1) {}
  127.             Iptr = bitmapData.Scan0
  128.             Marshal.Copy(Iptr, Pixels, 0, Pixels.Length)
  129.         Catch ex As Exception
  130.             Throw ex
  131.         End Try
  132.     End Sub
  133.     Public Sub UnlockBits()
  134.         Try
  135.             Marshal.Copy(Pixels, 0, Iptr, Pixels.Length)
  136.             source.UnlockBits(bitmapData)
  137.             bitmapData = Nothing
  138.         Catch ex As Exception
  139.             Throw ex
  140.         End Try
  141.     End Sub
  142.     Public Function GetPixel(x As Integer, y As Integer) As Color
  143.         Dim clr As Color = Color.Empty
  144.         Dim cCount As Integer = Depth / 8
  145.         Dim i As Integer = ((y * Width) + x) * cCount
  146.         If i > Pixels.Length - cCount Then Throw New IndexOutOfRangeException()
  147.         If Depth = 32 Then
  148.             Dim b As Byte = Pixels(i)
  149.             Dim g As Byte = Pixels(i + 1)
  150.             Dim r As Byte = Pixels(i + 2)
  151.             Dim a As Byte = Pixels(i + 3)
  152.             clr = Color.FromArgb(a, r, g, b)
  153.         End If
  154.         If Depth = 24 Then
  155.             Dim b As Byte = Pixels(i)
  156.             Dim g As Byte = Pixels(i + 1)
  157.             Dim r As Byte = Pixels(i + 2)
  158.             clr = Color.FromArgb(r, g, b)
  159.         End If
  160.         If Depth = 8 Then
  161.             Dim c As Byte = Pixels(i)
  162.             clr = Color.FromArgb(c, c, c)
  163.         End If
  164.         Return clr
  165.     End Function
  166.     Public Sub SetPixel(x As Integer, y As Integer, color As Color)
  167.         Dim cCount As Integer = Depth / 8
  168.         Dim i As Integer = ((y * Width) + x) * cCount
  169.         If Depth = 32 Then
  170.             Pixels(i) = color.B
  171.             Pixels(i + 1) = color.G
  172.             Pixels(i + 2) = color.R
  173.             Pixels(i + 3) = color.A
  174.         End If
  175.         If Depth = 24 Then
  176.             Pixels(i) = color.B
  177.             Pixels(i + 1) = color.G
  178.             Pixels(i + 2) = color.R
  179.         End If
  180.         If Depth = 8 Then Pixels(i) = color.B
  181.     End Sub
  182.     Public Sub FillNoise(ByVal RNG As Random)
  183.         If bitmapData IsNot Nothing Then
  184.             RNG.NextBytes(m_Pixels)
  185.         End If
  186.     End Sub
  187.     Public Sub Threshold(ByVal value As Byte)
  188.         If bitmapData IsNot Nothing Then
  189.             For i = 1 To m_Pixels.Length - 1 Step 4
  190.                 If m_Pixels(i) < value Then
  191.                     m_Pixels(i) = 0
  192.                     m_Pixels(i - 1) = 255
  193.                 Else
  194.                     m_Pixels(i) = 255
  195.                     m_Pixels(i - 1) = 0
  196.                 End If
  197.                 m_Pixels(i + 1) = 0
  198.                 m_Pixels(i + 2) = 255
  199.             Next
  200.         End If
  201.     End Sub
  202. End Class
Advertisement
Add Comment
Please, Sign In to add comment