///  Imagen en escala de grises pasarla a blanco y negro   ///

procedure TForm1.Umbral1Click(Sender: TObject);

var valor : integer;
    alto, ancho : integer;
    fila,col : integer;
begin
    Form2.Showmodal;
    valor:=StrToInt(Form2.Edit1.Text);
    ancho := Image1.width;
    alto := Image1.Height;

    for fila:=0 to alto-1 do
      for col:= 0 to ancho-1 do
         if GetBValue(Image1.Canvas.Pixels[col,fila])>valor then
              Image1.Canvas.Pixels[col,fila] := RGB(255,255,255)
         else Image1.Canvas.Pixels[col,fila] := RGB(0,0,0);
end;

///   Cambiar contraste de una imagen en escala de grises   ///

procedure TForm1.Contraste1Click(Sender: TObject);

Var  contraste : array [0..255] of integer;
     A, B, fila, col, i: integer;
     INLOW, INHIGH, OUTLOW : integer;
     valor1, ancho, alto : integer;

begin
        ParContraste.showmodal;
        INLOW:=StrToInt(ParContraste.Edit1.Text);
        INHIGH:= StrToInt(ParContraste.Edit2.Text);
        OUTLOW:=StrToInt(ParContraste.Edit3.Text);

        for i:=0 to INLOW  do
            contraste[i]:=OUTLOW;
        for i:=INHIGH to 255  do
            contraste[i]:=255;
        for i:=INLOW  to INHIGH  do
          contraste[i]:=round(((255 - OUTLOW)/(INHIGH - INLOW))*(i - INLOW)+OUTLOW);

     ancho:=Image1.Width;
     alto:=Image1.Height;

  for fila:=0 to alto-1 do
      begin
        for col:=0 to ancho-1 do
          begin
            valor1:=GetBValue(Image1.Canvas.Pixels[col,fila]);
            Image1.Canvas.Pixels[col, fila]:=RGB(contraste[valor1],contraste[valor1],contraste[valor1]);
          end;
      end;
end;

///   Histograma

procedure TForm1.HistogramaGris1Click(Sender: TObject);

Var  histo : array[0..255] of integer;
     valor1, ancho, alto, valor2 : integer;
     fila, col, i,r,j, q : integer;

begin
   for i:=0 to 255 do
       histo[i]:=0;

     ancho:=Image1.Width;
     alto:=Image1.Height;

  for fila:=0 to alto-1 do
      begin
        for col:=0 to ancho-1 do
          begin
            valor1:=GetBValue(Image1.Canvas.Pixels[col,fila]);
            histo[valor1]:=histo[valor1]+1;
          end;
      end;

   Form3.show;

  for  r:=0  to  280  do
         Form3.Image2.Canvas.Pixels[50, 400-r]:=clBlack;

  for r:=0  to 300  do
         Form3.Image2.Canvas.Pixels[50+r, 400]:=clBlack;

   Form3.Image2.Refresh;

  for j:=0  to  255   do
   begin
    valor2:=round(histo[j]/10);
    for  q:=0  to valor2 do
       Form3.Image2.Canvas.Pixels[51+j, 399-q]:=clRed;
   end;
  Form3.Image2.Refresh;

end;