$include "c:\rapidq\rapidq.inc"

$resource magentaBMP as "c:\rapidq\magenta.bmp"
$resource cyanBMP as "c:\rapidq\cyan.bmp"
$resource 320BMP as "c:\rapidq\320.bmp"
'$resource 200BMP as "200.bmp"

declare sub imChange
declare sub hotFill
declare sub hotChange
declare sub hotSave
declare sub hotGroup
declare sub tabChange
declare sub tab1Change
declare sub saveText
declare sub selectFile (Sender as QFileListBox)
declare sub changeDirectory (sender as qdirtree)
declare sub changeDir
declare sub pckFile (f$)
declare sub hotFile (f$)
declare sub countPckFile (f$)
declare sub colFile (f$)
declare sub drawImage (f$ as string, startOfImage as long, endOfImage as long, xp as word, yp as word, s as word, i as word)
declare sub drawGroup (f$ as string, grp as integer)
declare sub drawMyImage (f$ as string, startOfImage as long, endOfImage as long, xp as word, yp as word, s as word, i as word)
declare sub cvtCol (ssCol as word, r as word, g as word, b as word, bt as word)
declare function winCol (r as word, g as word, b as word, bt as word) as long
declare function winToSuSt (w as integer) as word
declare function sustToWin (w as integer) as integer
declare sub addToWinPal (wc as integer)
declare sub addToSustPal (wc as integer)
declare sub drawAll (f$)
declare sub hotAll 
declare sub BMPtoPCK
declare sub BMPsToPCK (Sender as Qbutton)
declare sub newWay (Sender as Qbutton)
declare sub scanBMP (bp as qbitmap)
declare sub writeImage (bp as qbitmap) 
declare sub PCKtoBMPs (Sender as Qbutton)
declare sub PCKtoEditBMPs (Sender as Qbutton)
declare sub createOneBMP (Sender as Qbutton)
declare sub selectList (Sender as QListBox)
declare sub scrollChange (Sender as Qscrollbar)
declare sub glbOutFileChange (Sender as Qedit)
declare function paletteNumber(wc as integer) as byte
declare sub paletteGlb(wc as integer)

declare sub rs2File (f$ as string)
declare sub rs2FilePose (f$ as string, pose as integer)
declare sub rs2FileGroup (f$ as string, grp as integer)
declare sub pckImage (f$ as string, startOfImage as long, endOfImage as long)
declare sub aniFile (f$ as string)
declare sub resFile (f$ as string)
declare sub selectAny (Sender as qRadioButton)
declare sub formResize
declare sub canvasClick (Button%, X%, Y%, Shift%)
declare sub drawPalette

declare sub writeTestPattern()

CREATE Form AS QFORM
    Caption = "SuSt_units 0.0.146"
    Width = 800
    Height = 600
    Center
    onPaint = formResize

    create miscLbl as qlabel
        left = 10
        top = 560
        width = 100
        caption = "........................."
    end create
    
'----------
CREATE Tab AS QTabControl 
    AddTabs "files", "about"
    Left = 5
    Top = 5
    Width = 370
    Height = 250 
'    Width = Form.ClientWidth 
'    Height = Form.ClientHeight 
    OnChange = TabChange 
    HotTrack = True 
    
    CREATE Panel0 AS QPanel 
        Top = 40 
        Left = 5 
        Width = 360 
        Height = 205 
'        Caption = "Panel 1" 
        BevelInner = bvLowered 
        create dirtree as QDirTree
            initialDir = CURDIR$
'        initialDir = "c:\program files\sudden strike\my_mod\units"        
            Left = 5
            Top = 5
            width = 175
            height = 160
            showhint = false
            onchange = changeDirectory
        end create
        create dirLbl as qlabel
            left = 5
            top = 210
            caption = "select your units folder"
        end create
        CREATE FileListBox1 AS QFILELISTBOX
            Left = 180
            Top = 5
            width = 175
            Height = 160
            Drive = "C"
'        Directory = "D:\rapidQ"
'        Directory = "C:\rapidQ"
            ItemHeight = 16
            onClick = selectFile
        END CREATE
        create p as qpanel
            Left = 5 : width = 350 : Top = 170 : Height = 30
            CREATE showAny AS Qradiobutton
                Left = 5 : Top = 5 : width = 40 : checked = true : caption = "any" : onClick = selectAny
            END CREATE
            CREATE showPck AS Qradiobutton
                Left = 50 : Top = 5 : width = 40 : checked = false : caption = "PCK" : onClick = selectAny
            END CREATE
            CREATE showRs2 AS Qradiobutton
                Left = 100 : Top = 5 : width = 40 : checked = false : caption = "RS2" : onClick = selectAny
            END CREATE
            CREATE showBmp AS Qradiobutton
                Left = 150 : Top = 5 : width = 45 : checked = false : caption = "BMP" : onClick = selectAny
            END CREATE
            CREATE showCOL AS Qradiobutton
                Left = 200 : Top = 5 : width = 45 : checked = false : caption = "COL" : onClick = selectAny
            END CREATE
            CREATE showANI AS Qradiobutton
                Left = 200 : Top = 5 : width = 45 : checked = false : caption = "ANI" : onClick = selectAny
            END CREATE
            CREATE showRES AS Qradiobutton
                Left = 250 : Top = 5 : width = 45 : checked = false : caption = "RES" : onClick = selectAny
            END CREATE
        end create ' panel 
    END CREATE 
    CREATE Panel1 AS QPanel 
        Top = 40 
        Left = 5 
        Width = 360 
        Height = 205 
'        Caption = "Panel 2" 
        BevelInner = bvLowered 
        Visible = False 
'        CREATE ListBox1 AS QLISTBOX
'            Left = 5 : Top = 5 : Width = 370 : Height = 195 : ItemHeight = 16
'            TabOrder = 3 : multiselect = true : hint = "this is a hint"
'            font.name = "Courier New" : font.size = 8 : onclick = selectList
'        END CREATE
    END CREATE 
END CREATE 

CREATE Tab1 AS QTabControl 
    AddTabs "text", "BMP", "view", "new unit", "palette", "hot", "editorBMPs" 
    Left = 375
    Top = 5
    Width = 410
    Height = 250 
'    Width = Form.ClientWidth 
'    Height = Form.ClientHeight 
    OnChange = Tab1Change 
    HotTrack = True 
    
    CREATE Panel10 AS QPanel 
        Top = 40 
        Left = 5 
        Width = 400 
        Height = 205 
'        Caption = "Panel 2" 
        BevelInner = bvLowered 
        create saveListToText as qbutton
            left = 20
            top = 5
            width = 150
            caption = "save text"
            onclick = saveText 
        end create
        CREATE ListBox1 AS QLISTBOX
            Left = 5 : Top = 25 : Width = 390 : Height = 175 : ItemHeight = 16
            TabOrder = 3 : multiselect = true : hint = "this is a hint"
            font.name = "Courier New" : font.size = 8 : onclick = selectList
        END CREATE
    END CREATE 

    CREATE Panel11 AS QPanel 
        Top = 23 
        Left = 3 
        Width = 402 
        Height = 425 
        Visible = False 
        create bmpLbl as qlabel
            left = 5
            top = 3
'            caption = "select your units folder"
        end create
        create myImage as Qimage
            Left = 1  '580
            Top = 18  '360
            width = 200
            Height = 200
            bmphandle = magentaBMP
        end create
    END CREATE 
    
    CREATE Panel12 AS QPanel 
        Top = 40 
        Left = 5 
        Width = 400 
        Height = 205 
        BevelInner = bvLowered 
        Visible = False 
        CREATE imageNumber AS Qscrollbar
            Left = 20
            Top = 10
            Width = 275
            Height = 20
            min = 0
            max = 31
            position = 0
            onchange = scrollChange
        END CREATE
        create imgLbl as qlabel
            top = 35
            left = 80
            caption = "<< rotate PCK images >>"
        end create
        CREATE showTransparent AS Qcheckbox
            Left = 20
            Top = 60
            Width = 180
            Height = 20
            caption = "show transparent as white"
        END CREATE
        CREATE showCompression AS Qcheckbox
            Left = 20
            Top = 90
            Width = 180
            Height = 20
            caption = "show RLE information"
        END CREATE
        CREATE showGrid AS Qcheckbox
            Left = 250
            Top = 60
            Width = 180
            Height = 20
            caption = "show crosshairs"
            checked = true
        END CREATE
        CREATE showDouble AS Qcheckbox
            Left = 250
            Top = 90
            Width = 180
            Height = 20
            caption = "show double size"
            checked = false
        END CREATE
        create PCKtoBMPsbtn as qbutton
            left = 20
            top = 120
            width = 150
            caption = "PCK ---> BMPs"
            onclick = PCKtoBMPs 
        end create
' not used currently ...
'        create oneBMP as qbutton
'            left = 20
'            top = 120
'            width = 150
'            caption = "create first BMP"
'            onclick = createOneBMP
'            visible = false
'            enabled = false
'        end create
    END CREATE 

    CREATE Panel13 AS QPanel 
        Top = 40 
        Left = 5 
        Width = 400 
        Height = 205 
        BevelInner = bvLowered 
        Visible = False 
        create BMPsToPCKbtn as qbutton
            left = 20
            top = 10
            width = 150
            caption = "BMPs ---> PCK"
            onclick = BMPsToPCK 
        end create
        create gsLbl as qlabel
            top = 40 : left = 20 : caption = "BMPs    start"
        end create
        create glbStartEdit as qedit
            top = 40 : left = 90 : width = 40 : text = ""
        end create
        create geLbl as qlabel
            top = 40 : left = 140 : caption = "end"
        end create
        create glbEndEdit as qedit
            top = 40 : left = 180 : width = 40 : text = ""
        end create
        create goLbl as qlabel
            top = 70
            left = 20
            caption = "new unit name"
        end create
        create glbOutEdit as qedit
            top = 70
            left = 100
            width = 60
            text = "new_unit"
            onChange = glbOutFileChange
        end create
        create gtsLbl as qlabel
            top = 100 : left = 20 : caption = "shadow tolerance "
        end create
        create glbShadowTolEdit as qedit
            top = 100 : left = 120 : width = 40 : text = "64"
        end create
        create gcLbl as qlabel
            top = 130 : left = 20 : caption = "color reduction "
        end create
        create glbColRedEdit as qedit
            top = 130 : left = 120 : width = 60 : text = ""
        end create
        create quitOnOverflow as qcheckbox
            top = 160 : left = 20 : width = 160 : caption = " quit on palette overflow "
            checked = true
        end create
    end create
    
    CREATE Panel14 AS QPanel 
        Top = 25 
        Left = 5 
        Width = 400 
        Height = 350 
        BevelInner = bvLowered 
        Visible = False 
        create palName as qlabel
            top = 3 : left = 5 : width = 100 : caption = "palette file name"
        end create
        create palSize as qlabel
            top = 25 : left = 340 : width = 50 : caption = "size"
        end create
        create colPalette as QIMAGE
            Top = 20
            Left = 5
            width = 320
            Height = 320
            bmphandle = 320bmp
'            onMouseDown = clickedPalette 
        end create
    end create
    
    CREATE Panel15 AS QPanel 
        Top = 25 
        Left = 5 
        Width = 400 
        Height = 350 
        BevelInner = bvLowered 
        Visible = False 
        create ht1Lbl as qlabel : top = 13 : left = 10 : caption = "1" : end create
        create ht2Lbl as qlabel : top = 13 : left = 110 : caption = "2" : end create
        create ht3Lbl as qlabel : top = 13 : left = 210 : caption = "3" : end create
        create ht4Lbl as qlabel : top = 13 : left = 310 : caption = "4" : end create
        create htx1Edit as qedit : top = 10 : left = 30 : width = 30 : text = "" : end create
        create hty1Edit as qedit : top = 10 : left = 60 : width = 30 : text = "" : end create
        create htx2Edit as qedit : top = 10 : left = 130 : width = 30 : text = "" : end create
        create hty2Edit as qedit : top = 10 : left = 160 : width = 30 : text = "" : end create
        create htx3Edit as qedit : top = 10 : left = 230 : width = 30 : text = "" : end create
        create hty3Edit as qedit : top = 10 : left = 260 : width = 30 : text = "" : end create
        create htx4Edit as qedit : top = 10 : left = 330 : width = 30 : text = "" : end create
        create hty4Edit as qedit : top = 10 : left = 360 : width = 30 : text = "" : end create

        create ht5Lbl as qlabel : top = 33 : left = 10 : caption = "5" : end create
        create ht6Lbl as qlabel : top = 33 : left = 110 : caption = "6" : end create
        create ht7Lbl as qlabel : top = 33 : left = 210 : caption = "7" : end create
        create ht8Lbl as qlabel : top = 33 : left = 310 : caption = "8" : end create
        create htx5Edit as qedit : top = 30 : left = 30 : width = 30 : text = "" : end create
        create hty5Edit as qedit : top = 30 : left = 60 : width = 30 : text = "" : end create
        create htx6Edit as qedit : top = 30 : left = 130 : width = 30 : text = "" : end create
        create hty6Edit as qedit : top = 30 : left = 160 : width = 30 : text = "" : end create
        create htx7Edit as qedit : top = 30 : left = 230 : width = 30 : text = "" : end create
        create hty7Edit as qedit : top = 30 : left = 260 : width = 30 : text = "" : end create
        create htx8Edit as qedit : top = 30 : left = 330 : width = 30 : text = "" : end create
        create hty8Edit as qedit : top = 30 : left = 360 : width = 30 : text = "" : end create

        create ht9Lbl as qlabel : top = 53 : left = 10 : caption = "9" : end create
        create ht10Lbl as qlabel : top = 53 : left = 110 : caption = "10" : end create
        create ht11Lbl as qlabel : top = 53 : left = 210 : caption = "11" : end create
        create ht12Lbl as qlabel : top = 53 : left = 310 : caption = "12" : end create
        create htx9Edit as qedit : top = 50 : left = 30 : width = 30 : text = "" : end create
        create hty9Edit as qedit : top = 50 : left = 60 : width = 30 : text = "" : end create
        create htx10Edit as qedit : top = 50 : left = 130 : width = 30 : text = "" : end create
        create hty10Edit as qedit : top = 50 : left = 160 : width = 30 : text = "" : end create
        create htx11Edit as qedit : top = 50 : left = 230 : width = 30 : text = "" : end create
        create hty11Edit as qedit : top = 50 : left = 260 : width = 30 : text = "" : end create
        create htx12Edit as qedit : top = 50 : left = 330 : width = 30 : text = "" : end create
        create hty12Edit as qedit : top = 50 : left = 360 : width = 30 : text = "" : end create
        
        create ht13Lbl as qlabel : top = 73 : left = 10 : caption = "13" : end create
        create ht14Lbl as qlabel : top = 73 : left = 110 : caption = "14" : end create
        create ht15Lbl as qlabel : top = 73 : left = 210 : caption = "15" : end create
        create ht16Lbl as qlabel : top = 73 : left = 310 : caption = "16" : end create
        create htx13Edit as qedit : top = 70 : left = 30 : width = 30 : text = "" : end create
        create hty13Edit as qedit : top = 70 : left = 60 : width = 30 : text = "" : end create
        create htx14Edit as qedit : top = 70 : left = 130 : width = 30 : text = "" : end create
        create hty14Edit as qedit : top = 70 : left = 160 : width = 30 : text = "" : end create
        create htx15Edit as qedit : top = 70 : left = 230 : width = 30 : text = "" : end create
        create hty15Edit as qedit : top = 70 : left = 260 : width = 30 : text = "" : end create
        create htx16Edit as qedit : top = 70 : left = 330 : width = 30 : text = "" : end create
        create hty16Edit as qedit : top = 70 : left = 360 : width = 30 : text = "" : end create
        
        create ht17Lbl as qlabel : top = 93 : left = 10 : caption = "17" : end create
        create ht18Lbl as qlabel : top = 93 : left = 110 : caption = "18" : end create
        create ht19Lbl as qlabel : top = 93 : left = 210 : caption = "19" : end create
        create ht20Lbl as qlabel : top = 93 : left = 310 : caption = "20" : end create
        create htx17Edit as qedit : top = 90 : left = 30 : width = 30 : text = "" : end create
        create hty17Edit as qedit : top = 90 : left = 60 : width = 30 : text = "" : end create
        create htx18Edit as qedit : top = 90 : left = 130 : width = 30 : text = "" : end create
        create hty18Edit as qedit : top = 90 : left = 160 : width = 30 : text = "" : end create
        create htx19Edit as qedit : top = 90 : left = 230 : width = 30 : text = "" : end create
        create hty19Edit as qedit : top = 90 : left = 260 : width = 30 : text = "" : end create
        create htx20Edit as qedit : top = 90 : left = 330 : width = 30 : text = "" : end create
        create hty20Edit as qedit : top = 90 : left = 360 : width = 30 : text = "" : end create
        
        create ht21Lbl as qlabel : top = 113 : left = 10 : caption = "21" : end create
        create ht22Lbl as qlabel : top = 113 : left = 110 : caption = "22" : end create
        create ht23Lbl as qlabel : top = 113 : left = 210 : caption = "23" : end create
        create ht24Lbl as qlabel : top = 113 : left = 310 : caption = "24" : end create
        create htx21Edit as qedit : top = 110 : left = 30 : width = 30 : text = "" : end create
        create hty21Edit as qedit : top = 110 : left = 60 : width = 30 : text = "" : end create
        create htx22Edit as qedit : top = 110 : left = 130 : width = 30 : text = "" : end create
        create hty22Edit as qedit : top = 110 : left = 160 : width = 30 : text = "" : end create
        create htx23Edit as qedit : top = 110 : left = 230 : width = 30 : text = "" : end create
        create hty23Edit as qedit : top = 110 : left = 260 : width = 30 : text = "" : end create
        create htx24Edit as qedit : top = 110 : left = 330 : width = 30 : text = "" : end create
        create hty24Edit as qedit : top = 110 : left = 360 : width = 30 : text = "" : end create
        
        create ht25Lbl as qlabel : top = 133 : left = 10 : caption = "25" : end create
        create ht26Lbl as qlabel : top = 133 : left = 110 : caption = "26" : end create
        create ht27Lbl as qlabel : top = 133 : left = 210 : caption = "27" : end create
        create ht28Lbl as qlabel : top = 133 : left = 310 : caption = "28" : end create
        create htx25Edit as qedit : top = 130 : left = 30 : width = 30 : text = "" : end create
        create hty25Edit as qedit : top = 130 : left = 60 : width = 30 : text = "" : end create
        create htx26Edit as qedit : top = 130 : left = 130 : width = 30 : text = "" : end create
        create hty26Edit as qedit : top = 130 : left = 160 : width = 30 : text = "" : end create
        create htx27Edit as qedit : top = 130 : left = 230 : width = 30 : text = "" : end create
        create hty27Edit as qedit : top = 130 : left = 260 : width = 30 : text = "" : end create
        create htx28Edit as qedit : top = 130 : left = 330 : width = 30 : text = "" : end create
        create hty28Edit as qedit : top = 130 : left = 360 : width = 30 : text = "" : end create
        
        create ht29Lbl as qlabel : top = 153 : left = 10 : caption = "29" : end create
        create ht30Lbl as qlabel : top = 153 : left = 110 : caption = "30" : end create
        create ht31Lbl as qlabel : top = 153 : left = 210 : caption = "31" : end create
        create ht32Lbl as qlabel : top = 153 : left = 310 : caption = "32" : end create
        create htx29Edit as qedit : top = 150 : left = 30 : width = 30 : text = "" : end create
        create hty29Edit as qedit : top = 150 : left = 60 : width = 30 : text = "" : end create
        create htx30Edit as qedit : top = 150 : left = 130 : width = 30 : text = "" : end create
        create hty30Edit as qedit : top = 150 : left = 160 : width = 30 : text = "" : end create
        create htx31Edit as qedit : top = 150 : left = 230 : width = 30 : text = "" : end create
        create hty31Edit as qedit : top = 150 : left = 260 : width = 30 : text = "" : end create
        create htx32Edit as qedit : top = 150 : left = 330 : width = 30 : text = "" : end create
        create hty32Edit as qedit : top = 150 : left = 360 : width = 30 : text = "" : end create

        create htDLbl as qlabel : top = 175 : left = 13 : caption = "HOT groups" : end create
        create htDspLbl as qlabel : top = 198 : left = 20 : caption = "1" : end create
'        create htGrpLbl as qlabel : top = 175 : left = 50 : caption = "group" : end create
        create htGrpEdit as qedit 
            top = 195 : left = 40 : width = 30 : text = "1" 
            onchange = hotChange
        end create
        
        create imDLbl as qlabel : top = 175 : left = 303 : caption = "image groups" : end create
        create imCountLbl as qlabel : top = 198 : left = 240 : caption = "1" : end create
        create imDspLbl as qlabel : top = 198 : left = 320 : caption = "1" : end create
