Subject: Re: unsigned images

From: owen at astro.washington.edu (Russell E. Owen)
Date: Wed, 26 Feb 1997 11:09:48 -0800

In article <5ep2l1$nko at electra.saaf.se>, pausch at electra.saaf.se (Paul
Schlyter) wrote:

>In article <5ekod7$17m at netline-fddi.jpl.nasa.gov>,
>Bill Owen <wmo at wansor.jpl.nasa.gov> wrote:
> 
> (regarding converting unsigned data into signed, for FITS data output)...
>>
>>       IF (VALUE(I) .GE. 0) THEN
>>          VALUE(I) = VALUE(I) - 32768
>>       ELSE
>>          VALUE(I) = VALUE(I) + 32768
>>       END IF
> 
>This is non-portable, since it assumes integers are stored as
>2-complement binary numbers, and ANSI FORTRAN has no such requirements
>on integers.  Your code will fail on platforms using 1-complement
>or sign-magnitude integer representations - not to mention BCD
>representation..... yes, there are FORTRAN implementations using
>BCD for integers.

Unfortunately true. Is it even possible to have an algorithm that works for
all of the commonly found integer representations? I could not think of
one. This is one good argument for supporting unsigned FITS images -- it
involves less mucking around with machine-specific and compiler-specific
internals.

Meanwhile, I don't see much choice except to code for a specific integer
representation.

-- Russell

---

From: owen at astro.washington.edu (Russell E. Owen)
Date: Wed, 26 Feb 1997 11:00:25 -0800

In article <5ekod7$17m at netline-fddi.jpl.nasa.gov>, wmo at wansor.jpl.nasa.gov
(Bill Owen) wrote:
>...
>In ANSI C, however, the problem is trivial.  Use the bitwise exclusive OR
>operator, and the one statement
>      value[i] ^= 0x8000;
>will flip the sign bit and leave the rest alone.  (Yes, I realize that one
>would probably code it up using a pointer instead of a subscript.)  The bit
>pattern 0x8000 -- sign bit on, others off -- is -32768 on two's complement
>machines.
>
>No matter how you do it, BZERO should be set to +32768.

Thank you very much. I was sure I was missing something, and you nailed it.
(I actually considered XOR but missed an important detail that made it
work.)

So for FITS output my plan is to:
- convert the internal data in place (saves copying it to another array)
- write it out in one operation
- convert it back again
using appropriate exception handling to be sure the internal representation
actually DOES get converted back.
(Storing the data internally as signed data is less efficient, as every
image will have to be converted even if it's never written out as a FITS
file).

If anyone has alternative suggestions, feel free to comment. Having missed
the XOR solution it seems possible I'm overlooking something equally simple
for the rest of the problem.

Regards,

-- Russell