How to convert a color image into a black/white image Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiMultimedia/html/delphi_2006120713000976.html
How to convert a color image into a black/white image
Procedure tform1.button3click (Sender: tobject );
VaR
P: pbytearray;
X, Y: integer;
Gray: integer;
BMP: tbitmap;
Begin
BMP: = tbitmap. Create;
BMP. Assign (image1.picture. Bitmap );
BMP. pixelformat: = pf24bit;
For Y: = 0 to BMP. Height-1 do
Begin
P: = BMP. scanline [y];
For X: = 0 to BMP. Width-1 do
Begin
Gray: = (P [3 * x + 2] + P [3 * x + 1] + P [3 * x]) Div 3;
P [3 * x + 2]: = byte (Gray );
P [3 * x + 1]: = byte (Gray );
P [3 * x]: = byte (Gray );
End;
End;
Canvas. Draw (0, 0, BMP );
BMP. Free;
End;
Or:
Procedure tform1.button3click (Sender: tobject );
VaR
P: pbytearray;
X, Y: integer;
Gray: integer;
BMP: tbitmap;
Begin
BMP: = tbitmap. Create;
BMP. Assign (image1.picture. Bitmap );
BMP. pixelformat: = pf24bit;
For Y: = 0 to BMP. Height-1 do
Begin
P: = BMP. scanline [y];
For X: = 0 to BMP. Width-1 do
Begin
Gray: = max (P [3 * x + 2], p [3 * x + 1]);
Gray: = max (Gray, P [3 * x]);
P [3 * x + 2]: = byte (Gray );
P [3 * x + 1]: = byte (Gray );
P [3 * x]: = byte (Gray );
End;
End;
Canvas. Draw (0, 0, BMP );
BMP. Free;
End;
Or:
Procedure tform1.button3click (Sender: tobject );
VaR
P: pbytearray;
X, Y: integer;
Gray: integer;
BMP: tbitmap;
Begin
BMP: = tbitmap. Create;
BMP. Assign (image1.picture. Bitmap );
BMP. pixelformat: = pf24bit;
For Y: = 0 to BMP. Height-1 do
Begin
P: = BMP. scanline [y];
For X: = 0 to BMP. Width-1 do
Begin
Gray: = round (P [3 * x + 2] * 0.3 + P [3 * x + 1] * 0.59 + P [3 * x] * 0.11 );
P [3 * x + 2]: = byte (Gray );
P [3 * x + 1]: = byte (Gray );
P [3 * x]: = byte (Gray );
End;
End;
Canvas. Draw (0, 0, BMP );
BMP. Free;
End;
Mark
Learning