'        create htGrpLbl as qlabel : top = 175 : left = 50 : caption = "group" : end create
        create imGrpEdit as qedit 
            top = 195 : left = 340 : width = 30 : text = "" 
            onchange = imChange
        end create
        
        create hotSaveBtn as qbutton
            left = 110
            top = 185
            width = 100
            caption = "save HOT"
            onclick = hotSave 
        end create
    end create

    CREATE Panel16 AS QPanel 
        Top = 30 
        Left = 225 
        Width = 150 
        Height = 205 
        BevelInner = bvLowered 
        Visible = False 
        create PCKtoEDITbtn as qbutton
            left = 10
            top = 10
            width = 130
            caption = "PCK to editor BMPs"
            onclick = PCKtoEditBMPs
        end create
        create ebLbl1 as qlabel
            top = 40 : left = 20 : caption = " first group "
        end create
        create ebEditGrp1 as qedit
            top = 40 : left = 90 : width = 20 : text = ""
        end create
        create ebLbl2 as qlabel
            top = 70 : left = 20 : caption = "second group"
        end create
        create ebEditGrp2 as qedit
            top = 70 : left = 90 : width = 20 : text = ""
        end create
        create ebLbl3 as qlabel
            top = 100 : left = 20 : caption = " third group"
        end create
        create ebEditGrp3 as qedit
            top = 100 : left = 90 : width = 20 : text = ""
        end create
        create ebLbl4 as qlabel
            top = 130 : left = 20 : caption = "fourth group"
        end create
        create ebEditGrp4 as qedit
            top = 130 : left = 90 : width = 20 : text = ""
        end create
        create ebLbl5 as qlabel
            top = 160 : left = 10 : caption = "fourth group uses hot 1"
        end create
    end create
    
END CREATE 
    
'----------    

    
'    create glbBmpEdit as qedit
'        top = 200
'        left = 580
'        text = "JOSS"
'        onChange = glbOutFileChange
'        visible = false
'        enabled = false
'    end create
    
    create myCanvas as qcanvas
        Left = 5
        Top = 105
'        Top = 275
        Width = 780
        Height = 460 
'        Height = 300 
'        color = &HFFFFFF
        onMouseDown = canvasClick '(Button%, X%, Y%, Shift%)

    end create 
    
end create

dim pfStart(500) as long
dim pfEnd(500) as long

' use to load palette from existing units.COL and SOLD.PL
' loads at relative 0 - should go max 255 for unit palettes
dim colValue(1024) as word  
dim WINcolValue(1024) as long  
dim glbColMax as word
dim problemImages as integer

dim glbFileName$ as string
dim glbOutFile as string
dim glbBmpLocation as string
dim hotX(500) as integer
dim hotY(500) as integer
dim hotPtr as integer

dim WinPal(1500) as integer
dim WinPalCount(1500) as integer
dim WinPalPtr as integer

dim sustPal(1500) as word
dim sustPalCount(1500) as integer
dim sustPalPtr as integer

dim noChange as byte
dim doPalette as integer
dim basePath as string : dim z as integer
dim glbShadowTol as integer
dim glbColRed as integer
dim glbPose as integer
dim glbHotTot as integer
' display offsets
dim sXinc as integer : dim sYinc as integer 
    
dim wrkFile as string : dim outPath as string

    basePath = curdir$
    if fileExists("BMPtoPCK.exe") then
        for z = 1 to len(basePath) 
            if mid$(basePath,z,1) = " " then basePath = replace$(basePath,"*",z) 
        next z
    end if
    misclbl.caption = basePath

    glbOutFile = ltrim$(rtrim$(glbOutEdit.text))
    glbBmpLocation = ltrim$(rtrim$(glbBmpEdit.text))
    noChange = false
    doPalette = false
'    doPalette = true

    BMPsToPCKbtn.enabled = false
    goLbl.enabled = false
    glbOutEdit.enabled = false
    PCKtoBMPsbtn.enabled = false
    glbStartEdit.enabled = false
    glbEndEdit.enabled = false
    gsLbl.enabled = false
    gelbl.enabled = false
    glbShadowTolEdit.enabled = false
    gtsLbl.enabled = false
    gcLbl.enabled = false
    glbColRedEdit.enabled = false
    quitOnOverflow.enabled = false

    Form.ShowModal

' create test pattern pcq and col files ...
'    call writeTestPattern

    call changeDir

sub formResize
'    tab.Left = Form.ClientWidth - 420
    myCanvas.width = form.width - 20
    myCanvas.height = form.height - 150
end sub

sub saveText
    listbox1.saveToFile("list.txt")
end sub

sub canvasClick (Button%, X%, Y%, Shift%)
    if y% > 200 and y% < 260 then
        if x% > 20 and x% < 80 then
            glbPose = 0
            call rs2File (fileListBox1.filename)
            call rs2FilePose (fileListBox1.filename, 0)
        end if
        if x% > 90 and x% < 150 then
            glbPose = 1
            call rs2File (fileListBox1.filename)
            call rs2FilePose (fileListBox1.filename, 1)
        end if
        if x% > 160 and x% < 220 then
            glbPose = 2
            call rs2File (fileListBox1.filename)
            call rs2FilePose (fileListBox1.filename, 2)
        end if
        if x% > 230 and x% < 290 then
            glbPose = 3
            call rs2File (fileListBox1.filename)
            call rs2FilePose (fileListBox1.filename, 3)
        end if
        if x% > 300 and x% < 360 then
            glbPose = 4
            call rs2File (fileListBox1.filename)
            call rs2FilePose (fileListBox1.filename, 4)
        end if
        if x% > 370 and x% < 430 then
            glbPose = 5
            call rs2File (fileListBox1.filename)
            call rs2FilePose (fileListBox1.filename, 5)
        end if
        if x% > 440 and x% < 500 then
            glbPose = 6
            call rs2File (fileListBox1.filename)
            call rs2FilePose (fileListBox1.filename, 6)
        end if
        if x% > 510 and x% < 570 then
            glbPose = 7
            call rs2File (fileListBox1.filename)
            call rs2FilePose (fileListBox1.filename, 7)
        end if
        if x% > 580 and x% < 640 then
            glbPose = 8
            call rs2File (fileListBox1.filename)
            call rs2FilePose (fileListBox1.filename, 8)
        end if
        if x% > 650 and x% < 710 then
            glbPose = 9
            call rs2File (fileListBox1.filename)
            call rs2FilePose (fileListBox1.filename, 9)
        end if
    end if
    if y% > 300 and y% < 350 then
        if x% > 20 and x% < 80 then
            call rs2File (fileListBox1.filename)
            call rs2FilePose (fileListBox1.filename, glbPose)
            call rs2FileGroup (fileListBox1.filename, (glbPose * 8) + 1)
        end if
        if x% > 90 and x% < 150 then
            call rs2File (fileListBox1.filename)
            call rs2FilePose (fileListBox1.filename, glbPose)
            call rs2FileGroup (fileListBox1.filename, (glbPose * 8) + 2)
        end if
        if x% > 160 and x% < 220 then
            call rs2File (fileListBox1.filename)
            call rs2FilePose (fileListBox1.filename, glbPose)
            call rs2FileGroup (fileListBox1.filename, (glbPose * 8) + 3)
        end if
        if x% > 230 and x% < 290 then
            call rs2File (fileListBox1.filename)
            call rs2FilePose (fileListBox1.filename, glbPose)
            call rs2FileGroup (fileListBox1.filename, (glbPose * 8) + 4)
        end if
        if x% > 300 and x% < 360 then
            call rs2File (fileListBox1.filename)
            call rs2FilePose (fileListBox1.filename, glbPose)
            call rs2FileGroup (fileListBox1.filename, (glbPose * 8) + 5)
        end if
        if x% > 370 and x% < 430 then
            call rs2File (fileListBox1.filename)
            call rs2FilePose (fileListBox1.filename, glbPose)
            call rs2FileGroup (fileListBox1.filename, (glbPose * 8) + 6)
        end if
        if x% > 440 and x% < 500 then
            call rs2File (fileListBox1.filename)
            call rs2FilePose (fileListBox1.filename, glbPose)
            call rs2FileGroup (fileListBox1.filename, (glbPose * 8) + 7)
        end if
        if x% > 510 and x% < 570 then
            call rs2File (fileListBox1.filename)
            call rs2FilePose (fileListBox1.filename, glbPose)
            call rs2FileGroup (fileListBox1.filename, (glbPose * 8) + 8)
        end if
    end if
end sub

sub canvasRefresh
'    tab.Left = Form.ClientWidth - 420
end sub

SUB TabChange
  SELECT CASE Tab.TabIndex
    CASE 0
      Panel0.Visible = True
      Panel1.Visible = False
      tab.height = 250
      panel0.height = 205
    CASE 1
      Panel1.Visible = True
      Panel0.Visible = False
      tab.height = 250
      panel1.height = 205
  END SELECT
END SUB

SUB Tab1Change
  SELECT CASE Tab1.TabIndex
    CASE 0
      Panel10.Visible = True
      Panel11.Visible = False
      Panel12.Visible = False
      Panel13.Visible = False
      Panel14.Visible = False
      Panel15.Visible = False
      tab1.height = 250
      panel10.height = 205
      tab1.width = 410
      panel10.width = 400
    CASE 1
      Panel11.Visible = true
      Panel10.Visible = False
      Panel12.Visible = False
      Panel13.Visible = False
      Panel14.Visible = False
      Panel15.Visible = False
      Panel16.Visible = False
'      panel11.height = myImage.height + 20
'      panel11.width = myImage.width + 3
'      tab1.height = myImage.height + 45
'      tab1.width = myImage.width + 10
      panel11.height = myImage.height + 20
      panel11.width = myImage.width + 3
      bmpLbl.caption = str$(myImage.width) & " by " & str$(myImage.height)
      tab1.height = myImage.height + 45
      if myImage.width > 190 then
          tab1.width = myImage.width + 10
      else
          tab1.width = 200
      end if
    CASE 2
      Panel12.Visible = True
      Panel10.Visible = False
      Panel11.Visible = False
      Panel13.Visible = False
      Panel14.Visible = False
      Panel15.Visible = False
      Panel16.Visible = False
      tab1.height = 250
      panel12.height = 205
      tab1.width = 410
      panel12.width = 400
    CASE 3
      Panel13.Visible = True
      Panel10.Visible = False
      Panel11.Visible = False
      Panel12.Visible = False
      Panel14.Visible = False
      Panel15.Visible = False
      Panel16.Visible = False
      tab1.height = 250
      panel13.height = 205
      tab1.width = 410
      panel13.width = 400
    CASE 4
      Panel14.Visible = True
      Panel10.Visible = False
      Panel11.Visible = False
      Panel12.Visible = False
      Panel13.Visible = False
      Panel15.Visible = False
      Panel16.Visible = False
'      panel11.height = 320
'      panel11.width = 400
      tab1.height = 380
      tab1.width = 410
'      tab1.height = 250
'      panel14.height = 205
'      tab1.width = 410
'      panel14.width = 400
    CASE 5
      Panel15.Visible = True
      Panel10.Visible = False
      Panel11.Visible = False
      Panel12.Visible = False
      Panel13.Visible = False
      Panel14.Visible = False
      Panel16.Visible = False
      tab1.height = 250
      tab1.width = 410
      panel15.height = 220
      panel15.width = 400
    CASE 6
      Panel16.Visible = True
      Panel10.Visible = False
      Panel11.Visible = True
      Panel12.Visible = False
      Panel13.Visible = False
      Panel14.Visible = False
      Panel15.Visible = False
      tab1.height = 250
      tab1.width = 380
      panel16.height = 200
      panel16.width = 150
      panel11.height = 210
      panel11.width = 210
  END SELECT
END SUB

sub glbOutFileChange
    glbOutFile = ltrim$(rtrim$(glbOutEdit.edittext))
    glbBmpLocation = ltrim$(rtrim$(glbBmpEdit.edittext))
'    miscLbl.caption = glbBmpLocation & " " & glbOutFile
end sub

sub selectAny
    if showAny.checked = true then
        fileListBox1.mask = "*.*"
    elseif showPck.checked = true then
        fileListBox1.mask = "*.PCK"
    elseif showRs2.checked = true then
        fileListBox1.mask = "*.RS2"
    elseif showBmp.checked = true then
        fileListBox1.mask = "*.BMP"
    elseif showCOL.checked = true then
        fileListBox1.mask = "*.COL"
    elseif showANI.checked = true then
        fileListBox1.mask = "*.ANI"
    elseif showRES.checked = true then
        fileListBox1.mask = "*.RES"
    else
        fileListBox1.mask = "*.*"
    end if
    call changeDir
end sub

sub changeDirectory
    call changeDir
end sub

sub changeDir
'    form.textout (5, 300, str$(&H7fffffff), &H00007F, form.color)
    if noChange then exit sub
