# TBGRABitmap class

Back to BGRABitmap.

TBGRABitmap class contains an image stored in BGRA 32-bit format. It derives from TBGRADefaultBitmap which derives from TBGRACustomBitmap from which it inherits its properties. So it implements the interface IBGRAScanner which allows it to be used as a texture (like a brush).

### Implementations

TBGRABitmap is a shortcut provided by BGRABitmap unit for the active implementation, depending on the widgetset. It can be one of the following:

• TBGRAWinBitmap on Windows
• TBGRAGtkBitmap on Gtk
• TBGRAQtBitmap when Qt widget is active
• TBGRAfpGUIBitmap when using the package for fpGUI toolkit (bgrabitmappack4fpgui.lpk)
• TBGRANoGUIBitmap when using the package independent of the graphic user interface (bgrabitmappack4nogui.lpk)

### Direct pixel access

The image is stored as BGRA 32-bit:

 PBGRAPixel = ^TBGRAPixel; Pointer for direct pixel access. Data is stored as a sequence of TBGRAPixel. See BGRABitmap tutorial 4 TBGRAPixel = packed record blue, green, red, alpha: byte; end; Each pixel is a sequence of 4 bytes containing blue, green, red and alpha channel. Values range from 0 to 255, color is in sRGB colorspace. The alpha value of 0 is transparent and 255 is opaque

You can either use Data property if you want to go through all pixels, or ScanLine property if you want to do something that depends on the line number. Data returns the first pixel in memory, so that you can read all the pixels from there by increasing the pointer (add 1 to PBGRAPixel). Otherwise, you can do a loop where y goes from 0 to Height-1, and for each line, retrieve with ScanLine the pointer of the beginning of the line. Then you can read all the pixels of the line from there by increasing the pointer.

The following properties of TBGRABitmap allow to access the data directly:

 TBGRABitmap = class(TBGRACustomBitmap) property Width: integer read; Width of the image in pixels property Height: integer read; Height of the image in pixels property NbPixels: integer read; Total number of pixels. It is always true that NbPixels = Width * Height property ScanLine[y: integer]: PBGRAPixel read; Returns the address of the left-most pixel of any line. The parameter y ranges from 0 to Height-1 property LineOrder: TRawImageLineOrder read; Indicates the order in which lines are stored in memory. If it is equal to riloTopToBottom, the first line is the top line. If it is equal to riloBottomToTop, the first line is the bottom line. See miscellaneous types property Data: PBGRAPixel read; Provides a pointer to the first pixel in memory. Depending on the LineOrder property, this can be the top-left pixel or the bottom-left pixel. There is no padding between scanlines, so the start of the next line is at the address Data + Width. See BGRABitmap tutorial 4 property Caption: string read write; Can contain a text to describe the image. It is not displayed. It is the name of the layer if this image is in a layered bitmap.

### TBGRABitmap reference

 TBGRABitmap = class(TBGRACustomBitmap) This class is the base for all TBGRABitmap classes. It implements most function to the exception from implementations specific to the widgetset property CanvasBGRA: TBGRACanvas read; Provides a canvas with opacity and antialiasing property Canvas2D: TBGRACanvas2D read; Provides a canvas with 2d transformation and similar to HTML5.

For more properties, see parent class TBGRACustomBitmap

