Advertisement
TizzyT

Image2GVD PRE -TizzyT

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