Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Drawing
- Imports System.Drawing.Imaging
- Imports System.IO
- Module Image2GVD
- Sub Main()
- Try
- Dim SrcPath As String = Environment.GetCommandLineArgs(1)
- Console.WriteLine(SrcPath)
- Dim test2 As New GVD(New Bitmap(SrcPath))
- test2.Save(SrcPath & ".gvd")
- Catch ex As Exception
- Console.WriteLine(ex.Message)
- End Try
- Console.ReadKey()
- End Sub
- End Module
- Public Class GVD
- Private Shared ReadOnly GVEW0100JPEG0100() As Byte = _
- New Byte() {71, 86, 69, 87, 48, 49, 48, 48, 74, 80, 69, 71, 48, 49, 48, 48} 'Static magic
- Private Shared ReadOnly BLK_() As Byte = New Byte() {66, 76, 75, 95} 'Static BLK_
- Private Shared ReadOnly Static1() As Byte = New Byte() {0, 0, 0, 1, 0, 0, 0, 0} 'Static value 1
- Private Shared ReadOnly AppCode1() As Byte = New Byte() {0, 0, 0, 32} 'Static App/Machine code
- Private Shared ReadOnly AppCode2() As Byte = New Byte() {0, 0, 0, 4}
- Private Shared ReadOnly notUsed() As Byte = New Byte() {0, 0, 0, 0} 'Static not used
- Private Shared ReadOnly Static2() As Byte = New Byte() {0, 0, 0, 2, 0, 0, 0, 0} 'Static value 2
- Private TotalWidth() As Byte 'Source image demensions
- Private TotalHeight() As Byte
- Private DBlen() As Byte 'Size of the database
- Private DataBase As List(Of DataBaseEntry) 'Database of entries
- Private resolutions() As Size 'Resolutions for image
- Private FinalData As List(Of Byte) 'Bytes representation of final GVD file
- Public Sub New(ByVal SrcImage As Bitmap)
- TotalWidth = BitConverter.GetBytes(SrcImage.Width) 'Bytes for TotalWidth
- Array.Reverse(TotalWidth)
- TotalHeight = BitConverter.GetBytes(SrcImage.Height) 'Bytes for Totalheight
- Array.Reverse(TotalHeight)
- DataBase = New List(Of DataBaseEntry) 'Initializes a new Database
- FinalData = New List(Of Byte) 'Initialize a new byte array to store final output
- FinalData.AddRange(GVEW0100JPEG0100) 'Adds magic bytes (16bytes)
- FinalData.AddRange(TotalWidth) 'Adds image width byts (4bytes)
- FinalData.AddRange(TotalHeight) 'Adds image height bytes (4bytes)
- FinalData.AddRange(BLK_) 'Adds BLK_ bytes (4bytes)
- resolutions = CalcRes(SrcImage.Width, SrcImage.Height) 'Calculate resolutions for each level
- For i = 0 To resolutions.Length - 1 'Processes level of images
- Gridder(ResizeImage(SrcImage, resolutions(i)), i)
- Next
- DBlen = BitConverter.GetBytes(DataBase.Count * 32 + 8)
- Array.Reverse(DBlen)
- FinalData.AddRange(DBlen)
- FinalData.AddRange(Static1) 'Adds static1 bytes (8bytes)
- FinalData.AddRange(AppCode1) 'Adds Appcode 1
- FinalData.AddRange(AppCode2) 'Adds Appcode 2
- Dim TotalLen As Integer = 0
- For Each img As DataBaseEntry In DataBase
- FinalData.AddRange(img.Xposition)
- FinalData.AddRange(img.Yposition)
- FinalData.AddRange(img.LayerLvL)
- FinalData.AddRange(img.ImageDataLen)
- FinalData.AddRange(img.PadLen)
- FinalData.AddRange(notUsed)
- FinalData.AddRange(img.ImageWidth)
- FinalData.AddRange(img.ImageHeight)
- TotalLen += img.getSize
- Next
- FinalData.AddRange(BLK_)
- Dim TotalLenBytes() As Byte = BitConverter.GetBytes(TotalLen)
- Array.Reverse(TotalLenBytes)
- FinalData.AddRange(TotalWidth)
- FinalData.AddRange(Static2)
- For Each img As DataBaseEntry In DataBase
- FinalData.AddRange(img.ImageData)
- If Not img.PadLen.Length = 17 Then FinalData.AddRange(img.Pad)
- Next
- End Sub
- Private Sub Gridder(ByVal bm As Bitmap, ByVal lvl As Integer)
- Dim PosX As Integer = 0
- Dim PosY As Integer = 0
- Dim RY As Integer = bm.Height Mod 256
- Dim RX As Integer = bm.Width Mod 256
- Dim g As Graphics
- For y = 0 To bm.Height - 256 Step 256
- For x = 0 To bm.Width - 256 Step 256
- Dim newBm As New Bitmap(256, 256)
- g = Graphics.FromImage(newBm)
- g.DrawImage(bm, x * -1, y * -1, bm.Width, bm.Height)
- DataBase.Add(New DataBaseEntry(newBm, lvl, PosX, PosY))
- PosX += 1
- Next
- If RX > 0 Then
- Dim newBm As New Bitmap(RX, 256)
- g = Graphics.FromImage(newBm)
- g.DrawImage(bm, (bm.Width - RX) * -1, y * -1, bm.Width, bm.Height)
- DataBase.Add(New DataBaseEntry(newBm, lvl, PosX, PosY))
- PosX += 1
- End If
- PosX = 0
- PosY += 1
- Next
- If RY > 0 Then
- For x = 0 To bm.Width - 256 Step 256
- Dim newBm As New Bitmap(256, RY)
- g = Graphics.FromImage(newBm)
- g.DrawImage(bm, x * -1, (bm.Height - RY) * -1, bm.Width, bm.Height)
- DataBase.Add(New DataBaseEntry(newBm, lvl, PosX, PosY))
- PosX += 1
- Next
- If RX > 0 Then
- Dim newBm As New Bitmap(RX, RY)
- g = Graphics.FromImage(newBm)
- g.DrawImage(bm, (bm.Width - RX) * -1, (bm.Height - RY) * -1, bm.Width, bm.Height)
- DataBase.Add(New DataBaseEntry(newBm, lvl, PosX, PosY))
- End If
- End If
- g.Dispose()
- End Sub
- Private Function ResizeImage(ByVal Img As Bitmap, ByVal res As Size) As Bitmap
- If Img.Size = res Then Return Img
- Dim newBm As Bitmap = New Bitmap(res.Width, res.Height)
- Using g As Graphics = Graphics.FromImage(newBm)
- g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
- g.CompositingMode = Drawing2D.CompositingMode.SourceCopy
- g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
- g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
- g.DrawImage(Img, 0, 0, newBm.Width, newBm.Height)
- g.Dispose()
- End Using
- Return newBm
- End Function
- Private Function CalcRes(ByVal X As Integer, ByVal Y As Integer) As Size()
- Dim Res(CalcLevel(X, Y)) As Size
- For i = 0 To Res.Length - 1
- Res(i) = New Size(X, Y)
- X = Math.Round(X / 2)
- Y = Math.Round(Y / 2)
- Next
- Return Res
- End Function
- Private Function CalcLevel(ByVal X As Integer, Y As Integer) As Integer
- CalcLevel = 0
- While X > 256 OrElse Y > 256
- X = Math.Round(X / 2)
- Y = Math.Round(Y / 2)
- CalcLevel += 1
- End While
- End Function
- Private Structure DataBaseEntry
- Public Xposition() As Byte 'X position of image section
- Public Yposition() As Byte 'Y position of image section
- Public ImageWidth() As Byte 'The width of the image section
- Public ImageHeight() As Byte 'The height of the image section
- Public LayerLvL() As Byte 'Layer Level of image section
- Public ImageData() As Byte 'The jpeg image
- Public ImageDataLen() As Byte 'The length of the image data
- Public Pad() As Byte 'Padding
- Public PadLen() As Byte 'The length of padding
- Public Sub New(ByVal GridImage As Bitmap, _
- ByVal layer As Integer, _
- ByVal Xpos As Integer, _
- ByVal Ypos As Integer)
- Xposition = BitConverter.GetBytes(Xpos) 'Bytes for Xposition
- Array.Reverse(Xposition)
- Yposition = BitConverter.GetBytes(Ypos) 'Bytes for Yposition
- Array.Reverse(Yposition)
- ImageWidth = BitConverter.GetBytes(GridImage.Width) 'Bytes for ImageWidth
- Array.Reverse(ImageWidth)
- ImageHeight = BitConverter.GetBytes(GridImage.Height) 'Bytes for ImageHeight
- Array.Reverse(ImageHeight)
- LayerLvL = BitConverter.GetBytes(layer) 'Bytes for LayerLvL
- Array.Reverse(LayerLvL)
- Using memoryStream As MemoryStream = New MemoryStream() 'Bytes for Jpeg
- GridImage.Save(memoryStream, ImageFormat.Jpeg)
- ImageData = memoryStream.GetBuffer()
- End Using
- ImageDataLen = BitConverter.GetBytes(ImageData.Length)
- Array.Reverse(ImageDataLen)
- Dim plen As Integer = 15 - (ImageData.Length Mod 16) 'Bytes for padding
- If plen = 15 Then
- PadLen = New Byte() {0, 0, 0, 0}
- Pad = New Byte() {}
- Else
- Pad = New Byte(plen) {}
- For i As Integer = 0 To Pad.Length - 1
- Pad(i) = 255
- Next
- PadLen = BitConverter.GetBytes(Pad.Length)
- Array.Reverse(PadLen)
- End If
- End Sub
- Public Function getSize() As Integer 'Returns the total data length including padding of image
- Return ImageData.Length + Pad.Length
- End Function
- End Structure
- Public Sub Save(ByVal Destination As String) 'Saves GVD at a specified location
- IO.File.WriteAllBytes(Destination, FinalData.ToArray)
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement