huffman.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #ifndef HUFFMAN_H
  2. #define HUFFMAN_H
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #define ASCII_SIZE 256
  7. #define ASCII_BYTE_SIZE 8
  8. #define NO_CHAR -1
  9. #define NO_BIT -1
  10. #define TRUE 1
  11. #define FALSE 0
  12. #define LEFT 0
  13. #define RIGHT 1
  14. typedef struct _huff_node {
  15. char ascii;
  16. int weight;
  17. struct _huff_node *left;
  18. struct _huff_node *right;
  19. struct _huff_node *next;
  20. } Huff_Node;
  21. void count_weights(char *filename, int *weights);
  22. int build_list(Huff_Node **head, int *weights);
  23. Huff_Node * create_huff_node(char ascii, int weight);
  24. void add_list_node(Huff_Node **headp, Huff_Node *toadd);
  25. void print_list(Huff_Node *head);
  26. Huff_Node * gen_huff_tree(Huff_Node *head, int weight_total);
  27. void print_tree(Huff_Node *root);
  28. Huff_Node * remove_min_node(Huff_Node *head);
  29. int get_tree_height(Huff_Node *root);
  30. int get_tree_height_rh(Huff_Node *root, int height);
  31. int get_num_leaves(Huff_Node *root);
  32. void get_num_leaves_rh(Huff_Node *root, int *leaves);
  33. void gen_codes(Huff_Node * root, int ** codebook);
  34. void gen_codes_rh(Huff_Node *root, int ** codebook, int * row, int col);
  35. void print_codes(int ** codebook, int numRow);
  36. void map_codebook(int ** codebook, int size, int * map);
  37. Huff_Node *read_file_header_bits(FILE *fptr);
  38. void write_file_header_bits(FILE *fptr, Huff_Node *root, int weight_total, unsigned int num_leaves);
  39. void write_file_header_bits_rh(FILE *fptr, Huff_Node *root, unsigned char *bit_pos, unsigned char *cur_byte);
  40. void write_ascii_bits(FILE *fptr, char ascii, unsigned char *bit_pos, unsigned char *cur_byte);
  41. void write_bit(FILE *fptr, char bit, unsigned char *bit_pos, unsigned char *cur_byte);
  42. void read_bit(FILE *fptr, unsigned char *bit, unsigned char *bit_pos, unsigned char * cur_byte);
  43. void compress_file(char *filename_in, char *filename_out, int ** codebook, int * map);
  44. void decompress_file(char *filename_in, char *filename_out);
  45. void write_file_header(FILE *fptr, int *weights);
  46. Huff_Node * read_file_header(FILE *fptr);
  47. void write_file_data(FILE *fptr_out, FILE *fptr_in, int ** codebook, int * map);
  48. char * read_file_data(FILE *fptr, Huff_Node *root);
  49. void write_encoded_message(char * data, Huff_Node *root, FILE *out);
  50. #endif