'
'   Test of QFIMAGE (QFImage.inc and FreeImage.dll)
'   Include code ported from VB code by StoneCold (stonecold@sctools.de)
'
'   ImageView v0.7
'       7 Feb 2010 d_homans@yahoo.com.au
'
'   ImageView v0.8 15 Jan 2013
'       Animated GIF component:
'       Added display (start/stop) an animated GIF, and split animated GIFs facility.
'       Tidy-ups and one additional function for Animated GIFs in the include file
'
$APPTYPE GUI
$TYPECHECK ON

$INCLUDE "Rapidq2.inc"
$INCLUDE "qfimage.inc"

$DEFINE MAXTOOLBTNS     16
$INCLUDE "QToolBarEx.inc"

$RESOURCE ALLButtons AS "ToolBar.bmp"

CONST CRLF=CHR$(13)+CHR$(10)

DECLARE SUB Initialise
DECLARE SUB ExitDllNotFound

DECLARE SUB LoadImage
DECLARE SUB SaveImage
DECLARE SUB FlipImageVertical
DECLARE SUB FlipImageHorizontal
DECLARE SUB RotateImage
DECLARE SUB MagnifyImage
DECLARE SUB ShowAnimatedGIF
DECLARE SUB StopAnimatedGIF
DECLARE SUB SplitAnimatedGIF
DECLARE SUB FlipTransparent
DECLARE SUB DebugMe
DECLARE SUB ShowImgInfo
DECLARE SUB ShowAbout
DECLARE SUB theEnd

DECLARE SUB ButnCmd(Idx AS integer)
DECLARE SUB SetRadBtn1(Sender AS QRADIOBUTTON)
DECLARE SUB SetRadBtn2(Sender AS QRADIOBUTTON)
DECLARE SUB CancelOperation

DIM RQImage AS QFIMAGE
DIM RQAnimGif AS QFGifImage

DIM radBtn1(1 TO 8) AS QRADIOBUTTON
DIM radBtn2(1 TO 5) AS QRADIOBUTTON
DEFSTR FileName
DEFINT OrigWidth, OrigHeight
DEFSTR OrigType
DIM Toggle

CREATE Form AS QFORM
    Caption="Image Viewer"
    Width=Screen.Width
    Height=Screen.Height-30
    OnShow=Initialise
    OnClose=theEnd
    CREATE pan1 AS QPANEL
        Left=0
        Top=0
        Width=32
        Height=Form.ClientHeight
        BorderStyle=bvLowered 
        CREATE ToolBar AS QToolBarEX
            Align=alLeft
            BtnCount=16
            Cursor=crHandPoint
            BMPHandle=0                                         ' Resource(0)
            OnSelect=ButnCmd
        END CREATE
    END CREATE
    CREATE pan2 AS QSCROLLBOX
        Left=30
        Top=0
        Width=Form.ClientWidth-32
        Height=Form.ClientHeight        
    END CREATE
    CREATE savPanel AS QPANEL
        Width=150
        Height=240
        Left=(Form.ClientWidth-savPanel.Width)\2
        Top=(Form.ClientHeight-savPanel.Height)\2
        Visible=False
        CREATE Label1 AS QLABEL
            Left=20
            Top=10
            Width=80
            Height=15
            Caption="Save File AS ..."
        END CREATE
        CREATE Cool1 AS QCOOLBTN
            Left=80
            Top=160
            Width=55
            Caption="Save"
        END CREATE
        CREATE Cool2 AS QCOOLBTN
            Left=80
            Top=200
            Width=55
            Caption="Cancel"
            OnClick=CancelOperation
        END CREATE
        CREATE GrpBox1 AS QGROUPBOX
            Left=12
            Top=30
            Width=60
            Height=196
            Caption="Format"
            Tag=0
        END CREATE
        CREATE GrpBox2 AS QGROUPBOX
            Left=75
            Top=30
            Width=70
            Height=120
            Caption="Filter"
            Tag=0
            Visible=False
        END CREATE
    END CREATE
END CREATE

