View Single Post
Old 25th May 2008, 04:04   #49  |  Link
mikeytown2
Resize Abuser
 
mikeytown2's Avatar
 
Join Date: Apr 2005
Location: Seattle, WA
Posts: 623
Starting to play with the Align option
Here is my test image

I'm working on negative Align values first because it is the easiest to do. Got rid of inputs x1,x2,y1,y2 just for this.

Example Test Code
Code:
E = ImageReader("traingr4.jpg")

E.ZoomBoxer(640,480, "BilinearResize", 16.0/9.0, -3)
StackHorizontal(last,E.ZoomBoxer(640,320, "BilinearResize", 16.0/9.0, -7).AddBorders(0,160,0,0,$FF0000))

Function ZoomBoxer(clip c, int "width", int "height", string "ResizeMethod", float "DisplayAR", int "Align", int "color", int "IgnoreAR")
{
	#set defaults
	ResizeMethod = Default(ResizeMethod, "BilinearResize")
	IgnoreAR = Default(IgnoreAR, 1)
	color = Default(color, $000000)
	width = Default(width, c.width())
	height = Default(height, c.height())
	Align = Default(Align, 0)
	
	SourceAR = float(c.width())/float(c.height())
	FinalAR = float(width)/float(height)
	DisplayAR = Default(DisplayAR, Float(c.width())/Float(c.height()))
	DisplayAR = DisplayAR == 0 ? Float(c.width())/Float(c.height()) : DisplayAR
	#BoxAR = ((c.width() + ((width/DisplayAR-height)/2.0)*(c.width()/Float(height)))-(((width/DisplayAR-height)/2.0)*(c.width()/Float(height))))/c.height()
	
	#If Align=5 or -5 then center clip. -5: Add borders. 5: Crop.
	#Display Aspect Ratio = Final Output Ratio. No Change, Show All Pixels
	#Display Aspect Ratio > Final Output Ratio. Add Height or Crop Width 
	#Display Aspect Ratio < Final Output Ratio. Add Width or Crop Height 
	EvalString = 
	\   Align<>0 && DisplayAR==FinalAR ?
	\ "x1=0" + "
	   x2="  + String(c.width()) + "
	   y1=0" + "
	   y2="  + String(c.height()) + "" 
	   
	   
	\ : (Align==-5 || Align==-4 || Align==-6) && DisplayAR>FinalAR ?
	\ "y1=" + String(0 - ((height*DisplayAR-width)/2.0)*(c.height()/Float(width))) + "
	   y2=" + String(c.height() + ((height*DisplayAR-width)/2.0)*(c.height()/Float(width))) + "
	   x1=0" + "
	   x2=" + String(c.width())
	\ : Align==5 && DisplayAR>FinalAR ?
	\ "x1=" + String(0 + ((height-width/DisplayAR)/2.0)*(c.width()/Float(height))) + "
	   x2=" + String(c.width() - ((height-width/DisplayAR)/2.0)*(c.width()/Float(height))) + "
	   y1=0" + "
	   y2=" + String(c.height()) + "" 
	\ : (Align==-5 || Align==-2 || Align==-8)  && DisplayAR<FinalAR ?
	\ "x1=" + String(0 - ((width/DisplayAR-height)/2.0)*(c.width()/Float(height))) + "
	   x2=" + String(c.width() + ((width/DisplayAR-height)/2.0)*(c.width()/Float(height))) + "
	   y1=0" + "
	   y2=" + String(c.height()) + ""
	\ : Align==5  && DisplayAR<FinalAR  ?
	\ "y1=" + String(0 + ((width-height*DisplayAR)/2.0)*(c.height()/Float(width))) + "
	   y2=" + String(c.height() - ((width-height*DisplayAR)/2.0)*(c.height()/Float(width))) + "
	   x1=0" + "
	   x2=" + String(c.width()) 


	\ : (Align==-1 || Align==-3) && DisplayAR>FinalAR ?
	\ "y1=0" + "
	   y2=" + String(c.height() + ((height*DisplayAR-width)/1.0)*(c.height()/Float(width))) + "
	   x1=0" + "
	   x2=" + String(c.width())
	\ : (Align==-1 || Align==-7)  && DisplayAR<FinalAR ?
	\ "x1=0" + "
	   x2=" + String(c.width() + ((width/DisplayAR-height)/1.0)*(c.width()/Float(height))) + "
	   y1=0" + "
	   y2=" + String(c.height()) + ""


	\ : ""
	Eval(EvalString)
	
	#Take Crop Like Input
	x2 = x2<=x1 && x2 < 0 ? c.width() + x2 : x2
	y2 = y2<=y1 && y2 < 0 ? c.height() + y2 : y2
	
	#Check if x2 or y2 needs to be calculated
	Assert( ((x2==x1) && (y2==y1))==False, "x2 [" + String(x2) + "] or y2 [" + String(y2) + "] needs a value that is different from x1 [" + String(x1) + "] or y1 [" + String(y1) + "]")
	y2 = (y2==y1) ? (((x2-x1)/FinalAR)/(SourceAR/DisplayAR) + y1) : y2
	x2 = (x2==x1) ? (((y2-y1)*FinalAR)*(SourceAR/DisplayAR) + x1) : x2
	
	BoxAR = Float(x2-x1)/Float(y2-y1)
	#Check For Any Unreasonable Inputs
	Assert(x1<x2, "ZoomBox: x1[" + String(x1) + "] point larger then x2 Point[" + String(x2) + "]")
	Assert(y1<y2, "ZoomBox: y1[" + String(y1) + "] point larger then y2 Point[" + String(y2) + "]")
	Assert(IgnoreAR>0 || BoxAR == FinalAR, "ZoomBox: Box Aspect Ratio [" + String(BoxAR) + "] does not equal clip output Aspect Ratio [" + string(FinalAR) + "]")
	
	#Pad clip so resizer interpolates from border when zooming out. 64 for spline64
	c = c.AddBorders(64,64,64,64,color)
	#Do it
	c = Eval(ResizeMethod + "(c, " + String(Round(width)) + ", " + String(Round(height)) + ", src_left=" + String(x1) + "+64, src_top=" + String(y1) + "+64, src_width=" + String(x2-x1) + ", src_height=" + String(y2-y1) + ")")	
	
	c
	Subtitle(String(x1) + ", " + String(y1) + "    " + String(x2) + ", " + String(y2) + "    BoxAR " + String(BoxAR) + "  " + String())
}

