summaryrefslogtreecommitdiff
path: root/filter/rle.c
blob: 531971393e5a1a9509b43835d4c4487d98c73991 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <fitz.h>

/* TODO: rewrite!
 * make it non-optimal or something,
 * just not this horrid mess...
 */

#define noDEBUG

typedef struct fz_rle_s fz_rle;

struct fz_rle_s
{
	fz_filter super;
	int reclen;
	int curlen;
	int state;
	int run;
	unsigned char buf[128];
};

enum {
	ZERO,
	ONE,
	DIFF,
	SAME,
	END
};

fz_error *
fz_newrle(fz_filter **fp, fz_obj *params)
{
	FZ_NEWFILTER(fz_rle, enc, rle);

	if (params)
		enc->reclen = fz_toint(params);
	else
		enc->reclen = 0;

	enc->curlen = 0;
	enc->state = ZERO;
	enc->run = 0;

	return nil;
}

void
fz_droprle(fz_filter *enc)
{
}

static fz_error *
putone(fz_rle *enc, fz_buffer *in, fz_buffer *out)
{
	if (out->wp + 2 >= out->ep)
		return fz_ioneedout;

#ifdef DEBUG
fprintf(stderr, "one '%c'\n", enc->buf[0]);
#endif

	*out->wp++ = 0;
	*out->wp++ = enc->buf[0];

	return nil;
}

static fz_error *
putsame(fz_rle *enc, fz_buffer *in, fz_buffer *out)
{
	if (out->wp + enc->run >= out->ep)
		return fz_ioneedout;

#ifdef DEBUG
fprintf(stderr, "same %d x '%c'\n", enc->run, enc->buf[0]);
#endif

	*out->wp++ = 257 - enc->run;
	*out->wp++ = enc->buf[0];
	return nil;
}

static fz_error *
putdiff(fz_rle *enc, fz_buffer *in, fz_buffer *out)
{
	int i;
	if (out->wp + enc->run >= out->ep)
		return fz_ioneedout;

#ifdef DEBUG
fprintf(stderr, "diff %d\n", enc->run);
#endif

	*out->wp++ = enc->run - 1;
	for (i = 0; i < enc->run; i++)
		*out->wp++ = enc->buf[i];
	return nil;
}

static fz_error *
puteod(fz_rle *enc, fz_buffer *in, fz_buffer *out)
{
	if (out->wp + 1 >= out->ep)
		return fz_ioneedout;

#ifdef DEBUG
fprintf(stderr, "eod\n");
#endif

	*out->wp++ = 128;
	return nil;
}

static fz_error *
savebuf(fz_rle *enc, fz_buffer *in, fz_buffer *out)
{
	switch (enc->state)
	{
		case ZERO: return nil;
		case ONE: return putone(enc, in, out);
		case SAME: return putsame(enc, in, out);
		case DIFF: return putdiff(enc, in, out);
		case END: return puteod(enc, in, out);
		default: assert(!"invalid state in rle"); return nil;
	}
}

fz_error *
fz_processrle(fz_filter *filter, fz_buffer *in, fz_buffer *out)
{
	fz_rle *enc = (fz_rle*)filter;
	fz_error *err;
	unsigned char c;

	while (1)
	{

		if (enc->reclen && enc->curlen == enc->reclen) {
			err = savebuf(enc, in, out);
			if (err) return err;
#ifdef DEBUG
fprintf(stderr, "--record--\n");
#endif
			enc->state = ZERO;
			enc->curlen = 0;
		}

		if (in->rp == in->wp) {
			if (in->eof) {
				if (enc->state != END) {
					err = savebuf(enc, in, out);
					if (err) return err;
				}
				enc->state = END;
			}
			else
				return fz_ioneedin;
		}

		c = *in->rp;

		switch (enc->state)
		{
		case ZERO:
			enc->state = ONE;
			enc->run = 1;
			enc->buf[0] = c;
			break;

		case ONE:
			enc->state = DIFF;
			enc->run = 2;
			enc->buf[1] = c;
			break;

		case DIFF:
			/* out of space */
			if (enc->run == 128) {
				err = putdiff(enc, in, out);
				if (err) return err;

				enc->state = ONE;
				enc->run = 1;
				enc->buf[0] = c;
			}

			/* run of three that are the same */
			else if ((enc->run > 1) &&
				(c == enc->buf[enc->run - 1]) &&
				(c == enc->buf[enc->run - 2]))
			{
				if (enc->run >= 3) {
					enc->run -= 2;	/* skip prev two for diff run */
					err = putdiff(enc, in, out);
					if (err) return err;
				}

				enc->state = SAME;
				enc->run = 3;
				enc->buf[0] = c;
			}

			/* keep on collecting */
			else {
				enc->buf[enc->run++] = c;
			}
			break;

		case SAME:
			if (enc->run == 128 || c != enc->buf[0]) {
				err = putsame(enc, in, out);
				if (err) return err;

				enc->state = ONE;
				enc->run = 1;
				enc->buf[0] = c;
			}
			else {
				enc->run ++;
			}
			break;

		case END:
			err = puteod(enc, in, out);
			if (err) return err;
			
			out->eof = 1;
			return fz_iodone;
		}

		in->rp ++;

		enc->curlen ++;

	}
}