Oops, left off the '16' in 'ENDIAN16_SH'.
Be sure that the image for writing is not in a floating point format.
Use the T_BYTES_REAL macro instead of duplicating its functionality.
Oops, don't mask off the number of bytes in the pixel type before testing it.
Some other minor rearrangements.