/***********************************************************************/ /* decompress - decompresses image lines stored in compressed format */ /***********************************************************************/ void decompress( byte *ibuf, /* Compressed data buffer */ byte *obuf, /* Decompressed image line */ int64_t *nin, /* Number of bytes on input buffer */ int64_t *nout /* Number of bytes in output buffer */ ) { /* The external root pointer to tree */ extern huff_tree_node_t *tree; dcmprs(ibuf,obuf,nin,nout,tree); return; } /**************************************************/ /* huff_tree_free - free memory of all allocated huff_tree_nodes */ /**************************************************/ void huff_tree_free( int64_t *nfreed /* (Out) Total count of huff_tree_nodes freed */ ) /* This routine is supplied to the programmer to free up all the allocated memory required to build the huffman tree. The count of the huff_tree_nodes freed is returned in the parameter 'nfreed'. The purpose of the routine is so if the user wishes to decompress more than one file per run, the program will not keep allocating new memory without first deallocating all previous huff_tree_nodes associated with the previous file decompression. 16-AUG-89 Kris Becker USGS, Flagstaff Original Version */ { int64_t total_free = 0; extern huff_tree_node_t *tree; /* Huffman tree root pointer */ /* Specify the function to free the tree */ *nfreed = free_huff_tree_node(tree,total_free); return; } /*****************************************************/ /* free_huff_tree_node - deallocates an allocated huff_tree_node_t pointer */ /*****************************************************/ int64_t free_huff_tree_node( huff_tree_node_t *pnode, /* Pointer to huff_tree_node to free */ int64_t total_free /* Total number of freed huff_tree_nodes */ ) /* free_huff_tree_node will check both right and left pointers of a huff_tree_node and then free the current huff_tree_node using the free() C utility. Note that all huff_tree_nodes attached to the huff_tree_node via right or left pointers area also freed, so be sure that this is the desired result when calling this routine. This routine is supplied to allow successive calls to the huff_tree_build routine. It will free up the memory allocated by previous calls to the {huff_tree_build} routine. The call to free a previous huffman tree is: total = free_huff_tree_node(tree,(long) 0); This call must be done by the programmer application routine and is not done by any of these routines. 16-AUG-89 Kris Becker U.S.G.S Flagstaff Original Version */ { if (pnode == (huff_tree_node_t *) NULL) return(total_free); if (pnode->right != (huff_tree_node_t *) NULL) total_free = free_huff_tree_node(pnode->right,total_free); if (pnode->left != (huff_tree_node_t *) NULL) total_free = free_huff_tree_node(pnode->left,total_free); free((char *) pnode); return(total_free + 1); } void printusage(void) { fprintf(stderr, "%s version %s, usage:\n%s\n", PROG_NAME, PROG_VERS, PROG_HELP); } /***********************************************************************/ /* decompress - decompresses image lines stored in compressed format */ /***********************************************************************/ void decompress( byte *ibuf, /* Compressed data buffer */ byte *obuf, /* Decompressed image line */ int64_t *nin, /* Number of bytes on input buffer */ int64_t *nout /* Number of bytes in output buffer */ ) { /* The external root pointer to tree */ extern huff_tree_node_t *tree; dcmprs(ibuf,obuf,nin,nout,tree); return; } /**********************************/ /* openread - open file for input */ /**********************************/ FILE *openread(char *inname) { FILE *f = fopen(inname, "r"); if (f == NULL) { fprintf(stderr, "can't open input file: %s\n", inname); exit(1); } return f; } /*************************************/ /* swap_long - Swap 4 byte integer. */ /*************************************/ long swap_long(long inval) { union /* this union is used to swap 16 and 32 bit integers. */ { byte ichar[4]; short slen; long llen; } onion; byte temp; /* Byte swap the input field: */ onion.llen = inval; temp = onion.ichar[0]; onion.ichar[0] = onion.ichar[3]; onion.ichar[3] = temp; temp = onion.ichar[1]; onion.ichar[1] = onion.ichar[2]; onion.ichar[2] = temp; return (onion.llen); }