1 | /* sub.c not copyrighted (n) 1993 by Mark Adler */
|
---|
2 | /* version 1.1 11 Jun 1993 */
|
---|
3 |
|
---|
4 | /* sub is a simple filter to preprocess a data file before compression.
|
---|
5 | It can increase compression for data whose points tend to be close to
|
---|
6 | the last point. The output is the difference of successive bytes of
|
---|
7 | the input. The add filter is used to undo what sub does. This could
|
---|
8 | be used on 8-bit sound or graphics data.
|
---|
9 |
|
---|
10 | sub can also take an argument to apply this to interleaved sets of
|
---|
11 | bytes. For example, if the data are 16-bit sound samples, then you
|
---|
12 | can use "sub 2" to take differences on the low-byte stream and the
|
---|
13 | high-byte stream. (This gives nearly the same effect as subtracting
|
---|
14 | the 16-bit values, but avoids the complexities of endianess of the
|
---|
15 | data.) The concept extends to RGB image data (sub 3), 16-bit stereo
|
---|
16 | data (sub 4), floating point data (sub 4 or sub 8), etc.
|
---|
17 |
|
---|
18 | add takes no options, since the number of interleaved byte streams
|
---|
19 | is put in the first two bytes of the output stream for add to use
|
---|
20 | (in little-endian format).
|
---|
21 |
|
---|
22 | Examples:
|
---|
23 |
|
---|
24 | sub < graph.vga | gzip -9 > graph.vga.sgz
|
---|
25 | sub < phone.snd | gzip -9 > phone.snd.sgz
|
---|
26 | sub 2 < audio.snd | gzip -9 > audio.snd.sgz
|
---|
27 | sub 3 < picture.rgb | gzip -9 > picture.rgb.sgz
|
---|
28 | sub 4 < stereo.snd | gzip -9 > stereo.snd.sgz
|
---|
29 | sub 8 < double.data | gzip -9 > double.data.sgz
|
---|
30 |
|
---|
31 | To expand, use the reverse operation, as in:
|
---|
32 |
|
---|
33 | gunzip < double.data.sgz | add > double.data
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <stdio.h>
|
---|
37 |
|
---|
38 | #define MAGIC1 'S' /* sub data */
|
---|
39 | #define MAGIC2 26 /* ^Z */
|
---|
40 | #define MAX_DIST 16384
|
---|
41 |
|
---|
42 | char a[MAX_DIST]; /* last byte buffer for up to MAX_DIST differences */
|
---|
43 |
|
---|
44 | int main(argc, argv)
|
---|
45 | int argc;
|
---|
46 | char **argv;
|
---|
47 | {
|
---|
48 | int n = 1; /* number of differences */
|
---|
49 | int i; /* difference counter */
|
---|
50 | int c; /* byte from input */
|
---|
51 | int atoi(); /* (avoid including stdlib for portability) */
|
---|
52 |
|
---|
53 | /* process arguments */
|
---|
54 | if (argc > 2)
|
---|
55 | {
|
---|
56 | fputs("sub: only one argument needed--# of differences\n", stderr);
|
---|
57 | exit(1);
|
---|
58 | }
|
---|
59 | if (argc > 1)
|
---|
60 | n = atoi(argv[1]);
|
---|
61 |
|
---|
62 | if (n < 0) n = -n; /* tolerate "sub -2" */
|
---|
63 | if (n == 0 || n > MAX_DIST) {
|
---|
64 | fputs("sub: incorrect distance\n", stderr);
|
---|
65 | exit(1);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /* initialize last byte */
|
---|
69 | i = n;
|
---|
70 | do {
|
---|
71 | a[--i] = 0;
|
---|
72 | } while (i);
|
---|
73 |
|
---|
74 | /* write differenced data */
|
---|
75 | putchar(MAGIC1); putchar(MAGIC2); /* magic word for add */
|
---|
76 | putchar(n & 0xff); /* so add knows what to do */
|
---|
77 | putchar((n>>8) & 0xff);
|
---|
78 |
|
---|
79 | while ((c = getchar()) != EOF)
|
---|
80 | {
|
---|
81 | putchar((c - a[i]) & 0xff); /* write difference */
|
---|
82 | a[i++] = c; /* save last byte */
|
---|
83 | if (i == n) /* cycle on n differences */
|
---|
84 | i = 0;
|
---|
85 | }
|
---|
86 | exit(0);
|
---|
87 | return 0; /* avoid warning */
|
---|
88 | }
|
---|