home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pnm / pnmtofits.c < prev    next >
C/C++ Source or Header  |  1994-02-14  |  5KB  |  177 lines

  1. /* pnmtofits.c - read a portable anymap and produce a FITS file
  2. **
  3. ** Copyright (C) 1989 by Wilson H. Bent (whb@hoh-2.att.com).
  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. ** Modified by Alberto Accomazzi (alberto@cfa.harvard.edu), Dec 1, 1992.
  13. **
  14. ** Added support for PPM files, the program is renamed pnmtofits.
  15. ** This program produces files with NAXIS = 2 if input file is in PBM
  16. ** or PGM format, and NAXIS = 3, NAXIS3 = 3 if input file is a PPM file.
  17. ** Data is written out as either 8 bits/pixel or 16 bits/pixel integers,
  18. ** depending on the value of maxval in the input file.
  19. ** Flags -max, -min can be used to set DATAMAX, DATAMIN, BSCALE and BZERO
  20. ** in the FITS header, but do not cause the data to be rescaled.
  21. */
  22.  
  23. #include "pnm.h"
  24. #define write_card(s)    fwrite( s, sizeof(char), 80, stdout )
  25.  
  26. int
  27. main( argc, argv )
  28.     int argc;
  29.     char* argv[];
  30.     {
  31.     FILE* ifp;
  32.     xel** xels;
  33.     xel* xelrow;
  34.     register xel* xP;
  35.     int argn, row, col, rows, cols, planes, format, i, npad,
  36.         bitpix, forcemin, forcemax;
  37.     double datamin, datamax, bscale, fits_bzero, frmax, frmin;
  38.     xelval maxval;
  39.     register unsigned short color;
  40.     char card[81];
  41.     char* usage = "[-max f] [-min f] [pnmfile]";
  42.  
  43.     pnm_init( &argc, argv );
  44.  
  45.     forcemin = 0;
  46.     forcemax = 0;
  47.     argn = 1;
  48.  
  49.     while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  50.     {
  51.         if ( pm_keymatch( argv[argn], "-max", 3 ) )
  52.       {
  53.       ++argn;
  54.       forcemax = 1;
  55.       if ( argn == argc || sscanf( argv[argn], "%lf", &frmax ) != 1 )
  56.         pm_usage( usage );
  57.       }
  58.         else if ( pm_keymatch( argv[argn], "-min", 3 ) )
  59.       {
  60.       ++argn;
  61.       forcemin = 1;
  62.       if ( argn == argc || sscanf( argv[argn], "%lf", &frmin ) != 1 )
  63.         pm_usage( usage );
  64.       }
  65.         else
  66.         pm_usage( usage );
  67.     ++argn;
  68.     }
  69.  
  70.     if ( argn != argc )
  71.     {
  72.     ifp = pm_openr( argv[argn] );
  73.     ++argn;
  74.     }
  75.     else
  76.     ifp = stdin;
  77.  
  78.     if ( argn != argc )
  79.     pm_usage( usage );
  80.  
  81.     xels = pnm_readpnm( ifp, &cols, &rows, &maxval, &format );
  82.  
  83.     datamin = 0.0;
  84.     datamax = (double) maxval;
  85.     if (forcemin) datamin = frmin;
  86.     if (forcemax) datamax = frmax;
  87.     fits_bzero = datamin;
  88.     bscale = ( datamax - datamin ) / ( double ) maxval;
  89.  
  90.     bitpix = 8;
  91.     if (maxval > 255) bitpix = 16;
  92.  
  93.     pm_close( ifp );
  94.  
  95.     /* Figure out the proper depth */
  96.     switch ( PNM_FORMAT_TYPE(format) )
  97.     {
  98.     case PPM_TYPE:
  99.     planes = 3;
  100.     break;
  101.  
  102.     default:
  103.     planes = 1;
  104.     break;
  105.     }
  106.  
  107.     /* write out fits header */
  108.     i = 0;
  109.     sprintf( card, "SIMPLE  =                    T                                                  " );
  110.     write_card( card ); ++i;
  111.     sprintf( card, "BITPIX  =           %10d                                                  ", bitpix );
  112.     write_card( card ); ++i;
  113.     sprintf( card, "NAXIS   =           %10d                                                  ", ( planes == 3 ) ? 3 : 2 );
  114.     write_card( card ); ++i;
  115.     sprintf( card, "NAXIS1  =           %10d                                                  ", cols );
  116.     write_card( card ); ++i;
  117.     sprintf( card, "NAXIS2  =           %10d                                                  ", rows );
  118.     write_card( card ); ++i;
  119.     if ( planes == 3 )
  120.     {
  121.         sprintf( card, "NAXIS3  =                    3                                                  " );
  122.         write_card( card ); ++i;
  123.     }
  124.     sprintf( card, "BSCALE  =         %lE                                                   ", bscale );
  125.     write_card( card ); ++i;
  126.     sprintf( card, "BZERO   =         %lE                                                   ", fits_bzero );
  127.     write_card( card ); ++i;
  128.     sprintf( card, "DATAMAX =         %lE                                                   ", datamax );
  129.     write_card( card ); ++i;
  130.     sprintf( card, "DATAMIN =         %lE                                                   ", datamin );
  131.     write_card( card ); ++i;
  132.     sprintf( card, "HISTORY Created by pnmtofits.                                                   " );
  133.     write_card( card ); ++i;
  134.     sprintf( card, "END                                                                             " );
  135.     write_card( card ); ++i;
  136.  
  137.     /* pad end of header with blanks */
  138.     npad = ( i * 80 ) % 2880;
  139.     if ( npad == 0 )
  140.     npad = 2880;
  141.     while ( npad++ < 2880 )
  142.     putchar ( ' ' );
  143.     
  144.     for ( i = 0; i < planes; i++ )
  145.         for ( row = 0; row < rows; ++row )
  146.         {
  147.         xelrow = xels[row];
  148.         for ( col = 0, xP = xelrow; col < cols; ++col, ++xP )
  149.         {
  150.         if ( planes == 3 )
  151.             {
  152.             /* we are reading a ppm file */
  153.             if ( i == 0 )     color = PPM_GETR( *xP );
  154.             else if ( i == 1 )     color = PPM_GETG( *xP );
  155.             else         color = PPM_GETB( *xP );
  156.             }
  157.         else
  158.             color = PNM_GET1( *xP );
  159.         
  160.         if ( bitpix == 16 )
  161.             putchar( ( color >> 8 ) & 0xff );
  162.  
  163.         putchar( color & 0xff );
  164.         }
  165.         }
  166.     
  167.     /* pad end of file with nulls */
  168.     npad = ( rows * cols * planes * bitpix / 8 ) % 2880;
  169.     if ( npad == 0 )
  170.     npad = 2880;
  171.     while ( npad++ < 2880 )
  172.     putchar ( 0 );
  173.  
  174.     exit( 0 );
  175.     }
  176.  
  177.