#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

/* This routine reads in each real-valued pixel, checks for garbage values 
   and replaces them with nulls as needed, swaps the bytes, inverts the axes,
   and writes spits them back out for use as a FITS data segment.

   09 November 2006, ACR
*/

main( int argc, char *argv[])

{ union swapspace
    { float r4;
      char  b1[4];
    };

  int   lines,samples;
  int   row,col;
  float img[1024][1022];
  union swapspace pixel;
  char  hold;

  /* Initialize: */

  lines = 1022;
  samples = 1024;

  for (row=0; row<lines; row++)
    { for (col=0; col<samples; col++)
        { fread((void *)&(pixel.r4),4,1,stdin);

          if (pixel.r4 < 1.0e-06)
            { pixel.r4 = 0.0; }
          else
            { hold = pixel.b1[0];
              pixel.b1[0] = pixel.b1[3];
              pixel.b1[3] = hold;
              hold = pixel.b1[1];
              pixel.b1[1] = pixel.b1[2];
              pixel.b1[2] = hold;
            }
 
          img[col][row] = pixel.r4;
        }
    }

  for (col=0; col<samples; col++)
    { for (row=0; row<lines; row++)
        { fwrite((void *)&(img[col][row]),4,1,stdout); }
    }

  exit(0);
}