#!/usr/bin/env ascript
import(sdl)
import(gif)

imgfile(filename:string) = path.join(path.dirname(sys.executable),
									'sample', 'resource', filename)

gif.read(imgfile('kangaroo.gif'), `rgba) {|g|
	syms = @{
		`lsd$LogicalScreenWidth
		`lsd$LogicalScreenHeight
		`lsd$GlobalColorTableFlag
		`lsd$ColorResolution
		`lsd$SortFlag
		`lsd$SizeOfGlobalColorTable
		`lsd$BackgroundColorIndex
		`lsd$PixelAspectRatio
		`cmmt$CommentData
		`ptxt$TextGridLeftPosition
		`ptxt$TextGridTopPosition
		`ptxt$TextGridWidth
		`ptxt$TextGridHeight
		`ptxt$CharacterCellWidth
		`ptxt$CharacterCellHeight
		`ptxt$TextForegroundColorIndex
		`ptxt$TextBackgroundColorIndex
		`ptxt$PlainTextData
		`appl$ApplicationIdentifier
		`appl$AuthenticationCode
		`appl$ApplicationData
	}
	symsGctl = @{
		`DisposalMethod
		`UserInputFlag
		`TransparentColorFlag
		`DelayTime
		`TransparentColorIndex
	}
	symsDesc = @{
		`ImageLeftPosition
		`ImageTopPosition
		`ImageWidth
		`ImageHeight
		`LocalColorTableFlag
		`InterlaceFlag
		`SortFlag
		`SizeOfLocalColorTable
	}
	printf("%-*s %s\n", syms:*tostring():*len().max(),
							syms:*tostring(), g.getprop!(syms, '(not defined)'))
	for (img in g.images) {|i|
		printf("image#%d\n", i)
		printf("gif$gctl.%-*s %s\n", symsGctl:*tostring():*len().max(),
			symsGctl:*tostring(), img.gif$gctl.getprop!(symsGctl, '(not defined)'))
		printf("gif$desc.%-*s %s\n", symsDesc:*tostring():*len().max(),
			symsDesc:*tostring(), img.gif$desc.getprop!(symsDesc, '(not defined)'))
	}
	g.appl$ApplicationIdentifier.dump()
	g.appl$AuthenticationCode.dump()
	g.write('>a.gif')
}
sdl.Init(sdl.INIT_EVERYTHING)
img = image(`rgba).readgif('a.gif').resize(400)
sdl.SetVideoMode(img.width, img.height, 16, sdl.SWSURFACE) {|screen|
	screen.PutSurface(img, 0, 0)
	screen.UpdateRect()
}
repeat {
	event = sdl.WaitEvent()
	if (event.type == sdl.QUIT || \
		(event.type == sdl.KEYDOWN && event.sym == sdl.K_ESCAPE)) { break }
}
