VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "graphics"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False


Public mainwin As MainWindow
Dim bytesread As Long
Public bytes As Long

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 Long
Private itemsizescompressed(800) As Long

'the data of the items
Private itemstrs(801) As String
'the standard color palette?
Private colorarray(17) As Long
'the custom modified color palette
Private modifiedcolorarray(17) As Long
'god this shit is gay
Private itempics(801, 5) As StdPicture
Private itempicsloaded(801, 5) As Boolean

Private gitemsizes() As Long
Private gitemsizescompressed() As Long
Private gitemdatas() As String
Private gbitmaps() As Long
Private gbitmapwidths() As Long
Private gbitmapheights() As Long
Private gDC As Long
Private gMaskDC As Long
Public gMirrored As Long


'palette info
Dim gpalettes(0 To 15, 0 To 255) As Long
Dim ginvertedpalettes(0 To 15, 0 To 255) As Long
Public gPALETTE_MAIN As Long
Public gPALETTE_TITLE As Long
Public gPALETTE_MAINMENU As Long
Public gPALETTE_CREDITS As Long
Public gPALETTE_CUSTOM As Long
Public curPalette As Long

Public numitems As Long
'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


Function setmirrored(ByVal num As Long)
    If num = 0 Then
        gMirrored = 0
    Else
        gMirrored = 1
    End If
End Function
Function getgbitmapwidth(ByVal num As Long)
    getgbitmapwidth = gbitmapwidths(num)
End Function
Function getgbitmapheight(ByVal num As Long)
    getgbitmapheight = gbitmapheights(num)
End Function
Function setpalette(ByVal palettenum As Long, ByVal colornum As Long, ByVal color As Long)
    Dim r, g, b As Long
    gpalettes(palettenum, colornum) = color
    r = color And 255
    g = color And 65280
    b = color And 16711680
    r = r * 65536
    b = b / 65536
    color = r + b + g
    ginvertedpalettes(palettenum, colornum) = color
End Function
Function getpalette(ByVal palettenum As Long, ByVal colornum As Long) As Long
    getpalette = gpalettes(palettenum, colornum)
End Function
Function getinvertedpalette(ByVal palettenum As Long, ByVal colornum As Long) As Long
    getinvertedpalette = ginvertedpalettes(palettenum, colornum)
End Function
Function copypalette(ByVal src As Long, ByVal dst As Long)
    Dim i As Integer
    For i = 0 To 255
        gpalettes(dst, i) = gpalettes(src, i)
        ginvertedpalettes(dst, i) = ginvertedpalettes(src, i)
    Next
End Function
Function build_invertedpalettes()
    'we have to build an inverted set as well, dunno why
    Dim i, j As Long
    Dim col As Long
    Dim r, g, b As Long
    
    For i = 0 To 15
        For j = 0 To 255
            col = gpalettes(i, j)
            r = col And 255
            g = col And 65280
            b = col And 16711680
            r = r * 65536
            b = b / 65536
            col = r + b + g
            ginvertedpalettes(i, j) = col
        Next
    Next
