Option Strict Off
Option Explicit On

Imports System.IO

Friend Class Tgraphics


    Public mainwin As MainWindow
    Dim bytesread As Integer
    Public bytes As Integer

    Public compressed As Byte
    Public endian As Byte 'true = little, false = big
    Public checksum As Byte
    Public File As String
    Public first As Boolean

    'various files with data
    Public f556 As New file556
    Public f558 As New file558
    Public f559 As New file559
    Public f560 As New file560
    Public f561 As New file561
    Public f562 As New file562

    'size of all the items in the graphics.dat file
    Private itemsizes(800) As Integer
    Private itemsizescompressed(800) As Integer

    'the data of the items
    Private itemstrs(801) As IO.MemoryStream
    'the standard color palette?
    Private colorarray(17) As Integer
    'the custom modified color palette
    Private modifiedcolorarray(17) As Color
    'god this shit is gay
    Private itempics(801, 5) As System.Drawing.Image
    Private itempicsloaded(801, 5) As Boolean

    Private gitemsizes() As Integer
    Private gitemsizescompressed() As Integer
    Private gitemdatas() As IO.MemoryStream
    'Private gbitmaps(,) As Bitmap
    'Private gbitmapwidths() As Integer
    'Private gbitmapheights() As Integer
    Private gDC As Integer
    Private gMaskDC As Integer
    Public gMirrored As Integer


    'palette info
    Dim gpalettes()() As Color = ADGEDotNetUt.ArrayUt(Of Color).NewJagArray2(15, 255)
    Dim ginvertedpalettes()() As Color = ADGEDotNetUt.ArrayUt(Of Color).NewJagArray2(15, 255)
    Public gPALETTE_MAIN As Integer
    Public gPALETTE_TITLE As Integer
    Public gPALETTE_MAINMENU As Integer
    Public gPALETTE_CREDITS As Integer
    Public gPALETTE_CUSTOM As Integer
    Public curPalette As Integer

    Public numitems As Integer
    'Public GNumItems As Long

    '======================================
    'OBSOLETE FUNCTION SHOULD NOT BE USED!
    '======================================
    'Function loadgraphic(ByVal num As Integer, ByVal offset As Integer) As StdPicture
    '
    '    If mapfile.gettype(num) <> "IMG1" Then
    '        Set loadgraphic = loadgraphic(1, offset)
    '    Else
    '
    '        If itempicsloaded(num, offset) = False Then
    '            graphics.export_img1 itemstrs(num), App.apppath + "\temp.bmp", offset
    '            Set itempics(num, offset) = LoadPicture(App.apppath + "\temp.bmp")
    '            itempicsloaded(num, offset) = True
    '        End If
    '        Set loadgraphic = itempics(num, offset)
    '    End If
    'End Function
    '======================================
    'OBSOLETE FUNCTION SHOULD NOT BE USED!
    '======================================
    'Function reloadgraphic(ByVal num As Integer, ByVal offset As Integer) As StdPicture
    '
    '    If mapfile.gettype(num) <> "IMG1" Then
    '        Set reloadgraphic = reloadgraphic(1, offset)
    '    Else
    '
    '        graphics.export_img1 itemstrs(num), App.apppath + "\temp.bmp", offset
    '        Set itempics(num, offset) = LoadPicture(App.apppath + "\temp.bmp")
    '        itempicsloaded(num, offset) = True
    '        Set reloadgraphic = itempics(num, offset)
    '    End If
    'End Function

    '======================================
    'OBSOLETE FUNCTION SHOULD NOT BE USED!
    '======================================
    'Function resetmodifiedcolorarray()
    '    Dim i As Integer
    '    For i = 1 To 17
    '        modifiedcolorarray(i) = colorarray(i)
    '    Next
    'End Function
    'Function setmodifiedcolor(ByVal num As Integer, ByVal color As Long)
    '    modifiedcolorarray(num) = color
    'End Function
    'Function modifiedcolor(ByVal num As Integer) As Long
    '    modifiedcolor = modifiedcolorarray(num)
    'End Function
    'Function setcolor(ByVal num As Integer, ByVal color As Long) As Long
    '    colorarray(num) = color
    'End Function
    'Function color(ByVal num As Integer) As Long
    '    color = colorarray(num)
    'End Function
    '
    'Function forcereload(ByVal num As Integer, ByVal offset As Integer)
    '    itempicsloaded(num, offset) = False
    'End Function
    'Function reload() As Boolean
    '
    'End Function


    Sub setmirrored(ByVal num As Integer)
        If num = 0 Then
            gMirrored = 0
        Else
            gMirrored = 1
        End If
    End Sub
    Function getgbitmapwidth(ByVal num As Integer) As Integer
        getgbitmapwidth = gbitmapwidths(num)
    End Function
    Function getgbitmapheight(ByVal num As Integer) As Integer
        getgbitmapheight = gbitmapheights(num)
    End Function
    Sub setpalette(ByVal palettenum As Integer, ByVal colornum As Integer, ByVal color As Color)
        gpalettes(palettenum)(colornum) = color
        ginvertedpalettes(palettenum)(colornum) = Drawing.Color.FromArgb(color.B, color.G, color.R)
    End Sub
    Function getpalette(ByVal palettenum As Integer, ByVal colornum As Integer) As Color
        getpalette = gpalettes(palettenum)(colornum)
    End Function
    Function getinvertedpalette(ByVal palettenum As Integer, ByVal colornum As Integer) As Color
        getinvertedpalette = ginvertedpalettes(palettenum)(colornum)
    End Function
    Sub copypalette(ByVal src As Integer, ByVal dst As Integer)
        Dim i As Integer
        For i = 0 To 255
            gpalettes(dst)(i) = gpalettes(src)(i)
            ginvertedpalettes(dst)(i) = ginvertedpalettes(src)(i)
        Next
    End Sub
    Sub build_invertedpalettes()
        'we have to build an inverted set as well, dunno why
        Dim i As Integer
        Dim j As Integer
        Dim col As Color

        For i = 0 To 15
            For j = 0 To 255
                col = gpalettes(i)(j)
                ginvertedpalettes(i)(j) = Color.FromArgb(col.B, col.G, col.R)
            Next
        Next
    End Sub
    Sub build_palettes()
        Dim i As Integer
        Dim palettenum As Integer

        'build the main palette, as 0
        palettenum = 0
        gPALETTE_MAIN = palettenum

#If True Then
        For i = 0 To 15
            gpalettes(palettenum)(i) = MyWordClrUt.WordClrTo1Color(f562.Palette552(1).color(1 + i))
        Next
#Else
        gpalettes(palettenum)(0) = (Color.FromArgb(&H0, &H0, &H0))
        gpalettes(palettenum)(1) = (Color.FromArgb(&H6D, &H6D, &H6D))
        gpalettes(palettenum)(2) = (Color.FromArgb(&H91, &H91, &H91))
        gpalettes(palettenum)(3) = (Color.FromArgb(&H6D, &H24, &H0))
        gpalettes(palettenum)(4) = (Color.FromArgb(&H0, &HDA, &HDA))
        gpalettes(palettenum)(5) = (Color.FromArgb(&H91, &H48, &H0))
        gpalettes(palettenum)(6) = (Color.FromArgb(&H0, &H91, &H0))
        gpalettes(palettenum)(7) = (Color.FromArgb(&H0, &HDA, &H0))
        gpalettes(palettenum)(8) = (Color.FromArgb(&HFF, &H0, &H0))
        gpalettes(palettenum)(9) = (Color.FromArgb(&HFF, &HB6, &H0))
        gpalettes(palettenum)(10) = (Color.FromArgb(&HDA, &H91, &H6D))
        gpalettes(palettenum)(11) = (Color.FromArgb(&HFF, &HFF, &H0))
        gpalettes(palettenum)(12) = (Color.FromArgb(&H48, &H48, &H48))
        gpalettes(palettenum)(13) = (Color.FromArgb(&HB6, &HB6, &HB6))
        gpalettes(palettenum)(14) = (Color.FromArgb(&H0, &H0, &HFF))
        gpalettes(palettenum)(15) = (Color.FromArgb(&HFF, &HFF, &HFF))
#End If

        'gpalettes(palettenum, 0) = RGB(&H0, &H0, &H0)
        ' gpalettes(palettenum, 1) = RGB(&H6D, &H6D, &H6D)
        'gpalettes(palettenum, 2) = RGB(&H91, &H91, &H91)
        'gpalettes(palettenum, 3) = RGB(&H0, &H24, &H6D)
        'gpalettes(palettenum, 4) = RGB(&HDA, &HDA, &H0)
        'gpalettes(palettenum, 5) = RGB(&H0, &H48, &H91)
        'gpalettes(palettenum, 6) = RGB(&H0, &H91, &H0)
        'gpalettes(palettenum, 7) = RGB(&H0, &HDA, &H0)
        'gpalettes(palettenum, 8) = RGB(&H0, &H0, &HFF)
        'gpalettes(palettenum, 9) = RGB(&H0, &HB6, &HFF)
        'gpalettes(palettenum, 10) = RGB(&H6D, &H91, &HDA)
        'gpalettes(palettenum, 11) = RGB(&H0, &HFF, &HFF)
        'gpalettes(palettenum, 12) = RGB(&H48, &H48, &H48)
        'gpalettes(palettenum, 13) = RGB(&HB6, &HB6, &HB6)
        'gpalettes(palettenum, 14) = RGB(&HFF, &H0, &H0)
        'gpalettes(palettenum, 15) = RGB(&HFF, &HFF, &HFF)
        For i = 16 To 255
            gpalettes(palettenum)(i) = Color.Black  'set the rest to black
        Next

        'title screen palette
        palettenum = 1
        gPALETTE_TITLE = palettenum
        gpalettes(palettenum)(0) = Color.FromArgb(0, 0, 42)
        gpalettes(palettenum)(1) = Color.FromArgb(109, 109, 109)
        gpalettes(palettenum)(2) = Color.FromArgb(145, 145, 145)
        gpalettes(palettenum)(3) = Color.FromArgb(181, 140, 33)
        gpalettes(palettenum)(4) = Color.FromArgb(140, 66, 33)
        gpalettes(palettenum)(5) = Color.FromArgb(214, 181, 33)
        gpalettes(palettenum)(6) = Color.FromArgb(181, 107, 33)
        gpalettes(palettenum)(7) = Color.FromArgb(0, 0, 66)
        gpalettes(palettenum)(8) = Color.FromArgb(255, 255, 66)
        gpalettes(palettenum)(9) = Color.FromArgb(0, 0, 66)
        gpalettes(palettenum)(10) = Color.FromArgb(0, 0, 0)
        gpalettes(palettenum)(11) = Color.FromArgb(0, 0, 66)
        gpalettes(palettenum)(12) = Color.FromArgb(255, 0, 0)
        gpalettes(palettenum)(13) = Color.FromArgb(0, 0, 66)
        gpalettes(palettenum)(14) = Color.FromArgb(0, 0, 66)
        gpalettes(palettenum)(15) = Color.FromArgb(255, 255, 255)
        For i = 16 To 255
            gpalettes(palettenum)(i) = Color.Black 'set the rest to black
        Next

        'Main menu pallette
        palettenum = 2
        gPALETTE_MAINMENU = palettenum
#If True Then
        For i = 0 To 15
            gpalettes(palettenum)(i) = MyWordClrUt.WordClrTo1Color(f562.Palette360.color(1 + i))
        Next
#Else
        gpalettes(palettenum)(0) = Color.FromArgb(0, 0, 0)
        gpalettes(palettenum)(1) = Color.FromArgb(108, 108, 108)
        gpalettes(palettenum)(2) = Color.FromArgb(144, 144, 144)
        gpalettes(palettenum)(3) = Color.FromArgb(144, 72, 0)
        gpalettes(palettenum)(4) = Color.FromArgb(216, 180, 144)
        gpalettes(palettenum)(5) = Color.FromArgb(66, 33, 0)
        gpalettes(palettenum)(6) = Color.FromArgb(0, 144, 0)
        gpalettes(palettenum)(7) = Color.FromArgb(0, 180, 0)
        gpalettes(palettenum)(8) = Color.FromArgb(144, 108, 108)
        gpalettes(palettenum)(9) = Color.FromArgb(252, 0, 0)
        gpalettes(palettenum)(10) = Color.FromArgb(180, 144, 72)
        gpalettes(palettenum)(11) = Color.FromArgb(108, 72, 36)
        gpalettes(palettenum)(12) = Color.FromArgb(72, 72, 72)
        gpalettes(palettenum)(13) = Color.FromArgb(180, 180, 180)
        gpalettes(palettenum)(14) = Color.FromArgb(108, 36, 0)
        gpalettes(palettenum)(15) = Color.FromArgb(255, 255, 255)
