unit anali1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, jpeg, FileCtrl, clipbrd, iniquick;

type
  TForm1 = class(TForm)
    Image1: TImage;
    OpenDialog1: TOpenDialog;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    FileListBox1: TFileListBox;
    DirectoryListBox1: TDirectoryListBox;
    DriveComboBox1: TDriveComboBox;
    Button6: TButton;
    CheckBox1: TCheckBox;
    ListBox1: TListBox;
    Label1: TLabel;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    Edit1: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    Edit2: TEdit;
    Label4: TLabel;
    Edit3: TEdit;
    IQ1: TIniQuick;
    Label5: TLabel;
    Edit4: TEdit;
    Label6: TLabel;
    Edit5: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

var mapab: TBitmap;
    mapa:array[0..3999,0..3999] of byte;
    posi:array[-200..3999,-200..3999] of byte;
    ancho,alto:integer;
    nivel:array[0..255] of integer;
    xt1,yt1,xt2,yt2:integer;
    xb1,yb1,xb2,yb2:integer;
    xi1,yi1,xi2,yi2:integer;
    xd1,yd1,xd2,yd2:integer;
    x1,x2,x3,x4,y1,y2,y3,y4:integer;
    msup,minf,mizq,mder:real;
    sephor,sepver:real;
    uno:boolean=true;
    dx,dy:array[0..79,0..11] of integer;
    maxdf,maxdfr:integer;
    npixpos:integer;
    nf:string;

function tono(x,y:integer):integer;
var s:pBytearray;
    n:integer;
begin
  s:=mapab.scanline[y];
  n:=Round(s[x*3]*0.3+s[x*3+1]*0.59+s[x*3+2]*0.11);
  result:=n; // s[x*3];
end;

procedure buscamanchar(lado,ladofin,x,y:integer;var xdif:integer; var ydif:integer; var npos:integer);
var xmin,ymin,xmax,ymax:integer;
    xmed,ymed:integer;
    np,sumax,sumay:integer;
    i,j:integer;
    c:TColor;
    n:integer;
    xv,yv:integer;
    mxx,mxy,mnx,mny:integer;
    radio:integer;
begin
  xmin:=X-(lado div 2);
  xmax:=xmin+lado;
  ymin:=Y-(lado div 2);
  ymax:=ymin+lado;
  lado:=xmax-xmin;
  radio:=lado div 2;
  xv:=-4;
  yv:=-4;
  for n:=1 to 25 do begin
    mxx:=0;
    mxy:=0;
    mnx:=xmax+1;
    mny:=ymax+1;
    np:=0;
    sumax:=0;
    sumay:=0;
    for i:=xmin to xmax do begin
      for j:=ymin to ymax do begin
        if posi[i,j]=2 then begin
          np:=np+1;
          sumax:=sumax+i;
          sumay:=sumay+j;
          if i>mxx then mxx:=i;
          if i<mnx then mnx:=i;
          if j>mxy then mxy:=j;
          if j<mny then mny:=j;
        end;
      end;
    end;
    xmed:=round(sumax/np);
    ymed:=round(sumay/np);
{    if (xmed=xv)
       and (ymed=yv)
       and ((mxy-mny)< ladofin)
       and ((mxx-mnx)< ladofin)
       then break;  }
    xv:=xmed;
    yv:=ymed;
    lado:=(ladofin+lado*2) div 3;
    xmin:=xmed-(lado div 2);
    xmax:=xmin+lado;
    ymin:=Ymed-(lado div 2);
    ymax:=ymin+lado;
    application.processmessages;
  end;
  xdif:=xmed;
  ydif:=ymed;
  npos:=np;
end;

procedure buscamancha(lado,ladofin,x,y:integer;var xdif:integer; var ydif:integer; var npos:integer);
var xmin,ymin,xmax,ymax:integer;
    xmed,ymed:integer;
    np,sumax,sumay:integer;
    i,j:integer;
    c:TColor;
    n:integer;
    xv,yv:integer;
    mxx,mxy,mnx,mny:integer;
    radio:integer;
