Recent

Author Topic: BGRABitmap tutorial  (Read 302575 times)

circular

  • Hero Member
  • *****
  • Posts: 2912
    • Personal webpage
Re: BGRABitmap tutorial
« Reply #330 on: October 11, 2012, 05:37:28 pm »
New version of BGRABitmap (v6.1)
1) OpenRaster and Paint.NET format not automatically registered.
2) FreeType support added (need Lazarus 1.0)
3) Pixelwise EraseLine and EraseLineAntialias
4) GetDifferenceBounds : compute the area where an image has changed

1) OpenRaster and Paint.NET format are not automatically registered now. It means that if you want to be able to load ORA and PDN files with classic TBitmap.LoadFromFile, you need first to call :
Code: [Select]
uses BGRAOpenRaster, BGRAPaintNET;

begin
  RegisterOpenRasterFormat;
  RegisterPaintNetFormat;
end;

Why ? Because it takes some space. On Windows 64-bit, when BGRABitmap is compiled with optimizations and no debugger info, it takes 536 Ko only, and if you want OpenRaster and Paint.NET format support and call the above procedures, it will be 774 Ko.

Note that those formats are still registered whenever a TPaintDotNetFile or a TBGRAOpenRasterDocument is created.

2) To use FreeType, you need to add EasyLazFreeType and BGRAFreeType units. Here is an example :
Code: [Select]
unit unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  //units added here
  EasyLazFreeType,             //FreeType support
  BGRABitmap, BGRABitmapTypes, //BGRABitmap
  BGRAFreeType;                //BGRABitmap FreeType support

type

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormPaint(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    ftfont: TFreeTypeFont;
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  ftfont := TFreeTypeFont.Create;
  ftfont.Name := 'Arial.ttf';
  ftfont.SizeInPixels := 30;
  ftfont.ClearType := True;
end;

procedure TForm1.FormPaint(Sender: TObject);
var
  bmp: TBGRABitmap;
  drawer: TBGRAFreeTypeDrawer;
begin
  bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,ColorToRGB(clBtnFace));
  drawer := TBGRAFreeTypeDrawer.Create(bmp);
  drawer.DrawText('Hello world',ftfont,0,0,BGRABlack,[ftaTop,ftaLeft]);
  drawer.Free;
  bmp.Draw(Canvas,0,0);
  bmp.Free;
end;

end.
Conscience is the debugger of the mind

circular

  • Hero Member
  • *****
  • Posts: 2912
    • Personal webpage
Re: BGRABitmap tutorial
« Reply #331 on: October 20, 2012, 12:59:32 pm »
New version of BGRABitmap (6.2) with BGRASliceScaling.

http://sourceforge.net/projects/lazpaint/files/src/

How to use it : you can create a TBGRASliceScaling object providing an image and margins and then use the Draw function to draw it on a TBGRABitmap. The SliceRepeat property allows to defined if parts (Top,Bottom,Left,Right,Middle) should be stretched or repeated.

TBGRAMultiSliceScaling does the same, except you provide an image containing multiple images (of different states for example).

See BGRAControls thread (customdrawn_win7) for an example of usage with CustomDrawn controls.
Conscience is the debugger of the mind

Osvaldo Arruda

  • New member
  • *
  • Posts: 27
Re: BGRABitmap tutorial
« Reply #332 on: October 20, 2012, 01:45:12 pm »
BGRABitmap component (6.2) does not compile/install on Lazarus for Linux 1.0.2

Error:
{$IFDEF LCLgtk2}
type TGtkDeviceContext = TGtk2DeviceContext;
{$ENDIF}   

