Log in

View Full Version : PARResize() | PAR-aware resizing function


vampiredom
27th November 2011, 22:35
PARResize() is useful for standards conversions and arbitrary anamorphic pixel resizing.

It can automatically detect the "correct" input and output PAR using the assumptions here (http://forum.doom9.org/showthread.php?p=1540998#post1540998). Or you can specify SourcePAR or TargetPAR as a decimal or fraction string.

It operates in two modes: Boxing=true (default) will add borders to achieve the target dimensions. When Boxing=false, it will crop the source to match the PAR-compensated target dimensions.

100% AviSynth, no dependent DLLs required.
Download it here (http://www.3dvp.com/PARResize.zip)

#####################################################################################################
# PARResize()
# 2011/11/29
#####################################################################################################
#
# Parameters:
#
# TargetWidth, TargetHeight [integer]
# These determine the output width and height
# SourcePAR, TargetPAR [string or float]
# These determine the PAR of the input and output, respectively
# Legal vales are either a floating decimal PAR (ex: SourcePAR=0.909091) or a string
# The allowed string values are:
# "" (default)
# A blank string will set the corresponding PAR automatically, assuming 4:3 DAR
# "ws"
# A value of "ws" (widescreen) will set the corresponding PAR automatically, assuming 16:9 DAR
# Fractional String
# You can also use fractions as strings (Example: SourcePAR="10/11", TargetPAR="40/33")
# Boxing [boolean]
# When Boxing=true (default), borders will be added to achieve the TargetWidth or Height as needed
# When false, PARResize will crop the source as needed to achieve the target DAR
# Color [int]
# The Color parameter determines the border color when Boxing=true
# CropBias [float]
# When CropBias=0.5 (default), any cropping of the source will be symmetrical
# Setting it lower (minimum is 0.0) will show more of the source's left or top, cropping more from the right or bottom
# Setting it higher (minimum is 1.0) will show more of the source's right or bottom, cropping more from the left or top
# CropBias has no effect if Boxing=true
#
#####################################################################################################

Examples:
# Assume 4:3 input and resize to 4:3 NTSC DVD
PARResize(720, 480, SourcePAR="", TargetPAR="")

# Assume widescreen input and output
PARResize(720, 480, SourcePAR="ws", TargetPAR="ws")

# Use arbitrary PARs
PARResize(176, 576, SourcePAR=1.0, TargetPAR="472/81")

# Assume an HD source, convert to widescreen DVD specs
# Here, Boxing=false will crop a few lines from the source before scaling to fill the width
PARResize(720, 576, "ws", "ws", Boxing=false)

# Assume an HD source, convert to 4:3 DVD specs
# Here, Boxing=false will crop the sides to fit the screen
PARResize(720, 576, "ws" , "", false)

# Set the letterboxing color to white
PARResize(704, 480, color=$FFFFFF)


I have tested it with standard input/output. More testing is needed for odd-sized sources and output and arbitrary PARs. There is some sanity-checking... you can also set debug=true to see what is going on behind the scenes with the numbers.

Gavino
29th November 2011, 01:14
I haven't looked in detail at the algorithmic part, so can't comment on that, but I spotted a couple of things on a quick look at the code.

In the Assert statement error messages, CHR(13) does not produce a newline, just a square blob. Use CHR(10) instead, or just use a multi-line string literal, eg
"first line
second line" # note no '\' required

Also, Avisynth string comparison is case-insensitive, so there's no need to use LCase when comparing.

vampiredom
29th November 2011, 02:01
In the Assert statement error messages, CHR(13) does not produce a newline, just a square blob. Use CHR(10) instead, or just use a multi-line string literal, eg

Thanks. That's convenient. However, on my system [Win7/x64 when opening in VirtualDub], the CHR(13) does create a new line on Assert(), strangely enough. I'll look into it and revise using your recommendation.

Also, Avisynth string comparison is case-insensitive, so there's no need to use LCase when comparing.
Another handy thing to know! Thanks again.

vampiredom
29th November 2011, 05:37
Updated: Some code cleanup per Gavino's suggestions, more sanity-checking, more flexibility for odd cropping of RGB sources. (download link is in the top post)

Gavino
29th November 2011, 11:51
However, on my system [Win7/x64 when opening in VirtualDub], the CHR(13) does create a new line on Assert(), strangely enough.
Hmm, you're right - it seems it's just AvsP that displays it wrongly (haven't tried AvsPmod, but it's probably the same). Curious...