begin
  xmin:=X-(lado div 2);
  xmax:=xmin+lado;
  ymin:=Y-(lado div 2);
  ymax:=ymin+lado;
  lado:=xmax-xmin;
  radio:=lado div 2;
  xv:=-4;
  yv:=-4;
  xmed:=x;
  ymed:=y;
  for n:=1 to 25 do begin
    mxx:=0;
    mxy:=0;
    mnx:=xmax+1;
    mny:=ymax+1;
    np:=0;
    sumax:=0;
    sumay:=0;
    for i:=-radio to radio do begin
      for j:=-radio to radio do begin
        if (i*i+j*j)< (radio*radio) then begin
          if posi[xmed+i,ymed+j]=2 then begin
            np:=np+1;
            sumax:=sumax+xmed+i;
            sumay:=sumay+ymed+j;
            if xmed+i>mxx then mxx:=xmed+i;
            if xmed+i<mnx then mnx:=xmed+i;
            if ymed+j>mxy then mxy:=ymed+j;
            if ymed+j<mny then mny:=ymed+j;
          end;
        end;
      end;
    end;
    xmed:=round(sumax/np);
    ymed:=round(sumay/np);
{    if (xmed=xv)
       and (ymed=yv)
       and ((mxy-mny)< ladofin)
       and ((mxx-mnx)< ladofin)
       then break;  }
    xv:=xmed;
    yv:=ymed;
    lado:=(ladofin+lado*2) div 3;
    radio:=lado div 2;
    xmin:=xmed-(lado div 2);
    xmax:=xmin+lado;
    ymin:=Ymed-(lado div 2);
    ymax:=ymin+lado;
//    application.processmessages;
  end;
  application.processmessages;
  xdif:=xmed;
  ydif:=ymed;
  npos:=np;
end;

procedure buscamanchag(lado,ladofin,x,y:integer;var xdif:integer; var ydif:integer; var npos:integer);
var xmin,ymin,xmax,ymax:integer;
    xmed,ymed:integer;
    np,sumax,sumay:integer;
    i,j:integer;
    c:TColor;
    n:integer;
    xv,yv:integer;
    mxx,mxy,mnx,mny:integer;
    radio:integer;
    incx,incy:real;
begin
  xmin:=X-(lado div 2);
  xmax:=xmin+lado;
  ymin:=Y-(lado div 2);
  ymax:=ymin+lado;
  lado:=xmax-xmin;
  radio:=lado div 2;
  xv:=-4;
  yv:=-4;
  xmed:=x;
  ymed:=y;
  for n:=1 to 35 do begin
    mxx:=-radio;
    mxy:=-radio;
    mnx:=radio+1;
    mny:=radio+1;
    np:=0;
    sumax:=0;
    sumay:=0;
    incx:=0;
    incy:=0;
    for i:=-radio to radio do begin
      for j:=-radio to radio do begin
        if ((i*i+j*j)< (radio*radio)) and (abs(i)+abs(j)<>0) then begin
          if posi[xmed+i,ymed+j]=2 then begin
            np:=np+1;
            incx:=incx+i/(i*i+j*j);
            incy:=incy+j/(i*i+j*j);
            sumax:=sumax+i;
            sumay:=sumay+j;
            if i>mxx then mxx:=i;
            if i<mnx then mnx:=i;
            if j>mxy then mxy:=j;
            if j<mny then mny:=j;
          end;
        end;
      end;
    end;
    xmed:=round(xmed+(incx{+sumax/np)/2}));
    ymed:=round(ymed+(incy{+sumay/np)/2}));
{    if (xmed=xv)
       and (ymed=yv)
       and ((mxy-mny)< ladofin)
       and ((mxx-mnx)< ladofin)
       then break;  }
    xv:=xmed;
    yv:=ymed;
    lado:=(ladofin+lado*2) div 3;
    radio:=lado div 2;
    xmin:=xmed-(lado div 2);
    xmax:=xmin+lado;
    ymin:=Ymed-(lado div 2);
    ymax:=ymin+lado;
    application.processmessages;
  end;
  xdif:=xmed;
  ydif:=ymed;
  npos:=np;
end;

procedure intersec(x1,y1,x2,y2,x3,y3,x4,y4:int64;var x:integer; var y:integer);
begin
  x:=round(((x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-y3*x4))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)));
  y:=round(((x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)));
end;

procedure ponpix(x,y,v:integer);
var s:pBytearray;
begin
  if (x>=0)
    and (y>=0)
    and (x<ancho)
    and (y<alto)
  then begin
    s:=form1.Image1.picture.bitmap.scanline[y];
    s[x*3]:=v;
    s[x*3+1]:=v;
    s[x*3+2]:=v;
  end;
end;

var diametro:integer=80;

procedure TForm1.Button1Click(Sender: TObject);
var jp:TJpegImage;
    b: TBitmap;