End Function
Function build_palettes()
    Dim palettenum As Long
    
    'build the main palette, as 0
    palettenum = 0
    gPALETTE_MAIN = palettenum
    
    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(&H6D, &H24, &H0))
    gpalettes(palettenum, 4) = (RGB(&H0, &HDA, &HDA))
    gpalettes(palettenum, 5) = (RGB(&H91, &H48, &H0))
    gpalettes(palettenum, 6) = (RGB(&H0, &H91, &H0))
    gpalettes(palettenum, 7) = (RGB(&H0, &HDA, &H0))
    gpalettes(palettenum, 8) = (RGB(&HFF, &H0, &H0))
    gpalettes(palettenum, 9) = (RGB(&HFF, &HB6, &H0))
    gpalettes(palettenum, 10) = (RGB(&HDA, &H91, &H6D))
    gpalettes(palettenum, 11) = (RGB(&HFF, &HFF, &H0))
    gpalettes(palettenum, 12) = (RGB(&H48, &H48, &H48))
    gpalettes(palettenum, 13) = (RGB(&HB6, &HB6, &HB6))
    gpalettes(palettenum, 14) = (RGB(&H0, &H0, &HFF))
    gpalettes(palettenum, 15) = (RGB(&HFF, &HFF, &HFF))
    
    '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) = RGB(0, 0, 0) 'set the rest to black
    Next
    
    'title screen palette
    palettenum = 1
    gPALETTE_TITLE = palettenum
    gpalettes(palettenum, 0) = RGB(0, 0, 42)
    gpalettes(palettenum, 1) = RGB(109, 109, 109)
    gpalettes(palettenum, 2) = RGB(145, 145, 145)
    gpalettes(palettenum, 3) = RGB(181, 140, 33)
    gpalettes(palettenum, 4) = RGB(140, 66, 33)
    gpalettes(palettenum, 5) = RGB(214, 181, 33)
    gpalettes(palettenum, 6) = RGB(181, 107, 33)
    gpalettes(palettenum, 7) = RGB(0, 0, 66)
    gpalettes(palettenum, 8) = RGB(255, 255, 66)
    gpalettes(palettenum, 9) = RGB(0, 0, 66)
    gpalettes(palettenum, 10) = RGB(0, 0, 0)
    gpalettes(palettenum, 11) = RGB(0, 0, 66)
    gpalettes(palettenum, 12) = RGB(255, 0, 0)
    gpalettes(palettenum, 13) = RGB(0, 0, 66)
    gpalettes(palettenum, 14) = RGB(0, 0, 66)
    gpalettes(palettenum, 15) = RGB(255, 255, 255)
    For i = 16 To 255
        gpalettes(palettenum, i) = RGB(0, 0, 0) 'set the rest to black
    Next
    
    'Main menu pallette
    palettenum = 2
    gPALETTE_MAINMENU = palettenum
    gpalettes(palettenum, 0) = RGB(0, 0, 0)
    gpalettes(palettenum, 1) = RGB(108, 108, 108)
    gpalettes(palettenum, 2) = RGB(144, 144, 144)
    gpalettes(palettenum, 3) = RGB(144, 72, 0)
    gpalettes(palettenum, 4) = RGB(216, 180, 144)
    gpalettes(palettenum, 5) = RGB(66, 33, 0)
    gpalettes(palettenum, 6) = RGB(0, 144, 0)
    gpalettes(palettenum, 7) = RGB(0, 180, 0)
    gpalettes(palettenum, 8) = RGB(144, 108, 108)
    gpalettes(palettenum, 9) = RGB(252, 0, 0)
    gpalettes(palettenum, 10) = RGB(180, 144, 72)
    gpalettes(palettenum, 11) = RGB(108, 72, 36)
    gpalettes(palettenum, 12) = RGB(72, 72, 72)
    gpalettes(palettenum, 13) = RGB(180, 180, 180)
    gpalettes(palettenum, 14) = RGB(108, 36, 0)
    gpalettes(palettenum, 15) = RGB(255, 255, 255)
    For i = 16 To 255
        gpalettes(palettenum, i) = RGB(0, 0, 0) 'set the rest to black
    Next
    
    'credits pallette
    palettenum = 3
    gPALETTE_CREDITS = palettenum
    gpalettes(palettenum, 0) = RGB(0, 0, 0)
    gpalettes(palettenum, 1) = RGB(0, 180, 180)
    gpalettes(palettenum, 2) = RGB(252, 252, 180)
    gpalettes(palettenum, 3) = RGB(140, 72, 0)
    gpalettes(palettenum, 4) = RGB(252, 252, 144)
    gpalettes(palettenum, 5) = RGB(0, 0, 0)
    gpalettes(palettenum, 6) = RGB(0, 144, 0)
    gpalettes(palettenum, 7) = RGB(180, 0, 0)
    gpalettes(palettenum, 8) = RGB(216, 144, 72)
    gpalettes(palettenum, 9) = RGB(252, 252, 180)
    gpalettes(palettenum, 10) = RGB(252, 144, 72)
    gpalettes(palettenum, 11) = RGB(252, 216, 0)
    gpalettes(palettenum, 12) = RGB(252, 180, 0)
    gpalettes(palettenum, 13) = RGB(0, 0, 0)
    gpalettes(palettenum, 14) = RGB(108, 36, 0)
    gpalettes(palettenum, 15) = RGB(252, 252, 216)
    For i = 16 To 255
        gpalettes(palettenum, i) = RGB(0, 0, 0) 'set the rest to black
    Next
    
    palettenum = 4
    gPALETTE_CUSTOM = palettenum
    'reserve the rest for custom palettes.
End Function

Function setsize(ByVal num As Long)
    ReDim gitemsizes(0 To num - 1) As Long
    ReDim gitemsizescompressed(0 To num - 1) As Long
    ReDim gitemdatas(0 To num - 1) As String
    ReDim gbitmaps(0 To num - 1, 0 To 1) As Long
    ReDim gbitmapwidths(0 To num - 1) As Long
    ReDim gbitmapheights(0 To num - 1) As Long