CREATE menu AS QMAINMENU
    Parent=Form
    CREATE mFile AS QMENUITEM
        Caption="File"
        CREATE FLoad AS QMENUITEM
            Caption="Load Image"
            OnClick=LoadImage
        END CREATE
        CREATE Save AS QMENUITEM
            Caption="Save Image"
            OnClick=SaveImage
        END CREATE
        CREATE Quit AS QMENUITEM
            Caption="EXIT"
            OnClick=theEnd
        END CREATE
    END CREATE
    CREATE mAbout AS QMENUITEM
        Caption="About"
        CREATE mDetail AS QMENUITEM
            Caption="Image Details"
            OnClick=ShowImgInfo
        END CREATE
        CREATE mHelp AS QMENUITEM
            Caption="Info"
            OnClick=ShowAbout
        END CREATE
    END CREATE
END CREATE

Form.ShowModal

SUB Initialise
DEFINT i

    IF FileExists("FreeImage.dll")=0 THEN
        ExitDllNotFound
    END IF
    SetWindowLong(Form.Handle, -8, 0)
    SetWindowLong(Application.Handle, -8, Form.Handle)

    RQImage.Parent=pan2
    RQImage.Left=0: RQImage.Top=0
    RQImage.autosize=True: RQImage.Stretch=True
    RQImage.Transparent=False

    RQAnimGif.Parent=pan2
    RQAnimGif.Left=0: RQAnimGif.Top=0
    RQAnimGif.autosize=True: RQAnimGif.Stretch=True
    RQAnimGif.Visible=False

    ToolBar.Hint="Load image,Save image,Flip horizontal,Flip vertical,Rotate,Magnify,Resize,Crop,Colorize, _
                                Show AnimatedGIF,Split AnimatedGIF,Transparency,Debug,Image info,About,Exit"

    FOR i=1 TO 8
        radBtn1(i).Parent=GrpBox1
        radBtn1(i).Left=10
        radBtn1(i).Top=i*20
        radBtn1(i).Width=45
        radBtn1(i).Tag=i
        radBtn1(i).OnClick=SetradBtn1
    NEXT i
    FOR i=1 TO 5
        radBtn2(i).Parent=GrpBox2
        radBtn2(i).Left=6
        radBtn2(i).Top=i*20
        radBtn2(i).Width=54
        radBtn2(i).OnClick=SetradBtn2
    NEXT i
    radBtn2(1).Caption="none": radBtn2(1).Tag=FI_FILTER_PIX
    radBtn2(2).Caption="Bicubic": radBtn2(2).Tag=FI_FILTER_BICUBIC
    radBtn2(3).Caption="Bilinear": radBtn2(3).Tag=FI_FILTER_BILINEAR
    radBtn2(4).Caption="Bspline": radBtn2(4).Tag=FI_FILTER_BSPLINE
    radBtn2(5).Caption="LancZos": radBtn2(5).Tag=FI_FILTER_LANCZOS3
    radBtn2(1).Checked=True
END SUB

SUB ButnCmd(Idx)
    SELECT CASE Idx
        CASE 0: LoadImage
        CASE 1: SaveImage
        CASE 2: FlipImageHorizontal
        CASE 3: FlipImageVertical
        CASE 4: RotateImage
        CASE 5: MagnifyImage
        CASE 6: 
        CASE 7: 
        CASE 8: 
        CASE 9: ShowAnimatedGIF
        CASE 10: SplitAnimatedGIF
        CASE 11: FlipTransparent
        CASE 12: DebugMe
        CASE 13: ShowImgInfo
        CASE 14: ShowAbout
        CASE 15: theEnd
    END SELECT
END SUB

