procedure TForm1.Button1Click(Sender: TObject);
var
t: TTifImage;
Bmp,Bmp2: TBitmap;
x,y,i: Integer;
G,B: Byte;
P,R: PByteArray;
begin
Image1.Picture.LoadFromFile('demo.jpg');
Bmp := TBitmap.Create;
Bmp.PixelFormat := pf32bit;
Bmp.SetSize(Image1.Picture.Graphic.Width, Image1.Picture.Graphic.Height);
Bmp.Canvas.Draw(0,0, Image1.Picture.Graphic);
for y:=0 to Bmp.Height-1 do begin
P := Bmp.Scanline[y];
for x:=0 to Bmp.Width-1 do begin
G := (P^[4*x] + P^[4*x+1] + P^[4*x+2]) div 3;
if G > 127 then G := 1
else G := 0;
P^[4*x ] := G;
end;
end;
Bmp2 := TBitmap.Create;
Bmp2.PixelFormat := pf1bit;
Bmp2.SetSize(Bmp.Width, Bmp.Height);
for y:=0 to Bmp2.Height-1 do begin
P := Bmp2.Scanline[y];
R := Bmp.Scanline[y];
for x:=0 to Ceil(Bmp2.Width/8)-1 do begin
B := 0;
for i:=0 to 7 do begin
G := R^[4*(8*x+i)];
B := B + (G shl (7-i));
end;
P^[x] := B;
end;
end;
t := TTifImage.Create;
t.Assign(Bmp2);
t.SetCompression(COMPRESSION_CCITT4);
t.SaveToFile('out.tif');
t.Free;
Bmp2.Free;
Bmp.Free;
end;