begin
  uno:=true;
  button1.enabled:=false;
  button6.enabled:=false;
  if opendialog1.execute then begin
    jp:=TJpegImage.create;
    jp.loadfromfile(opendialog1.filename);
    nf:=opendialog1.filename;
    b:=TBitmap.create;
    b.assign(jp);
    b.pixelformat:=pf24bit;
    mapab.width:=jp.Width;
    mapab.height:=jp.height;
    mapab.canvas.draw(0,0,jp);
    image1.picture.bitmap.assign(b);
    b.free;
    jp.free;
    ancho:=mapab.width;
    alto:=mapab.height;
    button2.enabled:=true;
    button2click(self);
//    button3click(self);
//    button4click(self);
    button5click(self);
    image1.refresh;
    clipboard.astext:=caption+#13#10;
  end;
  button1.enabled:=true;
  button6.enabled:=true;
end;

procedure TForm1.FormCreate(Sender: TObject);
var i,j:integer;
begin
  iq1.restore;
  edit1.text:=iq1.GetStrD('diametro',edit1.text);
  edit2.text:=iq1.GetStrD('contraste',edit2.text);
  edit3.text:=iq1.GetStrD('fraccion',edit3.text);
  edit4.text:=iq1.GetStrD('umbgris',edit4.text);
  edit5.text:=iq1.GetStrD('fracgris',edit5.text);
  mapab:=TBitmap.create;
  mapab.pixelformat:=pf24bit;
  for i:=-200 to 3999 do begin
    for j:=-200 to 3999 do begin
      posi[i,j]:=0;
    end;
  end;
  if not fileexists(extractfilepath(application.exename)+'leeme.txt') then begin
    listbox1.items.savetofile(extractfilepath(application.exename)+'leeme.txt');
  end;
  listbox1.Items.clear;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  mapab.free;
  iq1.PutStr('diametro',edit1.text);
  iq1.PutStr('contraste',edit2.text);
  iq1.PutStr('fraccion',edit3.text);
  iq1.PutStr('umbgris',edit4.text);
  iq1.PutStr('fracgris',edit5.text);
  iq1.Save;
end;

var prefblanco:real=0.6;
    umbgris:real=0.5;

procedure TForm1.Button2Click(Sender: TObject);
var i,j:integer;
  n,ng:integer;
  cb,sumab:integer;
  ca,sumaa:integer;
  tmue:integer;
begin
  button2.enabled:=false;
  val(edit2.text,prefblanco,n);
  if prefblanco=0.0 then prefblanco:=1.0;
  val(edit4.text,umbgris,n);
  if umbgris=0.0 then umbgris:=0.5;
  for i:=0 to 255 do begin
    nivel[i]:=0;
  end;
  for j:=0 to alto-1 do begin
    for i:=0 to ancho-1 do begin
      n:=tono(i,j);
      mapa[i,j]:=n;
      nivel[n]:=nivel[n]+1;
    end;
  end;
  tmue:=ancho*alto div 50;
  ca:=0;
  sumaa:=0;
  i:=255;
  while ca<tmue do begin
    ca:=ca+nivel[i];
    sumaa:=sumaa+i*nivel[i];
    i:=i-1;
  end;
  cb:=0;
  sumab:=0;
  i:=0;
  while cb<tmue do begin
    cb:=cb+nivel[i];
    sumab:=sumab+i*nivel[i];
    i:=i+1;
  end;
  n:=round((sumaa*prefblanco/ca+sumab/cb)/(1+prefblanco));
  ng:=round(n+(sumaa/ca-n)*umbgris);
  npixpos:=0;
  for j:=0 to alto-1 do begin
    for i:=0 to ancho-1 do begin
      if mapa[i,j]>ng then begin
        posi[i,j]:=0;
        if uno then begin
          ponpix(i,j,255);
        end;
//        image1.picture.bitmap.canvas.pixels[i,j]:=clwhite;
      end else begin
        if mapa[i,j]>n then begin
          posi[i,j]:=1;
          if uno then begin
            ponpix(i,j,191);
          end;
  //        image1.picture.bitmap.canvas.pixels[i,j]:=clwhite;
        end else begin
          posi[i,j]:=2;
          npixpos:=npixpos+1;
          ponpix(i,j,0);
  //        image1.picture.bitmap.canvas.pixels[i,j]:=clBlack;
        end;
      end;
    end;
    application.processmessages;
  end;
  image1.refresh;
  button2.enabled:=true;
end;

