Log in

View Full Version : EasyAR() resize with aspect ratio


chachu
30th December 2010, 18:27
Hi,

( Sorry in advance for my english, I have just my school notions and "Google Translate" :p )
( Sorry in advance for my beginner function, it's my first true function. I would learn Avisynth :) )

/*
Description :
-----------
EasyAR() is a simple tool who can help to get rid of your boring calculations and resize image/video sources to a defined aspect ratio with 1 of this 3 methods :
-1 : Crop() and Spline64Resize()
0 : only Spline64Resize()
1 : Spline64Resize() and AddBorders()

Examples :
-----------
ImageReader("src.png")
#Crop() and Spline64Resize() to a 4:3 (16/12 = 4/3 ˜ 1.333)
EasyAR(16,12,-1)

ImageReader("src.png")
#only Spline64Resize() to a 16:9 (16/9 = 4/3*4/3 ˜ 1.778)
EasyAR(16,9)

ImageReader("src.png")
#Spline64Resize() and AddBorders() to a 10:16 (10/16 = 5/8 = 0.625)
EasyAR(10,16,1)
*/

function EasyAR(clip src, int "x", int "y", int "method") {

x = default(x,0)
y = default(y,0)
method = default(method,0)

Assert(x == 16 || y == 16,"x=16 or y=16")
Assert(1 <= x <= 16,"x=1~16")
Assert(1 <= y <= 16,"y=1~16")
Assert(method == -1 || method == 1 || method == 0,"method=-1 or method=0 or method=1")

w = Float(src.Width)
h = Float(src.Height)

(method == -1)
\ ?
\ (w > h/y*x)
\ ?
\ src.Crop( Round((w-(h/y*x))/4)*2 , 0 , -Round((w-(h/y*x))/4)*2 , -0 )
\ :
\ src.Crop( 0 , Round((h-(w/x*y))/4)*2 , -0 , -Round((h-(w/x*y))/4)*2 )
\ :
\ (method == 0)
\ ?
\ (w > h/y*x)
\ ?
\ src.Spline64Resize( Floor(Floor(w/x)/2)*2*x , Floor(Floor(w/x)/2)*2*y )
\ :
\ src.Spline64Resize( Floor(Floor(h/y)/2)*2*x , Floor(Floor(h/y)/2)*2*y )
\ :
\ (method == 1)
\ ?
\ (w > h/y*x)
\ ?
\ src.Spline64Resize( (Floor(Floor(w/x)/2)*2)*x , Floor(h/2)*2 )
\ :
\ src.Spline64Resize( Floor(w/2)*2 , (Floor(Floor(h/y)/2)*2)*y )
\ :
\ src

w = Float(last.Width)
h = Float(last.Height)

(method == -1)
\ ?
\ last.Spline64Resize( Round((w/x)/2)*2*x , Round((w/x)/2)*2*y )
\ :
\ (method == 1)
\ ?
\ (w > h/y*x)
\ ?
\ last.AddBorders( 0 , Int((((w/x)*y)-h)/2) , 0 , Int((((w/x)*y)-h)/2) )
\ :
\ last.AddBorders( Int((((h/y)*x)-w)/2) , 0 , Int((((h/y)*x)-w)/2) , 0 )
\ :
\ last

}


all constructive messages are welcome

Gavino
30th December 2010, 21:35
chachu, welcome to the forum.

A mistake often made by beginners when doing AR calculations is forgetting that integer division truncates the result, eg 3/2 = 1, not 1.5. So at least one operand of the division needs to be type float to get an accurate result.

So when you write expressions like src.Height/y*x, you will get the wrong value if src.Height is not a multiple of y. You should use float(src.Height)/y*x, for example.

chachu
31st December 2010, 03:58
chachu, welcome to the forum.

Doom9 best forum ever :cool:

A mistake often made by beginners when doing AR calculations is forgetting that integer division truncates the result, eg 3/2 = 1, not 1.5. So at least one operand of the division needs to be type float to get an accurate result.

So when you write expressions like src.Height/y*x, you will get the wrong value if src.Height is not a multiple of y. You should use float(src.Height)/y*x, for example.

Thanks Gavino for the first help !
Are you 100% sure "src.Height/y*x" isn't a float ?
Of course I would that equation become a float !

I'm going to try it but no doubt to answer me if you come back
Thanks again.

dansrfe
31st December 2010, 06:03
I have made a similar function like this: http://forum.doom9.org/showthread.php?t=157872

The idea seems cool though :)

Gavino
31st December 2010, 10:43
Are you 100% sure "src.Height/y*x" isn't a float ?
Absolutely certain.
All the operands in that expression (src.height, y, x) are integers, so the result is also an integer. In particular, since src.Height and y are both integers, the division gives an integer result, not a float.

For the same reason, all the 'floor' operations in your function are redundant, since in every case the input argument is an integer.

jmac698
2nd January 2011, 09:49
I have done something similar as well. http://forum.doom9.org/showthread.php?p=1467062#post1467062
I use an interesting variation though, the output size defines the aspect ratio for you, so there's only two parameters.
I'm not quite sure how to describe what it does, but it does what I want :)