home *** CD-ROM | disk | FTP | other *** search
/ Voyagers to the Outer Planets 6: Jupiter / VoyagerstotheOuterPlanetsVol6.cdr / software / cdcomp.c < prev    next >
C/C++ Source or Header  |  1989-09-19  |  49KB  |  1,230 lines

  1. /********************************************************************/
  2. /*  Voyager Image Decompression Program - C Version for PC, VAX,    */
  3. /*  UNIX and Macintosh systems.                                     */
  4. /*                                                                  */
  5. /*  Decompresses images using Kris Becker's subroutine DECOMP.C     */
  6. /*  which is included in this program in a shortened version.       */
  7. /*                                                                  */
  8. /*  Reads a variable length compressed VOYAGER image and outputs a  */
  9. /*  fixed length uncompressed image file in PDS format with         */
  10. /*  labels, image histogram, engineering table and 800 lines of     */
  11. /*  836 bytes (800 samples, 36 engineering bytes); or an 800 by     */
  12. /*  800 array with FITS, VICAR or no labels.  If used on a non-     */
  13. /*  byte-swapped machine the image histogram is un-swapped.         */
  14. /*                                                                  */
  15. /********************************************************************/
  16. /*                                                                  */
  17. /*  Use the following commands to compile and link to produce an    */
  18. /*  executable file:                                                */
  19. /*                                                                  */
  20. /*  On an IBM PC (using Microsoft C Version 5.x)                    */
  21. /*                                                                  */
  22. /*    cl /c cdcomp.c                                                */
  23. /*    link  cdcomp/stack:10000;                                     */
  24. /*                                                                  */
  25. /*  On a VAX:                                                       */
  26. /*                                                                  */
  27. /*    cc   cdcomp                                                   */
  28. /*    $define lnk$library sys$library:vaxcrtl.olb                   */
  29. /*    link cdcomp                                                   */
  30. /*                                                                  */
  31. /*  On a Unix host (Sun, Masscomp)                                  */
  32. /*                                                                  */
  33. /*    cc -o cdcomp cdcomp.c                                         */
  34. /*                                                                  */
  35. /*  On a Macintosh (using Lightspeed C)                             */
  36. /*                                                                  */
  37. /*    link with the following libraries:                            */
  38. /*    stdio, storage, strings, unix and MacTraps, with MacTraps     */
  39. /*    in a separate segment.                                        */
  40. /*                                                                  */
  41. /********************************************************************/
  42. /*                                                                  */
  43. /*  Use the following command to run the program:                   */
  44. /*                                                                  */
  45. /*    CDCOMP [infile] [outfile] [output format]                     */
  46. /*                                                                  */
  47. /*       infile        - name of compressed image file.             */
  48. /*       outfile       - name of uncompressed output file.          */
  49. /*       output format - selected from the following list:          */
  50. /*                                                                  */
  51. /*          1  SFDU/PDS format [DEFAULT].                           */
  52. /*          2  FITS format.                                         */
  53. /*          3  VICAR format.                                        */
  54. /*          4  Unlabelled binary array.                             */
  55. /*                                                                  */
  56. /*  On the VAX computer you will need to 'install' the program to   */
  57. /*  be able to use command line arguments using the following       */
  58. /*  command:                                                        */
  59. /*                                                                  */
  60. /*  $ vaxdcomp :== $DISKNAME:[DIRECTORY]vaxcomp.exe                 */
  61. /*                                                                  */
  62. /*  where DISKNAME is the disk drive and DIRECTORY is the           */
  63. /*  directory where VAXDCOMP.EXE is stored.                         */
  64. /*                                                                  */
  65. /********************************************************************/
  66. /*                                                                  */
  67. /* LIMS                                                             */
  68. /*  This program has been tested on a VAX 780 (VMS 4.6), SUN        */
  69. /*  Workstation (UNIX 4.2, release 3.4), an IBM PC                  */
  70. /*  (MICROSOFT 5.1 compiler) and Macintosh IIx using Lightspeed C   */
  71. /*  version 3.0.  When converting to other systems, check for       */
  72. /*  portability conflicts.                                          */
  73. /*                                                                  */
  74. /* HIST                                                             */
  75. /*  AUG89 Added code to get command line arguments for filenames    */
  76. /*  and output format; routines to free memory used by the Huffman  */
  77. /*  tree); fixed the SFDU label output length; and modified the     */
  78. /*  I/O routines so that the open for Host type 2 uses binary I/O.  */
  79. /*  JUN89 Fixed READVAR, to get length on 16-bit unswapped hosts.   */
  80. /*  JUL88 C driver to decompress standard Voyager Compressed images */
  81. /*  by Mike Martin 1989/06/10                                       */
  82. /*                                                                  */
  83. /*  Inputs   - Input file to be decompressed.                       */
  84. /*                                                                  */
  85. /*  Outputs  - Output file containing decompressed image.           */
  86. /*                                                                  */
  87. /********************************************************************/
  88.  
  89. #include <stdio.h>
  90.  
  91. #define TRUE                  1
  92. #define FALSE                 0
  93. #define RECORD_BYTES        836
  94.  
  95.                                     /* pc i/o defines               */
  96. #define O_RDONLY         0x0000     /* open for reading only        */
  97. #define O_BINARY         0x8000     /* file mode is binary          */
  98.  
  99.                                     /* vax i/o defines              */
  100. #define RECORD_TYPE      "rfm=fix"  /* VAX fixed length output      */
  101. #define CTX              "ctx=bin"  /* no translation of \n         */
  102. #define FOP          "fop=cif,sup"  /* file processing ops          */
  103.  
  104. typedef struct leaf
  105.   {
  106.    struct leaf *right;
  107.    short int dn;
  108.    struct leaf *left;
  109.   } NODE;
  110.  
  111. /*************************************************************************
  112.  Declare the tree pointer. This pointer will hold the root of the tree
  113.  once the tree is created by the accompanying routine huff_tree.
  114. **************************************************************************/
  115.  
  116.   NODE *tree;
  117.  
  118. /* subroutine definitions                                           */
  119.  
  120. void               pds_labels();
  121. void               fits_labels();
  122. void               vicar_labels();
  123. void               no_labels();
  124. int                check_host();
  125. int                get_files();
  126. long               swap_long();
  127. void               decompress();
  128. void               decmpinit();
  129. void               free_tree();
  130.  
  131. /* global variables                                                 */
  132.  
  133. int                infile;
  134. FILE               *outfile;
  135. char               inname[80],outname[80];
  136. int                output_format;
  137.  
  138. main(argc,argv)
  139.  
  140. int  argc;
  141. char **argv;
  142.  
  143. {
  144. char          ibuf[2048],obuf[2048];
  145. unsigned char blank=32;
  146. short         host,length,total_bytes,line,i;
  147. long          long_length,hist[511];
  148. int           out_bytes = RECORD_BYTES;
  149. int           count;
  150.  
  151. /*********************************************************************/
  152. /*                                                                   */
  153. /* get host information and input and output files                   */
  154. /*                                                                   */
  155. /*********************************************************************/
  156.  
  157.  
  158.    strcpy(inname,"   ");  
  159.    strcpy(outname,"   ");
  160.    output_format = 0;
  161.  
  162.    if (argc == 1);                     /* prompt user for parameters */
  163.    else if (argc == 2 && (strncmp(argv[1],"help",4) == 0 || 
  164.                           strncmp(argv[1],"HELP",4) == 0 ||
  165.                           strncmp(argv[1],"?",1)    == 0))
  166.      {
  167.       printf("Voyager Image Decompression Program.  Command line format:\n\n");
  168.       printf("CDCOMP [infile] [outfile] [format code]\n");
  169.       printf("   infile        - name of compressed image file.     \n");
  170.       printf("   outfile       - name of uncompressed output file.  \n");
  171.       printf("   output format - selected from the following list:  \n");   
  172.       printf("                                                      \n");   
  173.       printf("     1  SFDU/PDS format [DEFAULT].                    \n");   
  174.       printf("     2  FITS format.                                  \n");   
  175.       printf("     3  VICAR format.                                 \n");   
  176.       printf("     4  Unlabelled binary array.                    \n\n");   
  177.       exit(1);
  178.      }  
  179.    else 
  180.      {
  181.       strcpy(inname,argv[1]);  
  182.       if (argc >= 3) strcpy(outname,argv[2]); 
  183.       if (argc == 3) output_format = 1;
  184.       if (argc == 4) sscanf(argv[3],"%d",&output_format); 
  185.      }
  186.    host = check_host();
  187.    host = get_files(host); /* may change host if VAX */
  188.  
  189. /*********************************************************************/
  190. /*                                                                   */
  191. /* read and edit compressed file labels                              */
  192. /*                                                                   */
  193. /*********************************************************************/
  194.  
  195.    switch (output_format)
  196.      {
  197.        case 1: pds_labels(host);
  198.                break;
  199.        case 2: fits_labels(host);
  200.                break;
  201.        case 3: vicar_labels(host);
  202.                break;
  203.        case 4: no_labels(host);
  204.      }
  205. /*********************************************************************/
  206. /*                                                                   */
  207. /* process the image histogram                                       */
  208. /*                                                                   */
  209. /*********************************************************************/
  210.  
  211. /* need to know record_bytes,hist_count,hist_item_type,item_count.*/
  212.    total_bytes = 0;
  213.    length = read_var((char *)hist,host);
  214.    total_bytes = total_bytes + length;
  215.    length = read_var((char *)hist+836,host);
  216.    total_bytes = total_bytes + length;
  217.  
  218.    if (host == 2 || host == 5)             /* If non-byte swapped    */
  219.      for (i=0;i<256;i++)                   /* host, swap bytes in    */
  220.        hist[i] = swap_long(hist[i]);       /* the output histogram   */
  221.  
  222.    if (output_format == 1)
  223.      {
  224.       fwrite((char *)hist,    836,1,outfile);
  225.       fwrite((char *)hist+836,length,1,outfile);
  226.  
  227.       /*  pad out the histogram to a multiple of RECORD_BYTES */
  228.       for (i=total_bytes;i<RECORD_BYTES*2;i++) fputc(blank,outfile);
  229.      }
  230. /*********************************************************************/
  231. /*                                                                   */
  232. /* process the encoding histogram                                    */
  233. /* don't have to byte-swap because DECOMP.C does it for us           */
  234. /*                                                                   */
  235. /*********************************************************************/
  236.  
  237.    length = read_var((char *)hist,host);
  238.    length = read_var((char *)hist+836,host);
  239.    length = read_var((char *)hist+1672,host);
  240.  
  241. /*********************************************************************/
  242. /*                                                                   */
  243. /* process the engineering summary                                   */
  244. /*                                                                   */
  245. /*********************************************************************/
  246.  
  247.    total_bytes = 0;
  248.    length = read_var(ibuf,host);
  249.  
  250.    if (output_format == 1)
  251.      {
  252.       fwrite(ibuf,length,1,outfile);
  253.       total_bytes = total_bytes + length;
  254.  
  255.       /*  pad out engineering to multiple of 836 */
  256.       for (i=total_bytes;i<RECORD_BYTES;i++) fputc(blank,outfile);
  257.      }
  258. /*********************************************************************/
  259. /*                                                                   */
  260. /* initialize the decompression                                      */
  261. /*                                                                   */
  262. /*********************************************************************/
  263.  
  264.     printf("\nInitializing decompression routine...");
  265.     decmpinit(hist);
  266.  
  267. /*********************************************************************/
  268. /*                                                                   */
  269. /* decompress the image                                              */
  270. /*                                                                   */
  271. /*********************************************************************/
  272.  
  273.     printf("\nDecompressing data...");
  274.     line=0;
  275.     do
  276.       {
  277.        length = read_var(ibuf,host);
  278.        if (length <= 0) break;
  279.        long_length = (long)length;
  280.        line += 1;
  281.        decompress(ibuf, obuf,&long_length, &out_bytes);
  282.        if (output_format == 1)
  283.          {
  284.           count = fwrite(obuf,RECORD_BYTES,1,outfile);
  285.           if (count != 1)
  286.             {
  287.              printf("\nError writing output file.  Aborting program.");
  288.              printf("\nCheck disk space or for duplicate file name on VAX.");
  289.              exit(1);
  290.             }
  291.          }
  292.        else fwrite(obuf,800,1,outfile);
  293.  
  294.        if (line % 100 == 0) printf("\n line %d",line);
  295.       } while (length > 0 && line < 800);
  296.  
  297.  /*  pad out FITS file to a multiple of 2880 */
  298.  if (output_format == 2)
  299.    for (i=0;i<2240;i++) fputc(blank,outfile);
  300.  
  301.  printf("\n");
  302.  free_tree(&long_length);
  303.  close(infile);
  304.  fclose(outfile);
  305. }
  306.  
  307. /*********************************************************************/
  308. /*                                                                   */
  309. /* subroutine get_files - get input filenames and open               */
  310. /*                                                                   */
  311. /*********************************************************************/
  312.  
  313. int get_files(host)
  314. int host;
  315.  
  316. {
  317. short   shortint;
  318.  
  319.   if (inname[0] == ' ')
  320.     {
  321.      printf("\nEnter name of file to be decompressed: ");
  322.      gets (inname);
  323.     }
  324.   if (host == 1 | host == 2)
  325.     {
  326.      if ((infile = open(inname,O_RDONLY | O_BINARY)) <= 0)
  327.        {
  328.         printf("\ncan't open input file: %s\n",inname);
  329.         exit(1);
  330.        }
  331.     }
  332.   else if (host == 3 | host == 5)
  333.     {
  334.      if ((infile = open(inname,O_RDONLY)) <= 0)
  335.        {
  336.         printf("\ncan't open input file: %s\n",inname);
  337.         exit(1);
  338.        }
  339.  
  340.   /****************************************************************/
  341.   /* If we are on a vax see if the file is in var length format.  */
  342.   /* This logic is in here in case the vax file has been stored   */
  343.   /* in fixed or undefined format.  This might be necessary since */
  344.   /* vax variable length files can't be moved to other computer   */
  345.   /* systems with standard comm programs (kermit, for example).   */
  346.   /****************************************************************/
  347.  
  348.      if (host == 3)
  349.        {
  350.         read(infile,&shortint,2);
  351.         if (shortint > 0 && shortint < 80)
  352.           {
  353.            host = 4;              /* change host to 4                */
  354.            printf("This is not a VAX variable length file.");
  355.           }
  356.         else printf("This is a VAX variable length file.");
  357.         lseek(infile,0,0);        /* reposition to beginning of file */
  358.        }
  359.     }
  360.  
  361.   if (output_format == 0)
  362.   do
  363.     {
  364.      printf("\nEnter a number for the output format desired:\n");
  365.      printf("\n  1.  SFDU/PDS format.");
  366.      printf("\n  2.  FITS format.");
  367.      printf("\n  3.  VICAR format.");
  368.      printf("\n  4.  Unlabelled binary array.\n");
  369.      printf("\n  Enter format number:");
  370.      gets(inname);
  371.      output_format = atoi(inname);
  372.     } while (output_format < 1 || output_format > 4);
  373.  
  374.   if (outname[0] == ' ')
  375.     {
  376.      printf("\nEnter name of uncompressed output file: ");
  377.      gets (outname);
  378.     }
  379.  
  380.   if (host == 1 || host == 2 || host == 5)
  381.     {
  382.      if ((outfile = fopen(outname,"wb"))==NULL)
  383.        {
  384.         printf("\ncan't open output file: %s\n",outname);
  385.         exit(1);
  386.        }
  387.     }
  388.   else if (host == 3 || host == 4)
  389.     {
  390.      if (output_format == 1)      /* write PDS format blocks */
  391.        {
  392.         if ((outfile=fopen(outname,"w",
  393.                            "mrs=836",FOP,CTX,RECORD_TYPE))==NULL)
  394.          {
  395.           printf("\ncan't open output file: %s\n",outname);
  396.           exit(1);
  397.          }
  398.        }
  399.      else if (output_format == 2) /* write FITS format blocks */
  400.        {
  401.         if ((outfile=fopen(outname,"w",
  402.                            "mrs=2880",FOP,CTX,RECORD_TYPE))==NULL)
  403.          {
  404.           printf("\ncan't open output file: %s\n",outname);
  405.           exit(1);
  406.          }
  407.        }
  408.      else                         /* write 800 byte records */
  409.        {
  410.         if ((outfile=fopen(outname,"w",
  411.                            "mrs=800",FOP,CTX,RECORD_TYPE))==NULL)
  412.          {
  413.           printf("\ncan't open output file: %s\n",outname);
  414.           exit(1);
  415.          }
  416.        }
  417.     }
  418.  
  419.   return(host);  /* In case its been updated */
  420. }
  421.  
  422. /*********************************************************************/
  423. /*                                                                   */
  424. /* subroutine pds_labels - edit PDS labels and write to output file  */
  425. /*                                                                   */
  426. /*********************************************************************/
  427.  
  428. void pds_labels(host)
  429. int host;
  430.  
  431. {
  432. char          outstring[80],ibuf[2048];
  433. unsigned char cr=13,lf=10,blank=32;
  434. short         length,nlen,total_bytes,line,i;
  435.  
  436.  
  437. total_bytes = 0;
  438. do
  439.   {
  440.    length = read_var(ibuf,host);
  441.    ibuf[length]=NULL;
  442.  
  443.   /******************************************************************/
  444.   /*  edit labels which need to be changed                          */
  445.   /******************************************************************/
  446.  
  447.    if      ((i = strncmp(ibuf,"NJPL1I00PDS1",12)) == 0)
  448.    /*****************************************************************/
  449.    /* add the output file length to the sfdu label                  */
  450.    /*****************************************************************/
  451.      {
  452.       strcpy(outstring,ibuf);
  453.       strcpy(outstring+12,"00673796");
  454.       strcpy(outstring+20,ibuf+20);
  455.       fwrite(outstring,length,1,outfile);
  456.       fprintf(outfile,"%c%c",cr,lf);
  457.       total_bytes = total_bytes + length + 2;
  458.      }
  459.    else if ((i = strncmp(ibuf,"RECORD_TYPE",11)) == 0)
  460.    /*****************************************************************/
  461.    /* change the record_type value from variable to fixed           */
  462.    /*****************************************************************/
  463.      {
  464.       strcpy(ibuf+35,"FIXED_LENGTH");
  465.       length = length - 3;
  466.       fwrite(ibuf,length,1,outfile);
  467.       fprintf(outfile,"%c%c",cr,lf);
  468.       total_bytes = total_bytes + length + 2;
  469.      }
  470.    else if ((i = strncmp(ibuf,"FILE_RECORDS",12)) == 0)
  471.    /*****************************************************************/
  472.    /* change the file_records count to 806                          */
  473.    /*****************************************************************/
  474.      {
  475.       strcpy(ibuf+35,"806");
  476.       fwrite(ibuf,length,1,outfile);
  477.       fprintf(outfile,"%c%c",cr,lf);
  478.       total_bytes = total_bytes + length + 2;
  479.      }
  480.    else if ((i = strncmp(ibuf,"LABEL_RECORDS",13)) == 0)
  481.    /*****************************************************************/
  482.   /* change the label_records count from 56 to 3                    */
  483.    /*****************************************************************/
  484.      {
  485.       strcpy(ibuf+35,"3");
  486.       length -= 1;
  487.       fwrite(ibuf,length,1,outfile);
  488.       fprintf(outfile,"%c%c",cr,lf);
  489.       total_bytes = total_bytes + length + 2;
  490.      }
  491.    else if ((i = strncmp(ibuf,"^IMAGE_HISTOGRAM",16)) == 0)
  492.    /*****************************************************************/
  493.    /* change the location pointer of image_histogram to record 4    */
  494.    /*****************************************************************/
  495.      {
  496.       strcpy(ibuf+35,"4");
  497.       length -= 1;
  498.       fwrite(ibuf,length,1,outfile);
  499.       fprintf(outfile,"%c%c",cr,lf);
  500.       total_bytes = total_bytes + length + 2;
  501.      }
  502.    else if ((i = strncmp(ibuf,"^ENCODING_HISTOGRAM)",19)) == 0);
  503.    /*****************************************************************/
  504.    /* delete the encoding_histogram location pointer                */
  505.    /*****************************************************************/
  506.    else if ((i = strncmp(ibuf,"^ENGINEERING_TABLE",18)) == 0)
  507.    /*****************************************************************/
  508.    /* change the location pointer of engineering_summary to record 6*/
  509.    /*****************************************************************/
  510.      {
  511.       strcpy(ibuf+35,"6");
  512.       length -= 1;
  513.       fwrite(ibuf,length,1,outfile);
  514.       fprintf(outfile,"%c%c",cr,lf);
  515.       total_bytes = total_bytes + length + 2;
  516.      }
  517.    else if ((i = strncmp(ibuf,"^IMAGE",6)) == 0)
  518.    /*****************************************************************/
  519.    /* change the location pointer of image to record 7              */
  520.    /*****************************************************************/
  521.      {
  522.       strcpy(ibuf+35,"7");
  523.       length = length -1;
  524.       fwrite(ibuf,length,1,outfile);
  525.       fprintf(outfile,"%c%c",cr,lf);
  526.       total_bytes = total_bytes + length + 2;
  527.      }
  528.    else if ((i = strncmp(ibuf,
  529.              "OBJECT                           = ENCODING",43)) == 0)
  530.    /*****************************************************************/
  531.    /* delete the 4 encoding histogram labels                        */
  532.    /*****************************************************************/
  533.      {
  534.       for (i=0;i<4;i++)   /* ignore these labels */
  535.         {
  536.          length = read_var(ibuf,host);
  537.         }
  538.      }
  539.    else if ((i = strncmp(ibuf," ENCODING",9)) == 0);
  540.    /*****************************************************************/
  541.    /* delete the encoding type label in the image object            */
  542.    /*****************************************************************/
  543.    else if ((host == 2 || host == 5) && (i = strncmp(ibuf,
  544.              " ITEM_TYPE                       = VAX_INTEGER",46)) == 0)
  545.    /*****************************************************************/
  546.    /* change the record_type value from variable to fixed           */
  547.    /*****************************************************************/
  548.      {
  549.       strcpy(ibuf+35,"INTEGER");
  550.       length = length - 4;
  551.       fwrite(ibuf,length,1,outfile);
  552.       fprintf(outfile,"%c%c",cr,lf);
  553.       total_bytes = total_bytes + length + 2;
  554.      }
  555.  
  556.  
  557.  
  558.    /*****************************************************************/
  559.    /* if none of above write out the label to the output file       */
  560.    /*****************************************************************/
  561.    else
  562.      {
  563.       fwrite(ibuf,length,1,outfile);
  564.       fprintf(outfile,"%c%c",cr,lf);
  565.       total_bytes = total_bytes + length + 2;
  566.      }
  567.    /*****************************************************************/
  568.    /* test for the end of the PDS labels                            */
  569.    /*****************************************************************/
  570.    if ((i = strncmp(ibuf,"END",3)) == 0 && length == 3) break;
  571.   } while (length > 0);
  572.  
  573. /* pad out the labels with blanks to multiple of RECORD_BYTES */
  574.    for (i=total_bytes;i<RECORD_BYTES*3;i++) fputc(blank,outfile);
  575. }
  576.  
  577. /*********************************************************************/
  578. /*                                                                   */
  579. /* subroutine fits_labels - write FITS header to output file */
  580. /*                                                                   */
  581. /*********************************************************************/
  582.  
  583. void fits_labels(host)
  584. int host;
  585.  
  586. {
  587. char          ibuf[2048],outstring[80];
  588. unsigned char cr=13,lf=10,blank=32;
  589. short         length,nlen,total_bytes,line,i;
  590.  
  591. do
  592.   {
  593.    length = read_var(ibuf,host);
  594.    /*****************************************************************/
  595.    /* read to the end of the PDS labels                             */
  596.    /*****************************************************************/
  597.    if ((i = strncmp(ibuf,"END",3)) == 0 && length == 3) break;
  598.   } while (length > 0);
  599.  
  600. total_bytes = 0;
  601.  
  602. strcpy(outstring,
  603. "SIMPLE  =                    T                                                ");
  604. fwrite(outstring,78,1,outfile);
  605. fprintf(outfile,"%c%c",cr,lf);
  606. total_bytes = total_bytes + 80;
  607.  
  608. strcpy(outstring,
  609. "BITPIX  =                    8                                                ");
  610. fwrite(outstring,78,1,outfile);
  611. fprintf(outfile,"%c%c",cr,lf);
  612. total_bytes = total_bytes + 80;
  613.  
  614. strcpy(outstring,
  615. "NAXIS   =                    2                                                ");
  616. fwrite(outstring,78,1,outfile);
  617. fprintf(outfile,"%c%c",cr,lf);
  618. total_bytes = total_bytes + 80;
  619.  
  620. strcpy(outstring,
  621. "NAXIS1  =                  800                                                ");
  622. fwrite(outstring,78,1,outfile);
  623. fprintf(outfile,"%c%c",cr,lf);
  624. total_bytes = total_bytes + 80;
  625.  
  626. strcpy(outstring,
  627. "NAXIS2  =                  800                                                ");
  628. fwrite(outstring,78,1,outfile);
  629. fprintf(outfile,"%c%c",cr,lf);
  630. total_bytes = total_bytes + 80;
  631.  
  632. strcpy(outstring,
  633. "END                                                                           ");
  634. fwrite(outstring,78,1,outfile);
  635. fprintf(outfile,"%c%c",cr,lf);
  636. total_bytes = total_bytes + 80;
  637.  
  638. /* pad out the labels with blanks to multiple of RECORD_BYTES */
  639.    for (i=total_bytes;i<2880;i++) fputc(blank,outfile);
  640. }
  641.  
  642. /*********************************************************************/
  643. /*                                                                   */
  644. /* subroutine vicar_labels - write vicar labels to output file       */
  645. /*                                                                   */
  646. /*********************************************************************/
  647.  
  648. void vicar_labels(host)
  649. int host;
  650.  
  651. {
  652. char          ibuf[2048],outstring[80];
  653. unsigned char cr=13,lf=10,blank=32;
  654. short         length,nlen,total_bytes,line,i;
  655.  
  656. do
  657.   {
  658.    length = read_var(ibuf,host);
  659.    /*****************************************************************/
  660.    /* read to the end of the PDS labels                             */
  661.    /*****************************************************************/
  662.    if ((i = strncmp(ibuf,"END",3)) == 0 && length == 3) break;
  663.   } while (length > 0);
  664.  
  665. total_bytes = 0;
  666.  
  667. strcpy(outstring,
  668. "LBLSIZE=800             FORMAT='BYTE'  TYPE='IMAGE'  BUFSIZ=800  DIM=2  ");
  669. fwrite(outstring,72,1,outfile);
  670. total_bytes = total_bytes + 72;
  671. strcpy(outstring,
  672. "EOL=0  RECSIZE=800  ORG='BSQ'  NL=800  NS=800  NB=1  N1=0  N2=0  N3=0  ");
  673. total_bytes = total_bytes + 71;
  674. fwrite(outstring,71,1,outfile);
  675. strcpy(outstring,
  676. "N4=0  NBB=0  NLB=0");
  677. fwrite(outstring,18,1,outfile);
  678. fprintf(outfile,"%c%c",cr,lf);
  679. total_bytes = total_bytes + 20;
  680.  
  681. /* pad out the labels with blanks to multiple of RECORD_BYTES */
  682.    for (i=total_bytes;i<800;i++) fputc(blank,outfile);
  683. }
  684.  
  685. /*********************************************************************/
  686. /*                                                                   */
  687. /* subroutine no_labels - read past the pds labels                   */
  688. /*                                                                   */
  689. /*********************************************************************/
  690.  
  691. void no_labels(host)
  692. int host;
  693.  
  694. {
  695. char          ibuf[2048],outstring[80];
  696. unsigned char cr=13,lf=10,blank=32;
  697. short         length,nlen,total_bytes,line,i;
  698.  
  699. do
  700.   {
  701.    length = read_var(ibuf,host);
  702.    /*****************************************************************/
  703.    /* read to the end of the PDS labels                             */
  704.    /*****************************************************************/
  705.    if ((i = strncmp(ibuf,"END",3)) == 0 && length == 3) break;
  706.   } while (length > 0);
  707.  
  708. }
  709.  
  710.  
  711. /*********************************************************************/
  712. /*                                                                   */
  713. /* subroutine read_var - read variable length records from input file*/
  714. /*                                                                   */
  715. /*********************************************************************/
  716.  
  717. read_var(ibuf,host)
  718. char  *ibuf;
  719. int   host;
  720. {
  721. int   length,result,nlen;
  722. char  temp;
  723. union /* this union is used to swap 16 and 32 bit integers          */
  724.   {
  725.    char  ichar[4];
  726.    short slen;
  727.    long  llen;
  728.   } onion;
  729.  
  730.   switch (host)
  731.     {
  732.      case 1: /*******************************************************/
  733.              /* IBM PC host                                         */
  734.              /*******************************************************/
  735.        length = 0;
  736.        result = read(infile,&length,2);
  737.        nlen =   read(infile,ibuf,length+(length%2));
  738.        return (length);
  739.        break;
  740.  
  741.      case 2: /*******************************************************/
  742.              /* Macintosh host                                      */
  743.              /*******************************************************/
  744.        length = 0;
  745.        result = read(infile,onion.ichar,2);
  746.        /*     byte swap the length field                            */
  747.        temp   = onion.ichar[0];
  748.        onion.ichar[0]=onion.ichar[1];
  749.        onion.ichar[1]=temp;
  750.        length = onion.slen;       /* left out of earlier versions   */
  751.        /* printf("length=%04x,result=%d\n",length,result);          */
  752.        nlen =   read(infile,ibuf,length+(length%2));
  753.        return (length);
  754.        break;
  755.  
  756.      case 3: /*******************************************************/
  757.              /* VAX host with variable length support               */
  758.              /*******************************************************/
  759.        length = read(infile,ibuf,RECORD_BYTES);
  760.        return (length);
  761.  
  762.      case 4: /*******************************************************/
  763.              /* VAX host, but not a variable length file            */
  764.              /*******************************************************/
  765.        length = 0;
  766.        result = read(infile,&length,2);
  767.        nlen =   read(infile,ibuf,length+(length%2));
  768.  
  769.        /* check to see if we crossed a vax record boundary          */
  770.        while (nlen < length)
  771.          nlen += read(infile,ibuf+nlen,length+(length%2)-nlen);
  772.        return (length);
  773.        break;
  774.  
  775.      case 5: /*******************************************************/
  776.              /* Unix workstation host (non-byte-swapped 32 bit host)*/
  777.              /*******************************************************/
  778.        length = 0;
  779.        result = read(infile,onion.ichar,2);
  780.        /*     byte swap the length field                            */
  781.        temp   = onion.ichar[0];
  782.        onion.ichar[0]=onion.ichar[1];
  783.        onion.ichar[1]=temp;
  784.        length = onion.slen;
  785.        /* printf("length=%04x,result=%d\n",length,result); */
  786.        nlen =   read(infile,ibuf,length+(length%2));
  787.        return (length);
  788.        break;
  789.     }
  790. }
  791.  
  792. /*********************************************************************/
  793. /*                                                                   */
  794. /* subroutine check_host - find out what kind of machine we are on   */
  795. /*                                                                   */
  796. /*********************************************************************/
  797.  
  798. int check_host()
  799. {
  800. /*  This subroutine checks the attributes of the host computer and
  801.     returns a host code number.
  802. */
  803. char hostname[80];
  804.  
  805. int swap,host,bits,var;
  806. union
  807.   {
  808.    char  ichar[2];
  809.    short ilen;
  810.   } onion;
  811.  
  812. if (sizeof(var) == 4) bits = 32;
  813.                  else bits = 16;
  814.  
  815. onion.ichar[0] = 1;
  816. onion.ichar[1] = 0;
  817.  
  818. if (onion.ilen == 1) swap = TRUE;
  819. else                 swap = FALSE;
  820.  
  821. if (bits == 16 && swap == TRUE)
  822.   {
  823.    host = 1; /* IBM PC host  */
  824.    strcpy(hostname,
  825.           "Host 1 - 16 bit integers with swapping, no var len support.");
  826.   }
  827.  
  828. if (bits == 16 && swap == FALSE)
  829.   {
  830.    host = 2; /* Non byte swapped 16 bit host  */
  831.    strcpy(hostname,
  832.           "Host 2 - 16 bit integers without swapping, no var len support.");
  833.   }
  834.  
  835. if (bits == 32 && swap == TRUE)
  836.  { host = 3; /* VAX host with var length support */
  837.    strcpy(hostname,
  838.           "Host 3,4 - 32 bit integers with swapping.");
  839.  }
  840.  
  841. if (bits == 32 && swap == FALSE)
  842.   {
  843.    host = 5; /* OTHER 32-bit host  */
  844.    strcpy(hostname,
  845.           "Host 5 - 32 bit integers without swapping, no var len support.");
  846.   }
  847.  
  848. printf("%s\n",hostname);
  849. return(host);
  850. }
  851.  
  852. long swap_long(inval)  /* swap 4 byte integer                       */
  853. long inval;
  854. {
  855. union /* this union is used to swap 16 and 32 bit integers          */
  856.   {
  857.    char  ichar[4];
  858.    short slen;
  859.    long  llen;
  860.   } onion;
  861.   char   temp;
  862.  
  863.   /* byte swap the input field                                      */
  864.   onion.llen   = inval;
  865.   temp   = onion.ichar[0];
  866.   onion.ichar[0]=onion.ichar[3];
  867.   onion.ichar[3]=temp;
  868.   temp   = onion.ichar[1];
  869.   onion.ichar[1]=onion.ichar[2];
  870.   onion.ichar[2]=temp;
  871.   return (onion.llen);
  872. }
  873.  
  874.  void decompress(ibuf,obuf,nin,nout)
  875. /****************************************************************************
  876. *_TITLE decompress - decompresses image lines stored in compressed format   *
  877. *_ARGS  TYPE       NAME      I/O        DESCRIPTION                         */
  878.         char       *ibuf;  /* I         Compressed data buffer              */
  879.         char       *obuf;  /* O         Decompressed image line             */
  880.         long int   *nin;   /* I         Number of bytes on input buffer     */
  881.         long int   *nout;  /* I         Number of bytes in output buffer    */
  882.  
  883.   {
  884.  /* The external root pointer to tree */
  885.     extern NODE *tree;
  886.  
  887.  /* Declare functions called from this routine */
  888.     void dcmprs();
  889.  
  890. /*************************************************************************
  891.   This routine is fairly simple as it's only function is to call the
  892.   routine dcmprs.
  893. **************************************************************************/
  894.  
  895.     dcmprs(ibuf,obuf,nin,nout,tree);
  896.  
  897.     return;
  898.   }
  899.  
  900. void decmpinit(hist)
  901. /***************************************************************************
  902. *_TITLE decmpinit - initializes the Huffman tree                           *
  903. *_ARGS  TYPE       NAME      I/O        DESCRIPTION                        */
  904.         long int   *hist;  /* I         First-difference histogram.        */
  905.  
  906. {
  907.   extern NODE *tree;          /* Huffman tree root pointer */
  908.  
  909.   /* Specify the calling function to initialize the tree */
  910.   NODE *huff_tree();
  911.  
  912. /****************************************************************************
  913.   Simply call the huff_tree routine and return.
  914. *****************************************************************************/
  915.  
  916.   tree = huff_tree(hist);
  917.  
  918.   return;
  919.  }
  920.  
  921. NODE *huff_tree(hist)
  922. /****************************************************************************
  923. *_TITLE huff_tree - constructs the Huffman tree; returns pointer to root    *
  924. *_ARGS  TYPE          NAME        I/O   DESCRIPTION                         */
  925.         long int     *hist;     /* I    First difference histogram          */
  926.  
  927.   {
  928.   /*  Local variables used */
  929.     long int freq_list[512];      /* Histogram frequency list */
  930.     NODE **node_list;             /* DN pointer array list */
  931.  
  932.     register long int *fp;        /* Frequency list pointer */
  933.     register NODE **np;           /* Node list pointer */
  934.  
  935.     register long int num_freq;   /* Number non-zero frequencies in histogram */
  936.     long int sum;                 /* Sum of all frequencies */
  937.  
  938.     register short int num_nodes; /* Counter for DN initialization */
  939.     register short int cnt;       /* Miscellaneous counter */
  940.  
  941.     short int znull = -1;         /* Null node value */
  942.  
  943.     register NODE *temp;          /* Temporary node pointer */
  944.  
  945.   /* Functions called */
  946.     void sort_freq();
  947.     NODE *new_node();
  948.     char *malloc();
  949.  
  950. /***************************************************************************
  951.   Allocate the array of nodes from memory and initialize these with numbers
  952.   corresponding with the frequency list.  There are only 511 possible
  953.   permutations of first difference histograms.  There are 512 allocated
  954.   here to adhere to the FORTRAN version.
  955. ****************************************************************************/
  956.  
  957.    fp = freq_list;
  958.    node_list = (NODE **) malloc(sizeof(temp)*512);
  959.    if (node_list == NULL)
  960.     {
  961.       printf("\nOut of memory in huff_tree!\n");
  962.       exit(1);
  963.     }
  964.    np = node_list;
  965.  
  966.    for (num_nodes=1, cnt=512 ; cnt-- ; num_nodes++)
  967.      {
  968. /**************************************************************************
  969.     The following code has been added to standardize the VAX byte order
  970.     for the "long int" type.  This code is intended to make the routine
  971.     as machine independant as possible.
  972. ***************************************************************************/
  973.         unsigned char *cp = (unsigned char *) hist++;
  974.         unsigned long int j;
  975.         short int i;
  976.         for (i=4 ; --i >= 0 ; j = (j << 8) | *(cp+i));
  977.  
  978. /* Now make the assignment */
  979.         *fp++ = j;
  980.         temp = new_node(num_nodes);
  981.         *np++ = temp;
  982.      }
  983.  
  984.      (*--fp) = 0;         /* Ensure the last element is zeroed out.  */
  985.  
  986. /***************************************************************************
  987.   Now, sort the frequency list and eliminate all frequencies of zero.
  988. ****************************************************************************/
  989.  
  990.   num_freq = 512;
  991.   sort_freq(freq_list,node_list,num_freq);
  992.  
  993.   fp = freq_list;
  994.   np = node_list;
  995.  
  996.   for (num_freq=512 ; (*fp) == 0 && (num_freq) ; fp++, np++, num_freq--);
  997.  
  998.  
  999. /***************************************************************************
  1000.   Now create the tree.  Note that if there is only one difference value,
  1001.   it is returned as the root.  On each interation, a new node is created
  1002.   and the least frequently occurring difference is assigned to the right
  1003.   pointer and the next least frequency to the left pointer.  The node
  1004.   assigned to the left pointer now becomes the combination of the two
  1005.   nodes and it's frequency is the sum of the two combining nodes.
  1006. ****************************************************************************/
  1007.  
  1008.   for (temp=(*np) ; (num_freq--) > 1 ; )
  1009.     {
  1010.         temp = new_node(znull);
  1011.         temp->right = (*np++);
  1012.         temp->left = (*np);
  1013.         *np = temp;
  1014.         *(fp+1) = *(fp+1) + *fp;
  1015.         *fp++ = 0;
  1016.         sort_freq(fp,np,num_freq);
  1017.     }
  1018.  
  1019.   return temp;
  1020.  }
  1021.  
  1022. NODE *new_node(value)
  1023. /****************************************************************************
  1024. *_TITLE new_node - allocates a NODE structure and returns a pointer to it   *
  1025. *_ARGS  TYPE        NAME        I/O     DESCRIPTION                         */
  1026.         short int   value;    /* I      Value to assign to DN field         */
  1027.  
  1028.   {
  1029.     NODE *temp;         /* Pointer to the memory block */
  1030.  
  1031.   char *malloc();       /* Memory allocation function */
  1032.  
  1033. /***************************************************************************
  1034.   Allocate the memory and intialize the fields.
  1035. ****************************************************************************/
  1036.  
  1037.   temp = (NODE *) malloc(sizeof(NODE));
  1038.  
  1039.   if (temp != NULL)
  1040.     {
  1041.       temp->right = NULL;
  1042.       temp->dn = value;
  1043.       temp->left = NULL;
  1044.     }
  1045.   else
  1046.     {
  1047.        printf("\nOut of memory in new_node!\n");
  1048.        exit(1);
  1049.     }
  1050.  
  1051.    return temp;
  1052.   }
  1053.  
  1054.  void sort_freq(freq_list,node_list,num_freq)
  1055. /****************************************************************************
  1056. *_TITLE sort_freq - sorts frequency and node lists in increasing freq. order*
  1057. *_ARGS  TYPE       NAME            I/O  DESCRIPTION                         */
  1058.         long int   *freq_list;   /* I   Pointer to frequency list           */
  1059.         NODE       **node_list;  /* I   Pointer to array of node pointers   */
  1060.         long int   num_freq;     /* I   Number of values in freq list       */
  1061.  
  1062.   {
  1063.     /* Local Variables */
  1064.     register long int *i;       /* primary pointer into freq_list */
  1065.     register long int *j;       /* secondary pointer into freq_list */
  1066.  
  1067.     register NODE **k;          /* primary pointer to node_list */
  1068.     register NODE **l;          /* secondary pointer into node_list */
  1069.  
  1070.     long int temp1;             /* temporary storage for freq_list */
  1071.     NODE *temp2;                /* temporary storage for node_list */
  1072.  
  1073.     register long int cnt;      /* count of list elements */
  1074.  
  1075.  
  1076. /************************************************************************
  1077.   Save the current element - starting with the second - in temporary
  1078.   storage.  Compare with all elements in first part of list moving
  1079.   each up one element until the element is larger.  Insert current
  1080.   element at this point in list.
  1081. *************************************************************************/
  1082.  
  1083.    if (num_freq <= 0) return;      /* If no elements or invalid, return */
  1084.  
  1085.    for (i=freq_list, k=node_list, cnt=num_freq ; --cnt ; *j=temp1, *l=temp2)
  1086.      {
  1087.         temp1 = *(++i);
  1088.         temp2 = *(++k);
  1089.  
  1090.         for (j = i, l = k ;  *(j-1) > temp1 ; )
  1091.           {
  1092.             *j = *(j-1);
  1093.             *l = *(l-1);
  1094.             j--;
  1095.             l--;
  1096.             if ( j <= freq_list) break;
  1097.           }
  1098.  
  1099.      }
  1100.   return;
  1101.   }
  1102.  
  1103.  void dcmprs(ibuf,obuf,nin,nout,root)
  1104. /****************************************************************************
  1105. *_TITLE dcmprs - decompresses Huffman coded compressed image lines          *
  1106. *_ARGS  TYPE       NAME       I/O       DESCRIPTION                         */
  1107.         char       *ibuf;   /* I        Compressed data buffer              */
  1108.         char       *obuf;   /* O        Decompressed image line             */
  1109.         long int   *nin;    /* I        Number of bytes on input buffer     */
  1110.         long int   *nout;   /* I        Number of bytes in output buffer    */
  1111.         NODE       *root;   /* I        Huffman coded tree                  */
  1112.  
  1113.   {
  1114.     /* Local Variables */
  1115.     register NODE *ptr = root;        /* pointer to position in tree */
  1116.     register unsigned char test;      /* test byte for bit set */
  1117.     register unsigned char idn;       /* input compressed byte */
  1118.  
  1119.     register char odn;                /* last dn value decompressed */
  1120.  
  1121.     char *ilim = ibuf + *nin;         /* end of compressed bytes */
  1122.     char *olim = obuf + *nout;        /* end of output buffer */
  1123.  
  1124. /**************************************************************************
  1125.   Check for valid input values for nin, nout and make initial assignments.
  1126. ***************************************************************************/
  1127.  
  1128.     if (ilim > ibuf && olim > obuf)
  1129.        odn = *obuf++ = *ibuf++;
  1130.     else
  1131.        {
  1132.            printf("\nInvalid byte count in dcmprs!\n");
  1133.            exit(1);
  1134.        }
  1135.  
  1136. /**************************************************************************
  1137.   Decompress the input buffer.  Assign the first byte to the working
  1138.   variable, idn.  An arithmatic and (&) is performed using the variable
  1139.   'test' that is bit shifted to the right.  If the result is 0, then
  1140.   go to right else go to left.
  1141. ***************************************************************************/
  1142.  
  1143.     for (idn=(*ibuf) ; ibuf < ilim  ; idn =(*++ibuf))
  1144.      {
  1145.         for (test=0x80 ; test ; test >>= 1)
  1146.            {
  1147.             ptr = (test & idn) ? ptr->left : ptr->right;
  1148.  
  1149.             if (ptr->dn != -1)
  1150.               {
  1151.                 if (obuf >= olim) return;
  1152.                 odn -= ptr->dn + 256;
  1153.                 *obuf++ = odn;
  1154.                 ptr = root;
  1155.               }
  1156.           }
  1157.      }
  1158.    return;
  1159.   }
  1160.  
  1161.  
  1162. void free_tree(nfreed)
  1163. /****************************************************************************
  1164. *_TITLE free_tree - free memory of all allocated nodes                      *
  1165. *_ARGS  TYPE       NAME       I/O        DESCRIPTION                        */
  1166.         long int   *nfreed;  /* O        Return of total count of nodes     *
  1167. *                                        freed.                             */
  1168.  
  1169. /*
  1170. *_DESCR This routine is supplied to the programmer to free up all the       *
  1171. *       allocated memory required to build the huffman tree.  The count     *
  1172. *       of the nodes freed is returned in the parameter 'nfreed'.  The      *
  1173. *       purpose of the routine is so if the user wishes to decompress more  *
  1174. *       than one file per run, the program will not keep allocating new     *
  1175. *       memory without first deallocating all previous nodes associated     *
  1176. *       with the previous file decompression.                               *
  1177.  
  1178. *_HIST  16-AUG-89 Kris Becker   USGS, Flagstaff Original Version            *
  1179. *_END                                                                       *
  1180. ****************************************************************************/
  1181.  
  1182. {
  1183.     long int total_free = 0;
  1184.  
  1185.     extern NODE *tree;      /* Huffman tree root pointer */
  1186.  
  1187. /* Specify the function to free the tree */
  1188.     long int free_node();
  1189. /****************************************************************************
  1190.   Simply call the free_node routine and return the result.
  1191. *****************************************************************************/
  1192.  
  1193.     *nfreed = free_node(tree,total_free);
  1194.  
  1195.     return;
  1196. }
  1197.  
  1198. long int free_node(pnode,total_free)
  1199. /***************************************************************************
  1200. *_TITLE free_node - deallocates an allocated NODE pointer
  1201. *_ARGS  TYPE     NAME          I/O   DESCRIPTION                           */
  1202.         NODE     *pnode;       /* I  Pointer to node to free               */
  1203.         long int total_free;   /* I  Total number of freed nodes           */
  1204.  
  1205. /*
  1206. *_DESCR  free_node will check both right and left pointers of a node       *
  1207. *        and then free the current node using the free() C utility.        *
  1208. *        Note that all nodes attached to the node via right or left        *
  1209. *        pointers area also freed, so be sure that this is the desired     *
  1210. *        result when calling this routine.                                 *
  1211.  
  1212. *        This routine is supplied to allow successive calls to the         *
  1213. *        decmpinit routine.  It will free up the memory allocated          *
  1214. *        by previous calls to the decmpinit routine.  The call to free     *
  1215. *        a previous huffman tree is:  total = free_node(tree,(long) 0);    *
  1216. *        This call must be done by the programmer application routine      *
  1217. *        and is not done by any of these routines.                         *
  1218. *_HIST   16-AUG-89  Kris Becker U.S.G.S  Flagstaff Original Version        */
  1219. {
  1220.     if (pnode == (NODE *) NULL) return(total_free);
  1221.     
  1222.     if (pnode->right != (NODE *) NULL)
  1223.         total_free = free_node(pnode->right,total_free);
  1224.     if (pnode->left != (NODE *) NULL)
  1225.         total_free = free_node(pnode->left,total_free);
  1226.  
  1227.     free((char *) pnode);
  1228.     return(total_free + 1);
  1229. }
  1230.