End Function
Function gread() As Boolean
    Dim i, j As Long
    Dim var As Long
    
    Open File For Binary Access Read As 135
    
    dlg_progresswindow.setcurrent (0)
    dlg_progresswindow.updateinterval = 8
    
    gread = False
    numitems = App.gread_int(135)
    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(135)
    Next
    For i = 0 To numitems - 1
        gitemsizes(i) = gread_int(135)
    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.Caption = "Item #" + App.zerostr(i, 4)
        'dlg_progresswindow.Refresh
        gitemdatas(i) = gread_bytes(gitemsizescompressed(i), 135)
        If gitemsizes(i) <> gitemsizescompressed(i) Then
        
            App.dprint ("decompressing item #" + Str(i))
            App.itemdata = gitemdatas(i)
            gitemdatas(i) = App.LZWUncompress()
        End If
    Next
    
    Close 135
    
    dlg_progresswindow.setcurrent (0)
    dlg_progresswindow.updateinterval = 1
    dlg_progresswindow.setmax (6)
    dlg_progresswindow.Title.Caption = "Loading file 556..."
    dlg_progresswindow.Refresh
    f556.itemdata = gitemdatas(556)
    f556.itemlen = gitemsizes(556)
    f556.read
    
    dlg_progresswindow.setcurrent (1)
    dlg_progresswindow.Title.Caption = "Loading file 558..."
    dlg_progresswindow.Refresh
    f558.itemdata = gitemdatas(558)
    f558.read
    
    dlg_progresswindow.setcurrent (2)
    dlg_progresswindow.Title.Caption = "Loading file 559..."
    dlg_progresswindow.Refresh
    f559.itemdata = gitemdatas(559)
    f559.read
    
    dlg_progresswindow.setcurrent (3)
    dlg_progresswindow.Title.Caption = "Loading file 560..."
    dlg_progresswindow.Refresh
    f560.itemdata = gitemdatas(560)
    f560.read

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

    dlg_progresswindow.setcurrent (5)
    dlg_progresswindow.Title.Caption = "Loading file 562..."
    dlg_progresswindow.Refresh
    'f562.itemdata = itemstrs(562)
    'f562.read
    
    gread = True
    'init_colorarray
    build_palettes
    build_invertedpalettes
    
    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.Caption = "Creating bitmap #" + zerostr(i, 4) + "..."
        If mapfile.gettype(i) = "IMG1" Then
            'build the bmp's
            buildbmp i
        End If
    Next
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

Function gsave()
    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 = 0 To numitems - 1
        write_swapword gitemsizes(i)
    Next
    For i = 0 To numitems - 1
        write_swapword gitemsizes(i)
    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))
        For j = 1 To gitemsizes(i)
            write_byte AscB(MidB(gitemdatas(i), j, 1))
        Next
    Next
            
    Close 135
End Function

'======================================
'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 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 = 0 To 15
        l1 = modifiedcolorarray(i)
        l1 = gpalettes(curPalette, i)
        
        b1 = (l1 And 255)
        g1 = (l1 And 65280) / 256
        r1 = (l1 And 16711425) / 65536
        
        l1 = color
        b2 = (l1 And 255)
        g2 = (l1 And 65280) / 256
        r2 = (l1 And 16711425) / 65536
        
        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
    
    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 Integer, ByVal num As Long) As Long
    itemstrs(itemnum) = InputB(num, 135)
End Function
Function read_byte() As Byte
    read_byte = AscB(InputB(1, 135))
    bytesread = bytesread + 1
End Function

Function read_int() As Long
    Dim b1, b2 As Byte
    Dim l1 As Long
    If endian = 1 Then 'little endian
        b1 = read_byte
        b2 = read_byte
    Else
        b2 = read_byte
        b1 = read_byte
    End If
    
    read_int = b2
    read_int = read_int * 256
    read_int = read_int + b1
End Function

Public Function sread_byte(ByVal data As String, ByVal offset As Long) As Byte
    sread_byte = AscB(MidB(data, offset, 1))
End Function
Public Function sread_word(ByVal data As String, ByVal offset As Long) As Long
    sread_word = sread_byte(data, offset) * &H100
    sread_word = sread_word + sread_byte(data, offset + 1)
End Function

Public Function swrite_byte(ByRef data As String, ByVal val As Byte)
    data = data + ChrB(val)
