Recent

Author Topic: Text anti aliasing  (Read 5303 times)

circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: Text anti aliasing
« Reply #15 on: September 08, 2020, 05:23:52 pm »
If you have the actual polygon definition, you can draw it in white in a TBGRABitmap, and then draw that mask with FillClearTypeMask function of the target TBGRABitmap.

If you only have the the aliased image, you can create an image containing a mask of the non-transparent pixels, and then you can determine the contour using BGRAVectorize. Then you can do an antialiased fill poly of that. Though that would not be ClearType because ClearType antialias is not implemented yet for polygons. If you come up with some code to do that, I would be happy to know.
Conscience is the debugger of the mind

ChrisR

  • Full Member
  • ***
  • Posts: 247
Re: Text anti aliasing
« Reply #16 on: September 08, 2020, 09:35:51 pm »
One solution to this problem would be to use OpenGL. The project  font_gl.lpi https://github.com/neurolabusc/Metal-Demos/tree/master/font uses multi-channel signed distance field fonts. This allows nicely scalable, sharp looking text. This does require a custom shader. For bitmaps and lines, the multi-sampling built into OpenGL will anti-alias edges nicely.

To see the benefit of multisampling on lines, try the cube_gl.lpi project https://github.com/neurolabusc/Metal-Demos/tree/master/cube and compare the difference between the default
  ViewGPU1.MultiSampling:=4;
 versus
  ViewGPU1.MultiSampling:=1;

user5

  • Sr. Member
  • ****
  • Posts: 357
Re: Text anti aliasing
« Reply #17 on: September 10, 2020, 11:55:34 pm »
    I'm working now on some code to do TrueType or semi-TrueType edge softening and at some point I may have to rotate the image. I plan to use BGRABitmap for the rotation but for purposes of illustration the code below uses TImage canvas pixels and it rotates the image -90 degrees. I've studied topics on rotation is this forum and on the Web but I haven't been able to get anything else to work correctly. I wouldn't be surprised if I'm doing something wrong. Anyway, is there a way to rotate the image 90 degrees clockwise back to its original position without having to go counter clockwise around the dial until the image is back to its first position?

   
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button23Click(Sender: TObject);
  2. var x,y:integer;
  3. begin
  4.  for y := 0 to (image13.height - 1) do
  5.   begin
  6.    for x := 0 to (image13.width - 1) do
  7.     begin
  8.  
  9.      image16.canvas.pixels[y,x] := image13.canvas.pixels[x,y];
  10.  
  11.     end;
  12.   end;
  13.  
  14. end;

user5

  • Sr. Member
  • ****
  • Posts: 357
Re: Text anti aliasing
« Reply #18 on: September 11, 2020, 12:10:46 am »
    I'm working now on some code to do TrueType or semi-TrueType edge softening. At some point I may have to rotate the image. I plan to use BGRABitmap but for purposes of illustration the code below uses TImage canvas pixels and it rotates the image -90 degrees. I've studied topics on rotation is this forum and on the Web but I haven't been able to get anything else to work correctly. I wouldn't be surprised if I'm doing something wrong. Anyway, is there a way to rotate the image 90 degrees clockwise back to its original position without having to go counter clockwise around the dial until the image is back to its first position?

 
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button23Click(Sender: TObject);
  2. var x,y:integer;
  3. begin
  4.  for y := 0 to (image13.height - 1) do
  5.   begin
  6.    for x := 0 to (image13.width - 1) do
  7.     begin
  8.  
  9.      image16.canvas.pixels[y,x] := image13.canvas.pixels[x,y];
  10.  
  11.     end;
  12.   end;
  13.  
  14. end;


winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Text anti aliasing
« Reply #19 on: September 11, 2020, 12:50:14 am »
Hi!

I show you a simple procedure to rotate a TBitmap clockwise and counterclockwise.
For demonstration purpose all with TBitmap - awful slow.

But it's easy to convert it to BGRAbitmap.

Take a Form, an Image >= 100x300, a Button.

And this 2 procedures:

Code: Pascal  [Select][+][-]
  1. function rotate90 (Source: TBitmap; clockwise: Boolean) : TBitmap;
  2. var dest : TBitmap;
  3.       x,y : Integer;
  4.       col : TColor;
  5. begin
  6. dest := TBitmap.create;
  7. dest.setSize (Source.height,source.width);
  8. for x := 0 to Source.width - 1 do
  9.   begin
  10.      for y := 0 to Source.Height - 1 do
  11.        begin
  12.        col := Source.Canvas.pixels[x,y];
  13.        if clockwise then dest.canvas.pixels[y, dest.Height-x-1] := col else
  14.             dest.Canvas.Pixels[dest.Width-y-1, x] := col;
  15.        end;//y
  16.      end; // x
  17.  result := dest;
  18. end; // proc
  19.  
  20.  
  21.  
  22.  
  23. procedure TForm1.TestClick(Sender: TObject);
  24.   var
  25.   bmp, rot, rot2 : TBitmap;
  26.  
  27. begin
  28. Image1.Canvas.FillRect(0,0,Image1.Width,Image1.Height);
  29. bmp := TBitmap.Create;
  30. bmp.setSize(50,80);
  31. bmp.Canvas.Pen.Color := clRed;
  32. bmp.Canvas.Line(0,0,bmp.width, bmp.Height);
  33. bmp.Canvas.brush.Color := clLime;
  34. bmp.Canvas.FillRect(0,0,10,10);
  35. image1.Canvas.Draw(0,0,bmp);
  36. rot := rotate90(bmp, true);
  37. Image1.Canvas.Draw(0,100,rot);
  38. rot2 := rotate90 (rot, false);
  39. Image1.Canvas.Draw(0,200,rot2);
  40. bmp.Free;
  41. rot.free;
  42. rot2.Free;
  43. end;
  44.  
  45.  

