File: SLDiff.c

package info (click to toggle)
acm4 4.7-18
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,776 kB
  • ctags: 1,621
  • sloc: ansic: 16,777; makefile: 364; sh: 31
file content (319 lines) | stat: -rw-r--r-- 6,556 bytes parent folder | download | duplicates (6)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <Alib.h>
#include <stdio.h>

extern char * malloc();

#ifdef X11

void AX11AllocBufferedSegments ();

#ifdef X11_DRAW_SEGMENTS
#define  OutputSegment(w, yp, x0p, x1p, c) \
	{ \
		register int		i; \
		register XSegment	*p; \
 \
		if (w->bseg[c].count == -1) \
			AX11AllocBufferedSegments (w, c); \
		i = ++(w->bseg[c].count); \
		p = &(w->bseg[c].head[i-1]); \
		p->x1 = x0p; \
		p->x2 = x1p; \
		p->y1 = p->y2 = yp; \
		if (i == w->bsegSize) { \
			if (w->flags & AMonoMode) \
				XSetStipple (w->display, w->gc, \
					w->stipple[c]); \
			else \
				XSetForeground (w->display, w->gc, c); \
			p = w->bseg[c].head; \
			XDrawSegments (w->display, w->d, w->gc, p, \
				w->bsegSize); \
			w->bseg[c].count = 0; \
		} \
	}
#else
#define  OutputSegment(w, yp, x0p, x1p, c) \
	{ \
		register int		i; \
		register XRectangle	*p; \
 \
		if (w->bseg[c].count == -1) \
			AX11AllocBufferedSegments (w, c); \
		i = ++(w->bseg[c].count); \
		p = &(w->bseg[c].head[i-1]); \
		p->x = x0p; \
		p->width = x1p - x0p + 1; \
		p->y= yp; \
		p->height = 1; \
		if (i == w->bsegSize) { \
			if (w->flags & AMonoMode) \
				XSetStipple (w->display, w->gc, \
					w->stipple[c]); \
			else \
				XSetForeground (w->display, w->gc, c); \
			p = w->bseg[c].head; \
			XFillRectangles (w->display, w->d, w->gc, p, \
				w->bsegSize); \
			w->bseg[c].count = 0; \
		} \
	}
#endif

void AX11FlushBufferedSegments (w)
AWindow	*w; {

	register int	i;

	for (i=0; i<AMaxPixelValue; ++i) {
		if (w->bseg[i].count > 0) {
			if (w->flags & AMonoMode) {
				XSetStipple (w->display, w->gc,
					w->stipple[i]);
			}
			else {
				XSetForeground (w->display, w->gc, i);
			}
#ifdef X11_DRAW_SEGMENTS
			XDrawSegments (w->display, w->d, w->gc,
				w->bseg[i].head, w->bseg[i].count);
#else
			XFillRectangles (w->display, w->d, w->gc,
				w->bseg[i].head, w->bseg[i].count);
#endif
			w->bseg[i].count = 0;
		}
	}
}

void AX11AllocBufferedSegments (w, c)
AWindow	*w;
Color	c; {

#ifdef X11_DRAW_SEGMENTS
	w->bseg[c].head = (XSegment *) malloc (w->bsegSize *
		sizeof (XSegment));
	if (w->bseg[c].head == (XSegment *) NULL) {
		fprintf (stderr, "Error allocating A library buffered segments\n");
		exit (1);
	}
#else
	w->bseg[c].head = (XRectangle *) malloc (w->bsegSize *
		sizeof (XRectangle));
	if (w->bseg[c].head == (XRectangle *) NULL) {
		fprintf (stderr, "Error allocating A library buffered segments\n");
		exit (1);
	}
#endif
	w->bseg[c].count = 0;

}

#endif

#ifdef DEBUG
void printScanLine (s, count)
ColorSegment	*s;
int		count; {

	for ( ; count > 0; --count, ++s)
		printf ("[%d %d %d ] ", s->x, s->length, s->color);

	printf ("\n");
}
#endif