#### Reference counting

 function NewReference: TBGRACustomBitmap; Adds a reference (this reference count is not the same as the reference count of an interface, it changes only by explicit calls procedure FreeReference; Free a reference. When the resulting reference count gets to zero, the image is freed. The initial reference count is equal to 1 function GetUnique: TBGRACustomBitmap; Returns an object with a reference count equal to 1. Duplicate this bitmap if necessary

#### Constructors

Example:
var bmp1, bmp2: TBGRABitmap;
begin
bmp1 := TBGRABitmap.Create(100,100);
bmp2 := bmp1.NewBitmap(100,100) as TBGRABitmap;
...
end;
See tutorial 2 on how to load and display an image.

#### Pixel functions

 function PtInClipRect(x, y: int32or64): boolean; inline; Checks if the specified point is in the clipping rectangle ClipRect procedure SetPixel(x, y: int32or64; c: TColor); override; Sets the pixel by replacing the content at (x,y) with the specified color. Alpha value is set to 255 (opaque) procedure SetPixel(x, y: int32or64; c: TBGRAPixel); override; Sets the pixel at (x,y) with the specified content procedure XorPixel(x, y: int32or64; c: TBGRAPixel); override; Applies a logical xor to the content of the pixel with the specified value. This includes the alpha channel, so if you want to preserve the opacity, provide a color c with alpha channel equal to zero procedure DrawPixel(x, y: int32or64; c: TBGRAPixel); override; Draws a pixel with gamma correction at (x,y). Pixel is supplied in sRGB colorspace procedure DrawPixel(x, y: int32or64; c: TBGRAPixel; ADrawMode: TDrawMode); overload; Draws a pixel with the specified ADrawMode at (x,y). Pixel is supplied in sRGB colorspace. Gamma correction may be applied depending on the draw mode procedure DrawPixel(x, y: int32or64; ec: TExpandedPixel); override; Draws a pixel with gamma correction at (x,y). Pixel is supplied in gamma expanded colorspace procedure FastBlendPixel(x, y: int32or64; c: TBGRAPixel); override; Draws a pixel without gamma correction at (x,y). Pixel is supplied in sRGB colorspace procedure ErasePixel(x, y: int32or64; alpha: byte); override; Erase the content of the pixel by reducing the value of the alpha channel. alpha specifies how much to decrease. If the resulting alpha reaches zero, the content is replaced by BGRAPixelTransparent procedure AlphaPixel(x, y: int32or64; alpha: byte); override; Sets the alpha value at (x,y). If alpha = 0, the pixel is replaced by BGRAPixelTransparent function GetPixel(x, y: int32or64): TBGRAPixel; override; Returns the content of the specified pixel. If it is out of the bounds of the picture, the result is BGRAPixelTransparent function GetPixel(x, y: single; AResampleFilter: TResampleFilter = rfLinear; smoothBorder: boolean = true): TBGRAPixel; override; Computes the value of the pixel at a floating point coordiante by interpolating the values of the pixels around it. There is a one pixel wide margin around the pixel where the pixels are still considered inside. If smoothBorder is set to true, pixel fade to transparent. If it is more out of the bounds, the result is BGRAPixelTransparent. AResampleFilter specifies how pixels must be interpolated. Accepted values are rfBox, rfLinear, rfHalfCosine and rfCosine function GetPixel256(x, y, fracX256,fracY256: int32or64; AResampleFilter: TResampleFilter = rfLinear; smoothBorder: boolean = true): TBGRAPixel; override; Similar to previous GetPixel function, but the fractional part of the coordinate is supplied with a number from 0 to 255. The actual coordinate is (x + fracX256/256, y + fracY256/256) function GetPixelCycle(x, y: single; AResampleFilter: TResampleFilter = rfLinear): TBGRAPixel; override; Computes the value of the pixel at a floating point coordiante by interpolating the values of the pixels around it. If the pixel is out of bounds, the image is repeated. AResampleFilter specifies how pixels must be interpolated. Accepted values are rfBox, rfLinear, rfHalfCosine and rfCosine function GetPixelCycle256(x, y, fracX256,fracY256: int32or64; AResampleFilter: TResampleFilter = rfLinear): TBGRAPixel; override; Similar to previous GetPixel function, but the fractional part of the coordinate is supplied with a number from 0 to 255. The actual coordinate is (x + fracX256/256, y + fracY256/256) function GetPixelCycle(x, y: single; AResampleFilter: TResampleFilter; repeatX: boolean; repeatY: boolean): TBGRAPixel; override; Computes the value of the pixel at a floating point coordiante by interpolating the values of the pixels around it. repeatX and repeatY specifies if the image is to be repeated or not. AResampleFilter specifies how pixels must be interpolated. Accepted values are rfBox, rfLinear, rfHalfCosine and rfCosine function GetPixelCycle256(x, y, fracX256,fracY256: int32or64; AResampleFilter: TResampleFilter; repeatX: boolean; repeatY: boolean): TBGRAPixel; override; Similar to previous GetPixel function, but the fractional part of the coordinate is supplied with a number from 0 to 255. The actual coordinate is (x + fracX256/256, y + fracY256/256)

#### Drawing lines and polylines (integer coordinates)

These functions do not take into account current pen style/cap/join. See coordinate system.

 procedure SetHorizLine(x, y, x2: int32or64; c: TBGRAPixel); override; Replaces the content of the pixels at line y and at columns x to x2 included, using specified color procedure XorHorizLine(x, y, x2: int32or64; c: TBGRAPixel); override; Applies xor to the pixels at line y and at columns x to x2 included, using specified color. This includes the alpha channel, so if you want to preserve the opacity, provide a color c with alpha channel equal to zero procedure DrawHorizLine(x, y, x2: int32or64; c: TBGRAPixel); override; Draws an horizontal line with gamma correction at line y and at columns x to x2 included, using specified color procedure DrawHorizLine(x, y, x2: int32or64; ec: TExpandedPixel); override; Draws an horizontal line with gamma correction at line y and at columns x to x2 included, using specified color procedure DrawHorizLine(x, y, x2: int32or64; texture: IBGRAScanner); overload; Draws an horizontal line with gamma correction at line y and at columns x to x2 included, using specified scanner to get the source colors procedure FastBlendHorizLine(x, y, x2: int32or64; c: TBGRAPixel); override; Draws an horizontal line without gamma correction at line y and at columns x to x2 included, using specified color procedure HorizLine(x, y, x2: int32or64; texture: IBGRAScanner; ADrawMode : TDrawMode); override; Draws an horizontal line at line y and at columns x to x2 included, using specified scanner and the specified ADrawMode procedure HorizLine(x,y,x2: Int32or64; c: TBGRAPixel; ADrawMode: TDrawMode); overload; Draws an horizontal line at line y and at columns x to x2 included, using specified color and the specified ADrawMode procedure AlphaHorizLine(x, y, x2: int32or64; alpha: byte); override; Replaces the alpha value of the pixels at line y and at columns x to x2 included procedure DrawHorizLineDiff(x, y, x2: int32or64; c, compare: TBGRAPixel; Draws an horizontal line with gamma correction at line y and at columns x to x2 included, using specified color, and with a transparency that increases with the color difference with compare. If the difference is greater than maxDiff, pixels are not changed procedure SetVertLine(x, y, y2: int32or64; c: TBGRAPixel); override; Replaces a vertical line at column x and at row y to y2 procedure XorVertLine(x, y, y2: int32or64; c: TBGRAPixel); override; Xors a vertical line at column x and at row y to y2 procedure DrawVertLine(x, y, y2: int32or64; c: TBGRAPixel); override; Draws a vertical line with gamma correction at column x and at row y to y2 procedure FastBlendVertLine(x, y, y2: int32or64; c: TBGRAPixel); override; Draws a vertical line without gamma correction at column x and at row y to y2 procedure AlphaVertLine(x, y, y2: int32or64; alpha: byte); override; Replace alpha values in a vertical line at column x and at row y to y2 procedure VertLine(x,y,y2: Int32or64; c: TBGRAPixel; ADrawMode: TDrawMode); Draws a vertical line with the specified draw mode at column x and at row y to y2 procedure DrawLine(x1, y1, x2, y2: integer; c: TBGRAPixel; DrawLastPixel: boolean; ADrawMode: TDrawMode = dmDrawWithTransparency); override; Draws an aliased line from (x1,y1) to (x2,y2) using Bresenham's algorithm c specifies the color. DrawLastPixel specifies if (x2,y2) must be drawn. ADrawMode specifies the mode to use when drawing the pixels procedure DrawLineAntialias(x1, y1, x2, y2: integer; c: TBGRAPixel; DrawLastPixel: boolean); override; Draws an antialiased line from (x1,y1) to (x2,y2) using an improved version of Bresenham's algorithm c specifies the color. DrawLastPixel specifies if (x2,y2) must be drawn procedure DrawLineAntialias(x1, y1, x2, y2: integer; c1, c2: TBGRAPixel; dashLen: integer; DrawLastPixel: boolean); override; Draws an antialiased line with two colors c1 and c2 as dashes of lenght dashLen procedure DrawLineAntialias(x1, y1, x2, y2: integer; c1, c2: TBGRAPixel; dashLen: integer; DrawLastPixel: boolean; var DashPos: integer); override; Draws an antialiased line with two colors c1 and c2 as dashes of lenght dashLen. DashPos can be used to specify the start dash position and to retrieve the dash position at the end of the line, in order to draw a polyline with consistent dashes procedure EraseLine(x1, y1, x2, y2: integer; alpha: byte; DrawLastPixel: boolean); override; Erases the line from (x1,y1) to (x2,y2) using Bresenham's algorithm. alpha specifies how much to decrease. If alpha = 0, nothing is changed and if alpha = 255, all pixels become transparent. DrawListPixel specifies if (x2,y2) must be changed procedure EraseLineAntialias(x1, y1, x2, y2: integer; alpha: byte; DrawLastPixel: boolean); override; Erases the line from (x1,y1) to (x2,y2) width antialiasing. alpha specifies how much to decrease. If alpha = 0, nothing is changed and if alpha = 255, all pixels become transparent. DrawListPixel specifies if (x2,y2) must be changed

#### Drawing lines and polylines (floating point coordinates)

These functions use the current pen style/cap/join. The parameter w specifies the width of the line and the base unit for dashes. See coordinate system.

 procedure DrawLineAntialias(x1, y1, x2, y2: single; c: TBGRAPixel; w: single); override; Draws a line from (x1,y1) to (x2,y2) using current pen style/cap/join procedure DrawLineAntialias(x1, y1, x2, y2: single; texture: IBGRAScanner; w: single); override; Draws a line from (x1,y1) to (x2,y2) using current pen style/cap/join. texture specifies the source color to use when filling the line procedure DrawLineAntialias(x1, y1, x2, y2: single; c: TBGRAPixel; w: single; Closed: boolean); override; Draws a line from (x1,y1) to (x2,y2) using current pen style/cap/join. Closed specifies if the end of the line is closed. If it is not closed, a space is left so that the next line can fit procedure DrawLineAntialias(x1, y1, x2, y2: single; texture: IBGRAScanner; w: single; Closed: boolean); override; Same as above with texture specifying the source color to use when filling the line procedure DrawPolyLineAntialias(const points: array of TPointF; c: TBGRAPixel; w: single); override; Draws a polyline using current pen style/cap/join procedure DrawPolyLineAntialias(const points: array of TPointF; texture: IBGRAScanner; w: single); override; Draws a polyline using current pen style/cap/join. texture specifies the source color to use when filling the line procedure DrawPolyLineAntialias(const points: array of TPointF; c: TBGRAPixel; w: single; Closed: boolean); override; Draws a polyline using current pen style/cap/join. Closed specifies if the end of the line is closed. If it is not closed, a space is left so that the next line can fit procedure DrawPolyLineAntialias(const points: array of TPointF; c: TBGRAPixel; w: single; fillcolor: TBGRAPixel); override; Draws a polyline using current pen style/cap/join. fillcolor specifies a color to fill the polygon formed by the points procedure DrawPolyLineAntialiasAutocycle(const points: array of TPointF; c: TBGRAPixel; w: single); override; Draws a polyline using current pen style/cap/join. The last point considered as a join with the first point if it has the same coordinate procedure DrawPolygonAntialias(const points: array of TPointF; c: TBGRAPixel; w: single); override; Draws a polygon using current pen style/cap/join. The polygon is always closed. You don't need to set the last point to be the same as the first point procedure DrawPolygonAntialias(const points: array of TPointF; texture: IBGRAScanner; w: single); override; Draws a polygon using current pen style/cap/join. The polygon is always closed. You don't need to set the last point to be the same as the first point procedure DrawPolygonAntialias(const points: array of TPointF; c: TBGRAPixel; w: single; fillcolor: TBGRAPixel); override; Draws a filled polygon using current pen style/cap/join. The polygon is always closed. You don't need to set the last point to be the same as the first point. procedure EraseLineAntialias(x1, y1, x2, y2: single; alpha: byte; w: single); override; Erases a line from (x1,y1) to (x2,y2) using current pen style/cap/join procedure EraseLineAntialias(x1, y1, x2, y2: single; alpha: byte; w: single; Closed: boolean); override; Erases a line from (x1,y1) to (x2,y2) using current pen style/cap/join. Closed specifies if the end of the line is closed. If it is not closed, a space is left so that the next line can fit procedure ErasePolyLineAntialias(const points: array of TPointF; alpha: byte; w: single); override; Erases a polyline using current pen style/cap/join

#### Rectangles (integer coordinates)

The integer coordinates of rectangles interpreted such that that the bottom/right pixels are not drawn. The width is equal to x2-x, and pixels are drawn from x to x2-1. If x = x2, then nothing is drawn. See coordinate system.
These functions do not take into account current pen style/cap/join. They draw a continuous 1-pixel width border

 procedure Rectangle(x, y, x2, y2: integer; c: TBGRAPixel; mode: TDrawMode); override; Draw a size border of a rectangle, using the specified mode procedure Rectangle(x, y, x2, y2: integer; BorderColor, FillColor: TBGRAPixel; mode: TDrawMode); override; Draw a filled rectangle with a border of color BorderColor, using the specified mode procedure FillRect(x, y, x2, y2: integer; c: TBGRAPixel; mode: TDrawMode); override; overload; Fills completely a rectangle, without any border, with the specified mode procedure FillRect(x, y, x2, y2: integer; texture: IBGRAScanner; mode: TDrawMode); override; overload; Fills completely a rectangle, without any border, with the specified texture and with the specified mode procedure AlphaFillRect(x, y, x2, y2: integer; alpha: byte); override; Sets the alpha value within the specified rectangle procedure RoundRect(X1, Y1, X2, Y2: integer; DX, DY: integer; BorderColor, FillColor: TBGRAPixel; ADrawMode: TDrawMode = dmDrawWithTransparency); override; Draws a filled round rectangle, with corners having an elliptical diameter of DX and DY procedure RoundRect(X1, Y1, X2, Y2: integer; DX, DY: integer; BorderColor: TBGRAPixel; ADrawMode: TDrawMode = dmDrawWithTransparency); override; Draws a round rectangle, with corners having an elliptical diameter of DX and DY

#### Rectangles and ellipses (floating point coordinates)

These functions use the current pen style/cap/join. The parameter w specifies the width of the line and the base unit for dashes
The coordinates are pixel-centered, so that when filling a rectangle, if the supplied values are integers, the border will be half transparent. If you want the border to be completely filled, you can subtract/add 0.5 to the coordinates to include the remaining thin border. See coordinate system.

 procedure RectangleAntialias(x, y, x2, y2: single; c: TBGRAPixel; w: single; back: TBGRAPixel); override; Draws a rectangle with antialiasing and fills it with color back. Note that the pixel (x2,y2) is included contrary to integer coordinates procedure RectangleAntialias(x, y, x2, y2: single; texture: IBGRAScanner; w: single); override; Draws a rectangle with antialiasing. Note that the pixel (x2,y2) is included contrary to integer coordinates procedure FillRectAntialias(x, y, x2, y2: single; c: TBGRAPixel); override; Fills a rectangle with antialiasing. For example (-0.5,-0.5,0.5,0.5) fills one pixel procedure FillRectAntialias(x, y, x2, y2: single; texture: IBGRAScanner); override; Fills a rectangle with a texture procedure EraseRectAntialias(x, y, x2, y2: single; alpha: byte); override; Erases the content of a rectangle with antialiasing procedure RoundRectAntialias(x,y,x2,y2,rx,ry: single; c: TBGRAPixel; w: single; options: TRoundRectangleOptions = []); override; Draws a rounded rectangle border with antialiasing. The corners have an elliptical radius of rx and ry. options specifies how to draw the corners. See geometry types procedure RoundRectAntialias(x,y,x2,y2,rx,ry: single; texture: IBGRAScanner; w: single; options: TRoundRectangleOptions = []); override; Draws a rounded rectangle border with the specified texture. The corners have an elliptical radius of rx and ry. options specifies how to draw the corners. See geometry types procedure RoundRectAntialias(x,y,x2,y2,rx,ry: single; pencolor: TBGRAPixel; w: single; fillcolor: TBGRAPixel; options: TRoundRectangleOptions = []); override; Draws and fills a round rectangle procedure RoundRectAntialias(x,y,x2,y2,rx,ry: single; penTexture: IBGRAScanner; w: single; fillTexture: IBGRAScanner; options: TRoundRectangleOptions = []); override; Draws and fills a round rectangle with textures procedure FillRoundRectAntialias(x,y,x2,y2,rx,ry: single; c: TBGRAPixel; options: TRoundRectangleOptions = []); override; Fills a rounded rectangle with antialiasing. The corners have an elliptical radius of rx and ry. options specifies how to draw the corners. See geometry types procedure FillRoundRectAntialias(x,y,x2,y2,rx,ry: single; texture: IBGRAScanner; options: TRoundRectangleOptions = []); override; Fills a rounded rectangle with a texture procedure EraseRoundRectAntialias(x,y,x2,y2,rx,ry: single; alpha: byte; options: TRoundRectangleOptions = []); override; Erases the content of a rounded rectangle with a texture procedure EllipseAntialias(x, y, rx, ry: single; c: TBGRAPixel; w: single); override; Draws an ellipse with antialising. rx is the horizontal radius and ry the vertical radius procedure EllipseAntialias(x, y, rx, ry: single; texture: IBGRAScanner; w: single); override; Draws an ellipse border with a texture procedure EllipseAntialias(x, y, rx, ry: single; c: TBGRAPixel; w: single; back: TBGRAPixel); override; Draws and fills an ellipse procedure FillEllipseAntialias(x, y, rx, ry: single; c: TBGRAPixel); override; Fills an ellipse procedure FillEllipseAntialias(x, y, rx, ry: single; texture: IBGRAScanner); override; Fills an ellipse with a texture procedure FillEllipseLinearColorAntialias(x, y, rx, ry: single; outercolor, innercolor: TBGRAPixel); override; Fills an ellipse with a gradient of color. outercolor specifies the end color of the gradient on the border of the ellipse and innercolor the end color of the gradient at the center of the ellipse procedure EraseEllipseAntialias(x, y, rx, ry: single; alpha: byte); override; Erases the content of an ellipse

### Text fonctions

class TBGRABitmap
property FontQuality : TBGRAFontQuality; read write;
property FontPixelMetric: TFontPixelMetric;
function TextSize(sUTF8: string): TSize;