Search results for 'TransparentColor'. 1 post(s) found.

  1. 2007/09/11 Grayscaling a bitmap ?
2007/09/11 07:53

Grayscaling a bitmap ?


This example uses an averaging mechanism to obtain GrayScale intensity on a pixel-by-pixel basis. Specifically, it averages the Red, Green and Blue component values of a color and assigns that value back to each RGB component.

Usage:

GrayScale(Image1.Picture.Graphic, image1.Picture.Bitmap, 0) ;

Here's the source code can use above usage code.

type
  TRGBColor = record
  case Integer of
    0 : (Color : TColor) ;
    1 : (Red, Green, Blue, Intensity : Byte) ;
    2 : (Colors : array[0..3] of Byte) ;
end;

procedure GrayScale(Graphic : TGraphic;
                    Bitmap : TBitmap;
                    Contrast : Integer ) ;
var bmp, mask : TBitmap;
    x, y , Avg : Integer;
    RGBColor : TRGBColor;
begin
  if not Assigned(Graphic) then
    Exit;

  bmp := TBitmap.Create;
  mask := TBitmap.Create;

  try
    bmp.Height := Graphic.Height;
    bmp.Width := Graphic.Width;
    with bmp.Canvas do begin
      if Graphic is TBitmap then
        Brush.Color := (Graphic as TBitmap).TransparentColor
      else
        Brush.Color := clPurple;
      FillRect(ClipRect) ;
      Draw(0,0,Graphic) ;
    end;
    mask.assign(bmp) ;
    mask.mask(bmp.TransparentColor) ;
    with bmp.Canvas do begin
      for y := 0 to ClipRect.Bottom do begin
        for x := 0 to ClipRect.Right do begin
          if mask.Canvas.Pixels[x,y] = clBlack then begin
            RGBColor.Color := Pixels[x,y] ;
            with RGBColor do begin
              Avg := ((Red+Green+Blue) div 3) ;
              Inc(Avg, Contrast) ;
              if Avg > 255 then
                Avg := 255
              else if Avg < 0 then
                Avg := 0;
              Red := Avg;
              Green := Avg;
              Blue := Avg;
            end;
            Pixels[x,y] := RGBColor.Color;
          end;
        end;
      end;
    end;
    if Assigned(Bitmap) then
      Bitmap.Assign(bmp)
    else if Graphic is TBitmap then
      (Graphic as TBitmap).Assign(bmp) ;
  finally
    bmp.Free;
    mask.Free;
  end;
end;
Trackback 3 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 03:02 delete

    moneyideas

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 11:44 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:39 delete

    moneyideas