was ist neu ¦  programmier tips ¦  indy artikel ¦  intraweb artikel ¦  informationen ¦  links ¦  interviews
 sonstiges ¦  tutorials ¦  Add&Win Gewinnspiel

Tips (1541)

Dateien (137)
Datenbanken (90)
Drucken (35)
Grafik (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Mathematik (76)
Multimedia (45)
Oberfläche (107)
Objekte/
ActiveX (51)

OpenTools API (3)
Sonstiges (126)
Strings (83)
System (266)
VCL (242)

Tips sortiert nach
Komponente


Tip suchen

Tip hinzufügen

Add&Win Gewinnspiel

Werbung

41 Visitors Online


 
...ein Bitmap in Graustufen darstellen?
Autor: Peter Burjak in cooperation with Sebastian
[ Tip ausdrucken ]  

Tip Bewertung (32):  
     


//This function turns a colored Bitmap into Grayshades
//Diese Funktion wandelt ein färbiges Bitmap in Graustufen um

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;


 

Bewerten Sie diesen Tipp:

dürftig
ausgezeichnet


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners