Конвертация bitmap to sepia or greyscale

Материал из DRKB


// 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;


Source: http://www.swissdelphicenter.ch/en/tipsindex.php
ID: 03893