home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume9 / pbmplus / part09 / pgm / fitstopgm.c < prev    next >
C/C++ Source or Header  |  1989-11-26  |  5KB  |  196 lines

  1. /* fitstopgm.c - read a FITS file and produce a portable graymap
  2. **
  3. ** Copyright (C) 1989 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include <stdio.h>
  14. #include "pgm.h"
  15. #ifdef SYSV
  16. #include <string.h>
  17. #else /*SYSV*/
  18. #include <strings.h>
  19. #endif /*SYSV*/
  20.  
  21. #define min(a,b) ((a) < (b) ? (a) : (b))
  22.  
  23. struct FITS_Header {
  24.     int simple;        /* basic format or not */
  25.     int bitpix;        /* number of bits per pixel */
  26.     int naxis;        /* number of axes */
  27.     int naxis1;        /* number of points on axis 1 */
  28.     int naxis2;        /* number of points on axis 2 */
  29.     double datamin;    /* min # */
  30.     double datamax;    /* max # */
  31.     double bzero;    /* ??? */
  32.     double bscale;    /* ??? */
  33.     };
  34.  
  35. main( argc, argv )
  36. int argc;
  37. char *argv[];
  38.     {
  39.     FILE *ifd;
  40.     register gray *grayrow, *gP;
  41.     int argn, row;
  42.     register int col;
  43.     gray maxval;
  44.     double fmaxval, scale;
  45.     int rows, cols;
  46.     struct FITS_Header h;
  47.  
  48.     pm_progname = argv[0];
  49.  
  50.     argn = 1;
  51.  
  52.     if ( argn < argc )
  53.     {
  54.     ifd = pm_openr( argv[argn] );
  55.     argn++;
  56.     }
  57.     else
  58.     ifd = stdin;
  59.  
  60.     if ( argn != argc )
  61.     pm_usage( "[fitsfile]" );
  62.  
  63.     read_fits_header( ifd, &h );
  64.  
  65.     if ( ! h.simple )
  66.     pm_error( "FITS file is not in simple format, can't read", 0,0,0,0,0 );
  67.     switch ( h.bitpix )
  68.     {
  69.     case 8:
  70.     fmaxval = 255.0;
  71.     break;
  72.  
  73.     case 16:
  74.     fmaxval = 65535.0;
  75.     break;
  76.  
  77.     case 32:
  78.     fmaxval = 4294967295.0;
  79.     break;
  80.  
  81.     default:
  82.     pm_error( "unusual bits per pixel (%d), can't read", h.bitpix, 0,0,0,0 );
  83.     }
  84.     if ( h.naxis != 2 )
  85.     pm_error( "FITS file has %d axes, can't read", h.naxis, 0,0,0,0 );
  86.     cols = h.naxis1;
  87.     rows = h.naxis2;
  88.     maxval = min( fmaxval, PGM_MAXMAXVAL );
  89.     scale = maxval / ( h.datamax - h.datamin );
  90.  
  91.     pgm_writepgminit( stdout, cols, rows, maxval );
  92.     grayrow = pgm_allocrow( cols );
  93.     for ( row = 0; row < rows; row++)
  94.     {
  95.     for ( col = 0, gP = grayrow; col < cols; col++, gP++ )
  96.         {
  97.         int ich;
  98.         double val;
  99.  
  100.         switch ( h.bitpix )
  101.         {
  102.         case 8:
  103.         ich = getc( ifd );
  104.         if ( ich == EOF )
  105.             pm_error( "premature EOF", 0,0,0,0,0 );
  106.         val = ich;
  107.         break;
  108.  
  109.         case 16:
  110.         ich = getc( ifd );
  111.         if ( ich == EOF )
  112.             pm_error( "premature EOF", 0,0,0,0,0 );
  113.         val = ich << 8;
  114.         ich = getc( ifd );
  115.         if ( ich == EOF )
  116.             pm_error( "premature EOF", 0,0,0,0,0 );
  117.         val += ich;
  118.         break;
  119.  
  120.         case 32:
  121.         ich = getc( ifd );
  122.         if ( ich == EOF )
  123.             pm_error( "premature EOF", 0,0,0,0,0 );
  124.         val = ich << 24;
  125.         ich = getc( ifd );
  126.         if ( ich == EOF )
  127.             pm_error( "premature EOF", 0,0,0,0,0 );
  128.         val += ich << 16;
  129.         ich = getc( ifd );
  130.         if ( ich == EOF )
  131.             pm_error( "premature EOF", 0,0,0,0,0 );
  132.         val += ich << 8;
  133.         ich = getc( ifd );
  134.         if ( ich == EOF )
  135.             pm_error( "premature EOF", 0,0,0,0,0 );
  136.         val += ich;
  137.         break;
  138.  
  139.         default:
  140.         pm_error( "can't happen", 0,0,0,0,0 );
  141.         }
  142.         *gP = (gray) ( scale * ( val * h.bscale + h.bzero - h.datamin) );
  143.         }
  144.     pgm_writepgmrow( stdout, grayrow, cols, maxval );
  145.     }
  146.     pm_close( ifd );
  147.  
  148.     exit( 0 );
  149.     }
  150.  
  151. read_fits_header( fd, hP )
  152. FILE *fd;
  153. struct FITS_Header *hP;
  154.     {
  155.     char buf[80];
  156.     int seen_end;
  157.     int i;
  158.     char c;
  159.  
  160.     seen_end = 0;
  161.     hP->simple = 0;
  162.     hP->bzero = 0.0;
  163.     hP->bscale = 1.0;
  164.     hP->datamin = 0.0;
  165.     hP->datamax = 1.0;
  166.  
  167.     while ( ! seen_end )
  168.     for ( i = 0; i < 36; i++ )
  169.         {
  170.         read_card( fd, buf );
  171.  
  172.         if ( sscanf( buf, "SIMPLE = %c", &c ) == 1 )
  173.         {
  174.         if ( c == 'T' || c == 't' )
  175.             hP->simple = 1;
  176.         }
  177.         else if ( sscanf( buf, "BITPIX = %d", &(hP->bitpix) ) == 1 );
  178.         else if ( sscanf( buf, "NAXIS = %d", &(hP->naxis) ) == 1 );
  179.         else if ( sscanf( buf, "NAXIS1 = %d", &(hP->naxis1) ) == 1 );
  180.         else if ( sscanf( buf, "NAXIS2 = %d", &(hP->naxis2) ) == 1 );
  181.         else if ( sscanf( buf, "DATAMIN = %lg", &(hP->datamin) ) == 1 );
  182.         else if ( sscanf( buf, "DATAMAX = %lg", &(hP->datamax) ) == 1 );
  183.         else if ( sscanf( buf, "BZERO = %lg", &(hP->bzero) ) == 1 );
  184.         else if ( sscanf( buf, "BSCALE = %lg", &(hP->bscale) ) == 1 );
  185.         else if ( strncmp( buf, "END ", 4 ) == 0 ) seen_end = 1;
  186.         }
  187.     }
  188.  
  189. read_card( fd, buf )
  190. FILE *fd;
  191. char *buf;
  192.     {
  193.     if ( fread( buf, 1, 80, fd ) == 0 )
  194.     pm_error( "error reading header", 0,0,0,0,0 );
  195.     }
  196.