End Function
Public Function swrite_word(ByRef data As String, ByVal val As Long)
    Dim b1, b2 As Byte
    Dim l1 As Long
    l1 = val And &HFF00
    b1 = l1 / &H100
    b2 = val And &HFF
    swrite_byte data, b1
    swrite_byte data, b2
End Function

Function write_byte(num As Byte)
    Put 135, , num
End Function
Function write_word(ByVal num As Integer)
    Dim b1, b2 As Byte
    Dim l1 As Long
    l1 = num And &HFF00
    b1 = l1 / &H100
    b2 = num And &HFF
    write_byte (b2)
    write_byte (b1)
End Function
Function write_swapword(ByVal num As Long)
    Dim b1, b2 As Byte
    Dim l1 As Long
    l1 = num And &HFF00
    b1 = l1 / &H100
    b2 = num And &HFF
    write_byte (b1)
    write_byte (b2)
End Function
Function write_swaplong(ByVal num As Long)
    Dim b1, b2, b3, b4 As Byte
    Dim l1 As Long
    '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 (b2)
    write_byte (b3)
    write_byte (b4)
    write_byte (b1)
End Function
Function write_long(ByVal num As Long)
    Dim b1, b2, b3, b4 As Byte
    Dim l1 As Long
    '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)
    write_byte (b3)
    write_byte (b2)
    write_byte (b1)
End Function

Function getgitemdata(ByVal Index As Long) As String
    getgitemdata = gitemdatas(Index)
End Function
Function setgitemdata(ByVal Index As Long, ByVal data As String)
    gitemdatas(Index) = data
    'update the size? nah
    'gitemsizes(index) = LenB(data)
End Function
Function getgitemsize(ByVal Index As Long) As Long
    getgitemsize = gitemsizes(Index)
End Function
Sub setgitemsize(ByVal Index As Long, ByVal num As Long)
    gitemsizes(Index) = num
End Sub
Function getgitemsizecompressed(ByVal Index As Long) As Long
    getgitemsizecompressed = gitemsizescompressed(Index)
End Function
Sub setgitemsizecompressed(ByVal Index As Long, ByVal num As Long)
    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
