灰度级处理
//This function adds a sepia effect to a bitmap.
//the 'depth' sets the colour intensity of the red-brown colour
//greater numbers set a higher intensity.
//To create a greyscale effect instead, set 'depth' to 0
function bmptosepia ( const bmp : TBitmap ; depth : Integer ): Boolean ;
var
color , color2 : longint ;
r , g , b , rr , gg : byte ;
h , w : integer ;
begin
for h := 0 to bmp . height do
begin
for w := 0 to bmp . width do
begin
//first convert the bitmap to greyscale
color := colortorgb ( bmp . Canvas . pixels [ w , h ]);
r := getrvalue ( color );
g := getgvalue ( color );
b := getbvalue ( color );
color2 := ( r + g + b ) div 3 ;
bmp . canvas . Pixels [ w , h ] := RGB ( color2 , color2 , color2 );
//then convert it to sepia
color := colortorgb ( bmp . Canvas . pixels [ w , h ]);
r := getrvalue ( color );
g := getgvalue ( color );
b := getbvalue ( color );
rr := r + ( depth * 2 );
gg := g + depth ;
if rr <= (( depth * 2 ) - 1 ) then
rr := 255 ;
if gg <= ( depth - 1 ) then
gg := 255 ;
bmp . canvas . Pixels [ w , h ] := RGB ( rr , gg , b );
end ;
end ;
end ;
//Example:
procedure TForm1 . Button1Click ( Sender : TObject );
begin
bmptosepia ( image1 . picture . bitmap , 20 );
end ;
---------------------------------------
procedure Gray(bmp: TBitmap);
var
p: PByteArray;
w: Integer;
i, j: Integer;
begin
bmp.pixelformat := pf24bit;
for i := 0 to bmp.height - 1 do
begin
p := bmp.scanline[i];
j := 0;
while j < (bmp.width-1) * 3 do
begin
w :=(p[j] * 28 + p[j+1] * 151 + p[j+2]*77);
w := w shr 8;
p[j] := byte(w);
p[j+1] := byte(w);
p[j+2] := byte(w);
inc(j, 3)
end;
end;
end;
**************************
//This function turns a colored Bitmap into Grayshades
uses
Windows, Graphics;
function ConvertBitmapToGrayscale1(const Bitmap: TBitmap): TBitmap;
var
i, j: Integer;
Grayshade, Red, Green, Blue: Byte;
PixelColor: Longint;
begin
with Bitmap do
for i := 0 to Width - 1 do
for j := 0 to Height - 1 do
begin
PixelColor := ColorToRGB(Canvas.Pixels[i, j]);
Red := PixelColor;
Green := PixelColor shr 8;
Blue := PixelColor shr 16;
Grayshade := Round(0.3 * Red + 0.6 * Green + 0.1 * Blue);
Canvas.Pixels[i, j] := RGB(Grayshade, Grayshade, Grayshade);
end;
Result := Bitmap;
end;
procedure ConvertBitmapToGrayscale2(const Bmp: TBitmap);
{From: Pascal Enz, pascal.enz@datacomm.ch }
type
TRGBArray = array[0..32767] of TRGBTriple;
PRGBArray = ^TRGBArray;
var
x, y, Gray: Integer;
Row: PRGBArray;
begin
Bmp.PixelFormat := pf24Bit;
for y := 0 to Bmp.Height - 1 do
begin
Row := Bmp.ScanLine[y];
for x := 0 to Bmp.Width - 1 do
begin
Gray := (Row[x].rgbtRed + Row[x].rgbtGreen + Row[x].rgbtBlue) div 3;
Row[x].rgbtRed := Gray;
Row[x].rgbtGreen := Gray;
Row[x].rgbtBlue := Gray;
end;
end;
end;
procedure ConvertBitmapToGrayscale3(const Bitmap: TBitmap);
type
PPixelRec = ^TPixelRec;
TPixelRec = packed record
B: Byte;
G: Byte;
R: Byte;
Reserved: Byte;
end;
var
X: Integer;
Y: Integer;
P: PPixelRec;
Gray: Byte;
begin
Assert(Bitmap.PixelFormat = pf32Bit);
for Y := 0 to (Bitmap.Height - 1) do
begin
P := Bitmap.ScanLine[Y];
for X := 0 to (Bitmap.Width - 1) do
begin
Gray := Round(0.30 * P.R + 0.59 * P.G + 0.11 * P.B);
// Gray := (P.R shr 2) + (P.R shr 4) + (P.G shr 1) + (P.G shr 4) + (P.B shr 3);
P.R := Gray;
P.G := Gray;
P.B := Gray;
Inc(P);
end;
end;
end;