// Auteurs
// Matthieu Aubry
// Anthony Combe
// Licence
// Domaine publique
program main;


uses  image_unit, outils_unit, mesure_unit, comptage_unit, insapetanque_unit, interface_unit, crt;





procedure rungui();
var i:longint;
l, h, dim : string;
ch : char;
restart : boolean;
matrice : guimat;
av1, av2 : longint;

img,img2 : pTimage;
cochonnet:comp_objet;
pdeb1, pdeb2 : pliste_objets;
numero_image:string;
IMG_NOM1, IMG_NOM2, IMG_NOM3, IMG_NOM4:string;

begin
    l := '';
    h := '';
    restart := true;

    while restart do
    begin
        av1 := 0;
        av2 := 0;
        restart := false;
        
        // initialisation de la matrice
        init_matrice(@matrice);
        
        // affichage des commentaires
        addstring(3,5, 'INSA PETANQUE vous permet en toute simplicit�d''analyser', @matrice);
        addstring(3,6, 'vos photos de parties de boules ! vous pourrez ainsi savoir', @matrice);
        addstring(3,7, 'le nombre de boules mises en jeu et la distance les s�arant', @matrice);
        addstring(3,8, 'du cochonnet...', @matrice);
        
        // affichage des choix
        addstringtmp(5,12, '- [T] : Commencer le traitement', @matrice, 20);
        addstringtmp(5,13, '- [C] : Cr�its', @matrice, 20);
        addstringtmp(5,14, '- [Q] : quitter INSA P�anque', @matrice, 20);
        
        ch := '0';
        
        // boucle de lecture du clavier
        while ch<>'q' do
        begin
            print_matrice(@matrice);
            delay(30);    
            
            // appui sur une touche d�ect�
            if KeyPressed then
            begin
                // capture de la touche
                ch := Readkey;
                        
                // touche traitement
                if ch='t' then 
                begin
                
                    // initialisation du texte du traitement
                    init_matrice(@matrice);
                    addstringtmp(5,6, 'Num�o de l''image : ', @matrice, 20);
                    numero_image := readstring(25, 6, @matrice);                    
                    addstring(5,14, 'Avancement partiel :', @matrice);
                    addstring(5,17, 'Avancement total :', @matrice);        
                    addstring(5,8, 'Action en cours :', @matrice);
                    

                    // programme principal
                    
                    // fichiers cibles
                    IMG_NOM1 := 'photos/boules'+numero_image+'.ppm'; // image originale en couleurs
                    IMG_NOM2 := 'photos/boules_'+numero_image+'_lut.ppm'; // apr� seuil, rectanges autour des objets
                    IMG_NOM3 := 'photos/boules_'+numero_image+'_traits.ppm'; // final, apr� trac�des traits
                    IMG_NOM4 := 'photos/boules_'+numero_image+'_cochonnet.ppm'; // capture du cochonnet
                        
                    
                    // 
                    // OUVERTURES ET PREPARATION IMAGE SOURCE
                    //   
                    
                    // image source �analyser
                    
                    addstring(25,8, 'Ouverture de l''image ', @matrice);
                    
                    
                    // lecture de l'image et affichage des dimensions
                    img:=read_image(IMG_NOM1);                        
                    str(img^.largeur, l);
                    str(img^.hauteur, h);
                    dim := concat('Dimension : ',l,'*',h);  
                    addstring(5,9, dim, @matrice);
                    
                    
                    
                    // passage en niveau de gris
                    img:=grey_level(img);
                    
                    // seuillage
                    img:=applique_lut(img,seuillage,50,0);
                    
                    // inversion des couleurs
                    img:=reverse_color(img);
                    
                    // enregistrement tmp (indispensable car utilis�par  // lecture image seuill�)
                    for i:=25 to GUIlarg-2 do matrice[i, 8]:=' ';
                    addstring(25,8, 'Ecriture de l''image ', @matrice);
                    write_image(img, IMG_NOM2, @matrice, av1, av2, 0);
                    
                    
                    //
                    // ON CAPTURE LE COCHONNET
                    //
                    pdeb2 := init_liste();
                    
                    // lecture image source
                    for i:=25 to GUIlarg-2 do matrice[i, 8]:=' ';
                    addstring(25,8, 'Lecture de l''image ', @matrice);
                    img2:=read_image(IMG_NOM1);
                    
                    
                    for i:=25 to GUIlarg-2 do matrice[i, 8]:=' ';
                    addstring(25,8, 'Traitement de l''image source ', @matrice);
                    delay(1000);
                    // seuil sur la composante rouge pour isoler le cochonnet
                    img2 := applique_seuil_composante(img2, rouge, 230);
                    
                    // on capture les objets rouges
                    capture_objet(reverse_color(img2), pdeb2);
                    
                    // debug
                    img2:=add_rect_objets(img2, pdeb2);
                    
                    
                    for i:=25 to GUIlarg-2 do matrice[i, 8]:=' ';
                    addstring(25,8, 'Ecriture de l''image ', @matrice);
                    write_image(img2, IMG_NOM4, @matrice, av1, av2, 1);
                    
                    // on prend le plus gd objet : le cochonnet
                    cochonnet := get_cochonnet(pdeb2);
                        
                    //
                    // ON CAPTURE LES BOULES 
                    //
                    pdeb1 := init_liste();
                    
                    // lecture image seuill�
                    
                    for i:=25 to GUIlarg-2 do matrice[i, 8]:=' ';
                    addstring(25,8, 'Lecture de l''image ', @matrice);
                    img:=read_image(IMG_NOM2);
                    
                    
                    for i:=25 to GUIlarg-2 do matrice[i, 8]:=' ';
                    addstring(25,8, 'D�ection des boules ', @matrice);
                    delay(1000);
                    // on capture les objets
                    capture_objet(img, pdeb1);
                    
                    // 1�e passe, on supprime tous les objets de surface vraiment trop petite
                    supprime_objets_hors_gabarit(img, pdeb1, surface, 50, -1);
                    
                    // 2�e passe, on supprime les objets trop peu larges par rapport �la largeur moyenne des objets restants
                    // cela a pour but de supprimer le cochonnet si il est encore dans la liste
                    // ainsi que les autres petits objets (mais pas trop petits :-D)
                    supprime_objets_hors_gabarit(img, pdeb1, largeur, get_moyenne_largeur(pdeb1) DIV 2, img^.largeur DIV 6);
                    
                    // on ajoute les rectanges
                    img := add_rect_objets(img, pdeb1);
                    
                    
                    for i:=25 to GUIlarg-2 do matrice[i, 8]:=' ';
                    addstring(25,8, 'Ecriture de l''image ', @matrice);
                    write_image(img, IMG_NOM2, @matrice, av1, av2, 2);
                    
                    // lib�ation m�oire
                    delete_image(img);
                
                    
                    // 
                    // TRAITEMENTS POST DETECTION
                    //
                    img := new_image();
                    
                    // lecture image source en couleurs
                    
                    for i:=25 to GUIlarg-2 do matrice[i, 8]:=' ';
                    addstring(25,8, 'Lecture de l''image ', @matrice);
                    img :=read_image(IMG_NOM1);
                    
                    // trace les traits entre boules et cochonnet
                    trace_trait_objets_cochonnet(img, pdeb1, cochonnet);
                    
    
                    for i:=25 to GUIlarg-2 do matrice[i, 8]:=' ';
                    addstring(25,8, 'Ecriture de l''image ', @matrice);
                    write_image(img, IMG_NOM3, @matrice, av1, av2, 3);
                        
                    // lib�ation m�oire
                    
                    for i:=25 to GUIlarg-2 do matrice[i, 8]:=' ';
                    addstring(25,8, 'Lib�ation de l''espace m�oire ', @matrice);
                    delay(1000);
                    delete_image(img);
                    delete_image(img2);
                    dispose(pdeb1);
    
    
                    restart := true;
                    ch := 'q';
                    
                end
                
                
                
                // touche cr�it
                else if ch='c' then
                begin
                    init_matrice(@matrice);
                    addstringtmp(5, 5, 'Programmeurs : Aubry Matthieu', @matrice, 20);
                    addstringtmp(20, 6, 'Combe Anthony', @matrice, 20);
                    addstringtmp(5, 8, 'Remerciements : Basler Sylvie', @matrice, 20);
                    addstringtmp(21, 9, 'Le r�isseur du B ', @matrice, 20);
                    readstring(0,0,@matrice);
                    restart := true;       
                    ch := 'q';
                end;
            end;
        end;
    end;
end;



//-----------------
// Variables
//-----------------

// poiteur debut

       
//----------------------
// Programme principal
//----------------------
begin
    
    rungui();

end.