BGRABitmap/de

From Free Pascal wiki
Jump to navigationJump to search

Deutsch (de) English (en) español (es) français (fr) русский (ru) 中文(中国大陆)‎ (zh_CN)

bgrabitmap logo.jpg

Siehe auch: Developing with Graphics

Beschreibung

BGRABitmap ist eine Sammlung von Units zum Erzeugen und Modifizieren von Bildern mit Transparenzinformationen (Alphakanal). Der direkte Zugriff auf einzelne Pixels eine schnelle Bildbearbeitung. Diese Bibliothek wurde optimiert und getestet unter Windows und Ubuntu, mit den Schnittstellen win32 und gtk2.

Die Hauptklasse ist TBGRABitmap die von TFPCustomImage abgeleitet wurde. Es gibt auch eine Klasse TBGRAPtrBitmap die es erlaubt, bereits allozierte BGRA-Daten zu bearbeiten. Diese Formate bestehen aus 4 Bytes pro Pixel (blau, grün, rot und alpha; in dieser Reihenfolge).

Verwendung von BGRABitmap

Sie kopieren dazu die BGRABitmap-Units und linken sie zu Ihrem Projekt: <delphi> Uses Classes, SysUtils, BGRABitmap, BGRABitmapTypes; </delphi>

Die Unit BGRABitmapTypes enthält alle notwendigen Definitionen, aber Sie können auch einfach eine Variable vom Typ BGRABitmap deklarieren, um eine Bitmap zu laden und anzuzeigen. Dann ist der erste Schritt der, ein TBGRABitmap-Objekt zu erzeugen: <delphi> var bmp: TBGRABitmap; begin

 bmp := TBGRABitmap.Create(100,100,BGRABlack); //erzeugt ein 100x100 Pixel großes Bild mit schwarzem Hintergrund
 bmp.FillRect(20,20,60,60,BGRAWhite, dmSet); //zeichnet ein weißes Quadrat ohne Transparenz
 bmp.FillRect(40,40,80,80,BGRA(0,0,255,128), dmDrawWithTransparency); //zeichnet ein transparentes blaues Quadrat

end; </delphi>

Zuletzt wird die Bitmap angezeigt: <delphi> procedure TFMain.FormPaint(Sender: TObject); begin

 bmp.Draw(Canvas, 0, 0, True); // zeichnet die Bitmap im opaquen Modus (schneller)

end; </delphi>

Anmerkungen

Die Pixels in einer transparenten Bitmap werden in 4 Werten gespeichert, also 4 Bytes in der Reihenfolge Blau, Grün, Rot, Alpha. Der letzte Kanal definiert die Transparenzstufe (0 bedeutet transparent, 255 bedeutet undurchsichtig/opak), die anderen Kanäle definieren die Farbe und die Luminosität.

Grundsätzlich gibt es zwei Zeichenmodi. Der erste besteht darin, die Inhaltsinformation eines Pixels vollständig zu ersetzen. Die zweite besteht darin, das vorhandene Pixel mit einem anderen zu überblenden, das nennt man Alphablending.

Die BGRABitmap-Funktionen bieten dazu 3 Modi an:

  • dmSet : ersetzt die 4 Bytes des gezeichneten Pixels. Die vorhandene Transparenz bleibt nicht erhalten.
  • dmDrawWithTransparency : zeichnet mit Alphablending und mit Gammakorrektur (siehe unten)
  • dmFastBlend oder dmLinearBlend : zeichnet mit Alphablending aber ohne Gammakorrektur (schneller, bewirkt aber Farbverzerrungen bei geringen Intensitäten).

Integrierte Zeichenfunktionen

  • Zeichnen/Löschen von Pixeln
  • Zeichnen einer Linie mit oder ohne Antialiasing
  • Koordinaten in Fließkommagenauigkeit
  • Stiftbreite in Fließkommagenauigkeit
  • Rechteck (gerahmt oder gefüllt)
  • Ellipse und Polygone mit Antialiasing
  • Spline-Berechnung (abgerundete Kurven)
  • einfache Füllung (Floodfill) oder progressive Füllung
  • Farbverläufe (linear, radial...)
  • abgerundete Rechtecke (ohne Antialiasing)
  • Texte mit Transparenz

Zeichnen auf der Leinwand

It is possible to draw with a Canvas object, with usual functions but without antialiasing. Opacity of drawing is defined by the CanvasOpacity property. This way is slower because it needs image transformations.

Direkter Zugriff auf ein Pixel

To access pixels, there are two properties, Data et Scanline. The first gives a pointer to the firts pixel of the image, and the second gives a pointer to the first pixel of a given line.

<delphi> var

  bmp: TBGRABitmap;
  p: PBGRAPixel;
  n: integer;

begin

    bmp := TBGRABitmap.Create('image.png');
    p := bmp.Data;
    for n := bmp.NbPixels-1 downto 0 do
    begin
         p^.red := not p^.red;  //invert red canal
         inc(p);
    end;
    bmp.InvalidateBitmap;  //note that we have accessed directly to pixels
    bmp.Draw(Canvas,0,0,True);
    bmp.Free;

end; </delphi>

It is necessary to call the function InvalidateBitmap to rebuild the image in a next call to Draw' for example. Notice that the line order can be reverse, depending on the LineOrder property.

Bildmanipulation

Available filters:

   * Radial blur : non directionnal blur
   * Motion blur : directionnal blur
   * Custom blur : blur according to a mask
   * Median : computes the median of colors around each pixel, which soften corners
   * Smooth : soften whole image, complentary to Sharpen
   * Sharpen : makes contours more accute, complentary to Smooth
   * Contour : draws contours on a white background (like a pencil drawing)
   * Emboss : draws contours with shadow
   * EmbossHighlight : draws contours of a selection defined with grayscale
   * Grayscale : converts colors to grayscale with gamma correction
   * Normalize : use whole range of color luminosity 
   * Rotate : rotation of the image around a point
   * SmartZoom3 : resize the image x3 and detects borders, to have a useful zoom with ancien games sprites

Bildkombination

PutImage is the basic image drawing function and BlendImage allows to combine images, like layers of image editing softwares. Available modes are the following:

  • LinearBlend : simple superimposition without gamma correction (equivalent to dmFastBlend)
  • Transparent : superimposition with gamma correction
  • Multiply : multiplication of color values (with gamma correction)
  • LinearMultiply : multiplication of color values (without gamma correction)
  • Additive : addition of color values (with gamma correction)
  • LinearAdd : addition of color values (without gamma correction)
  • Difference : difference of color values (with gamma correction)
  • LinearDifference : difference of color values (without gamma correction)
  • Negation : makes common colors disappear (with gamma correction)
  • LinearNegation : makes common colors disappear (without gamma correction)
  • Reflect, Glow : for light effects
  • ColorBurn, ColorDodge, Overlay, Screen : misc. filters
  • Lighten : keeps the lightest color values
  • Darken : keeps the darkest color values
  • Xor : exclusive or of color values

Screenshots

Lazpaint contour.png Lazpaint curve redim.png Bgra wirecube.png Bgra chessboard.jpg

Lizenz

modified LGPL

Download

Sourceforge with LazPaint et BGRABitmap : https://sourceforge.net/projects/lazpaint/files/lazpaint/

Old link : http://consume.o2switch.net/lazarus/

Installation

Add BGRABitmap and BGRABitmapTypes to the uses clause.