Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'Coded by ShadowTzu
- 'Free to use
- 'my 3D engine, Tzu3D: http://shadowtzu.free.fr
- 'Youtube: https://www.youtube.com/user/shadowtzu
- 'Facebook: https://www.facebook.com/Tzu3d
- 'Twitter: https://twitter.com/shadowtzu
- 'Twitch: http://www.twitch.tv/shadowtzu
- 'Website: http://tzu3d.weebly.com
- Imports System.IO
- Friend Class BitmapFast
- Implements IDisposable
- #Region "private"
- Private b() As Byte
- Private i_Width As Integer
- Private i_Height As Integer
- Private m_stride As Integer
- Private mLoad_Failed As Boolean
- #End Region
- #Region "New"
- Sub New(Width As Integer, Height As Integer)
- Me.i_Width = Width
- Me.i_Height = Height
- ReDim b(Height * Width * 4 - 1)
- End Sub
- Sub New(ByVal filename As String)
- Me.New(filename, False, 0)
- End Sub
- Public Sub New(ByVal filename As String, Optional ByVal clamp As Boolean = False, Optional ByVal clamp_add As Integer = 0)
- If (filename = "") Or (System.IO.File.Exists(filename) = False) Then
- MsgBox("file '" & filename & "' not found!")
- mLoad_Failed = True
- Exit Sub
- End If
- Dim pv_bitmap As System.Drawing.Bitmap = Nothing
- Dim image_width, image_height As Integer
- Dim myimage As System.Drawing.Image = System.Drawing.Image.FromFile(filename)
- image_height = myimage.Height
- If clamp = True Then
- image_height = clamp_base_two(myimage.Height)
- image_width = clamp_base_two(myimage.Width)
- If clamp_add <> 0 Then
- image_height = CInt(myimage.Height / 2) * 2 + clamp_add
- image_width = CInt(myimage.Width / 2) * 2 + clamp_add
- End If
- Else
- image_height = myimage.Height
- image_width = myimage.Width
- End If
- pv_bitmap = New System.Drawing.Bitmap(myimage, image_width, image_height)
- i_Width = pv_bitmap.Width
- i_Height = pv_bitmap.Height
- Dim bounds As System.Drawing.Rectangle = New System.Drawing.Rectangle(0, 0, pv_bitmap.Width, pv_bitmap.Height)
- Dim bitmapData As System.Drawing.Imaging.BitmapData = pv_bitmap.LockBits(bounds, Drawing.Imaging.ImageLockMode.ReadOnly, Drawing.Imaging.PixelFormat.Format32bppArgb)
- ReDim b(pv_bitmap.Height * bitmapData.Stride - 1)
- m_stride = bitmapData.Stride
- System.Runtime.InteropServices.Marshal.Copy(bitmapData.Scan0, b, 0, b.Length)
- bounds = Nothing
- bitmapData = Nothing
- pv_bitmap.Dispose()
- pv_bitmap = Nothing
- End Sub
- Private Function clamp_base_two(ByVal value As Integer) As Integer
- Dim clamp_test(0) As Integer
- Dim inc_clamp As Integer = 16
- Dim start_test As Integer = 32
- clamp_test(0) = start_test
- Dim inc As Integer
- Do While start_test < 4096
- ReDim Preserve clamp_test(inc)
- clamp_test(inc) = start_test + CInt(start_test / 2)
- start_test += inc_clamp
- inc += 1
- Loop
- For i As Integer = 0 To clamp_test.Length - 1
- If value < clamp_test(i) Then
- value = clamp_test(i) - 16
- Erase clamp_test
- Return value
- End If
- Next
- Erase clamp_test
- Return 4096
- End Function
- #End Region
- #Region "Function Get/Set Pixel"
- Public Function Getpixel(ByVal x As Integer, ByVal y As Integer) As System.Drawing.Color
- Dim red, green, blue, Alpha As Byte
- If x > Me.i_Width Then x = x Mod i_Width
- If y > Me.i_Height Then y = y Mod i_Height
- If x < 0 Then x = Me.i_Width - (Math.Abs(x) Mod i_Width)
- If y < 0 Then y = Me.i_Height - (Math.Abs(y) Mod i_Height)
- red = b((x * 4) + y * m_stride)
- green = b((x * 4 + 1) + y * m_stride)
- blue = b((x * 4 + 2) + y * m_stride)
- Alpha = b((x * 4 + 3) + y * m_stride)
- Return System.Drawing.Color.FromArgb(Alpha, red, green, blue)
- End Function
- Public Sub Setpixel(ByVal x As Integer, ByVal y As Integer, ByVal color As System.Drawing.Color)
- Dim red, green, blue, Alpha As Byte
- red = color.R : green = color.G : blue = color.B : Alpha = color.A
- b((x * 4) + y * m_stride) = blue
- b((x * 4 + 1) + y * m_stride) = green
- b((x * 4 + 2) + y * m_stride) = red
- b((x * 4 + 3) + y * m_stride) = Alpha
- End Sub
- Public Property Pixel(X As Integer, Y As Integer) As System.Drawing.Color
- Get
- Return Getpixel(X, Y)
- End Get
- Set(value As System.Drawing.Color)
- Setpixel(X, Y, value)
- End Set
- End Property
- #End Region
- #Region "property"
- Public ReadOnly Property Load_Failed As Boolean
- Get
- Return mLoad_Failed
- End Get
- End Property
- Public ReadOnly Property Width() As Integer
- Get
- Return i_Width
- End Get
- End Property
- Public ReadOnly Property Height() As Integer
- Get
- Return i_Height
- End Get
- End Property
- Public Function Get_Bitmap() As System.Drawing.Bitmap
- Get_Bitmap = New System.Drawing.Bitmap(i_Width, i_Height, Drawing.Imaging.PixelFormat.Format32bppArgb)
- Dim bounds As System.Drawing.Rectangle = New System.Drawing.Rectangle(0, 0, Get_Bitmap.Width, Get_Bitmap.Height)
- Dim bitmapData As System.Drawing.Imaging.BitmapData = Get_Bitmap.LockBits(bounds, Drawing.Imaging.ImageLockMode.ReadOnly, Drawing.Imaging.PixelFormat.Format32bppArgb)
- System.Runtime.InteropServices.Marshal.Copy(b, 0, bitmapData.Scan0, b.Length)
- Get_Bitmap.UnlockBits(bitmapData)
- End Function
- #End Region
- #Region "save"
- Public Sub Save(ByVal filename As String, ByVal format As System.Drawing.Imaging.ImageFormat)
- Dim pv_bitmap As System.Drawing.Bitmap = Nothing
- pv_bitmap = New System.Drawing.Bitmap(i_Width, i_Height, Drawing.Imaging.PixelFormat.Format32bppArgb)
- Dim bounds As System.Drawing.Rectangle = New System.Drawing.Rectangle(0, 0, pv_bitmap.Width, pv_bitmap.Height)
- Dim bitmapData As System.Drawing.Imaging.BitmapData = pv_bitmap.LockBits(bounds, Drawing.Imaging.ImageLockMode.ReadOnly, Drawing.Imaging.PixelFormat.Format32bppArgb)
- System.Runtime.InteropServices.Marshal.Copy(b, 0, bitmapData.Scan0, b.Length)
- pv_bitmap.Save(filename, format)
- pv_bitmap.Dispose()
- pv_bitmap = Nothing
- End Sub
- #End Region
- Private disposedValue As Boolean = False
- Protected Friend Overridable Sub Dispose(ByVal disposing As Boolean)
- If Not Me.disposedValue Then
- If disposing Then
- End If
- Erase b
- b = Nothing
- End If
- Me.disposedValue = True
- End Sub
- #Region " IDisposable Support "
- Friend Sub Dispose() Implements IDisposable.Dispose
- Dispose(True)
- GC.SuppressFinalize(Me)
- End Sub
- #End Region
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement