bangnaga

Mengubah gambar menjadi Grayscale

May 10th, 2012
93
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type
  2.   PRGB32Array = ^TRGB32Array;
  3.   TRGB32Array = packed array[0..MaxInt div SizeOf(TRGBQuad)-1] of TRGBQuad;
  4.  
  5. procedure MakeGrey(Bitmap: TBitmap);
  6. var
  7.   w, h: integer;
  8.   y: Integer;
  9.   sl: PRGB32Array;
  10.   x: Integer;
  11.   grey: byte;
  12. begin
  13.   Bitmap.PixelFormat := pf32bit;
  14.   w := Bitmap.Width;
  15.   h := Bitmap.Height;
  16.   for y := 0 to h - 1 do
  17.   begin
  18.     sl := Bitmap.ScanLine[y];
  19.     for x := 0 to w - 1 do
  20.       with sl[x] do
  21.       begin
  22.         grey := (rgbBlue + rgbGreen + rgbRed) div 3;
  23.         rgbBlue := grey;
  24.         rgbGreen := grey;
  25.         rgbRed := grey;
  26.       end;
  27.   end;
  28. end;
  29.  
  30. // Sample usage:
  31.  
  32. procedure TForm4.Button1Click(Sender: TObject);
  33. var
  34.   bm: TBitmap;
  35. begin
  36.   bm := TBitmap.Create;
  37.   try
  38.     bm.LoadFromFile('C:\gambar\foto.bmp');
  39.     MakeGrey(bm);
  40.     Canvas.Draw(0, 0, bm);
  41.   finally
  42.     bm.Free;
  43.   end;
  44. end;
RAW Paste Data