Add a helper function for flipping image data.

This commit also fixes a bug that would cause an invalid memcpy.