# AVSLib :: 'clip' package :: 'core' module
# Copyright (c) 2005 George Zarkadas (gzarkadas@users.sourceforge.net)

# This program is free software; you can redistribute it and/or modify it under the terms of 
# the GNU General Public License as published by the Free Software Foundation; either 
# version 2 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
# PURPOSE.  See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this program; 
# if not, write to the "Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
# MA  02111-1307  USA"

DefineModule("avslib", "clip", "core")

global __load__ = Eval("""
	LoadModule("avslib", "numeric", "rounding")
	LoadModule("avslib", "array", "core")
	""")

# Returns the clip colorspace as a "pixel_type" string (as it is used 
# by many Avisynth filters).
# If 'uppercase' == false it returns the string in lowecase instead of 
# uppercase (the default).
#
Function ColorSpace(clip base, bool "uppercase")
{
	uppercase = Default(uppercase, true)
	# if colorspace is YUY2 or YV12, IsYUV == true. 
	# Similarly if colorspace is RGB24 or RGB32, IsRGB == true
	ret = base.IsYUV \
		? (base.IsYUY2  ? "YUY2"  : "YV12" ) \
		: (base.IsRGB32 ? "RGB32" : "RGB24")
	return uppercase ? ret : LCase(ret)
}

# Returns true if the string passed in is a valid "pixel_type" string 
# (as it is used by many Avisynth filters).
# If 'extended' == true the value of "RGB" is also considered to be a
# valid pixel type (is used by the Show{Red/Green/Blue/Alpha} filters).
#
Function IsPixelType(string pixel_type, bool "extended")
{
	pixel_type = UCase(pixel_type)
	extended = Default(extended, false)
	b_pxtype = pixel_type == "YUY2" || pixel_type == "YV12" \
		|| pixel_type == "RGB32" || pixel_type == "RGB24"
	return extended \
		? b_pxtype || pixel_type == "RGB" \
		: b_pxtype
}

# Combines three (R,G,B) channel color values into a 
# single RGB color value as it is used by other Avisynth filters.
#
Function MakeRGBColor(int red, int green, int blue)
{
	red = red % 256
	green = green % 256
	blue = blue % 256
	return (red * 256 + green) * 256 + blue
}

# Returns an array triplet of R,G,B values from color 'rgb_color'
#
Function SplitRGBColor(int rgb_color)
{
	b = rgb_color % 256
	g = ((rgb_color - b) / 256) % 256
	r = ((rgb_color - g) / 65536) % 256
	return ArrayCreate(r, g, b)
}

# Returns the dimension passed in appropriately modified so that 
# passing it to a resize function will not crash Avisynth (ver. < 2.5.6)
# or result in an error dialog box (ver. >= 2.5.6).
# NOTE: for interlaced clips, set interlaced = true.

Function SafeWidth(clip c, int target_width)
{
	base = c.IsYV12 ? 4 : (c.IsYUY2 ? 2 : 1)
	return RoundBs(target_width, base)
}

Function SafeHeight(clip c, int target_height, bool "interlaced")
{
	interlaced = Default(interlaced, false)
	base = (c.IsYV12 ? 2 : 1) * (interlaced ? 2 : 1)
	return RoundBs(target_height, base)
}
