Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

Domains: forum.doom9.org / forum.doom9.net / forum.doom9.se

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Usage

Reply
 
Thread Tools Search this Thread Display Modes
Old 28th November 2010, 16:45   #1  |  Link
tin3tin
Registered User
 
tin3tin's Avatar
 
Join Date: Mar 2005
Posts: 366
Drawing lines with Gscript

Here's a function for drawing lines with Gscript:

Code:
Loadplugin("GScript.dll")
blankclip(1,500,500,"RGB32", color=$FF0000FF)

# Example a line:
#DrawLine(canvas, 1,1,500,500, $FFFFFFFF)

#Example a red box:
#DrawLine(last,  100,100,400,100,5, $FFFF0000)
#DrawLine(last,  100,100,100,400,5, $FFFF0000) 
#DrawLine(last,  100,400,400,400,5, $FFFF0000) 
#DrawLine(last,  400,100,400,400,5, $FFFF0000) 

# Example a lot of lines:
GScript("""
for (i=0, 500,10){
DrawLine(last,  0,i,500,500-i,1, $FFFFFFFF)
DrawLine(last,  i,0,500-i,500,1, $FFFFFFFF)
}
    
""")

function DrawLine(clip canvas, val x0, val y0,val x1,val y1, val dsize, val dcolor)
{
    dtotal=canvas.framecount()
    OptCanvas=blankclip(1,canvas.width(),canvas.height(),"RGB32", color=$00000000)
    pen=blankclip(1,dsize,dsize,"RGB32", color=dcolor)
 
              dx = float(x1 - x0)
              dy = float(y1 - y0)
 
              sx = dx > 0 ? 1 : -1
              sy = dy > 0 ? 1 : -1
             
              dx = float(dx*sx)
              dy = float(dy*sy)         

              dxdy= float(dx/dy)
              dydx = float(dy/dx)          

              dxdy = dxdy *sx
              dydx = dydx *sy          

              py = float(y0)
              px = float(x0)
                
    GScript("""
              if( dx > dy )
              {
                
                 for(x=0, int(dx))
                 {
                    OptCanvas=Layer(OptCanvas,pen,"add",257,int(px),int(py))
                    px=px+sx
                    py=py+dydx
                 }
              }
              else
              {
                 for(y=0, int(dy))
                 {
                    OptCanvas=Layer(OptCanvas,pen,"add",257,int(px),int(py))
                    py=py+sy
                    px=px+dxdy
                 }
              }            
  
    """)
return Layer(Canvas,OptCanvas.loop(dtotal),"add",257,0,0)
}
__________________
DVD slideshow GUI(Freeware).

Last edited by tin3tin; 28th November 2010 at 22:10.
tin3tin is offline   Reply With Quote
Old 28th November 2010, 19:13   #2  |  Link
tin3tin
Registered User
 
tin3tin's Avatar
 
Join Date: Mar 2005
Posts: 366
Tried to make a DrawCircle too, from this example: http://www.cs.unc.edu/~mcmillan/comp...e7/circle.html

However there is an error...

