BGRABitmap/zh CN

From Free Pascal wiki
Jump to navigationJump to search

说明

BGRABitmap 是一个用来创建和更改透明图像(具有alpha 通道)的单元集. 直接像素访问允许进行快速的图像处理. 这个库已在 Windows, Ubuntu 和 Mac OS X 平台测试通过(最后的版本不能在 Mac平台工作), 以及各平台的小部件集 win32, gtk1, gtk2 和 carbon.

其中主要的类是 TBGRABitmap, 派生自 TFPCustomImage. 还有一个类 TBGRAPtrBitmap 允许编辑已经分配的 BGRA 数据. 每个像素的格式包括4个字节 (顺序为 蓝, 绿, 红 和 alpha.

使用 BGRABitmap

教程

概述

为了便于理解,函数的命名比较长. 其中的任何功能如函数或使用TBGRABitmap对象的属性都是容易理解的. 例如, 你可以使用CanvasBGRA作为画布,这和TCanvas类似 (但具有透明度和反锯齿),并且可以使用Canvas2D来实现类似 HTML canvas的一些功能.

一些特定的功能需要依赖一些单元的引用, 但你可能不需要使用它们 :

  • TBGRAMultishapeFiller 实现多边形的反锯齿节点(交叉点),在 BGRAPolygon 单元中
  • TBGRATextEffect 在 BGRATextFX 单元中
  • 2D 转换在 BGRATransform 单元中
  • TBGRAScene3D 在 BGRAScene3D 单元中
  • 如果你需要用到层, BGRALayers 单元提供了 TBGRALayeredBitmap

双缓冲并不包含在 BGRABitmap 单元中, 因为其更多的是关于如何处理窗口. 如果要使用双缓冲技术, 你可以使用 TBGRAVirtualScreen ,包含在 BGRAControls 组件包中. 此外, BGRABitmap中双缓冲和其它任何缓冲用法一样. 你需要一个位图来存储你的绘图,然后使用一条绘图指令将其显示出来.

简单示例

你必须在你的工程中引用 BGRABitmap 单元:

Uses Classes, SysUtils, BGRABitmap, BGRABitmapTypes;

单元 BGRABitmapTypes 包含所有必要的定义, 对于仅仅加载和显示位图,可以只声明引用 BGRABitmap. 然后, 首先创建一个 TBGRABitmap 对象:

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 square without transparency
  bmp.FillRect(40,40,80,80,BGRA(0,0,255,128), dmDrawWithTransparency); //draws a transparent blue square
end;

最后显示位图:

procedure TFMain.FormPaint(Sender: TObject);
begin
  bmp.Draw(Canvas, 0, 0, True); // draw the bitmap in opaque mode (faster)
end;

概念

位图中带有透明特性的像素被保存为4个值, 这里是4个字节,其顺序为 蓝, 绿, 红, Alpha. 最后一个通道定义了透明度 (0 代表 完全透明, 255 代表完全不透明), 其它通道定义了颜色和亮度.

基本上又两种绘图模式. 第一种是直接替换像素信息, 第二种是已有的像素和新的进行混合, 称为alpha通道混合.

BGRABitmap 的函数提供4种模式:

  • dmSet : 替换绘制像素的4个字节, 不处理透明度
  • dmDrawWithTransparency : 使用 alpha混合绘制并进行 gamma 矫正 (see below)
  • dmFastBlend or dmLinearBlend : 使用alpha混合绘制但不进行gamma矫正 (更快速 但会带有轻度的颜色畸变).
  • dmXor : 对个组件进行异或(Xor)操作,包括alpha (如果只反相颜色而保持alpha, 使用 BGRA(255,255,255,0) )

集成的绘图功能

  • 绘制/擦除 像素
  • 画线(带或不带抗锯齿)
  • 浮点坐标
  • 浮点画笔宽度
  • 矩形 (边框 或 填充)
  • 椭圆 和 多边形 (带有抗锯齿)
  • 样条计算 (园曲线)
  • 简单填充 (Floodfill) 或 渐进式填充
  • 颜色渐变渲染 (线性, 放射...)
  • 圆角矩形
  • 带透明特性的文本

使用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. If you can, use CanvasBGRA instead, which allows transparency and antialiasing while having the same function names as with TCanvas.

直接像素访问

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

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 channel
    inc(p);
  end;
  bmp.InvalidateBitmap;  //note that we have accessed pixels directly
  bmp.Draw(Canvas,0,0,True);
  bmp.Free;
end;

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.

See also the comparison between direct pixel access methods.

图像处理

Available filters (prefixed with Filter) :

  • Radial blur : non directional blur
  • Motion blur : directional blur
  • Custom blur : blur according to a mask
  • Median : computes the median of colors around each pixel, which softens corners
  • Pixelate : simplifies the image with rectangles of the same color
  • Smooth : soften whole image, complementary to Sharpen
  • Sharpen : makes contours more accute, complementary 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 : uses whole range of color luminosity
  • Rotate : rotation of the image around a point
  • Sphere : distorts the image to make it look like projected on a sphere
  • Twirl : distorts the image with a twirl effect
  • Cylinder : distorts the image to make it look like projected on a cylinder
  • Plane : computes a high precision projection on a horizontal plane. This is quite slow.
  • SmartZoom3 : resizes the image x3 and detects borders, to have a useful zoom with ancient games sprites

Some functions are not prefixed with Filter, because they do not return a newly allocated image. They modify the image in-place :

  • VerticalFlip : flips the image vertically
  • HorizontalFlip : flips the image horizontally
  • Negative : inverse of colors
  • LinearNegative : inverse without gamma correction
  • SwapRedBlue : swap red and blue channels (to convert between BGRA and RGBA)
  • ConvertToLinearRGB : to convert from sRGB to RGB. Note the format used by BGRABitmap is sRGB when using

dmDrawWithTransparency and RGB when using dmLinearBlend.

  • ConvertFromLinearRGB : convert from RGB to sRGB.

图像混合

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

These modes can be used in TBGRALayeredBitmap, which makes it easier because BlendImage only provides the basic blending operations.

屏幕快照

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

许可

modified LGPL

Author: Johann ELSASS (Facebook)

下载

Sourceforge with LazPaint and BGRABitmap : http://sourceforge.net/projects/lazpaint/files/src/

Old link : http://lazarus.johann-elsass.net/

If you did not find the information you are looking for or have a suggestion, you can send feedback here.