#End If
        For i = 16 To 255
            gpalettes(palettenum)(i) = Color.Black 'set the rest to black
        Next

        'credits pallette
        palettenum = 3
        gPALETTE_CREDITS = palettenum
#If True Then
        For i = 0 To 15
            gpalettes(palettenum)(i) = MyWordClrUt.WordClrTo1Color(f562.Palette328.color(1 + i))
        Next
#Else
        gpalettes(palettenum)(0) = Color.FromArgb(0, 0, 0)
        gpalettes(palettenum)(1) = Color.FromArgb(0, 180, 180)
        gpalettes(palettenum)(2) = Color.FromArgb(252, 252, 180)
        gpalettes(palettenum)(3) = Color.FromArgb(140, 72, 0)
        gpalettes(palettenum)(4) = Color.FromArgb(252, 252, 144)
        gpalettes(palettenum)(5) = Color.FromArgb(0, 0, 0)
        gpalettes(palettenum)(6) = Color.FromArgb(0, 144, 0)
        gpalettes(palettenum)(7) = Color.FromArgb(180, 0, 0)
        gpalettes(palettenum)(8) = Color.FromArgb(216, 144, 72)
        gpalettes(palettenum)(9) = Color.FromArgb(252, 252, 180)
        gpalettes(palettenum)(10) = Color.FromArgb(252, 144, 72)
        gpalettes(palettenum)(11) = Color.FromArgb(252, 216, 0)
        gpalettes(palettenum)(12) = Color.FromArgb(252, 180, 0)
        gpalettes(palettenum)(13) = Color.FromArgb(0, 0, 0)
        gpalettes(palettenum)(14) = Color.FromArgb(108, 36, 0)
        gpalettes(palettenum)(15) = Color.FromArgb(252, 252, 216)
#End If
        For i = 16 To 255
            gpalettes(palettenum)(i) = Color.FromArgb(0, 0, 0) 'set the rest to black
        Next

        palettenum = 4
        gPALETTE_CUSTOM = palettenum
        'reserve the rest for custom palettes.
    End Sub

    Sub setsize(ByVal num As Integer)
        ReDim gitemsizes(num - 1)
        ReDim gitemsizescompressed(num - 1)
        ReDim gitemdatas(num - 1)
        'ReDim gbitmaps(num - 1, 1)
        'ReDim gbitmapwidths(num - 1)
        'ReDim gbitmapheights(num - 1)
    End Sub
    Function gread() As Boolean
        Dim i As Integer

        Using f135 As FileStream = IO.File.OpenRead(File)

            dlg_progresswindow.setcurrent((0))
            dlg_progresswindow.updateinterval = 8

            gread = False
            numitems = App.gread_int(f135)
            If numitems <> 563 Then
                MsgBox("CAUTION: Incorrect Dungeon size: " & Str(numitems) & " != 563")
            End If
            dlg_progresswindow.setmax((numitems))

            setsize(numitems)

            For i = 0 To numitems - 1
                gitemsizescompressed(i) = gread_int(f135)
            Next
            For i = 0 To numitems - 1
                gitemsizes(i) = gread_int(f135)
            Next

            'ReDim items(563, highest)
            For i = 0 To numitems - 1
                'For j = 1 To itemsizes(i)
                '    items(i, j) = read_byte
                'Next
                dlg_progresswindow.setcurrent((i))
                dlg_progresswindow.Title.Text = "Item #" & App.zerostr(i, 4)
                'dlg_progresswindow.Refresh
                gitemdatas(i) = New IO.MemoryStream(gread_bytes(gitemsizescompressed(i), f135))
                If gitemsizes(i) <> gitemsizescompressed(i) Then

                    App.dprint(("decompressing item #" & Str(i)))
                    App.itemdata = gitemdatas(i)
                    gitemdatas(i) = App.LZWUncompress()

                    Debug.Assert(gitemdatas(i).Length = gitemsizes(i), "The item #" & i & ", LZW decode may have failed. The expected size (" & gitemsizes(i) & ") doesn't match actual decoded size(" & gitemdatas(i).Length & ")")
                End If
            Next

        End Using

        dlg_progresswindow.setcurrent((0))
        dlg_progresswindow.updateinterval = 1
        dlg_progresswindow.setmax((6))
        dlg_progresswindow.Title.Text = "Loading file 556..."
        dlg_progresswindow.Refresh()
        f556.itemdata = gitemdatas(556)
        f556.itemlen = gitemsizes(556)
        f556.read()

        dlg_progresswindow.setcurrent((1))
        dlg_progresswindow.Title.Text = "Loading file 558..."
        dlg_progresswindow.Refresh()
        f558.itemdata = gitemdatas(558)
        f558.read()

        dlg_progresswindow.setcurrent((2))
        dlg_progresswindow.Title.Text = "Loading file 559..."
        dlg_progresswindow.Refresh()
        f559.itemdata = gitemdatas(559)
        f559.read()

        dlg_progresswindow.setcurrent((3))
        dlg_progresswindow.Title.Text = "Loading file 560..."
        dlg_progresswindow.Refresh()
        f560.itemdata = gitemdatas(560)
        f560.read()

        dlg_progresswindow.setcurrent((4))
        dlg_progresswindow.Title.Text = "Loading file 561..."
        dlg_progresswindow.Refresh()
        f561.itemdata = gitemdatas(561)
        f561.read()

        dlg_progresswindow.setcurrent((5))
        dlg_progresswindow.Title.Text = "Loading file 562..."
        dlg_progresswindow.Refresh()
        f562.itemdata = gitemdatas(562)
        f562.read()

        gread = True
        'init_colorarray
        build_palettes()
        build_invertedpalettes()

#If False Then
        dlg_progresswindow.setcurrent((0))
        dlg_progresswindow.setmax((numitems - 1))
        dlg_progresswindow.Refresh()
        'gDC = CreateCompatibleDC(mainwin.hdc)
        For i = 0 To numitems - 1
            dlg_progresswindow.setcurrent((i))
            dlg_progresswindow.Title.Text = "Creating bitmap #" & zerostr(i, 4) & "..."
            If mapfile.gettype_Renamed(i) = "IMG1" Then
                'build the bmp's
                buildbmp(i)
            End If
        Next
