[fitsbits] Unsigned Integers

William C. Keel keel at bildad.astr.ua.edu
Mon Nov 29 10:05:53 EST 1999


William Pence <pence at tetra.gsfc.nasa.gov> wrote:
> Steve Willner wrote:
>> 
>> Some while ago, somebody posted a trick (or anyway a method) for
>> handling unsigned, 16-bit integers in FITS images.  Could somebody
>> please repost or point me to a copy of the method?  I seem to recall
>> some clever use of BZERO, but I can't figure out how to make it work.
>> Thanks.

> The method is to subtract 32768 (2**15) from each unsigned 16-bit integer
> value (which converts it into the range of a signed 16-bit integer) before
> writing it to the FITS file. Then add the keyword BZERO = 32768 to the FITS
> header to tell the reading software that this zero point offset must be added
> back to the raw signed integer values in the FITS file to regenerate the
> actual image pixel values.

> The 'trick' is that subtracting 32768 from a 16-bit unsigned integer is
> equivalent to flipping the most significant bit of the number, which is
> computationally much faster.  To add 32768 to the value when reading, you just
> flip the most significant bit again, then interprete the bits as an unsigned
> 16-bit integer.

> The same thing can be done with unsigned 32-bit integers, using BZERO =
> 2147483648 (i.e., 2**31).

There is a less than elegant, but useful, way to do this transformation
within IRAF (if you're the only other being besides myself in the whole
Local Group that finds IRAF easier to handle than, say, IDL). Courtesy
of the IRAF support folks, this script will properly fix the signed/unsigned
wrap problem:

imexpr "(a < 0 ? a+65536 : a)" outimage a=inimage


Yeah, I would have thought of that given about 6 months...

Anyway, forgive the intrusion beyond strictly FITS issues, and I now return
you to more relevant threads...

Bill Keel




More information about the fitsbits mailing list