Public Sub gExport(ByVal itemnum As Integer, ByVal fname As String)

    Dim ItemType As String
    ItemType = mapfile.gettype(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 Long, 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 Long, ByVal fname As String)

    
    Open fname For Binary Access Write As 135
    
    For i = 1 To gitemsizes(itemnum)
        write_byte AscB(MidB(gitemdatas(itemnum), i, 1))
    Next
    
    Close 135
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 String, ByVal fname As String) As Long

    import_raw = FileLen(fname)
    
    Open fname For Binary Access Read As 135
    
    itemdata = ""
    For i = 1 To import_raw
        itemdata = itemdata + ChrB(read_byte)
    Next
    
    Close 135
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 Long, ByVal height As Long) As String
    encode_img1 = ""
    swrite_word encode_img1, width
    swrite_word encode_img1, height
    
    Dim i, j, k, n As Long
    Dim b As Byte
    Dim max As Long
    Dim pixelcount_different As Long
    Dim pixelcount_same As Long
    Dim pixelcount_line As Long
    Dim pixelcolor As Long
    Dim nextpixel As Long
    Dim nibblearray(800000) As Byte
    
    max = width
    max = max * height
    n = 1
    For i = 1 To max
        pixelcolor = pixelarray(i)
        
        If pixelarray(i) = 16 Then 'transparent pixel(s)
            
            For j = i To max
                If pixelarray(j) <> pixelcolor Or j = max Then
                    pixelcount_same = j - i
                    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) = ((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) = ((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) = ((pixelcount_same - 1) And 61440) / 4096
                n = n + 1
                nibblearray(n) = ((pixelcount_same - 1) And 3840) / 256
                n = n + 1
                nibblearray(n) = ((pixelcount_same - 1) And 240) / 16
                n = n + 1
                nibblearray(n) = (pixelcount_same - 1) And 15
                n = n + 1
            End If
            i = i + pixelcount_same - 1
        Else
            pixelcount_same = 1
            For j = i To max
                If pixelarray(j) <> pixelcolor Or j = max Then
                    pixelcount_same = j - i
                    j = max + 1
                End If
            Next
            If pixelcount_same < 1 Then
                pixelcount_same = 1
            End If
                
            pixelcount_line = 0
            If (i - 1) > width Then 'check for copying from above
                
                For j = i To max - 1
                    If pixelarray(j) <> pixelarray(j - width) Or j = (max - 1) Then
                        pixelcount_line = j - i
                        j = max + 1
                    End If
                Next
            End If
            
            If pixelcount_line > pixelcount_same Then
                'do copybits from above line
                nextpixel = pixelarray(i + pixelcount_line)
                If pixelcount_line <= 256 Then
                    nibblearray(n) = 11
                    n = n + 1
                    nibblearray(n) = nextpixel
                    n = n + 1
                    nibblearray(n) = ((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) = ((pixelcount_line - 1) And 61440) / 4096
                    n = n + 1
                    nibblearray(n) = ((pixelcount_line - 1) And 3840) / 256
                    n = n + 1
                    nibblearray(n) = ((pixelcount_line - 1) And 240) / 16
                    n = n + 1
                    nibblearray(n) = (pixelcount_line - 1) And 15
                    n = n + 1
                End If
                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) = ((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) = ((pixelcount_same - 1) And 61440) / 4096
                    n = n + 1
                    nibblearray(n) = ((pixelcount_same - 1) And 3840) / 256
                    n = n + 1
                    nibblearray(n) = ((pixelcount_same - 1) And 240) / 16
                    n = n + 1
                    nibblearray(n) = (pixelcount_same - 1) And 15
                    n = n + 1
                End If
                i = i + pixelcount_same - 1
            End If
            'swrite_byte encode_img1, pixelarray(i)
        End If
    Next
    n = n - 1
    For i = 1 To n
        b = nibblearray(i) * 16
        i = i + 1
        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 Long, ByVal fname As String, mirrored As Integer)

    ' if mirrored > 0 then mirror it, else dont... dont use that old shit
    '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 = 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) = (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
    'write the current palette
    For i = 0 To 255
        write_swaplong gpalettes(curPalette, i)
    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 > 0 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

' 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 Function buildbmp(ByVal itemnum As Long) As Long

    'construct nibblearray
    Dim nibbles() As Byte
    Dim nibblelen As Long
    Dim pixelarray() As Byte
    Dim mirroredpixelarray() 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(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) = (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 Function
        End If
        
    Next
    
    
    p = p - 1
    
    'ok we have the pixel array, now lets create a bitmap
    Dim bmpinfo As BITMAPINFO_256
    
    'bmpheader
    With bmpinfo.bmiHeader
        .biSize = Len(bmpinfo.bmiHeader)
        .biWidth = pwidth          ' Width in pixels.
        .biHeight = pheight * -1       ' Height in pixels.
        .biPlanes = 1           ' 1 color plane.
        .biBitCount = 8         ' 8 bits per pixel.
        .biCompression = BI_RGB ' No compression.
        .biSizeImage = 0        ' Unneeded with no compression.
        .biXPelsPerMeter = 0    ' Unneeded.
        .biYPelsPerMeter = 0    ' Unneeded.
        .biClrUsed = 256        ' # colors in color table that are used by the image. 0 means all.
        .biClrImportant = 256   ' # important colors. 0 means all.
    End With
    
    For i = 0 To 255
        'for some reason, we have to swap colors here, i dont know why
        'it seems as though VB's RGB(r,g,b) produces a BGR type effect in bitmaps
        bmpinfo.bmiColors(i) = ginvertedpalettes(0, i)
    Next

    'regular
    If gbitmaps(itemnum, 0) > 0 Then
        DeleteObject gbitmaps(itemnum, 0)
    End If
    
    'create the bitmap section, this was so gay to figure out
    gbitmaps(itemnum, 0) = CreateDIBSection(gDC, bmpinfo, DIB_RGB_COLORS, Result, 0, 0)
    If gbitmaps(itemnum, 0) = 0 Then
        MsgBox "Error creating graphic #" + Str(itemnum)
    End If
    
    'set the bits to the pixelarray
    Result = SetDIBits(gDC, gbitmaps(itemnum, 0), 0, pheight, pixelarray(1), bmpinfo, DIB_RBG_COLORS)
    If Result = 0 Then
        MsgBox "Error setting bits for graphic #" + Str(itemnum)
    End If
    
    '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
    
    If gbitmaps(itemnum, 1) > 0 Then
        'we have an old bitmap, lets toss it
        DeleteObject gbitmaps(itemnum, 1)
    End If
    
    'mirrored
    'create the bitmap section, this was so gay to figure out
    gbitmaps(itemnum, 1) = CreateDIBSection(gDC, bmpinfo, DIB_RGB_COLORS, Result, 0, 0)
    If gbitmaps(itemnum, 1) = 0 Then
        MsgBox "Error creating graphic #" + Str(itemnum) + " mirrored"
    End If
    'set the bits to the pixelarray
    Result = SetDIBits(gDC, gbitmaps(itemnum, 1), 0, pheight, mirroredpixelarray(1), bmpinfo, DIB_RBG_COLORS)
    If Result = 0 Then
        MsgBox "Error setting bits for graphic #" + Str(itemnum) + " mirrored"
    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 Function drawbmp(ByRef dst As Long, ByVal itemnum As Long)
    Dim Result As Long
    'Dim srcDC As Long
    Dim dibct(0 To 350) As Long
    Dim distr As String
    Dim dirbg(0 To 255) As RGBQUAD
    Dim i As Long
    
    
    'srcDC = CreateCompatibleDC(dst.hdc)
    
    SelectObject gDC, gbitmaps(itemnum, gMirrored)
    'SelectObject gDC(itemnum), gbitmaps(itemnum, 0)
    
    'Result = GetDIBColorTable(gDC, 0, 256, dibct(0))
   ' MsgBox Str(Result) + " " + Hex(dibct(0)) + " " + Hex(dibct(1)) + " " + Hex(dibct(2)) + " " + Hex(dibct(3))
    
    For i = 0 To 255
        dibct(i) = ginvertedpalettes(curPalette, i)
    Next
    Result = SetDIBColorTable(gDC, 0, 256, dibct(0))
    'MsgBox Str(Result) + " " + Hex(ginvertedpalettes(curPalette, 0)) + " " + Hex(ginvertedpalettes(curPalette, 1)) + " " + Hex(ginvertedpalettes(curPalette, 2)) + " " + Hex(ginvertedpalettes(curPalette, 3))
    
    'Result = GetDIBColorTable(gDC, 0, 256, dibct(0))
    'MsgBox Str(Result) + " " + Hex(dibct(0)) + " " + Hex(dibct(1)) + " " + Hex(dibct(2)) + " " + Hex(dibct(3))
    
    'Result = GetDIBColorTable(gDC, 0, 256, dirbg(0))
    'MsgBox Str(Result)
    'For i = 0 To 255
    '    dibct(i) = gpalettes(3, i)
    'Next
    
    Result = BitBlt(dst, 0, 0, gbitmapwidths(itemnum), gbitmapheights(itemnum), gDC, 0, 0, vbSrcCopy)
    'MsgBox "bitblt: " + Str(Result)
    'DeleteDC srcDC
    
    'dst.Refresh
End Function
'standard drawbmp, just draw the fucker.
Public Function drawbmpat(ByVal dst As Long, ByVal itemnum As Long, ByVal X As Long, ByVal Y As Long)
    Dim Result As Long
    'Dim srcDC As Long
    Dim dibct(0 To 350) As Long
    Dim distr As String
    Dim dirbg(0 To 255) As RGBQUAD
    Dim i As Long
    
    
    SelectObject gDC, gbitmaps(itemnum, gMirrored)
    
    For i = 0 To 255
        dibct(i) = ginvertedpalettes(curPalette, i)
    Next
    Result = SetDIBColorTable(gDC, 0, 256, dibct(0))
    
    Result = BitBlt(dst, X, Y, gbitmapwidths(itemnum), gbitmapheights(itemnum), gDC, 0, 0, vbSrcCopy)

End Function
Public Function stretchbmp(ByVal dst As Long, ByVal itemnum As Long, ByVal dstx As Long, ByVal dsty As Long, ByVal dstwidth As Long, ByVal dstheight As Long, ByVal srcx As Long, ByVal srcy As Long)
    Dim Result As Long
    'Dim srcDC As Long
    Dim dibct(0 To 350) As Long
    Dim i As Long
    
    
    SelectObject gDC, gbitmaps(itemnum, gMirrored)
    
    For i = 0 To 255
        dibct(i) = ginvertedpalettes(curPalette, i)
    Next
    Result = SetDIBColorTable(gDC, 0, 256, dibct(0))
    Result = StretchBlt(dst, dstx, dsty, dstwidth, dstheight, gDC, srcx, srcy, gbitmapwidths(itemnum), gbitmapheights(itemnum), vbSrcCopy)
'    Result = StretchBlt(dst, X, Y, gbitmapwidths(itemnum), gbitmapheights(itemnum), gDC, srcx, srcy, srcwidth, srcheight, vbSrcCopy)

End Function
Public Function stretchbmpby(ByVal dst As Long, ByVal itemnum As Long, ByVal dstx As Long, ByVal dsty As Long, ByVal srcx As Long, ByVal srcy As Long, ByVal dstwidth As Long, ByVal dstheight As Long)
    Dim Result As Long
    'Dim srcDC As Long
    Dim dibct(0 To 350) As Long
    Dim i As Long
    
    dstheight = dstheight * gbitmapheights(itemnum) / 100
    dstwidth = dstwidth * gbitmapwidths(itemnum) / 100
    SelectObject gDC, gbitmaps(itemnum, gMirrored)
    
    For i = 0 To 255
        dibct(i) = ginvertedpalettes(curPalette, i)
    Next
    Result = SetDIBColorTable(gDC, 0, 256, dibct(0))
    
    Result = StretchBlt(dst, dstx, dsty, dstwidth, dstheight, gDC, srcx, srcy, gbitmapwidths(itemnum), gbitmapheights(itemnum), vbSrcCopy)
    'Result = StretchBlt(dst, x, y, gbitmapwidths(itemnum), gbitmapheights(itemnum), gDC, srcx, srcy, srcwidth, srcheight, vbSrcCopy)

End Function
Public Function drawtransparentbmp(ByRef dst As Long, ByVal itemnum As Long, ByVal TransparentPixel As Long)
    Dim Result As Long
    'Dim srcDC As Long
    Dim dibct(0 To 350) As Long
    Dim i As Long
    
    
    SelectObject gDC, gbitmaps(itemnum, gMirrored)
    
    For i = 0 To 255
        dibct(i) = RGB(255, 255, 255)
    Next
    dibct(TransparentPixel) = RGB(0, 0, 0)
    Result = SetDIBColorTable(gDC, 0, 256, dibct(0))
    
    Result = BitBlt(dst, 0, 0, gbitmapwidths(itemnum), gbitmapheights(itemnum), gDC, 0, 0, vbSrcInvert)
    For i = 0 To 255
        dibct(i) = ginvertedpalettes(curPalette, i)
    Next
    dibct(TransparentPixel) = RGB(0, 0, 0)
    Result = SetDIBColorTable(gDC, 0, 256, dibct(0))
    Result = BitBlt(dst, 0, 0, gbitmapwidths(itemnum), gbitmapheights(itemnum), gDC, 0, 0, vbSrcPaint)

End Function
'standard drawbmp, just draw the fucker.
Public Function drawtransparentbmpat(ByVal dst As Long, ByVal itemnum As Long, ByVal X As Long, ByVal Y As Long, ByVal TransparentPixel As Long)
    Dim Result As Long
    'Dim srcDC As Long
    Dim dibct(0 To 350) As Long
    Dim i As Long
    
    SelectObject gDC, gbitmaps(itemnum, gMirrored)
    
    For i = 0 To 255
        dibct(i) = RGB(0, 0, 0)
    Next
    dibct(TransparentPixel) = RGB(255, 255, 255)
    Result = SetDIBColorTable(gDC, 0, 256, dibct(0))
    
    Result = BitBlt(dst, X, Y, gbitmapwidths(itemnum), gbitmapheights(itemnum), gDC, 0, 0, vbSrcAnd)
    For i = 0 To 255
        dibct(i) = ginvertedpalettes(curPalette, i)
    Next
    dibct(TransparentPixel) = RGB(0, 0, 0)
    Result = SetDIBColorTable(gDC, 0, 256, dibct(0))
    Result = BitBlt(dst, X, Y, gbitmapwidths(itemnum), gbitmapheights(itemnum), gDC, 0, 0, vbSrcPaint)

End Function
Public Function stretchtransparentbmp(ByVal dst As Long, ByVal itemnum As Long, ByVal dstx As Long, ByVal dsty As Long, ByVal dstwidth As Long, ByVal dstheight As Long, ByVal srcx As Long, ByVal srcy As Long, ByVal TransparentPixel As Long)
    Dim Result As Long
    'Dim srcDC As Long
    Dim dibct(0 To 350) As Long
    Dim i As Long
    
    
    SelectObject gDC, gbitmaps(itemnum, gMirrored)
    
    For i = 0 To 255
        dibct(i) = RGB(0, 0, 0)
    Next
    dibct(TransparentPixel) = RGB(255, 255, 255)
    Result = SetDIBColorTable(gDC, 0, 256, dibct(0))
    
    Result = StretchBlt(dst, dstx, dsty, dstwidth, dstheight, gDC, srcx, srcy, gbitmapwidths(itemnum), gbitmapheights(itemnum), vbSrcAnd)
    'Result = BitBlt(dst, X, Y, gbitmapwidths(itemnum), gbitmapheights(itemnum), gDC, 0, 0, vbSrcAnd)
    For i = 0 To 255
        dibct(i) = ginvertedpalettes(curPalette, i)
    Next
    dibct(TransparentPixel) = RGB(0, 0, 0)
    Result = SetDIBColorTable(gDC, 0, 256, dibct(0))
    Result = StretchBlt(dst, dstx, dsty, dstwidth, dstheight, gDC, srcx, srcy, gbitmapwidths(itemnum), gbitmapheights(itemnum), vbSrcPaint)
    'Result = BitBlt(dst, X, Y, gbitmapwidths(itemnum), gbitmapheights(itemnum), gDC, 0, 0, vbSrcPaint)

'    For i = 0 To 255
'        dibct(i) = ginvertedpalettes(curPalette, i)
'    Next
'    Result = SetDIBColorTable(gDC, 0, 256, dibct(0))
'    Result = StretchBlt(dst, dstX, dstY, dstwidth, dstheight, gDC, srcx, srcy, gbitmapwidths(itemnum), gbitmapheights(itemnum), vbSrcCopy)
'    Result = StretchBlt(dst, X, Y, gbitmapwidths(itemnum), gbitmapheights(itemnum), gDC, srcx, srcy, srcwidth, srcheight, vbSrcCopy)

End Function

Public Function stretchtransparentbmpsrc(ByVal dst As Long, ByVal itemnum As Long, ByVal dstx As Long, ByVal dsty As Long, ByVal dstwidth As Long, ByVal dstheight As Long, ByVal srcx As Long, ByVal srcy As Long, ByVal srcwidth As Long, ByVal srcheight As Long, ByVal TransparentPixel As Long)
    Dim Result As Long
    'Dim srcDC As Long
    Dim dibct(0 To 350) As Long
    Dim i As Long
    
    
    SelectObject gDC, gbitmaps(itemnum, gMirrored)
    
    For i = 0 To 255
        dibct(i) = RGB(0, 0, 0)
    Next
    dibct(TransparentPixel) = RGB(255, 255, 255)
    Result = SetDIBColorTable(gDC, 0, 256, dibct(0))
    
    Result = StretchBlt(dst, dstx, dsty, dstwidth, dstheight, gDC, srcx, srcy, srcwidth, srcheight, vbSrcAnd)
    For i = 0 To 255
        dibct(i) = ginvertedpalettes(curPalette, i)
    Next
    dibct(TransparentPixel) = RGB(0, 0, 0)
    Result = SetDIBColorTable(gDC, 0, 256, dibct(0))
    Result = StretchBlt(dst, dstx, dsty, dstwidth, dstheight, gDC, srcx, srcy, srcwidth, srcheight, vbSrcPaint)

End Function
Public Function fill(ByVal dst As Long, ByVal dstx As Long, ByVal dsty As Long, ByVal dstwidth As Long, ByVal dstheight As Long, ByVal color As Long)
    
    Dim hBr As Long
    Dim hRgn As Long
    
    Dim Result As Long
    
    hBr = CreateSolidBrush(color)
    hRgn = CreateRectRgn(dstx, dsty, dstwidth + dstx, dstheight + dsty)
    Result = FillRgn(dst, hRgn, hBr)
    
    'Select this brush into our form's device context
    'SelectObject dst, hFFBrush
    'Floodfill our form
    'result = FloodFill(dst, dstx, dsty, color)
    'Delete our brush
    DeleteObject hBr
    DeleteObject hRgn
    
End Function
Public Function drawgrect(ByVal dst As Long, ByVal grectnum As Long, ByVal GraphicNum As Long, ByVal TransparentPixel As Long, ByVal percent As Long)

    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 Function


Public Function unload()

    Dim i As Integer
    Dim num As Integer
    num = 0
    For i = 0 To numitems - 1
        If gbitmaps(i, 0) <> 0 Then
            'we have an old bitmap, lets toss it
            DeleteObject gbitmaps(i, 0)
            num = num + 1
        End If
        If gbitmaps(i, 1) <> 0 Then
            'we have an old bitmap, lets toss it
            DeleteObject gbitmaps(i, 1)
            num = num + 1
        End If
    Next
    
    DeleteDC gDC
End Function
