aboutsummaryrefslogtreecommitdiff
path: root/src/lzw-enc.c
blob: 0c6db3b572c8fe72f10f407528b2eab1d7475bf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <search.h>
#include <wchar.h>
#include <errno.h>
#include <locale.h>
#include <inttypes.h>
#include <libgen.h>

#include "config.h"

int
main (int argc, char *argv[])
{
  setlocale (LC_ALL, "");
  errno = 0;

  if (argc != 3) {
    fprintf (stderr, "Usage: %s {infile} {outfile}\n", basename (argv[0]));
    return EXIT_FAILURE;
  }

  // initialize dictionary
  hcreate ((1 << 12)*6/5);
  size_t next_code = 0;
  for (next_code = 0; next_code < wcslen (ALPHABET); ++next_code) {
    wchar_t wseq[2];
    wseq[0] = ALPHABET[next_code];
    wseq[1] = L'\0';
    char *seq = malloc (3);
    wcstombs (seq, wseq, 3);
    ENTRY entry = {
      .key = seq,
      .data = (void *)next_code,
    };
    // printf ("%d %s\n", next_code, seq);
    ENTRY *ep = hsearch (entry, ENTER);
  } // putchar ('\n');

  FILE *istream = fopen (argv[1], "r,ccs="CCS);
  if (istream == NULL) {
    perror (NULL);
    return EXIT_FAILURE;
  }

  FILE *ostream = fopen (argv[2], "wb");
  if (ostream == NULL) {
    perror (NULL);
    return EXIT_FAILURE;
  }

  size_t len = 1;
  wchar_t *wseq = malloc ((len + 1)*sizeof (wchar_t));
  char *seq = malloc (2*len + 1);
  wseq[0] = fgetwc (istream);
  wseq[1] = L'\0';
  uint8_t buf[3] = { 0 };
  int bufcount = 0;
  size_t dictvol = next_code;
  while (wseq[len - 1] != WEOF) {
    wchar_t wc = fgetwc (istream);
    if (wc == WEOF)
      break;
    ++len;
    wseq = realloc (wseq, (len + 1)*sizeof (wchar_t));
    seq = realloc (seq, 2*len + 1);
    wseq[len - 1] = wc;
    wseq[len] = L'\0';
    wcstombs (seq, wseq, 2*len + 1);
    ENTRY entry = {
      .key = seq,
    };
    ENTRY *ep = hsearch (entry, FIND);
    if (ep == NULL) {
      if (next_code < 1 << 11 - 1) {
        entry.key = strdup (entry.key);
        entry.data = (void *) next_code++;
        hsearch (entry, ENTER);
        entry.key = seq;
        dictvol += wcslen (wseq);
      }

      wseq[len - 1] = '\0';
      wcstombs (seq, wseq, 2*len + 1);
      ENTRY *ep = hsearch (entry, FIND);
      // printf ("%llu %s\n", ep->data, ep->key);
      uint16_t c = (uint16_t) ep->data;
      if (bufcount) {
        buf[1] |= (c & ((1 << 4) - 1)) << 4;
        buf[2]  = c >> 4;
        bufcount = 0;
        fwrite (buf, 1, 3, ostream);
      } else {
        buf[0] = c & ((1 << 8) - 1);
        buf[1] = c >> 8;
        bufcount = 1;
      }

      wseq[0] = wc;
      wseq[1] = L'\0';
      len = 1;
    }
  }
  wcstombs (seq, wseq, 2*len + 1);
  ENTRY entry = {
    .key = seq,
  };
  ENTRY *ep = hsearch (entry, FIND);
  // printf ("%llu %s\n", ep->data, ep->key);
  uint16_t c = (uint16_t) ep->data;
  if (bufcount) {
    buf[1] |= (c & ((1 << 4) - 1)) << 4;
    buf[2]  = c >> 4;
    fwrite (buf, 1, 3, ostream);
  } else {
    buf[0] = c & ((1 << 8) - 1);
    buf[1] = c >> 8;
    fwrite (buf, 1, 2, ostream);
  }

  printf ("Coding cost: %.2f\n", (float) dictvol/next_code);

  if (fclose (istream) == EOF) {
    perror (NULL);
    return EXIT_FAILURE;
  }

  if (fclose (ostream) == EOF) {
    perror (NULL);
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}