Difference between revisions of "BGRABitmap/es"

From Free Pascal wiki
Jump to navigationJump to search
m (french wiki)
(version 1.5)
Line 1: Line 1:
[[BGRABitmap/fr|French]]
+
[[BGRABitmap|English]] | Voir aussi : [[Developing with Graphics/fr|Développer en mode graphique]]
  
===About===
+
===Description===
'''BGRABitmap''' is a set of units designed to handle images with transparency (alpha channel). It allows to perform fast memory image processing and pixel manipulations. The standard version should work on any platform. It has been tested and optimised on Windows and Ubuntu, i.e. widgetsets win32 et gtk2.
+
'''BGRABitmap''' is a set of units designed to modify and create images with transparency (alpha channel). Direct pixel access allow to do fast image processing. The library has beend optimized and tested on Windows and Ubuntu, with widgetsets win32 and gtk2.
  
The main class is ''TBGRABitmap'' which is derived from ''TFPCustomImage''. There is also ''TBGRAPtrBitmap'' which allows you to modify some data already allocated as a BGRA image. The BGRA format consists of four bytes for each pixel (blue, green, red and alpha in that order).
+
The main class is ''TBGRABitmap'' which is derived from ''TFPCustomImage''. There is also ''TBGRAPtrBitmap'' which allows to edit BGRA data the is already allocated. This formats consists in 4 bytes for each pixel (blue, green, red and alpha in that order).
  
There are three basic modes for drawing :
+
===Using BGRABitmap===
* Drawing with alpha blending (you can draw a transparent object)
+
You must copy BGRABitmap units and link them to your project:
* Replacing pixels including alpha channel (you can draw a hole in the bitmap)
+
<delphi>
* Drawing an image except transparent pixels (like a simple sprite)
+
Uses Classes, SysUtils, BGRABitmap, BGRABitmapTypes;
 +
</delphi>
  
You can draw using direct drawing functions embedded in the ''TBGRABitmap'' class or you can access a ''Canvas'' property to draw using standard functions. The second option is slower because it can need some transformations of the picture. To specity the opacity of objects drawn with the ''Canvas'' property, you can specify a ''CanvasOpacity'' value.
+
The unit BGRABitmapTypes contains all necessary definitions, but one can declare only BGRABitmap in order to load and show a bitmap. Then, the first step is to create a TBGRABitmap object:
 +
<delphi>
 +
var bmp: TBGRABitmap;
 +
begin
 +
  bmp := TBGRABitmap.Create(100,100,BGRABlack); //creates a 100x100 pixels image with black background
  
Available direct functions for drawing :
+
  bmp.FillRect(20,20,60,60,BGRAWhite, dmSet); //draws a white sqaure without transparency
* pixels, lines, rectangles
+
  bmp.FillRect(40,40,80,80,BGRA(0,0,255,128), dmDrawWithTransparency); //draws a transparent blue square
* lines with anti-aliasing
+
end;
* round rectangles
+
</delphi>
* text with transparency
 
* flood fill, replace color
 
* loading and saving images
 
* resample, merge images
 
  
To access directly to pixels, use the ''Data'' and ''Scanline'' properties. You need to call ''InvalidateBitmap'' to take changes into account for example if you draw the bitmap on a ''Canvas''. Note that the row order can be reversed according to ''LineOrder'' property.
+
Finally to show the bitmap:
 +
<delphi>
 +
procedure TFMain.FormPaint(Sender: TObject);
 +
begin
 +
  bmp.Draw(Canvas, 0, 0, True); // draw the bitmap in opaque mode (faster)
 +
end;
 +
</delphi>
 +
 
 +
===Notions===
 +
 
 +
Pixels in a bitmap with transparency are stored with 4 values, here 4 bytes in the order Blue, Green, Red, Alpha. The last channel defines the level of opacity (0 signifies transparent, 255 signifies opaque), other channels define color and luminosity.
 +
 
 +
There are basically two drawing modes. The firts consist in replacing the content of the pixel information, the second consists in blending the pixel already here with the new one, which is call alpha blending.
 +
 
 +
BGRABitmap functions propose 3 modes:
 +
* dmSet : replaces the four bytes of the drawn pixel, transparency not handled
 +
* dmDrawWithTransparency : draws with alphablending and with gamma correction (see below)
 +
* dmFastBlend ou dmLinearBlend : draws with alphablending but without gamma correction (faster but entails color distortions with low intensities).
 +
 
 +
===Integrated drawing functions===
 +
 
 +
* draw/erase pixels
 +
* draw a line with or without antialiasing
 +
* floating point coordinates
 +
* floating point pen width
 +