'    fileListBox1.mask = "*.PCK"
    fileListBox1.directory = dirTree.directory
    doevents
    fileListBox1.update
    doevents
    miscLbl.caption = curdir$
    if (ucase$(right$(dirTree.directory,7)) = "\UNITS\") or (ucase$(right$(dirTree.directory,7)) = "COMMON\") then
        BMPsToPCKbtn.enabled = false
        goLbl.enabled = false
        glbOutEdit.enabled = false
        PCKtoBMPsbtn.enabled = false
        listbox1.clear
' list all PCK files
'        listbox1.additems "zz"
'        d$ = dirTree.directory & "*.pck"
'        f$ = dir$(d$,0)
'        while f$ <> "" do
'            call countPckFile(f$)
'            f$ = dir$
'            doevents
'        wend
'        listbox1.insertItem(0, "images / file")
'        listbox1.delItems listbox1.itemcount -1
    elseif fileExists("P001.BMP") then
    ' assume it has valid bitmaps ...
        BMPsToPCKbtn.enabled = true
'        BMPsToPCKbtn.visible = true
        goLbl.enabled = true
        glbOutEdit.enabled = true
        PCKtoBMPsbtn.enabled = false
'        PCKtoBMPsbtn.visible = false
        glbStartEdit.enabled = true
        glbEndEdit.enabled = true
        gsLbl.enabled = true
        gelbl.enabled = true
        glbShadowTolEdit.enabled = true
        gtsLbl.enabled = true
        gcLbl.enabled = true
        glbColRedEdit.enabled = true
        glbShadowTol = val(glbShadowTolEdit.text)
        quitOnOverflow.enabled = true
    else
        BMPsToPCKbtn.enabled = false
        goLbl.enabled = false
        glbOutEdit.enabled = false
        PCKtoBMPsbtn.enabled = false
        glbStartEdit.enabled = false
        glbEndEdit.enabled = false
        gsLbl.enabled = false
        gelbl.enabled = false
        glbShadowTolEdit.enabled = false
        gtsLbl.enabled = false
        gcLbl.enabled = false
        glbColRedEdit.enabled = false
        quitOnOverflow.enabled = false
    end if
end sub
    
sub countPckFile (f$ as string)
    DIM File AS QFileStream
    dim ptrWork as long
    dim imStart as long
    dim imEnd as long
    dim wHshift as short
    dim wVshift as short
    dim wWidth as short
    dim wHeight as short
    dim wb as byte
    dim o$
    dim images as integer
     
    File.Open(f$, fmOpenRead)

    ptrWork = 0
    images = 1
    imEnd = 0
    while imEnd < file.size

   ' position to offset ...
         file.position = ptrWork
     
         file.read(imStart)
   ' save offset for next image
         ptrWork = file.position
         
   ' get start of next image for use as end of this image
         file.read(imEnd)
   ' image ...
            
         pfStart(images) = imStart
         pfEnd(images) = imEnd
         
'         call pckImage(f$, imStart, imEnd)
         
         images = images + 1
     wend
          
     file.close

     w$ = right$("   " & str$(images - 1),3) & " - " & ucase$(f$)
     
     for q! = 0 to listbox1.itemcount
         if w$ < listbox1.item(q!) then
             listbox1.insertitem(q!,w$)
             exit for
         end if
     next q!
          
end sub

sub selectList
    for a! = 0 to listbox1.itemCount - 1
        if listbox1.selected(a!) = 1 then 
            t$ = listbox1.item(a!) 
            li! = a!
        end if
    next a!
    f$ = mid$(t$, 7)
    glbFIleName$ = f$
    fc$ = left$(f$, len(f$) -3) & "col"
    fh$ = left$(f$, len(f$) -3) & "hot"
' debug
'    listbox1.additems f$ 
' debug
'    listbox1.additems fc$
    if ucase$(right$(f$,3)) = "PCK" then
        BMPsToPCKbtn.enabled = false
        goLbl.enabled = false
        glbOutEdit.enabled = false
        PCKtoBMPsbtn.enabled = true
        PCKtoBMPsbtn.visible = true
'        form.fillrect(0,0,800,600,form.color)
        form.repaint
        call colFile (fc$)
        call pckFile (f$)
        if fileexists (fh$) then call hotFile (fh$)
        if rtrim$(imGrpEdit.text) = "" then
        ' draw first image of each group
            call drawAll(f$)
            if fileexists (fh$) then call hotAll
        else
        ' draw all images of a group
            call drawGroup(f$, val(imGrpEdit.text))
            if fileexists (fh$) then call hotGroup
        end if
    else
    end if
end sub

sub selectFile
    f$ = fileListBox1.filename
    glbFileName$ = f$
    fc$ = left$(f$, len(f$) -3) & "col"
    fh$ = left$(f$, len(f$) -3) & "hot"
' debug
'    listbox1.additems f$ 
' debug
'    listbox1.additems fc$
    if ucase$(right$(fileListBox1.filename,3)) = "PCK" then
        BMPsToPCKbtn.enabled = false
        goLbl.enabled = false
        glbOutEdit.enabled = false
        PCKtoBMPsbtn.enabled = true
        form.fillrect(0,0,800,600,form.color)
        form.repaint
        call colFile (fc$)
        call pckFile (f$)
        if fileexists (fh$) then call hotFile (fh$)
        if fileexists (fh$) then call hotFill
        if rtrim$(imGrpEdit.text) = "" then
        ' draw first image of each group
            call drawAll(f$)
            if fileexists (fh$) then call hotAll
        else
        ' draw all images of a group
            call drawGroup(f$, val(imGrpEdit.text))
            if fileexists (fh$) then call hotGroup
        end if
    elseif ucase$(right$(fileListBox1.filename,3)) = "RS2" then
        BMPsToPCKbtn.enabled = false
        goLbl.enabled = false
        glbOutEdit.enabled = false
        PCKtoBMPsbtn.enabled = false
        call rs2File (fileListBox1.filename)
        glbPose = 0
        call rs2FilePose (fileListBox1.filename, 0)
    elseif ucase$(right$(fileListBox1.filename,3)) = "ANI" then
        listbox1.clear
        call colFile (fc$)
        call aniFile (fileListBox1.filename)
    elseif ucase$(right$(fileListBox1.filename,3)) = "RES" then
        fc$ = left$(fc$,len(fc$) - 3) & "PL"
        call colFile (fc$)
        call resFile (fileListBox1.filename)
    elseif ucase$(right$(fileListBox1.filename,3)) = "COL" then
        call colFile (fileListBox1.filename)
    elseif ucase$(right$(fileListBox1.filename,2)) = "PL" then
        call colFile (fileListBox1.filename)
    elseif ucase$(right$(fileListBox1.filename,3)) = "BMP" then
        BMPsToPCKbtn.enabled = false
        PCKtoBMPsbtn.enabled = false
        myImage.autosize = true
        myImage.bmp = fileListBox1.filename
        panel11.height = myImage.height + 20
        panel11.width = myImage.width + 3
        bmpLbl.caption = str$(myImage.width) & " by " & str$(myImage.height)
        if Tab1.TabIndex = 1 then
            tab1.height = myImage.height + 45
            if myImage.width > 190 then
                tab1.width = myImage.width + 10
            else
                tab1.width = 200
            end if
        end if
    else
        BMPsToPCKbtn.enabled = false
        goLbl.enabled = false
        glbOutEdit.enabled = false
        PCKtoBMPsbtn.enabled = false
    end if
end sub

sub scrollChange
    f$ = glbFileName$
    fc$ = left$(f$, len(f$) -3) & "col"
    fh$ = left$(f$, len(f$) -3) & "hot"
    if ucase$(right$(f$,3)) = "PCK" then
        form.repaint
        call colFile (fc$)
        call pckFile (f$)
        call hotFile (fh$)
        if rtrim$(imGrpEdit.text) = "" then
        ' draw first image of each group
            call drawAll(f$)
            call hotAll
        else
        ' draw all images of a group
            call drawGroup(f$, val(imGrpEdit.text))
            call hotGroup
        end if
    end if
end sub

dim images as integer

sub hotFile(f$ as string)
    DIM File AS QFileStream
    
    miscLbl.caption = f$ 
    File.Open(f$, fmOpenRead)

    hotPtr = 0
    while not file.eof
        hotPtr = hotPtr + 1
        file.read(hotX(hotPtr))
        file.read(hotY(hotPtr))
    wend
    
    file.close
'    call hotChange
    glbHotTot = str$(hotPtr / 32)
    htDspLbl.caption = str$(hotPtr / 32)
end sub

sub imChange
    f$ = glbFileName$
    fc$ = left$(f$, len(f$) -3) & "col"
    fh$ = left$(f$, len(f$) -3) & "hot"
        form.repaint
        call colFile (fc$)
        call pckFile (f$)
        call hotFile (fh$)
        if rtrim$(imGrpEdit.text) = "" then
        ' draw first image of each group
            call drawAll(f$)
            call hotAll
        else
        ' draw all images of a group
            call drawGroup(f$, val(imGrpEdit.text))
            call hotGroup
        end if
end sub

sub hotFill()
dim h as integer
    h = (val(htGrpEdit.text) - 1) * 32
    htx1edit.text = str$(hotX(h + 1))
    hty1edit.text = str$(hotY(h + 1))
    htx2edit.text = str$(hotX(h + 2))
    hty2edit.text = str$(hotY(h + 2))
    htx3edit.text = str$(hotX(h + 3))
    hty3edit.text = str$(hotY(h + 3))
    htx4edit.text = str$(hotX(h + 4))
    hty4edit.text = str$(hotY(h + 4))
    htx5edit.text = str$(hotX(h + 5))
    hty5edit.text = str$(hotY(h + 5))
    htx6edit.text = str$(hotX(h + 6))
    hty6edit.text = str$(hotY(h + 6))
    htx7edit.text = str$(hotX(h + 7))
    hty7edit.text = str$(hotY(h + 7))
    htx8edit.text = str$(hotX(h + 8))
    hty8edit.text = str$(hotY(h + 8))
    htx9edit.text = str$(hotX(h + 9))
    hty9edit.text = str$(hotY(h + 9))
    htx10edit.text = str$(hotX(h + 10))
    hty10edit.text = str$(hotY(h + 10))
    htx11edit.text = str$(hotX(h + 11))
    hty11edit.text = str$(hotY(h + 11))
    htx12edit.text = str$(hotX(h + 12))
    hty12edit.text = str$(hotY(h + 12))
    htx13edit.text = str$(hotX(h + 13))
    hty13edit.text = str$(hotY(h + 13))
    htx14edit.text = str$(hotX(h + 14))
    hty14edit.text = str$(hotY(h + 14))
    htx15edit.text = str$(hotX(h + 15))
    hty15edit.text = str$(hotY(h + 15))
    htx16edit.text = str$(hotX(h + 16))
    hty16edit.text = str$(hotY(h + 16))
    htx17edit.text = str$(hotX(h + 17))
    hty17edit.text = str$(hotY(h + 17))
    htx18edit.text = str$(hotX(h + 18))
    hty18edit.text = str$(hotY(h + 18))
    htx19edit.text = str$(hotX(h + 19))
    hty19edit.text = str$(hotY(h + 19))
    htx20edit.text = str$(hotX(h + 20))
    hty20edit.text = str$(hotY(h + 20))
    htx21edit.text = str$(hotX(h + 21))
    hty21edit.text = str$(hotY(h + 21))
    htx22edit.text = str$(hotX(h + 22))
    hty22edit.text = str$(hotY(h + 22))
    htx23edit.text = str$(hotX(h + 23))
    hty23edit.text = str$(hotY(h + 23))
    htx24edit.text = str$(hotX(h + 24))
    hty24edit.text = str$(hotY(h + 24))
    htx25edit.text = str$(hotX(h + 25))
    hty25edit.text = str$(hotY(h + 25))
    htx26edit.text = str$(hotX(h + 26))
    hty26edit.text = str$(hotY(h + 26))
    htx27edit.text = str$(hotX(h + 27))
    hty27edit.text = str$(hotY(h + 27))
    htx28edit.text = str$(hotX(h + 28))
    hty28edit.text = str$(hotY(h + 28))
    htx29edit.text = str$(hotX(h + 29))
    hty29edit.text = str$(hotY(h + 29))
    htx30edit.text = str$(hotX(h + 30))
    hty30edit.text = str$(hotY(h + 30))
    htx31edit.text = str$(hotX(h + 31))
    hty31edit.text = str$(hotY(h + 31))
    htx32edit.text = str$(hotX(h + 32))
    hty32edit.text = str$(hotY(h + 32))
end sub    
    
sub hotChange()
dim h as integer
    call hotFill
    h = (val(htGrpEdit.text) - 1) * 32
'    htx1edit.text = str$(hotX(h + 1))
'    hty1edit.text = str$(hotY(h + 1))
'    htx2edit.text = str$(hotX(h + 2))
'    hty2edit.text = str$(hotY(h + 2))
'    htx3edit.text = str$(hotX(h + 3))
'    hty3edit.text = str$(hotY(h + 3))
'    htx4edit.text = str$(hotX(h + 4))
'    hty4edit.text = str$(hotY(h + 4))
'    htx5edit.text = str$(hotX(h + 5))
'    hty5edit.text = str$(hotY(h + 5))
'    htx6edit.text = str$(hotX(h + 6))
'    hty6edit.text = str$(hotY(h + 6))
'    htx7edit.text = str$(hotX(h + 7))
'    hty7edit.text = str$(hotY(h + 7))
'    htx8edit.text = str$(hotX(h + 8))
'    hty8edit.text = str$(hotY(h + 8))
'    htx9edit.text = str$(hotX(h + 9))
'    hty9edit.text = str$(hotY(h + 9))
'    htx10edit.text = str$(hotX(h + 10))
'    hty10edit.text = str$(hotY(h + 10))
'    htx11edit.text = str$(hotX(h + 11))
'    hty11edit.text = str$(hotY(h + 11))
'    htx12edit.text = str$(hotX(h + 12))
'    hty12edit.text = str$(hotY(h + 12))
'    htx13edit.text = str$(hotX(h + 13))
'    hty13edit.text = str$(hotY(h + 13))
'    htx14edit.text = str$(hotX(h + 14))
'    hty14edit.text = str$(hotY(h + 14))
'    htx15edit.text = str$(hotX(h + 15))
'    hty15edit.text = str$(hotY(h + 15))
'    htx16edit.text = str$(hotX(h + 16))
'    hty16edit.text = str$(hotY(h + 16))
'    htx17edit.text = str$(hotX(h + 17))
'    hty17edit.text = str$(hotY(h + 17))
'    htx18edit.text = str$(hotX(h + 18))
'    hty18edit.text = str$(hotY(h + 18))
'    htx19edit.text = str$(hotX(h + 19))
'    hty19edit.text = str$(hotY(h + 19))
'    htx20edit.text = str$(hotX(h + 20))
'    hty20edit.text = str$(hotY(h + 20))
'    htx21edit.text = str$(hotX(h + 21))
'    hty21edit.text = str$(hotY(h + 21))
'    htx22edit.text = str$(hotX(h + 22))
'    hty22edit.text = str$(hotY(h + 22))
'    htx23edit.text = str$(hotX(h + 23))
'    hty23edit.text = str$(hotY(h + 23))
'    htx24edit.text = str$(hotX(h + 24))
'    hty24edit.text = str$(hotY(h + 24))
'    htx25edit.text = str$(hotX(h + 25))
'    hty25edit.text = str$(hotY(h + 25))
'    htx26edit.text = str$(hotX(h + 26))
'    hty26edit.text = str$(hotY(h + 26))
'    htx27edit.text = str$(hotX(h + 27))
'    hty27edit.text = str$(hotY(h + 27))
'    htx28edit.text = str$(hotX(h + 28))
'    hty28edit.text = str$(hotY(h + 28))
'    htx29edit.text = str$(hotX(h + 29))
'    hty29edit.text = str$(hotY(h + 29))
'    htx30edit.text = str$(hotX(h + 30))
'    hty30edit.text = str$(hotY(h + 30))
'    htx31edit.text = str$(hotX(h + 31))
'    hty31edit.text = str$(hotY(h + 31))
'    htx32edit.text = str$(hotX(h + 32))
'    hty32edit.text = str$(hotY(h + 32))
    if rtrim$(imGrpEdit.text) <> "" then
        f$ = glbFileName$
'        fc$ = left$(f$, len(f$) -3) & "col"
'        fh$ = left$(f$, len(f$) -3) & "hot"
        form.repaint
'        call colFile (fc$)
'        call pckFile (f$)
'        call hotFile (fh$)
       ' draw all images of a group
        call drawGroup(f$, val(imGrpEdit.text))
        call hotGroup
    end if
end sub

sub hotSave()
DIM File AS QFileStream
dim h as integer
    h = (val(htGrpEdit.text) - 1) * 32
    hotX(h + 1) = val(htx1edit.text)
    hotY(h + 1) = val(hty1edit.text)
    hotX(h + 2) = val(htx2edit.text)
    hotY(h + 2) = val(hty2edit.text)
    hotX(h + 3) = val(htx3edit.text)
    hotY(h + 3) = val(hty3edit.text)
    hotX(h + 4) = val(htx4edit.text)
    hotY(h + 4) = val(hty4edit.text)
    hotX(h + 5) = val(htx5edit.text)
    hotY(h + 5) = val(hty5edit.text)
    hotX(h + 6) = val(htx6edit.text)
    hotY(h + 6) = val(hty6edit.text)
    hotX(h + 7) = val(htx7edit.text)
    hotY(h + 7) = val(hty7edit.text)
    hotX(h + 8) = val(htx8edit.text)
    hotY(h + 8) = val(hty8edit.text)
    hotX(h + 9) = val(htx9edit.text)
    hotY(h + 9) = val(hty9edit.text)
    hotX(h + 10) = val(htx10edit.text)
    hotY(h + 10) = val(hty10edit.text)
    hotX(h + 11) = val(htx11edit.text)
    hotY(h + 11) = val(hty11edit.text)
    hotX(h + 12) = val(htx12edit.text)
    hotY(h + 12) = val(hty12edit.text)
    hotX(h + 13) = val(htx13edit.text)
    hotY(h + 13) = val(hty13edit.text)
    hotX(h + 14) = val(htx14edit.text)
    hotY(h + 14) = val(hty14edit.text)
    hotX(h + 15) = val(htx15edit.text)
    hotY(h + 15) = val(hty15edit.text)
    hotX(h + 16) = val(htx16edit.text)
    hotY(h + 16) = val(hty16edit.text)
    hotX(h + 17) = val(htx17edit.text)
    hotY(h + 17) = val(hty17edit.text)
    hotX(h + 18) = val(htx18edit.text)
    hotY(h + 18) = val(hty18edit.text)
    hotX(h + 19) = val(htx19edit.text)
    hotY(h + 19) = val(hty19edit.text)
    hotX(h + 20) = val(htx20edit.text)
    hotY(h + 20) = val(hty20edit.text)
    hotX(h + 21) = val(htx21edit.text)
    hotY(h + 21) = val(hty21edit.text)
    hotX(h + 22) = val(htx22edit.text)
    hotY(h + 22) = val(hty22edit.text)
    hotX(h + 23) = val(htx23edit.text)
    hotY(h + 23) = val(hty23edit.text)
    hotX(h + 24) = val(htx24edit.text)
    hotY(h + 24) = val(hty24edit.text)
    hotX(h + 25) = val(htx25edit.text)
    hotY(h + 25) = val(hty25edit.text)
    hotX(h + 26) = val(htx26edit.text)
    hotY(h + 26) = val(hty26edit.text)
    hotX(h + 27) = val(htx27edit.text)
    hotY(h + 27) = val(hty27edit.text)
    hotX(h + 28) = val(htx28edit.text)
    hotY(h + 28) = val(hty28edit.text)
    hotX(h + 29) = val(htx29edit.text)
    hotY(h + 29) = val(hty29edit.text)
    hotX(h + 30) = val(htx30edit.text)
    hotY(h + 30) = val(hty30edit.text)
    hotX(h + 31) = val(htx31edit.text)
    hotY(h + 31) = val(hty31edit.text)
    hotX(h + 32) = val(htx32edit.text)
    hotY(h + 32) = val(hty32edit.text)
    
    f$ = glbFileName$
    fh$ = left$(f$, len(f$) -3) & "hot"
    miscLbl.caption = fh$ 
    File.Open(fh$, fmCreate)

    for h = 1 to hotPtr
        file.write(hotX(h))
        file.write(hotY(h))
    next h
    
    file.close
    
    if rtrim$(imGrpEdit.text) <> "" then
        f$ = glbFileName$
        fc$ = left$(f$, len(f$) -3) & "col"
        fh$ = left$(f$, len(f$) -3) & "hot"
        form.repaint
        call colFile (fc$)
        call pckFile (f$)
        call hotFile (fh$)
        ' draw all images of a group
        call drawGroup(f$, val(imGrpEdit.text))
        call hotGroup
    end if
end sub

sub pckFile (f$ as string)
    DIM File AS QFileStream
    dim ptrWork as long
    dim imStart as long
    dim imEnd as long
    dim wHshift as short
    dim wVshift as short
    dim wWidth as short
    dim wHeight as short
    dim wb as byte
    dim o$
    
    miscLbl.caption = f$ 
    File.Open(f$, fmOpenRead)

    ptrWork = 0
    images = 1
    imEnd = 0
' PCK file format ...
'    starts with a series of addresses of data type integer / long - 4 bytes.
'    each address is the start of data for an image.
'    the last address is the size of the file.
    while imEnd < file.size

   ' position to offset ...
        file.position = ptrWork
     
        file.read(imStart)
   ' save offset for next image
        ptrWork = file.position
         
   ' get start of next image for use as end of this image
        file.read(imEnd)
   ' image ...
            
        pfStart(images) = imStart
        pfEnd(images) = imEnd
         
'        call pckImage(f$, imStart, imEnd)
         
        images = images + 1
    wend
          
    file.close
        
    c% = images / 32
    images = images - 1
    imCountLbl.caption = "(" & str$(images) & " images)" 
    imDspLbl.caption = str$(images / 32)
    
'    if rtrim$(imGrpEdit.text) = "" then
'        call drawAll(f$)
'    else
'        call drawGroup(f$, val(imGrpEdit.text))
'    end if
 
end sub

sub hotGroup
dim g as integer : dim l as integer
    g = ( val(htGrpEdit.text) - 1 ) * 32
'    gb = (grp - 1) * 32
    for l = 1 to 8
        myCanvas.line ((l * 90) + hotX(g + l) - 10, 200 + hotY(g + l) - 10, (l * 90) + hotX(g + l) + 10, 200 + hotY(g + l) + 10, &HFFFFFF)
        myCanvas.line ((l * 90) + hotX(g + l) + 10, 200 + hotY(g + l) - 10, (l * 90) + hotX(g + l) - 10, 200 + hotY(g + l) + 10, &HFFFFFF)
    next l
    for l = 9 to 16
        myCanvas.line (((l - 8) * 90) + hotX(g + l) - 10, 260 + hotY(g + l) - 10, ((l - 8) * 90) + hotX(g + l) + 10, 260 + hotY(g + l) + 10, &HFFFFFF)
        myCanvas.line (((l - 8) * 90) + hotX(g + l) + 10, 260 + hotY(g + l) - 10, ((l - 8) * 90) + hotX(g + l) - 10, 260 + hotY(g + l) + 10, &HFFFFFF)
    next l
    for l = 17 to 24
        myCanvas.line (((l - 16) * 90) + hotX(g + l) - 10, 320 + hotY(g + l) - 10, ((l - 16) * 90) + hotX(g + l) + 10, 320 + hotY(g + l) + 10, &HFFFFFF)
        myCanvas.line (((l - 16) * 90) + hotX(g + l) + 10, 320 + hotY(g + l) - 10, ((l - 16) * 90) + hotX(g + l) - 10, 320 + hotY(g + l) + 10, &HFFFFFF)
    next l
    for l = 25 to 32
        myCanvas.line (((l - 24) * 90) + hotX(g + l) - 10, 380 + hotY(g + l) - 10, ((l - 24) * 90) + hotX(g + l) + 10, 380 + hotY(g + l) + 10, &HFFFFFF)
        myCanvas.line (((l - 24) * 90) + hotX(g + l) + 10, 380 + hotY(g + l) - 10, ((l - 24) * 90) + hotX(g + l) - 10, 380 + hotY(g + l) + 10, &HFFFFFF)
    next l
end sub

sub hotAll 
    v% = imageNumber.position + 1
    if hotX(v%) > 1000 then
    myCanvas.textout (5, 220, "-- --", &H0000FF, form.color)
    else
    myCanvas.line (100 + (hotX(v%) * 2) - 10, 250 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) + 10, 250 + (hotY(v%) * 2) + 10, &H0000FF)
    myCanvas.line (100 + (hotX(v%) * 2) + 10, 250 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) - 10, 250 + (hotY(v%) * 2) + 10, &H0000FF)
    myCanvas.textout (5, 220, str$(hotX(v%)) & " " & str$(hotY(v%)), &H0000FF, myCanvas.color)
    end if
    
  if hotPtr > 32 then
    v% = imageNumber.position + 33
    if hotX(v%) > 1000 then
    myCanvas.textout (5, 240, "-- --", &HFFFFFF, form.color)
    else
    myCanvas.line (100 + (hotX(v%) * 2) - 10, 250 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) + 10, 250 + (hotY(v%) * 2) + 10, &HFFFFFF)
    myCanvas.line (100 + (hotX(v%) * 2) + 10, 250 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) - 10, 250 + (hotY(v%) * 2) + 10, &HFFFFFF)
    myCanvas.textout (5, 240, str$(hotX(v%)) & " " & str$(hotY(v%)), &HFFFFFF, myCanvas.color)
    end if
  end if
  if hotPtr > 64 then
    v% = imageNumber.position + 65
    if hotX(v%) > 1000 then
    myCanvas.textout (5, 260, "-- --", &HFF0000, form.color)
    else
    myCanvas.line (100 + (hotX(v%) * 2) - 10, 250 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) + 10, 250 + (hotY(v%) * 2) + 10, &HFF0000)
    myCanvas.line (100 + (hotX(v%) * 2) + 10, 250 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) - 10, 250 + (hotY(v%) * 2) + 10, &HFF0000)
    myCanvas.textout (5, 260, str$(hotX(v%)) & " " & str$(hotY(v%)), &HFF0000, myCanvas.color)
    end if
  end if
  if hotPtr > 96 then
    v% = imageNumber.position + 97
    if hotX(v%) > 1000 then
    myCanvas.textout (5, 280, "-- --", &H007F00, form.color)
    else
    myCanvas.line (100 + (hotX(v%) * 2) - 10, 250 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) + 10, 250 + (hotY(v%) * 2) + 10, &H007F00)
    myCanvas.line (100 + (hotX(v%) * 2) + 10, 250 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) - 10, 250 + (hotY(v%) * 2) + 10, &H007F00)
    myCanvas.textout (5, 280, str$(hotX(v%)) & " " & str$(hotY(v%)), &H007F00, myCanvas.color)
    end if
  end if
  if hotPtr > 128 then
    v% = imageNumber.position + 129
    if hotX(v%) > 1000 then
    myCanvas.textout (5, 300, "-- --", &H00007F, form.color)
    else
    myCanvas.line (100 + (hotX(v%) * 2) - 10, 250 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) + 10, 250 + (hotY(v%) * 2) + 10, &H00007F)
    myCanvas.line (100 + (hotX(v%) * 2) + 10, 250 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) - 10, 250 + (hotY(v%) * 2) + 10, &H00007F)
    myCanvas.textout (5, 300, str$(hotX(v%)) & " " & str$(hotY(v%)), &H00007F, myCanvas.color)
    end if
  end if
  if hotPtr > 160 then
    v% = imageNumber.position + 161
    if hotX(v%) > 1000 then
    myCanvas.textout (5, 320, "-- --", &H00007F, form.color)
    else
    myCanvas.line (100 + (hotX(v%) * 2) - 10, 250 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) + 10, 250 + (hotY(v%) * 2) + 10, &H00007F)
    myCanvas.line (100 + (hotX(v%) * 2) + 10, 250 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) - 10, 250 + (hotY(v%) * 2) + 10, &H00007F)
    myCanvas.textout (5, 320, str$(hotX(v%)) & " " & str$(hotY(v%)), &H00007F, myCanvas.color)
    end if
  end if
  if hotPtr > 192 then
    v% = imageNumber.position + 193
    if hotX(v%) > 1000 then
    myCanvas.textout (5, 340, "-- --", &H00007F, form.color)
    else
    myCanvas.line (100 + (hotX(v%) * 2) - 10, 250 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) + 10, 250 + (hotY(v%) * 2) + 10, &H00007F)
    myCanvas.line (100 + (hotX(v%) * 2) + 10, 250 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) - 10, 250 + (hotY(v%) * 2) + 10, &H00007F)
    myCanvas.textout (5, 340, str$(hotX(v%)) & " " & str$(hotY(v%)), &H00007F, myCanvas.color)
    end if
  end if
  if hotPtr > 224 then
    v% = imageNumber.position + 225
    if hotX(v%) > 1000 then
    myCanvas.textout (5, 360, "-- --", &H00007F, form.color)
    else
    myCanvas.line (100 + (hotX(v%) * 2) - 10, 250 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) + 10, 250 + (hotY(v%) * 2) + 10, &H00007F)
    myCanvas.line (100 + (hotX(v%) * 2) + 10, 250 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) - 10, 250 + (hotY(v%) * 2) + 10, &H00007F)
    myCanvas.textout (5, 360, str$(hotX(v%)) & " " & str$(hotY(v%)), &H00007F, myCanvas.color)
    end if
  end if
  if hotPtr > 256 then
    v% = imageNumber.position + 257
    if hotX(v%) > 1000 then
    myCanvas.textout (5, 380, "-- --", &H00007F, form.color)
    else
    myCanvas.line (100 + (hotX(v%) * 2) - 10, 250 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) + 10, 250 + (hotY(v%) * 2) + 10, &H00007F)
    myCanvas.line (100 + (hotX(v%) * 2) + 10, 250 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) - 10, 250 + (hotY(v%) * 2) + 10, &H00007F)
    myCanvas.textout (5, 380, str$(hotX(v%)) & " " & str$(hotY(v%)), &H00007F, myCanvas.color)
    end if
  end if
  
    if images = 288 then
        v% = imageNumber.position + 1
        myCanvas.line (100 + (hotX(v%) * 2) - 10, 360 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) + 10, 360 + (hotY(v%) * 2) + 10, &H0000FF)
        myCanvas.line (100 + (hotX(v%) * 2) + 10, 360 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) - 10, 360 + (hotY(v%) * 2) + 10, &H0000FF)
