unsigned images -- RESULTS
Russell E. Owen
owen at astro.washington.edu
Fri Feb 28 13:42:15 EST 1997
Thanks to Bill Owen for providing the math that did the trick. I am using
C, so I simply toggling the sign bit by XORinig with 8000 hex, but as he
pointed out, one could also use an if statement and add/subtract 32768
(depending on the value, to avoid over/underflow). And of course many
compilers will simply ignore the under/overflow, allowing straight addition
or subtraction.
An aside:Paul Schlyter noted that these manipulations assume
twos-complement integer representation. Well it turns out that the FITS
standard actually REQUIRES that representation.
For computers that use something other than twos-complement for signed
integers, one technique that would work (I'm not claiming it's the best, I
don't know) is:
- represent, or convert, the data to 2-byte UNsigned
- apply the math listed above to convert the bit pattern to twos complement
signed 2-byte integers
- use a binary write to dump the data to the FITS file
(This is an argument in favor of always using unsigned integers for integer
image data; no ambiguity except the inevitable byte order problem).
For reading 2-byte integer FITS data, simply reverse the process -- read
the data into an unsigned integer array, then perform the math above (it
toggles the info, amusingly enough) to produce genuinely signed data. Your
machine's own representation of signed data is totally ignored.
I have coded this all up and it works perfectly and the code is nice and
simple. A general FITS reader that produces 2-byte unsigned integers (my
internal representation) would be a lot of work. Fortunately, my program
scarcely needs to read FITS at all; it's oocasionally useful to read files
I have written, and that's it. So I made things easy by only parsing 2-byte
signed FITS files. No sweat.
Thanks again, all of you.
-- Russell
More information about the fitsbits
mailing list