Happy rotating!

Winni

circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: Text anti aliasing
« Reply #20 on: September 11, 2020, 01:41:39 am »
@user5: in general, I suggest you avoid using Image.Canvas. The image component draw the bitmap it contains. So that is rather Image.Picture.Bitmap.Canvas or something like that. This image needs to be sized of course.

If you are just using the area to draw things, then TPaintBox makes more sense. There you have no bitmap, just the control's canvas.

Also note that you are not supposed to access the control's canvas outside of its OnPaint event. Even though can work on Windows and Linux, it never does on MacOS.
« Last Edit: September 11, 2020, 01:43:43 am by circular »
Conscience is the debugger of the mind

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Text anti aliasing
« Reply #21 on: September 11, 2020, 02:12:33 am »

Also note that you are not supposed to access the control's canvas outside of its OnPaint event. Even though can work on Windows and Linux, it never does on MacOS.

This true for all kind of visible canvas - but again:
This is NOT true for a TImage.

Winni

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Text anti aliasing
« Reply #22 on: September 11, 2020, 02:49:47 am »
In fact, you can paint on TImage.Canvas at OnPaint event conditionally.
See code of TCustomImage.Paint:
Code: Pascal  [Select][+][-]
  1. begin
  2.   // detect loop
  3.   if FUseAncestorCanvas then exit;
  4.  
  5.   if csDesigning in ComponentState
  6.   then DrawFrame;
  7.  
  8.   if Picture.Graphic = nil
  9.   then Exit;
  10.  
  11.   C := inherited Canvas;
  12.   R := DestRect;
  13.   C.AntialiasingMode := FAntialiasingMode;
  14.   FPainting:=true;
  15.   try
  16.     if Assigned(FOnPaintBackground) then
  17.       FOnPaintBackground(Self, C, R);
  18.     C.StretchDraw(R, Picture.Graphic);
  19.   finally
  20.     FPainting:=false;
  21.   end;
  22.  
  23.   FUseAncestorCanvas := True;
  24.   try
  25.     inherited Paint;
  26.   finally
  27.     FUseAncestorCanvas := False;
  28.   end;
  29. end;

If Picture.Graphic is nil then there's exit, i.e. OnPaintBackground and OnPaint are not triggered at all.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Text anti aliasing
« Reply #23 on: September 11, 2020, 12:50:22 pm »
Hi!

To clarify the situation with the TImage :

There are two different Canvas in a TImage:

The Canvas which is shown to you: Image.Canvas

And the Canvas of the persistent data in the picture, which maybe a Bitmap, a PNG, a JPG .

The Picture data like Image.Picture.PNG.Canvas are not visible to you.
If the the Canvases both are unlocked the data is exchanged between them.

The code shown by @Blaazen is the painting on the Background, the invisible data in the picture.

You can draw at any time on an Image without violating any rules.

Winni


circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: Text anti aliasing
« Reply #24 on: September 11, 2020, 08:03:51 pm »
Also note that you are not supposed to access the control's canvas outside of its OnPaint event. Even though can work on Windows and Linux, it never does on MacOS.
This true for all kind of visible canvas - but again:
This is NOT true for a TImage.
That's why I am specifying "control's canvas". Otherwise you can access the canvas of a bitmap anytime indeed. In the case of a TImage, that would be Image1.Picture.Bitmap.Canvas.
Conscience is the debugger of the mind

user5

  • Sr. Member
  • ****
  • Posts: 357
Re: Text anti aliasing
« Reply #25 on: September 24, 2020, 06:19:29 pm »
    Hey everyone. Some of you recently helped me on my quest to achieve TrueType/ClearType/super Wu type of edge softening/anti aliasing. TrueType is Apple and ClearType is Microsoft. I am happy to report that the results are very encouraging to say the least. I'm talking about applying this kind of anti aliasing to all foreground shapes, text and images in pictures and videos. The edging of this new process is based on what pictures or videos are under the edged foreground image when the background surrounding that foreground image is made transparent, even if the edged picture or video frame is itself moving.
    The processing time it takes to apply this type of edging to videos is pretty slow but the quality of the results appears at this point to exceed my expectations. In addition to this, I'm also working on another new faster edging feature that will do semi-TrueType edging. All of these methods are designed to work only on raster scan foreground images on transparencies since any existing edge softening screws up the results. Thus, the de-edging tools I made remove existing anti aliasing so that it can be re-applied later after other work is done to improve the image and make it be truly and universally transparent. The program does all this graphically without relying on any existing standard algorithms using BGRABitmap fast pixel access. The reason that the former process is slow to process videos is due to the many passes that are made though each video frame and it would be impossible w/o BGRA. An interesting thing is that these methods can be applied to both solid color backgrounds and non-changing or changing muli-colored backgrounds.
    Anyway, all this has been made possible by Lazarus and the Forum and I thank you very much. I am excited about this. I was unsure whether to post this note here or in the third party section. I hope that posting it here is okay. I had hoped that some of the folks who helped me here in the graphics section would see this note since they took so much effort, patience and time to help me. They sure know their stuff. Much work remains but the hard part is done.

 

TinyPortal © 2005-2018