#!/usr/bin/env ascript
import(sdl)
import(bmp)
import(msicon)
import(jpeg)
import(png)
import(gif)
import(hash)
import(freetype)

tester(test:expr):map:dynamic_scope = printf("%s .. %s\n", test, eval(test)):nomap
I(filename:string) = path.join(sys.datadir, 'sample', 'resource', filename)
tester_image(image:image, comment:string):map:void = {
	printf('%-20s %3dx%3d\n', comment, image.width, image.height)
	sdl.Init(sdl.INIT_EVERYTHING)
	screen = sdl.SetVideoMode(image.width, image.height, 16, sdl.SWSURFACE)
	screen.PutSurface(image)
	screen.UpdateRect()
	repeat {
		event = sdl.WaitEvent()
		(event.type == sdl.QUIT) && sys.exit()
		(event.type == sdl.KEYDOWN && event.sym == sdl.K_SPACE) && break
	}
}
tester_image(image:image, comment:string):map:void = {
	md5 = hash.md5()
	image.bmpwrite(md5)
	printf('%-20s %3dx%3d %s\n', comment, image.width, image.height, md5.hexdigest)
}
if (sys.build != `gnuc) {
	lambda {
		img = image(`rgb, 32, 32, `black)
		face = freetype.open('msgothic.ttc').setheight(16).setcolor(`white)
		str = '\xe5\x9c\x92' // kanji 'en'
		img.drawtext(face, -4, 12, str)
		img.drawtext(face, 24, 12, str)
		img.drawtext(face, -4, 36, str)
		img.drawtext(face, 24, 36, str)
		tester_image(img, 'msgothic 16pix')
	}()
	lambda {
		img = image(`rgb, 32, 32, `black)
		face = freetype.open('arial.ttf').setheight(16).setcolor(`white)
		img.drawtext(face, -8, 8, '@')
		img.drawtext(face, 24, 8, '@')
		img.drawtext(face, -8, 36, '@')
		img.drawtext(face, 24, 36, '@')
		tester_image(img, 'arial 16pix')
	}()
	lambda {
		img = image(`rgb, 320, 320, `black)
		face = freetype.open('arial.ttf').setheight(320).setcolor(`white)
		img.drawtext(face, 0, 240, '@')
		tester_image(img, 'arial 320pix')
	}()
}
lambda {
	charLines = nil
	for (fileName in ['arrow-green-u.gif', 'arrow-green-l.gif', 'arrow-green-d.gif', 'arrow-green-r.gif']) {
		img = image(I(fileName), `rgb)
		charLines = charLines || repeat (img.height):list {''}
		charLines = for (charLine in charLines, row in img.each():*gray.fold(img.width)):list {
			charLine + ' ' + choose(int(row / 64), '@', '*', '.', ' ').join()
		}
	}
	println(charLines)
}()
lambda {
	imgs = msicon.read(I('msnms.ico'), `rgba).images
	tester_image(imgs:*delpalette(), format('msnms #%d', 0..))
	md5 = hash.md5()
	msicon.msicon().addimage(imgs).write(md5)
	printf("icon output: %s\n", md5.hexdigest)
}()
lambda {
	tester_image(gif.read(I('chicken-anim.gif'), `rgba).images:*delpalette(),
													format('chicken #%d', 0..))
}()
lambda {
	img = image(I('Water_lilies.jpg'), `rgb)
	tester_image(img.reducecolor(palette(`win256)).delpalette(), 'reducecolor')
	img = image(I('kangaroo.png'), `rgba)
	tester_image(img, 'normal kangaroo')
	tester_image(img.flip(`horz), 'flip horizontal')
	tester_image(img.flip(`vert), 'flip vertical')
	tester_image(img.flip(`both), 'flip both')
	tester_image(img.rotate(90), 'rotate 90')
	tester_image(img.rotate(-90), 'rotate -90')
	degrees = range(0, 360, 30)
	tester_image(img.rotate(degrees), format('rotate %d', degrees))
}()
lambda {
	plt = palette(`blank256)
	plt.updateby(palette(`basic))
	println(plt.each():*html().join(','))
	plt.updateby(palette(`win256))
	println(plt.each():*html().join(','))
	plt = palette(`blank256)
	plt.updateby(palette(`basic)):shrink
	println(plt.each():*html().join(','))
	plt = palette(`blank256)
	plt.updateby(image(I('kangaroo.gif'), `rgb))
	println(plt.each():*html().join(','))
	plt.shrink()
	println(plt.each():*html().join(','))
	try {
		plt = palette(`blank16)
		plt.updateby(image(I('kangaroo.gif'), `rgb))
	} except {|e|
		println('expected error occurs: ', e.text)
	}
	plt = palette(`win256)
	plt2 = plt.clone()
	plt2[0, 1, 2, 3] = color(`red)
	println(plt.each():*html().join(','))
	println(plt2.each():*html().join(','))
}()
lambda {
	md5 = hash.md5()
	image(I('chicken256.bmp'), `rgb).delpalette().bmpwrite(md5.init())
	println('chicken256.bmp to BMP: ', md5.hexdigest)
	image(I('chicken16.bmp'), `rgb).delpalette().bmpwrite(md5.init())
	println('chicken16.bmp to BMP: ', md5.hexdigest)
	image(I('Winter.jpg'), `rgb).bmpwrite(md5.init())
	println('Winter.jpg to BMP: ', md5.hexdigest)
	image(I('kangaroo.png'), `rgb).bmpwrite(md5.init())
	println('kangaroo.png to BMP: ', md5.hexdigest)
	image(I('kangaroo.gif'), `rgb).delpalette().bmpwrite(md5.init())
	println('kangaroo.gif to BMP: ', md5.hexdigest)
	image(I('kangaroo.png'), `rgb).gifwrite(md5.init())
	println('kangaroo.png to GIF: ', md5.hexdigest)
	image(I('kangaroo.png'), `rgba).gifwrite(md5.init())
	println('kangaroo.png to GIF (alpha): ', md5.hexdigest)
	img:image = I('kangaroo.png'), img.gifwrite(md5.init())
	println('kangaroo.png to GIF (cast): ', md5.hexdigest)
	image(I('Winter.jpg'), `rgb).jpegwrite(md5.init())
	println('Winter.jpg to JPEG: ', md5.hexdigest)
	image(I('Winter.jpg'), `rgb).pngwrite(md5.init())
	println('Winter.jpg to PNG: ', md5.hexdigest)
	try {
		image(I('Winter.jpg'), `rgb, 'png')
	} except {|e|
		println('expected error occurs: ', e.text)
	}
	try {
		image(I('kangaroo.png'), `rgb, 'jpeg')
	} except {|e|
		println('expected error occurs: ', e.text)
	}
	try {
		image(I('kangaroo.png'), `rgb, 'gif')
	} except {|e|
		println('expected error occurs: ', e.text)
	}
	try {
		image(I('kangaroo.png'), `rgb, 'bmp')
	} except {|e|
		println('expected error occurs: ', e.text)
	}
}()
lambda {
	tester(`palette(`blank2).each():*html().join(','))
	tester(`palette(`blank4).each():*html().join(','))
	tester(`palette(`blank8).each():*html().join(','))
	tester(`palette(`blank16).each():*html().join(','))
	tester(`palette(`blank32).each():*html().join(','))
	tester(`palette(`blank64).each():*html().join(','))
	tester(`palette(`blank128).each():*html().join(','))
	tester(`palette(`blank256).each():*html().join(','))
	tester(`palette(`basic).each():*html().join(','))
	tester(`palette(`win256).each():*html().join(','))
	tester(`palette(`websafe).each():*html().join(','))
}()
lambda {
	tester(`(color * [1]))
	tester(`(color * [1, 2]))
	tester(`(color * [1, 2, 3]))
	tester(`(color * [1, 2, 3, 4]))
	try {
		tester(`(color * [1, 2, 3, 4, 5]))
	} except {|e|
		println('expected error occurs: ', e.text)
	}
	tester(`(color * [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10], [11, 12], [13, 14, 15, 16]]))
}()
lambda {
	plt = palette(`basic)
	names = @{
		`black, `maroon, `green, `olive, `navy, `purple, `teal, `gray,
		`silver, `red, `lime, `yellow, `blue, `fuchsia, `aqua, `white,
		"#ff0000", "#00ff00", "#0000ff", "#123456", "#123",
	}
	printf("%-*s %s %d\n", names:*tostring():*len().max(),
						names, color(names), plt.nearest(color(names)):index)
}()
