Fix various memory leaks

This should fix various memory leaks.
One rare leak which happens if --input-pages 2 is set and the other ones in loadImage() and saveImage().
Those were found with valgrind. My tests reveal no more merory leaks and valgrind is happy, too.