Use RAII

I would use FreeImagePlus, but it seems to be missing some
functionality that we use.