#End If
    End Function

    '======================================
    'OBSOLETE FUNCTION SHOULD NOT BE USED!
    '======================================
    'Function read() As Boolean
    '    Dim i, j As Integer
    '
    '    Open File For Binary Access Read As 135
    '
    '    dlg_progresswindow.setcurrent (0)
    '    dlg_progresswindow.updateinterval = 8
    '    bytesread = 0
    '    read = False
    '    endian = 0
    '    var = read_int
    '    numitems = var
    '    'If var <> 563 Then
    '    '    MsgBox "Incorrect Dungeon size: " + Str(var) + " != 563"
    '    '    read = False
    '    '    Close 135
    '    '    Exit Function
    '    'End If
    '    dlg_progresswindow.setmax (var)
    '
    '    i = 0
    '    For i = 1 To var
    '        itempicsloaded(i, 0) = False
    '        itempicsloaded(i, 1) = False
    '        itempicsloaded(i, 2) = False
    '        itempicsloaded(i, 3) = False
    '        itempicsloaded(i, 4) = False
    '        itempicsloaded(i, 5) = False
    '        itemsizescompressed(i) = read_int
    '    Next
    '    For i = 1 To var
    '        itemsizes(i) = read_int
    '        If itemsizes(i) > highest Then
    '            highest = itemsizes(i)
    '        End If
    '    Next
    '
    '    'ReDim items(563, highest)
    '    For i = 1 To var
    '        'For j = 1 To itemsizes(i)
    '        '    items(i, j) = read_byte
    '        'Next
    '        dlg_progresswindow.setcurrent (i)
    '        dlg_progresswindow.Title.Caption = "Item #" + Str(i)
    '        'dlg_progresswindow.Refresh
    '        j = read_bytes(i, itemsizescompressed(i))
    '        If itemsizes(i) <> itemsizescompressed(i) Then
    '            'MsgBox "Graphics are compressed, unsupported."
    '            'Exit Function
    '                    App.dprint ("decompressing item #" + Str(i))
    '            App.itemdata = itemstrs(i)
    '            itemstrs(i) = App.LZWUncompress()
    '        End If
    '    Next
    '
    '
    '    Close 135
    '
    '    dlg_progresswindow.setcurrent (0)
    '    dlg_progresswindow.updateinterval = 4
    '    dlg_progresswindow.setmax (50)
    '    dlg_progresswindow.Title.Caption = "Loading file 556..."
    '        dlg_progresswindow.Refresh
    '    f556.itemdata = itemstrs(557)
    '    f556.itemlen = itemsizes(557)
    '    f556.read
    '
    '    dlg_progresswindow.setcurrent (10)
    '    dlg_progresswindow.Title.Caption = "Loading file 558..."
    '        dlg_progresswindow.Refresh
    '    f558.itemdata = itemstrs(559)
    '    f558.read
    '
    '    dlg_progresswindow.setcurrent (20)
    '    dlg_progresswindow.Title.Caption = "Loading file 559..."
    '        dlg_progresswindow.Refresh
    '    f559.itemdata = itemstrs(560)
    '    f559.read
    '
    '    dlg_progresswindow.setcurrent (30)
    '    dlg_progresswindow.Title.Caption = "Loading file 560..."
    '        dlg_progresswindow.Refresh
    '    f560.itemdata = itemstrs(561)
    '    f560.read
    '
    '    dlg_progresswindow.setcurrent (40)
    '    dlg_progresswindow.Title.Caption = "Loading file 561..."
    '        dlg_progresswindow.Refresh
    '    f561.itemdata = itemstrs(562)
    '    f561.read
    '
    '    dlg_progresswindow.setcurrent (50)
    '    init_colorarray
    '
    '    read = True
    'End Function
    '
    '
    '======================================
    'OBSOLETE FUNCTION SHOULD NOT BE USED!
    '======================================
    'Function Save()
    '    Dim i, j As Long
    '
    '    f556.update
    '    f558.update
    '    f559.update
    '    f560.update
    '    f561.update
    '
    '    Open File For Binary Access Write As 135
    '
    ''    write_swapword 563 '563 items... static
    '    write_swapword numitems
    '    i = 0
    '    For i = 1 To numitems
    '        write_swapword itemsizes(i)
    '    Next
    '    For i = 1 To numitems
    '        write_swapword itemsizes(i)
    '    Next
    '
    '
    '    'ReDim items(numitems, highest)
    '    For i = 1 To numitems
    '        'For j = 1 To itemsizes(i)
    '        '    items(i, j) = read_byte
    '        'Next
    '        'j = read_bytes(i, itemsizes(i))
    '        For j = 1 To itemsizes(i)
    '            write_byte AscB(MidB(itemstrs(i), j, 1))
    '        Next
    '    Next
    '
    '    Close 135
    'End Function

    Sub gsave()
        Dim i As Integer

        f556.update()
        f558.update()
        f559.update()
        f560.update()
        f561.update()
        f562.update()

        Using f135 As FileStream = IO.File.Create(File)
            '    write_swapword 563 '563 items... static
            write_swapword(numitems, f135)
            i = 0
            For i = 0 To numitems - 1
                write_swapword(gitemsizes(i), f135)
            Next
            For i = 0 To numitems - 1
                write_swapword(gitemsizes(i), f135)
            Next


            'ReDim items(numitems, highest)
            For i = 0 To numitems - 1
                'For j = 1 To itemsizes(i)
                '    items(i, j) = read_byte
                'Next
                'j = read_bytes(i, itemsizes(i))
                gitemdatas(i).WriteTo(f135)
            Next

            f135.Close()
        End Using
    End Sub

    '======================================
    'OBSOLETE FUNCTION SHOULD NOT BE USED!
    '======================================
    'Function init_colorarray()
    '
    '    colorarray(1) = (build_color(&H0, &H0, &H0))
    '    colorarray(2) = (build_color(&H6D, &H6D, &H6D))
    '    colorarray(3) = (build_color(&H91, &H91, &H91))
    '    colorarray(4) = (build_color(&H6D, &H24, &H0))
    '    colorarray(5) = (build_color(&H0, &HDA, &HDA))
    '    colorarray(6) = (build_color(&H91, &H48, &H0))
    '    colorarray(7) = (build_color(&H0, &H91, &H0))
    '    colorarray(8) = (build_color(&H0, &HDA, &H0))
    '    colorarray(9) = (build_color(&HFF, &H0, &H0))
    '    colorarray(10) = (build_color(&HFF, &HB6, &H0))
    '    colorarray(11) = (build_color(&HDA, &H91, &H6D))
    '    colorarray(12) = (build_color(&HFF, &HFF, &H0))
    '    colorarray(13) = (build_color(&H48, &H48, &H48))
    '    colorarray(14) = (build_color(&HB6, &HB6, &HB6))
    '    colorarray(15) = (build_color(&H0, &H0, &HFF))
    '    colorarray(16) = (build_color(&HFF, &HFF, &HFF))
    '    colorarray(17) = (build_color(&HFF, &H0, &HFF))
    '
    '    Dim i As Integer
    '    For i = 1 To 17
    '        modifiedcolorarray(i) = colorarray(i)
    '    Next
    'End Function

    'TODO: REIMPLEMENT THIS WITH CUSTOM PALETTE
    Function gfindcolor(ByVal color As Color) As Byte
        'find a color nearest to the color array

        Dim i As Short
        Dim closest As Integer
        Dim closest_len As Integer
        Dim length As Integer
        Dim b2, g2, r2, r1, g1, b1 As Integer
        Dim rd, gd, bd As Integer
        Dim l1 As Color

        b2 = color.B
        g2 = color.G
        r2 = color.R

        closest = -1
        For i = 0 To 15
            'l1 = modifiedcolorarray(i)
            l1 = gpalettes(curPalette)(i)

            b1 = l1.B
            g1 = l1.G
            r1 = l1.R

            rd = System.Math.Abs(r2 - r1)
            gd = System.Math.Abs(g2 - g1)
            bd = System.Math.Abs(b2 - b1)

            length = rd * rd + gd * gd + bd * bd

            If closest = -1 Then
                closest = i
                closest_len = length
            Else
                If length < closest_len Then
                    closest = i
                    closest_len = length
                End If
            End If
        Next

        gfindcolor = closest
        'If closest_len <> 0 Then
        '    MsgBox "Could not find color " + Hex(color) + ", using " + Hex(colorarray(closest))
        'MsgBox "Closest_len = " + Str(closest_len)
        'End If
        'If findcolor > 15 Then
        '    MsgBox "Findcolor > 15 = " + Str(findcolor)
        'End If
    End Function

    '========= findcolor now obsolete ========
    'Function findcolor(ByVal color As Long) As Byte
    '    'find a color nearest to the color array
    '
    '    Dim i As Integer
    '    Dim closest As Integer
    '    Dim closest_len As Long
    '    Dim length As Long
    '    Dim r1, r2, g1, g2, b1, b2 As Byte
    '    Dim rd, gd, bd As Long
    '    Dim l1 As Long
    '
    '    closest = -1
    '    For i = 1 To 17
    '        l1 = modifiedcolorarray(i)
    '        b1 = (l1 And 255)
    '        g1 = (l1 And 65280) / &H100
    '        r1 = (l1 And 16711425) / &H10000
    '
    '        l1 = color
    '        b2 = (l1 And 255)
    '        g2 = (l1 And 65280) / &H100
    '        r2 = (l1 And 16711425) / &H10000
    '
    '        rd = Abs(r2 - r1)
    '        gd = Abs(g2 - g1)
    '        bd = Abs(b2 - b1)
    '
    '        length = rd * rd + gd * gd + bd * bd
    '
    '        If closest = -1 Then
    '            closest = i
    '            closest_len = length
    '        Else
    '            If length < closest_len Then
    '                closest = i
    '                closest_len = length
    '            End If
    '        End If
    '    Next
    '
    '    findcolor = closest - 1
    '    'If closest_len <> 0 Then
    '    '    MsgBox "Could not find color " + Hex(color) + ", using " + Hex(colorarray(closest))
    '        'MsgBox "Closest_len = " + Str(closest_len)
    '    'End If
    '    'If findcolor > 15 Then
    '    '    MsgBox "Findcolor > 15 = " + Str(findcolor)
    '    'End If
    'End Function

    Function read_bytes(ByVal itemnum As Short, ByVal num As Integer, ByVal f135 As Stream) As Integer
        'UPGRADE_ISSUE: InputB function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        itemstrs(itemnum) = New IO.MemoryStream(New BinaryReader(f135).ReadBytes(num))
    End Function
    Function read_byte(ByVal f135 As Stream) As Byte
        'UPGRADE_ISSUE: InputB function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        'UPGRADE_ISSUE: AscB function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        read_byte = New BinaryReader(f135).ReadByte()
        bytesread = bytesread + 1
    End Function

    Function read_int(ByVal f135 As Stream) As Integer
        Dim b1 As Byte
        Dim b2 As Byte
        If endian = 1 Then 'little endian
            'UPGRADE_WARNING: Couldn't resolve default property of object b1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            b1 = read_byte(f135)
            b2 = read_byte(f135)
        Else
            b2 = read_byte(f135)
            'UPGRADE_WARNING: Couldn't resolve default property of object b1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            b1 = read_byte(f135)
        End If

        read_int = b2
        read_int = read_int * 256
        'UPGRADE_WARNING: Couldn't resolve default property of object b1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        read_int = read_int + b1
    End Function

    Public Function sread_byte(ByVal data As IO.MemoryStream, ByVal offset1based As Integer) As Byte
        'UPGRADE_ISSUE: MidB function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        'UPGRADE_ISSUE: AscB function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        data.Position = offset1based - 1
        sread_byte = New IO.BinaryReader(data).ReadByte()
    End Function
    Public Function sread_word(ByVal data As IO.MemoryStream, ByVal offset1based As Integer) As Integer
        sread_word = sread_byte(data, offset1based) * &H100
        sread_word = sread_word + sread_byte(data, offset1based + 1)
    End Function

    'UPGRADE_NOTE: val was upgraded to val_Renamed. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
    Public Sub swrite_byte(ByRef data As IO.MemoryStream, ByVal val_Renamed As Byte)
        'UPGRADE_ISSUE: ChrB function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        data.Position = data.Length
        data.WriteByte(val_Renamed)
    End Sub
    'UPGRADE_NOTE: val was upgraded to val_Renamed. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
    Public Sub swrite_word(ByRef data As IO.MemoryStream, ByVal val_Renamed As Integer)
        Dim b1 As Byte
        Dim b2 As Byte
        Dim l1 As Integer
        l1 = val_Renamed And &HFF00
        'UPGRADE_WARNING: Couldn't resolve default property of object b1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        b1 = l1 / &H100
        b2 = val_Renamed And &HFF
        'UPGRADE_WARNING: Couldn't resolve default property of object b1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        swrite_byte(data, b1)
        swrite_byte(data, b2)
    End Sub

    Sub write_byte(ByRef num As Byte, ByVal f135 As Stream)
        'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
        f135.WriteByte(num)
    End Sub
    Sub write_word(ByVal num As Short, ByVal f135 As Stream)
        Dim b1 As Byte
        Dim b2 As Byte
        Dim l1 As Integer
        l1 = num And &HFF00
        'UPGRADE_WARNING: Couldn't resolve default property of object b1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        b1 = l1 / &H100
        b2 = num And &HFF
        write_byte(b2, f135)
        'UPGRADE_WARNING: Couldn't resolve default property of object b1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        write_byte(b1, f135)
    End Sub
    Sub write_swapword(ByVal num As Integer, ByVal f135 As Stream)
        Dim b1 As Byte
        Dim b2 As Byte
        Dim l1 As Integer
        l1 = num And &HFF00
        'UPGRADE_WARNING: Couldn't resolve default property of object b1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        b1 = l1 / &H100
        b2 = num And &HFF
        'UPGRADE_WARNING: Couldn't resolve default property of object b1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        write_byte(b1, f135)
        write_byte(b2, f135)
    End Sub
    Sub write_swaplong(ByVal num As Integer, ByVal f135 As Stream)
        Dim b2, b1, b3 As Integer
        Dim b4 As Byte
        Dim l1 As Integer
        'l1 = num And 4278190080#
        l1 = num
        l1 = l1 / &H1000000
        If l1 < 0 Then
            l1 = l1 + 256
        End If
        'UPGRADE_WARNING: Couldn't resolve default property of object b1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        b1 = l1
        l1 = num And 16711680
        'UPGRADE_WARNING: Couldn't resolve default property of object b2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        b2 = l1 / &H10000
        l1 = num And 65280
        'UPGRADE_WARNING: Couldn't resolve default property of object b3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        b3 = l1 / &H100
        b4 = num And 255
        'UPGRADE_WARNING: Couldn't resolve default property of object b2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        write_byte(b2, f135)
        'UPGRADE_WARNING: Couldn't resolve default property of object b3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        write_byte(b3, f135)
        write_byte(b4, f135)
        'UPGRADE_WARNING: Couldn't resolve default property of object b1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        write_byte(b1, f135)
    End Sub
    Sub write_long(ByVal num As Integer, ByVal f135 As Stream)
        Dim b2, b1, b3 As Integer
        Dim b4 As Byte
        Dim l1 As Integer
        'l1 = num And 4278190080#
        l1 = num
        l1 = l1 / &H1000000
        If l1 < 0 Then
            l1 = l1 + 256
        End If
        b1 = l1
        l1 = num And 16711680
        b2 = l1 / &H10000
        l1 = num And 65280
        b3 = l1 / &H100
        b4 = num And 255
        write_byte(b4, f135)
        write_byte(b3, f135)
        write_byte(b2, f135)
        write_byte(b1, f135)
    End Sub

    Function getgitemdata(ByVal Index As Integer) As MemoryStream
        getgitemdata = gitemdatas(Index)
    End Function
    Sub setgitemdata(ByVal Index As Integer, ByVal data As MemoryStream)
        gitemdatas(Index) = data
        'update the size? nah
        'gitemsizes(index) = LenB(data)
    End Sub
    Function getgitemsize(ByVal Index As Integer) As Integer
        getgitemsize = gitemsizes(Index)
    End Function
    Sub setgitemsize(ByVal Index As Integer, ByVal num As Integer)
        gitemsizes(Index) = num
    End Sub
    Function getgitemsizecompressed(ByVal Index As Integer) As Integer
        getgitemsizecompressed = gitemsizescompressed(Index)
    End Function
    Sub setgitemsizecompressed(ByVal Index As Integer, ByVal num As Integer)
        gitemsizescompressed(Index) = num
    End Sub

    '======================================
    'OBSOLETE FUNCTION SHOULD NOT BE USED!
    '======================================
    'Function itemsize(ByVal i As Integer) As Long
    '    itemsize = itemsizes(i)
    'End Function
    'Sub setitemsize(ByVal Index As Integer, ByVal num As Long)
    '    itemsizes(Index) = num
    'End Sub
    'Function compresseditemsize(ByVal i As Integer) As Long
    '    compresseditemsize = itemsizescompressed(i)
    'End Function
    'Sub setcompresseditemsize(ByVal Index As Integer, ByVal num As Long)
    '    itemsizescompressed(Index) = num
    'End Sub
    'Function itemdata(ByVal Item As Integer, ByVal offset As Integer) As Byte
    '    itemdata = AscB(MidB(itemstrs(Item), offset, 1))
    'End Function
    'Function itemint(ByVal Item As Integer, ByVal offset As Integer) As Long
    '    If Item < 0 Or Item > 800 Then
    '        itemint = 0
    '    ElseIf offset + 1 > itemsizes(Item) Then
    '        itemint = 0
    '    Else
    '        itemint = AscB(MidB(itemstrs(Item), offset, 1))
    '        itemint = itemint * 256
    '        itemint = itemint + AscB(MidB(itemstrs(Item), offset + 1, 1))
    '    End If
    'End Function
    'Sub setitemstr(ByVal Index As Integer, ByVal s As String)
    '    itemstrs(Index) = s
    'End Sub
    'Function itemstr(ByVal Index As Integer) As String
    '    itemstr = itemstrs(Index)
    'End Function



    '======================================
    'OBSOLETE FUNCTION SHOULD NOT BE USED!
    '======================================
    'Public Function build_color(ByVal Red As Byte, ByVal Green As Byte, ByVal Blue As Byte) As Long
    '    Dim l1, l2, l3, l4 As Long
    '    l1 = 0
    '    l2 = Red
    '    l3 = Green
    '    l4 = Blue
    '    l3 = l3 * 256
    '    l4 = l4 * 65536
    '    build_color = l1 + l2 + l3 + l4
    'End Function

    'TODO: Fixme for new item storage type
    'UPGRADE_NOTE: mapfile was upgraded to mapfile_Renamed. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
    Public Sub gExport(ByVal itemnum As Short, ByVal fname As String)
        Dim ItemType As String
        'UPGRADE_WARNING: Couldn't resolve default property of object mapfile.gettype. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        ItemType = mapfile.gettype_Renamed(itemnum)

        If ItemType = "IMG1" Then
            gexport_img1(itemnum, fname, False)

        ElseIf ItemType = "TXT1" Then
            gexport_txt1(itemnum, fname)
        Else
            gexport_raw(itemnum, fname)
        End If


    End Sub

    ' == OBSOLETE, use gExport
    'Public Sub Export(ByVal itemnum As Integer, ByVal fname As String)
    '
    '    Dim ItemType As String
    '    ItemType = mapfile.gettype(itemnum)
    '
    '    If ItemType = "IMG1" Then
    '        export_img1 itemstrs(itemnum), fname, False
    '
    '    ElseIf ItemType = "TXT1" Then
    '        export_txt1 itemstrs(itemnum), fname
    '    Else
    '        export_raw itemstrs(itemnum), fname
    '    End If
    '
    '
    'End Sub

    'not implemented
    Public Sub gexport_txt1(ByVal itemnum As Integer, ByVal fname As String)

    End Sub
    'TODO: Fixme for new item storage type
    'TODO: add optimization for using app.gwrite_bytes
    Public Sub gexport_raw(ByVal itemnum As Integer, ByVal fname As String)
        IO.File.WriteAllBytes(fname, gitemdatas(itemnum).ToArray())
    End Sub
    ' == now obsolete, use gexport_txt1 and gexport_raw
    'not implemented
    'Public Sub export_txt1(ByVal itemdata As String, ByVal fname As String)
    '
    'End Sub
    ''TODO: Fixme for new item storage type
    'Public Sub export_raw(ByVal itemdata As String, ByVal fname As String)
    '
    '    Dim itemlen As Long
    '
    '    itemlen = LenB(itemdata)
    '
    '    Open fname For Binary Access Write As 135
    '
    '    For i = 1 To itemlen
    '        write_byte AscB(MidB(itemdata, i, 1))
    '    Next
    '
    '    Close 135
    'End Sub

    'TODO: Fixme for new item storage type
    'should be fine as is?
    Public Function import_raw(ByRef itemdata As MemoryStream, ByVal fname As String) As Integer
        import_raw = FileLen(fname)

        itemdata = New IO.MemoryStream(IO.File.ReadAllBytes(fname))
    End Function

    'TODO: Fix for custom palette etc
    'TODO: Fixme for new item storage type
    'actually, this should be fine as is
    Public Function encode_img1(ByRef pixelarray() As Byte, ByVal width As Integer, ByVal height As Integer) As MemoryStream
        encode_img1 = New IO.MemoryStream
        swrite_word(encode_img1, width)
        swrite_word(encode_img1, height)

        Dim j, i As Integer
        Dim n As Integer
        Dim b As Byte
        Dim max As Integer
        Dim pixelcount_same As Integer
        Dim pixelcount_line As Integer
        Dim pixelcolor As Integer
        Dim nextpixel As Integer
        Dim nibblearray(800000) As Byte

        max = width
        max = max * height
        n = 1
        For i = 1 To max
            'UPGRADE_WARNING: Couldn't resolve default property of object i. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            pixelcolor = pixelarray(i)

            'UPGRADE_WARNING: Couldn't resolve default property of object i. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            If pixelarray(i) = 16 Then 'transparent pixel(s)

                'UPGRADE_WARNING: Couldn't resolve default property of object i. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                For j = i To max
                    'UPGRADE_WARNING: Couldn't resolve default property of object j. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    If pixelarray(j) <> pixelcolor Or j = max Then
                        'UPGRADE_WARNING: Couldn't resolve default property of object i. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        'UPGRADE_WARNING: Couldn't resolve default property of object j. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        pixelcount_same = j - i
                        'UPGRADE_WARNING: Couldn't resolve default property of object j. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        j = max + 1
                    End If
                Next

                If pixelcount_same <= 16 Then
                    nibblearray(n) = 10
                    n = n + 1
                    nibblearray(n) = pixelcount_same
                    n = n + 1
                ElseIf pixelcount_same <= 29 Then
                    nibblearray(n) = 14
                    n = n + 1
                    nibblearray(n) = pixelcount_same - 17
                    n = n + 1
                ElseIf pixelcount_same <= 256 Then
                    nibblearray(n) = 14
                    n = n + 1
                    nibblearray(n) = 13
                    n = n + 1
                    nibblearray(n) = CShort((pixelcount_same - 1) And 240) / 16
                    n = n + 1
                    nibblearray(n) = (pixelcount_same - 1) And 15
                    n = n + 1
                ElseIf pixelcount_same <= 512 Then
                    nibblearray(n) = 14
                    n = n + 1
                    nibblearray(n) = 14
                    n = n + 1
                    nibblearray(n) = CShort((pixelcount_same - 257) And 240) / 16
                    n = n + 1
                    nibblearray(n) = (pixelcount_same - 257) And 15
                    n = n + 1
                Else
                    nibblearray(n) = 14
                    n = n + 1
                    nibblearray(n) = 15
                    n = n + 1
                    If pixelcount_same > 65536 Then
                        pixelcount_same = 65536
                    End If

                    nibblearray(n) = CShort((pixelcount_same - 1) And 61440) / 4096
                    n = n + 1
                    nibblearray(n) = CShort((pixelcount_same - 1) And 3840) / 256
                    n = n + 1
                    nibblearray(n) = CShort((pixelcount_same - 1) And 240) / 16
                    n = n + 1
                    nibblearray(n) = (pixelcount_same - 1) And 15
                    n = n + 1
                End If
                'UPGRADE_WARNING: Couldn't resolve default property of object i. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                i = i + pixelcount_same - 1
            Else
                pixelcount_same = 1
                'UPGRADE_WARNING: Couldn't resolve default property of object i. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                For j = i To max
                    'UPGRADE_WARNING: Couldn't resolve default property of object j. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    If pixelarray(j) <> pixelcolor Or j = max Then
                        'UPGRADE_WARNING: Couldn't resolve default property of object i. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        'UPGRADE_WARNING: Couldn't resolve default property of object j. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        pixelcount_same = j - i
                        'UPGRADE_WARNING: Couldn't resolve default property of object j. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        j = max + 1
                    End If
                Next
                If pixelcount_same < 1 Then
                    pixelcount_same = 1
                End If

                pixelcount_line = 0
                'UPGRADE_WARNING: Couldn't resolve default property of object i. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                If (i - 1) > width Then 'check for copying from above

                    'UPGRADE_WARNING: Couldn't resolve default property of object i. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    For j = i To max - 1
                        'UPGRADE_WARNING: Couldn't resolve default property of object j. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        If pixelarray(j) <> pixelarray(j - width) Or j = (max - 1) Then
                            'UPGRADE_WARNING: Couldn't resolve default property of object i. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                            'UPGRADE_WARNING: Couldn't resolve default property of object j. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                            pixelcount_line = j - i
                            'UPGRADE_WARNING: Couldn't resolve default property of object j. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                            j = max + 1
                        End If
                    Next
                End If

                If pixelcount_line > pixelcount_same Then
                    'do copybits from above line
                    'UPGRADE_WARNING: Couldn't resolve default property of object i. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    nextpixel = pixelarray(i + pixelcount_line)
                    If pixelcount_line <= 256 Then
                        nibblearray(n) = 11
                        n = n + 1
                        nibblearray(n) = nextpixel
                        n = n + 1
                        nibblearray(n) = CShort((pixelcount_line - 1) And 240) / 16
                        n = n + 1
                        nibblearray(n) = (pixelcount_line - 1) And 15
                        n = n + 1
                    Else
                        nibblearray(n) = 15
                        n = n + 1
                        nibblearray(n) = nextpixel
                        n = n + 1

                        If pixelcount_line > 65536 Then
                            pixelcount_line = 65536
                        End If

                        nibblearray(n) = CShort((pixelcount_line - 1) And 61440) / 4096
                        n = n + 1
                        nibblearray(n) = CShort((pixelcount_line - 1) And 3840) / 256
                        n = n + 1
                        nibblearray(n) = CShort((pixelcount_line - 1) And 240) / 16
                        n = n + 1
                        nibblearray(n) = (pixelcount_line - 1) And 15
                        n = n + 1
                    End If
                    'UPGRADE_WARNING: Couldn't resolve default property of object i. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    i = i + pixelcount_line
                Else
                    If pixelcount_same <= 8 Then
                        nibblearray(n) = pixelcount_same - 1
                        n = n + 1
                        nibblearray(n) = pixelcolor
                        n = n + 1
                    ElseIf pixelcount_same <= 256 Then
                        nibblearray(n) = 8
                        n = n + 1
                        nibblearray(n) = pixelcolor
                        n = n + 1
                        nibblearray(n) = CShort((pixelcount_same - 1) And 240) / 16
                        n = n + 1
                        nibblearray(n) = (pixelcount_same - 1) And 15
                        n = n + 1
                    Else
                        nibblearray(n) = 12
                        n = n + 1
                        nibblearray(n) = pixelcolor
                        n = n + 1

                        If pixelcount_same > 65536 Then
                            pixelcount_same = 65536
                        End If

                        nibblearray(n) = CShort((pixelcount_same - 1) And 61440) / 4096
                        n = n + 1
                        nibblearray(n) = CShort((pixelcount_same - 1) And 3840) / 256
                        n = n + 1
                        nibblearray(n) = CShort((pixelcount_same - 1) And 240) / 16
                        n = n + 1
                        nibblearray(n) = (pixelcount_same - 1) And 15
                        n = n + 1
                    End If
                    'UPGRADE_WARNING: Couldn't resolve default property of object i. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    i = i + pixelcount_same - 1
                End If
                'swrite_byte encode_img1, pixelarray(i)
            End If
        Next
        n = n - 1
        For i = 1 To n
            'UPGRADE_WARNING: Couldn't resolve default property of object i. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            b = nibblearray(i) * 16
            'UPGRADE_WARNING: Couldn't resolve default property of object i. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            i = i + 1
            'UPGRADE_WARNING: Couldn't resolve default property of object i. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            b = b + nibblearray(i)
            swrite_byte(encode_img1, b)
        Next
    End Function

    'TODO: Fixme for new item storage type
    Public Sub gexport_img1(ByVal itemnum As Integer, ByVal fname As String, ByRef mirrored As Short)

        ' if mirrored > 0 then mirror it, else dont... dont use that old shit
        'construct nibblearray
        Dim nibbles() As Byte
        Dim nibblelen As Integer
        Dim pixelarray() As Byte
        Dim pwidth As Integer
        Dim pheight As Integer


        Dim i As Integer
        Dim j As Integer
        Dim p As Integer
        Dim b As Integer
        Dim n4, n2, n1, n3, n5 As Integer
        Dim n6 As Byte

        Dim itemlen As Integer

        'dprint ("function: export_img1")
        itemlen = gitemsizes(itemnum)
        'itemlen = LenB(itemdata)

        'dprint ("itemlen: " + Str(itemlen))
        ReDim nibbles(itemlen * 2)

        pwidth = sread_word(gitemdatas(itemnum), 1)
        pheight = sread_word(gitemdatas(itemnum), 3)

        'dprint ("width: " + Str(pwidth) + " height: " + Str(pheight))
        ReDim pixelarray(pwidth * pheight + 2048)
        'dprint ("pixelarray size: " + Str(pwidth * pheight + 2048))
        j = 1
        For i = 5 To itemlen
            b = sread_byte(gitemdatas(itemnum), i)
            nibbles(j) = CShort(b And &HF0) / 16
            j = j + 1
            nibbles(j) = (b And &HF)
            j = j + 1
        Next

        nibblelen = j - 1

        p = 1
        For i = 1 To nibblelen

            'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            n1 = nibbles(i)
            'dprint ("n1 = nibble(" + Str(i) + "): " + Str(n1))
            i = i + 1
            'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            n2 = nibbles(i)
            'dprint ("n2 = nibble(" + Str(i) + "): " + Str(n2))
            'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            If n1 <= 7 Then
                'dprint ("write n1+1(" + Str(n1 + 1) + ") pixels of color n2(" + Str(n2) + ")")
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n1 = n1 + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                For j = 1 To n1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    pixelarray(p) = n2
                    p = p + 1
                Next
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 8 Then
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n3 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n4 = nibbles(i)
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                b = (n3 * 16) + n4
                b = b + 1
                'dprint ("read 2 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
                'dprint ("write b(" + Str(b) + ") pixels of color n2(" + Str(n2) + ")")
                For j = 1 To b
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    pixelarray(p) = n2
                    p = p + 1
                Next
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 9 Then
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n3 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n4 = nibbles(i)
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                b = (n3 * 16) + n4
                'dprint ("read 2 nibbles as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
                If b Mod &H2 = 1 Then 'odd
                    'dprint ("b is odd (b=b+1):")
                    b = b + 1
                    'dprint ("write b(" + Str(b) + ") pixels by reading one nibble for each color")
                    For j = 1 To b
                        i = i + 1
                        'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        n2 = nibbles(i)
                        ''dprint ("write pixel of color n2(" + Str(n2) + ")")
                        'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        pixelarray(p) = n2
                        p = p + 1
                    Next
                Else
                    'dprint ("b is even:")
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    pixelarray(p) = n2
                    'dprint ("write pixel of color " + Str(n2))
                    p = p + 1
                    'dprint ("write b(" + Str(b) + ") pixels by reading one nibble for each color")
                    For j = 1 To b
                        i = i + 1
                        'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        n2 = nibbles(i)
                        'dprint ("write pixel of color " + Str(n2))
                        'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        pixelarray(p) = n2
                        p = p + 1
                    Next
                End If
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 10 Then
                'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n2 = n2 + 1
                'dprint ("write n2+1(" + Str(n2) + " transparent pixels")
                'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                For j = 1 To n2
                    pixelarray(p) = 16
                    p = p + 1
                Next
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 11 Then
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n3 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n4 = nibbles(i)
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                b = (n3 * 16) + n4
                b = b + 1
                'dprint ("read 2 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
                'dprint ("write b(" + Str(b) + " pixels by coping them from the row above")
                For j = 1 To b
                    'dprint ("write pixel of color " + Str(pixelarray(p - pwidth)))
                    pixelarray(p) = pixelarray(p - pwidth)
                    p = p + 1
                Next
                'dprint ("write pixel of color n2(" + Str(n2) + ")")
                'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                pixelarray(p) = n2
                p = p + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 12 Then
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n3 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n4 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n5. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n5 = nibbles(i)
                i = i + 1
                n6 = nibbles(i)
                'UPGRADE_WARNING: Couldn't resolve default property of object n5. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                b = (n3 * 4096) + (n4 * 256) + (n5 * 16) + n6
                b = b + 1
                'dprint ("read 4 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + ",n5: " + Str(n5) + ",n6: " + Str(n6) + "): " + Str(b))
                'dprint ("write " + Str(b) + " pixels of color n2(" + Str(n2) + ")")
                For j = 1 To b
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    pixelarray(p) = n2
                    p = p + 1
                Next
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 13 Then
                'dprint ("do nothing")
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 14 Then
                'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                If n2 < 13 Then
                    'dprint ("n2 < 13:")
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n2 = n2 + 17
                    'dprint ("write n2+17(" + Str(n2) + ") transparent pixels")
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    For j = 1 To n2
                        pixelarray(p) = 16
                        p = p + 1
                    Next
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                ElseIf n2 = 13 Then
                    'dprint ("n2 = 13:")
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n3 = nibbles(i)
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n4 = nibbles(i)
                    'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    b = (n3 * 16) + n4
                    b = b + 1
                    'dprint ("read 2 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
                    'dprint ("write b(" + Str(b) + ") transparent pixels")
                    For j = 1 To b
                        pixelarray(p) = 16
                        p = p + 1
                    Next
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                ElseIf n2 = 14 Then
                    'dprint ("n2 = 14:")
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n3 = nibbles(i)
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n4 = nibbles(i)
                    'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    b = (n3 * 16) + n4
                    b = b + 257
                    'dprint ("read 2 nibbles + 257 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
                    'dprint ("write b(" + Str(b) + ") transparent pixels")
                    For j = 1 To b
                        pixelarray(p) = 16
                        p = p + 1
                    Next
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                ElseIf n2 = 15 Then
                    'dprint ("n2 = 15:")
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n3 = nibbles(i)
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n4 = nibbles(i)
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n5. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n5 = nibbles(i)
                    i = i + 1
                    n6 = nibbles(i)
                    'UPGRADE_WARNING: Couldn't resolve default property of object n5. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    b = (n3 * 4096) + (n4 * 256) + (n5 * 16) + n6
                    b = b + 1
                    'dprint ("read 4 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + ",n5: " + Str(n5) + ",n6: " + Str(n6) + "): " + Str(b))
                    'dprint ("write b(" + Str(b) + ") transparent pixels")
                    For j = 1 To b
                        pixelarray(p) = 16
                        p = p + 1
                    Next
                End If
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 15 Then
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n3 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n4 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n5. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n5 = nibbles(i)
                i = i + 1
                n6 = nibbles(i)
                'UPGRADE_WARNING: Couldn't resolve default property of object n5. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                b = (n3 * 4096) + (n4 * 256) + (n5 * 16) + n6
                b = b + 1
                'dprint ("read 4 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + ",n5: " + Str(n5) + ",n6: " + Str(n6) + "): " + Str(b))
                'dprint ("write b(" + Str(b) + ") pixels by coping them from the row above")
                For j = 1 To b
                    If p - pwidth < 0 Then
                        'dprint ("write pixel of color 0 ***not on second line of graphic")
                        pixelarray(p) = 0
                    Else
                        'dprint ("write pixel of color " + Str(pixelarray(p - pwidth)))
                        pixelarray(p) = pixelarray(p - pwidth)
                    End If
                    p = p + 1
                Next
                'dprint ("write pixel of color n2(" + Str(n2) + ")")
                'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                pixelarray(p) = n2
                p = p + 1
            Else
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                MsgBox("Error: 4 bit nibble >= 16! (" & Str(n1) & ")")
                Exit Sub
            End If

        Next
        p = p - 1

        Dim padding As Byte

        padding = pwidth And &H3
        If padding > 0 Then
            padding = 4 - padding
        End If

        Dim f135 As FileStream = IO.File.Create(fname)

        write_word(&H4D42, f135)
        write_long(54 + (256 * 4) + ((pwidth * 8 + padding) * pheight / 8), f135)
        write_word(0, f135)
        write_word(0, f135)
        write_long(54 + (256 * 4), f135)

        write_long(40, f135)
        write_long(pwidth, f135)
        write_long(pheight, f135)
        write_word(1, f135)
        write_word(8, f135)
        write_long(0, f135)
        write_long((pwidth * 8 + padding) * pheight / 8, f135)
        write_long(2835, f135)
        write_long(2835, f135)
        write_long(256, f135)
        write_long(256, f135)

        'colors
        'For i = 1 To 17
        '    write_swaplong (modifiedcolorarray(i))
        '    Next
        '
        'For j = 18 To 256
        'write_long (&H0)
        'Next
        'write the current palette
        For i = 0 To 255
            write_long(Drawing.Color.FromArgb(0, gpalettes(curPalette)(i)).ToArgb(), f135)
        Next

        Dim line As Integer
        Dim pixel As Integer


        'dprint ("p=" + Str(p))
        p = pwidth * pheight
        For line = 1 To pheight
            For j = 1 To pwidth
                'For pixel = pwidth To 1
                If mirrored > 0 Then
                    pixel = j - 1
                Else
                    pixel = pwidth - j
                End If
                'pixel = j - 1
                write_byte(pixelarray(p - ((line - 1) * pwidth) - pixel), f135)
            Next
            For j = 1 To padding
                write_byte(0, f135)
            Next
        Next

        f135.Close()
        'dprint ("p=" + Str(p))

    End Sub

    Public Sub kkspec_gexport_img1_type2(ByVal itemnum As Integer, ByVal fname As String, ByRef mirrored As Short)

        ' if mirrored > 0 then mirror it, else dont... dont use that old shit
        'construct nibblearray
        Dim nibbles() As Byte
        Dim nibblelen As Integer
        Dim pixelarray() As Byte
        Dim pwidth As Integer
        Dim pheight As Integer


        Dim i As Integer
        Dim j As Integer
        Dim p As Integer
        Dim b As Integer
        Dim n4, n2, n1, n3, n5 As Integer
        Dim n6 As Byte

        Dim itemlen As Integer

        'dprint ("function: export_img1")
        itemlen = gitemsizes(itemnum)
        'itemlen = LenB(itemdata)

        'dprint ("itemlen: " + Str(itemlen))
        ReDim nibbles(itemlen * 2)

        pwidth = sread_word(gitemdatas(itemnum), 1)
        pheight = sread_word(gitemdatas(itemnum), 3)

        'dprint ("width: " + Str(pwidth) + " height: " + Str(pheight))
        ReDim pixelarray(pwidth * pheight + 2048)
        'dprint ("pixelarray size: " + Str(pwidth * pheight + 2048))
        j = 1
        For i = 5 To itemlen
            b = sread_byte(gitemdatas(itemnum), i)
            nibbles(j) = CShort(b And &HF0) / 16
            j = j + 1
            nibbles(j) = (b And &HF)
            j = j + 1
        Next

        nibblelen = j - 1

        p = 1
        For i = 1 To nibblelen

            'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            n1 = nibbles(i)
            'dprint ("n1 = nibble(" + Str(i) + "): " + Str(n1))
            i = i + 1
            'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            n2 = nibbles(i)
            'dprint ("n2 = nibble(" + Str(i) + "): " + Str(n2))
            'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            If n1 <= 7 Then
                'dprint ("write n1+1(" + Str(n1 + 1) + ") pixels of color n2(" + Str(n2) + ")")
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n1 = n1 + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                For j = 1 To n1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    pixelarray(p) = n2
                    p = p + 1
                Next
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 8 Then
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n3 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n4 = nibbles(i)
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                b = (n3 * 16) + n4
                b = b + 1
                'dprint ("read 2 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
                'dprint ("write b(" + Str(b) + ") pixels of color n2(" + Str(n2) + ")")
                For j = 1 To b
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    pixelarray(p) = n2
                    p = p + 1
                Next
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 9 Then
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n3 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n4 = nibbles(i)
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                b = (n3 * 16) + n4
                'dprint ("read 2 nibbles as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
                If b Mod &H2 = 1 Then 'odd
                    'dprint ("b is odd (b=b+1):")
                    b = b + 1
                    'dprint ("write b(" + Str(b) + ") pixels by reading one nibble for each color")
                    For j = 1 To b
                        i = i + 1
                        'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        n2 = nibbles(i)
                        ''dprint ("write pixel of color n2(" + Str(n2) + ")")
                        'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        pixelarray(p) = n2
                        p = p + 1
                    Next
                Else
                    'dprint ("b is even:")
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    pixelarray(p) = n2
                    'dprint ("write pixel of color " + Str(n2))
                    p = p + 1
                    'dprint ("write b(" + Str(b) + ") pixels by reading one nibble for each color")
                    For j = 1 To b
                        i = i + 1
                        'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        n2 = nibbles(i)
                        'dprint ("write pixel of color " + Str(n2))
                        'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        pixelarray(p) = n2
                        p = p + 1
                    Next
                End If
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 10 Then
                'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n2 = n2 + 1
                'dprint ("write n2+1(" + Str(n2) + " transparent pixels")
                'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                For j = 1 To n2
                    pixelarray(p) = 16
                    p = p + 1
                Next
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 11 Then
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n3 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n4 = nibbles(i)
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                b = (n3 * 16) + n4
                b = b + 1
                'dprint ("read 2 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
                'dprint ("write b(" + Str(b) + " pixels by coping them from the row above")
                For j = 1 To b
                    'dprint ("write pixel of color " + Str(pixelarray(p - pwidth)))
                    pixelarray(p) = pixelarray(p - pwidth)
                    p = p + 1
                Next
                'dprint ("write pixel of color n2(" + Str(n2) + ")")
                'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                pixelarray(p) = n2
                p = p + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 12 Then
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n3 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n4 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n5. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n5 = nibbles(i)
                i = i + 1
                n6 = nibbles(i)
                'UPGRADE_WARNING: Couldn't resolve default property of object n5. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                b = (n3 * 4096) + (n4 * 256) + (n5 * 16) + n6
                b = b + 1
                'dprint ("read 4 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + ",n5: " + Str(n5) + ",n6: " + Str(n6) + "): " + Str(b))
                'dprint ("write " + Str(b) + " pixels of color n2(" + Str(n2) + ")")
                For j = 1 To b
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    pixelarray(p) = n2
                    p = p + 1
                Next
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 13 Then
                'dprint ("do nothing")
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 14 Then
                'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                If n2 < 13 Then
                    'dprint ("n2 < 13:")
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n2 = n2 + 17
                    'dprint ("write n2+17(" + Str(n2) + ") transparent pixels")
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    For j = 1 To n2
                        pixelarray(p) = 16
                        p = p + 1
                    Next
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                ElseIf n2 = 13 Then
                    'dprint ("n2 = 13:")
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n3 = nibbles(i)
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n4 = nibbles(i)
                    'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    b = (n3 * 16) + n4
                    b = b + 1
                    'dprint ("read 2 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
                    'dprint ("write b(" + Str(b) + ") transparent pixels")
                    For j = 1 To b
                        pixelarray(p) = 16
                        p = p + 1
                    Next
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                ElseIf n2 = 14 Then
                    'dprint ("n2 = 14:")
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n3 = nibbles(i)
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n4 = nibbles(i)
                    'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    b = (n3 * 16) + n4
                    b = b + 257
                    'dprint ("read 2 nibbles + 257 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
                    'dprint ("write b(" + Str(b) + ") transparent pixels")
                    For j = 1 To b
                        pixelarray(p) = 16
                        p = p + 1
                    Next
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                ElseIf n2 = 15 Then
                    'dprint ("n2 = 15:")
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n3 = nibbles(i)
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n4 = nibbles(i)
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n5. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n5 = nibbles(i)
                    i = i + 1
                    n6 = nibbles(i)
                    'UPGRADE_WARNING: Couldn't resolve default property of object n5. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    b = (n3 * 4096) + (n4 * 256) + (n5 * 16) + n6
                    b = b + 1
                    'dprint ("read 4 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + ",n5: " + Str(n5) + ",n6: " + Str(n6) + "): " + Str(b))
                    'dprint ("write b(" + Str(b) + ") transparent pixels")
                    For j = 1 To b
                        pixelarray(p) = 16
                        p = p + 1
                    Next
                End If
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 15 Then
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n3 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n4 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n5. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n5 = nibbles(i)
                i = i + 1
                n6 = nibbles(i)
                'UPGRADE_WARNING: Couldn't resolve default property of object n5. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                b = (n3 * 4096) + (n4 * 256) + (n5 * 16) + n6
                b = b + 1
                'dprint ("read 4 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + ",n5: " + Str(n5) + ",n6: " + Str(n6) + "): " + Str(b))
                'dprint ("write b(" + Str(b) + ") pixels by coping them from the row above")
                For j = 1 To b
                    If p - pwidth < 0 Then
                        'dprint ("write pixel of color 0 ***not on second line of graphic")
                        pixelarray(p) = 0
                    Else
                        'dprint ("write pixel of color " + Str(pixelarray(p - pwidth)))
                        pixelarray(p) = pixelarray(p - pwidth)
                    End If
                    p = p + 1
                Next
                'dprint ("write pixel of color n2(" + Str(n2) + ")")
                'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                pixelarray(p) = n2
                p = p + 1
            Else
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                MsgBox("Error: 4 bit nibble >= 16! (" & Str(n1) & ")")
                Exit Sub
            End If

        Next
        p = p - 1

        Dim padding As Byte

        padding = pwidth And &H3
        If padding > 0 Then
            padding = 4 - padding
        End If

        Dim f135 As FileStream = IO.File.Create(fname)

        write_word(&H4D42, f135)
        write_long(54 + (256 * 4) + ((pwidth * 8 + padding) * pheight / 8), f135)
        write_word(0, f135)
        write_word(0, f135)
        write_long(54 + (256 * 4), f135)

        write_long(40, f135)
        write_long(pwidth, f135)
        write_long(pheight, f135)
        write_word(1, f135)
        write_word(8, f135)
        write_long(0, f135)
        write_long((pwidth * 8 + padding) * pheight / 8, f135)
        write_long(2835, f135)
        write_long(2835, f135)
        write_long(256, f135)
        write_long(256, f135)

        'colors
        'For i = 1 To 17
        '    write_swaplong (modifiedcolorarray(i))
        '    Next
        '
        'For j = 18 To 256
        'write_long (&H0)
        'Next
        'write the current palette

        kkspec_selPal4Item(itemnum)

        For i = 0 To 255
            write_long(Drawing.Color.FromArgb(0, gpalettes(curPalette)(i)).ToArgb(), f135)
        Next

        Dim line As Integer
        Dim pixel As Integer


        'dprint ("p=" + Str(p))
        p = pwidth * pheight
        For line = 1 To pheight
            For j = 1 To pwidth
                'For pixel = pwidth To 1
                If mirrored > 0 Then
                    pixel = j - 1
                Else
                    pixel = pwidth - j
                End If
                'pixel = j - 1
                write_byte(pixelarray(p - ((line - 1) * pwidth) - pixel), f135)
            Next
            For j = 1 To padding
                write_byte(0, f135)
            Next
        Next

        f135.Close()
        'dprint ("p=" + Str(p))

    End Sub

    Sub kkspec_selPal4Item(ByVal itemnum As Integer)
        Dim imagecount As Integer, mon As Integer, i As Integer, j As Integer

        If itemnum = 1 Then
            graphics.curPalette = graphics.gPALETTE_TITLE
        ElseIf itemnum >= 2 And itemnum <= 4 Then  'Main menu pallette
            graphics.curPalette = graphics.gPALETTE_MAINMENU
        ElseIf itemnum = 5 Then  'credits pallette
            graphics.curPalette = graphics.gPALETTE_CREDITS
        ElseIf itemnum >= 446 And itemnum < 532 Then  ' creatures
            'j = 447
            graphics.copypalette(graphics.gPALETTE_MAIN, graphics.gPALETTE_CUSTOM)
            graphics.curPalette = graphics.gPALETTE_CUSTOM
            mon = -1
            For i = 1 To 27
                j = graphics.f558.monstergraphicinfo(i).graphicnum + 446
                imagecount = 1
                If graphics.f559.monster(i).w4_sideimage Then
                    imagecount = imagecount + 1
                End If
                If graphics.f559.monster(i).w4_backimage Then
                    imagecount = imagecount + 1
                End If
                'If graphics.f559.monster(i).w4_attackimage Then
                imagecount = imagecount + 1
                'End If
                If itemnum >= j And itemnum < j + imagecount Then
                    mon = i
                    i = 28
                End If
            Next
            If mon >= 0 Then
                If graphics.f558.monstergraphicinfo(mon).colora > 0 Then
                    graphics.setpalette(graphics.gPALETTE_CUSTOM, 10, graphics.f558.palettetocolor(graphics.f558.palette(graphics.f558.monstergraphicinfo(mon).colora, 1)))
                    'graphics.setmodifiedcolor 11, graphics.f558.palettetocolor(graphics.f558.palette(graphics.f558.monstergraphicinfo(mon).ColorA, 1))
                End If
                If graphics.f558.monstergraphicinfo(mon).colorb > 0 Then
                    graphics.setpalette(graphics.gPALETTE_CUSTOM, 9, graphics.f558.palettetocolor(graphics.f558.palette(graphics.f558.monstergraphicinfo(mon).colorb, 1)))
                    'graphics.setmodifiedcolor 10, graphics.f558.palettetocolor(graphics.f558.palette(graphics.f558.monstergraphicinfo(mon).ColorB, 1))
                End If
            End If

            'MsgBox "Mon: " + Str(mon)

        Else
            graphics.curPalette = graphics.gPALETTE_MAIN
        End If
    End Sub

    ' OBSOLETE, use gexport_img1
    'TODO: Fixme for new item storage type
    'Public Sub export_img1(ByVal itemdata As String, ByVal fname As String, mirrored As Integer)
    '
    '    'construct nibblearray
    '    Dim nibbles() As Byte
    '    Dim nibblelen As Long
    '    Dim pixelarray() As Byte
    '    Dim pwidth As Long
    '    Dim pheight As Long
    '
    '
    '    Dim i As Long
    '    Dim j As Long
    '    Dim p As Long
    '    Dim b As Long
    '    Dim n1, n2, n3, n4, n5, n6 As Byte
    '
    '    Dim itemlen As Long
    '
    '    'dprint ("function: export_img1")
    '
    '    itemlen = LenB(itemdata)
    '
    '    'dprint ("itemlen: " + Str(itemlen))
    '    ReDim nibbles(itemlen * 2)
    '
    '    pwidth = sread_word(itemdata, 1)
    '    pheight = sread_word(itemdata, 3)
    '
    '    'dprint ("width: " + Str(pwidth) + " height: " + Str(pheight))
    '    ReDim pixelarray(pwidth * pheight + 2048)
    '    'dprint ("pixelarray size: " + Str(pwidth * pheight + 2048))
    '    j = 1
    '    For i = 5 To itemlen
    '        b = sread_byte(itemdata, i)
    '        nibbles(j) = (b And &HF0) / 16
    '        j = j + 1
    '        nibbles(j) = (b And &HF)
    '        j = j + 1
    '    Next
    '
    '    nibblelen = j - 1
    '
    '    p = 1
    '    For i = 1 To nibblelen
    '
    '        n1 = nibbles(i)
    '        'dprint ("n1 = nibble(" + Str(i) + "): " + Str(n1))
    '        i = i + 1
    '        n2 = nibbles(i)
    '        'dprint ("n2 = nibble(" + Str(i) + "): " + Str(n2))
    '        If n1 <= 7 Then
    '            'dprint ("write n1+1(" + Str(n1 + 1) + ") pixels of color n2(" + Str(n2) + ")")
    '            n1 = n1 + 1
    '            For j = 1 To n1
    '                pixelarray(p) = n2
    '                p = p + 1
    '            Next
    '        ElseIf n1 = 8 Then
    '            i = i + 1
    '            n3 = nibbles(i)
    '            i = i + 1
    '            n4 = nibbles(i)
    '            b = (n3 * 16) + n4
    '            b = b + 1
    '            'dprint ("read 2 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
    '            'dprint ("write b(" + Str(b) + ") pixels of color n2(" + Str(n2) + ")")
    '            For j = 1 To b
    '                pixelarray(p) = n2
    '                p = p + 1
    '            Next
    '        ElseIf n1 = 9 Then
    '            i = i + 1
    '            n3 = nibbles(i)
    '            i = i + 1
    '            n4 = nibbles(i)
    '            b = (n3 * 16) + n4
    '            'dprint ("read 2 nibbles as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
    '            If b Mod &H2 = 1 Then 'odd
    '                'dprint ("b is odd (b=b+1):")
    '                b = b + 1
    '                'dprint ("write b(" + Str(b) + ") pixels by reading one nibble for each color")
    '                For j = 1 To b
    '                    i = i + 1
    '                    n2 = nibbles(i)
    '                    ''dprint ("write pixel of color n2(" + Str(n2) + ")")
    '                    pixelarray(p) = n2
    '                    p = p + 1
    '                Next
    '            Else
    '                'dprint ("b is even:")
    '                pixelarray(p) = n2
    '                    'dprint ("write pixel of color " + Str(n2))
    '                p = p + 1
    '                'dprint ("write b(" + Str(b) + ") pixels by reading one nibble for each color")
    '                For j = 1 To b
    '                    i = i + 1
    '                    n2 = nibbles(i)
    '                    'dprint ("write pixel of color " + Str(n2))
    '                    pixelarray(p) = n2
    '                    p = p + 1
    '                Next
    '            End If
    '        ElseIf n1 = 10 Then
    '            n2 = n2 + 1
    '            'dprint ("write n2+1(" + Str(n2) + " transparent pixels")
    '            For j = 1 To n2
    '                pixelarray(p) = 16
    '                p = p + 1
    '            Next
    '        ElseIf n1 = 11 Then
    '            i = i + 1
    '            n3 = nibbles(i)
    '            i = i + 1
    '            n4 = nibbles(i)
    '            b = (n3 * 16) + n4
    '            b = b + 1
    '            'dprint ("read 2 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
    '            'dprint ("write b(" + Str(b) + " pixels by coping them from the row above")
    '            For j = 1 To b
    '                'dprint ("write pixel of color " + Str(pixelarray(p - pwidth)))
    '                pixelarray(p) = pixelarray(p - pwidth)
    '                p = p + 1
    '            Next
    '            'dprint ("write pixel of color n2(" + Str(n2) + ")")
    '            pixelarray(p) = n2
    '            p = p + 1
    '        ElseIf n1 = 12 Then
    '            i = i + 1
    '            n3 = nibbles(i)
    '            i = i + 1
    '            n4 = nibbles(i)
    '            i = i + 1
    '            n5 = nibbles(i)
    '            i = i + 1
    '            n6 = nibbles(i)
    '            b = (n3 * 4096) + (n4 * 256) + (n5 * 16) + n6
    '            b = b + 1
    '            'dprint ("read 4 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + ",n5: " + Str(n5) + ",n6: " + Str(n6) + "): " + Str(b))
    '            'dprint ("write " + Str(b) + " pixels of color n2(" + Str(n2) + ")")
    '            For j = 1 To b
    '                pixelarray(p) = n2
    '                p = p + 1
    '            Next
    '        ElseIf n1 = 13 Then
    '            'dprint ("do nothing")
    '        ElseIf n1 = 14 Then
    '            If n2 < 13 Then
    '                'dprint ("n2 < 13:")
    '                n2 = n2 + 17
    '                'dprint ("write n2+17(" + Str(n2) + ") transparent pixels")
    '                For j = 1 To n2
    '                    pixelarray(p) = 16
    '                    p = p + 1
    '                Next
    '            ElseIf n2 = 13 Then
    '                'dprint ("n2 = 13:")
    '                i = i + 1
    '                n3 = nibbles(i)
    '                i = i + 1
    '                n4 = nibbles(i)
    '                b = (n3 * 16) + n4
    '                b = b + 1
    '                'dprint ("read 2 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
    '                'dprint ("write b(" + Str(b) + ") transparent pixels")
    '                For j = 1 To b
    '                    pixelarray(p) = 16
    '                    p = p + 1
    '                Next
    '            ElseIf n2 = 14 Then
    '                'dprint ("n2 = 14:")
    '                i = i + 1
    '                n3 = nibbles(i)
    '                i = i + 1
    '                n4 = nibbles(i)
    '                b = (n3 * 16) + n4
    '                b = b + 257
    '                'dprint ("read 2 nibbles + 257 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
    '                'dprint ("write b(" + Str(b) + ") transparent pixels")
    '                For j = 1 To b
    '                    pixelarray(p) = 16
    '                    p = p + 1
    '                Next
    '            ElseIf n2 = 15 Then
    '                'dprint ("n2 = 15:")
    '                i = i + 1
    '                n3 = nibbles(i)
    '                i = i + 1
    '                n4 = nibbles(i)
    '                i = i + 1
    '                n5 = nibbles(i)
    '                i = i + 1
    '                n6 = nibbles(i)
    '                b = (n3 * 4096) + (n4 * 256) + (n5 * 16) + n6
    '                b = b + 1
    '                'dprint ("read 4 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + ",n5: " + Str(n5) + ",n6: " + Str(n6) + "): " + Str(b))
    '                'dprint ("write b(" + Str(b) + ") transparent pixels")
    '                For j = 1 To b
    '                    pixelarray(p) = 16
    '                    p = p + 1
    '                Next
    '            End If
    '        ElseIf n1 = 15 Then
    '            i = i + 1
    '            n3 = nibbles(i)
    '            i = i + 1
    '            n4 = nibbles(i)
    '            i = i + 1
    '            n5 = nibbles(i)
    '            i = i + 1
    '            n6 = nibbles(i)
    '            b = (n3 * 4096) + (n4 * 256) + (n5 * 16) + n6
    '            b = b + 1
    '            'dprint ("read 4 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + ",n5: " + Str(n5) + ",n6: " + Str(n6) + "): " + Str(b))
    '            'dprint ("write b(" + Str(b) + ") pixels by coping them from the row above")
    '            For j = 1 To b
    '                If p - pwidth < 0 Then
    '                    'dprint ("write pixel of color 0 ***not on second line of graphic")
    '                    pixelarray(p) = 0
    '                Else
    '                    'dprint ("write pixel of color " + Str(pixelarray(p - pwidth)))
    '                    pixelarray(p) = pixelarray(p - pwidth)
    '                End If
    '                p = p + 1
    '            Next
    '            'dprint ("write pixel of color n2(" + Str(n2) + ")")
    '            pixelarray(p) = n2
    '            p = p + 1
    '        Else
    '            MsgBox "Error: 4 bit nibble >= 16! (" + Str(n1) + ")"
    '            Exit Sub
    '        End If
    '
    '    Next
    '    p = p - 1
    '
    '    Dim padding As Byte
    '
    '    padding = pwidth And &H3
    '    If padding > 0 Then
    '        padding = 4 - padding
    '    End If
    '
    '    Open fname For Binary Access Write As 135
    '
    '    write_word (&H4D42)
    '    write_long (54 + (256 * 4) + ((pwidth * 8 + padding) * pheight / 8))
    '    write_word (0)
    '    write_word (0)
    '    write_long (54 + (256 * 4))
    '
    '    write_long (40)
    '    write_long (pwidth)
    '    write_long (pheight)
    '    write_word (1)
    '    write_word (8)
    '    write_long (0)
    '    write_long ((pwidth * 8 + padding) * pheight / 8)
    '    write_long (2835)
    '    write_long (2835)
    '    write_long (256)
    '    write_long (256)
    '
    '    'colors
    '    For i = 1 To 17
    '        write_swaplong (modifiedcolorarray(i))
    '        Next
    '
    '    For j = 18 To 256
    '    write_long (&H0)
    '    Next
    '
    '    Dim line As Long
    '    Dim pixel As Long
    '
    '
    '    'dprint ("p=" + Str(p))
    '    p = pwidth * pheight
    '    For line = 1 To pheight
    '        For j = 1 To pwidth
    '        'For pixel = pwidth To 1
    '        If mirrored = 1 Or mirrored = 3 Or mirrored = 5 Then
    '            pixel = j - 1
    '        Else
    '            pixel = pwidth - j
    '        End If
    '        'pixel = j - 1
    '            write_byte (pixelarray(p - ((line - 1) * pwidth) - pixel))
    '        Next
    '        For j = 1 To padding
    '            write_byte (0)
    '        Next
    '    Next
    '
    '    Close 135
    '    'dprint ("p=" + Str(p))
    '
    'End Sub
    Public Sub buildbmp(ByVal itemnum As Integer)

    End Sub
    Public Function gbitmapwidths(ByVal itemnum As Integer) As Integer
        gbitmapwidths = sread_word(gitemdatas(itemnum), 1)
    End Function
    Public Function gbitmapheights(ByVal itemnum As Integer) As Integer
        gbitmapheights = sread_word(gitemdatas(itemnum), 3)
    End Function
    Public Function gbitmaps(ByVal itemnum As Integer, ByVal isMirrored As Integer) As Drawing.Bitmap
        If Not mapfile.gettype_Renamed(itemnum) = "IMG1" Then
            gbitmaps = Nothing
            Exit Function
        End If

        'construct nibblearray
        Dim nibbles() As Byte
        Dim nibblelen As Integer
        Dim pixelarray() As Byte
        Dim mirroredpixelarray() As Byte
        Dim pwidth As Integer
        Dim pheight As Integer


        Dim i As Integer
        Dim j As Integer
        Dim p As Integer
        Dim b As Integer
        Dim n4, n2, n1, n3, n5 As Integer
        Dim n6 As Byte

        Dim itemlen As Integer

        'dprint ("function: export_img1")

        'itemlen = LenB(gitemdatas(itemnum))
        itemlen = getgitemsize(itemnum)
        'dprint ("itemlen: " + Str(itemlen))
        ReDim nibbles(itemlen * 2)

        pwidth = sread_word(gitemdatas(itemnum), 1)
        pheight = sread_word(gitemdatas(itemnum), 3)

        'dprint ("width: " + Str(pwidth) + " height: " + Str(pheight))
        ReDim pixelarray(pwidth * pheight + 16) '16 bytes buffer in case of overflow
        ReDim mirroredpixelarray(pwidth * pheight + 16) '16 bytes buffer in case of overflow
        'dprint ("pixelarray size: " + Str(UBound(pixelarray)))

        j = 1
        For i = 5 To itemlen
            b = sread_byte(gitemdatas(itemnum), i)
            nibbles(j) = CShort(b And &HF0) / 16
            j = j + 1
            nibbles(j) = (b And &HF)
            j = j + 1
        Next

        nibblelen = j - 1

        p = 1
        For i = 1 To nibblelen

            'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            n1 = nibbles(i)
            'dprint ("n1 = nibble(" + Str(i) + "): " + Str(n1))
            i = i + 1
            'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            n2 = nibbles(i)
            'dprint ("n2 = nibble(" + Str(i) + "): " + Str(n2))
            'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            If n1 <= 7 Then
                'dprint ("write n1+1(" + Str(n1 + 1) + ") pixels of color n2(" + Str(n2) + ")")
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n1 = n1 + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                For j = 1 To n1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    pixelarray(p) = n2
                    p = p + 1
                Next
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 8 Then
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n3 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n4 = nibbles(i)
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                b = (n3 * 16) + n4
                b = b + 1
                'dprint ("read 2 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
                'dprint ("write b(" + Str(b) + ") pixels of color n2(" + Str(n2) + ")")
                For j = 1 To b
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    pixelarray(p) = n2
                    p = p + 1
                Next
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 9 Then
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n3 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n4 = nibbles(i)
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                b = (n3 * 16) + n4
                'dprint ("read 2 nibbles as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
                If b Mod &H2 = 1 Then 'odd
                    'dprint ("b is odd (b=b+1):")
                    b = b + 1
                    'dprint ("write b(" + Str(b) + ") pixels by reading one nibble for each color")
                    For j = 1 To b
                        i = i + 1
                        'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        n2 = nibbles(i)
                        ''dprint ("write pixel of color n2(" + Str(n2) + ")")
                        'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        pixelarray(p) = n2
                        p = p + 1
                    Next
                Else
                    'dprint ("b is even:")
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    pixelarray(p) = n2
                    'dprint ("write pixel of color " + Str(n2))
                    p = p + 1
                    'dprint ("write b(" + Str(b) + ") pixels by reading one nibble for each color")
                    For j = 1 To b
                        i = i + 1
                        'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        n2 = nibbles(i)
                        ''dprint ("write pixel of color " + Str(n2))
                        'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        pixelarray(p) = n2
                        p = p + 1
                    Next
                End If
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 10 Then
                'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n2 = n2 + 1
                'dprint ("write n2+1(" + Str(n2) + " transparent pixels")
                'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                For j = 1 To n2
                    pixelarray(p) = 16
                    p = p + 1
                Next
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 11 Then
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n3 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n4 = nibbles(i)
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                b = (n3 * 16) + n4
                b = b + 1
                'dprint ("read 2 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
                'dprint ("write b(" + Str(b) + " pixels by coping them from the row above")
                For j = 1 To b
                    ''dprint ("write pixel of color " + Str(pixelarray(p - pwidth)))
                    pixelarray(p) = pixelarray(p - pwidth)
                    p = p + 1
                Next
                'dprint ("write pixel of color n2(" + Str(n2) + ")")
                'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                pixelarray(p) = n2
                p = p + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 12 Then
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n3 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n4 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n5. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n5 = nibbles(i)
                i = i + 1
                n6 = nibbles(i)
                'UPGRADE_WARNING: Couldn't resolve default property of object n5. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                b = (n3 * 4096) + (n4 * 256) + (n5 * 16) + n6
                b = b + 1
                'dprint ("read 4 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + ",n5: " + Str(n5) + ",n6: " + Str(n6) + "): " + Str(b))
                'dprint ("write " + Str(b) + " pixels of color n2(" + Str(n2) + ")")
                For j = 1 To b
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    pixelarray(p) = n2
                    p = p + 1
                Next
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 13 Then
                'dprint ("do nothing")
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 14 Then
                'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                If n2 < 13 Then
                    'dprint ("n2 < 13:")
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n2 = n2 + 17
                    'dprint ("write n2+17(" + Str(n2) + ") transparent pixels")
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    For j = 1 To n2
                        pixelarray(p) = 16
                        p = p + 1
                    Next
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                ElseIf n2 = 13 Then
                    'dprint ("n2 = 13:")
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n3 = nibbles(i)
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n4 = nibbles(i)
                    'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    b = (n3 * 16) + n4
                    b = b + 1
                    'dprint ("read 2 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
                    'dprint ("write b(" + Str(b) + ") transparent pixels")
                    For j = 1 To b
                        pixelarray(p) = 16
                        p = p + 1
                    Next
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                ElseIf n2 = 14 Then
                    'dprint ("n2 = 14:")
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n3 = nibbles(i)
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n4 = nibbles(i)
                    'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    b = (n3 * 16) + n4
                    b = b + 257
                    'dprint ("read 2 nibbles + 257 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + "): " + Str(b))
                    'dprint ("write b(" + Str(b) + ") transparent pixels")
                    For j = 1 To b
                        pixelarray(p) = 16
                        p = p + 1
                    Next
                    'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                ElseIf n2 = 15 Then
                    'dprint ("n2 = 15:")
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n3 = nibbles(i)
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n4 = nibbles(i)
                    i = i + 1
                    'UPGRADE_WARNING: Couldn't resolve default property of object n5. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    n5 = nibbles(i)
                    i = i + 1
                    n6 = nibbles(i)
                    'UPGRADE_WARNING: Couldn't resolve default property of object n5. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    b = (n3 * 4096) + (n4 * 256) + (n5 * 16) + n6
                    b = b + 1
                    'dprint ("read 4 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + ",n5: " + Str(n5) + ",n6: " + Str(n6) + "): " + Str(b))
                    'dprint ("write b(" + Str(b) + ") transparent pixels")
                    For j = 1 To b
                        pixelarray(p) = 16
                        p = p + 1
                    Next
                End If
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            ElseIf n1 = 15 Then
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n3 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n4 = nibbles(i)
                i = i + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object n5. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                n5 = nibbles(i)
                i = i + 1
                n6 = nibbles(i)
                'UPGRADE_WARNING: Couldn't resolve default property of object n5. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n4. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_WARNING: Couldn't resolve default property of object n3. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                b = (n3 * 4096) + (n4 * 256) + (n5 * 16) + n6
                b = b + 1
                'dprint ("read 4 nibbles + 1 as b (n3: " + Str(n3) + ",n4: " + Str(n4) + ",n5: " + Str(n5) + ",n6: " + Str(n6) + "): " + Str(b))
                'dprint ("write b(" + Str(b) + ") pixels by coping them from the row above")
                For j = 1 To b
                    If p - pwidth < 0 Then
                        'dprint ("write pixel of color 0 ***not on second line of graphic")
                        pixelarray(p) = 0
                    Else
                        'dprint ("write pixel of color " + Str(pixelarray(p - pwidth)))
                        pixelarray(p) = pixelarray(p - pwidth)
                    End If
                    p = p + 1
                Next
                'dprint ("write pixel of color n2(" + Str(n2) + ")")
                'UPGRADE_WARNING: Couldn't resolve default property of object n2. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                pixelarray(p) = n2
                p = p + 1
            Else
                'UPGRADE_WARNING: Couldn't resolve default property of object n1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                MsgBox("Error: 4 bit nibble >= 16! (" & Str(n1) & ")")
                gbitmaps = Nothing
                Exit Function
            End If

        Next


        p = p - 1

        If isMirrored = 0 Then
            gbitmaps = ADGEDotNetUt.ADGEUt.CreatePic(pwidth, pheight, ginvertedpalettes(0), pixelarray)
        Else
            'count = 1
            For i = 1 To pheight
                For j = 1 To pwidth
                    mirroredpixelarray(1 + (i) * pwidth - j) = pixelarray((i - 1) * pwidth + j)
                    'mirroredpixelarray(count) = pixelarray(((i - 1) * pwidth) + j)
                    'count = count + 1
                Next
            Next

            gbitmaps = ADGEDotNetUt.ADGEUt.CreatePic(pwidth, pheight, ginvertedpalettes(0), mirroredpixelarray)
        End If

        'gbitmapwidths(itemnum) = pwidth
        'gbitmapheights(itemnum) = pheight

    End Function


    '=== GRAPHIC ROUTINES ===
    'made for simplicity, drawgrect is so nice!

    'standard drawbmp, just draw the fucker.
    Public Sub drawbmp( _
        ByRef dst As Drawing.Graphics, _
        ByVal itemnum As Integer _
    )
        Dim picin As Bitmap = gbitmaps(itemnum, gMirrored)
        Debug.Assert(picin.PixelFormat = Imaging.PixelFormat.Format8bppIndexed, "pic#" & itemnum & " isn't 8bpp?")
        Dim pal As Imaging.ColorPalette = picin.Palette
        Dim i As Integer
        For i = 0 To 255
            ' It doesn't need ginvertedpalettes for .net
            pal.Entries(i) = gpalettes(curPalette)(i)
        Next
        picin.Palette = pal

        dst.DrawImage(picin, 0, 0)
    End Sub
    'standard drawbmp, just draw the fucker.
    Public Sub drawbmpat(ByVal dst As Control, ByVal itemnum As Integer, ByVal X As Integer, ByVal Y As Integer)
        Dim cv As Drawing.Graphics = dst.CreateGraphics()
        cv.DrawImage(gbitmaps(itemnum, gMirrored), X, Y)
    End Sub
    Public Sub stretchbmp(ByVal dst As Drawing.Graphics, ByVal itemnum As Integer, ByVal dstx As Integer, ByVal dsty As Integer, ByVal dstwidth As Integer, ByVal dstheight As Integer, ByVal srcx As Integer, ByVal srcy As Integer)
        Dim picin As Bitmap = gbitmaps(itemnum, gMirrored)
        Debug.Assert(picin.PixelFormat = Imaging.PixelFormat.Format8bppIndexed, "pic#" & itemnum & " isn't 8bpp?")
        Dim pal As Imaging.ColorPalette = picin.Palette
        Dim i As Integer
        For i = 0 To 255
            ' It doesn't need ginvertedpalettes for .net
            pal.Entries(i) = gpalettes(curPalette)(i)
        Next
        picin.Palette = pal

        dst.DrawImage( _
            picin, _
            New Rectangle(dstx, dsty, dstwidth, dstheight), _
            New Rectangle(srcx, srcy, gbitmapwidths(itemnum), gbitmapheights(itemnum)), _
            GraphicsUnit.Pixel _
            )
    End Sub
    Public Sub stretchbmpby( _
        ByVal dst As Drawing.Graphics, _
        ByVal itemnum As Integer, _
        ByVal dstx As Integer, _
        ByVal dsty As Integer, _
        ByVal srcx As Integer, _
        ByVal srcy As Integer, _
        ByVal dstwidth As Integer, _
        ByVal dstheight As Integer _
    )
        dstheight = dstheight * gbitmapheights(itemnum) / 100
        dstwidth = dstwidth * gbitmapwidths(itemnum) / 100

        Dim picin As Bitmap = gbitmaps(itemnum, gMirrored)
        Debug.Assert(picin.PixelFormat = Imaging.PixelFormat.Format8bppIndexed, "pic#" & itemnum & " isn't 8bpp?")
        Dim pal As Imaging.ColorPalette = picin.Palette
        Dim i As Integer
        For i = 0 To 255
            ' It doesn't need ginvertedpalettes for .net
            pal.Entries(i) = gpalettes(curPalette)(i)
        Next
        picin.Palette = pal

        dst.DrawImage( _
            picin, _
            New Rectangle(dstx, dsty, dstwidth, dstheight), _
            New Rectangle(srcx, srcy, gbitmapwidths(itemnum), gbitmapheights(itemnum)), _
            GraphicsUnit.Pixel)
    End Sub
    Public Sub drawtransparentbmp(ByRef dst As Control, ByVal itemnum As Integer, ByVal TransparentPixel As Integer)
        Dim cv As Drawing.Graphics = dst.CreateGraphics()
        cv.DrawImage(gbitmaps(itemnum, gMirrored), 0, 0)
    End Sub
    'standard drawbmp, just draw the fucker.
    Public Sub drawtransparentbmpat( _
        ByVal dst As Drawing.Graphics, _
        ByVal itemnum As Integer, _
        ByVal X As Integer, _
        ByVal Y As Integer, _
        ByVal TransparentPixel As Integer _
    )
        Dim picin As Bitmap = gbitmaps(itemnum, gMirrored)
        Debug.Assert(picin.PixelFormat = Imaging.PixelFormat.Format8bppIndexed, "pic#" & itemnum & " isn't 8bpp?")
        Dim pal As Imaging.ColorPalette = picin.Palette
        Dim i As Integer
        For i = 0 To 255
            ' It doesn't need ginvertedpalettes for .net
            pal.Entries(i) = gpalettes(curPalette)(i)
        Next
        pal.Entries(TransparentPixel) = Color.Transparent
        picin.Palette = pal

        dst.DrawImage(picin, X, Y)
    End Sub
    Public Sub stretchtransparentbmp( _
        ByVal dst As Drawing.Graphics, _
        ByVal itemnum As Integer, _
        ByVal dstx As Integer, _
        ByVal dsty As Integer, _
        ByVal dstwidth As Integer, _
        ByVal dstheight As Integer, _
        ByVal srcx As Integer, _
        ByVal srcy As Integer, _
        ByVal TransparentPixel As Integer _
    )
        Dim picin As Bitmap = gbitmaps(itemnum, gMirrored)
        Debug.Assert(picin.PixelFormat = Imaging.PixelFormat.Format8bppIndexed, "pic#" & itemnum & " isn't 8bpp?")
        Dim pal As Imaging.ColorPalette = picin.Palette
        Dim i As Integer
        For i = 0 To 255
            ' It doesn't need ginvertedpalettes for .net
            pal.Entries(i) = gpalettes(curPalette)(i)
        Next
        pal.Entries(TransparentPixel) = Color.Transparent
        picin.Palette = pal

        dst.DrawImage( _
            picin, _
            New Rectangle(dstx, dsty, dstwidth, dstheight), _
            New Rectangle(srcx, srcy, gbitmapwidths(itemnum), gbitmapheights(itemnum)), _
            GraphicsUnit.Pixel _
            )

    End Sub

    Public Sub stretchtransparentbmpsrc( _
        ByVal dst As Drawing.Graphics, _
        ByVal itemnum As Integer, _
        ByVal dstx As Integer, _
        ByVal dsty As Integer, _
        ByVal dstwidth As Integer, _
        ByVal dstheight As Integer, _
        ByVal srcx As Integer, _
        ByVal srcy As Integer, _
        ByVal srcwidth As Integer, _
        ByVal srcheight As Integer, _
        ByVal TransparentPixel As Integer _
    )
        Dim picin As Bitmap = gbitmaps(itemnum, gMirrored)
        Debug.Assert(picin.PixelFormat = Imaging.PixelFormat.Format8bppIndexed, "pic#" & itemnum & " isn't 8bpp?")
        Dim pal As Imaging.ColorPalette = picin.Palette
        Dim i As Integer
        For i = 0 To 255
            ' It doesn't need ginvertedpalettes for .net
            pal.Entries(i) = gpalettes(curPalette)(i)
        Next
        pal.Entries(TransparentPixel) = Color.Transparent
        picin.Palette = pal

        dst.DrawImage( _
            picin, _
            New Rectangle(dstx, dsty, dstwidth, dstheight), _
            New Rectangle(srcx, srcy, srcwidth, srcheight), _
            GraphicsUnit.Pixel _
            )
    End Sub
    Public Sub fill( _
        ByVal dst As Drawing.Graphics, _
        ByVal dstx As Integer, _
        ByVal dsty As Integer, _
        ByVal dstwidth As Integer, _
        ByVal dstheight As Integer, _
        ByVal color As Drawing.Color _
    )
        dst.FillRectangle( _
            New SolidBrush(color), _
            dstx, _
            dsty, _
            dstwidth, _
            dstheight _
        )
    End Sub
    Public Sub drawgrect( _
           ByVal dst As Drawing.Graphics, _
           ByVal grectnum As Integer, _
           ByVal GraphicNum As Integer, _
           ByVal TransparentPixel As Integer, _
           ByVal percent As Integer _
    )

        If GraphicNum = -1 Then
            GraphicNum = f558.graphicrect(grectnum).displayedGraphic - 1
        End If
        If f558.graphicrect(grectnum).mirrored <> 0 Then
            setmirrored(1)
        End If
        stretchtransparentbmpsrc(dst, GraphicNum, f558.graphicrect(grectnum).x1 * percent / 100, f558.graphicrect(grectnum).y1 * percent / 100, (f558.graphicrect(grectnum).x2 - f558.graphicrect(grectnum).x1 + 1) * percent / 100, (f558.graphicrect(grectnum).y2 - f558.graphicrect(grectnum).y1 + 1) * percent / 100, f558.graphicrect(grectnum).srcx, f558.graphicrect(grectnum).srcy, f558.graphicrect(grectnum).x2 - f558.graphicrect(grectnum).x1 + 1, f558.graphicrect(grectnum).y2 - f558.graphicrect(grectnum).y1 + 1, TransparentPixel)

        setmirrored(0)
    End Sub


    Public Sub unload()

        Dim i As Short
        Dim num As Short
        num = 0
        For i = 0 To numitems - 1
            If Not (gbitmaps(i, 0) Is Nothing) Then
                'we have an old bitmap, lets toss it
                gbitmaps(i, 0).Dispose()
                num = num + 1
            End If
            If Not (gbitmaps(i, 1) Is Nothing) Then
                'we have an old bitmap, lets toss it
                gbitmaps(i, 1).Dispose()
                num = num + 1
            End If
        Next

    End Sub
End Class