View Single Post
Old 16th March 2008, 06:40   #12  |  Link
mikeytown2
Resize Abuser
 
mikeytown2's Avatar
 
Join Date: Apr 2005
Location: Seattle, WA
Posts: 623
Quote:
Originally Posted by Gavino View Post
Have you considered using the width and height parameters of zoom?

With judicious use of these parameters, it ought to be possible to speed up your function a lot.

At the moment you are zooming the entire image (albeit a reduced one in fast mode), but often most of the pixels will get thrown away in the final result.

It might involve changing the logic of your script a fair bit, but it's worth looking into, if you haven't already.

(Not sure if your experiments with FreeFrame, etc, make my suggestion redundant, as I haven't caught up with that part of the discussion yet.)
I've taken a stab at using the other parameters of zoom(), and I've given up on it. I took your suggestion though and improved my other function ZoomBox() .


Here is a very BAD example of what the ZoomBox can do...
Code:
Global w=0 
Global x=0 
Global y=640
Global z=640
Global Resizer = "Spline16Resize"
Global zoomstep = -8

a = ImageSource("testP2.png", end=1, fps=30).ConvertToRGB32().ResizeKAR(640,640, Resizer).Trim(0,-1)

VeryBadIdea(a)
Global zoomstep = 8
last + VeryBadIdea(a)
Global zoomstep = 8
last + VeryBadIdea(a)

Function BadIdea(clip c)
{
	Global w = w+zoomstep
	Global x = x+zoomstep
	GLobal y = y-zoomstep
	Global z = z-zoomstep
	return c
}


Function VeryBadIdea(clip a)
{
	a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
	last + a.ZoomBox(w, x, y, z, Resizer=Resizer).ZoomBox(0, 80, 640, 560, 640, 480, Resizer=Resizer).Subtitle("  w: " + String(w) + "  x: " + String(x) + "  y: " + String(y) + "  z: " + String(z)).BadIdea()
}


# ZoomBox() - March 15th, 2008
#  Put a "box" around a clip. Box can be used to zoom in or out.
# 
# Cordinates for ZoomBox:
#  int x1: upper left x cord
#  int y1: upper left y cord
#  int x2: lower right x cord
#  int y2: lower right y cord
# Optional Parameters
#  int w: output width
#  int h: output height
#  string Resizer: name of resize function. Default = BilinearResize
# 
# Notes
#  Aspect ratio for output and box must be the same.
#  Be aware of colorspace resctictions.

Function ZoomBox(clip c, int x1, int y1, int x2, int y2, int "w", int "h", string "Resizer")
{
	#set defaults
	w = Default(w, c.width())
	h = Default(h, c.height())
	Resizer = Default(Resizer, "BilinearResize")
	
	#Check For Any Unreasonable Inputs
	Assert(x1<x2, "ZoomBox: Start x1[" + String(x1) + "] point larger then x2 Point[" + String(x2) + "]")
	Assert(y1<y2, "ZoomBox: Start y1[" + String(y1) + "] point larger then y2 Point[" + String(y2) + "]")
	Assert(Float(x2-x1)/Float(y2-y1) == Float(w)/Float(h), "ZoomBox: Box Aspect Ratio [" + String(Float(x2-x1)/Float(y2-y1)) + "] does not equal clip output Aspect Ratio [" + string(Float(w)/Float(h)) + "]")
	
	#inverse zoomfactor
	zx = float(w)/float(x2-x1)
	zy = float(h)/float(y2-y1)
	
	#shrinkpic before for speed boost when zooming out
	wOut = zx<1 ? zx*c.width() : c.width()
	x1 = zx<1 ? zx*x1 : x1
	x2 = zx<1 ? zx*x2 : x2
	hOut = zy<1 ? zy*c.height() : c.height()
	y1 = zy<1 ? zy*y1 : y1
	y2 = zy<1 ? zy*y2 : y2
	c = Eval(Resizer + "(c," + String(Round(wOut)) + "," + String(Round(hOut)) + ")")
	
	#Add Borders If Needed
	left = x1 < 0 ? -x1 : 0
	top = y1 < 0 ? -y1 : 0
	right =  x2-c.width() > 0 ? x2-c.width() : 0
	bottom =  y2-c.height() > 0 ? y2-c.height() : 0
	d = AddBorders(c, round(left), round(top), round(right), round(bottom))
	
	#Crop If Needed
	left = x1 > 0 ? x1 : 0
	top = y1 > 0 ? y1 : 0	
	right = x2-c.width() < 0 ?  x2-c.width() : 0
	bottom = y2-c.height() < 0 ? y2-c.height() : 0
	c = Crop(d, round(left), round(top), round(right), round(bottom))
	
	#Resize for output
	Return Eval(Resizer + "(c," + String(w) + "," + String(h) + ")")
}

function ResizeKAR(clip c, int maxW, int maxH, string "ResizeMethod", int "BackgroundColor")
{
	BackgroundColor = Default(BackgroundColor, $000000)
	ResizeMethod = Default(ResizeMethod, "BilinearResize")
	ratioS = Float(width(c))/Float(height(c))
	ratioD = Float(maxW)/Float(maxH)
	newW = Round(maxH*ratioS/2)*2
	newH = Round(maxW/ratioS/2)*2
	
	
	#Dest Higher Then Source; Dest Wider Then Source; Same Ratio
	c = 
	\	(ratioS>ratioD) ? 
	\	Eval(ResizeMethod + "(c, " + String(maxW) + ", " + String(newH) + ")").
	\	AddBorders(0, Round((maxH-newH)/2), 0, Round((maxH-newH)/2), BackgroundColor) :
	\	(ratioS<ratioD) ? 
	\	Eval(ResizeMethod + "(c, " + String(newW) + ", " + String(maxH) + ")").
	\	AddBorders(Round((maxW-newW)/2), 0, Round((maxW-newW)/2), 0, BackgroundColor) :
	\	(ratioS==ratioD) ?
	\	Eval(ResizeMethod + "(c, " + String(maxW) + ", " + String(maxH) + ")" ) :
	\	nop()
	
	#fix 1px changes, works only with 4:4:4
	c = 
	\	IsInterleaved(c) && (maxW>width(c)) ? c.AddBorders(0, 0, 1, 0) : 
	\	IsInterleaved(c) && (maxH>height(c)) ? c.AddBorders(0, 0, 0, 1) : 
	\	c
	return c
}
This code requires no DLL's. It is a good way to see how a resizer effects the picture in terms of ringing.

Last edited by mikeytown2; 16th March 2008 at 06:46.
mikeytown2 is offline   Reply With Quote