Recent

Author Topic: Bgrabitmap fast textrect  (Read 640 times)

Onur2x

  • New Member
  • *
  • Posts: 34
Bgrabitmap fast textrect
« on: January 15, 2023, 10:47:26 am »
Hello   
I need to write grid-like text on bgrabitmap, but it takes a long time. where am i doing wrong my code is below.
Code: Pascal  [Select][+][-]
  1.  
  2. Procedure Tform1.button1click(Sender: Tobject);
  3. var
  4. a:Tbgrabitmap;
  5. i,r,c,roww,colw,x,y:integer;
  6.  initialtime, elapsedtime: DWord;
  7. Begin
  8.   a:=Tbgrabitmap.Create;
  9.   a.SetSize(self.Width,self.Height);
  10.   a.Fill(colortobgra(clred));
  11.   initialtime := GetTickCount;
  12.   roww:=0;
  13.   x:=0;
  14.   y:=0;
  15.   for i:=0 to 250 do
  16.   begin
  17.     colw:=0;
  18.     roww:=roww+30;
  19.     for c:=0 to 5 do
  20.     begin
  21.      colw:=colw+80;
  22.       a.TextRect(rect(x,y,colw,roww),i.ToString,taCenter,tlCenter,colortobgra(clblack));
  23.       x:=colw;
  24.     end;
  25.     y:=roww;
  26.   End;
  27.    elapsedtime := GetTickCount - initialtime;
  28.    WriteLn(IntToStr(elapsedtime));
  29. End;      
  30.  

circular

  • Hero Member
  • *****
  • Posts: 4221
    • Personal webpage
Re: Bgrabitmap fast textrect
« Reply #1 on: January 15, 2023, 05:39:32 pm »
Hello Onur2x,

There are various possible problems:
- the rectangles are bigger than necessary
- the font rendering is in itself too slow
- the text outside the visible area is drawn needlessly

For the first problem, you can fix the size of the rectangles by writing the code in a simpler way.

For the second, you can use a lighter text rendering by setting the FontQuality property.

For the third, you can check if the area is actually visible.

Drawing text is generally a slow process though. If all of these are not enough, I suggest to store in a cache the rendered cells. At a next rendering phase, you can reuse the cache those that are still visible and free the unused cache.

Code: Pascal  [Select][+][-]
  1. procedure Tform1.button1click(Sender: Tobject);
  2. const
  3.   roww = 30;
  4.   colw = 80;
  5. var
  6.   a: TBGRABitmap;
  7.   r, c, x, y: integer;
  8.   cellRect: TRect;
  9. Begin
  10.   a:=TBGRABitmap.Create;
  11.   a.SetSize(self.ClientWidth, self.ClientHeight); // use client size
  12.   a.Fill(CSSRed); // use BGRA constants
  13.   a.FontQuality:= fqSystemClearType; // less precision but faster
  14.   y:=0;
  15.   for r:=0 to 250 do
  16.   begin
  17.     x:=0;
  18.     for c:=0 to 5 do
  19.     begin
  20.       cellRect := RectWithSize(x, y, colw, roww); // specify size
  21.       // check if cell actually visible
  22.       if cellRect.IntersectsWith(ClientRect) then
  23.         a.TextRect(cellRect, r.ToString, taCenter, tlCenter, BGRABlack);
  24.       x += colw;
  25.     end;
  26.     y += roww;
  27.   End;
  28.   ... // do something with the bitmap
  29.   a.free; // free memory
  30. End;
Conscience is the debugger of the mind

Onur2x

  • New Member
  • *
  • Posts: 34
Re: Bgrabitmap fast textrect
« Reply #2 on: January 15, 2023, 09:34:12 pm »
Thank you very much, the program is a little more relaxed, but unfortunately it is not quite fast. It takes longer when you draw with canvas or canvasbgra

circular

  • Hero Member
  • *****
  • Posts: 4221
    • Personal webpage
Re: Bgrabitmap fast textrect
« Reply #3 on: January 20, 2023, 08:15:08 pm »
Another approach, if you don't need some special characters, is to use BGRAFreeType. This will render font polygons directly, without going though a system call to render the text.
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018