* rectangle (frame or fill)
 +
* ellipse and polygons with antialiasing
 +
* spline computation (rounded curve)
 +
* simple fill Floodfill) or progressive fill
 +
* color gradient rendering (linéaire, radial...)
 +
* round rectangles (without antialiasing)
 +
* texts with transparency
 +
 
 +
===Drawing with the Canvas===
 +
 
 +
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.
 +
 
 +
===Direct access to pixels===
 +
 
 +
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.
 +
 
 +
=== Image manipulation ===
 +
 
 +
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
 +
 
 +
=== Images combination ===
 +
 
 +
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 ===
 
=== Screenshots ===
 
+
[[Image:Lazpaint_contour.png]]
 +
[[Image:Lazpaint curve redim.png]]
 
[[Image:Bgra_wirecube.png]]
 
[[Image:Bgra_wirecube.png]]
 
[[Image:Bgra_chessboard.jpg]]
 
[[Image:Bgra_chessboard.jpg]]
  
===License===
+
===Licence===
Modified LGPL
+
modified LGPL
  
===Download and feedback===
+
===Download===
Sourceforge with LazPaint and BGRABitmap: https://sourceforge.net/projects/lazpaint/files/lazpaint/
+
Sourceforge with [[LazPaint|LazPaint]] et BGRABitmap : https://sourceforge.net/projects/lazpaint/files/lazpaint/
  
Old link: http://consume.o2switch.net/lazarus/
+
Old link : http://consume.o2switch.net/lazarus/
  
 
===Installation===
 
===Installation===
 
Add BGRABitmap and BGRABitmapTypes to the '''uses''' clause.
 
Add BGRABitmap and BGRABitmapTypes to the '''uses''' clause.
 
==See also==
 
 
* [[Developing with Graphics]]
 
 
[[Category:Components]]
 

Revision as of 23:00, 16 February 2011

English | Voir aussi : Développer en mode graphique

Description

BGRABitmap is a set of units designed to modify and create images with transparency (alpha channel). Direct pixel access allow to do fast image processing. The library has beend optimized and tested on Windows and Ubuntu, with widgetsets win32 and gtk2.

The main class is TBGRABitmap which is derived from TFPCustomImage. There is also TBGRAPtrBitmap which allows to edit BGRA data the is already allocated. This formats consists in 4 bytes for each pixel (blue, green, red and alpha in that order).

Using BGRABitmap

You must copy BGRABitmap units and link them to your project: <delphi> Uses Classes, SysUtils, BGRABitmap, BGRABitmapTypes; </delphi>

The unit BGRABitmapTypes contains all necessary definitions, but one can declare only BGRABitmap in order to load and show a bitmap. Then, the first step is to create a TBGRABitmap object: <delphi> var bmp: TBGRABitmap; begin

 bmp := TBGRABitmap.Create(100,100,BGRABlack); //creates a 100x100 pixels image with black background
 bmp.FillRect(20,20,60,60,BGRAWhite, dmSet); //draws a white sqaure without transparency
 bmp.FillRect(40,40,80,80,BGRA(0,0,255,128), dmDrawWithTransparency); //draws a transparent blue square

end; </delphi>

Finally to show the bitmap: <delphi> procedure TFMain.FormPaint(Sender: TObject); begin

 bmp.Draw(Canvas, 0, 0, True); // draw the bitmap in opaque mode (faster)

end; </delphi>

Notions

Pixels in a bitmap with transparency are stored with 4 values, here 4 bytes in the order Blue, Green, Red, Alpha. The last channel defines the level of opacity (0 signifies transparent, 255 signifies opaque), other channels define color and luminosity.

There are basically two drawing modes. The firts consist in replacing the content of the pixel information, the second consists in blending the pixel already here with the new one, which is call alpha blending.

BGRABitmap functions propose 3 modes:

  • dmSet : replaces the four bytes of the drawn pixel, transparency not handled
  • dmDrawWithTransparency : draws with alphablending and with gamma correction (see below)
  • dmFastBlend ou dmLinearBlend : draws with alphablending but without gamma correction (faster but entails color distortions with low intensities).

Integrated drawing functions

  • draw/erase pixels
  • draw a line with or without antialiasing
  • floating point coordinates
  • floating point pen width
  • rectangle (frame or fill)
  • ellipse and polygons with antialiasing
  • spline computation (rounded curve)
  • simple fill Floodfill) or progressive fill
  • color gradient rendering (linéaire, radial...)
  • round rectangles (without antialiasing)
  • texts with transparency

Drawing with the Canvas

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.

Direct access to pixels

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.

Image manipulation

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

Images combination

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

Licence

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.