Solved

Antialiased TImage?

Posted on 2000-04-08
25
539 Views
Last Modified: 2010-04-04
Is there anyway to remove the jagged edges of an line drawn on the canvas of a TImage in realtime?
0
Comment
Question by:sageryd
  • 14
  • 10
25 Comments
 

Expert Comment

by:Portal
ID: 2697387
Have a look at this component which antialiases shapes and lines.

www.delphipages.com/edit/count.cfm?ID=292

Quoted from the author Dean Wilkinson:
"SmoothLn is derived from TPaintBox. It provides antialiasing line and shape methods to produce smooth, high quality graphics. antialiasing eliminates staircase patterns from lines drawn at angles other than vertical or horizontal. "
0
 
LVL 1

Author Comment

by:sageryd
ID: 2697473
Nice component, but I can't install it since I'm running D5. Need either the .pas file or a .dcu compiled in D5.

--johan
0
 
LVL 2

Expert Comment

by:brainware
ID: 2698272
sageryd if you need Source for an Anti-Aliased Line i could write one i guess..
for use on a TCanvas or?

so far i have never seen any Source for such in Delphi and i checked almost everywhere some weeks ago...

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 1

Author Comment

by:sageryd
ID: 2698341
Thanks brainware! Yes, I'm using the canvas of a TImage. Isn't it quite hard writing such stuff??

:0)

--johan
0
 
LVL 2

Expert Comment

by:brainware
ID: 2698606
well it aint exactly all easy hehe..
but if it is what you need well you need it... :)
but REALTIME you say... thats NOT 100% Possiple with a TImage or Canvas now days hehe.. well AA can be done fast yes but like most other Image Effects they need a fast access to the pixels.

Where do you need it and for how much drawing... aka you trying to do like 10 fps redraws on a Timage or just Regular Replaint whatever...
0
 
LVL 1

Author Comment

by:sageryd
ID: 2699088
Actually I am making kind of a game, similar to Pictionary. There are some clients connected to a server, one of the clients draw an image and the others get it broadcasted. So I want the just drawn line to be antialiased, like when the user has the mousebutton dow and moves the mouse on the canvas the MouseMove event calls Canvas.MoveTo(X, Y); That's where I'd like the AA. I can't tell ya the FPS though...the drawing client's picture is painted up smoothly, the other clients get the picture in small portions.
0
 
LVL 1

Author Comment

by:sageryd
ID: 2699111
I forgot to mention, I'm not only drawing lines, there are rectangles and circles and stuff to. But only the shapes that the TImage.Canvas have procedures/functions for.

--johan
0
 
LVL 2

Expert Comment

by:brainware
ID: 2705582
Well what you can do is this, Draw at a Larger SCALE and Resample it 'Down-Sample' this is a easy way to do Global AntiAliasing.

anyhow... check our product at www.wrapx.suite.dk / www.bw-soft.com 
is a DirectX 6 Wrapper (commercial), So far most power is on the 2D Side, next version comes with AALine.. but if you wanna use regular GDI.. well i can get you the AALine Rutine for that, But for Circle etc you gotta do alot more work..

Resampling is prolly the best way if it would work with the thing you are doing.

www.efg2.com <-- check this page for some AA or Resampling stuff..
www.melander.dk <-- a Unit for Resampling..

Resample from like 200x200 to 220x220 and down to 200x200 again..


-Michael
0
 
LVL 1

Author Comment

by:sageryd
ID: 2706463
Well I can't really downsample it, because the user draws in realtime and it must get antialiased directly. Like in photoshop.
0
 
LVL 1

Author Comment

by:sageryd
ID: 2706477
I think what I really need is that AALine routine, as I havn't figured out a way to draw circles like mspaint does I might just skip that part.
0
 
LVL 2

Expert Comment

by:brainware
ID: 2707432
Tell ya what.. ill do what i can to help ya, tho it aint many points for such rutines but as im already working on such with Christian, we can share some i guess...

Have you ever worked with DIB's ?
'Device-Independant-Bitmaps'
just becuse when you want to be workng with Realtime Graphics a TBitmap/Image aint the best solution when you wanna do good quality graphic rutines.

-Michael who is in need of Coffee.....
0
 
LVL 1

Author Comment

by:sageryd
ID: 2708060
Adjusted points from 100 to 200
0
 
LVL 1

Author Comment

by:sageryd
ID: 2708061
Thanx! That would be nice.

No, I havn't used DIBs, at least I think i havn't. What is it really? What is the difference between a normal canvas and a DIB canvas?

You are from denmark right?
0
 
LVL 2

Expert Comment

by:brainware
ID: 2709979
Well a great example of compare is hmm TBitmap's Pixels[] speed vs if you make a good DIB Class.. you can get a extremly nice speed boost, involves alot of LowLevel api stuff but also a big preformance difference.. its prolly the best preformance you can get without DirectX.

Denmark.. yeah why?

anyhow.. if ya have DirectX 6+ installed i can show you some AA Line in works.. the code me and christian will make a small Canvas version of.
0
 
LVL 1

Author Comment

by:sageryd
ID: 2710636
ok, DIB sounds good. And yes please, send any examples you want: sageryd@gmx.net

My program has to work without DirectX though.