procedure TForm1.Button3Click(Sender: TObject);
var i,d,j:integer;
    minb:integer;
    n:integer;
begin
  button3.enabled:=false;
  i:=0;
  minb:=alto div 8;
  repeat
    n:=0;
    for j:=0 to alto-1 do begin
      n:=n+1-posi[i,j];
    end;
    i:=i+1;
  until n>=minb;
  i:=i-1;
  d:=ancho-1;
  repeat
    n:=0;
    for j:=0 to alto-1 do begin
      n:=n+1-posi[d,j];
    end;
    d:=d-1;
  until n>=minb;
  d:=d+1;
  minb:=ancho div 50;
  xt1:=i+(ancho div 10);
  yt1:=0;
  repeat
    n:=0;
    for j:=xt1-minb to xt1+minb do begin
      n:=n+1-posi[j,yt1];
    end;
    yt1:=yt1+1;
  until n>=minb;
  yt1:=yt1-1;
  xb2:=d-(ancho div 10);
  yb2:=alto-1;
  repeat
    n:=0;
    for j:=xb2-minb to xb2+minb do begin
      n:=n+1-posi[j,yb2];
    end;
    yb2:=yb2-1;
  until n>=minb;
  yb2:=yb2+1;
  xt2:=d-(ancho div 10);
  yt2:=0;
  repeat
    n:=0;
    for j:=xt2-minb to xt2+minb do begin
      n:=n+1-posi[j,yt2];
    end;
    yt2:=yt2+1;
  until n>=minb;
  yt2:=yt2-1;
  xb1:=i+(ancho div 10);
  yb1:=alto-1;
  repeat
    n:=0;
    for j:=xb1-minb to xb1+minb do begin
      n:=n+1-posi[j,yb1];
    end;
    yb1:=yb1-1;
  until n>=minb;
  yb1:=yb1+1;
//----------------------
  minb:=(yb1-yt1) div 30;
  yi1:=yt1+minb*5;
  xi1:=0;
  repeat
    n:=0;
    for j:=yi1-minb to yi1+minb do begin
      n:=n+1-posi[xi1,j];
    end;
    xi1:=xi1+1;
  until n>=minb;
  xi1:=xi1-1;
//....................................
  yi2:=yb1-minb*5;
  xi2:=0;
  repeat
    n:=0;
    for j:=yi2-minb to yi2+minb do begin
      n:=n+1-posi[xi2,j];
    end;
    xi2:=xi2+1;
  until n>=minb;
  xi2:=xi2-1;
//.....................................
  minb:=(yb2-yt2) div 30;
  yd1:=yt2+minb*5;
  xd1:=ancho-1;
  repeat
    n:=0;
    for j:=yd1-minb to yd1+minb do begin
      n:=n+1-posi[xd1,j];
    end;
    xd1:=xd1-1;
  until n>=minb;
  xd1:=xd1+1;
//....................................
  yd2:=yb2-minb*5;
  xd2:=ancho-1;
  repeat
    n:=0;
    for j:=yd2-minb to yd2+minb do begin
      n:=n+1-posi[xd2,j];
    end;
    xd2:=xd2-1;
  until n>=minb;
  xd2:=xd2+1;
//-------------------------------------
  intersec(xt1,yt1,xt2,yt2,xi1,yi1,xi2,yi2,x1,y1);
  intersec(xt1,yt1,xt2,yt2,xd1,yd1,xd2,yd2,x2,y2);
  intersec(xb1,yb1,xb2,yb2,xi1,yi1,xi2,yi2,x3,y3);
  intersec(xb1,yb1,xb2,yb2,xd1,yd1,xd2,yd2,x4,y4);
//--------------------------------------
  with image1.picture.bitmap.canvas do begin
    pen.color:=clred;
    pen.width:=7;
    ellipse(x1-10,y1-10,x1+10,y1+10);
    ellipse(x2-10,y2-10,x2+10,y2+10);
    ellipse(x3-10,y3-10,x3+10,y3+10);
    ellipse(x4-10,y4-10,x4+10,y4+10);
    moveto(xi1,yi1);
    lineto(xi2,yi2);
    moveto(xd1,yd1);
    lineto(xd2,yd2);
    moveto(xt1,yt1);
    lineto(xt2,yt2);
    moveto(xb1,yb1);
    lineto(xb2,yb2);
  end;
  button3.enabled:=true;
end;