'        form.textout (5, 220, str$(hotX(v%)) & " " & str$(hotY(v%)), &H0000FF, form.color)

        if hotPtr > 32 then
            v% = imageNumber.position + 33
            myCanvas.line (100 + (hotX(v%) * 2) - 10, 360 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) + 10, 360 + (hotY(v%) * 2) + 10, &HFFFFFF)
            myCanvas.line (100 + (hotX(v%) * 2) + 10, 360 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) - 10, 360 + (hotY(v%) * 2) + 10, &HFFFFFF)
'            form.textout (5, 240, str$(hotX(v%)) & " " & str$(hotY(v%)), &HFFFFFF, form.color)
        end if
        if hotPtr > 64 then
            v% = imageNumber.position + 65
            myCanvas.line (100 + (hotX(v%) * 2) - 10, 360 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) + 10, 360 + (hotY(v%) * 2) + 10, &HFF0000)
            myCanvas.line (100 + (hotX(v%) * 2) + 10, 360 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) - 10, 360 + (hotY(v%) * 2) + 10, &HFF0000)
'            form.textout (5, 260, str$(hotX(v%)) & " " & str$(hotY(v%)), &HFF0000, form.color)
        end if
        if hotPtr > 96 then
            v% = imageNumber.position + 97
            myCanvas.line (100 + (hotX(v%) * 2) - 10, 360 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) + 10, 360 + (hotY(v%) * 2) + 10, &H007F00)
            myCanvas.line (100 + (hotX(v%) * 2) + 10, 360 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) - 10, 360 + (hotY(v%) * 2) + 10, &H007F00)
'            form.textout (5, 280, str$(hotX(v%)) & " " & str$(hotY(v%)), &H007F00, form.color)
        end if
        if hotPtr > 128 then
            v% = imageNumber.position + 129
            myCanvas.line (100 + (hotX(v%) * 2) - 10, 360 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) + 10, 360 + (hotY(v%) * 2) + 10, &H00007F)
            myCanvas.line (100 + (hotX(v%) * 2) + 10, 360 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) - 10, 360 + (hotY(v%) * 2) + 10, &H00007F)
'            form.textout (5, 300, str$(hotX(v%)) & " " & str$(hotY(v%)), &H00007F, form.color)
        end if
      if hotPtr > 160 then
        v% = imageNumber.position + 161
        myCanvas.line (100 + (hotX(v%) * 2) - 10, 360 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) + 10, 360 + (hotY(v%) * 2) + 10, &H00007F)
        myCanvas.line (100 + (hotX(v%) * 2) + 10, 360 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) - 10, 360 + (hotY(v%) * 2) + 10, &H00007F)
        myCanvas.textout (5, 320, str$(hotX(v%)) & " " & str$(hotY(v%)), &H00007F, myCanvas.color)
      end if
      if hotPtr > 192 then
        v% = imageNumber.position + 193
        myCanvas.line (100 + (hotX(v%) * 2) - 10, 360 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) + 10, 360 + (hotY(v%) * 2) + 10, &H00007F)
        myCanvas.line (100 + (hotX(v%) * 2) + 10, 360 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) - 10, 360 + (hotY(v%) * 2) + 10, &H00007F)
        myCanvas.textout (5, 340, str$(hotX(v%)) & " " & str$(hotY(v%)), &H00007F, myCanvas.color)
      end if
      if hotPtr > 224 then
        v% = imageNumber.position + 225
        myCanvas.line (100 + (hotX(v%) * 2) - 10, 360 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) + 10, 360 + (hotY(v%) * 2) + 10, &H00007F)
        myCanvas.line (100 + (hotX(v%) * 2) + 10, 360 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) - 10, 360 + (hotY(v%) * 2) + 10, &H00007F)
        myCanvas.textout (5, 360, str$(hotX(v%)) & " " & str$(hotY(v%)), &H00007F, myCanvas.color)
      end if
      if hotPtr > 256 then
        v% = imageNumber.position + 257
        myCanvas.line (100 + (hotX(v%) * 2) - 10, 360 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) + 10, 360 + (hotY(v%) * 2) + 10, &H00007F)
        myCanvas.line (100 + (hotX(v%) * 2) + 10, 360 + (hotY(v%) * 2) - 10, 100 + (hotX(v%) * 2) - 10, 360 + (hotY(v%) * 2) + 10, &H00007F)
        myCanvas.textout (5, 380, str$(hotX(v%)) & " " & str$(hotY(v%)), &H00007F, myCanvas.color)
      end if
    end if
    
'    call drawMyImage(f$, hotX(1 + v%), pfEnd(1 + v%), 100, 100, 2, 1 + v%)
'    
'    call drawImage(f$, pfStart(1 + v%), pfEnd(1 + v%), 100, 250, 2, 1 + v%)
'    if images > 63 then call drawImage(f$, pfStart(33 + v%), pfEnd(33 + v%), 220, 250, 2, 33 + v%)
'    if images > 95 then call drawImage(f$, pfStart(65 + v%), pfEnd(65 + v%), 340, 250, 2, 65 + v%)
'    
'    if images > 127 then call drawImage(f$, pfStart(97 + v%), pfEnd(97 + v%), 460, 250, 2, 97 + v%)
'    if images > 159 then call drawImage(f$, pfStart(129 + v%), pfEnd(129 + v%), 580, 250, 2, 129 + v%)
'    if images > 191 then call drawImage(f$, pfStart(161 + v%), pfEnd(161 + v%), 700, 250, 2, 161 + v%)
'    
'    if images > 223 then call drawImage(f$, pfStart(193 + v%), pfEnd(193 + v%), 100, 360, 2, 193 + v%)
'    if images > 255 then call drawImage(f$, pfStart(225 + v%), pfEnd(225 + v%), 220, 360, 2, 225 + v%)
'    if images > 287 then call drawImage(f$, pfStart(257 + v%), pfEnd(257 + v%), 340, 360, 2, 257 + v%)
'          
'    if images > 319 then call drawImage(f$, pfStart(289 + v%), pfEnd(289 + v%), 460, 360, 2, 289 + v%)
'    if images > 351 then call drawImage(f$, pfStart(321 + v%), pfEnd(321 + v%), 580, 360, 2, 321 + v%)
'    if images > 383 then call drawImage(f$, pfStart(353 + v%), pfEnd(353 + v%), 700, 360, 2, 353 + v%)
'          
'    if images > 415 then call drawImage(f$, pfStart(385 + v%), pfEnd(385 + v%), 100, 470, 2, 385 + v%)
'    if images > 447 then call drawImage(f$, pfStart(417 + v%), pfEnd(417 + v%), 220, 470, 2, 417 + v%)
'    if images > 479 then call drawImage(f$, pfStart(449 + v%), pfEnd(449 + v%), 340, 470, 2, 449 + v%)
          
end sub
    
sub drawGroup (f$, grp as integer)
dim g as integer : dim l as integer
    g = (grp - 1) * 32
'    gb = (grp - 1) * 32
    for l = 1 to 8
        call drawImage(f$, pfStart(g + l), pfEnd(g + l), l * 90, 200, 1, g + l)
    next l
    for l = 9 to 16
        call drawImage(f$, pfStart(g + l), pfEnd(g + l), (l - 8) * 90, 260, 1, g + l)
    next l
    for l = 17 to 24
        call drawImage(f$, pfStart(g + l), pfEnd(g + l), (l - 16) * 90, 320, 1, g + l)
    next l
    for l = 25 to 32
        call drawImage(f$, pfStart(g + l), pfEnd(g + l), (l - 24) * 90, 380, 1, g + l)
    next l
end sub

sub drawAll (f$)    
    v% = imageNumber.position
    
    call drawMyImage(f$, pfStart(1 + v%), pfEnd(1 + v%), 100, 100, 2, 1 + v%)
    
    call drawImage(f$, pfStart(1 + v%), pfEnd(1 + v%), 100, 250, 2, 1 + v%)
    if images > 63 then call drawImage(f$, pfStart(33 + v%), pfEnd(33 + v%), 220, 250, 2, 33 + v%)
    if images > 95 then call drawImage(f$, pfStart(65 + v%), pfEnd(65 + v%), 340, 250, 2, 65 + v%)
    
    if images > 127 then call drawImage(f$, pfStart(97 + v%), pfEnd(97 + v%), 460, 250, 2, 97 + v%)
    if images > 159 then call drawImage(f$, pfStart(129 + v%), pfEnd(129 + v%), 580, 250, 2, 129 + v%)
    if images > 191 then call drawImage(f$, pfStart(161 + v%), pfEnd(161 + v%), 700, 250, 2, 161 + v%)
    
    if images > 223 then call drawImage(f$, pfStart(193 + v%), pfEnd(193 + v%), 100, 360, 2, 193 + v%)
    if images > 255 then call drawImage(f$, pfStart(225 + v%), pfEnd(225 + v%), 220, 360, 2, 225 + v%)
    if images > 287 then call drawImage(f$, pfStart(257 + v%), pfEnd(257 + v%), 340, 360, 2, 257 + v%)
          
    if images > 319 then call drawImage(f$, pfStart(289 + v%), pfEnd(289 + v%), 460, 360, 2, 289 + v%)
    if images > 351 then call drawImage(f$, pfStart(321 + v%), pfEnd(321 + v%), 580, 360, 2, 321 + v%)
    if images > 383 then call drawImage(f$, pfStart(353 + v%), pfEnd(353 + v%), 700, 360, 2, 353 + v%)
          
    if images > 415 then call drawImage(f$, pfStart(385 + v%), pfEnd(385 + v%), 100, 470, 2, 385 + v%)
    if images > 447 then call drawImage(f$, pfStart(417 + v%), pfEnd(417 + v%), 220, 470, 2, 417 + v%)
    if images > 479 then call drawImage(f$, pfStart(449 + v%), pfEnd(449 + v%), 340, 470, 2, 449 + v%)
          
end sub

sub drawImage(f$ as string, startOfImage as long, endOfImage as long, xp as word, yp as word, s as word, i as word)
'
' draws on canvas - bottom part of form ...
' uses palette stored in WINcolValue()
'
    DIM File AS QFileStream
    dim wHshift as short
    dim wVshift as short
    dim wWidth as short
    dim wHeight as short
    dim ptrWork as long
    dim wb as byte
    dim iLength as word
    dim iData as byte
    dim iPixel as byte
    dim a as word
    dim b as word
    dim formXoffset as word
    dim formYoffset as word
    dim pixSize as word
    dim x as integer : dim y as integer
    dim leastX as integer : dim mostX as integer : dim leastY as integer : dim mostY as integer
    
    File.Open(f$, fmOpenRead)
    file.position = startOfImage     

    if ucase$(right$(f$,9)) = "CLIFF.RES" then
    ' extra stuff ...
        file.read(ptrWork)
        file.read(ptrWork)
    end if

    if ucase$(right$(f$,9)) = "ZABOR.RES" then
    ' extra stuff ...
        file.read(ptrWork)
    end if

'    if ucase$(right$(f$,8)) = "TREE.RES" then
'    ' extra stuff ...
'        file.read(ptrWork)
'        file.read(ptrWork)
'        file.read(ptrWork)
'        file.read(ptrWork)
'        file.read(ptrWork)
'        file.read(ptrWork)
'        file.read(ptrWork)
'        file.read(wb)
'    end if

    if ucase$(right$(f$,9)) = "STAND.RES" then
    ' extra stuff ...
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(wb)
    end if

    if ucase$(right$(f$,7)) = "LAY.RES" then
    ' extra stuff ...
        file.read(ptrWork)
        file.read(wb)
    end if

    file.read(wHshift)
    file.read(wVshift)
    file.read(wWidth)
    file.read(wHeight)
    file.read(wb)

    if ucase$(right$(f$,8)) = "ICON.RES" then
        sXinc = (wWidth * s) + 8
        sYinc = (wHeight * s) + 8
    end if
    
    formXoffset = xp
    formYoffset = yp
    pixSize = s
'    pixSize = pixelSize.position
        
    y = 1
' work on image line
    leastX = 500
    mostX = -500
    leastY = 500
    mostY = -500
'    form.line (xp - 50, yp, xp + 50, yp, &HFF00FF)
'    form.line (xp, yp - 50, xp, yp + 50, &HFF00FF)
    ptrWork = file.position
    while ptrWork < endOfImage
        h$ = ""
        x = 1
        file.read(iLength)
        ptrWork = file.position + iLength 
        l$ = ""
        
        if ptrWork > endOfImage then
            listbox1.additems " bad stuff "
            listbox1.additems h$
            listbox1.additems "    ------------    "
            listbox1.additems ""
            problemImages = problemImages + 1
            exit sub
        end if
        
        ' iLength has number of bytes in line
        ' positioned at line data
        while file.position < ptrWork
            ' image data
'    listbox1.additems " pixel data "
            file.read(iData)
            
' PCK and RS2 normal images ...            
            if (wb = 162) then    
                if iData > &HC0 then
                ' empty pixels for iData - 192
'                    x = x + iData - 192
' testing ...  
                    for b = 1 to iData - 192
                        l$ = l$ & "_"
'            if showTransparent.checked then form.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&Hffffff)
            if showTransparent.checked then myCanvas.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&Hffffff)
                        x = x + 1
                    next b
                elseif iData > &H80 then
                ' repeated data for iData - 128
                    file.read(iPixel)
                    for b = 1 to iData - 128
                        l$ = l$ & "R"
        
                        if showCompression.checked = true then
'                            form.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&HFF0000)
                            myCanvas.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&HFF0000)
                        else
'                            form.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),WINcolValue(iPixel))
                            myCanvas.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),WINcolValue(iPixel))
'                            if glbDisplayAsWhite = iPixel then
                            if (iPixel = glbPaletteSelection) or (iPixel >= glbPaletteSelection and iPixel <= glbPaletteEndSelection) then
                            ' display the selected color as white
'                                if showSelectionAsWhite.checked then form.rectangle (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&HFFFFFF)
                                if showSelectionAsWhite.checked then myCanvas.rectangle (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&HFFFFFF)
                            end if
                        end if
                        if (x + wHshift) < leastX then leastX = (x + wHshift)
                        if (x + wHshift) > mostX then mostX = (x + wHshift)
                        if (y + wVshift) < leastY then leastY = (y + wVshift)
                        if (y + wVshift) > mostY then mostY = (y + wVshift)
                        x = x + 1
                    next b
                elseif iData > &H40 then
                ' soft drawn
                    for b = 1 to iData - 64
                        file.read(iPixel)
                        l$ = l$ & "S"
                        if showCompression.checked = true then
'                            form.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&H0000FF)
                            myCanvas.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&H0000FF)
                        else
'                            form.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),WINcolValue(iPixel))
                            myCanvas.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),WINcolValue(iPixel))
'                            if glbDisplayAsWhite = iPixel then
                            if (iPixel = glbPaletteSelection) or (iPixel >= glbPaletteSelection and iPixel <= glbPaletteEndSelection) then
                            ' display the selected color as white
'                                if showSelectionAsWhite.checked then form.rectangle (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&HFFFFFF)
                                if showSelectionAsWhite.checked then myCanvas.rectangle (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&HFFFFFF)
                            end if
                        end if
                        if (x + wHshift) < leastX then leastX = (x + wHshift)
                        if (x + wHshift) > mostX then mostX = (x + wHshift)
                        if (y + wVshift) < leastY then leastY = (y + wVshift)
                        if (y + wVshift) > mostY then mostY = (y + wVshift)
                        x = x + 1
                    next b
                else
                ' normal
                    for b = 1 to iData 
                        file.read(iPixel)
                        l$ = l$ & "N"
                        if showCompression.checked = true then
'                            form.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&H00FF00)
                            myCanvas.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&H00FF00)
                        else
'                            form.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),WINcolValue(iPixel))
                            myCanvas.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),WINcolValue(iPixel))
'                            if glbDisplayAsWhite = iPixel then
                            if (iPixel = glbPaletteSelection) or (iPixel >= glbPaletteSelection and iPixel <= glbPaletteEndSelection) then
                            ' display the selected color as white
'                                if showSelectionAsWhite.checked then form.rectangle (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&HFFFFFF)
                                if showSelectionAsWhite.checked then myCanvas.rectangle (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&HFFFFFF)
                            else
                            end if
                        end if
                        if (x + wHshift) < leastX then leastX = (x + wHshift)
                        if (x + wHshift) > mostX then mostX = (x + wHshift)
                        if (y + wVshift) < leastY then leastY = (y + wVshift)
                        if (y + wVshift) > mostY then mostY = (y + wVshift)
                        x = x + 1
                    next b
                end if
                
                
' animation images ...                
            elseif (wb = 169) then    
                if iData > &H80 then
' try h80+ as transparent ...                
                    for b = 1 to iData - 128
                        l$ = l$ & "_"
'            if showTransparent.checked then form.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&Hffffff)
            if showTransparent.checked then myCanvas.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&Hffffff)
                        x = x + 1
                    next b
' i don't know why yet ...                    
                    file.read(iPixel)

                        if showCompression.checked = true then
'                            form.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&H00FF00)
                            myCanvas.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&H00FF00)
                        else
'                            form.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),WINcolValue(iPixel))
                            myCanvas.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),WINcolValue(iPixel))
'                            if glbDisplayAsWhite = iPixel then
                            if (iPixel = glbPaletteSelection) or (iPixel >= glbPaletteSelection and iPixel <= glbPaletteEndSelection) then
                            ' display the selected color as white
'                                if showSelectionAsWhite.checked then form.rectangle (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&HFFFFFF)
                                if showSelectionAsWhite.checked then myCanvas.rectangle (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&HFFFFFF)
                            else
                            end if
                        end if
                        
                else
                ' normal
                    for b = 1 to iData 
                        file.read(iPixel)
                        l$ = l$ & "N"
                        if showCompression.checked = true then
'                            form.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&H00FF00)
                            myCanvas.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&H00FF00)
                        else
'                            form.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),WINcolValue(iPixel))
                            myCanvas.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),WINcolValue(iPixel))
'                            if glbDisplayAsWhite = iPixel then
                            if (iPixel = glbPaletteSelection) or (iPixel >= glbPaletteSelection and iPixel <= glbPaletteEndSelection) then
                            ' display the selected color as white
'                                if showSelectionAsWhite.checked then form.rectangle (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&HFFFFFF)
                                if showSelectionAsWhite.checked then myCanvas.rectangle (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&HFFFFFF)
                            else
                            end if
                        end if
                        if (x + wHshift) < leastX then leastX = (x + wHshift)
                        if (x + wHshift) > mostX then mostX = (x + wHshift)
                        if (y + wVshift) < leastY then leastY = (y + wVshift)
                        if (y + wVshift) > mostY then mostY = (y + wVshift)
                        x = x + 1
                    next b
                end if

