Function DivXResize(clip c, int WIDTH, int HEIGHT, int OVERSCAN, string RESIZER, int widescreen) {

ratio=((c.width()+.0)/(c.height()+.0))+.0
ratio=(ratio<1.33333) ? 1.333333 : ratio
halfheight43=(round(c.width()/1.33333)-c.height())/2
halfheight43=(widescreen==1)&&(ratio>1.7777) ? (round(c.width()/1.77777)-c.height())/2 : halfheight43

#Crop to fullscreen it this has been asked
fullscreen_cropper=round ((c.width()-(c.height()*1.33333) ) /2)
f_c_mod=fullscreen_cropper % 2
fullscreen_cropper=( f_c_mod == 1) ? (fullscreen_cropper+1) : fullscreen_cropper

ratio=(widescreen == 2) ? 1.333333 : ratio
c= (widescreen == 2) ? c.crop(fullscreen_cropper,0,(c.width()-2*fullscreen_cropper),0): c

#What would be the height keeping the aspect ratio if width was 640/320-NTSC or 768/384-PAL(4:3)
no_borders_height1=(c.height() < HEIGHT)? c.height() : HEIGHT

#4:3 (option 0 and 2)
no_borders_height1=(HEIGHT == 480) ? ceil(640*(1/ratio)) : no_borders_height1
no_borders_height1=(HEIGHT == 240) ? ceil(320*(1/ratio)) : no_borders_height1

no_borders_height1=(HEIGHT == 576) ? ceil(768*(1/ratio)) : no_borders_height1
no_borders_height1=(HEIGHT == 288) ? ceil(384*(1/ratio)) : no_borders_height1

#anamorphic (option 1)
no_borders_height1=(HEIGHT == 480)&&(widescreen==1)&&(ratio>1.7777) ? ceil(848*(1/ratio)) : no_borders_height1
no_borders_height1=(HEIGHT == 240)&&(widescreen==1)&&(ratio>1.7777) ? ceil(424*(1/ratio)) : no_borders_height1

no_borders_height1=(HEIGHT == 576)&&(widescreen==1)&&(ratio>1.7777) ? ceil(1024*(1/ratio)) : no_borders_height1
no_borders_height1=(HEIGHT == 288)&&(widescreen==1)&&(ratio>1.7777) ? ceil(512*(1/ratio)) : no_borders_height1


#Now how much we have to shrink to make it fit inside a macroblock?
mod16height=no_borders_height1 % 16

#If the rest is <=8, subtracts the rest, else, completes to 16.
completion= (mod16height <= 8) ? (-mod16height) : (16-mod16height)
no_borders_height2=no_borders_height1+completion

#finally resize
#c=Eval( RESIZER + "(c," +  string(WIDTH) + "," + string(no_borders_height2) + ")" )

#add lateral overscan
c=letterbox(c,0,0,overscan*8,overscan*8)
return(c)
}

Function DivXBorders(clip c, int HEIGHT, int OVERSCAN) {
halfheight43=(HEIGHT-c.height())/2
c=addborders(c,0,halfheight43,0,halfheight43)
#Add vertical overscan
c=letterbox(c,overscan*8,overscan*8,overscan*8,overscan*8)
return(c)
}

Function SimpleDVDCrop(clip c, bool interlaced) {
   global SourceDVDHeight=c.height()
   global SourceDVDWidth=c.width()
   c=(interlaced==false) ? AutoCrop(c,0, 4, 2, 2, 2, 2, 2, 40, 10) : c
   return(c)
}

Function SimpleDVDResize(clip c, int NewFullWidth, int NewFullHeight, int source_widescreen, int widescreen) {
   SAR=(source_widescreen==0) ? 1.3333 : 1.77777
   DAR=(widescreen==0) ? 1.3333 : 1.77777
   #Factor is: (SourceHeight with borders*SourceAspectRatio) /
   #            (Newheight with borders*DestinationAspectRatio)
   factor= ((SourceDVDHeight*1.0)*SAR) / ( (NewFullHeight*1.0)*DAR)
   # Stores parameters to be used again later
   global gNewFullWidth = NewFullWidth
   global gNewFullHeight = NewFullHeight
   #

   #NewCroppedHeight = (SAR==DAR)&&(NewFullHeight==SourceDVDHeight) ? c.height() : round((c.height()+.0)/(factor+.0)/16)*16
   #NewCroppedWidth= round((NewFullWidth - (((NewFullWidth+.0)/(SourceDVDWidth+.0))*((SourceDVDWidth+.0)-(c.width()+.0))))/4)*4
   Height = (SAR==DAR)&&(NewFullHeight==SourceDVDHeight) ? c.height() : round((c.height()+.0)/(factor+.0)/16)*16
   Width= round((NewFullWidth - (((NewFullWidth+.0)/(SourceDVDWidth+.0))*((SourceDVDWidth+.0)-(c.width()+.0))))/4)*4
   #c=bicubicResize(c,int(NewCroppedWidth),int(NewCroppedHeight))
   return(c)
}

Function SimpleAddBorders(clip c) {
   HorizontalBorder=((gNewFullWidth-c.width()) != 0 ) ? (gNewFullWidth-c.width)/2 : 0

   FullVerticalBorder=((gNewFullHeight-c.height()) != 0 ) ? (gNewFullHeight-c.height) : 0
   TopBorder=(FullVerticalBorder !=0) ? round((FullVerticalBorder/16))*8 : 0
   BottomBorder=(FullVerticalBorder !=0) ? ( round((FullVerticalBorder/16))*8+(FullVerticalBorder % 16) ) : 0
   c=addborders(c,HorizontalBorder,TopBorder,HorizontalBorder,BottomBorder)
   return(c)
}
