Results 1 to 6 of 6

Thread: Image turns out all strange, and I can't find what's wrong

  1. #1

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Unhappy Image turns out all strange, and I can't find what's wrong

    I'm making my own new image format where any pixel with an alpha less than a specific amount is dropped, and up to 255 colors are stored in a dictionary for additional compression. The format is like this:

    Not one of the 255 colors in the dictionary: Argb representation
    In dictionary: 0 for the alpha byte, number of repetitions in the R and G bytes, and index in the B byte.

    Each group is stored like so:
    0 x y colors.

    I'm getting an IndexOutOfRangeException because the dictionary never has anything in it, and I can't figure out why. I have no idea what's going wrong! Here's my code:
    vb.net Code:
    1. Imports System.IO
    2. Imports System.Runtime.InteropServices
    3. Imports System.Drawing.Imaging
    4. Public NotInheritable Class EACImage
    5.  
    6.     Public Shared Sub FromBitmap(ByVal b As Bitmap, ByVal output As String, Optional ByVal threshold As Integer = 0)
    7.         Dim r As New Rectangle(0, 0, b.Width, b.Height)
    8.         Using fs As New FileStream(output, FileMode.Create, FileAccess.Write)
    9.             Using bw As New BinaryWriter(fs)
    10.                 Dim bd As BitmapData = b.LockBits(r, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb)
    11.                 Dim arr(bd.Width * bd.Height - 1) As Integer
    12.                 Marshal.Copy(bd.Scan0, arr, 0, arr.Length)
    13.  
    14.                 'Prepare information
    15.                 Dim dictionary As New List(Of Integer)
    16.                 Dim data As New List(Of Integer)
    17.                 For i As Integer = 0 To arr.Length - 1
    18.                     While GetAComp(arr(i)) <= threshold
    19.                         i += 1
    20.                         If i = arr.Length Then Exit For
    21.                     End While
    22.                     data.Add(0) 'Begin a new group
    23.                     data.Add(i Mod bd.Width) 'Width
    24.                     data.Add(i \ bd.Width) 'Height
    25.                     While (i < arr.Length) AndAlso (GetAComp(arr(i)) > threshold)
    26.                         data.Add(arr(i))
    27.                         i += 1
    28.                     End While
    29.                 Next
    30.  
    31.                 'Compress the list
    32.                 CompressList(data, dictionary)
    33.  
    34.                 'Write header information
    35.                 For Each item As Integer In dictionary
    36.                     bw.Write(item)
    37.                     MsgBox("Wrote " & item)
    38.                 Next
    39.                 bw.Write(0I)
    40.                 bw.Write(bd.Width)
    41.                 bw.Write(bd.Height)
    42.  
    43.                 'Write data
    44.                 For Each item As Integer In data
    45.                     bw.Write(item)
    46.                 Next
    47.  
    48.                 'Unlock the bitmap
    49.                 b.UnlockBits(bd)
    50.  
    51.                 'Close the streams.
    52.                 bw.Close()
    53.                 fs.Close()
    54.             End Using
    55.         End Using
    56.     End Sub
    57.  
    58.     Public Shared Function FromFile(ByVal f As String) As Bitmap
    59.         Dim r As Bitmap = Nothing
    60.  
    61.         Using fs As New FileStream(f, FileMode.Open, FileAccess.Read)
    62.             Using br As New BinaryReader(fs)
    63.                 Dim dict As New List(Of Integer)
    64.                 Do
    65.                     Dim itm As Integer = br.ReadInt32()
    66.                     If itm = 0 Then Exit Do
    67.                     dict.Add(itm)
    68.                 Loop
    69.                 r = New Bitmap(br.ReadInt32(), br.ReadInt32(), PixelFormat.Format32bppArgb)
    70.                 Dim bd As BitmapData = r.LockBits(New Rectangle(0, 0, r.Width, r.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb)
    71.                 Dim arr(bd.Width * bd.Height - 1) As Integer
    72.                 Dim x As Integer = 0, y As Integer = 0
    73.                 Do
    74.                     If br.BaseStream.Position = br.BaseStream.Length Then Exit Do
    75.                     Dim n_int As Integer = br.ReadInt32()
    76.                     If n_int = 0 Then 'next group
    77.                         x = br.ReadInt32()
    78.                         y = br.ReadInt32()
    79.                     End If
    80.                     If GetAComp(n_int) = 0 Then 'It's compressed
    81.                         For j As Integer = 1 To GetRepetition(n_int)
    82.                             arr(y * bd.Width + x) = dict(GetIndex(n_int))
    83.                             x += 1
    84.                             If x >= bd.Width Then
    85.                                 y += 1
    86.                                 x = x Mod bd.Width
    87.                             End If
    88.                         Next
    89.                     Else 'Not compressed
    90.                         arr(y * bd.Width + x) = n_int
    91.                     End If
    92.                     x += 1
    93.                     If x >= bd.Width Then
    94.                         y += 1
    95.                         x = x Mod bd.Width
    96.                     End If
    97.                 Loop
    98.  
    99.                 'Copy back and unlock
    100.                 Marshal.Copy(arr, 0, bd.Scan0, arr.Length)
    101.                 r.UnlockBits(bd)
    102.  
    103.                 'Close the streams.
    104.                 br.Close()
    105.                 fs.Close()
    106.             End Using
    107.         End Using
    108.  
    109.         Return r
    110.     End Function
    111.  
    112. #Region "GetXXX"
    113.     Friend Shared Function GetAComp(ByVal i As Integer) As Integer
    114.         Dim sh24 As Integer = 255I << 24
    115.         Dim res As Integer = i And sh24
    116.         Return res >> 24
    117.     End Function
    118.  
    119.     Friend Shared Function GetRepetition(ByVal i As Integer) As Integer
    120.         Dim sh16 As Integer = 255 << 16
    121.         Dim sh8 As Integer = 255 << 8
    122.         Dim res As Integer = i And (sh16 Or sh8)
    123.         Return res >> 8
    124.     End Function
    125.  
    126.     Friend Shared Function GetIndex(ByVal i As Integer) As Integer
    127.         Return (i And 255)
    128.     End Function
    129.  
    130.     Friend Shared Function GetInt(ByVal count As Integer, ByVal index As Integer) As Integer
    131.         If count > Short.MaxValue OrElse index > 255 Then Throw New ArgumentException()
    132.         Return index Or (count << 8)
    133.     End Function
    134. #End Region
    135.  
    136.  
    137.     Private Shared Sub CompressList(ByRef l As List(Of Integer), ByRef dict As List(Of Integer), Optional ByVal minrep As Integer = 30)
    138.         dict.Clear()
    139.         Dim nl As New List(Of Integer)
    140.         Dim l_itm As Integer = -1
    141.         Dim count As Integer = 0
    142.         For i As Integer = 0 To l.Count - 1
    143.             Dim itm As Integer = l(i)
    144.             If itm = 0 Then
    145.                 i += 2
    146.                 If i >= l.Count Then Exit For
    147.                 itm = l(i)
    148.             End If
    149.             If count = 0 Then
    150.                 count = 1
    151.                 l_itm = itm
    152.             Else
    153.                 If l_itm = itm Then
    154.                     count += 1
    155.                     If (count < UInt16.MaxValue) AndAlso (dict.Count < 255) Then Continue For
    156.                 End If
    157.                 If count <= minrep Then
    158.                     nl.Add(itm)
    159.                 Else
    160.                     Dim idx As Integer = dict.Count
    161.                     If Not dict.Contains(itm) Then
    162.                         dict.Add(itm)
    163.                     Else
    164.                         idx = dict.IndexOf(itm)
    165.                     End If
    166.                     nl.Add(GetInt(count, idx))
    167.                 End If
    168.                 l_itm = itm
    169.                 count = 1
    170.             End If
    171.         Next
    172.         l = nl
    173.     End Sub
    174.  
    175. End Class

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Image turns out all strange, and I can't find what's wrong

    Which line is giving the exception?
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Image turns out all strange, and I can't find what's wrong

    Sorry, I fixed that just now, I made a stupid mistake...

    Right now, my problem is this:
    Only the picture's outline shows. That's great for special effects, but not so good for what I want. Here's the code, and "before & after" pictures:

    vb.net Code:
    1. Imports System.IO
    2. Imports System.Runtime.InteropServices
    3. Imports System.Drawing.Imaging
    4. Public NotInheritable Class EACImage
    5.  
    6. #Region "FromBitmap"
    7.     Public Shared Sub FromBitmap(ByVal b As Bitmap, ByVal output As String, Optional ByVal threshold As Integer = 0)
    8.         Dim r As New Rectangle(0, 0, b.Width, b.Height)
    9.         Using fs As New FileStream(output, FileMode.Create, FileAccess.Write)
    10.             Using bw As New BinaryWriter(fs)
    11.                 Dim bd As BitmapData = b.LockBits(r, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb)
    12.                 Dim arr(bd.Width * bd.Height - 1) As Integer
    13.                 Marshal.Copy(bd.Scan0, arr, 0, arr.Length)
    14.  
    15.                 'Prepare information
    16.                 Dim dictionary As New List(Of Integer)
    17.                 Dim data As New List(Of Integer)
    18.                 For i As Integer = 0 To arr.Length - 1
    19.                     data.Add(0) 'Begin a new group
    20.                     Dim x As Integer = i Mod bd.Width
    21.                     Dim y As Integer = i \ bd.Width
    22.                     Debug.WriteLine("Adding at " & x & "," & y & "...")
    23.                     data.Add(x)
    24.                     data.Add(y)
    25.  
    26.                     While GetAComp(arr(i)) <= threshold
    27.                         i += 1
    28.                         If i = arr.Length Then Exit For
    29.                     End While
    30.                     While (i < arr.Length) AndAlso (GetAComp(arr(i)) > threshold)
    31.                         data.Add(arr(i))
    32.                         i += 1
    33.                     End While
    34.                 Next
    35.  
    36.                 'Compress the list
    37.                 CompressList(data, dictionary)
    38.  
    39.                 'Write header information
    40.                 For Each item As Integer In dictionary
    41.                     bw.Write(item)
    42.                 Next
    43.                 bw.Write(0I)
    44.                 bw.Write(bd.Width)
    45.                 bw.Write(bd.Height)
    46.  
    47.                 'Write data
    48.                 For Each item As Integer In data
    49.                     bw.Write(item)
    50.                 Next
    51.  
    52.                 'Unlock the bitmap
    53.                 b.UnlockBits(bd)
    54.  
    55.                 'Close the streams.
    56.                 bw.Close()
    57.                 fs.Close()
    58.             End Using
    59.         End Using
    60.     End Sub
    61. #End Region
    62.  
    63. #Region "FromFile"
    64.     Public Shared Function FromFile(ByVal f As String) As Bitmap
    65.         Dim r As Bitmap = Nothing
    66.  
    67.         Using fs As New FileStream(f, FileMode.Open, FileAccess.Read)
    68.             Using br As New BinaryReader(fs)
    69.                 Dim dict As New List(Of Integer)
    70.                 Do
    71.                     Dim itm As Integer = br.ReadInt32()
    72.                     If itm = 0 Then Exit Do
    73.                     dict.Add(itm)
    74.                 Loop
    75.                 r = New Bitmap(br.ReadInt32(), br.ReadInt32(), PixelFormat.Format32bppArgb)
    76.                 Dim bd As BitmapData = r.LockBits(New Rectangle(0, 0, r.Width, r.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb)
    77.                 Dim arr(bd.Width * bd.Height - 1) As Integer
    78.                 Dim i As Integer = 0
    79.                 Do
    80.                     If br.BaseStream.Position = br.BaseStream.Length Then Exit Do
    81.                     Dim n_int As Integer = br.ReadInt32()
    82.                     If n_int = 0 Then 'next group
    83.                         Dim x As Integer = br.ReadInt32()
    84.                         Dim y As Integer = br.ReadInt32()
    85.                         Debug.WriteLine("Recieved coordinates " & x & "," & y & ".")
    86.                         i = (y * bd.Width) + x
    87.                         Continue Do
    88.                     End If
    89.                     If GetAComp(n_int) = 0 Then 'It's compressed
    90.                         Dim index As Integer = GetIndex(n_int)
    91.                         For j As Integer = 1 To GetRepetition(n_int)
    92.                             arr(i) = dict(index)
    93.                             i += 1
    94.                         Next
    95.                     Else 'Not compressed
    96.                         Debug.WriteLine("Current X is " & (i Mod bd.Width).ToString())
    97.                         Debug.WriteLine("Current Y is " & (i \ bd.Width).ToString())
    98.                         arr(i) = n_int
    99.                     End If
    100.                     i += 1
    101.                 Loop
    102.  
    103.                 'Copy back and unlock
    104.                 Marshal.Copy(arr, 0, bd.Scan0, arr.Length)
    105.                 r.UnlockBits(bd)
    106.  
    107.                 'Close the streams.
    108.                 br.Close()
    109.                 fs.Close()
    110.             End Using
    111.         End Using
    112.  
    113.         Return r
    114.     End Function
    115. #End Region
    116.  
    117. #Region "GetXXX"
    118.     Friend Shared Function GetAComp(ByVal i As Integer) As Integer
    119.         Dim sh24 As Integer = 255I << 24
    120.         Dim res As Integer = i And sh24
    121.         Return res >> 24
    122.     End Function
    123.  
    124.     Friend Shared Function GetRepetition(ByVal i As Integer) As Integer
    125.         'Dim sh16 As Integer = 255 << 16
    126.         'Dim sh8 As Integer = 255 << 8
    127.         'Dim res As Integer = i And (sh16 Or sh8)
    128.         'Return res >> 8
    129.         Return (i Xor 255) >> 8
    130.     End Function
    131.  
    132.     Friend Shared Function GetIndex(ByVal i As Integer) As Integer
    133.         Return (i And 255)
    134.     End Function
    135.  
    136.     Friend Shared Function GetInt(ByVal count As Integer, ByVal index As Integer) As Integer
    137.         If count > Short.MaxValue OrElse index > 255 Then Throw New ArgumentException()
    138.         Return index Or (count << 8)
    139.     End Function
    140. #End Region
    141.  
    142. #Region "CompressList"
    143.     ''' <summary>
    144.     ''' Compresses a list of colors representing the first stage of an EAC image.
    145.     ''' </summary>
    146.     ''' <param name="l">The list of colors.</param>
    147.     ''' <param name="dict">The dictionary.</param>
    148.     ''' <param name="minrep">The minimum amount of repetitions.</param>
    149.     Private Shared Sub CompressList(ByRef l As List(Of Integer), ByRef dict As List(Of Integer), Optional ByVal minrep As Integer = 30)
    150.         dict.Clear()
    151.         Dim nl As New List(Of Integer)
    152.         Dim l_itm As Integer = -1
    153.         Dim count As Integer = 0
    154.         For i As Integer = 0 To l.Count - 1
    155.             Dim itm As Integer = l(i)
    156.             If itm = 0 Then
    157.                 nl.Add(l(i))
    158.                 nl.Add(l(i + 1))
    159.                 nl.Add(l(i + 2))
    160.                 i += 2
    161.                 Continue For
    162.             End If
    163.             If count = 0 Then
    164.                 count = 1
    165.                 l_itm = itm
    166.             Else
    167.                 If l_itm = itm Then
    168.                     count += 1
    169.                     If (count < UInt16.MaxValue) AndAlso (dict.Count < 255) Then Continue For
    170.                 End If
    171.                 If count <= minrep Then
    172.                     nl.Add(itm)
    173.                 Else
    174.                     Dim idx As Integer = dict.IndexOf(itm)
    175.                     If idx = -1 Then
    176.                         dict.Add(itm)
    177.                         idx = dict.Count - 1
    178.                     End If
    179.                     nl.Add(GetInt(count, idx))
    180.                 End If
    181.                 l_itm = itm
    182.                 count = 1
    183.             End If
    184.         Next
    185.         l = nl
    186.     End Sub
    187. #End Region
    188.  
    189. End Class
    Attached Images Attached Images   
    Last edited by minitech; Mar 8th, 2010 at 03:20 PM. Reason: Added pictures.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Image turns out all strange, and I can't find what's wrong

    Looks like you are not decompressing correctly. It looks like you are getting the first and last bits, but the internal bits are being lost.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Image turns out all strange, and I can't find what's wrong

    Yup. I think the information is being stored properly, because of the size of the EAC file. Do you know why? At first, I thought it was because of the CompressList method, so I commented the body out (if you do that, it just doesn't compress)... no change.

  6. #6

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Unhappy Re: Image turns out all strange, and I can't find what's wrong

    Ok, I've added to my code, changed some things, and added debugging statements. It seems that the file isn't being written to correctly. Here's my code, and the log file is attached:
    vb.net Code:
    1. Imports System.IO
    2. Imports System.Runtime.InteropServices
    3. Imports System.Drawing.Imaging
    4. Public NotInheritable Class EACImage
    5.  
    6. #Region "Log Debugger"
    7. #If CONFIG = "Debug" Then
    8.     Private Shared Debug As Debugger
    9.  
    10.     Private Class Debugger
    11.         Implements IDisposable
    12.  
    13.         Private m_InnerStream As StreamWriter
    14.         Public Sub New()
    15.             m_InnerStream = New StreamWriter("debug_log.txt")
    16.         End Sub
    17.         Public Sub WriteLine(ByVal ln As String)
    18.             m_InnerStream.WriteLine(ln)
    19.         End Sub
    20.         Private disposedValue As Boolean = False
    21.         Protected Overridable Sub Dispose(ByVal disposing As Boolean)
    22.             If Not Me.disposedValue Then
    23.                 If disposing Then
    24.                     m_InnerStream.Close()
    25.                     m_InnerStream.Dispose()
    26.                     m_InnerStream = Nothing
    27.                 End If
    28.             End If
    29.             Me.disposedValue = True
    30.         End Sub
    31.         Public Sub Dispose() Implements IDisposable.Dispose
    32.             Dispose(True)
    33.             GC.SuppressFinalize(Me)
    34.         End Sub
    35.  
    36.         Protected Overrides Sub Finalize()
    37.             Me.Dispose(False)
    38.             MyBase.Finalize()
    39.         End Sub
    40.     End Class
    41. #End If
    42. #End Region
    43.  
    44. #Region "FromBitmap"
    45.     Public Shared Sub FromBitmap(ByVal b As Bitmap, ByVal output As String, Optional ByVal threshold As Integer = 0)
    46. #If CONFIG = "Debug" Then
    47.         Debug = New Debugger()
    48. #End If
    49.         Dim r As New Rectangle(0, 0, b.Width, b.Height)
    50.         Using fs As New FileStream(output, FileMode.Create, FileAccess.Write)
    51.             Using bw As New BinaryWriter(fs)
    52.                 Dim bd As BitmapData = b.LockBits(r, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb)
    53.                 Dim arr(bd.Width * bd.Height - 1) As Integer
    54.                 Marshal.Copy(bd.Scan0, arr, 0, arr.Length)
    55.  
    56.                 'Unlock the bitmap
    57.                 b.UnlockBits(bd)
    58.  
    59.                 'Prepare information
    60.                 Dim dictionary As New List(Of Integer)
    61.                 Dim data As New List(Of Integer)
    62.                 For i As Integer = 0 To arr.Length - 1
    63.                     While GetAComp(arr(i)) <= threshold
    64.                         i += 1
    65.                         If i = arr.Length Then Exit For
    66.                     End While
    67.  
    68.                     'Begin a new group
    69.                     data.Add(0I)
    70.                     Dim x As Integer = i Mod bd.Width
    71.                     Dim y As Integer = i \ bd.Width
    72.                     data.Add(x)
    73.                     data.Add(y)
    74.  
    75.                     While (i < arr.Length) AndAlso (GetAComp(arr(i)) > threshold)
    76.                         data.Add(arr(i))
    77.                         i += 1
    78.                     End While
    79.                 Next
    80.  
    81.                 'Compress the list
    82.                 'CompressList(data, dictionary, 30)
    83.  
    84.                 'Write header information
    85.                 For Each item As Integer In dictionary
    86.                     bw.Write(item)
    87.                 Next
    88.                 bw.Write(0I)
    89.                 bw.Write(bd.Width)
    90.                 bw.Write(bd.Height)
    91.  
    92.                 'Write data
    93.                 For Each item As Integer In data
    94.                     bw.Write(item)
    95.                 Next
    96.  
    97.                 'Close the streams.
    98.                 bw.Close()
    99.                 fs.Close()
    100.             End Using
    101.         End Using
    102. #If CONFIG = "Debug" Then
    103.         Debug.Dispose()
    104.         Debug = Nothing
    105. #End If
    106.     End Sub
    107. #End Region
    108.  
    109. #Region "FromFile"
    110.     Public Shared Function FromFile(ByVal f As String) As Bitmap
    111. #If CONFIG = "Debug" Then
    112.         Debug = New Debugger()
    113. #End If
    114.         Dim r As Bitmap = Nothing
    115.  
    116.         Using fs As New FileStream(f, FileMode.Open, FileAccess.Read)
    117.             Using br As New BinaryReader(fs)
    118.                 Dim dict As New List(Of Integer)
    119.                 Do
    120.                     Dim itm As Integer = br.ReadInt32()
    121.                     If itm = 0 Then Exit Do
    122.                     dict.Add(itm)
    123.                 Loop
    124.                 r = New Bitmap(br.ReadInt32(), br.ReadInt32(), PixelFormat.Format32bppArgb)
    125.                 Dim bd As BitmapData = r.LockBits(New Rectangle(0, 0, r.Width, r.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb)
    126.                 Dim arr(bd.Width * bd.Height - 1) As Integer
    127.                 Dim i As Integer = 0
    128.                 Do
    129.                     If br.BaseStream.Position = br.BaseStream.Length Then Exit Do
    130.                     Dim n_int As Integer = br.ReadInt32()
    131.                     If n_int = 0 Then 'next group
    132.                         Dim x As Integer = br.ReadInt32()
    133.                         Dim y As Integer = br.ReadInt32()
    134.                         Debug.WriteLine("Recieved coordinates " & x & "," & y & ".")
    135.                         i = (y * bd.Width) + x
    136.                         Continue Do
    137.                     End If
    138.                     If GetAComp(n_int) = 0 Then 'It's compressed
    139.                         Dim index As Integer = GetIndex(n_int)
    140.                         For j As Integer = 1 To GetRepetition(n_int)
    141.                             arr(i) = dict(index)
    142.                             i += 1
    143.                         Next
    144.                     Else 'Not compressed
    145.                         Debug.WriteLine("Current X is " & (i Mod bd.Width).ToString())
    146.                         Debug.WriteLine("Current Y is " & (i \ bd.Width).ToString())
    147.                         arr(i) = n_int
    148.                     End If
    149.                     i += 1
    150.                 Loop
    151.  
    152.                 'Copy back and unlock
    153.                 Marshal.Copy(arr, 0, bd.Scan0, arr.Length)
    154.                 r.UnlockBits(bd)
    155.  
    156.                 'Close the streams.
    157.                 br.Close()
    158.                 fs.Close()
    159.             End Using
    160.         End Using
    161. #If CONFIG = "Debug" Then
    162.         Debug.Dispose()
    163. #End If
    164.         Return r
    165.     End Function
    166. #End Region
    167.  
    168. #Region "GetXXX"
    169.     Friend Shared Function GetAComp(ByVal i As Integer) As Integer
    170.         Dim sh24 As Integer = 255I << 24
    171.         Dim res As Integer = i And sh24
    172.         Return res >> 24
    173.     End Function
    174.  
    175.     Friend Shared Function GetRepetition(ByVal i As Integer) As Integer
    176.         'Dim sh16 As Integer = 255 << 16
    177.         'Dim sh8 As Integer = 255 << 8
    178.         'Dim res As Integer = i And (sh16 Or sh8)
    179.         'Return res >> 8
    180.         Return (i Xor 255) >> 8
    181.     End Function
    182.  
    183.     Friend Shared Function GetIndex(ByVal i As Integer) As Integer
    184.         Return (i And 255)
    185.     End Function
    186.  
    187.     Friend Shared Function GetInt(ByVal count As Integer, ByVal index As Integer) As Integer
    188.         If count > Short.MaxValue OrElse index > 255 Then Throw New ArgumentException()
    189.         Return index Or (count << 8)
    190.     End Function
    191. #End Region
    192.  
    193. #Region "CompressList"
    194.     ''' <summary>
    195.     ''' Compresses a list of colors representing the first stage of an EAC image.
    196.     ''' </summary>
    197.     ''' <param name="l">The list of colors.</param>
    198.     ''' <param name="dict">The dictionary.</param>
    199.     ''' <param name="minrep">The minimum amount of repetitions.</param>
    200.     Private Shared Sub CompressList(ByRef l As List(Of Integer), ByRef dict As List(Of Integer), Optional ByVal minrep As Integer = 30)
    201.         dict.Clear()
    202.         Dim nl As New List(Of Integer)
    203.         Dim l_itm As Integer = -1
    204.         Dim count As Integer = 0
    205.         For i As Integer = 0 To l.Count - 1
    206.             Dim itm As Integer = l(i)
    207.             If itm = 0 Then
    208.                 nl.Add(0I)
    209.                 nl.Add(l(i + 1))
    210.                 nl.Add(l(i + 2))
    211.                 i += 2
    212.                 Continue For 'Will also add 1 to i.
    213.             End If
    214.             If count = 0 Then
    215.                 count = 1
    216.                 l_itm = itm
    217.             Else
    218.                 If l_itm = itm Then
    219.                     count += 1
    220.                     If (count < UInt16.MaxValue) AndAlso (dict.Count < 255) Then Continue For
    221.                 End If
    222.                 If count <= minrep Then
    223.                     For j As Integer = 1 To count
    224.                         nl.Add(itm)
    225.                     Next
    226.                 Else
    227.                     Dim idx As Integer = dict.IndexOf(itm)
    228.                     If idx = -1 Then
    229.                         dict.Add(itm)
    230.                         idx = dict.Count - 1
    231.                     End If
    232.                     nl.Add(GetInt(count, idx))
    233.                 End If
    234.                 l_itm = itm
    235.                 count = 1
    236.             End If
    237.         Next
    238.         l = nl
    239.     End Sub
    240. #End Region
    241.  
    242. End Class
    Attached Files Attached Files

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width