; DECOUPEUR D'IMAGE AUTO
; Par Ar-S : 01/04/2017 
; Compilé en PB 5.60 x64



UsePNGImageDecoder()
UseJPEGImageDecoder()
UseTIFFImageDecoder()
UseJPEGImageEncoder()

Declare Open()
Declare Title(mess1.s,mess2.s)
Declare Affichage(IMG)
Declare AddCoordMin(NumPhoto, CoorX1, CoorX2, CoorY1, CoorY2, LeNom.s)
Declare SaveImageDD()
; Declare ListFichier()
Declare ClicLeft()
Declare DropFichier()
Declare Exit()

Enumeration
  #WIN
  #CANV
  #IMG_ORI
  #IMG_MIN
  #FONTtitle
  #FONTtitle2
  #BT_SAVE
  #IM1
  #IM2
  #IM3
  #IM4
EndEnumeration

Global.i x1,x2,x3,x4,y1,y2,y3,y4, Col
Global Nx1, Nx2, Ny1, Ny2
Global Tx1, Ty1, L1, L2, H1, H2, Clic
Global IMG2SHOW
Global L = 400, H = 400, MX, MY, DimL, DimH, NewL, NewH
Global.b OK
Global Fichier$,Name$,Path$,Ext$
Global.b Compteur, STOP
LoadFont(#FONTtitle, "Consolas", 20, #PB_Font_HighQuality)
LoadFont(#FONTtitle2, "Consolas", 14, #PB_Font_HighQuality)

STOP = #False 

Structure Photo
  Num.i
  x1.i
  y1.i
  x2.i
  y2.i
  name.s
  Crop.i
EndStructure

Global NewList photo.photo()


Procedure AddCoordMin(NumPhoto, CoorX1, CoorX2, CoorY1, CoorY2, LeNom.s)
  
  StartDrawing(CanvasOutput(#CAN))
  LineXY(CoorX1, CoorY1, CoorX2, CoorY1, #Red)
  LineXY(CoorX2, CoorY1, CoorX2, CoorY2, #Red)
  LineXY(CoorX2, CoorY2, CoorX1, CoorY2, #Red)
  LineXY(CoorX1, CoorY2, CoorX1, CoorY1, #Red)
  StopDrawing()
  
  
  AddElement( Photo() )
  Photo()\Num = NumPhoto
  Photo()\x1 = CoorX1
  Photo()\x2 = CoorX2
  Photo()\y1 = CoorY1
  Photo()\y2 = CoorY2
  Photo()\name = Lenom
  
  ;- TESTTTTTT ///////////////////////
  Debug "Photo :"+Str(Photo()\Num) + " ajoutée à la liste"
  Debug "Coord x1: " +Str(Photo()\x1) + " ----------" + "x2:"+ Str(Photo()\x2)
  Debug "Coord y1: " +Str(Photo()\y1) + " ----------" + "y2:"+ Str(Photo()\y2)
  ;/TESTTTTTTT ///////////////////////

EndProcedure


;- AFFICHAGE DU TITRE
Procedure Title(Mess1.s, Mess2.s)
  
  StartDrawing(CanvasOutput(#CAN))
  DrawingFont(FontID(#FONTtitle))
  L1 = TextWidth(Mess1) : L2 = TextWidth(Mess2)
  If L1 > GadgetWidth(#CAN) Or L2 > GadgetWidth(#CAN)
    DrawingFont(FontID(#FONTtitle2))
    L1 = TextWidth(Mess1) : L2 = TextWidth(Mess2)
  Else
    DrawingFont(FontID(#FONTtitle))
    L1 = TextWidth(Mess1) : L2 = TextWidth(Mess2)
  EndIf 
  
  H1 = TextHeight(Mess1) : H2 = TextHeight(Mess2)
  Tx1 = (GadgetWidth(#CAN)/2) - (L1/2)  : Tx2 = (GadgetWidth(#CAN)/2) - (L2/2)
  Ty1 = (GadgetHeight(#CAN)/2) - H1*2  : Ty2 = (GadgetHeight(#CAN)/2) 
  
  Box(0,0,WindowWidth(#Win),WindowHeight(#Win),$FFFFFF)
  DrawText(Tx1, Ty1, Mess1, $0, $FFFFFF)
  DrawText(Tx2, Ty2, Mess2, $0, $FFFFFF)
  
  StopDrawing()  
  ClearList(Photo())
  If IsImage(#IMG_ORI)
    FreeImage(#IMG_ORI)  
  EndIf
  If IsImage(#IMG_MIN)
    FreeImage(#IMG_MIN)  
  EndIf
  CLIC = 0
EndProcedure

;- SELECTION DES IMAGES
Procedure ClicLeft()
  MX = GetGadgetAttribute(#CAN, #PB_Canvas_MouseX)
  MY = GetGadgetAttribute(#CAN, #PB_Canvas_MouseY)
  
  If OK = #True
    CLIC + 1
    Debug "CLIC " +clic
    If Clic > 4
      Clic = 0
      StartDrawing(CanvasOutput(#CAN))
      DrawImage( ImageID(IMG2SHOW), 0, 0 )
      StopDrawing()
      ClearList(Photo()) 
    Else
      
      Select CLIC
        Case 1 ; Image Haut Gauche
          NewName$ = Path$+Name$+"_"+Str(CLIC)+".jpg"
          If NewName$ <> ""
            AddCoordMin(#IM1, 0, Mx, 0, MY, NewName$)
            
            StartDrawing(ImageOutput(#IMG_ORI))
            ; EXTRACTION DE L'IMAGE
            
            SelectElement(Photo(), CLIC-1)
            If IMG2SHOW = #IMG_MIN
              GrabDrawingImage(Photo()\NuM, 0, 0, Mx*8, My*8)
                Debug "image grabée a partie ORI"
              
            Else  
              GrabDrawingImage(Photo()\NuM, photo()\x1, photo()\y1, photo()\x2-photo()\x1, photo()\y2-photo()\y1)
                Debug "image grabée a partir MIN"
                   
            EndIf
            
            ;- TESTTTTTT ///////////////////////
            Debug "IMAGE 1 : " + Str( ImageWidth(Photo()\NuM) ) + " x " + Str( ImageHeight(Photo()\NuM) )
            ; /TESTTTTTT ///////////////////////
            StopDrawing()
          EndIf
          
        Case 2 ; Image Haut Droite
          NewName$ = Path$+Name$+"_"+Str(CLIC)+".jpg"
          If NewName$ <> ""
            AddCoordMin(#IM2, Mx, DimL-1, 0, MY, NewName$) 
            
              ; EXTRACTION DE L'IMAGE
            StartDrawing(ImageOutput(#IMG_ORI))
            
            SelectElement(Photo(), CLIC-1)
            If IMG2SHOW = #IMG_MIN
              If GrabDrawingImage(Photo()\NuM, Mx*8, 1, (DimL-1)-Mx*8, My*8)
                Debug "image grabée a partie ORI"
              EndIf
              
            Else  
              If GrabDrawingImage(Photo()\NuM, Mx, 1, (NewL-1)-Mx, My)
                Debug "image grabée a partir MIN"
              EndIf
              
            EndIf
       
            ;- TESTTTTTT ///////////////////////
            Debug "IMAGE 2 : " + Str( ImageWidth(Photo()\NuM) ) + " x " + Str( ImageHeight(Photo()\NuM) )
            ; /TESTTTTTT ///////////////////////
          EndIf
          
        Case 3 ; Image Bas Droite
          NewName$ = Path$+Name$+"_"+Str(CLIC)+".jpg"
          If NewName$ <> ""
          AddCoordMin(#IM3, MX, DimL-1, MY, DimH-1, NewName$)
          
          StartDrawing(ImageOutput(#IMG_ORI))
          ; EXTRACTION DE L'IMAGE
          
          If IMG2SHOW = #IMG_MIN
            If GrabDrawingImage(Photo()\NuM, Mx*8, My*8, (DimL-1)-Mx*8, (DimH-1)-My*8)
              Debug "image grabée a partir ORI"
            EndIf
            
          Else  
            If GrabDrawingImage(Photo()\NuM, Mx, My, (NewL-1)-Mx, (NewH-1)-My)
              Debug "image grabée a partir MIN"
            EndIf
            
          EndIf
          
          Debug "IMAGE 3 : " + Str( ImageWidth(Photo()\NuM) ) + " x " + Str( ImageHeight(Photo()\NuM) )
          
          StopDrawing()
          EndIf
          
        Case 4  ; Image Bas Gauche
          NewName$ = Path$+Name$+"_"+Str(CLIC)+".jpg"
          If NewName$ <> ""
          AddCoordMin(#IM4, 0, MX, MY, DimH-1, NewName$)
          
          StartDrawing(ImageOutput(#IMG_ORI))
          ; EXTRACTION DE L'IMAGE
          
          If IMG2SHOW = #IMG_MIN
            If GrabDrawingImage(Photo()\NuM, 0, My*8, Mx*8, (DimH-1) - My*8)
              Debug "image grabée a partie ORI"
            EndIf
            
          Else  
            If GrabDrawingImage(Photo()\NuM, 0, My, Mx, (newH-1) - My)
              Debug "image grabée a partir MIN"
            EndIf
            
          EndIf
          
          Debug "IMAGE 4 : " + Str( ImageWidth(Photo()\NuM) ) + " x " + Str( ImageHeight(Photo()\NuM) )
          
          StopDrawing()
        EndIf
        
      EndSelect
      
    EndIf
    
  EndIf  
EndProcedure

;- AFFICHAGE DE l'IMAGE MIN
Procedure Affichage(IMG)
  CopyImage (#IMG_ORI, #IMG_MIN)
  DimL = ImageWidth(IMG)
  DimH = ImageHeight(IMG)
  NewL = DimL/8
  NewH = DimH/8

  If Compteur = 0
    Debug "Compteur : "+ Compteur
    If DIML > 800 Or DIMH > 800
      ResizeWindow(#Win,#PB_Ignore, #PB_Ignore, NewL, NewH)
      ResizeGadget(#CAN,0,0,NewL, NewH)
      ResizeImage(#IMG_MIN,NewL, NewH)
      IMG2SHOW = #IMG_MIN
    Else
      ResizeWindow(#Win,#PB_Ignore, #PB_Ignore, DimL, DimH)
      ResizeGadget(#CAN,0,0,DimL, DimH)
      IMG2SHOW = #IMG_ORI
    EndIf
    
    ; On Affiche dans le canvas
    StartDrawing(CanvasOutput(#CAN))
    DrawImage(ImageID(IMG2SHOW),0,0)
    StopDrawing()
    Compteur + 1
  EndIf

EndProcedure


;- SAUVEGARDE DES IMAGES SELECTIONNEES
Procedure SaveImageDD()
           ; SAUVEGARDE DE L'IMAGE
           ;
  If CLIC > 0
    ForEach Photo()
      Debug Photo()\Num
      SAUVE = SaveImage(photo()\Num, photo()\name, #PB_ImagePlugin_JPEG, 8, 32)
      If SAUVE
        Debug "Image "+Str( ListIndex(Photo())+1) + " sauvée"
        FreeImage(photo()\Num)
      EndIf
    Next
    TITLE("OPERATION TERMINEE","VOUS POUVEZ POURSUIVRE")
  EndIf
EndProcedure

Procedure DropFichier()
  Debug "Dépose de fichier"
  SetActiveWindow(#Win)
  ext$  = GetExtensionPart(EventDropFiles())
  Debug ext$
  If LCase(ext$) = "tif" Or LCase(ext$) = "png" Or LCase(ext$) = "jpg" Or LCase(ext$) = "bmp"
    
    Fichier$ = EventDropFiles()
    Debug "Drop file: " + Fichier$
    Name$ = GetFilePart(Fichier$, #PB_FileSystem_NoExtension)
    Path$ = GetPathPart(Fichier$)
    
    If LoadImage(#IMG_ORI, Fichier$) 
      
      Debug "Image chargée ! " + " ligne " + Str(#PB_Compiler_Line)
      
      ;-/// AFFICHAGE DE LA MINIATURE //////////////////////
      
      Affichage(#IMG_ORI)
;       Title("GLISSER / DEPOSER","VOTRE IMAGE ICI")
      
;       StartDrawing(CanvasOutput(#CAN))
;       DrawingFont(FontID(#FONTtitle))
;       Txt1.s = "GLISSER / DEPOSER"
;       Txt2.s = 
;       L1 = TextWidth(Txt1) : L2 = TextWidth(Txt2)
;       H1 = TextHeight(Txt1) : H2 = TextHeight(Txt2)
;       Tx1 = (GadgetWidth(#CAN)/2) - (L1/2)  : Tx2 = (GadgetWidth(#CAN)/2) - (L2/2)
;       Ty1 = (GadgetHeight(#CAN)/2) - H1*2  : Ty2 = (GadgetHeight(#CAN)/2) 
;       StopDrawing()
      Compteur = 0
      ; /////////////////////////////////////////////////
      OK = #True
    Else
      Debug "erreur de lecture"
      OK = #False
    EndIf
    
    
  Else
    HideWindow(#Win,1)
    MessageRequester("Erreur","Votre image doit être au format JPG,PNG,TIF",#MB_ICONEXCLAMATION)
    Debug ext$ + " refusée"
    OK = #False
    HideWindow(#Win,0)
    STOP = #True
  EndIf  
EndProcedure

Procedure Exit()
  End
EndProcedure


Procedure Open()
  
  If OpenWindow(#WIN, #PB_Ignore, #PB_Ignore, L, H, "(c) LDVMULTIMEDIA - MAGIC X4 CROPPER v0.7", #PB_Window_Tool|#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
    StickyWindow(#Win,1)
    CanvasGadget(#CAN,0,0,L,H,#PB_Canvas_ClipMouse)
    SetGadgetAttribute(#CAN,#PB_Canvas_Cursor,#PB_Cursor_Cross)

    TITLE( "GLISSER / DEPOSER","VOTRE IMAGE ICI")
    
    OK = #False
    EnableGadgetDrop(#CAN, #PB_Drop_Files, #PB_Drag_Copy) 
    
    
    BindGadgetEvent(#CAN,@SaveImageDD(),#PB_EventType_RightButtonDown)
    BindGadgetEvent(#CAN,@ClicLeft(),#PB_EventType_LeftButtonUp)
    BindEvent(#PB_Event_GadgetDrop,@DropFichier(),#Win)
    BindEvent(#PB_Event_CloseWindow,@Exit(),#Win)
    
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
    End
    
  EndIf
  
  
EndProcedure




Open()



; IDE Options = PureBasic 5.60 (Windows - x64)
; CursorPosition = 185
; FirstLine = 99
; Folding = 0-
; EnableThread
; EnableXP
; UseIcon = ico1.ico
; Executable = D:\Desktop\Magic_X4_Cropper_Guigard.exe
; Compiler = PureBasic 5.60 (Windows - x64)
; EnableCompileCount = 346
; EnableBuildCount = 18
; IncludeVersionInfo
; VersionField0 = 0.7.0.0
; VersionField1 = 0.7.0.0
; VersionField2 = LDV MULTIMEDIA
; VersionField3 = Magic X4 Cropper
; VersionField4 = 0.7.0.0
; VersionField5 = 0.7.0.0
; VersionField6 = A simple x4 image cropper
; VersionField7 = Magic X4 Cropper Guigard Edition
; VersionField8 = Magic X4 Cropper
; VersionField9 = LDVMULTIMEDIA
; VersionField11 = A simple x4 image cropper
; VersionField12 = By Cyrille Landivier
; VersionField13 = info@ldvmultimedia.com
; VersionField14 = http://ldvmultimedia.com
; VersionField15 = VOS_NT_WINDOWS32
; VersionField17 = 040c French (Standard)
; EnableUnicode