Converting 16-bit images to 8-bit
It took us several attempts to get the remapping "right", as ImageJ seems to do the binning / calculations in a rather surprising way.
Eventually we went for the way of dividing the pixel values by a fixed divisor configured for 12 and 16 bits instead of calculating it on the fly (depending on the actual value range bit depth).
For the variant of using the min/max display range with a calculated maximum, see snippet map_to_8bit_carefully.ijm.