I've also got the "easy way" Example here. Using FlipHorizontal() & FlipVertical() Inside SplitScreener.
Code:
A = ColorBars().Crop(10, 20, -30, -400).ConvertToYV12()
B = ColorBars().invert().FlipHorizontal().Crop(10, 20, -300, -40).ConvertToYV12()
C = ColorBars().FlipVertical().Crop(10, 200, -30, -40).ConvertToYV12()
D = ColorBars().invert().FlipVertical().FlipHorizontal().Crop(100, 20, -30, -40).ConvertToYV12()

#SplitScreener(640, 480, 4, A, B, C, D)

function SplitScreener(int Width, int Height, int Border, clip A, clip B, clip C, clip D, string "ResizeMethod", bool "AudioMix")
{
	#Check Clips For Full Chroma - 4:4:4
	#Assert((IsYV12(A) || IsYUY2(A))==False, "Clip A is not at full chroma (4:4:4). Convert to RGB")
	#Assert((IsYV12(B) || IsYUY2(B))==False, "Clip B is not at full chroma (4:4:4). Convert to RGB")
	#Assert((IsYV12(C) || IsYUY2(C))==False, "Clip C is not at full chroma (4:4:4). Convert to RGB")
	#Assert((IsYV12(D) || IsYUY2(D))==False, "Clip D is not at full chroma (4:4:4). Convert to RGB")
	
	#Set Defaults
	ResizeMethod = Default(ResizeMethod, "BilinearResize")
	AudioMix = Default(AudioMix, true)
	
	#Set Borders
	BorderH = Border
	BorderW = Border
	
	#Set Width and Height of individual clips
	NewW = Round(float(Width)/2.0 - float(BorderW)*1.5)
	NewH = Round(float(Height)/2.0 - float(BorderH)*1.5)
	
	#Process Video
	A = A.FlipHorizontal().FlipVertical().ZoomBox(NewW, NewH, ResizeMethod, 0,  0,0, width(C),0).FlipHorizontal().FlipVertical()
	A = A.AddBorders(BorderW, BorderH, 0, 0)
	B = B.FlipVertical().ZoomBox(NewW, NewH, ResizeMethod, 0,  0,0, 0,height(B)).FlipVertical()
	B = B.AddBorders(BorderW, BorderH, 0, 0)
	C = C.FlipHorizontal().ZoomBox(NewW, NewH, ResizeMethod, 0,  0,0, width(C),0).FlipHorizontal()
	C = C.AddBorders(BorderW, BorderH, 0, 0)
	D = D.ZoomBox(NewW, NewH, ResizeMethod, 0,  0,0, 0,height(D))
	D = D.AddBorders(BorderW, BorderH, 0, 0)
	
	#Merge Clips
	X = StackHorizontal(A, B).AddBorders(0,0,BorderW,0)
	Y = StackHorizontal(C, D).AddBorders(0,0,BorderW,0)
	StackVertical(X, Y).AddBorders(0,0,0,BorderH).KillAudio()
	
	#Fix Audio
	LongestClip = Int(Max(Framecount(A), Framecount(B), Framecount(C), Framecount(D)))
	A = (Framecount(A) <> LongestClip) ? A ++ BlankClip(A, LongestClip-Framecount(A)) : A
	B = (Framecount(B) <> LongestClip) ? B ++ BlankClip(B, LongestClip-Framecount(B)) : B
	C = (Framecount(C) <> LongestClip) ? C ++ BlankClip(C, LongestClip-Framecount(C)) : C
	D = (Framecount(D) <> LongestClip) ? D ++ BlankClip(D, LongestClip-Framecount(D)) : D
	
	#Mix Audio
	Sound = (AudioMix) ? MergeChannels(MixAudio(ConvertToMono(A).Amplify(0.49), ConvertToMono(C).Amplify(0.49)), MixAudio(ConvertToMono(B).Amplify(0.49), ConvertToMono(D).Amplify(0.49))):
		\ MergeChannels(GetLeftChannel(A), GetRightChannel(A), GetLeftChannel(B), GetRightChannel(B), GetLeftChannel(C), GetRightChannel(C), GetLeftChannel(D), GetRightChannel(D))
	AudioDub(last, Sound)
}
I'll be working on other things, so if anyone wants to pick this up, feel free. otherwise it might be a week or 2.
mikeytown2 is offline   Reply With Quote