File: srle.c

package info (click to toggle)
gs 5.10-10.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 14,960 kB
  • ctags: 25,299
  • sloc: ansic: 164,376; makefile: 3,020; cpp: 2,237; sh: 1,219; asm: 684; tcl: 434; perl: 56
file content (199 lines) | stat: -rw-r--r-- 5,117 bytes parent folder | download | duplicates (2)
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
/* Copyright (C) 1995, 1996, 1997 Aladdin Enterprises.  All rights reserved.
  
  This file is part of GNU Ghostscript.
  
  GNU Ghostscript is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility to
  anyone for the consequences of using it or for whether it serves any
  particular purpose or works at all, unless he says so in writing.  Refer to
  the GNU General Public License for full details.
  
  Everyone is granted permission to copy, modify and redistribute GNU
  Ghostscript, but only under the conditions described in the GNU General
  Public License.  A copy of this license is supposed to have been given to
  you along with GNU Ghostscript so you can know your rights and
  responsibilities.  It should be in a file named COPYING.  Among other
  things, the copyright notice and this notice must be preserved on all
  copies.
  
  Aladdin Enterprises is not affiliated with the Free Software Foundation or
  the GNU Project.  GNU Ghostscript, as distributed by Aladdin Enterprises,
  does not depend on any other GNU software.
*/

/* srle.c */
/* RunLengthEncode filter */
#include "stdio_.h"		/* includes std.h */
#include "memory_.h"
#include "strimpl.h"
#include "srlx.h"

/* ------ RunLengthEncode ------ */

private_st_RLE_state();

#define ss ((stream_RLE_state *)st)

/* Set defaults */
private void
s_RLE_set_defaults(stream_state *st)
{	s_RLE_set_defaults_inline(ss);
}

/* Initialize */
private int
s_RLE_init(stream_state *st)
{	return s_RLE_init_inline(ss);
}

/* Process a buffer */
private int
s_RLE_process(stream_state *st, stream_cursor_read *pr,
  stream_cursor_write *pw, bool last)
{	register const byte *p = pr->ptr;
	register byte *q = pw->ptr;
	const byte *rlimit = pr->limit;
	byte *wlimit = pw->limit;
	int status = 0;
	ulong rleft = ss->record_left;

	/*
	 * We thought that the Genoa CET demands that the output from this
	 * filter be not just legal, but optimal, so we went to the trouble
	 * of ensuring this.  It turned out that this wasn't actually
	 * necessary, but we didn't want to change the code back.
	 *
	 * For optimal output, we can't just break runs at buffer
	 * boundaries: unless we hit a record boundary or the end of the
	 * input, we have to look ahead far enough to know that we aren't
	 * breaking a run prematurely.
	 */

	/* Check for leftover output. */
copy:	if ( ss->copy_left )
	  { uint rcount = rlimit - p;
	    uint wcount = wlimit - q;
	    uint count = ss->copy_left;

	    if ( rcount < count )
	      count = rcount;
	    if ( wcount < count )
	      count = wcount;
	    if ( rleft < count )
	      count = rleft;
	    memcpy(q + 1, p + 1, count);
	    pr->ptr = p += count;
	    pw->ptr = q += count;
	    if ( (ss->record_left = rleft -= count) == 0 )
	      ss->record_left = rleft = ss->record_size;
	    if ( (ss->copy_left -= count) != 0 )
	      return (rcount == 0 ? 0 : 1);
	  }
	while ( p < rlimit )
	  {	const byte *beg = p;
		const byte *p1;
		uint count = rlimit - p;
		bool end = last;
		byte next;

		if ( count > rleft )
		  count = rleft, end = true;
		if ( count > 128 )
		  count = 128, end = true;
		p1 = p + count - 1;
		if ( count < 3 )
		  { if ( !end || count == 0 )
		      break;	/* can't look ahead far enough */
		    if ( count == 1 )
		      { if ( wlimit - q < 2 )
			  { status = 1;
			    break;
			  }
			*++q = 0;
		      }
		    else	/* count == 2 */
		      { if ( p[1] == p[2] )
			  { if ( wlimit - q < 2 )
			      { status = 1;
			        break;
			      }
			    *++q = 255;
			  }
			else
			  { if ( wlimit - q < 3 )
			      { status = 1;
			        break;
			      }
			    *++q = 1;
			    *++q = p[1];
			  }
		      }
		    *++q = p1[1];
		    p = p1 + 1;
		  }
		else if ( (next = p[1]) == p[2] && next == p[3] )
		  {	if ( wlimit - q < 2 )
			  { status = 1;
			    break;
			  }
			/* Recognize leading repeated byte */
			do { p++; }
			while ( p < p1 && p[2] == next );
			if ( p == p1 && !end )
			  { p = beg; /* need to look ahead further */
			    break;
			  }
			p++;
			*++q = (byte)(257 - (p - beg));
			*++q = next;
		  }
		else
		  {	p1--;
			while ( p < p1 && (p[2] != p[1] || p[3] != p[1]) )
			  p++;
			if ( p == p1 )
			  { if ( !end )
			      { p = beg; /* need to look ahead further */
			        break;
			      }
			    p += 2;
			  }
			count = p - beg;
			if ( wlimit - q < count + 1 )
			  {	p = beg;
				if ( q >= wlimit )
				  { status = 1;
				    break;
				  }
				/* Copy some now and some later. */
				*++q = count - 1;
				ss->copy_left = count;
				goto copy;
			  }
			*++q = count - 1;
			memcpy(q + 1, beg + 1, count);
			q += count;
		  }
		rleft -= p - beg;
		if ( rleft == 0 )
		  rleft = ss->record_size;
	  }
	if ( last && status == 0 && ss->EndOfData )
	  { if ( q < wlimit )
	      *++q = 128;
	    else
	      status = 1;
	  }
	pr->ptr = p;
	pw->ptr = q;
	ss->record_left = rleft;
	return status;
}

#undef ss

/* Stream template */
const stream_template s_RLE_template =
{	&st_RLE_state, s_RLE_init, s_RLE_process, 128, 2, NULL,
	s_RLE_set_defaults, s_RLE_init
};