' View the loaded image (jpg,gif..whatever) as a bmp image on screen
SUB LoadImage
DIM fDialog AS QOPENDIALOG

    RQImage.Visible=True
    RQAnimGif.Visible=False
    FileName=""
    fDialog.InitialDir=Application.Path
    fDialog.Caption="Load Image File"
    fDialog.FileName=""
    fDialog.Filter="All Image Types|*.bmp;*.rle;*.dib;*.gif;*.ico;*.iff;*.lbm;*.jpg;_
                                    *.png;*.psd;*.ras;*.sgi;*.tif;*.tga;*.wmf;*.xpm"
    IF fDialog.Execute THEN
        FileName=fDialog.FileName
        FileName=RIGHT$(FileName,LEN(FileName)-RINSTR(FileName,"\"))
        IF RQImage.Handle THEN RQImage.UnloadImage
        RQImage.LoadFromFile(fDialog.FileName,FI_LOAD_PROP)
        RQImage.ShowImage(RQImage.Handle)
        OrigType=UCASE$(RIGHT$(FileName,LEN(FileName)-RINSTR(FileName,".")))
        OrigWidth=RQImage.Width: OrigHeight=RQImage.Height
    END IF
END SUB

SUB SaveFile
DEFSTR fext, fStr=FIELD$(FileName,".",1)

    fext=radBtn1(GrpBox1.Tag).Caption
' More checks here ico files? gif 256 bpp max? etc
    RQImage.SaveToFile(fStr+"."+fext,FI_SAVE_PROP)
    savPanel.Visible=False
END SUB

SUB SaveImage
DEFINT i

    Label1.Caption="Save File AS ..."
    GrpBox1.Caption="Format"
    radBtn1(1).Caption="BMP": radBtn1(2).Caption="JPG"
    radBtn1(3).Caption="GIF": radBtn1(4).Caption="PNG"
    radBtn1(5).Caption="ICO": radBtn1(6).Caption="JNG"
    radBtn1(7).Caption="TGA": radBtn1(8).Caption="TIF"
    FOR i=1 TO 8
        IF OrigType=radBtn1(i).Caption THEN
            radBtn1(i).Checked=True
            EXIT FOR
        END IF
    NEXT i
    GrpBox2.Visible=False
    Cool1.Caption="Save"
    Cool1.OnClick=SaveFile
    savPanel.Visible=True
END SUB

SUB FlipImageVertical
    RQImage.FlipVertical
END SUB

SUB FlipImageHorizontal
    RQImage.FlipHorizontal
END SUB

SUB ApplyRotate
DEFDBL rAngle

    rAngle=VAL(radBtn1(GrpBox1.Tag).Caption)
    RQImage.RotateImage(rAngle)
    savPanel.Visible=False
END SUB

SUB RotateImage
    Label1.Caption="Rotate by ..."
    GrpBox1.Caption="Angle"
    radBtn1(1).Caption="0.5": radBtn1(2).Caption="45"
    radBtn1(3).Caption="90": radBtn1(4).Caption="135"
    radBtn1(5).Caption="180": radBtn1(6).Caption="225"
    radBtn1(7).Caption="270": radBtn1(8).Caption="315"
    radBtn1(3).Checked=True
    GrpBox2.Visible=False
    Cool1.Caption="Apply"
    Cool1.OnClick=ApplyRotate
    savPanel.Visible=True
END SUB

' 100% return image to original dimensions (if changed)
SUB ApplyMagnify
DEFINT i, wid, hite
DEFSNG mult

    i=GrpBox1.Tag
    mult=VAL(radBtn1(i).Caption)/100
    SELECT CASE mult
        CASE 1.0
            IF (RQImage.Width=OrigWidth) AND (RQImage.Height=OrigHeight) THEN
                EXIT SUB
            ELSE
                RQImage.ResizeImage(OrigWidth, OrigHeight, 0)
            END IF
        CASE ELSE
            wid=RQImage.Width*mult: hite=RQImage.Height*mult
            RQImage.ResizeImage(wid, hite, GrpBox2.Tag)
    END SELECT
    RQImage.ShowImage(RQImage.Handle)
    GrpBox2.Visible=False
    savPanel.Visible=False
END SUB

SUB MagnifyImage
    Label1.Caption="Magnify by ..."
    GrpBox1.Caption="Ratio"
    radBtn1(1).Caption="10%": radBtn1(2).Caption="25%"
    radBtn1(3).Caption="33%": radBtn1(4).Caption="50%"
    radBtn1(5).Caption="75%": radBtn1(6).Caption="100%"
    radBtn1(7).Caption="150%": radBtn1(8).Caption="200%"
    radBtn1(6).Checked=True
    GrpBox2.Visible=True
    Cool1.Caption="Apply"
    Cool1.OnClick=ApplyMagnify
    savPanel.Visible=True
END SUB

'--------------------------------------------------------------------------------------------
SUB ShowImgInfo
DEFSTR tstr

    tstr="Type: "+RQImage.TypeString+CRLF+"Size: "+STR$(RQImage.size)+CRLF
    tstr=tstr+"Bpp: "+STR$(RQImage.Bpp)+CRLF+"Colors: "+STR$(RQImage.Colors)+CRLF
    tstr=tstr+"Width: "+STR$(RQImage.Width)+CRLF+"Height: "+STR$(RQImage.Height)
    MessageBox(tstr,FileName,&H40)
END SUB

SUB DebugMe
DEFINT wid, hite
DEFLNG nuHandle

    Toggle=1-Toggle                                     ' Flip
    IF Toggle THEN
        nuHandle=RQImage.ConvertImage(FI_CONV_GREY)
        IF nuHandle THEN
            RQImage.ShowImage(nuHandle)
            ShowImgInfo
        ELSE
            showmessage "Failed"
        END IF
    ELSE
        RQImage.ShowImage(RQImage.Handle)
    END IF
'    wid=100: hite=200
'    RQImage.ResizeImage(wid, hite, 0)        ' ok
'    RQImage.ShowImage(RQImage.Handle)
END SUB

SUB CancelOperation
    savPanel.Visible=False
END SUB

SUB SetradBtn1(Sender)
    GrpBox1.Tag=Sender.Tag
END SUB

SUB SetradBtn2(Sender)
    GrpBox2.Tag=Sender.Tag
END SUB

'--------------------- Some test stuff ----------------------
SUB StartAnimatedGIF
DIM fDialog AS QOPENDIALOG

    RQImage.Visible=False
    RQAnimGif.Visible=True
    fDialog.InitialDir=Application.Path
    fDialog.Caption="Load Animated GIF File"
    fDialog.FileName=""
    fDialog.Filter="Animated GIF files|*.gif"
    IF fDialog.Execute THEN
        IF RQAnimGif.IsAniGIF(fDialog.FileName) THEN
            RQAnimGif.LoadFromFile(fDialog.FileName)
            RQAnimGif.StartAnim(0)
        ELSE
            ShowMessage(fDialog.FileName+" is not an animated GIF!")
        END IF
    END IF
END SUB

' Stop and erase GIF from memory
SUB StopAnimatedGIF
    RQAnimGif.StopAnim
    RQAnimGif.UnloadImage
END SUB

SUB ShowAnimatedGIF
    IF RQAnimGif.Animate = True THEN
        StopAnimatedGIF
    ELSE
        StartAnimatedGIF
    END IF
END SUB

SUB SplitAnimatedGIF
DIM fDialog AS QOPENDIALOG

    fDialog.InitialDir=Application.Path
    fDialog.Caption="Load Animated GIF File"
    fDialog.FileName=""
    fDialog.Filter="Animated GIF files|*.gif"
    IF fDialog.Execute THEN
        IF RQAnimGif.IsAniGIF(fDialog.FileName) THEN
            RQAnimGif.LoadFromFile(fDialog.FileName)
            RQAnimGif.SaveFrames
            ShowMessage("Individual frames saved as bmp files")
        ELSE
            ShowMessage(fDialog.FileName+" is not an animated GIF!")
        END IF
    END IF
END SUB

SUB FlipTransparent
    IF RQImage.Transparent THEN
        RQImage.Transparent=False
    ELSE
        RQImage.Transparent=True
    END IF  
END SUB
'------------------------------------------------------------

SUB ShowAbout
DEFSTR tstr

    tstr="ImageView v0.8 - an unfinished demo of ..."+CRLF+CRLF
    tstr=tstr+"QFImage.inc an extension to the RapidQ QImage component"+CRLF
    tstr=tstr+"Original code v1.3 (c) July 2006 by StoneCold"+CRLF
    tstr=tstr+"Bug fixes and tidy-ups d_homans@yahoo.com.au"+CRLF+CRLF
    tstr=tstr+"Uses the FreeImage.dll library v"+FI_GetVersion+CRLF
    tstr=tstr+"Released under the GNU General Public License version 2"+CRLF
    tstr=tstr+"See http://freeimage.sourceforge.net for details."+CRLF+CRLF
    tstr=tstr+"Original dll file size (current version) is 2.3 MBytes."+CRLF
    tstr=tstr+"Using upx, the dll compacts to 885 KBytes, worth the effort"+CRLF
    tstr=tstr+"however the dll is not included in this distro"+CRLF
    MessageBox(tstr,"Image Viewer",&H40)
END SUB

SUB ExitDllNotFound
DEFSTR tstr

    tstr="FreeImage.dll not found - unable to continue!"
    MessageBox(tstr,"Image Viewer",&H10)
    Application.Terminate
END SUB

SUB theEnd
    RQImage.UnloadImage
    Application.Terminate
END SUB
    
