// Auteurs
// Matthieu Aubry
// Anthony Combe
// Licence
// Domaine publique
Unit mesure_unit;

interface
// on inclue les unit de niveau inf�ieur
uses outils_unit,image_unit;

type
    profils = (horizontale, verticale, rectangle_roi);

    pt_pics = ^t_pics;
    
    t_pics = record
        debutx, debuty, finx, finy : integer;
        psuivant : pt_pics;
        end;

function mesure_par_profil(img: pTimage; VAR pdeb_pics: pt_pics; seuil: integer; profil: profils; zone: roi; arg:integer): integer;

implementation


function mesure_par_profil(img: pTimage; VAR pdeb_pics: pt_pics; seuil: integer; profil: profils; zone: roi; arg:integer): integer;
var inpic, fini:boolean;
t, x, y:integer;
return : integer;
p, p2 : pt_pics;
begin
    inpic := false;
    fini := false;
    t:=-1;
    return := 0;

    while not(fini) do
            begin
    
    
            case profil of
            horizontale:
                    begin
                    if t=-1 then
                            begin
                            x:=-1;
                            y:=arg;
                            end;
                    inc(t);
                    inc(x);
                    if x=img^.largeur then fini:=true;
                    end;
    
    
            verticale:
                    begin
                    if t=-1 then
                            begin
                            y:=-1;
                            x:=arg;
                            end;
                    inc(t);
                    inc(y);
                    if y=img^.hauteur then fini:=true;
                    end;
    
    
            rectangle_roi:
                    begin
                    if t=-1 then
                            begin
                            x := zone.x0-1;
                            y := zone.y0;
                            end;
                    inc(t);
    
                    if (t<zone.x1-zone.x0) then inc(x);
                    if (t>=zone.x1-zone.x0) AND (t<zone.x1-zone.x0+zone.y1-zone.y0) then inc(y);
                    if (t>=zone.x1-zone.x0+zone.y1-zone.y0) AND (t<2*(zone.x1-zone.x0)+zone.y1-zone.y0) then dec(x);
                    if (t>=2*(zone.x1-zone.x0)+zone.y1-zone.y0) then dec(y);
    
                    if t=2*(zone.x1-zone.x0)+2*(zone.y1-zone.y0) then fini:= true;
    
    
                    end;
            end;
    
    
            // si le pixel d�asse le seuil on rentre ds un pic
            if (read_pixel(x, y, img).red > seuil) AND (inpic=false) then
                    begin
                            inpic := true;
                            // on ajoute un pic dans la liste chain�
                            p := new(pt_pics);
                            p2:=p;
                            if return=0 then pdeb_pics := p
                            else p2^.psuivant := p;
                            
                            p^.debutx := x;
                            p^.debuty := y;
                            p^.psuivant := NIL;
                            
                            // on sauvegarde le pointeur pour l'ajout suivant;
                            p2 := p;
    
                            // on ajoute 1 au nombre de pic trouv�                            inc(return);
                    end;
    
            // si le pixel est inf�ieur au seuil on sort peut �re d'un pic
    
            if (read_pixel(x, y, img).red <= seuil) AND (inpic=true) then
                    begin
                            inpic := false;
                            // on termine de remplir les champs de la liste
                            p^.finx := x;
                            p^.finy := y;
                    end;
    
    
    
            end;//while
    
            // si on est encore dans un pic �la fin du profil, on terime de remplir la structure
            if inpic=true then
                    begin
                            inpic := false;
                            // on termine de remplir les champs de la liste
                            p^.finx := x;
                            p^.finy := y;
                    end;
    
    
    mesure_par_profil := return;

end;

begin
end.