mbrlaser
asked on
Color to grayscale image
Dear Sirs:
How I can change an image (TBitMap or TJPEGImage) to grayscale. There is some functions API that do this.
Best regards.
How I can change an image (TBitMap or TJPEGImage) to grayscale. There is some functions API that do this.
Best regards.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This gives a really good render
procedure MakeGrey(Bmp: TBitmap);
const
GreyR = 11;
GreyG = 59;
GreyB = 30;
var
lx: integer;
ly: integer;
lc: longint;
begin
if Bmp.Empty then
Exit;
for lx := 0 to Bmp.Width do
begin
for ly := 0 to Bmp.Height do
begin
lc := Bmp.Canvas.Pixels[lx, ly];
lc := (GetRValue(lc) * GreyR + GetGValue(lc) * GreyG +
GetBValue(lc) * GreyB) div (GreyR + GreyG + GreyB);
Bmp.Canvas.Pixels[lx, ly] := RGB(lc, lc, lc);
end;
end;
end;
call it with
MakeGrey(lBmp);
passing in the bitmap you want greyed
regards
procedure MakeGrey(Bmp: TBitmap);
const
GreyR = 11;
GreyG = 59;
GreyB = 30;
var
lx: integer;
ly: integer;
lc: longint;
begin
if Bmp.Empty then
Exit;
for lx := 0 to Bmp.Width do
begin
for ly := 0 to Bmp.Height do
begin
lc := Bmp.Canvas.Pixels[lx, ly];
lc := (GetRValue(lc) * GreyR + GetGValue(lc) * GreyG +
GetBValue(lc) * GreyB) div (GreyR + GreyG + GreyB);
Bmp.Canvas.Pixels[lx, ly] := RGB(lc, lc, lc);
end;
end;
end;
call it with
MakeGrey(lBmp);
passing in the bitmap you want greyed
regards
You might want to use scanline instead of pixel (see my code above) because scanline is *much* faster, very noticable if you are working on a large bitmap.
procedure GrayScale(b: TBitmap);
var
i, j, Colr : Integer;
sl : pRGBArray; // Scanline
begin
if b.PixelFormat <> pf24bit then begin
ShowMessage('Not a 24Bit color bitmap!');
Exit;
end;
for j:=0 to b.Height-1 do begin
sl := b.ScanLine[j];
for i:=0 to b.Width-1 do begin
Colr:=HiByte(sl[i].rgbtRed
sl[i].rgbtBlue * 28);
sl[i].rgbtRed := Colr;
sl[i].rgbtGreen := Colr;
sl[i].rgbtBlue := Colr;
end;
end;
end;
alternatively, look at
http://www.efg2.com/Lab/Library/UseNet/2002/0719.txt
http://www.efg2.com/Lab/Library/UseNet/1999/0311a.txt
for 256 colored bitmaps whereby you need to set the palette for it as well.