# OverlayImage by Giacomo Bellini - www.arcade-extreme.com/users/jck
# Version: 20080122
#
# Overlays an image over the given video using the alpha channel as a mask.
#
# Parameters
# video: a video object
# logoPath: path to the image to be loaded
# position: 789
# 456
# 123
# HoriBorder: horizontal border (can be negative) (for positions 8, 5 and 2 is the distance from the center of the image)
# VertBorder: vertical border (can be negative) (for positions 4, 5 and 6 is the distance from the center of the image)
# ImageOpacity: Opacity of the image (0 to 1)
#
# Example: (see end of this file)
function OverlayImage(clip video, string ImgPath, int "ImgPos", int "HoriBorder", int "VertBorder", float "ImgOpacity")
{
#Sets default values for named arguments
ImgPos = Default(ImgPos, 3)
HoriBorder = Default(HoriBorder, 1)
VertBorder = Default(VertBorder, 1)
ImgOpacity = Default(ImgOpacity, 0.5)
#Load the image
logoVideo = imagesource(ImgPath, end = video.framecount(), fps = video.framerate(), pixel_type = "RGB32")
#TGA image needs to be vertically flipped
logoVideo = RightStr(ImgPath,4) == ".tga" ? FlipVertical(logoVideo) : logoVideo
#Calculates X and Y coordinates of the image
xPos = 0
yPos = 0
xPos = ImgPos == 7 ? HoriBorder : xPos
yPos = ImgPos == 7 ? VertBorder : yPos
xPos = ImgPos == 8 ? (video.width()/2) - (logoVideo.width()/2) + HoriBorder : xPos
yPos = ImgPos == 8 ? VertBorder : yPos
xPos = ImgPos == 9 ? video.width() - logoVideo.width() - HoriBorder : xPos
yPos = ImgPos == 9 ? VertBorder : yPos
xPos = ImgPos == 4 ? HoriBorder : xPos
yPos = ImgPos == 4 ? (video.height()/2) - (logoVideo.height()/2) - VertBorder : yPos
xPos = ImgPos == 5 ? (video.width()/2) - (logoVideo.width()/2) + HoriBorder : xPos
yPos = ImgPos == 5 ? (video.height()/2) - (logoVideo.height()/2) - VertBorder : yPos
xPos = ImgPos == 6 ? video.width() - logoVideo.width() - HoriBorder : xPos
yPos = ImgPos == 6 ? (video.height()/2) - (logoVideo.height()/2) - VertBorder : yPos
xPos = ImgPos == 1 ? HoriBorder : xPos
yPos = ImgPos == 1 ? video.height() - logoVideo.height() - VertBorder : yPos
xPos = ImgPos == 2 ? (video.width()/2) - (logoVideo.width()/2) + HoriBorder : xPos
yPos = ImgPos == 2 ? video.height() - logoVideo.height() - VertBorder : yPos
xPos = ImgPos == 3 ? video.width() - logoVideo.width() - HoriBorder : xPos
yPos = ImgPos == 3 ? video.height() - logoVideo.height() - VertBorder : yPos
#Composes and returns the video
return video.overlay(logoVideo, mask = logoVideo.showalpha("rgb"), x = xPos, y = yPos, opacity = ImgOpacity)
}
__END__
Example:
import("OverlayImage.avs")
blankclip(color = $ffff69)
OverlayImage("blue_square.PNG", imgopacity = 1, imgpos = 9) #top right
OverlayImage("blue_square.PNG", imgopacity = 1, imgpos = 8) #top center
OverlayImage("blue_square.PNG", imgopacity = 1, imgpos = 7) #top left
OverlayImage("blue_square.PNG", imgopacity = 1, imgpos = 6) #middle right
OverlayImage("blue_square.PNG", imgopacity = 1, imgpos = 5) #middle center
OverlayImage("blue_square.PNG", imgopacity = 1, imgpos = 4) #middle left
OverlayImage("blue_square.PNG", imgopacity = 1) #imgpos default value is 3 = bottom right
OverlayImage("blue_square.PNG", imgopacity = 1, imgpos = 2) #bottom center
OverlayImage("blue_square.PNG", imgopacity = 1, imgpos = 1) #bottom left