# by Thomas Opheys, thanks to the community to provide a base for this

# Resize(Clip, Width, Height, Sharpen, Interlaced)
#
# Resizes a clip.
# 
# Clip:       the clip to be resized; optional
# Width:      the new width
# Height:     the new height
# Sharpen:    optional sharpening of the result (0.0: no sharpen, 0.5 light, see Sharpen() function)
# Interlaced: true: input is interlaced; false: progressive material
#
function Resize (clip c, int w, int h, float sh, bool interlaced) {
  return interlaced ? \
    ((sh == 0.0) ? (SeparateFields(c).BicubicResize(w,h/2).Weave) : (SeparateFields(c).BicubicResize(w,h/2).Sharpen(sh).Weave)) : \
    ((sh == 0.0) ? c.BicubicResize(w,h) : c.BicubicResize(w,h).Sharpen(sh))
} 


# ReseizeInterlaced(Clip, Width, Height, Sharpen)
#
# Resizes a clip with interlaced material
#
# parameters: see Resize() above
#
function ResizeInterlaced (clip c, int w, int h, float sh) {
  return c.Resize(w, h, sh, true)
}


# ZoomInFrames(Clip, Startframe, Endframe, Left, Top, Width, Height, Sharpen, Interlaced)
#
# Manipulates Startframe to Endframe and zooms a part of the picture to fill the clip size.
#
# I use this function to "filter" out short announcement banners, etc. from recorded TV programs
# that can't be removed by a de-logo filter. If you don't lose too much space and use start and
# end frames where there are hard cuts in the material, results aren't noticeable.
#
# Clip:        clip to be processed. optional
# Startframe:  start frame in the clip
# Endframe:    end frame in the clip
# Left:        left coordinate of the box zoomed to full picture size
# Top:         top coordinate of the box zoomed
# Width:       width of the box zoomed. if width=0, automatically calculated using Height and the correct aspect ratio
# Height:      height of the box zoomed
# Sharpen:     optional sharpening of the result (0.0: no sharpen, 0.5 light, see Sharpen() function)
# Interlaced:  true: input is interlaced; false: progressive material
#
function ZoomInFrames (clip c, int start, int end, int left, int top, int w, int h, float sh, bool interlaced ) {
  width = (w == 0) ? (c.Width * h / c.Height) : w

  left_clip = (start == 0) ? BlankClip(c, 0) : ((start == 1) ? c.Trim(0,-1) : c.Trim(0,start-1))
  mid_clip = c.Trim(start, end).Crop(left, top, width, h).Resize(c.Width, c.Height, sh, interlaced)
  right_clip = (end == c.Framecount-1) ? BlankClip(c, 0) : c.Trim(end+1, 0)

  return left_clip + mid_clip + right_clip
}


# ZoomInFramesAnimate (Clip, Startframe, Endframe, L1,T1,W1,H1, L2,T2,W2,H2, Sharpen, Interlaced)
#
# Manipulates Startframe to Endframe and zooms a part of the picture to fill the clip size.
# The box coordinates L1...H1 will be used for Startframe, then these will be interpolated
# up to the coordinates L2..H2 for Endframe.
#
# I use this function to "filter" out short announcement banners, etc. from recorded TV programs
# that can't be removed by a de-logo filter. If scene changes around the disturbing scene to filter
# out are too far away, I use this function to smoothly zoom in 100 frames before, then use the
# ZoomInFrames() function above to filter the content, then zoom out 100 more frames.
#
# parameters: see ZoomInFrames() above
#
function ZoomInFramesAnimate (clip c, int start, int end, int left1, int top1, int w1, int h1, int left2, int top2, int w2, int h2, float sh, bool interlaced) {
  width1 = (w1 == 0) ? (c.Width * h1 / c.Height) : w1
  width2 = (w2 == 0) ? (c.Width * h2 / c.Height) : w2

  left_clip = (start == 0) ? BlankClip(c, 0) : ((start == 1) ? c.Trim(0,-1) : c.Trim(0,start-1))
  mid_clip = interlaced ? c.Trim(start, end).SeparateFields : c.Trim(start, end)
  mid_clip = interlaced ? (mid_clip.Animate(0, mid_clip.Framecount-1, "BicubicResize", c.Width, c.Height/2, 0.3, 0.3, left1, top1/2, width1, h1/2, c.Width, c.Height/2, 0.3, 0.3, left2, top2/2, width2, h2/2).Sharpen(sh).Weave) : \
    (mid_clip.Animate(0, mid_clip.Framecount-1, "BicubicResize", c.Width, c.Height, 0.3, 0.3, left1, top1, width1, h1, c.Width, c.Height, 0.3, 0.3, left2, top2, width2, h2).Sharpen(sh))
  right_clip = (end == c.Framecount-1) ? BlankClip(c, 0) : c.Trim(end+1, 0)

  return left_clip + mid_clip + right_clip
}


# DeLogoFrames (Clip, Startframe, Endframe, ImagePath)
#
# Uses the VirtualDub DeLogo plugin "delogo.vdf" which you have to have to load in your script with
# LoadVirtualDubPlugin ("<PATH>\delogo.vdf", "DeLogo", 0)
# Replace <PATH> with the complete file path to the VirtualDub plugin directory.
#
# Clip:        clip to be processed
# Startframe:  start frame in the clip to be processed
# Endframe:    end frame in the clip
# ImagePath:   path of a bitmap (.bmp) image that contains the logo removal information (pure red marked pixels)
#
function DeLogoFrames (clip c, int start, int end, string img) {
  left_clip = (start == 0) ? BlankClip(c, 0) : ((start == 1) ? c.Trim(0,-1) : c.Trim(0,start-1))
  mid_clip = c.Trim(start, end).DeLogo(1,"","","","",img,15,40,0,0)
  right_clip = (end == c.Framecount-1) ? BlankClip(c, 0) : c.Trim(end+1, 0)

  return left_clip + mid_clip + right_clip
}