void ScanLineDifference (w)
AWindow	*w; {

	register ColorSegment	*cur, *last;
	register int	len, curX = 0, curLength = 0, curCount, lastCount, y;

	for (y=0; y<w->height; ++y) {

	cur = w->scanLine[y].head;
	curCount = w->scanLine[y].count;

	if (curCount > 0) {
		curX = cur->x;
		curLength = cur->length;
	}

	last = w->lastScanLine[y].head;
	lastCount = w->lastScanLine[y].count;

#ifdef DEBUG
	if (curCount > 1 || lastCount > 1) {
		printf ("\ny = %d\nLast scan line:\n", y);
		printScanLine (last, lastCount);
		printf ("Current scan line:\n");
		printScanLine (cur, curCount);
	}
#endif

	while (curCount != 0) {

		if (lastCount == 0) {
#ifdef DEBUG
			printf ("row: %d, %d to %d color %d\n",
				y, curX, curX + curLength - 1,
				cur->color);
#endif
#ifdef HAS_FRAME_BUFFER
			FrameBufferRun (curX, y, curLength,
				cur->color);
#else
			OutputSegment (w, y, curX, curX + curLength - 1,
				cur->color);
#endif
			++ cur;
			curX = cur->x;
			curLength = cur->length;
			-- curCount;
		}

		else if (curX == last->x) {

			if (curLength == last->length) {

/*
 *  The trivial case:  Both segments have the same length.  If the colors are
 *  different, then output the correct new color.
 */

				if (cur->color != last->color) {
#ifdef HAS_FRAME_BUFFER
					FrameBufferRun (curX, y, curLength,
						cur->color);
#else
					OutputSegment (w, y, curX,
						curX + curLength - 1,
						cur->color);
#endif
#ifdef DEBUG
					printf ("row: %d, %d to %d color %d\n",
						y, curX,
						curX + curLength - 1,
						cur->color);
#endif
				}
				++ last;
				++ cur;
				curX = cur->x;
				curLength = cur->length;
				-- curCount;
				-- lastCount;
			}

			else if (curLength < last->length) {

/*
 *  The current segment is shorter than the last one.  Output a new line segment
 *  if the colors differ; delete the current segment and update the last one's
 *  x value and length.
 */
				if (cur->color != last->color) {
#ifdef HAS_FRAME_BUFFER
					FrameBufferRun (curX, y, curLength,
						cur->color);
#else
					OutputSegment (w, y, curX,
						curX + curLength - 1,
						cur->color);
#endif
#ifdef DEBUG
					printf ("row: %d, %d to %d color %d\n",
						y, curX,
						curX + curLength - 1,
						cur->color);
#endif
				}
				last->x += curLength;
				last->length -= curLength;
				++ cur;
				curX = cur->x;
				curLength = cur->length;
				-- curCount;
			}

/*
 *  The current segment is longer than the last one.  Output, if necessary;
 *  delete the last segment and update the current segment's x value and length.
 */

			else {
				if (cur->color != last->color) {
#ifdef HAS_FRAME_BUFFER
					FrameBufferRun (curX, y, last->length,
						cur->color);
#else
					OutputSegment (w, y, curX,
						curX + last->length - 1,
						cur->color);
#endif
#ifdef DEBUG
					printf ("row: %d, %d to %d color %d\n",
						y, curX,
						curX + lastLength - 1,
						cur->color);
#endif
				}
				curX += last->length;
				curLength -= last->length;
				++ last;
				-- lastCount;
			}

		}

/*
 *  The current segment starts before any previous segments.
 */

		else if (curX < last->x) {
			if (curX + curLength > last->x)
				len = last->x - curX;
			else
				len = curLength;

#ifdef HAS_FRAME_BUFFER
			FrameBufferRun (curX, y, len,
				cur->color);
#else
			OutputSegment (w, y, curX, curX + len - 1,
				cur->color);
#endif
#ifdef DEBUG
			printf ("row: %d, %d to %d color %d\n",
				y, curX, curX + len - 1, cur->color);
#endif
			curX += len;
			curLength -= len;
			if (curLength == 0) {
				++ cur;
				curX = cur->x;
				curLength = cur->length;
				-- curCount;
			}
		}

/*
 *  The previous segment begins before the first current segment.
 */

		else {
			if (last->x + last->length > curX)
				len = curX - last->x;
			else
				len = last->length;

			last->x += len;
			last->length -= len;
			if (last->length == 0) {
				++ last;
				-- lastCount;
			}
		}
	}
	} /* for (y=... */
}