Inital source code from http://www.labbookpages.co.uk/software/imgProc/files/libPNG/makePNG.c