REM	This example script mimics the Grayscale filter.
REM	The intent here is to show how to use a script to process 
REM	through an entire image, pixel by pixel.

REM	== Script start
	START

REM	== Set up a few variables
	STORE x 0
	STORE y 0

REM	== Start the main loop
	LABEL YLOOP
		LABEL XLOOP

REM	== Fetch the current pixel
		IMAGE.GETPIXEL x y
		STORE rgb .RESULT

REM	== Average the color's values
		COLOR.RED rgb
		STORE r .RESULT
		COLOR.GREEN rgb
		STORE g .RESULT
		COLOR.BLUE rgb
		STORE b .RESULT

		MATH.ADD r g
		MATH.ADD .RESULT b
		MATH.DIV .RESULT 3

		STORE n .RESULT
		COLOR.NEW n n n

REM	== Assign the pixel
		IMAGE.SETPIXEL x y .RESULT

REM	== Move to the next X coordinate
		MATH.ADD x 1
		STORE x .RESULT
		LTHAN x .WIDTH
		JUMPIF XLOOP

REM	== We've finished the row; move to the next Y
REM	== coordinate
	STORE x 0
	MATH.ADD y 1
	STORE y .RESULT
	LTHAN y .HEIGHT
	JUMPIF YLOOP

REM	== We've finished processing the image.

	END
