diff options
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | README.org | 80 | ||||
| -rw-r--r-- | UNLICENSE | 24 | ||||
| -rwxr-xr-x | build.sh | 11 | ||||
| -rwxr-xr-x | run.sh | 45 | ||||
| -rw-r--r-- | src/brle-dec.c | 89 | ||||
| -rw-r--r-- | src/brle-enc.c | 94 | ||||
| -rw-r--r-- | src/config.h | 9 | ||||
| -rw-r--r-- | src/gendata.c | 49 | ||||
| -rw-r--r-- | src/lzw-dec.c | 119 | ||||
| -rw-r--r-- | src/lzw-enc.c | 135 | ||||
| -rw-r--r-- | src/rle-dec.c | 59 | ||||
| -rw-r--r-- | src/rle-enc.c | 57 |
13 files changed, 775 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f6001d3 --- /dev/null +++ b/.gitignore | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | /build | ||
| 2 | /out | ||
| 3 | |||
| 4 | *.html | ||
diff --git a/README.org b/README.org new file mode 100644 index 0000000..8d3b9a7 --- /dev/null +++ b/README.org | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | #+title: LZW and RLE Text Compression | ||
| 2 | #+subtitle: with Two-Step Pipeline Analysis | ||
| 3 | |||
| 4 | * Task[fn:1] | ||
| 5 | 1. Randomly generate a file of 10,000 characters using the specified | ||
| 6 | alphabet (uppercase and lowercase letters). Encode the text | ||
| 7 | information using the algorithm specified in the task. Determine | ||
| 8 | the encoding cost. Decode the information, determine the | ||
| 9 | compression ratio. Programmatically verify that the decoding was | ||
| 10 | performed correctly. | ||
| 11 | 2. Encode the information using the RLE algorithm. Decode the | ||
| 12 | information, determine the compression ratio. Programmatically | ||
| 13 | verify that the decoding occurred correctly. | ||
| 14 | 3. Similarly to point 2, encode the information using two-step | ||
| 15 | encoding (RLE + variant algorithm and algorithm option + RLE). | ||
| 16 | Show which method is more effective. | ||
| 17 | |||
| 18 | Alphabet: *{ й, ё, п, в, о, д, space, 0-9, &, . }* | ||
| 19 | |||
| 20 | Algorithm: *LZW* | ||
| 21 | |||
| 22 | * Documentation | ||
| 23 | Sadly, there is no report for this lab. | ||
| 24 | |||
| 25 | * Showcase | ||
| 26 | |||
| 27 | #+begin_src text | ||
| 28 | Generating data... | ||
| 29 | Generated: 1480044 bytes | ||
| 30 | |||
| 31 | LZW encode/decode... | ||
| 32 | Coding cost: 3.05 | ||
| 33 | Encoded into: 588393 bytes | ||
| 34 | Compression coefficent (out/in): 0.39 | ||
| 35 | Files ./out/data.txt and ./out/data-enc-lzw-dec-lzw.txt are identical | ||
| 36 | |||
| 37 | RLE encode/decode... | ||
| 38 | Encoded into: 1087997 bytes | ||
| 39 | Compression coefficent (out/in): 0.73 | ||
| 40 | Files ./out/data.txt and ./out/data-enc-rle-dec-rle.txt are identical | ||
| 41 | |||
| 42 | LZW-RLE encode/decode... | ||
| 43 | Encoded into: 1001594 bytes | ||
| 44 | Compression coefficent (out/in): 0.67 | ||
| 45 | Files ./out/data-enc-lzw.bin and ./out/data-enc-lzw-brle-dec-brle.bin are identical | ||
| 46 | |||
| 47 | RLE-LZW encode/decode... | ||
| 48 | Encoded into: 609561 bytes | ||
| 49 | Compression coefficent (out/in): 0.41 | ||
| 50 | Files ./out/data-enc-rle.txt and ./out/data-enc-rle-lzw-dec-lzw.txt are identical | ||
| 51 | |||
| 52 | LZW encoding is the most efficient | ||
| 53 | #+end_src | ||
| 54 | |||
| 55 | * Unlicense | ||
| 56 | #+include: UNLICENSE verse | ||
| 57 | |||
| 58 | * Footnotes | ||
| 59 | |||
| 60 | [fn:1] Original text: | ||
| 61 | #+HTML: <i> | ||
| 62 | #+begin_quote | ||
| 63 | 1. Случайно сгенерировать файл в 10 тысяч символов, используя | ||
| 64 | указанный алфавит (заглавные и строчные буквы). Закодировать | ||
| 65 | текстовую информацию, используя указанный в задании | ||
| 66 | алгоритм. Определить цену кодирования. Декодировать информацию, | ||
| 67 | определить коэффициент сжатия. Программно проверить, что | ||
| 68 | декодирование произошло верно. | ||
| 69 | 2. Закодировать информацию алгоритмом RLE. Декодировать информацию, | ||
| 70 | определить коэффициент сжатия. Программно проверить, что | ||
| 71 | декодирование произошло верно. | ||
| 72 | 3. Аналогично пункту 2 закодировать информацию, применив | ||
| 73 | двухступенчатое кодирование (RLE + алгоритм варианта и алгоритм | ||
| 74 | варианта + RLE). Показать, какой из способов более эффективный. | ||
| 75 | |||
| 76 | Алфавит: *{ й, ё, п, в, о, д, пробел, 0-9, &, . }* | ||
| 77 | |||
| 78 | Алгоритм: *LZW* | ||
| 79 | #+end_quote | ||
| 80 | #+HTML: </i> | ||
diff --git a/UNLICENSE b/UNLICENSE new file mode 100644 index 0000000..efb9808 --- /dev/null +++ b/UNLICENSE | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | This is free and unencumbered software released into the public domain. | ||
| 2 | |||
| 3 | Anyone is free to copy, modify, publish, use, compile, sell, or | ||
| 4 | distribute this software, either in source code form or as a compiled | ||
| 5 | binary, for any purpose, commercial or non-commercial, and by any | ||
| 6 | means. | ||
| 7 | |||
| 8 | In jurisdictions that recognize copyright laws, the author or authors | ||
| 9 | of this software dedicate any and all copyright interest in the | ||
| 10 | software to the public domain. We make this dedication for the benefit | ||
| 11 | of the public at large and to the detriment of our heirs and | ||
| 12 | successors. We intend this dedication to be an overt act of | ||
| 13 | relinquishment in perpetuity of all present and future rights to this | ||
| 14 | software under copyright law. | ||
| 15 | |||
| 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
| 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
| 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
| 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
| 22 | OTHER DEALINGS IN THE SOFTWARE. | ||
| 23 | |||
| 24 | For more information, please refer to <https://unlicense.org/> | ||
diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..f5f2c5c --- /dev/null +++ b/build.sh | |||
| @@ -0,0 +1,11 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | set -x | ||
| 4 | |||
| 5 | gcc -std=gnu99 -ggdb3 -O3 -o ./build/brle-dec ./src/brle-dec.c | ||
| 6 | gcc -std=gnu99 -ggdb3 -O3 -o ./build/brle-enc ./src/brle-enc.c | ||
| 7 | gcc -std=gnu99 -ggdb3 -O3 -o ./build/gendata ./src/gendata.c | ||
| 8 | gcc -std=gnu99 -ggdb3 -O3 -o ./build/lzw-dec ./src/lzw-dec.c | ||
| 9 | gcc -std=gnu99 -ggdb3 -O3 -Wno-pointer-to-int-cast -o ./build/lzw-enc ./src/lzw-enc.c | ||
| 10 | gcc -std=gnu99 -ggdb3 -O3 -o ./build/rle-dec ./src/rle-dec.c | ||
| 11 | gcc -std=gnu99 -ggdb3 -O3 -o ./build/rle-enc ./src/rle-enc.c | ||
| @@ -0,0 +1,45 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | |||
| 3 | echo "Generating data..." | ||
| 4 | ./build/gendata ./out/data.txt | ||
| 5 | DATA_BYTES=`wc -c ./out/data.txt | cut -d' ' -f1` | ||
| 6 | echo "Generated: $DATA_BYTES bytes" | ||
| 7 | echo | ||
| 8 | echo "LZW encode/decode..." | ||
| 9 | ./build/lzw-enc ./out/data.txt ./out/data-enc-lzw.bin | ||
| 10 | ./build/lzw-dec ./out/data-enc-lzw.bin ./out/data-enc-lzw-dec-lzw.txt | ||
| 11 | LZW_BYTES=`wc -c ./out/data-enc-lzw.bin | cut -d' ' -f1` | ||
| 12 | echo "Encoded into: $LZW_BYTES bytes" | ||
| 13 | echo "Compression coefficent (out/in): $(printf "%.2f\n" $(bc <<< "scale = 2; $LZW_BYTES / $DATA_BYTES"))" | ||
| 14 | diff -qs ./out/data.txt ./out/data-enc-lzw-dec-lzw.txt | ||
| 15 | echo | ||
| 16 | echo "RLE encode/decode..." | ||
| 17 | ./build/rle-enc ./out/data.txt ./out/data-enc-rle.txt | ||
| 18 | ./build/rle-dec ./out/data-enc-rle.txt ./out/data-enc-rle-dec-rle.txt | ||
| 19 | RLE_BYTES=`wc -c ./out/data-enc-rle.txt | cut -d' ' -f1` | ||
| 20 | echo "Encoded into: $RLE_BYTES bytes" | ||
| 21 | echo "Compression coefficent (out/in): $(printf "%.2f\n" $(bc <<< "scale = 2; $RLE_BYTES / $DATA_BYTES"))" | ||
| 22 | diff -qs ./out/data.txt ./out/data-enc-rle-dec-rle.txt | ||
| 23 | echo | ||
| 24 | echo "LZW-RLE encode/decode..." | ||
| 25 | ./build/brle-enc ./out/data-enc-lzw.bin ./out/data-enc-lzw-brle.bin | ||
| 26 | ./build/brle-dec ./out/data-enc-lzw-brle.bin ./out/data-enc-lzw-brle-dec-brle.bin | ||
| 27 | LZW_RLE_BYTES=`wc -c ./out/data-enc-lzw-brle.bin | cut -d' ' -f1` | ||
| 28 | echo "Encoded into: $LZW_RLE_BYTES bytes" | ||
| 29 | echo "Compression coefficent (out/in): $(printf "%.2f\n" $(bc <<< "scale = 2; $LZW_RLE_BYTES / $DATA_BYTES"))" | ||
| 30 | diff -qs ./out/data-enc-lzw.bin ./out/data-enc-lzw-brle-dec-brle.bin | ||
| 31 | echo | ||
| 32 | echo "RLE-LZW encode/decode..." | ||
| 33 | ./build/lzw-enc ./out/data-enc-rle.txt ./out/data-enc-rle-lzw.bin >/dev/null | ||
| 34 | ./build/lzw-dec ./out/data-enc-rle-lzw.bin ./out/data-enc-rle-lzw-dec-lzw.txt | ||
| 35 | RLE_LZW_BYTES=`wc -c ./out/data-enc-rle-lzw.bin | cut -d' ' -f1` | ||
| 36 | echo "Encoded into: $RLE_LZW_BYTES bytes" | ||
| 37 | echo "Compression coefficent (out/in): $(printf "%.2f\n" $(bc <<< "scale = 2; $RLE_LZW_BYTES / $DATA_BYTES"))" | ||
| 38 | diff -qs ./out/data-enc-rle.txt ./out/data-enc-rle-lzw-dec-lzw.txt | ||
| 39 | echo | ||
| 40 | echo "\ | ||
| 41 | $LZW_BYTES LZW | ||
| 42 | $RLE_BYTES RLE | ||
| 43 | $LZW_RLE_BYTES LZW-RLE | ||
| 44 | $RLE_LZW_BYTES RLE-LZW" | sort -n | head -n1 | cut -d' ' -f2 | tr '\n' ' ' | ||
| 45 | echo "encoding is the most efficient" | ||
diff --git a/src/brle-dec.c b/src/brle-dec.c new file mode 100644 index 0000000..298c945 --- /dev/null +++ b/src/brle-dec.c | |||
| @@ -0,0 +1,89 @@ | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <stdint.h> | ||
| 4 | #include <wchar.h> | ||
| 5 | #include <locale.h> | ||
| 6 | #include <stdbool.h> | ||
| 7 | #include <libgen.h> | ||
| 8 | |||
| 9 | #include "config.h" | ||
| 10 | |||
| 11 | FILE *istream; | ||
| 12 | FILE *ostream; | ||
| 13 | |||
| 14 | uint8_t | ||
| 15 | nputbit (uint8_t bit, uint8_t n) | ||
| 16 | { | ||
| 17 | static uint16_t buff; | ||
| 18 | static uint8_t i = 0; | ||
| 19 | if (bit) | ||
| 20 | buff |= ((1 << n) - 1) << i; | ||
| 21 | i += n; | ||
| 22 | |||
| 23 | if (i >= 8) { | ||
| 24 | fputc (buff & (1 << 8) - 1, ostream); | ||
| 25 | buff >>= 8; | ||
| 26 | i -= 8; | ||
| 27 | } | ||
| 28 | |||
| 29 | return i; | ||
| 30 | } | ||
| 31 | |||
| 32 | uint8_t | ||
| 33 | get4bit (void) | ||
| 34 | { | ||
| 35 | static int byte; | ||
| 36 | static bool empty = true; | ||
| 37 | if (empty) { | ||
| 38 | byte = fgetc (istream); | ||
| 39 | if (byte == EOF) | ||
| 40 | return -1; | ||
| 41 | empty = false; | ||
| 42 | } else { | ||
| 43 | empty = true; | ||
| 44 | byte >>= 4; | ||
| 45 | } | ||
| 46 | return byte & (1 << 4) - 1; | ||
| 47 | } | ||
| 48 | |||
| 49 | int | ||
| 50 | main (int argc, char *argv[]) | ||
| 51 | { | ||
| 52 | setlocale (LC_ALL, ""); | ||
| 53 | |||
| 54 | if (argc != 3) { | ||
| 55 | fprintf (stderr, "Usage: %s {infile} {outfile}\n", basename (argv[0])); | ||
| 56 | return EXIT_FAILURE; | ||
| 57 | } | ||
| 58 | |||
| 59 | istream = fopen (argv[1], "rb"); | ||
| 60 | if (istream == NULL) { | ||
| 61 | perror (NULL); | ||
| 62 | return EXIT_FAILURE; | ||
| 63 | } | ||
| 64 | |||
| 65 | ostream = fopen (argv[2], "wb"); | ||
| 66 | if (ostream == NULL) { | ||
| 67 | perror (NULL); | ||
| 68 | return EXIT_FAILURE; | ||
| 69 | } | ||
| 70 | |||
| 71 | char current; | ||
| 72 | uint8_t buffered; | ||
| 73 | while ((current = get4bit ()) != -1) | ||
| 74 | buffered = nputbit (current >> 3, current & (1 << 3) - 1); | ||
| 75 | if (buffered) | ||
| 76 | nputbit (0, 8 - buffered); | ||
| 77 | |||
| 78 | if (fclose (ostream) == EOF) { | ||
| 79 | perror (NULL); | ||
| 80 | return EXIT_FAILURE; | ||
| 81 | } | ||
| 82 | |||
| 83 | if (fclose (istream) == EOF) { | ||
| 84 | perror (NULL); | ||
| 85 | return EXIT_FAILURE; | ||
| 86 | } | ||
| 87 | |||
| 88 | return EXIT_SUCCESS; | ||
| 89 | } | ||
diff --git a/src/brle-enc.c b/src/brle-enc.c new file mode 100644 index 0000000..d14c462 --- /dev/null +++ b/src/brle-enc.c | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <stdint.h> | ||
| 4 | #include <wchar.h> | ||
| 5 | #include <locale.h> | ||
| 6 | #include <stdbool.h> | ||
| 7 | #include <libgen.h> | ||
| 8 | |||
| 9 | #include "config.h" | ||
| 10 | |||
| 11 | FILE *istream; | ||
| 12 | FILE *ostream; | ||
| 13 | |||
| 14 | char | ||
| 15 | getbit (void) | ||
| 16 | { | ||
| 17 | static int byte; | ||
| 18 | static int i = 8; | ||
| 19 | if (i > 7) { | ||
| 20 | byte = fgetc (istream); | ||
| 21 | if (byte == EOF) | ||
| 22 | return -1; | ||
| 23 | i = 0; | ||
| 24 | } | ||
| 25 | char bit = byte >> i & 1; | ||
| 26 | i += 1; | ||
| 27 | return bit; | ||
| 28 | } | ||
| 29 | |||
| 30 | bool | ||
| 31 | put4bit (uint8_t data) | ||
| 32 | { | ||
| 33 | static uint8_t byte; | ||
| 34 | static bool nonempty = false; | ||
| 35 | if (nonempty) { | ||
| 36 | byte |= data << 4; | ||
| 37 | fputc (byte, ostream); | ||
| 38 | nonempty = false; | ||
| 39 | } else { | ||
| 40 | byte = data & (1 << 4) - 1; | ||
| 41 | nonempty = true; | ||
| 42 | } | ||
| 43 | return nonempty; | ||
| 44 | } | ||
| 45 | |||
| 46 | int | ||
| 47 | main (int argc, char *argv[]) | ||
| 48 | { | ||
| 49 | setlocale (LC_ALL, ""); | ||
| 50 | |||
| 51 | if (argc != 3) { | ||
| 52 | fprintf (stderr, "Usage: %s {infile} {outfile}\n", basename (argv[0])); | ||
| 53 | return EXIT_FAILURE; | ||
| 54 | } | ||
| 55 | |||
| 56 | istream = fopen (argv[1], "rb"); | ||
| 57 | if (istream == NULL) { | ||
| 58 | perror (NULL); | ||
| 59 | return EXIT_FAILURE; | ||
| 60 | } | ||
| 61 | |||
| 62 | ostream = fopen (argv[2], "wb"); | ||
| 63 | if (ostream == NULL) { | ||
| 64 | perror (NULL); | ||
| 65 | return EXIT_FAILURE; | ||
| 66 | } | ||
| 67 | |||
| 68 | char current = getbit (); | ||
| 69 | bool nonempty; | ||
| 70 | while (current != -1) { | ||
| 71 | uint8_t count = 1; | ||
| 72 | char next; | ||
| 73 | while ((next = getbit ()) == current && count < (1 << 3) - 1) | ||
| 74 | ++count; | ||
| 75 | |||
| 76 | nonempty = put4bit (current << 3 | count); | ||
| 77 | |||
| 78 | current = next; | ||
| 79 | } | ||
| 80 | if (nonempty) | ||
| 81 | put4bit (0); | ||
| 82 | |||
| 83 | if (fclose (ostream) == EOF) { | ||
| 84 | perror (NULL); | ||
| 85 | return EXIT_FAILURE; | ||
| 86 | } | ||
| 87 | |||
| 88 | if (fclose (istream) == EOF) { | ||
| 89 | perror (NULL); | ||
| 90 | return EXIT_FAILURE; | ||
| 91 | } | ||
| 92 | |||
| 93 | return EXIT_SUCCESS; | ||
| 94 | } | ||
diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..d551f87 --- /dev/null +++ b/src/config.h | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | #ifndef INCLUDED_CONFIG_H | ||
| 2 | #define INCLUDED_CONFIG_H | ||
| 3 | |||
| 4 | #define ALPHABET L"йёпводЙЁПВОД0123456789&. " | ||
| 5 | // #define LETTER_COUNT 10000 | ||
| 6 | #define LETTER_COUNT 1000000 | ||
| 7 | #define CCS "UTF8" | ||
| 8 | |||
| 9 | #endif | ||
diff --git a/src/gendata.c b/src/gendata.c new file mode 100644 index 0000000..cb92133 --- /dev/null +++ b/src/gendata.c | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <libgen.h> | ||
| 4 | #include <errno.h> | ||
| 5 | #include <time.h> | ||
| 6 | #include <wchar.h> | ||
| 7 | #include <unistd.h> | ||
| 8 | #include "config.h" | ||
| 9 | |||
| 10 | int | ||
| 11 | main (int argc, char *argv[]) | ||
| 12 | { | ||
| 13 | if (argc != 2) { | ||
| 14 | fprintf (stderr, "Usage: %s {outfile}\n", basename (argv[0])); | ||
| 15 | return EXIT_FAILURE; | ||
| 16 | } | ||
| 17 | |||
| 18 | FILE *stream = fopen (argv[1], "w,ccs="CCS); | ||
| 19 | if (stream == NULL) { | ||
| 20 | perror (NULL); | ||
| 21 | return EXIT_FAILURE; | ||
| 22 | } | ||
| 23 | |||
| 24 | srand (time (NULL)); | ||
| 25 | size_t letter_count = 15; | ||
| 26 | for (int i = 0; i < 15; ++i) | ||
| 27 | fputwc (ALPHABET[rand () % wcslen (ALPHABET)], stream); | ||
| 28 | while (letter_count < LETTER_COUNT) { | ||
| 29 | int count = rand () % 15 + 1; | ||
| 30 | size_t letters_left = LETTER_COUNT - letter_count; | ||
| 31 | count = count > letters_left ? letters_left : count; | ||
| 32 | if (rand () % 2) { | ||
| 33 | for (int i = 0; i < count; ++i) | ||
| 34 | fputwc (ALPHABET[rand () % wcslen (ALPHABET)], stream); | ||
| 35 | } else { | ||
| 36 | wchar_t wc = ALPHABET[rand () % wcslen (ALPHABET)]; | ||
| 37 | for (int i = 0; i < count; ++i) | ||
| 38 | fputwc (wc, stream); | ||
| 39 | } | ||
| 40 | letter_count += count; | ||
| 41 | } | ||
| 42 | |||
| 43 | if (fclose (stream) == EOF) { | ||
| 44 | perror (NULL); | ||
| 45 | return EXIT_FAILURE; | ||
| 46 | } | ||
| 47 | |||
| 48 | return EXIT_SUCCESS; | ||
| 49 | } | ||
diff --git a/src/lzw-dec.c b/src/lzw-dec.c new file mode 100644 index 0000000..e5125dc --- /dev/null +++ b/src/lzw-dec.c | |||
| @@ -0,0 +1,119 @@ | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <string.h> | ||
| 4 | #include <wchar.h> | ||
| 5 | #include <errno.h> | ||
| 6 | #include <locale.h> | ||
| 7 | #include <inttypes.h> | ||
| 8 | #include <libgen.h> | ||
| 9 | |||
| 10 | #include "config.h" | ||
| 11 | |||
| 12 | FILE *istream; | ||
| 13 | |||
| 14 | uint16_t next_input_code () | ||
| 15 | { | ||
| 16 | static uint8_t buf[3]; | ||
| 17 | static uint16_t next; | ||
| 18 | static int i = 0; | ||
| 19 | |||
| 20 | if (i) { | ||
| 21 | if (next != -1) | ||
| 22 | i = 0; | ||
| 23 | return next; | ||
| 24 | } else { | ||
| 25 | size_t read; | ||
| 26 | read = fread (buf, 1, 3, istream); | ||
| 27 | i = 1; | ||
| 28 | if (read == 3) { | ||
| 29 | next = buf[1] >> 4 | buf[2] << 4; | ||
| 30 | } else if (read == 2) { | ||
| 31 | next = -1; | ||
| 32 | } else | ||
| 33 | return -1; | ||
| 34 | |||
| 35 | return buf[0] | (buf[1] & ((1 << 4) - 1)) << 8; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | int | ||
| 40 | main (int argc, char *argv[]) | ||
| 41 | { | ||
| 42 | setlocale (LC_ALL, ""); | ||
| 43 | |||
| 44 | if (argc != 3) { | ||
| 45 | fprintf (stderr, "Usage: %s {infile} {outfile}\n", basename (argv[0])); | ||
| 46 | return EXIT_FAILURE; | ||
| 47 | } | ||
| 48 | |||
| 49 | // initialize dictionary | ||
| 50 | wchar_t *dict[1 << 11] = { NULL }; | ||
| 51 | size_t next_code; | ||
| 52 | for (next_code = 0; next_code < wcslen (ALPHABET); ++next_code) { | ||
| 53 | dict[next_code] = malloc (2*sizeof (wchar_t)); | ||
| 54 | dict[next_code][0] = ALPHABET[next_code]; | ||
| 55 | dict[next_code][1] = L'\0'; | ||
| 56 | // printf ("%d %ls\n", next_code, dict[next_code]); | ||
| 57 | } // putchar ('\n'); | ||
| 58 | |||
| 59 | istream = fopen (argv[1], "rb"); | ||
| 60 | if (istream == NULL) { | ||
| 61 | perror (NULL); | ||
| 62 | return EXIT_FAILURE; | ||
| 63 | } | ||
| 64 | |||
| 65 | FILE *ostream = fopen (argv[2], "w,ccs="CCS); | ||
| 66 | if (ostream == NULL) { | ||
| 67 | perror (NULL); | ||
| 68 | return EXIT_FAILURE; | ||
| 69 | } | ||
| 70 | |||
| 71 | uint16_t old = next_input_code (); | ||
| 72 | if (old == -1) { | ||
| 73 | return EXIT_SUCCESS; | ||
| 74 | } | ||
| 75 | uint16_t new; | ||
| 76 | wchar_t *seq = NULL; | ||
| 77 | wchar_t wc; | ||
| 78 | fputws (dict[old], ostream); | ||
| 79 | while ((new = next_input_code ()) != (uint16_t) -1) { | ||
| 80 | if (dict[new]) { | ||
| 81 | size_t new_len = wcslen (dict[new]); | ||
| 82 | seq = realloc (seq, (new_len + 1)*sizeof (wchar_t)); | ||
| 83 | wcscpy (seq, dict[new]); | ||
| 84 | } else { | ||
| 85 | size_t old_len = wcslen (dict[old]); | ||
| 86 | seq = realloc (seq, (old_len + 2)*sizeof (wchar_t)); | ||
| 87 | memcpy (seq, dict[old], old_len*sizeof (wchar_t)); | ||
| 88 | seq[old_len + 0] = wc; | ||
| 89 | seq[old_len + 1] = L'\0'; | ||
| 90 | } | ||
| 91 | |||
| 92 | fputws (seq, ostream); | ||
| 93 | |||
| 94 | wc = seq[0]; | ||
| 95 | |||
| 96 | if (next_code < 1 << 11 - 1) { | ||
| 97 | size_t old_len = wcslen (dict[old]); | ||
| 98 | dict[next_code] = malloc ((old_len + 2)*sizeof (wchar_t)); | ||
| 99 | memcpy (dict[next_code], dict[old], old_len*sizeof (wchar_t)); | ||
| 100 | dict[next_code][old_len + 0] = wc; | ||
| 101 | dict[next_code][old_len + 1] = L'\0'; | ||
| 102 | ++next_code; | ||
| 103 | } | ||
| 104 | |||
| 105 | old = new; | ||
| 106 | // fflush (stdout); | ||
| 107 | } // putchar ('\n'); | ||
| 108 | |||
| 109 | if (fclose (istream) == EOF) { | ||
| 110 | perror (NULL); | ||
| 111 | return EXIT_FAILURE; | ||
| 112 | } | ||
| 113 | if (fclose (ostream) == EOF) { | ||
| 114 | perror (NULL); | ||
| 115 | return EXIT_FAILURE; | ||
| 116 | } | ||
| 117 | |||
| 118 | return EXIT_SUCCESS; | ||
| 119 | } | ||
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 | |||
| 13 | int | ||
| 14 | main (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 | } | ||
diff --git a/src/rle-dec.c b/src/rle-dec.c new file mode 100644 index 0000000..02a006c --- /dev/null +++ b/src/rle-dec.c | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <wchar.h> | ||
| 4 | #include <locale.h> | ||
| 5 | #include <libgen.h> | ||
| 6 | |||
| 7 | #include "config.h" | ||
| 8 | |||
| 9 | int | ||
| 10 | main (int argc, char *argv[]) | ||
| 11 | { | ||
| 12 | setlocale (LC_ALL, ""); | ||
| 13 | |||
| 14 | if (argc != 3) { | ||
| 15 | fprintf (stderr, "Usage: %s {infile} {outfile}\n", basename (argv[0])); | ||
| 16 | return EXIT_FAILURE; | ||
| 17 | } | ||
| 18 | |||
| 19 | FILE *istream = fopen (argv[1], "r,ccs="CCS); | ||
| 20 | if (istream == NULL) { | ||
| 21 | perror (NULL); | ||
| 22 | return EXIT_FAILURE; | ||
| 23 | } | ||
| 24 | |||
| 25 | FILE *ostream = fopen (argv[2], "w,ccs="CCS); | ||
| 26 | // FILE *ostream = stdout; | ||
| 27 | if (ostream == NULL) { | ||
| 28 | perror (NULL); | ||
| 29 | return EXIT_FAILURE; | ||
| 30 | } | ||
| 31 | |||
| 32 | wint_t current = fgetwc (istream); | ||
| 33 | while (current != WEOF) { | ||
| 34 | wint_t next = fgetwc (istream); | ||
| 35 | if (next == current) { | ||
| 36 | size_t count; | ||
| 37 | fwscanf (istream, L"%llu", &count); | ||
| 38 | fgetwc (istream); | ||
| 39 | while (count--) | ||
| 40 | fputwc (current, ostream); | ||
| 41 | current = fgetwc (istream); | ||
| 42 | } else { | ||
| 43 | fputwc (current, ostream); | ||
| 44 | current = next; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | if (fclose (ostream) == EOF) { | ||
| 49 | perror (NULL); | ||
| 50 | return EXIT_FAILURE; | ||
| 51 | } | ||
| 52 | |||
| 53 | if (fclose (istream) == EOF) { | ||
| 54 | perror (NULL); | ||
| 55 | return EXIT_FAILURE; | ||
| 56 | } | ||
| 57 | |||
| 58 | return EXIT_SUCCESS; | ||
| 59 | } | ||
diff --git a/src/rle-enc.c b/src/rle-enc.c new file mode 100644 index 0000000..6620a81 --- /dev/null +++ b/src/rle-enc.c | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <wchar.h> | ||
| 4 | #include <locale.h> | ||
| 5 | #include <libgen.h> | ||
| 6 | |||
| 7 | #include "config.h" | ||
| 8 | |||
| 9 | int | ||
| 10 | main (int argc, char *argv[]) | ||
| 11 | { | ||
| 12 | setlocale (LC_ALL, ""); | ||
| 13 | |||
| 14 | if (argc != 3) { | ||
| 15 | fprintf (stderr, "Usage: %s {infile} {outfile}\n", basename (argv[0])); | ||
| 16 | return EXIT_FAILURE; | ||
| 17 | } | ||
| 18 | |||
| 19 | FILE *istream = fopen (argv[1], "r,ccs="CCS); | ||
| 20 | if (istream == NULL) { | ||
| 21 | perror (NULL); | ||
| 22 | return EXIT_FAILURE; | ||
| 23 | } | ||
| 24 | |||
| 25 | FILE *ostream = fopen (argv[2], "w,ccs="CCS); | ||
| 26 | if (ostream == NULL) { | ||
| 27 | perror (NULL); | ||
| 28 | return EXIT_FAILURE; | ||
| 29 | } | ||
| 30 | |||
| 31 | wint_t current = fgetwc (istream); | ||
| 32 | while (current != WEOF) { | ||
| 33 | size_t count = 1; | ||
| 34 | wint_t next; | ||
| 35 | while ((next = fgetwc (istream)) == current) | ||
| 36 | ++count; | ||
| 37 | |||
| 38 | if (count == 1) | ||
| 39 | fwprintf (ostream, L"%lc", current); | ||
| 40 | else | ||
| 41 | fwprintf (ostream, L"%lc%lc%llu ", current, current, count); | ||
| 42 | |||
| 43 | current = next; | ||
| 44 | } | ||
| 45 | |||
| 46 | if (fclose (ostream) == EOF) { | ||
| 47 | perror (NULL); | ||
| 48 | return EXIT_FAILURE; | ||
| 49 | } | ||
| 50 | |||
| 51 | if (fclose (istream) == EOF) { | ||
| 52 | perror (NULL); | ||
| 53 | return EXIT_FAILURE; | ||
| 54 | } | ||
| 55 | |||
| 56 | return EXIT_SUCCESS; | ||
| 57 | } | ||
