View Single Post
Old 25th November 2011, 04:40   #3  |  Link
vampiredom
Registered User
 
Join Date: Aug 2008
Posts: 233
Converting square pixels to non-square pixels

To convert a square pixel image to a different PAR is easy. Divide the width by the PAR like such:

640x480 @ 10/11 PAR
640 / (10/11) = 704
704x480

The above example is rather simplistic. In many cases, when converting from square pixels, we'll want to scale the image overall. First we need to determine the scaling factor. To accomplish this, divide the source height by the target height:

Code:
SourceHeight / TargetHeight = ScalingFactor
1280x720 -> 704x480 @ 40/33 PAR
480 / 720 = 0.66666666666666666666666666666667

Once we have the scaling factor, apply that to both width and height of the source.
1280 * 0.66666666666666666666666666666667 = 853.33333333333333333333333333333
720 * 0.66666666666666666666666666666667 = 480

Now, divide the source width by the target PAR:
853.33333333333333333333333333333 / (40/33) = 704

From this, we can see that 1280x720 transforms perfectly to 704x480 for widescreen NTSC. The same is true for widescreen PAL:
576/720 = 0.8
1280 * 0.8 / (118/81) = 702.9
720 * 0.8 = 576

Well, almost perfectly in the case of PAL… but 702.9 is more than close enough to 704 to call it 704x576.

Sometimes, however. It is necessary or advantageous to crop or pad the source image when converting to from square pixels. Let's take the case of a 4928x3264 photograph that we want to format for 4:3 PAL DVD.

First, determine if the image - when scaled proportionately - will be wider or taller than the destination. The destination in this case is 720x576 @ 59/54 PAR. Lets' turn this to square pixels (we don't want to round it yet)

MaxWidth = 720 * (59/54) = 786.66666666666666666666666666667
MaxHeight = 576

From this, we'll get the effective Display Aspect Ratio of the target in square pixels:
TargetDAR = MaxWidth / MaxHeight = 1.3657407407407407407407407407407

Let's now compare this with the photograph:
SourceDAR = 4928/3264 = 1.5098039215686274509803921568627

From this, we can see that the DAR for the photo is larger, meaning it is wider than the Standard 720x576 4:3 PAL DVD.
We have the choice of cropping from the sides or letterboxing it (putting bars on the top and bottom of the image.) Let's try it both ways:

Cropping to fit another format

To determine the cropping, let's get the closest PAL-sized box within that image, in round pixels.
We'll use source image's height and the target format's DAR to determine that:
3264 * 1.3657407407407407407407407407407 = 4457.7777777777777777777777777776 = ~ 4458

Which gives us a cropped dimension of 4458x3264.

Is is best that this number is evenly divisible by 2. In this case, it is.
Were it not, we could have rounded up or down to the nearest 2. More than close enough for this kind of conversion.

Letterboxing to fit another format

Now, we'll do letterboxing instead of cropping. Width is easy here (Our target width will be 720) we only need to determine height.
First, we'll determine our scaling factor. In this case, this will be determined by the target and source widths.

Code:
TargetWidth / SourceWidth = ScalingFactor
ScalingFactor = 720 / 4928 = 0.1461038961038961038961038961039

Now, multiply the height by the scaling factor:
3264 * 0.1461038961038961038961038961039 = 476.88311688311688311688311688313

At this point, we are still dealing with square pixel values for the image. We need to apply the PAR to our new height:
476.88311688311688311688311688313 * (59/54) = 521.03896103896103896103896103897 = ~ 520

In AviSynth, we would do something like this to fit our photo into letterboxed PAL 4:3

Code:
# load the 4928x3264 image
ImageSource("bigphoto.jpg")

# resize and add borders to fill 720x576
Spline36Resize(720, 520)
AddBorders(0,28,0,28)

Last edited by vampiredom; 25th November 2011 at 06:58.
vampiredom is offline   Reply With Quote