' icon images ...                
            elseif (wb = 161) then    
                if iData > &H80 then
' try h80+ as repeated ...                
                    file.read(iPixel)
                    for b = 1 to iData - 128
                        l$ = l$ & "R"
        
                        if showCompression.checked = true then
'                            form.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&HFF0000)
                            myCanvas.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&HFF0000)
                        else
'                            form.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),WINcolValue(iPixel))
                            myCanvas.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),WINcolValue(iPixel))
'                            if glbDisplayAsWhite = iPixel then
                            if (iPixel = glbPaletteSelection) or (iPixel >= glbPaletteSelection and iPixel <= glbPaletteEndSelection) then
                            ' display the selected color as white
'                                if showSelectionAsWhite.checked then form.rectangle (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&HFFFFFF)
                                if showSelectionAsWhite.checked then myCanvas.rectangle (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&HFFFFFF)
                            end if
                        end if
                        if (x + wHshift) < leastX then leastX = (x + wHshift)
                        if (x + wHshift) > mostX then mostX = (x + wHshift)
                        if (y + wVshift) < leastY then leastY = (y + wVshift)
                        if (y + wVshift) > mostY then mostY = (y + wVshift)
                        x = x + 1
                    next b
                else
                ' normal
                    for b = 1 to iData 
                        file.read(iPixel)
                        l$ = l$ & "N"
                        if showCompression.checked = true then
'                            form.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&H00FF00)
                            myCanvas.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&H00FF00)
                        else
'                            form.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),WINcolValue(iPixel))
                            myCanvas.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),WINcolValue(iPixel))
'                            if glbDisplayAsWhite = iPixel then
                            if (iPixel = glbPaletteSelection) or (iPixel >= glbPaletteSelection and iPixel <= glbPaletteEndSelection) then
                            ' display the selected color as white
'                                if showSelectionAsWhite.checked then form.rectangle (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&HFFFFFF)
                                if showSelectionAsWhite.checked then myCanvas.rectangle (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&HFFFFFF)
                            else
                            end if
                        end if
                        if (x + wHshift) < leastX then leastX = (x + wHshift)
                        if (x + wHshift) > mostX then mostX = (x + wHshift)
                        if (y + wVshift) < leastY then leastY = (y + wVshift)
                        if (y + wVshift) > mostY then mostY = (y + wVshift)
                        x = x + 1
                    next b
                end if

            else
' 163 type
' shadow ...
                if iData > &H80 then
                ' empty pixels for iData - 192
'                    x = x + iData - 128
                    for b = 1 to iData - 128
                        l$ = l$ & "_"
'            if showTransparent.checked then form.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&Hffffff)
            if showTransparent.checked then myCanvas.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&Hffffff)
                        x = x + 1
                    next b
                else
                ' normal - draw shadow
                    for b = 1 to iData 
                        l$ = l$ & "N"
'                        form.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&H808080)
                        myCanvas.fillrect (formXoffset + ((x + wHshift) * pixSize),formYoffset + ((y  + wVshift) * pixSize),formXoffset + pixSize + ((x + wHshift) * pixSize),formYoffset + pixSize + ((y  + wVshift) * pixSize),&H808080)
                        if (x + wHshift) < leastX then leastX = (x + wHshift)
                        if (x + wHshift) > mostX then mostX = (x + wHshift)
                        if (y + wVshift) < leastY then leastY = (y + wVshift)
                        if (y + wVshift) > mostY then mostY = (y + wVshift)
                        x = x + 1
                    next b
                end if
            
            end if
        wend    
    y = y + 1
    wend
    file.close
    if showGrid.checked = true and startOfImage <> endOfImage then
    ' image center
        myCanvas.line (xp - 48, yp + 2, xp + 48, yp + 2, &HFF00FF)
        myCanvas.line (xp + 2, yp - 48, xp + 2, yp + 48, &HFF00FF)
    ' image limits
        myCanvas.rectangle (formXoffset + (leastX * pixSize),formYoffset + (leastY * pixSize),formXoffset + pixSize + (mostX * pixSize),formYoffset + pixSize + (mostY * pixSize),&H00FFFF)
    ' image offset
        myCanvas.textout (xp + (wHshift * pixSize) + 5,  yp + (wVshift * pixSize) - 15, str$(wHshift) & " " & str$(wVshift), &HFF0000, myCanvas.color)
        myCanvas.line (xp + (wHshift * pixSize) - 20, yp + (wVshift * pixSize), xp + (wHshift * pixSize) + 20, yp + (wVshift * pixSize), &HFFFF00)
        myCanvas.line (xp + (wHshift * pixSize), yp + (wVshift * pixSize) - 20, xp + (wHshift * pixSize), yp + (wVshift * pixSize) + 20, &HFFFF00)
    ' image number
        myCanvas.textout (xp + (wHshift * pixSize) - 15,  yp + (wVshift * pixSize) - 15, right$("   " & str$(i),3), &H0000FF, myCanvas.color)
    ' explain text
        myCanvas.textout (10, 420, "image number", &H0000FF, myCanvas.color)
        myCanvas.textout (120, 420, "x, y offsets", &HFF0000, myCanvas.color)
        myCanvas.textout (200, 420, "image limits", &H00FFFF, myCanvas.color)
        myCanvas.textout (300, 420, "image center", &HFF00FF, myCanvas.color)
    end if
end sub

sub drawMyImage(f$ as string, startOfImage as long, endOfImage as long, xp as word, yp as word, s as word, i as word)
'
' draws on bitmap
' uses palette stored in WINcolValue()
'
    DIM File AS QFileStream
    dim wHshift as short
    dim wVshift as short
    dim wWidth as short
    dim wHeight as short
    dim ptrWork as long
    dim wb as byte
    dim iLength as word
    dim iData as byte
    dim iPixel as byte
    dim a as word
    dim b as word
    dim formXoffset as word
    dim formYoffset as word
'    dim pixSize as word
    
    File.Open(f$, fmOpenRead)
    file.position = startOfImage     

    file.read(wHshift)
    file.read(wVshift)
    file.read(wWidth)
    file.read(wHeight)
    file.read(wb)

    formXoffset = xp
    formYoffset = yp
'    pixSize = s
    if s <> 0 then
        if (wb = 162) then 
            myImage.bmphandle = magentaBMP
        else
            myImage.bmphandle = cyanBMP
        end if
    end if
'    myImage.fillrect (0,0,200,200,&HFFFF00)
    y = 1
' work on image line
    ptrWork = file.position
    while ptrWork < endOfImage
        x = 1
        file.read(iLength)
        ptrWork = file.position + iLength 
        
        if ptrWork > endOfImage then
            listbox1.additems " bad stuff "
            listbox1.additems "    ------------    "
            listbox1.additems ""
            exit sub
        end if
        
        ' iLength has number of bytes in line
        ' positioned at line data
        while file.position < ptrWork
            ' image data
'    listbox1.additems " pixel data "
            file.read(iData)
            
            if wb = 162 then    
            
                if iData > &HC0 then
        ' empty pixels for iData - 192
                    x = x + iData - 192
                elseif iData > &H80 then
        ' repeated data for iData - 128
                    file.read(iPixel)
                    if doPalette then addToSustPal (WINcolValue(iPixel))
                    for b = 1 to iData - 128
                        myImage.pSet (formXoffset + x + wHshift,formYoffset + y  + wVshift,WINcolValue(iPixel))
                        x = x + 1
                    next b
                elseif iData > &H40 then
        ' soft drawn
                    for b = 1 to iData - 64
                        file.read(iPixel)
                        myImage.pSet (formXoffset + x + wHshift,formYoffset + y  + wVshift,WINcolValue(iPixel))
                        if doPalette then addToSustPal (WINcolValue(iPixel))
                        x = x + 1
                    next b
                else
        ' normal
                    for b = 1 to iData 
                        file.read(iPixel)
                        myImage.pSet (formXoffset + x + wHshift,formYoffset + y  + wVshift,WINcolValue(iPixel))
                        if doPalette then addToSustPal (WINcolValue(iPixel))
                        x = x + 1
                    next b
                end if

' icon images ...                
            elseif (wb = 161) then    
                if iData > &H80 then
' try h80+ as repeated ...                
        ' repeated data for iData - 128
                    file.read(iPixel)
                    if doPalette then addToSustPal (WINcolValue(iPixel))
                    for b = 1 to iData - 128
                        myImage.pSet (formXoffset + x + wHshift,formYoffset + y  + wVshift,WINcolValue(iPixel))
                        x = x + 1
                    next b
                else
                ' normal
                    for b = 1 to iData 
                        file.read(iPixel)
                        myImage.pSet (formXoffset + x + wHshift,formYoffset + y  + wVshift,WINcolValue(iPixel))
                        if doPalette then addToSustPal (WINcolValue(iPixel))
                        x = x + 1
                    next b
                end if

            else
' 163 type
                if iData > &H80 then
        ' empty pixels for iData - 128
                    x = x + iData - 128
                else
        ' normal - draw shadow
                    for b = 1 to iData 
                        myImage.pSet (formXoffset + x + wHshift,formYoffset + y  + wVshift,&H808080)
                        x = x + 1
                    next b
                end if
                                
            end if
            
        wend    
    y = y + 1
    wend
    file.close
end sub

sub PCKtoBMPs
dim dest as qrect
dim src as qrect
dim bm as qbitmap 
dim c as integer : dim bmpPath as string

    glbBmpLocation = left$(glbFileName$, len(glbFileName$) - 4)
  
    bmpPath = glbBmpLocation
'    mkdir glbBmpLocation
    mkdir bmpPath
    miscLbl.caption = bmpPath
    noChange = true
    dirTree.reload
    noChange = false
    doPalette  = true
    doevents
    
'    exit sub
    
    bm.height = 200 : bm.width = 200 : bm.bmphandle = magentaBMP
    with dest
        .top = 1
        .left = 1
        .right = 200
        .bottom = 200
    end with
    with src
        .top = 1
        .left = 1
        .right = 200
        .bottom = 200
    end with
    WinPalPtr = 0
    sustPalPtr = 0
'    f$ = fileListBox1.filename
    f$ = glbFileName$
    fc$ = left$(f$, len(f$) -3) & "col"
    if ucase$(right$(f$,3)) = "PCK" then
        call colFile (fc$)
        call pckFile (f$)
        for a% = 1 to images
            call drawMyImage(f$, pfStart(a%), pfEnd(a%), 100, 100, 2, a%)

'            bf$ = "P" & right$("000" & str$(a%),3) & ".bmp"
            bf$ = bmpPath & "\P" & right$("000" & str$(a%),3) & ".bmp"

            miscLbl.caption = bf$
            bm.copyrect(dest, myImage, src)
            bm.saveToFile(bf$)
            doevents
        next a%
    end if
    
    myImage.fillrect (0, 0, 200, 200, &HFFFFFF)
    myImage.fillrect (180, 20, 190, 40, &HFF00FF)
    myImage.fillrect (180, 60, 190, 80, &HFFFF00)
    for c = 1 to sustPalPtr
'        listbox1.additems right$("     " & str$(c), 5) & " " & WinPal(c) & " " & right$("     " & str$(WInPalCount(c)), 5)
        py = (c - 1) \ 16
        px = (c - 1 ) - (py * 16)
        currentColor = sustToWin(sustPal(c))
        myImage.fillrect ((px * 10) + 10, (py * 10) + 10, (px * 10) + 20, (py * 10) + 20,currentColor)
    next c
    for c = sustPalPtr to 256
'        listbox1.additems right$("     " & str$(c), 5) & " " & WinPal(c) & " " & right$("     " & str$(WInPalCount(c)), 5)
        py = (c - 1) \ 16
        px = (c - 1 ) - (py * 16)
        if ((px + py) mod 2) = 1 then currentColor = &HFF00FF else currentColor = &HFFFF00
        myImage.fillrect ((px * 10) + 10, (py * 10) + 10, (px * 10) + 20, (py * 10) + 20,currentColor)
    next c
    
    with dest
        .top = 0
        .left = 0
        .right = 200
        .bottom = 200
    end with
    with src
        .top = 0
        .left = 0
        .right = 200
        .bottom = 200
    end with
    
    bm.copyrect(dest, myImage, src)
    bf$ = bmpPath & "\P000.bmp"
    bm.saveToFile(bf$)
    miscLbl.caption = " --------- complete --------- " 
    doPalette  = false
end sub