The question about Denmark was just out of curiosity, I'm from Sweden.

--johan
0
 
LVL 2

Expert Comment

by:brainware
ID: 2790225
Urg.. Im back 5% :) if you still need it that is, sorry that i have been I-Net dead for a 14 days time... i kinda bumped into a fantasic girl the weekend i was away :) and since then yeah you can guess....

0
 
LVL 1

Author Comment

by:sageryd
ID: 2791186
Ok, I understand ;)
0
 
LVL 2

Accepted Solution

by:
brainware earned 200 total points
ID: 2918229
I have the code ready for you now. This was written for use with TFastDIB so it was not danm slow :)
http://www.jps.net/gfody/


 Procedure AALine(Const aDIB : TFastDIB; A,B : TPoint; C : TFColor);
 Procedure SetAlphaPixel(Const aDib : TFastDIB; X,Y,Alpha : Integer; Color : TFColor);

implementation

Procedure AALine(Const aDIB : TFastDIB; A,B : TPoint; C : TFColor );
Const
  FP16_P5 = $8000;
  FP16_1 = $10000;
  FP16_1P5 = $18000;
Var
  DX,DY : Integer;
  S : Integer;
  I : Integer;
  T : TPoint;
  Q,P : Integer;
Begin
If (Round(A.X) = Round(B.X)) And (Round(A.Y) = Round(B.Y)) Then Exit;
DX := Abs(Round((B.X-A.X)*FP16_1));
DY := Abs(Round((B.Y-A.Y)*FP16_1));
With aDIB Do
If DX > DY Then
Begin
  If A.X > B.X Then
  Begin
    T := A;
    A := B;
    B := T;
  End;
  P := Round(A.X);
  S := Round(((B.Y-A.Y)/(B.X-A.X))*FP16_1);
  Q := Round(A.Y*FP16_1+S*Frac(A.X));
  For I := 0 To Round(DX Shr 16)-1 Do
  Begin
    SetAlphaPixel(aDIB,P+I,(Q-FP16_P5) Shr 16,Hi((Q-FP16_P5) And $FFFF) Xor $FF, C);
    SetAlphaPixel(aDIB,P+I,(Q+FP16_P5) Shr 16,Hi((Q-FP16_P5) And $FFFF), C);
    Q := Q+S;
  End;
End Else
Begin
  If A.Y > B.Y Then
  Begin
    T := A;
    A := B;
    B := T;
  End;
  P := Round(A.Y);
  S := Round(((B.X-A.X)/(B.Y-A.Y))*FP16_1);
  Q := Round(A.X*FP16_1+S*Frac(A.Y));
  For I := 0 To Round(DY Shr 16)-1 Do
  Begin
    SetAlphaPixel(aDIB,(Q-FP16_P5) Shr 16,P+I,Hi((Q-FP16_P5) And $FFFF) Xor $FF, C);
    SetAlphaPixel(aDIB,(Q+FP16_P5) Shr 16,P+I,Hi((Q-FP16_P5) And $FFFF), C);
    Q := Q+S;
  End;
End;
End;

Procedure SetAlphaPixel(Const aDib : TFastDIB; X,Y,Alpha : Integer; Color : TFColor);
 Var
   R,G,B : Byte;
   D2 : TFColor;
 Begin
 If (X > -1) And (X < Integer(aDib.Width)) And (Y > -1) And (Y < Integer(aDib.Height)) Then

  Case aDib.Bpp Of
   24 : Begin
          R := Color.R;
          G := Color.G;
          B := Color.B;

          D2 := aDIB.Pixels24[aDib.Height-Y,X];
          D2.R := (R*Alpha Shr 8)+D2.R*(255-Alpha) Shr 8;
          D2.G := (G*Alpha Shr 8)+D2.G*(255-Alpha) Shr 8;
          D2.B := (B*Alpha Shr 8)+D2.B*(255-Alpha) Shr 8;
          aDIB.Pixels24[aDib.Height-y,x] := D2;
        End;
  End;
 End;


Example
 AALine(myDIB,Point(0,0),Point(117,121),FRGB(255,255,255));


0
 
LVL 2

Expert Comment

by:brainware
ID: 2918240
Note - Original Source is from WrapX - Christian Iversen / BrainWare

www.wrapx.suite.dk - where it is also avalible using DirectX.

0
 
LVL 1

Author Comment

by:sageryd
ID: 2918475
Thanks! I'll try it A.S.A.P.!

johan
0
 
LVL 2

Expert Comment

by:brainware
ID: 2943146
So is it what ya need?

PS: i talked to author of FastDIB and he plans to implement the wu-AALine code above.
0
 
LVL 1

Author Comment

by:sageryd
ID: 2945116
Yes, I guess it's what I need, but as I havn't tried it yet, I don't really know, but it seems promising ;)

Nice! Did the FastDIB guy tell you when it will be ready?

--johan
0
 
LVL 2

Expert Comment

by:brainware
ID: 2980336
I think he will have it implemented shortly if not already done...
0
 
LVL 1

Author Comment

by:sageryd
ID: 2981832
Comment accepted as answer
0
 
LVL 1

Author Comment

by:sageryd
ID: 2981833
okay......well thanks! ;o)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question