procedure TForm1.Button4Click(Sender: TObject);
var i:integer;
begin
  msup:=0.077;
  minf:=msup;
  sepver:=(1-msup-minf)/11;
  with image1.picture.bitmap.canvas do begin
    pen.color:=clred;
    pen.width:=5;
    for i:=0 to 11 do begin
      moveto(x1+round((x3-x1)*(msup+i*sepver)),y1+round((y3-y1)*(msup+i*sepver)));
//      lineto(x2+round((x4-x2)*(msup+i*sepver)),y2+round((y4-y2)*(msup+i*sepver)));
    end;
  end;
  val (edit1.text,mder,i);
  if mder=0.0 then mder:=0.0323;
  mizq:=0.0343;
  sephor:=(1-mder-mizq)/79;
  with image1.picture.bitmap.canvas do begin
    pen.color:=clred;
    pen.width:=5;
    for i:=0 to 79 do begin
      moveto(x2+round((x1-x2)*(mder+i*sephor)),y2+round((y1-y2)*(mder+i*sephor)));
//      lineto(x4+round((x3-x4)*(mder+i*sephor)),y4+round((y3-y4)*(mder+i*sephor)));
    end;
  end;
end;

function codigo(n:integer):char;
//const c='&-0123456789ABCDEFGHIJKLMNOPQR/STUVWXYZ #@:>V?.¤(<§!$*);^±,%=''"';
const c='+-0123456789ABCDEFGHIJKLMNOPQR/STUVWXYZ ='':>@?.)[<§!$*];^±,(v\¶';
var x:integer;
begin
  case n of
    $000: x:=0;
    $800: x:=1;
    $400: x:=2;
    $200: x:=3;
    $100: x:=4;
    $080: x:=5;
    $040: x:=6;
    $020: x:=7;
    $010: x:=8;
    $008: x:=9;
    $004: x:=10;
    $002: x:=11;
    $001: x:=12;
    $900: x:=13;
    $880: x:=14;
    $840: x:=15;
    $820: x:=16;
    $810: x:=17;
    $808: x:=18;
    $804: x:=19;
    $802: x:=20;
    $801: x:=21;
    $500: x:=22;
    $480: x:=23;
    $440: x:=24;
    $420: x:=25;
    $410: x:=26;
    $408: x:=27;
    $404: x:=28;
    $402: x:=29;
    $401: x:=30;
    $300: x:=31;
    $280: x:=32;
    $240: x:=33;
    $220: x:=34;
    $210: x:=35;
    $208: x:=36;
    $204: x:=37;
    $202: x:=38;
    $201: x:=39;
    $082: x:=40;
    $042: x:=41;
    $022: x:=42;
    $012: x:=43;
    $00A: x:=44;
    $006: x:=45;
    $A00: x:=46;
    $842: x:=47;
    $822: x:=48;
    $812: x:=49;
    $80A: x:=50;
    $806: x:=51;
    $602: x:=52;
    $442: x:=53;
    $422: x:=54;
    $412: x:=55;
    $40A: x:=56;
    $406: x:=57;
    $282: x:=58;
    $242: x:=59;
    $222: x:=60;
    $212: x:=61;
    $20A: x:=62;
    $206: x:=63;
  else x:=81;
  end;
  case x of
    0 : Result:=' ';
    81: result:='z';
    else result:=c[x];
  end;
end;

procedure TForm1.Button5Click(Sender: TObject);
const minv=4;
      maxv=15;
var i,j,k,l,radio:integer;
    px,py,cx,cy:integer;
    x,y:integer;
    lh,lv:integer;
    umbral,c:integer;
    jp:TJpegimage;
    v:integer;
    s:string;
    xc,yc,aox,box:real;
    sumap,sumaf,sumac:real;
    nm:boolean;
    umbf,umbfg:real;
    umbm,umbg,np,ng:integer;
    nmanc:integer;

begin
  val (edit1.text,mder,i);
  if mder=0.0 then mder:=0.0521;
  val (edit3.text,umbf,i);
  if umbf=0.0 then umbf:=0.5;
  val (edit5.text,umbfg,i);
  if umbfg=0.0 then umbfg:=0.3;
  diametro:=round(ancho*mder);
  lh:=round(diametro/10);
  lv:=lh;
  umbm:=round(umbf*diametro*diametro*3.14/8);
  umbg:=round(umbfg*diametro*diametro*3.14/8);
  radio:=round(diametro/2);
  caption:=inttostr(lh)+' '+inttostr(lv);
  umbral:=(lv*lh) div 2;
  image1.picture.Bitmap.canvas.brush.Style:=bsclear;
  s:='';
  aox:=0;
  box:=0;
  maxdf:=0;
  maxdfr:=0;
  nmanc:=0;
  for i:=1 to round(ancho*4/diametro)-1 do begin
    px:=round(i*diametro/4);
    for j:=1 to round(alto*4/diametro)-1 do begin
      x:=px;
      y:=round(j*diametro/4);
      c:=0;
      if uno then begin
        image1.picture.bitmap.canvas.pen.width:=1;
        image1.picture.bitmap.canvas.pen.color:=clred;