''''''''''''''''''''''''''''''''''''''''

sub PCKtoEditBMPs
dim dest as qrect
dim src as qrect
dim bm as qbitmap 
dim c as integer : dim bmpPath as string 
dim a% as integer : dim b% as integer
dim ox% as integer : dim oy% as integer
    if ucase$(right$(glbFileName$,3)) <> "PCK" then
    ' leave if it's not a PCK
        exit sub
    end if
    
    glbBmpLocation = left$(glbFileName$, len(glbFileName$) - 4)
  
    bmpPath = glbBmpLocation & "_EDIT"
'    mkdir glbBmpLocation
    mkdir bmpPath
    miscLbl.caption = bmpPath
    noChange = true
    dirTree.reload
    noChange = false
    doPalette  = true
    doevents
    
'    exit sub
    
    bm.height = 200 : bm.width = 200 : bm.bmphandle = magentaBMP
    with dest
        .top = 1
        .left = 1
        .right = 200
        .bottom = 200
    end with
    with src
        .top = 1
        .left = 1
        .right = 200
        .bottom = 200
    end with
    WinPalPtr = 0
    sustPalPtr = 0
'    f$ = fileListBox1.filename
    f$ = glbFileName$
    fc$ = left$(f$, len(f$) -3) & "col"
    if ucase$(right$(f$,3)) = "PCK" then
        call colFile (fc$)
        call pckFile (f$)
        ' have all images for PCK
        for a% = 1 to 32
            if rtrim$(ebEditGrp1.text) <> "" then
                b% = val(ebEditGrp1.text) - 1
                b% = (b% * 32) + a%
                call drawMyImage(f$, pfStart(b%), pfEnd(b%), 100, 100, 2, b%)
            end if
            if rtrim$(ebEditGrp2.text) <> "" then
                b% = val(ebEditGrp2.text) - 1
                b% = (b% * 32) + a%
                call drawMyImage(f$, pfStart(b%), pfEnd(b%), 100, 100, 0, b%)
            end if
            if rtrim$(ebEditGrp3.text) <> "" then
                b% = val(ebEditGrp3.text) - 1
                b% = (b% * 32) + a%
                call drawMyImage(f$, pfStart(b%), pfEnd(b%), 100, 100, 0, b%)
            end if
            if rtrim$(ebEditGrp4.text) <> "" then
                b% = val(ebEditGrp4.text) - 1
'                if b% = 6 then
'                    ox% = 100 + hotx(a%)
'                    oy% = 100 + hoty(a%)
'                else
'                    ox% = 100 
'                    oy% = 100 
'                end if
                ox% = 100 + hotx(a%)
                oy% = 100 + hoty(a%)
                b% = (b% * 32) + a%
                call drawMyImage(f$, pfStart(b%), pfEnd(b%), ox%, oy%, 0, b%)
            end if
        
'            bf$ = "P" & right$("000" & str$(a%),3) & ".bmp"
            bf$ = bmpPath & "\P" & right$("000" & str$(a%),3) & ".bmp"

            miscLbl.caption = bf$
            bm.copyrect(dest, myImage, src)
            bm.saveToFile(bf$)
            doevents
        next a%
    end if
    
    myImage.fillrect (0, 0, 200, 200, &HFFFFFF)
    myImage.fillrect (180, 20, 190, 40, &HFF00FF)
    myImage.fillrect (180, 60, 190, 80, &HFFFF00)
    for c = 1 to sustPalPtr
'        listbox1.additems right$("     " & str$(c), 5) & " " & WinPal(c) & " " & right$("     " & str$(WInPalCount(c)), 5)
        py = (c - 1) \ 16
        px = (c - 1 ) - (py * 16)
        currentColor = sustToWin(sustPal(c))
        myImage.fillrect ((px * 10) + 10, (py * 10) + 10, (px * 10) + 20, (py * 10) + 20,currentColor)
    next c
    for c = sustPalPtr to 256
'        listbox1.additems right$("     " & str$(c), 5) & " " & WinPal(c) & " " & right$("     " & str$(WInPalCount(c)), 5)
        py = (c - 1) \ 16
        px = (c - 1 ) - (py * 16)
        if ((px + py) mod 2) = 1 then currentColor = &HFF00FF else currentColor = &HFFFF00
        myImage.fillrect ((px * 10) + 10, (py * 10) + 10, (px * 10) + 20, (py * 10) + 20,currentColor)
    next c
    
    with dest
        .top = 0
        .left = 0
        .right = 200
        .bottom = 200
    end with
    with src
        .top = 0
        .left = 0
        .right = 200
        .bottom = 200
    end with
    
    bm.copyrect(dest, myImage, src)
    bf$ = bmpPath & "\P000.bmp"
    bm.saveToFile(bf$)
    miscLbl.caption = " --------- complete --------- " 
    doPalette  = false
end sub

sub createOneBMP
dim dest as qrect
dim src as qrect
dim bm as qbitmap
    bm.height = 200 : bm.width = 200 : bm.bmphandle = magentaBMP
    with dest
        .top = 1
        .left = 1
        .right = 200
        .bottom = 200
    end with
    with src
        .top = 1
        .left = 1
        .right = 200
        .bottom = 200
    end with
'    f$ = fileListBox1.filename
    f$ = glbFIleName$
    fc$ = left$(f$, len(f$) -3) & "col"
    if ucase$(right$(f$,3)) = "PCK" then
        call colFile (fc$)
        call pckFile (f$)

    v% = imageNumber.position
    
    call drawMyImage(f$, pfStart(1 + v%), pfEnd(1 + v%), 100, 100, 1, 1 + v%)
        bf$ = "P" & right$("000" & str$(1 + v%),3) & ".bmp"
        miscLbl.caption = bf$
        bm.copyrect(dest, myImage, src)
        bm.saveToFile(bf$)
    doevents
    if images > 63 then 
        call drawMyImage(f$, pfStart(33 + v%), pfEnd(33 + v%), 100, 100, 1, 33 + v%)
        bf$ = "P" & right$("000" & str$(33 + v%),3) & ".bmp"
        miscLbl.caption = bf$
        bm.copyrect(dest, myImage, src)
        bm.saveToFile(bf$)
        doevents
    end if
    if images > 95 then 
        call drawMyImage(f$, pfStart(65 + v%), pfEnd(65 + v%), 100, 100, 1, 65 + v%)
        bf$ = "P" & right$("000" & str$(65 + v%),3) & ".bmp"
        miscLbl.caption = bf$
        bm.copyrect(dest, myImage, src)
        bm.saveToFile(bf$)
        doevents
    end if
    
    if images > 127 then 
        call drawMyImage(f$, pfStart(97 + v%), pfEnd(97 + v%), 100, 100, 1, 97 + v%)
        bf$ = "P" & right$("000" & str$(97 + v%),3) & ".bmp"
        miscLbl.caption = bf$
        bm.copyrect(dest, myImage, src)
        bm.saveToFile(bf$)
        doevents
    end if
    if images > 159 then 
        call drawMyImage(f$, pfStart(129 + v%), pfEnd(129 + v%), 100, 100, 1, 129 + v%)
        bf$ = "P" & right$("000" & str$(129 + v%),3) & ".bmp"
        miscLbl.caption = bf$
        bm.copyrect(dest, myImage, src)
        bm.saveToFile(bf$)
        doevents
    end if
    if images > 191 then 
        call drawMyImage(f$, pfStart(161 + v%), pfEnd(161 + v%), 100, 100, 1, 161 + v%)
        bf$ = "P" & right$("000" & str$(161 + v%),3) & ".bmp"
        miscLbl.caption = bf$
        bm.copyrect(dest, myImage, src)
        bm.saveToFile(bf$)
        doevents
    end if
    
    if images > 223 then 
        call drawMyImage(f$, pfStart(193 + v%), pfEnd(193 + v%), 100, 100, 1, 193 + v%)
        bf$ = "P" & right$("000" & str$(193 + v%),3) & ".bmp"
        miscLbl.caption = bf$
        bm.copyrect(dest, myImage, src)
        bm.saveToFile(bf$)
        doevents
    end if
    if images > 255 then 
        call drawMyImage(f$, pfStart(225 + v%), pfEnd(225 + v%), 100, 100, 1, 225 + v%)
        bf$ = "P" & right$("000" & str$(225 + v%),3) & ".bmp"
        miscLbl.caption = bf$
        bm.copyrect(dest, myImage, src)
        bm.saveToFile(bf$)
        doevents
    end if
    if images > 287 then 
        call drawMyImage(f$, pfStart(257 + v%), pfEnd(257 + v%), 100, 100, 1, 257 + v%)
        bf$ = "P" & right$("000" & str$(257 + v%),3) & ".bmp"
        miscLbl.caption = bf$
        bm.copyrect(dest, myImage, src)
        bm.saveToFile(bf$)
        doevents
    end if
          
    if images > 319 then 
        call drawMyImage(f$, pfStart(289 + v%), pfEnd(289 + v%), 100, 100, 1, 289 + v%)
        bf$ = "P" & right$("000" & str$(289 + v%),3) & ".bmp"
        miscLbl.caption = bf$
        bm.copyrect(dest, myImage, src)
        bm.saveToFile(bf$)
        doevents
    end if
    if images > 351 then 
        call drawMyImage(f$, pfStart(321 + v%), pfEnd(321 + v%), 100, 100, 1, 321 + v%)
        bf$ = "P" & right$("000" & str$(321 + v%),3) & ".bmp"
        miscLbl.caption = bf$
        bm.copyrect(dest, myImage, src)
        bm.saveToFile(bf$)
        doevents
    end if
    if images > 383 then 
        call drawMyImage(f$, pfStart(353 + v%), pfEnd(353 + v%), 100, 100, 1, 353 + v%)
        bf$ = "P" & right$("000" & str$(353 + v%),3) & ".bmp"
        miscLbl.caption = bf$
        bm.copyrect(dest, myImage, src)
        bm.saveToFile(bf$)
        doevents
    end if
          
    if images > 415 then 
        call drawMyImage(f$, pfStart(385 + v%), pfEnd(385 + v%), 100, 100, 1, 385 + v%)
        bf$ = "P" & right$("000" & str$(385 + v%),3) & ".bmp"
        miscLbl.caption = bf$
        bm.copyrect(dest, myImage, src)
        bm.saveToFile(bf$)
        doevents
    end if
    if images > 447 then 
        call drawMyImage(f$, pfStart(417 + v%), pfEnd(417 + v%), 100, 100, 1, 417 + v%)
        bf$ = "P" & right$("000" & str$(417 + v%),3) & ".bmp"
        miscLbl.caption = bf$
        bm.copyrect(dest, myImage, src)
        bm.saveToFile(bf$)
        doevents
    end if
    if images > 479 then 
        call drawMyImage(f$, pfStart(449 + v%), pfEnd(449 + v%), 100, 100, 1, 449 + v%)
'        bf$ = "c:\rapidq\P" & right$("000" & str$(a%),3) & ".bmp"
        bf$ = "P" & right$("000" & str$(449 + v%),3) & ".bmp"
        miscLbl.caption = bf$
        bm.copyrect(dest, myImage, src)
        bm.saveToFile(bf$)
        doevents
    end if
          
    end if
end sub

$typecheck on

sub addToWinPal (wc as integer)
dim c as integer
dim cs as string
    cs = "-"
    if WinPalPtr > 0 then
        for c = 1 to WinPalPtr
            if WinPal(c) = wc then
                cs = ""
                WinPalCount(c) = WinPalCount(c) + 1
            end if
        next c
    end if
    if cs <> "" then
    ' not in palette already
        WinPalPtr = WinPalPtr + 1
        WinPal(WinPalPtr) = wc
        WinPalCount(WinPalPtr) = 1
    end if
end sub

dim glbPalPtr as byte

sub paletteGlb(wc as integer)
dim p as byte
dim c as integer
dim sc as word
    sc = winToSust(wc)
    glbPalPtr = 0
    for c = 1 to sustPalPtr
        if sustPal(c) = sc then
            glbPalPtr = c - 1
            exit for
        end if
    next c
end sub

function paletteNumber(wc as integer) as integer
dim p as byte
dim c as integer
dim sc as word
'dim wp as byte
    listbox1.additems "palette " 
    sc = winToSust(wc)
'    wp = 0
    paletteNumber = 0
    for c = 1 to sustPalPtr
        if sustPal(c) = sc then
            paletteNumber = c - 1
    listbox1.additems "palette " & hex$(c - 1)
'            p = c - 1
'            wp = p
        end if
    next c
'    paletteNumber = wp
'    paletteNumber = 33
end function

dim lX as integer : dim hX as integer : dim lY as integer : dim hY as integer

sub scanBMP (bp as qbitmap)
dim a as integer : dim b as integer : dim c as integer
dim currentColor as integer : dim transparent as integer : dim wc as integer
'dim cs as string
dim px as integer : dim py as integer
dim bWidth as integer : dim bHeight as integer

    transParent = bp.pixel(2,2)
    if (transParent and &HFFFF00) = &HFFFF00 then
    ' blue and green at max
    ' ignore any red component
        transParent = &HFFFF00
    end if
' 05/08/2003 
'    lX = 500 : hX = -500
'    lY = 500 : hY = -500
    lX = 1000 : hX = -1000
    lY = 2000 : hY = -1000
' 05/08/2003 
' allow various size bitmaps ...
    bWidth = bp.width
    bHeight = bp.height
    for b = 2 to bHeight - 2
        for a = 2 to bWidth - 2
'    for b = 2 to 198
'        for a = 2 to 198
' 05/08/2003 end changes
            currentColor = bp.pixel(a,b)
            wc = currentColor and &HFFFF00
            if wc = &HFFFF00 then
            ' blue and green at max
               if (currentColor and &H0000FF) < glbShadowTol then
'               ' tolerance value for red ...
                   currentColor = &HFFFF00
               end if
'            ' ignore any red component
'                currentColor = &HFFFF00
            end if
            if currentColor <> transParent then
                if a < lX then lX = a
                if a > hX then hX = a
                if b < lY then lY = b
                if b > hY then hY = b
                if transparent = &Hff00ff then call addToSustPal (currentColor)
                doevents
            else
            end if
        next a
    next b
end sub

sub writeImage (bp as qbitmap)
DIM File AS QFileStream
dim a as integer : dim b as integer : dim c as integer
dim wHshift as short
dim wVshift as short
dim wWidth as short
dim wHeight as short
dim wb as byte
dim currentColor as integer : dim transparent as integer : dim wc as integer

    dim bytes(1000) as byte
    dim bytePtr as word
    dim lastTypePtr as word
    dim lastColorPtr as word
'    dim bytePtr as short
'    dim lastTypePtr as short
    
'    transParent = myImage.pixel(2,2)
    transParent = bp.pixel(2,2)
    if (transParent and &HFFFF00) = &HFFFF00 then
    ' blue and green at max
    ' ignore any red component
        transParent = &HFFFF00
    end if

    if transparent = &Hff00ff then
    ' normal color
        wb = 162
    else
    ' shadow
        wb = 163
        ' bypass shadows for now
'        exit sub
    end if
    
' 05/08/2003
'    wHshift = lX - 101
'    wVshift = lY - 101
    wHshift = lX - ((bp.width / 2) + 1)
    wVshift = lY - ((bp.height / 2) + 1)
    wWidth = hX - lX + 1
    wHeight = hY - lY + 1
    listbox1.additems "-- write image " & wrkFile
    listbox1.additems "center horizontal = " & str$(((bp.width / 2) + 1))
    listbox1.additems "center vertical   = " & str$(((bp.height / 2) + 1))

    File.Open("Q" & wrkFile & ".pcq", fmCreate)
    
' 05/08/2003
'    if wWidth = -999 and wHeight = -999 then
    if hX = -1000 and hY = -1000 then
    ' blank image
        wHshift = 0
        wVshift = 0
        wWidth = 1
        wHeight = 1
        file.write(wHshift)
        file.write(wVshift)
        file.write(wWidth)
        file.write(wHeight)
        file.write(wb)
        file.write(wHshift)
   
    else
    ' normal image
    file.write(wHshift)
    file.write(wVshift)
    file.write(wWidth)
    file.write(wHeight)
    file.write(wb)

    for b = lY to hY
        bytePtr = 0
        lastTypePtr = 0
        lastColorPtr = 0
        for a = lX to hX
        if wb = 162 then
        ' imgae data 
'            currentColor = myImage.pixel(a,b)
           currentColor = bp.pixel(a,b)
            if a = lX then
            ' start of line
                if currentColor = transParent then
                ' C1 means transparent 
                    bytePtr = bytePtr + 1 : lastTypePtr = bytePtr : bytes(bytePtr) = &HC1 
                else
' single colors
                    bytePtr = bytePtr + 1 : lastTypePtr = bytePtr : bytes(bytePtr) = 1 
' soft drawn
'                    bytePtr = bytePtr + 1 : lastTypePtr = bytePtr : bytes(bytePtr) = &H41 
                    bytePtr = bytePtr + 1 : call paletteGlb(currentColor) : bytes(bytePtr) = glbPalPtr
                    lastColorPtr = bytePtr
                end if
            else
            ' not start of line
                if currentColor = transParent and bytes(lastTypePtr) > 192 then
                ' last was transparent
                    if bytes(lastTypePtr) < 255 then
                    ' still have space
                        bytes(lastTypePtr) = bytes(lastTypePtr) + 1 
                    else
                    ' start new transparent streem
                        bytePtr = bytePtr + 1 : lastTypePtr = bytePtr : bytes(bytePtr) = &HC1 
                    end if
                    
'                elseif currentColor = transParent and bytes(lastTypePtr) < 64 then
                elseif currentColor = transParent and bytes(lastTypePtr) < &HC0 then
                ' last was actual color
                    bytePtr = bytePtr + 1 : lastTypePtr = bytePtr : bytes(bytePtr) = &HC1 
                    
'                elseif currentColor <> transParent and bytes(lastTypePtr) < 64 then
                elseif currentColor <> transParent and bytes(lastTypePtr) < &HC0 then
                ' last was color
'                    if bytes(lastTypePtr) < 63 then
                    if (bytes(lastTypePtr) and &H3F) < 63 then
                    ' still have space
                        bytes(lastTypePtr) = bytes(lastTypePtr) + 1 
                        bytePtr = bytePtr + 1 : call paletteGlb(currentColor) : bytes(bytePtr) = glbPalPtr
                        lastColorPtr = bytePtr
                    else
                    ' start new color streem
                        bytePtr = bytePtr + 1 : lastTypePtr = bytePtr : bytes(bytePtr) = 1 
' soft drawn
'                    bytePtr = bytePtr + 1 : lastTypePtr = bytePtr : bytes(bytePtr) = &H41 
                        bytePtr = bytePtr + 1 : call paletteGlb(currentColor) : bytes(bytePtr) = glbPalPtr
                        lastColorPtr = bytePtr
                    end if
                elseif currentColor <> transParent and bytes(lastTypePtr) > 192 then
                ' last was transparent
                    bytePtr = bytePtr + 1 : lastTypePtr = bytePtr : bytes(bytePtr) = 1 
' soft drawn
'                    bytePtr = bytePtr + 1 : lastTypePtr = bytePtr : bytes(bytePtr) = &H41 
                    bytePtr = bytePtr + 1 : call paletteGlb(currentColor) : bytes(bytePtr) = glbPalPtr
                    lastColorPtr = bytePtr
                end if
            end if
        else
        ' shadow data
'            currentColor = myImage.pixel(a,b)
           currentColor = bp.pixel(a,b)
           wc = currentColor and &HFFFF00
' testing with now3d images ...
' tolerance for background cyan 
           if wc = &HFFFF00 then
           ' blue and green at max
               if (currentColor and &H0000FF) < glbShadowTol then
'               ' tolerance value for red ...
                   currentColor = &HFFFF00
               end if
           end if
           
            if a = lX then
            ' start of line
                if currentColor = transParent then
                    bytePtr = bytePtr + 1 : lastTypePtr = bytePtr : bytes(bytePtr) = &H81 
                else
                    bytePtr = bytePtr + 1 : lastTypePtr = bytePtr : bytes(bytePtr) = 1 
                    lastColorPtr = bytePtr
                end if
            else
            ' not start of line
                if currentColor = transParent and bytes(lastTypePtr) > 128 then
                ' last was transparent
                    if bytes(lastTypePtr) < 255 then
                    ' still have space
                        bytes(lastTypePtr) = bytes(lastTypePtr) + 1 
                    else
                    ' start new transparent streem
                        bytePtr = bytePtr + 1 : lastTypePtr = bytePtr : bytes(bytePtr) = &H81 
                    end if
                elseif currentColor = transParent and bytes(lastTypePtr) < 128 then
                ' last was shadow
                    bytePtr = bytePtr + 1 : lastTypePtr = bytePtr : bytes(bytePtr) = &H81 
                    
                elseif currentColor <> transParent and bytes(lastTypePtr) < 128 then
                ' last was shadow
                    if bytes(lastTypePtr) < 127 then
                    ' still have space
                        bytes(lastTypePtr) = bytes(lastTypePtr) + 1 
                        lastColorPtr = bytePtr
                    else
                    ' start new shadow streem
                        bytePtr = bytePtr + 1 : lastTypePtr = bytePtr : bytes(bytePtr) = 1 
                        lastColorPtr = bytePtr
                    end if
                elseif currentColor <> transParent and bytes(lastTypePtr) > 128 then
                ' last was transparent
                    bytePtr = bytePtr + 1 : lastTypePtr = bytePtr : bytes(bytePtr) = 1 
                    lastColorPtr = bytePtr
                end if
            end if
        end if
        next a
        
        ' end of line
        ' doesn't work for shadows ...
        if wb = 162 then
        ' normal colors
            if bytes(lastTypePtr) > 192 then
            ' skip right part if transparent
'                bytePtr = lastTypePtr - 1
                bytePtr = lastColorPtr
            end if
        else
        ' shadows
            if bytes(lastTypePtr) > 128 then
            ' skip right part if transparent
'                bytePtr = lastTypePtr - 1
                bytePtr = lastColorPtr
            end if
        end if
        
        ' length of line
        file.write(bytePtr)
        for c = 1 to bytePtr
        ' data
            file.write(bytes(c))
        next c
        
    next b
    
    end if
    file.close
end sub

sub BMPtoPCK
DIM File AS QFileStream
dim dest as qrect
dim src as qrect
dim a as integer : dim g as integer : dim currentColor as integer : dim c as integer : dim px as integer : dim py as integer
dim f as string : dim intAddress as integer : dim pc as word
dim wf as string : dim maxBmp as integer
dim gblBmp as qbitmap
    dim redV as word
    dim greenV as word
    dim blueV as word
    dim bt as word

'    wrkFile = command$(2)
'    outPath = command$(1)
    
    miscLbl.caption = wrkFile  
'    listbox1.additems wrkFile  
    for a = 1 to len(outPath) 
        if mid$(outPath,a,1) = "*" then outPath = replace$(outPath," ",a) 
    next a
  
    chdir outPath
'    miscLbl.caption = outPath  
'    listbox1.additems outPath  
    
    if wrkFile = "001"  and glbStartEdit.text = "" and glbEndEdit.text = "" then
' 05/08/2003 testing ...    
        f = "WORK.COL"
        sustPalPtr = 0
        File.Open(f, fmCreate)
        file.close
    else
        f = "WORK.COL"
        sustPalPtr = 0
        File.Open(f, fmOpenRead)
        while not file.eof
            sustPalPtr = sustPalPtr + 1
            file.read(sustPal(sustPalPtr))
    ' convert to Windows color and store it 
            call cvtCol (sustPal(sustPalPtr), @redV, @greenV, @blueV, @bt)
            WINcolValue(sustPalPtr - 1) = winCol (redV, greenV, blueV, bt)
            glbColMax = sustPalPtr - 1
        wend
        file.close
        call drawPalette
    end if
        
    gblBmp.height = 200 : gblBmp.width = 200 : gblBmp.pixelformat = 6

    f = "P" & wrkFile & ".bmp"
    
    gblBmp.loadFromFile(f)
    myImage.bmp = f
    bmpLbl.caption = f & " " str$(myImage.width) & " by " str$(myImage.height)

    doevents
        
    call scanBMP (gblBmp)
        
    call writeImage (gblBmp)

    doevents
    
    miscLbl.caption = f & " --> " & "Q" & wrkFile & ".pcq   colors: " & str$(sustPalPtr)
    listbox1.additems f & " --> " & "Q" & wrkFile & ".pcq   colors: " & str$(sustPalPtr)
        
    f = "WORK.COL"
    File.Open(f, fmCreate)
    for g = 1 to sustPalPtr
        file.write(sustPal(g))
    next g
    file.close
   
end sub

'=================================

sub BMPsToPCK
'
' create PCK file form BMP files ...
'
DIM File AS QFileStream
DIM File2 AS QFileStream
dim dest as qrect
dim src as qrect
dim a as integer : dim g as integer : dim currentColor as integer : dim c as integer : dim px as integer : dim py as integer
dim f as string : dim f2 as string : dim intAddress as integer : dim pc as word
dim wf as string : dim maxBmp as integer : dim wc as string : dim startAt as integer : dim endAt as integer
dim gblBmp as qbitmap
dim quit as integer

    panel0.color = &HFF00FF
    panel0.enabled = false
    panel12.color = &HFF00FF
    panel12.enabled = false
    panel13.color = &HFF00FF
    panel13.enabled = false

    quit = false
    sustPalPtr = 0
    listbox1.clear
    
    wf = dir$("*.bmp",0)
    maxBmp = 0
    while wf <> ""
        miscLbl.caption = wf
        if val(mid$(wf,2,3)) > maxBmp then maxBmp = val(mid$(wf,2,3))
        wf = dir$
    wend
'    miscLbl.caption = "there are " & str$(maxBmp) & " bitmaps"
    listbox1.additems "folder: " & curdir$  
    listBox1.additems "there are " & str$(maxBmp) & " bitmaps"

    if glbStartEdit.text = "" then
        startAt = 1
    else
        startAt = val(glbStartEdit.text)
    end if
    if glbEndEdit.text = "" then
        endAt = maxBmp
    else
        endAt = val(glbEndEdit.text)
    end if
    
    if glbShadowTolEdit.text = "" then
        glbShadowTol = 1
    else
        glbShadowTol = val(glbShadowTolEdit.text) + 1
    end if
    
    glbColRed = val(convbase$(right$("11111111" & glbColRedEdit.text, 8), 2, 10))
'    form.textout (5, 300, hex$(glbColRed) & " " & bin$(glbColRed), &H00007F, form.color)
    
    listbox1.additems "start: " & str$(startAt) & "  end: " & str$(endAt)
    listbox1.additems "color reduction mask: " & bin$(glbColRed) & " - " & str$(glbColRed)
    listbox1.additems "shadow tolerance: " & str$(glbShadowTol)
' -------------------------        
    for a = startAt to endAt
 
        f = curdir$ 
        for g = 1 to len(f) 
            if mid$(f,g,1) = " " then f = replace$(f,"*",g) 
        next g
        miscLbl.caption = f
        wf = right$("000" & str$(a),3) 
        
        wrkFile = wf
        outPath = f
        
'        wc = basePath & "\BMPtoPCK.exe " & f & " " & wf
'        miscLbl.caption = wc
'        shell wc
        if quitOnOverflow.checked = true then
        ' quit when over 256 colors ...
            if sustPalPtr > 256 then
                listBox1.additems " --- quit because of palette overflow --- "
                quit = true
                exit for                
            else
                call BMPtoPCK
            end if
            doevents
        else
        ' continue anyway ...
            call BMPtoPCK
            doevents
        end if
    next a
    
    if quit = false then
        f = "WORK.COL"
        sustPalPtr = 0
        File.Open(f, fmOpenRead)
        while not file.eof
            sustPalPtr = sustPalPtr + 1
            file.read(sustPal(sustPalPtr))
        wend
        file.close

' palette ...
        f = glbOutFile & ".col"
        File.Open(f, fmCreate)
        if sustPalPtr < 256 then
        ' pad with white (?)
            for a = 1 to sustPalPtr
                file.write(sustPal(a))
            next a
            pc = &Hffffff
            for a = sustPalPtr + 1 to 256
                file.write(pc)
            next a
            file.close
        else
        ' write out the first 256
            for a = 1 to 256
                file.write(sustPal(a))
            next a
            file.close
        end if
    
        f = glbOutFile & ".pck"
        File.Open(f, fmCreate)
        ' need space for maxBmp + 1 address for eof
        intAddress = (maxBmp + 1) * 4
        for a = 0 to maxBmp
            file.write(intAddress)
        next a
        File.close
    
        File.Open(f, fmOpenReadWrite)
        intAddress = (maxBmp + 1) * 4
        for a = 1 to maxBmp
            intAddress = file.size
            file.position = (a - 1) * 4
            file.write(intAddress)
            file.position = intAddress
            f2 = "Q" & right$("000" & str$(a),3) & ".pcq"
            misclbl.caption = f2
            file2.open(f2, fmOpenRead)
            file.copyFrom(file2,0)
            file2.close
        next a
    
        intAddress = file.size
        file.position = maxBmp * 4
        file.write(intAddress)
        file.close
            
        miscLbl.caption = " --------- complete --------- " & str$(sustPalPtr) & " colors in palette "
        listbox1.additems " --------- complete --------- " & str$(sustPalPtr) & " colors in palette "
    end if
    listbox1.saveToFile (glbOutFile & ".log")
    panel0.enabled = true
    panel0.color = form.color
    panel12.enabled = true
    panel12.color = form.color
    panel13.enabled = true
    panel13.color = form.color
end sub

'----------------------                       rs2File

sub rs2File (f$ as string)
    DIM File AS QFileStream
    dim ptrWork as long
    dim imStart as long
    dim imEnd as long
    dim wHshift as short
    dim wVshift as short
    dim wWidth as short
    dim wHeight as short
    dim wb as byte
    dim o$
    dim images as integer
     
    dim imCount as long
    dim imDisp as long
    dim imVal as long
    dim imRel as long : dim q as integer
    dim sX as integer : dim sY as integer : dim iGrp as integer : dim iGrpI as integer
    dim iGrpS as integer
    
    call colfile("SOLD.PL")
    
    form.repaint
    
    File.Open(f$, fmOpenRead)
    listbox1.clear
    listbox1.color = &Hff00ff
    filelistbox1.color = &Hff00ff
    panel0.enabled = false
    panel1.enabled = false
    listbox1.additems " file size " & right$("00000000" & hex$(file.size),8) & "  " & str$(file.size) & " " & f$

    ptrWork = 0
    images = 0
    imEnd = 0
    iGrp = 0
    iGrpI = 0
    
    sx = 50
    sy = 230
    listbox1.clear
    listbox1.additems "image    start      end group  item count position  display  val"
    
    while imEnd < file.size

            file.read(imCount)
    
    ' more than 1 image is not correct ...
    ' extra stuff may be positions of images ...
    
            file.read(imDisp)
        
            imRel = file.position     
            
            file.read(imVal)
    
            iGrp = iGrp + 1
            iGrpI =  0
    
            if imCount = 1 then   
                imStart = imRel + imVal
                imEnd = imRel + imDisp 
'                call pckImage(f$, imStart, imEnd)
        images = images + 1
        iGrpI = iGrpI + 1
    listbox1.additems right$("     " & str$(images),5) & " " & right$("00000000" & hex$(imStart),8) & " " & right$("00000000" & hex$(imEnd),8) & " " & right$("     " & str$(iGrp),5) & " " & right$("     " & str$(iGrpI),5) & " " & right$("     " & str$(imCount),5) & " " & right$("00000000" & hex$(file.position),8) & " " & right$("00000000" & hex$(imDisp),8) & " " & right$("    " & str$(imVal),4)
    if ((iGrp mod 8) = 1) and iGrpI = 1 then
        call pckImage(f$, imStart, imEnd)
        if sY < 900 then call drawImage(f$, imStart, imEnd, sX, sY, 2, images)
        sX = sX + 70
        if sX > 750 then
            sX = 50
            sY = sY + 75
        end if
    end if 
            else
                for q = imCount to 2 step -1
                    imStart = imRel + imVal
                    file.read(imVal)
                    imEnd = imRel + imVal
'                    call pckImage(f$, imStart, imEnd)
        images = images + 1
        iGrpI = iGrpI + 1
    listbox1.additems right$("     " & str$(images),5) & " " & right$("00000000" & hex$(imStart),8) & " " & right$("00000000" & hex$(imEnd),8) & " " & right$("     " & str$(iGrp),5) & " " & right$("     " & str$(iGrpI),5) & " " & right$("     " & str$(imCount),5) & " " & right$("00000000" & hex$(file.position),8) & " " & right$("00000000" & hex$(imDisp),8) & " " & right$("    " & str$(imVal),4)
    if ((iGrp mod 8) = 1) and iGrpI = 1 then
        call pckImage(f$, imStart, imEnd)
        if sY < 900 then call drawImage(f$, imStart, imEnd, sX, sY, 2, images)
        sX = sX + 70
        if sX > 750 then
            sX = 50
            sY = sY + 75
        end if
    end if
                    doevents
                next q
                
                imStart = imEnd
                imEnd = imRel + imDisp
'                call pckImage(f$, imStart, imEnd)
        images = images + 1
        iGrpI = iGrpI + 1
    listbox1.additems right$("     " & str$(images),5) & " " & right$("00000000" & hex$(imStart),8) & " " & right$("00000000" & hex$(imEnd),8) & " " & right$("     " & str$(iGrp),5) & " " & right$("     " & str$(iGrpI),5) & " " & right$("     " & str$(imCount),5) & " " & right$("00000000" & hex$(file.position),8) & " " & right$("00000000" & hex$(imDisp),8) & " " & right$("    " & str$(imVal),4)
    if ((iGrp mod 8) = 1) and iGrpI = 1 then
        call pckImage(f$, imStart, imEnd)
        if sY < 900 then call drawImage(f$, imStart, imEnd, sX, sY, 2, images)
        sX = sX + 70
        if sX > 750 then
            sX = 50
            sY = sY + 75
        end if
    end if
           end if
            
        file.position = imEnd
         
        doevents
    wend
          
    file.close
    listbox1.additems ""
'    listbox1.additems "-- image count " & str$(images)
    listbox1.insertItem(1, "-- " & str$(images) & " images, " & str$(iGrp) & " groups")
    listbox1.color = &Hffffff
    filelistbox1.color = &Hffffff
    panel0.enabled = true
    panel1.enabled = true
end sub

sub rs2FilePose (f$ as string, pose as integer)
    DIM File AS QFileStream
    dim ptrWork as long
    dim imStart as long
    dim imEnd as long
    dim wHshift as short
    dim wVshift as short
    dim wWidth as short
    dim wHeight as short
    dim wb as byte
    dim o$
    dim images as integer
     
    dim imCount as long
    dim imDisp as long
    dim imVal as long
    dim imRel as long : dim q as integer
    dim sX as integer : dim sY as integer : dim iGrp as integer : dim iGrpI as integer
    dim iPose as integer
    
    call colfile("SOLD.PL")
    
'    form.repaint
    
    File.Open(f$, fmOpenRead)
'    listbox1.clear
'    listbox1.color = &Hff00ff
'    filelistbox1.color = &Hff00ff
    panel0.enabled = false
    panel1.enabled = false
'    listbox1.additems " file size " & right$("00000000" & hex$(file.size),8) & "  " & str$(file.size) & " " & f$

    ptrWork = 0
    images = 0
    imEnd = 0
    iGrp = 0
    iGrpI = 0
    
    sx = 50
    sy = 320
       
'    listbox1.additems "image    start      end group  item count position  display  val"
    
    while imEnd < file.size

            file.read(imCount)
    
    ' more than 1 image is not correct ...
    ' extra stuff may be positions of images ...
    
            file.read(imDisp)
        
            imRel = file.position     
            
            file.read(imVal)
    
            iPose = iGrp \ 8
            iGrp = iGrp + 1
            iGrpI =  0
    
            if imCount = 1 then   
                imStart = imRel + imVal
                imEnd = imRel + imDisp 
'                call pckImage(f$, imStart, imEnd)
        images = images + 1
        iGrpI = iGrpI + 1
'    listbox1.additems right$("     " & str$(images),5) & " " & right$("00000000" & hex$(imStart),8) & " " & right$("00000000" & hex$(imEnd),8) & " " & right$("     " & str$(iGrp),5) & " " & right$("     " & str$(iGrpI),5) & " " & right$("     " & str$(imCount),5) & " " & right$("00000000" & hex$(file.position),8) & " " & right$("00000000" & hex$(imDisp),8) & " " & right$("    " & str$(imVal),4)
    if (iPose = pose) and iGrpI = 1 then
        if sY < 900 then call drawImage(f$, imStart, imEnd, sX, sY, 2, images)
        sX = sX + 70
        if sX > 750 then
            sX = 50
            sY = sY + 75
        end if
    end if 
            else
                for q = imCount to 2 step -1
                    imStart = imRel + imVal
                    file.read(imVal)
                    imEnd = imRel + imVal
'                    call pckImage(f$, imStart, imEnd)
        images = images + 1
        iGrpI = iGrpI + 1
'    listbox1.additems right$("     " & str$(images),5) & " " & right$("00000000" & hex$(imStart),8) & " " & right$("00000000" & hex$(imEnd),8) & " " & right$("     " & str$(iGrp),5) & " " & right$("     " & str$(iGrpI),5) & " " & right$("     " & str$(imCount),5) & " " & right$("00000000" & hex$(file.position),8) & " " & right$("00000000" & hex$(imDisp),8) & " " & right$("    " & str$(imVal),4)
    if (iPose = pose) and iGrpI = 1 then
        if sY < 900 then call drawImage(f$, imStart, imEnd, sX, sY, 2, images)
        sX = sX + 70
        if sX > 750 then
            sX = 50
            sY = sY + 75
        end if
    end if
                    doevents
                next q
                
                imStart = imEnd
                imEnd = imRel + imDisp
'                call pckImage(f$, imStart, imEnd)
        images = images + 1
        iGrpI = iGrpI + 1
'    listbox1.additems right$("     " & str$(images),5) & " " & right$("00000000" & hex$(imStart),8) & " " & right$("00000000" & hex$(imEnd),8) & " " & right$("     " & str$(iGrp),5) & " " & right$("     " & str$(iGrpI),5) & " " & right$("     " & str$(imCount),5) & " " & right$("00000000" & hex$(file.position),8) & " " & right$("00000000" & hex$(imDisp),8) & " " & right$("    " & str$(imVal),4)
    if (iPose = pose) and iGrpI = 1 then
        if sY < 900 then call drawImage(f$, imStart, imEnd, sX, sY, 2, images)
        sX = sX + 70
        if sX > 750 then
            sX = 50
            sY = sY + 75
        end if
    end if
           end if
            
        file.position = imEnd
         
        doevents
    wend
          
    file.close
'    listbox1.additems ""
'    listbox1.additems "-- image count " & str$(images)
'    listbox1.insertItem(1, "-- " & str$(images) & " images, " & str$(iGrp) & " groups")
    listbox1.color = &Hffffff
    filelistbox1.color = &Hffffff
    panel0.enabled = true
    panel1.enabled = true
end sub

sub rs2FileGroup (f$ as string, grp as integer)
    DIM File AS QFileStream
    dim ptrWork as long
    dim imStart as long
    dim imEnd as long
    dim wHshift as short
    dim wVshift as short
    dim wWidth as short
    dim wHeight as short
    dim wb as byte
    dim o$
    dim images as integer
     
    dim imCount as long
    dim imDisp as long
    dim imVal as long
    dim imRel as long : dim q as integer
    dim sX as integer : dim sY as integer : dim iGrp as integer : dim iGrpI as integer
    dim iPose as integer
    
    call colfile("SOLD.PL")
    call drawPalette
    
'    form.repaint
    
    File.Open(f$, fmOpenRead)
'    listbox1.clear
'    listbox1.color = &Hff00ff
'    filelistbox1.color = &Hff00ff
    panel0.enabled = false
    panel1.enabled = false
'    listbox1.additems " file size " & right$("00000000" & hex$(file.size),8) & "  " & str$(file.size) & " " & f$

    ptrWork = 0
    images = 0
    imEnd = 0
    iGrp = 0
    iGrpI = 0
    
    sx = 50
    sy = 410
       
'    listbox1.additems "image    start      end group  item count position  display  val"
    
    while imEnd < file.size

            file.read(imCount)
    
    ' more than 1 image is not correct ...
    ' extra stuff may be positions of images ...
    
            file.read(imDisp)
        
            imRel = file.position     
            
            file.read(imVal)
    
            iPose = iGrp \ 8
            iGrp = iGrp + 1
            iGrpI =  0
    
            if imCount = 1 then   
                imStart = imRel + imVal
                imEnd = imRel + imDisp 
'                call pckImage(f$, imStart, imEnd)
        images = images + 1
        iGrpI = iGrpI + 1
'    listbox1.additems right$("     " & str$(images),5) & " " & right$("00000000" & hex$(imStart),8) & " " & right$("00000000" & hex$(imEnd),8) & " " & right$("     " & str$(iGrp),5) & " " & right$("     " & str$(iGrpI),5) & " " & right$("     " & str$(imCount),5) & " " & right$("00000000" & hex$(file.position),8) & " " & right$("00000000" & hex$(imDisp),8) & " " & right$("    " & str$(imVal),4)
    if (iGrp = grp) then
        if sY < 900 then call drawImage(f$, imStart, imEnd, sX, sY, 2, images)
        sX = sX + 50
        if sX > 750 then
            sX = 50
            sY = sY + 75
        end if
    end if 
            else
                for q = imCount to 2 step -1
                    imStart = imRel + imVal
                    file.read(imVal)
                    imEnd = imRel + imVal
'                    call pckImage(f$, imStart, imEnd)
        images = images + 1
        iGrpI = iGrpI + 1
'    listbox1.additems right$("     " & str$(images),5) & " " & right$("00000000" & hex$(imStart),8) & " " & right$("00000000" & hex$(imEnd),8) & " " & right$("     " & str$(iGrp),5) & " " & right$("     " & str$(iGrpI),5) & " " & right$("     " & str$(imCount),5) & " " & right$("00000000" & hex$(file.position),8) & " " & right$("00000000" & hex$(imDisp),8) & " " & right$("    " & str$(imVal),4)
    if (iGrp = grp) then
        if sY < 900 then call drawImage(f$, imStart, imEnd, sX, sY, 2, images)
        sX = sX + 50
        if sX > 750 then
            sX = 50
            sY = sY + 75
        end if
    end if
                    doevents
                next q
                
                imStart = imEnd
                imEnd = imRel + imDisp
'                call pckImage(f$, imStart, imEnd)
        images = images + 1
        iGrpI = iGrpI + 1
'    listbox1.additems right$("     " & str$(images),5) & " " & right$("00000000" & hex$(imStart),8) & " " & right$("00000000" & hex$(imEnd),8) & " " & right$("     " & str$(iGrp),5) & " " & right$("     " & str$(iGrpI),5) & " " & right$("     " & str$(imCount),5) & " " & right$("00000000" & hex$(file.position),8) & " " & right$("00000000" & hex$(imDisp),8) & " " & right$("    " & str$(imVal),4)
    if (iGrp = grp) then
        if sY < 900 then call drawImage(f$, imStart, imEnd, sX, sY, 2, images)
        sX = sX + 50
        if sX > 750 then
            sX = 50
            sY = sY + 75
        end if
    end if
           end if
            
        file.position = imEnd
         
        doevents
    wend
          
    file.close
'    listbox1.additems ""
'    listbox1.additems "-- image count " & str$(images)
'    listbox1.insertItem(1, "-- " & str$(images) & " images, " & str$(iGrp) & " groups")
    listbox1.color = &Hffffff
    filelistbox1.color = &Hffffff
    panel0.enabled = true
    panel1.enabled = true
end sub

sub aniFile (f$ as string)
    DIM File AS QFileStream
    dim ptrWork as long
    dim imStart as long
    dim imEnd as long
    dim dummy as long
    dim wHshift as short
    dim wVshift as short
    dim wWidth as short
    dim wHeight as short
    dim wb as byte
    dim o$
    dim sX as integer : dim sY as integer 
        
    miscLbl.caption = f$ 
    File.Open(f$, fmOpenRead)
    
    listbox1.color = &Hff00ff
    filelistbox1.color = &Hff00ff
    panel0.enabled = false
    panel1.enabled = false
    
'    listbox1.clear
    file.read(dummy)
    listbox1.additems "dummy " & hex$(dummy) 
    file.read(dummy)
    listbox1.additems "dummy " & hex$(dummy) & " " & str$(dummy)
    ptrWork = file.position
    
'    ptrWork = 0
    form.repaint
    images = 1
    imEnd = 0
    sx = 50
    sy = 300
' PCK file format ...
'    starts with a series of addresses of data type integer / long - 4 bytes.
'    each address is the start of data for an image.
'    the last address is the size of the file.
    while (imEnd < file.size) 
'    while imEnd < file.size

   ' position to offset ...
        file.position = ptrWork
     
        file.read(imStart)
   ' save offset for next image
        ptrWork = file.position
         
   ' get start of next image for use as end of this image
        file.read(imEnd)
   ' image ...
            
        pfStart(images) = imStart
        pfEnd(images) = imEnd
    ' text display ...
        listbox1.additems "image " & str$(images)
        if images < 11 then call pckImage(f$, imStart, imEnd)
    ' image display
        if images < 11 then call drawImage(f$, imStart, imEnd, sX, sY, 1, images)         
        sx = sx + 75
        if sX > 750 then
            sX = 50
            sY = sY + 75
        end if
        
        images = images + 1
        
    wend
    file.close
    listbox1.insertItem(0, "  --   " & str$(images - 1) & " images")
    listbox1.color = &Hffffff
    filelistbox1.color = &Hffffff
    panel0.enabled = true
    panel1.enabled = true
end sub

'----------------------------------------------------------------------                 resFile

sub resFile (f$ as string)
    DIM File AS QFileStream
    DIM File2 AS QFileStream
    dim ptrWork as long
    dim ptrWork2 as long
    dim imStart as long
    dim imEnd as long
    dim imEnd2 as long
    dim imEnd3 as long
    dim dummy as long
    dim wHshift as short
    dim wVshift as short
    dim wWidth as short
    dim wHeight as short
    dim wb as byte
    dim wb2 as byte
    dim o$
    dim sX as integer : dim sY as integer 
    dim h$ as string
' global now
'    dim sXinc as integer : dim sYinc as integer 
    dim sXlimit as integer : dim sXstart as integer
    dim showAll as integer
    dim dest as qrect
    dim src as qrect
    dim bm as qbitmap : dim bf as string 
    problemImages = 0
    
    bm.bmphandle = cyanBMP
'    bm.height = 50 : bm.width = 50     
    with dest
        .top = 0
        .left = 0
        .right = 100
        .bottom = 100
    end with
    with src
        .top = 0
        .left = 0
        .right = 100
        .bottom = 100
    end with
            
    listbox1.color = &Hff00ff
    filelistbox1.color = &Hff00ff
    panel0.enabled = false
    panel1.enabled = false

    miscLbl.caption = f$ 
    File.Open(f$, fmOpenRead)
    
    file.read(dummy)
'    file.read(dummy)
    ptrWork = file.position
    listbox1.clear
    
'    ptrWork = 0
    form.repaint
    images = 1
    imEnd = 0
    sx = 50
    sy = 200
    if ucase$(right$(f$,8)) = "ICON.RES" then
        sX = 5
        sXstart = 5
        sY = 160
        sXinc = 72
        sYinc = 72
        sXlimit = 750
        showAll = true
    elseif ucase$(right$(f$,9)) = "CLIFF.RES" then
        sX = 25
        sXstart = 25
        sY = 200
        sXinc = 75
        sYinc = 75
        sXlimit = 750
        showAll = true
    elseif ucase$(right$(f$,7)) = "FRE.RES" then
        sX = 5
        sXstart = 5
        sY = 160
        sXinc = 72
        sYinc = 72
        sXlimit = 750
        showAll = true
    elseif ucase$(right$(f$,7)) = "LAY.RES" then
        sX = 5
        sXstart = 5
        sY = 160
        sXinc = 72
        sYinc = 72
        sXlimit = 750
        showAll = true
    elseif ucase$(right$(f$,8)) = "TREE.RES" then
        sX = 50
        sXstart = 50
        sY = 250
        sXinc = 100
        sYinc = 100
        sXlimit = 750
        showAll = true
    elseif ucase$(right$(f$,8)) = "PONT.RES" then
        sX = 50
        sXstart = 50
        sY = 250
        sXinc = 100
        sYinc = 100
        sXlimit = 750
        showAll = true
    elseif ucase$(right$(f$,9)) = "ZABOR.RES" then
        sX = 50
        sXstart = 50
        sY = 250
        sXinc = 100
        sYinc = 100
        sXlimit = 750
        showAll = true
    elseif ucase$(right$(f$,7)) = "VOR.RES" then
        sX = 50
        sXstart = 50
        sY = 250
        sXinc = 100
        sYinc = 100
        sXlimit = 750
        showAll = true
    else
        sX = 50
        sXstart = 50
        sY = 200
        sXinc = 75
        sYinc = 75
        sXlimit = 750
        showAll = false
    end if
' PCK file format ...
'    starts with a series of addresses of data type integer / long - 4 bytes.
'    each address is the start of data for an image.
'    the last address is the size of the file.
    while (imEnd < file.size) 

   ' position to offset ...
        file.position = ptrWork
     
        file.read(imStart)
   ' save offset for next image
        ptrWork = file.position
         
   ' get start of next image for use as end of this image
        file.read(imEnd)
   ' image ...
            
        pfStart(images) = imStart
        pfEnd(images) = imEnd
    ' text display ...
        listbox1.additems "image " & str$(images)
        
        if ucase$(right$(f$,8)) = "TREE.RES" then
    ' extra stuff ...
            File2.Open(f$, fmOpenRead)
            file2.position = imStart     
            h$ = ""
            file2.read(ptrWork2)
            h$ = h$ & hex$(ptrWork2) & " " 
            file2.read(ptrWork2)
            h$ = h$ & hex$(ptrWork2) & " " 
            file2.read(ptrWork2)
            h$ = h$ & hex$(ptrWork2) & " " 
            file2.read(wb2)
            h$ = h$ & right$(hex$(wb2),2) & " " 
            
            file2.read(ptrWork2)
            h$ = h$ & hex$(ptrWork2) & " " 
            listbox1.additems h$
            doevents
            ' 
            imEnd2 = imStart + ptrWork2            
            h$ = ""
            h$ = h$ & hex$(imEnd2) & " < end 2 " 
            listbox1.additems h$
            h$ = ""
            doevents
            ' 
            h$ = ""
            file2.read(ptrWork2)
            h$ = h$ & hex$(ptrWork2) & " " 

            file2.read(ptrWork2)
            h$ = h$ & hex$(ptrWork2) & " " 
            if ptrWork2 <> 0 then imEnd3 = imStart + ptrWork2
            
            file2.read(ptrWork2)
            h$ = h$ & hex$(ptrWork2) & " " 
            listbox1.additems h$
            
            if ptrWork2 <> 0 then imEnd3 = imStart + ptrWork2
            h$ = ""
            h$ = h$ & hex$(imEnd3) & " < end 3 " 
            listbox1.additems h$
            h$ = ""
            doevents
            
'            file2.read(wb)
'            h$ = h$ & right$(hex$(wb2),2) & " " 
'            file2.read(wb)
'            h$ = h$ & right$(hex$(wb2),2) & " " 

            listbox1.additems h$
            h$ = ""
            listbox1.additems h$
            h$ = ""
            imStart = file2.position
            call pckImage(f$, imStart, imEnd2)
            call drawImage(f$, imStart, imEnd2, sX, sY, 1, images)
            
            call pckImage(f$, imEnd2, imEnd3)
            call drawImage(f$, imEnd2, imEnd3, sX, sY, 1, images)
            
            call pckImage(f$, imEnd3, imEnd)
            call drawImage(f$, imEnd3, imEnd, sX, sY, 1, images)
            file2.close
            
        else 
            if (images < 11) or (showAll = true) then call pckImage(f$, imStart, imEnd)
            doevents
        
    ' image display
'        if sX < sXlimit then 
            if (images < 11) or (showAll = true) then 
                if (showDouble.checked = false) then  
                    call drawImage(f$, imStart, imEnd, sX, sY, 1, images)
                    if (ucase$(right$(f$,8)) = "ICON.RES") then         
                        call drawMyImage(f$, imStart, imEnd, -1, -1, 1, images)         
                        bf = "ICON" & right$("000" & str$(images),3) & ".bmp"
                        bm.copyrect(dest, myImage, src)
                        bm.saveToFile(bf)
                        doevents
                    end if
                else
                    call drawImage(f$, imStart, imEnd, sX, sY, 2, images)         
                end if
            end if
        end if
                
            if imStart <> imEnd then sx = sx + sXinc
'            if sX > sXlimit then
            if sX > (myCanvas.width - sXinc) then
                sX = sXstart
                sY = sY + sYinc
            end if
'        end if
        
        images = images + 1
        
    wend
          
    file.close
    listbox1.insertItem(0, "  --   " & str$(images - 1) & " images -- " & str$(problemImages) & " problems")
    listbox1.color = &Hffffff
    filelistbox1.color = &Hffffff
    panel0.enabled = true
    panel1.enabled = true
end sub

sub pckImage(f$ as string, startOfImage as long, endOfImage as long)
    DIM File AS QFileStream
    dim wHshift as short
    dim wVshift as short
    dim wWidth as short
    dim wHeight as short
    dim ptrWork as long
    dim wb as byte
    dim iLength as word
    dim iData as byte
    dim iPixel as byte
    dim a as word
    dim b as word
    dim formXoffset as word
    dim formYoffset as word
    dim pixSize as word
    dim x as integer : dim y as integer : dim h$ as string : dim l$ as string
    
    File.Open(f$, fmOpenRead)
    file.position = startOfImage     

    listbox1.additems "  image offset " & hex$(startOfImage) & " " & hex$(endOfImage)

    if ucase$(right$(f$,9)) = "CLIFF.RES" then
    ' extra stuff ...
        file.read(ptrWork)
        file.read(ptrWork)
    end if
    
    if ucase$(right$(f$,9)) = "ZABOR.RES" then
    ' extra stuff ...
        file.read(ptrWork)
    end if

'    if ucase$(right$(f$,8)) = "TREE.RES" then
'    ' extra stuff ...
'        h$ = ""
'        file.read(ptrWork)
'        h$ = h$ & hex$(ptrWork) & " " 
'        file.read(ptrWork)
'        h$ = h$ & hex$(ptrWork) & " " 
'        file.read(ptrWork)
'        h$ = h$ & hex$(ptrWork) & " " 
'        file.read(ptrWork)
'        h$ = h$ & hex$(ptrWork) & " " 
'        listbox1.additems h$
'        h$ = ""
'        file.read(ptrWork)
'        h$ = h$ & hex$(ptrWork) & " " 
'        file.read(ptrWork)
'        h$ = h$ & hex$(ptrWork) & " " 
'        file.read(ptrWork)
'        h$ = h$ & hex$(ptrWork) & " " 
'        file.read(wb)
'        h$ = h$ & hex$(wb) & " " 
'        listbox1.additems h$
'        h$ = ""
'        listbox1.additems h$
'        h$ = ""
'    end if

    if ucase$(right$(f$,9)) = "STAND.RES" then
    ' extra stuff ...
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(ptrWork)
        file.read(wb)
    end if

    if ucase$(right$(f$,7)) = "LAY.RES" then
    ' extra stuff ...
        file.read(ptrWork)
        file.read(wb)
    end if

    file.read(wHshift)
    listbox1.additems "    horizontal shift " & right$("00000000" & hex$(wHshift),4) & right$("      " & str$(wHshift),5)

    file.read(wVshift)
    listbox1.additems "      vertical shift " & right$("00000000" & hex$(wVshift),4) & right$("      " & str$(wVshift),5)

    file.read(wWidth)
    listbox1.additems "               width " & right$("00000000" & hex$(wWidth),4) & right$("      " & str$(wWidth),5)

    file.read(wHeight)
    listbox1.additems "              height " & right$("00000000" & hex$(wHeight),4) & right$("      " & str$(wHeight),5)

    file.read(wb)
    listbox1.additems "           type code   " & right$("00000000" & hex$(wb),2) & right$("      " & str$(wb),5)

    listbox1.visible = true
    
    y = 1
' work on image line
    ptrWork = file.position
    while ptrWork < endOfImage
    h$ = ""
    x = 1
        
        file.read(iLength)
        h$ = h$ & "    " & right$("00000000" & hex$(iLength),4) & "  " 
        listbox1.additems " line " & str$(y) & " data " & str$(iLength) & " @ " & hex$(file.position - 2)
        ptrWork = file.position + iLength 
        l$ = ""
        
        if ptrWork > endOfImage then
            listbox1.additems " bad stuff "
            file.position = file.position - 2
            h$ = ""
            while file.position < endOfImage 
                file.read(iData)
                h$ = h$ & right$("00000000" & hex$(iData),2) & " " 
                if len(h$) > 47 then
                    listbox1.additems h$
                    h$ = ""
                end if
            wend    
            listbox1.additems h$
            listbox1.additems "    ------------    "
            listbox1.additems ""
            exit sub
        end if
        
        ' iLength has number of bytes in line
        ' positioned at line data
        while file.position < ptrWork
            ' image data
'    listbox1.additems " pixel data "
            file.read(iData)
            h$ = h$ & right$("00000000" & hex$(iData),2) & " " 
            if wb = 162 then
            ' normal ...
                if iData > &HC0 then
                ' empty pixels for iData - 192
                    for b = 1 to iData - 192
                        l$ = l$ & "_"
                        x = x + 1
                    next b
                    h$ = h$ & ". " 
                elseif iData > &H80 then
                ' repeated data for iData - 128
                    file.read(iPixel)
                    h$ = h$ & right$("00000000" & hex$(iPixel),2) & " " 
                    for b = 1 to iData - 128
                        l$ = l$ & "R"
                        x = x + 1
                    next b
                    h$ = h$ & ". " 
                elseif iData > &H40 then
                ' soft drawn
                    for b = 1 to iData - 64
                        file.read(iPixel)
                        h$ = h$ & right$("00000000" & hex$(iPixel),2) & " " 
                        l$ = l$ & "S"
                        x = x + 1
                    next b
                    h$ = h$ & ". " 
                else
                ' normal
                    for b = 1 to iData 
                        file.read(iPixel)
                        h$ = h$ & right$("00000000" & hex$(iPixel),2) & " " 
                        l$ = l$ & "N"
                        x = x + 1
                    next b
                    h$ = h$ & ". " 
                end if
            elseif wb = 163 then
' shadows ??? ...
                if iData > &H80 then
                ' empty pixels for iData - 192
                    for b = 1 to iData - 128
                        l$ = l$ & "_"
                        x = x + 1
                    next b
                    h$ = h$ & ". " 
                else
                ' normal shadow
                    for b = 1 to iData 
                        l$ = l$ & "S"
                        x = x + 1
                    next b
                    h$ = h$ & ". " 
                end if
            else
            end if
        wend    
        listbox1.additems h$
    y = y + 1
    wend
    
    listbox1.additems ""     
    file.close
    
end sub

'========================

sub cvtCol (ssCol as word, BYREF r as word, BYREF g as word, BYREF b as word, BYREF bt as word)
' 5 6 5 format
     r = (ssCol and &HF800) / 2048
     g = (ssCol and &H07E0) / 32
     b = ssCol and &H001F
     bt = 0
end sub

function winCol (r as word, g as word, b as word, bt as word) as long
dim redX as word : dim greenX as word : dim blueX as word
dim redV as word : dim greenV as word : dim blueV as word
' these factors seem to convert SuSt colors to windows colors pretty well ...
' verion 046 and version 130 and later
    redX = 12 : greenX = 6 : blueX = 12 
' testing with 8 4 8 thru version 125
'    redX = 8 : greenX = 4 : blueX = 8 
    blueV = b * blueX 
    greenV = g * greenX 
    redV = r * redX 
    
' 2003/07/17
'    blueV = blueV + 7 
'    greenV = greenV + 7 
'    redV = redV + 7 
    
    if blueV > 255 then blueV = 255
    if greenV > 255 then greenV = 255
    if redV > 255 then redV = 255
    
    winCol = (blueV * &H010000) + (greenV * &H000100) + (redV)
end function

function winToSuSt (w as integer) as word
dim redX as word : dim greenX as word : dim blueX as word
dim redV as word : dim greenV as word : dim blueV as word
dim r as word : dim g as word : dim b as word
' no color reduction ... 
    r = (w and &H0000ff) 
    g = (w and &H00ff00) / &H000100
    b = (w and &Hff0000) / &H010000 

' reduce colors 
    r = r and glbColRed 
    g = g and glbColRed
    b = b and glbColRed  
    
' these factors seem to convert SuSt colors to windows colors pretty well ...
' verion 046 and version 130 and later
    redX = 12 : greenX = 6 : blueX = 12 
' testing with 8 4 8 thru version 125
'    redX = 8 : greenX = 4 : blueX = 8 
    blueV = b / blueX     
    greenV = g / greenX 
    redV = r / redX 

    if blueV > 255 then blueV = 255
    if greenV > 255 then greenV = 255
    if redV > 255 then redV = 255
    winToSuSt = (blueV) + (greenV * &H0020) + (redV * &H0800)
end function

function sustToWin (w as integer) as integer
dim redX as word : dim greenX as word : dim blueX as word
dim redV as word : dim greenV as word : dim blueV as word
dim r as word : dim g as word : dim b as word
    r = (w and &Hf800) / &H0800
    g = (w and &H07e0) / &H0020
    b = (w and &H001f)  
' these factors seem to convert SuSt colors to windows colors pretty well ...
' verion 046 and version 130 and later
    redX = 12 : greenX = 6 : blueX = 12 
' testing with 8 4 8 thru version 125
'    redX = 8 : greenX = 4 : blueX = 8 
    blueV = b * blueX 
    greenV = g * greenX 
    redV = r * redX 
    
'    blueV = blueV + 7 
'    greenV = greenV + 7 
'    redV = redV + 7 
    
    if blueV > 255 then blueV = 255
    if greenV > 255 then greenV = 255
    if redV > 255 then redV = 255
    sustToWin = (blueV * &H010000) + (greenV * &H000100) + (redV)
end function

sub colFile (f$ as string)
    DIM File AS QFileStream
    dim w1 as word
    dim w2 as word
    dim i1 as integer
    dim redV as word
    dim greenV as word
    dim blueV as word
    dim bt as word
    dim fc as word
    dim r as word : dim g as word : dim b as word
    

    File.Open(f$, fmOpenRead)
'    listbox1.clear
    palName.caption = f$
    palSize.caption = str$(file.size)

'    listbox1.additems "palette file: "  & f$
'    listbox1.additems "  size: " & str$(file.size)
    
    if file.size = 512 then 
    ' assume it's standard 16 palette ...
        fc = 0
        while not file.eof
            file.read(w1)
    ' store it as it comes from file 
            colValue(fc) = w1
    ' convert to Windows color and store it 
            call cvtCol (colValue(fc), @redV, @greenV, @blueV, @bt)
            WINcolValue(fc) = winCol (redV, greenV, blueV, bt)
            glbColMax = fc
            fc = fc + 1
        wend
    elseif file.size = 1024 then
    ' assume it's 24 bit stored as 4 bytes ...
        fc = 0
        while not file.eof

' 24 bit ???
'            file.read(i1)
'            listbox1.additems hex$(i1)

' from mzach --- two values - first colors, second brightness(?)
            file.read(w1)
            file.read(w2)
            listbox1.additems hex$(w1) & " " & hex$(w2)
    ' store it as it comes from file 
            colValue(fc) = w1
    ' convert to Windows color and store it 
            call cvtCol (colValue(fc), @redV, @greenV, @blueV, @bt)
            WINcolValue(fc) = winCol (redV, greenV, blueV, bt)
            glbColMax = fc
            fc = fc + 1

' in file as rbg ???
'            g = (l1 and &H0000ff) 
'            b = (l1 and &H00ff00) / &H000100
'            r = (l1 and &Hff0000) / &H010000 
' in file as rgb ???
'            b = (i1 and &H0000ff) 
'            g = (i1 and &H00ff00) / &H000100
'            r = (i1 and &Hff0000) / &H010000 
' in file as bgr ???
'            r = (i1 and &H0000ff) 
'            g = (i1 and &H00ff00) / &H000100
'            b = (i1 and &Hff0000) / &H010000 
' in file as brg ???
'            g = (i1 and &H0000ff) 
'            r = (i1 and &H00ff00) / &H000100
'            b = (i1 and &Hff0000) / &H010000 
' in file as gbr ???
'            r = (i1 and &H0000ff) 
'            b = (i1 and &H00ff00) / &H000100
'            g = (i1 and &Hff0000) / &H010000 
' in file as grb ???
'            b = (i1 and &H0000ff) 
'            r = (i1 and &H00ff00) / &H000100
'            g = (i1 and &Hff0000) / &H010000 
'
'            WINcolValue(fc) = (b * &H010000) + (g * &H000100) + (r)
'            glbColMax = fc
'            fc = fc + 1
        wend
    else
    ' not sure ...
        for fc = 0 to 255
            WINcolValue(fc) = form.color
        next fc
    end if
    
    file.close
    call drawPalette
end sub

sub addToSustPal (wc as integer)
dim c as integer : dim sc as word
dim cs as string
    sc = winToSust(wc)
    cs = "-"
    if sustPalPtr > 0 then
        for c = 1 to sustPalPtr
            if sustPal(c) = sc then
                cs = ""
                sustPalCount(c) = sustPalCount(c) + 1
            end if
        next c
    end if
    if cs <> "" then
    ' not in palette already
        sustPalPtr = sustPalPtr + 1
        sustPal(sustPalPtr) = sc
        sustPalCount(sustPalPtr) = 1
    end if
end sub

sub drawPalette
    dim a as word : dim b as word
    dim x as word
    dim y as word
    dim redV as word
    dim greenV as word
    dim blueV as word
    dim red%
    dim green%
    dim blue%
    dim bt as word

'    listbox1.clear
    for a = 0 to glbColMax
        y = a \ 16
        x = a - (y * 16)
        colPalette.fillrect ((x * 20), (y * 20),(x * 20) + 20, (y * 20) + 20, WINcolValue(a))
    next a
    if glbColMax < 255 then
        for a = glbColMax + 1 to 255
            y = a \ 16
            x = a - (y * 16)
            colPalette.fillrect ((x * 20), (y * 20),(x * 20) + 20, (y * 20) + 20, form.color)
        next a
    end if
end sub

'----------------------------------------------

sub writeTestPattern()
DIM File AS QFileStream
dim a as integer : dim b as integer : dim c as integer
dim wHshift as short
dim wVshift as short
dim wWidth as short
dim wHeight as short
dim wb as byte
dim cs as word
dim currentColor as integer : dim transparent as integer
    dim bytes(1000) as byte
    dim bytePtr as word
    dim palCol as byte
    dim lastTypePtr as word
    dim lastColorPtr as word
    dim wbx as byte
    dim wby as byte
    
    File.Open("Qtst.pcq", fmCreate)
    wHshift = -32
    wVshift = -32
    wWidth = 64
    wHeight = 64    
    wb = 162
    ' normal image
    file.write(wHshift)
    file.write(wVshift)
    file.write(wWidth)
    file.write(wHeight)
    file.write(wb)
' want to put out 4 x 4 pixel blocks in 16 x 16 pattern
    bytePtr = 0
    palCol = 0
    for wby = 1 to 16 ' vertical blocks
        bytePtr = 0
        for wbx = 1 to 16 ' horizontal blocks
            bytePtr = bytePtr + 1 : bytes(bytePtr) = &H84    ' signal normal 4 pixels of same color
            bytePtr = bytePtr + 1 : bytes(bytePtr) = palCol
            palCol = palCol + 1
        next wbx
        for wbx = 1 to 4 
            file.write(bytePtr)
            for c = 1 to bytePtr
            ' data
                file.write(bytes(c))
            next c
        next wbx
    next wby
    file.close

    File.Open("Qtrn.pcq", fmCreate)
    wHshift = -32
    wVshift = -32
    wWidth = 64
    wHeight = 64    
    wb = 162
    ' normal image
    file.write(wHshift)
    file.write(wVshift)
    file.write(wWidth)
    file.write(wHeight)
    file.write(wb)
' want to put out 4 x 4 pixel blocks in 16 x 16 pattern
    bytePtr = 0
    palCol = 0
    for wby = 1 to 16 ' vertical blocks
        bytePtr = 0
        for wbx = 1 to 16 ' horizontal blocks
            bytePtr = bytePtr + 1 : bytes(bytePtr) = &H44    ' signal transparent 8 pixels of same color
            bytePtr = bytePtr + 1 : bytes(bytePtr) = palCol
            bytePtr = bytePtr + 1 : bytes(bytePtr) = palCol
            bytePtr = bytePtr + 1 : bytes(bytePtr) = palCol
            bytePtr = bytePtr + 1 : bytes(bytePtr) = palCol
            palCol = palCol + 1
        next wbx
        for wbx = 1 to 4 
            file.write(bytePtr)
            for c = 1 to bytePtr
            ' data
                file.write(bytes(c))
            next c
        next wbx
    next wby
    file.close
    
' r = (w and &Hf800) / &H0800
' g = (w and &H07e0) / &H0020
' b = (w and &H001f)  

    File.Open("test.COL", fmCreate)
    ' reds
    for c = 0 to 31
        cs = c * &H0800
        file.write(cs)
    next c
    ' greens
    for c = 0 to 63
        cs = c * &H0020
        file.write(cs)
    next c
    ' blues
    for c = 0 to 31
        cs = c
        file.write(cs)
    next c
    ' yellows
    for c = 0 to 31
        cs = c * &H0820
        file.write(cs)
    next c
    
    ' yellows
    for c = 0 to 31
        cs = c * &H0840
        file.write(cs)
    next c
    
    for c = 0 to 31
        cs = c * &H0841
        file.write(cs)
    next c
    
    for c = 0 to 15
        cs = c * &H0821
        file.write(cs)
    next c
    for c = 0 to 15
        cs = c * &H0821
        file.write(cs)
    next c
    file.close
    
end sub
