TizzyT

Image2GVD -TizzyT

Apr 3rd, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 8.83 KB | None | 0 0
  1. Imports System.IO
  2. Imports System.Drawing
  3. Imports System.Drawing.Imaging
  4.  
  5. Public Class GVD
  6.     Private Shared ReadOnly GVEW0100JPEG0100() As Byte = _
  7.         New Byte() {71, 86, 69, 87, 48, 49, 48, 48, 74, 80, 69, 71, 48, 49, 48, 48} 'Static magic
  8.     Private Shared ReadOnly BLK_() As Byte = New Byte() {66, 76, 75, 95} 'Static BLK_
  9.     Private Shared ReadOnly Static1() As Byte = New Byte() {0, 0, 0, 1, 0, 0, 0, 0} 'Static value 1
  10.     Private Shared ReadOnly AppCode1() As Byte = New Byte() {0, 0, 0, 32} 'Static App/Machine code
  11.     Private Shared ReadOnly AppCode2() As Byte = New Byte() {0, 0, 0, 4}
  12.     Private Shared ReadOnly notUsed() As Byte = New Byte() {0, 0, 0, 0} 'Static not used
  13.     Private Shared ReadOnly Static2() As Byte = New Byte() {0, 0, 0, 2, 0, 0, 0, 0} 'Static value 2
  14.     Private TotalWidth() As Byte 'Source image demensions
  15.     Private TotalHeight() As Byte
  16.     Private DBlen() As Byte
  17.     Private LengthOfDB() As Byte 'Size of the database
  18.     Private DataBase As List(Of DataBaseEntry) 'Database of entries
  19.     Private resolutions() As Size 'Resolutions for image
  20.     Private FinalData As List(Of Byte) 'Bytes representation of final GVD file
  21.  
  22.     Public Sub New(ByVal SrcImage As Bitmap)
  23.         TotalWidth = BitConverter.GetBytes(SrcImage.Width) 'Bytes for TotalWidth
  24.         Array.Reverse(TotalWidth)
  25.         TotalHeight = BitConverter.GetBytes(SrcImage.Height) 'Bytes for Totalheight
  26.         Array.Reverse(TotalHeight)
  27.         DataBase = New List(Of DataBaseEntry) 'Initializes a new Database
  28.         FinalData = New List(Of Byte) 'Initialize a new byte array to store final output
  29.         FinalData.AddRange(GVEW0100JPEG0100) 'Adds magic bytes (16bytes)
  30.         FinalData.AddRange(TotalWidth) 'Adds image width byts (4bytes)
  31.         FinalData.AddRange(TotalHeight) 'Adds image height bytes (4bytes)
  32.         FinalData.AddRange(BLK_) 'Adds BLK_ bytes (4bytes)
  33.         resolutions = CalcRes(SrcImage.Width, SrcImage.Height) 'Calculate resolutions for each level
  34.         For i = 0 To resolutions.Length - 1 'Processes level of images
  35.             Gridder(ResizeImage(SrcImage, resolutions(i)), i)
  36.         Next
  37.         DBlen = BitConverter.GetBytes(DataBase.Count * 32 + 8)
  38.         Array.Reverse(DBlen)
  39.         FinalData.AddRange(DBlen)
  40.         FinalData.AddRange(Static1) 'Adds static1 bytes (8bytes)
  41.         FinalData.AddRange(AppCode1)
  42.         FinalData.AddRange(AppCode2)
  43.         Dim TotalLen As Integer = 0
  44.         For Each img As DataBaseEntry In DataBase
  45.             FinalData.AddRange(img.Xposition)
  46.             FinalData.AddRange(img.Yposition)
  47.             FinalData.AddRange(img.LayerLvL)
  48.             FinalData.AddRange(img.ImageDataLen)
  49.             FinalData.AddRange(img.PadLen)
  50.             FinalData.AddRange(notUsed)
  51.             FinalData.AddRange(img.ImageWidth)
  52.             FinalData.AddRange(img.ImageHeight)
  53.             TotalLen += img.getSize
  54.         Next
  55.         FinalData.AddRange(BLK_)
  56.         Dim TotalLenBytes() As Byte = BitConverter.GetBytes(TotalLen)
  57.         Array.Reverse(TotalLenBytes)
  58.         FinalData.AddRange(TotalWidth)
  59.         FinalData.AddRange(Static2)
  60.         For Each img As DataBaseEntry In DataBase
  61.             FinalData.AddRange(img.ImageData)
  62.             If Not img.PadLen.Length = 17 Then FinalData.AddRange(img.Pad)
  63.         Next
  64.     End Sub
  65.  
  66.     Private Sub Gridder(ByVal bm As Bitmap, ByVal lvl As Integer)
  67.         Dim PosX As Integer = 0
  68.         Dim PosY As Integer = 0
  69.         Dim RY As Integer = bm.Height Mod 256
  70.         Dim RX As Integer = bm.Width Mod 256
  71.         Dim g As Graphics
  72.         For y = 0 To bm.Height - 256 Step 256
  73.             For x = 0 To bm.Width - 256 Step 256
  74.                 Dim newBm As New Bitmap(256, 256)
  75.                 g = Graphics.FromImage(newBm)
  76.                 g.DrawImage(bm, x * -1, y * -1, bm.Width, bm.Height)
  77.                 DataBase.Add(New DataBaseEntry(newBm, lvl, PosX, PosY))
  78.                 PosX += 1
  79.             Next
  80.             If RX > 0 Then
  81.                 Dim newBm As New Bitmap(RX, 256)
  82.                 g = Graphics.FromImage(newBm)
  83.                 g.DrawImage(bm, (bm.Width - RX) * -1, y * -1, bm.Width, bm.Height)
  84.                 DataBase.Add(New DataBaseEntry(newBm, lvl, PosX, PosY))
  85.                 PosX += 1
  86.             End If
  87.             PosX = 0
  88.             PosY += 1
  89.         Next
  90.         If RY > 0 Then
  91.             For x = 0 To bm.Width - 256 Step 256
  92.                 Dim newBm As New Bitmap(256, RY)
  93.                 g = Graphics.FromImage(newBm)
  94.                 g.DrawImage(bm, x * -1, (bm.Height - RY) * -1, bm.Width, bm.Height)
  95.                 DataBase.Add(New DataBaseEntry(newBm, lvl, PosX, PosY))
  96.                 PosX += 1
  97.             Next
  98.             If RX > 0 Then
  99.                 Dim newBm As New Bitmap(RX, RY)
  100.                 g = Graphics.FromImage(newBm)
  101.                 g.DrawImage(bm, (bm.Width - RX) * -1, (bm.Height - RY) * -1, bm.Width, bm.Height)
  102.                 DataBase.Add(New DataBaseEntry(newBm, lvl, PosX, PosY))
  103.             End If
  104.         End If
  105.         g.Dispose()
  106.     End Sub
  107.  
  108.     Private Function ResizeImage(ByVal Img As Bitmap, ByVal res As Size) As Bitmap
  109.         If Img.Size = res Then Return Img
  110.         Dim newBm As Bitmap = New Bitmap(res.Width, res.Height)
  111.         Using g As Graphics = Graphics.FromImage(newBm)
  112.             g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
  113.             g.CompositingMode = Drawing2D.CompositingMode.SourceCopy
  114.             g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
  115.             g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
  116.             g.DrawImage(Img, 0, 0, newBm.Width, newBm.Height)
  117.             g.Dispose()
  118.         End Using
  119.         Return newBm
  120.     End Function
  121.  
  122.     Private Function CalcRes(ByVal X As Integer, ByVal Y As Integer) As Size()
  123.         Dim Res(CalcLevel(X, Y)) As Size
  124.         For i = 0 To Res.Length - 1
  125.             Res(i) = New Size(X, Y)
  126.             X = Math.Round(X / 2)
  127.             Y = Math.Round(Y / 2)
  128.         Next
  129.         Return Res
  130.     End Function
  131.  
  132.     Private Function CalcLevel(ByVal X As Integer, Y As Integer) As Integer
  133.         CalcLevel = 0
  134.         While X > 256 OrElse Y > 256
  135.             X = Math.Round(X / 2)
  136.             Y = Math.Round(Y / 2)
  137.             CalcLevel += 1
  138.         End While
  139.     End Function
  140.  
  141.     Private Structure DataBaseEntry
  142.         Public Xposition() As Byte 'X position of image section
  143.         Public Yposition() As Byte 'Y position of image section
  144.         Public ImageWidth() As Byte 'The width of the image section
  145.         Public ImageHeight() As Byte 'The height of the image section
  146.         Public LayerLvL() As Byte 'Layer Level of image section
  147.         Public ImageData() As Byte 'The jpeg image
  148.         Public ImageDataLen() As Byte 'The length of the image data
  149.         Public Pad() As Byte 'Padding
  150.         Public PadLen() As Byte 'The length of padding
  151.         Public Sub New(ByVal GridImage As Bitmap, _
  152.                        ByVal layer As Integer, _
  153.                        ByVal Xpos As Integer, _
  154.                        ByVal Ypos As Integer)
  155.             Xposition = BitConverter.GetBytes(Xpos) 'Bytes for Xposition
  156.             Array.Reverse(Xposition)
  157.             Yposition = BitConverter.GetBytes(Ypos) 'Bytes for Yposition
  158.             Array.Reverse(Yposition)
  159.             ImageWidth = BitConverter.GetBytes(GridImage.Width) 'Bytes for ImageWidth
  160.             Array.Reverse(ImageWidth)
  161.             ImageHeight = BitConverter.GetBytes(GridImage.Height) 'Bytes for ImageHeight
  162.             Array.Reverse(ImageHeight)
  163.             LayerLvL = BitConverter.GetBytes(layer) 'Bytes for LayerLvL
  164.             Array.Reverse(LayerLvL)
  165.             Using memoryStream As MemoryStream = New MemoryStream() 'Bytes for Jpeg
  166.                 GridImage.Save(memoryStream, ImageFormat.Jpeg)
  167.                 ImageData = memoryStream.GetBuffer()
  168.             End Using
  169.             ImageDataLen = BitConverter.GetBytes(ImageData.Length)
  170.             Array.Reverse(ImageDataLen)
  171.             'Bytes for padding
  172.             Dim plen As Integer = 15 - (ImageData.Length Mod 16)
  173.             If plen = 15 Then
  174.                 PadLen = New Byte() {0, 0, 0, 0}
  175.                 Pad = New Byte() {}
  176.             Else
  177.                 Pad = New Byte(plen) {}
  178.                 For i As Integer = 0 To Pad.Length - 1
  179.                     Pad(i) = 255
  180.                 Next
  181.                 PadLen = BitConverter.GetBytes(Pad.Length)
  182.                 Array.Reverse(PadLen)
  183.             End If
  184.         End Sub
  185.         Public Function getSize() As Integer 'Returns the total data length including padding of image
  186.             Return ImageData.Length + Pad.Length
  187.         End Function
  188.     End Structure
  189.  
  190.     Public Sub Save(ByVal Destination As String) 'Saves GVD at a specified location
  191.         IO.File.WriteAllBytes(Destination, FinalData.ToArray)
  192.     End Sub
  193. End Class
Advertisement
Add Comment
Please, Sign In to add comment