//        image1.picture.bitmap.canvas.rectangle(x-lh,y-lv,x+lh,y+lv);
      end;
      for k:=x-lh to x+lh do begin
        for l:=y-lv to y+lv do begin
          if posi[k,l]=2 then c:=c+l;
        end;
      end;
      if c>umbral then begin
//        nm:=true;
        if uno then begin
          image1.picture.bitmap.canvas.pen.color:=clRed;
          image1.picture.bitmap.canvas.pen.width:=5;
//          image1.Picture.bitmap.canvas.Ellipse(x-15,y-15,x+15,y+15);
        end;
        buscamancha(round(diametro*0.6),round(diametro*1.12),x,y,cx,cy,np);
        np:=0;
        ng:=0;
        for k:=-radio to +radio do begin
          for l:=-radio to +radio do begin
            if ((k*k+l*l)< (radio*radio))  then begin
              case posi[cx+k,cy+l] of
                2 : begin
                      posi[cx+k,cy+l]:=0;
                      np:=np+1;
                    end;
                1 : begin
                      ng:=ng+1;
                    end;
              end;
//              ponpix(x+k,y+l,255);
            end;
          end;
        end;
        if np>umbm then begin
          image1.picture.bitmap.canvas.pen.color:=clLime;
          image1.picture.bitmap.canvas.pen.width:=2;
          if ng>umbg then begin
            image1.picture.bitmap.canvas.pen.color:=clRed;
            image1.picture.bitmap.canvas.pen.width:=5;
            nmanc:=nmanc+1;
          end;
        end else begin
          image1.picture.bitmap.canvas.pen.color:=clYellow;
          image1.picture.bitmap.canvas.pen.width:=2;
        end;
        image1.Picture.bitmap.canvas.Ellipse(cx-(diametro div 2),cy-(diametro div 2),cx+(diametro div 2),cy+(diametro div 2));
      end;
    end;
  end;
  if uno then begin
    jp:=TJpegimage.create;
    jp.assign(image1.picture.bitmap);
    jp.CompressionQuality:=60;
    jp.savetofile(extractfilepath(application.exename)+'salida.jpg');
    jp.free;
  end;
  s:=format('%5.3f %5.3f %5.3f %7d %5d %4d %4d ',[prefblanco,mder,umbf,npixpos,nmanc,ancho,alto])+extractfilename(nf);
  caption:=s;
end;

procedure TForm1.Button6Click(Sender: TObject);
var jp:TJpegImage;
    b: TBitmap;
    i:integer;
    t:integer;
begin
  button6.enabled:=false;
  button1.enabled:=false;
  uno:=false;
  listbox1.items.clear;
  t:=filelistbox1.items.Count;
  label1.caption:='0/'+inttostr(t);
  for i:=0 to filelistbox1.Items.count-1 do begin
    nf:=filelistbox1.items[i];
    jp:=TJpegImage.create;
    jp.loadfromfile(nf);
    b:=TBitmap.create;
    b.assign(jp);
    b.pixelformat:=pf24bit;
    mapab.width:=jp.Width;
    mapab.height:=jp.height;
    mapab.canvas.draw(0,0,jp);
    image1.picture.bitmap.assign(b);
    b.free;
    jp.free;
    ancho:=mapab.width;
    alto:=mapab.height;
    button2.enabled:=true;
    button2click(self);
//    button3click(self);
//    button4click(self);
    button5click(self);
    if checkbox1.checked then begin
      listbox1.Items.Insert(0,caption);
    end else begin
      listbox1.items.add(caption);
    end;
    label1.caption:=inttostr(i+1)+'/'+inttostr(t);
  end;
  listbox1.items.savetofile(extractfilepath(application.exename)+changefileext(extractfilename(filelistbox1.directory),'.txt'));
  button1.enabled:=true;
  button6.enabled:=true;
  uno:=true;
end;

end.
