Graphic Delphi

Title: How to GrayScale images
Question: How to scale an TImage containing a bitmap to gray using X graylevels.
Answer:
That's my first solution... a little slow, but fully functionally: just put in a TImage reference and the number of Grays (Grigi in italian) that you want:
Procedure GrayScaleImage( Immagine : TImage; Grigi : Byte );
Var
Grigio : Byte;
IdX : Integer;
IdY : Integer;
IdZ : Integer;
Begin
For IdY := 0 To ( Immagine.Height - 1 ) Do
For IdX := 0 To ( Immagine.Width - 1 ) Do Begin
Grigio := GetRValue( Immagine.Canvas.Pixels[ IdX, IdY ] ) Div 3 + GetGValue( Immagine.Canvas.Pixels[ IdX, IdY ] ) Div 3 + GetBValue( Immagine.Canvas.Pixels[ IdX, IdY ] ) Div 3;
// Verifica se nero.
If ( Grigio Grigio := 0
Else
// Verifica se bianco.
If ( Grigio ( 255 - ( 255 Div Grigi ) ) ) Then
Grigio := 255
Else // Verifica tutti i grigi
For IdZ := 1 To ( Grigi - 2 ) Do
If ( ( Grigio ( ( 255 Div ( Grigi - 2 ) ) * IdZ ) - ( 255 Div Grigi ) ) And
( Grigio Grigio := ( ( 255 Div ( Grigi - 2 ) ) * IdZ );
Immagine.Canvas.Pixels[ IdX, IdY ] := RGB( Grigio, Grigio, Grigio );
End;
End;
I will provide new solutions at days.