;+
;open avi file
;+
FUNCTION AviOpenw, filename, dimensions, red, green, blue, $
					Rate = rate				, $
					True = true				, $
					RootDir = rootDir		, $
					Codec = codec			, $
					Quality = quality		, $
					Iframe_Gap = iframe_gap	, $
					Bitrate = bitrate

	COMPILE_OPT IDL2

	IF filename EQ '' THEN  Print, 'can not open file'
	IF N_Elements(dimensions) NE 2 THEN Print, 'wrong dimension'
	IF NOT Keyword_Set(rate) THEN rate = 15
	rate = 1>rate< 25

	COMMON colors, r_orig, g_orig, b_orig, r_curr, g_curr, b_curr

	IF NOT Keyword_Set(True) THEN BEGIN
		avi_id = [8l,Long(dimensions),LonArr(6)]
		r = BytArr(256, /NoZero)
		g = BytArr(256, /NoZero)
		b = BytArr(256, /NoZero)

		IF N_Elements(red) LE 0 THEN BEGIN
			IF N_Elements(r_curr) EQ 0 THEN Loadct, 0, /Silent
			red = r_curr
			green = g_curr
			blue = b_curr
		ENDIF
		r[0] = red
		g[0] = green
		b[0] = blue
	ENDIF ELSE BEGIN
		avi_id = [24l, Long(dimensions), LonArr(6)]
		r = 0b
		g = 0b
		b = 0b
	ENDELSE

	IF NOT Keyword_Set(codec) THEN codec = ''
	codec = Byte(codec)
	IF N_Elements(codec) NE 4 THEN codec = BytArr(4)
	IF NOT Keyword_Set(quality) THEN quality = 100
	quality = 0>quality<100
	IF NOT Keyword_Set(iframe_gap) THEN iframe_gap=0
	IF NOT Keyword_Set(bitrate) THEN bitrate=0
	error = Call_External(rootDir+'\resource\avi\avi.dll'	, $
								'avi_openw'		    , $
								avi_id,fileName		, $
								r, g, b, Byte(rate)	, $
								Byte(codec)			, $
								Byte(quality)		, $
								Long(iframe_gap)	, $
								Long(bitrate)		)

	CASE error OF
		0: Return, avi_id
		1: error_message = 'unable set up'+fileName
		2: error_message = 'unable assign memory'
		3: error_message = 'unable set up video'
		4: error_message = 'unable set up parameter'
		5: error_message = 'unable set up video'
		6: error_message = 'unable set up video format'
	ENDCASE

	Print, error_message
END