aboutsummaryrefslogtreecommitdiff
path: root/src/lzw-enc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lzw-enc.c')
-rw-r--r--src/lzw-enc.c135
1 files changed, 135 insertions, 0 deletions
diff --git a/src/lzw-enc.c b/src/lzw-enc.c
new file mode 100644
index 0000000..0c6db3b
--- /dev/null
+++ b/src/lzw-enc.c
@@ -0,0 +1,135 @@
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4#include <search.h>
5#include <wchar.h>
6#include <errno.h>
7#include <locale.h>
8#include <inttypes.h>
9#include <libgen.h>
10
11#include "config.h"
12
13int
14main (int argc, char *argv[])
15{
16 setlocale (LC_ALL, "");
17 errno = 0;
18
19 if (argc != 3) {
20 fprintf (stderr, "Usage: %s {infile} {outfile}\n", basename (argv[0]));
21 return EXIT_FAILURE;
22 }
23
24 // initialize dictionary
25 hcreate ((1 << 12)*6/5);
26 size_t next_code = 0;
27 for (next_code = 0; next_code < wcslen (ALPHABET); ++next_code) {
28 wchar_t wseq[2];
29 wseq[0] = ALPHABET[next_code];
30 wseq[1] = L'\0';
31 char *seq = malloc (3);
32 wcstombs (seq, wseq, 3);
33 ENTRY entry = {
34 .key = seq,
35 .data = (void *)next_code,
36 };
37 // printf ("%d %s\n", next_code, seq);
38 ENTRY *ep = hsearch (entry, ENTER);
39 } // putchar ('\n');
40
41 FILE *istream = fopen (argv[1], "r,ccs="CCS);
42 if (istream == NULL) {
43 perror (NULL);
44 return EXIT_FAILURE;
45 }
46
47 FILE *ostream = fopen (argv[2], "wb");
48 if (ostream == NULL) {
49 perror (NULL);
50 return EXIT_FAILURE;
51 }
52
53 size_t len = 1;
54 wchar_t *wseq = malloc ((len + 1)*sizeof (wchar_t));
55 char *seq = malloc (2*len + 1);
56 wseq[0] = fgetwc (istream);
57 wseq[1] = L'\0';
58 uint8_t buf[3] = { 0 };
59 int bufcount = 0;
60 size_t dictvol = next_code;
61 while (wseq[len - 1] != WEOF) {
62 wchar_t wc = fgetwc (istream);
63 if (wc == WEOF)
64 break;
65 ++len;
66 wseq = realloc (wseq, (len + 1)*sizeof (wchar_t));
67 seq = realloc (seq, 2*len + 1);
68 wseq[len - 1] = wc;
69 wseq[len] = L'\0';
70 wcstombs (seq, wseq, 2*len + 1);
71 ENTRY entry = {
72 .key = seq,
73 };
74 ENTRY *ep = hsearch (entry, FIND);
75 if (ep == NULL) {
76 if (next_code < 1 << 11 - 1) {
77 entry.key = strdup (entry.key);
78 entry.data = (void *) next_code++;
79 hsearch (entry, ENTER);
80 entry.key = seq;
81 dictvol += wcslen (wseq);
82 }
83
84 wseq[len - 1] = '\0';
85 wcstombs (seq, wseq, 2*len + 1);
86 ENTRY *ep = hsearch (entry, FIND);
87 // printf ("%llu %s\n", ep->data, ep->key);
88 uint16_t c = (uint16_t) ep->data;
89 if (bufcount) {
90 buf[1] |= (c & ((1 << 4) - 1)) << 4;
91 buf[2] = c >> 4;
92 bufcount = 0;
93 fwrite (buf, 1, 3, ostream);
94 } else {
95 buf[0] = c & ((1 << 8) - 1);
96 buf[1] = c >> 8;
97 bufcount = 1;
98 }
99
100 wseq[0] = wc;
101 wseq[1] = L'\0';
102 len = 1;
103 }
104 }
105 wcstombs (seq, wseq, 2*len + 1);
106 ENTRY entry = {
107 .key = seq,
108 };
109 ENTRY *ep = hsearch (entry, FIND);
110 // printf ("%llu %s\n", ep->data, ep->key);
111 uint16_t c = (uint16_t) ep->data;
112 if (bufcount) {
113 buf[1] |= (c & ((1 << 4) - 1)) << 4;
114 buf[2] = c >> 4;
115 fwrite (buf, 1, 3, ostream);
116 } else {
117 buf[0] = c & ((1 << 8) - 1);
118 buf[1] = c >> 8;
119 fwrite (buf, 1, 2, ostream);
120 }
121
122 printf ("Coding cost: %.2f\n", (float) dictvol/next_code);
123
124 if (fclose (istream) == EOF) {
125 perror (NULL);
126 return EXIT_FAILURE;
127 }
128
129 if (fclose (ostream) == EOF) {
130 perror (NULL);
131 return EXIT_FAILURE;
132 }
133
134 return EXIT_SUCCESS;
135}