/home/osvaldo/lazarus/components/bgrabitmap6.2/bgragtkbitmap.pas(70,44) Error: Identifier not found "TGtk2DeviceContext"
/home/osvaldo/lazarus/components/bgrabitmap6.2/bgragtkbitmap.pas(70,44) Error: Error in type definition
/home/osvaldo/lazarus/components/bgrabitmap6.2/bgragtkbitmap.pas(144,5) Error: Illegal type conversion: "HDC" to "TGtkDeviceContext"
/home/osvaldo/lazarus/components/bgrabitmap6.2/bgragtkbitmap.pas(145,5) Error: Illegal type conversion: "HDC" to "TGtkDeviceContext"
/home/osvaldo/lazarus/components/bgrabitmap6.2/bgragtkbitmap.pas(147,5) Error: Illegal type conversion: "HDC" to "TGtkDeviceContext"
/home/osvaldo/lazarus/components/bgrabitmap6.2/bgragtkbitmap.pas(148,5) Error: Illegal type conversion: "HDC" to "TGtkDeviceContext"
/home/osvaldo/lazarus/components/bgrabitmap6.2/bgragtkbitmap.pas(248,10) Error: Illegal type conversion: "HDC" to "TGtkDeviceContext"
/home/osvaldo/lazarus/components/bgrabitmap6.2/bgragtkbitmap.pas(253,25) Error: Illegal type conversion: "HDC" to "TGtkDeviceContext"
/home/osvaldo/lazarus/components/bgrabitmap6.2/bgragtkbitmap.pas(254,5) Error: Illegal type conversion: "HDC" to "TGtkDeviceContext"
/home/osvaldo/lazarus/components/bgrabitmap6.2/bgragtkbitmap.pas(292,5) Error: Illegal type conversion: "HDC" to "TGtkDeviceContext"
/home/osvaldo/lazarus/components/bgrabitmap6.2/bgragtkbitmap.pas(294,5) Error: Illegal type conversion: "HDC" to "TGtkDeviceContext"
/home/osvaldo/lazarus/components/bgrabitmap6.2/bgragtkbitmap.pas(295,5) Error: Illegal type conversion: "HDC" to "TGtkDeviceContext"
/home/osvaldo/lazarus/components/bgrabitmap6.2/bgragtkbitmap.pas(304) Fatal: There were 12 errors compiling module, stopping

 >:(

circular

  • Hero Member
  • *****
  • Posts: 2912
    • Personal webpage
Re: BGRABitmap tutorial
« Reply #333 on: October 20, 2012, 02:25:03 pm »
Look at this thread and stop complaining
 >:(

http://www.lazarus.freepascal.org/index.php/topic,18618.0.html
Conscience is the debugger of the mind

circular

  • Hero Member
  • *****
  • Posts: 2912
    • Personal webpage
Re: BGRABitmap tutorial
« Reply #334 on: November 01, 2012, 06:41:50 pm »
Hello people,

I've added new features (on subversion for now) for vectorization and vectorized fonts.

New units :
- BGRAVectorize unit :
   o VectorizeMonochrome function analyze a black drawing on a white background and transforms it into a poly-polygon
   o TBGRAVectorizedFont class is a typewriter implementation that vectorizes system fonts so you can then draw it and measure all characters
- BGRATypeWriter unit :
   o TBGRACustomTypeWriter provides a skeleton for creating vectorial fonts
   o TBGRAPolygonalGlyph handles polygonal glyphs and can draw them as quadratic curves

Some features added to Canvas2D :
- polylineTo : same as lineTo several times
- toSpline : transform last shape into spline
- fillOverStroke : fill and draw the stroke where there is no fill
- strokeOverFill : stroke and fill the shape where there is no stroke

BGRAText now provides :
- FontEmHeightSign : sign to use for defining the em height in classic TFont object
- FontFullHeightSign : sign to use for defining the full height in a classic TFont object

How to use vectorized font :
Code: [Select]
//supposing you have Bitmap: TBGRABitmap
var vf : TBGRAVectorizedFont;
begin
  vf := TBGRAVectorizedFont.Create;
  vf.Name := 'Times New Roman';
  vf.FullHeight := 50;
  Bitmap.Canvas2D.fillStyle(BGRA(255,0,0,255));
  vf.DrawTextRect(Bitmap.Canvas2D, 'Some text', 0,0);   
  vf.Free;
end;
Note that you should keep TBGRAVectorialFont in memory to avoid loading characters each time you draw text. As you can see, you must use a Canvas2D object. It means that you can apply any effect with the Canvas2D object.

By default, TBGRAVectorialFont simply fills the font, but you can specify various options :
- OutlineMode : what to do exactly, i.e, define a path, stroke, fill, fill and stroke... (font "outline" effect)
- Style : the font style
- ItalicSlope : an italic slope added to the font, whatever the Style property is
- QuadraticCurves : you can deactive quadratic curves for faster rendering
- Orientation : rotation in tenth of degrees
- FontMatrix : any affine matrix transformation
- OnWordBreak : to define your own word breaking handler

More functions :
- DrawTextWordBreak and DrawTextRect : draw text with word break.
- GetTextGlyphBoxes, GetTextWordBreakGlyphBoxes, GetTextRectGlyphBoxes : returns an array of boxes that contains glyphs
- GetTextSize : returns text size (like classic function)
...
Conscience is the debugger of the mind

circular

  • Hero Member
  • *****
  • Posts: 2912
    • Personal webpage
Re: BGRABitmap tutorial
« Reply #335 on: November 05, 2012, 09:43:54 pm »
New svn address : svn checkout svn://svn.code.sf.net/p/lazpaint/code/ lazpaint-code
Conscience is the debugger of the mind

lainz

  • Guest
Re: BGRABitmap tutorial
« Reply #336 on: November 06, 2012, 02:43:32 am »
Ok thanks =)

circular

  • Hero Member
  • *****
  • Posts: 2912
    • Personal webpage
Re: BGRABitmap tutorial
« Reply #337 on: January 13, 2013, 11:22:22 pm »
Some minor updates on subversion :

- bug fix for blur on 64 bit platform (also faster by the way)
- no need anymore to write GetPixel(integer(...),integer(...)) because added GetPixel(int64,int64)
- optimization of polygon scanning (use TOnePassFillPolyInfo instead of TFillPolyInfo if possible)
Conscience is the debugger of the mind

circular

  • Hero Member
  • *****
  • Posts: 2912
    • Personal webpage
Re: BGRABitmap tutorial
« Reply #338 on: January 17, 2013, 10:46:00 am »
New zip version BGRABitmap 6.3 including latest bug fixes and optimizations :

http://sourceforge.net/projects/lazpaint/files/src/
Conscience is the debugger of the mind

circular

  • Hero Member
  • *****
  • Posts: 2912
    • Personal webpage
Re: BGRABitmap tutorial
« Reply #339 on: January 30, 2013, 12:40:13 pm »
New version of BGRABitmap 6.4 :
- works with NoGui (you need to use vectorized fonts or freetype fonts in this case)
- vectorized fonts can be saved and loaded
- FontRenderer property added to TBGRABitmap with two possible values : LCL renderer or vectorized font renderer

http://sourceforge.net/projects/lazpaint/files/src/

How to use vectorized font that are saved in *.glyphs files :
Code: [Select]
uses BGRAVectorize;
...
var img: TBGRABitmap;
begin
  ...
  img.FontRenderer := TBGRAVectorizedFontRenderer.Create('.'); //define your directory here

Here is a test project for TAChart and NoGui
« Last Edit: February 01, 2013, 01:53:28 pm by circular »
Conscience is the debugger of the mind

circular

  • Hero Member
  • *****
  • Posts: 2912
    • Personal webpage
Re: BGRABitmap tutorial
« Reply #340 on: February 01, 2013, 01:31:01 am »
New version of BGRABitmap 6.5. This needs latest SVN of units EasyLazFreeType, LazFreeTypeFontCollection and TTCMap of package LazUtils.

There are now 3 rendering methods for text with TBGRABitmap objects:
- using LCL (default, does not work with NoGUI)
- using vectorized fonts (using a fileformat i've made *.glyphs)
- using LazFreeType (using TTF files)

Here is a test program, to test and compare the 3 renders, and generate *.glyphs files :
http://lazarus.johann-elsass.net/fontrendering.zip

LCL is default font rendering. To set to LCL rendering :
Code: [Select]
var bmp: TBGRABitmap;
begin
   ...
   bmp.FontRenderer := nil; //need to do that only if you switched to another renderer before that
end;

To set to vectorized font rendering :
Code: [Select]
uses BGRAVectorize;
...
begin
   bmp.FontRenderer := Bitmap.FontRenderer := TBGRAVectorizedFontRenderer.Create(ExtractFilePath(APath);
where APath is the directory of *.glyphs files.

To set to FreeType rendering :
Code: [Select]
uses BGRAFreeType, LazFreeTypeFontCollection;

var FFreeTypeCollection : TFreeTypeFontCollection;
...
begin
  //create font collection
  FFreeTypeCollection := TFreeTypeFontCollection.Create;
  FFreeTypeCollection.AddFolder(APath);
  //use it in LazFreeType
  SetDefaultFreeTypeFontCollection(FFreeTypeCollection);
  //set the renderer
  bmp := TBGRABitmap.Create(200,200);
  bmp.FontRenderer := TBGRAFreeTypeFontRenderer.Create;

  ...

   bmp.Free;
   SetDefaultFreeTypeFontCollection(nil);
   FFreeTypeCollection.Free;
end;
where APath is a directory containing *.ttf files.
« Last Edit: February 03, 2013, 04:33:33 pm by circular »
Conscience is the debugger of the mind

Silvio Clécio

  • Guest
Re: BGRABitmap tutorial
« Reply #341 on: February 02, 2013, 04:22:51 am »
Very nice job buddy!

I'm dreaming day and night to make this possible in Linux. *u*

circular

  • Hero Member
  • *****
  • Posts: 2912
    • Personal webpage
Re: BGRABitmap tutorial
« Reply #342 on: February 03, 2013, 04:29:27 pm »
Here is yet a new version of BGRABitmap 6.6 :
http://sourceforge.net/projects/lazpaint/files/src/

New font renderer TBGRATextEffectFontRenderer in BGRATextFX, which takes the best out of LCL and vectorized renderer to provide font with :
- any size of text
- text rect and text angle
- outline of any width (filled a single color or a texture)
- text filled with a color or a texture
- drop shadow (any offset, blur size, any color)
- phong shading (does not work with angle)

Here is a sample code :
Code: [Select]
uses BGRABitmap, BGRABitmapTypes, BGRATextFX;

{ TForm1 }

procedure TForm1.FormPaint(Sender: TObject);
var bmp: TBGRABitmap;
  renderer: TBGRATextEffectFontRenderer;
begin
  bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,BGRAWhite);
  renderer := TBGRATextEffectFontRenderer.Create;
  bmp.FontRenderer := renderer;
  renderer.ShadowVisible := True;
  renderer.OutlineVisible := True;
  renderer.OutlineColor := CSSRed;
  renderer.OuterOutlineOnly := True;
  bmp.FontQuality:= fqFineAntialiasing;
  bmp.TextOut(5,5,'Hello world',BGRABlack);
  renderer.OutlineVisible := false;
  bmp.TextOutAngle(5,25,-200, 'Hello world',CSSDarkGreen, taLeftJustify);
  bmp.Draw(Canvas,0,0);
  bmp.Free;
end;   
Conscience is the debugger of the mind

circular

  • Hero Member
  • *****
  • Posts: 2912
    • Personal webpage
Re: BGRABitmap tutorial
« Reply #343 on: February 03, 2013, 04:36:55 pm »
Updated test program for font rendering:
http://lazarus.johann-elsass.net/
Conscience is the debugger of the mind

circular

  • Hero Member
  • *****
  • Posts: 2912
    • Personal webpage
Re: BGRABitmap tutorial
« Reply #344 on: February 07, 2013, 10:24:43 am »
Here is a tutorial on how to improve TAChart rendering with BGRABitmap :
http://wiki.freepascal.org/BGRABitmap_tutorial_TAChart
Conscience is the debugger of the mind