[Edit: Thanks Gavino! here's the working script:]

Code:
Loadplugin("GScript.dll")
blankclip(1,500,500,"RGB32", color=$FF0000FF)
            
DrawCircle(last, 250,250,100, 2, $FFFFFFFF)

function DrawCircle(clip canvas, val CenterX, val CenterY,val radius, val pensize, val pencolor)
{
    dtotal=canvas.framecount()
    OptCanvas=blankclip(1,canvas.width(),canvas.height(),"RGB32", color=$00000000)
    pen=blankclip(1,pensize,pensize,"RGB32", color=pencolor)            
    GScript("""   
        x = 0
        y = radius
        p = (5 - radius*4)/4

        while (x < y) 
        {
            x=x+1
            if (p < 0) {
                p = p+2*x+1
            } else {
                y=y-1
                p =p+ 2*(x-y+1)
            }
            OptCanvas=circlePoints(CenterX, CenterY, x, y, OptCanvas, pen)
            OptCanvas=Layer(OptCanvas,pen,"add",257,CenterX+int(x),CenterY+int(y))
        }       
""")

return Layer(Canvas,OptCanvas.loop(dtotal),"add",257,0,0)
}

function circlePoints(int cx, int cy, int x, int y, clip optCanvas, clip pen)
{

GScript("""       
        if (x == 0) {
            OptCanvas=Layer(OptCanvas,pen,"add",257, cx, cy + y)
            OptCanvas=Layer(OptCanvas,pen,"add",257, cx, cy - y)
            OptCanvas=Layer(OptCanvas,pen,"add",257, cx + y, cy)
            OptCanvas=Layer(OptCanvas,pen,"add",257, cx - y, cy)
        } else if (x == y) {
            OptCanvas=Layer(OptCanvas,pen,"add",257,cx + x, cy + y)
            OptCanvas=Layer(OptCanvas,pen,"add",257, cx - x, cy + y)
            OptCanvas=Layer(OptCanvas,pen,"add",257, cx + x, cy - y)
            OptCanvas=Layer(OptCanvas,pen,"add",257, cx - x, cy - y)
        } else if (x < y) {
            OptCanvas=Layer(OptCanvas,pen,"add",257, cx + x, cy + y)
            OptCanvas=Layer(OptCanvas,pen,"add",257, cx - x, cy + y)
            OptCanvas=Layer(OptCanvas,pen,"add",257, cx + x, cy - y)
            OptCanvas=Layer(OptCanvas,pen,"add",257, cx - x, cy - y)
            OptCanvas=Layer(OptCanvas,pen,"add",257, cx + y, cy + x)
            OptCanvas=Layer(OptCanvas,pen,"add",257, cx - y, cy + x)
            OptCanvas=Layer(OptCanvas,pen,"add",257, cx + y, cy - x)
            OptCanvas=Layer(OptCanvas,pen,"add",257, cx - y, cy - x)
        }
""")
            return OptCanvas
}
__________________
DVD slideshow GUI(Freeware).

Last edited by tin3tin; 28th November 2010 at 22:08. Reason: Corrected the code
tin3tin is offline   Reply With Quote
Old 28th November 2010, 21:56   #3  |  Link
Gavino
Avisynth language lover
 
Join Date: Dec 2007
Location: Spain
Posts: 3,442
Code:
function DrawCircle( ... )
{
    ...
    optCanvas = circlePoints(CenterX, CenterY, x, y, OptCanvas, pen)
    ...
}

function circlePoints( ... )
{
        ...       
        if (x == 0) {
            ...
        } else if (x == y) {
            ...
}
__________________
GScript and GRunT - complex Avisynth scripting made easier
Gavino is offline   Reply With Quote
Old 28th November 2010, 22:09   #4  |  Link
tin3tin
Registered User
 
tin3tin's Avatar
 
Join Date: Mar 2005
Posts: 366
Thanks Gavino!

I was so close... and so blind...
__________________
DVD slideshow GUI(Freeware).
tin3tin is offline   Reply With Quote
Old 17th March 2013, 05:50   #5  |  Link
Jenyok
Warm and fuzzy
 
Join Date: Apr 2010
Location: Moscow, Russia
Posts: 206
tin3tin
.
Exuse me, mate.
.
How to draw ARC ?
How to draw ELLIPSE ?
.
Gavino
.
Can you help, mate ?
.
__________________
Warm and fuzzy (white and fluffy)

Last edited by Jenyok; 17th March 2013 at 06:03.
Jenyok is offline   Reply With Quote
Old 17th March 2013, 08:42   #6  |  Link
tin3tin
Registered User
 
tin3tin's Avatar
 
Join Date: Mar 2005
Posts: 366
What I did was to google up some code handling the math and then changed it to Avisynth script.
__________________
DVD slideshow GUI(Freeware).
tin3tin is offline   Reply With Quote
Old 17th March 2013, 13:20   #7  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 11,406
Maybe of interest:
http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
http://en.wikipedia.org/wiki/Ellipse
http://en.wikipedia.org/wiki/Bresenh...line_algorithm
http://en.wikipedia.org/wiki/Digital...s_algorithm%29
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???
StainlessS is offline   Reply With Quote
Old 17th March 2013, 14:58   #8  |  Link
Jenyok
Warm and fuzzy
 
Join Date: Apr 2010
Location: Moscow, Russia
Posts: 206
StainlessS
Thanks, mate.
.
.
I do not understand, why line of code does not works, see code below ?
.
Code:
LoadPlugin("C:\PROGRAM FILES\AVISYNTH 2.5\PLUGINS\GSCRIPT_11\gscript.dll")

function DrawCircle(clip canvas, val CenterX, val CenterY, val radius, val pensize, val pencolor)
{
  dtotal    = canvas.framecount()
  OptCanvas = blankclip(1, canvas.width(), canvas.height(), "RGB32", color=$00000000)
  pen       = blankclip(1, pensize, pensize, "RGB32", color=pencolor)            

  GScript("""
          x = 0
          y = radius
          p = (5 - radius * 4) / 4

          dt = 0

          while (x < y) {
            x = x + 1
            
            if (p < 0) {
              p = p + 2 * x + 1
            }
            else {
              y = y - 1
              p = p + 2 * (x - y + 1)
            }

            OptCanvas = circlePoints(CenterX, CenterY, x, y, OptCanvas, pen)
            OptCanvas = Layer(OptCanvas, pen, "add", 257, CenterX+int(x), CenterY+int(y))

            OptCanvas = SubTitle(OptCanvas, "p = "+string(p), x=5, y=5+dt, size=10, align=1, text_color=$FFFFFF, halo_color=$000000, font="Arial Cyr")   # Why does this line not works ?
            dt=dt+10
          }
  """)

  return (Layer(canvas, OptCanvas.loop(dtotal), "add", 257, 0, 0))
}


function circlePoints(int cx, int cy, int x, int y, clip optCanvas, clip pen)
{
  GScript("""       
          if (x == 0) {
            OptCanvas = Layer(OptCanvas, pen, "add", 257, cx, cy + y)
            OptCanvas = Layer(OptCanvas, pen, "add", 257, cx, cy - y)
            OptCanvas = Layer(OptCanvas, pen, "add", 257, cx + y, cy)
            OptCanvas = Layer(OptCanvas, pen, "add", 257, cx - y, cy)
          } 
          else if (x == y) {
            OptCanvas = Layer(OptCanvas, pen, "add", 257, cx + x, cy + y)
            OptCanvas = Layer(OptCanvas, pen, "add", 257, cx - x, cy + y)
            OptCanvas = Layer(OptCanvas, pen, "add", 257, cx + x, cy - y)
            OptCanvas = Layer(OptCanvas, pen, "add", 257, cx - x, cy - y)
          }
          else if (x < y) {
            OptCanvas = Layer(OptCanvas, pen, "add", 257, cx + x, cy + y)
            OptCanvas = Layer(OptCanvas, pen, "add", 257, cx - x, cy + y)
            OptCanvas = Layer(OptCanvas, pen, "add", 257, cx + x, cy - y)
            OptCanvas = Layer(OptCanvas, pen, "add", 257, cx - x, cy - y)
            OptCanvas = Layer(OptCanvas, pen, "add", 257, cx + y, cy + x)
            OptCanvas = Layer(OptCanvas, pen, "add", 257, cx - y, cy + x)
            OptCanvas = Layer(OptCanvas, pen, "add", 257, cx + y, cy - x)
            OptCanvas = Layer(OptCanvas, pen, "add", 257, cx - y, cy - x)
          }
  """)

  return (OptCanvas)
}



BlankClip(length=50, width=768, height=576, fps=25.0, channels=2, color=$000000)

x2 = Width() / 2
y2 = Height() / 2

DrawCircle(last, x2+200, y2, 100, 2, $FFFFFFFF)

ConvertToYUY2()
__________________
Warm and fuzzy (white and fluffy)

Last edited by Jenyok; 17th March 2013 at 15:09.
Jenyok is offline   Reply With Quote
Old 17th March 2013, 21:07   #9  |  Link
Gavino
Avisynth language lover
 
Join Date: Dec 2007
Location: Spain
Posts: 3,442
Quote:
Originally Posted by Jenyok View Post
I do not understand, why line of code does not works, see code below ?
It's because OptCanvas is created transparent (alpha=0), and Subtitle() does not change the alpha layer.
You need to add this line after the Subtitle call:
Code:
OptCanvas = OptCanvas.Mask(OptCanvas.ShowAlpha().SubTitle("p = "+string(p), x=5, y=5+dt, size=10, align=1, text_color=$FFFFFF, halo_color=$FFFFFF, font="Arial Cyr"))
This will set alpha to 255 (opaque) where Subtitle() has written the text.
__________________
GScript and GRunT - complex Avisynth scripting made easier
Gavino is offline   Reply With Quote
Old 18th March 2013, 06:34   #10  |  Link
Jenyok
Warm and fuzzy
 
Join Date: Apr 2010
Location: Moscow, Russia
Posts: 206
Gavino
.
Thanks, mate.
It works.
.
__________________
Warm and fuzzy (white and fluffy)
Jenyok is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:28.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.