File: flood.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (233 lines) | stat: -rw-r--r-- 5,650 bytes parent folder | download | duplicates (3)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "ags/lib/allegro/flood.h"
#include "ags/lib/allegro/gfx.h"
#include "ags/shared/core/types.h"
#include "common/array.h"

namespace AGS3 {

struct FLOODED_LINE {
	short flags;                  /* status of the segment */
	short lpos, rpos;             /* left and right ends of segment */
	short y;                      /* y coordinate of the segment */
	int next;                     /* linked list if several per line */
};

#define FLOOD_IN_USE             1
#define FLOOD_TODO_ABOVE         2
#define FLOOD_TODO_BELOW         4

#define FLOOD_LINE(c)            (&scratchMem[c])

template<class SIZE>
static bool scanLine(BITMAP *bmp, int x, int y, int src_color, int &left, int &right) {
	// check start pixel
	const SIZE *pixel = (const SIZE *)bmp->getBasePtr(x, y);
	if (*pixel != src_color)
		return false;

	// Work left from starting point
	const SIZE *pixelLeft = pixel - 1;
	for (left = x - 1; left >= bmp->cl; left--, pixelLeft--) {
		if (*pixelLeft != src_color)
			break;
	}

	// Work right from starting point
	const SIZE *pixelRight = pixel + 1;
	for (right = x + 1; right < bmp->cr; right++, pixelRight++) {
		if (*pixelRight != src_color)
			break;
	}

	return true;
}

/**
 *  Fills a horizontal line around the specified position, and adds it
 *  to the list of drawn segments. Returns the first x coordinate after
 *  the part of the line which it has dealt with.
 */
static int flooder(BITMAP *bmp, int x, int y, int src_color, int dest_color,
                   Common::Array<FLOODED_LINE> &scratchMem) {
	FLOODED_LINE *p;
	int left = 0, right = 0;
	int c;

	assert(bmp);

	bool ret;
	switch (bmp->format.bytesPerPixel) {
	case 1:
		ret = scanLine<byte>(bmp, x, y, src_color, left, right);
		break;
	case 2:
		ret = scanLine<byte>(bmp, x, y, src_color, left, right);
		break;
	case 4:
		ret = scanLine<byte>(bmp, x, y, src_color, left, right);
		break;
	default:
		error("Unknown format");
	}
	if (!ret)
		return x + 1;

	left++;
	right--;

	/* draw the line */
	bmp->hLine(left, y, right, dest_color);

	/* store it in the list of flooded segments */
	c = y;
	p = FLOOD_LINE(c);

	if (p->flags) {
		while (p->next) {
			c = p->next;
			p = FLOOD_LINE(c);
		}

		p->next = c = scratchMem.size();
		scratchMem.resize(scratchMem.size() + 1);
		p = FLOOD_LINE(c);
	}

	p->flags = FLOOD_IN_USE;
	p->lpos = left;
	p->rpos = right;
	p->y = y;
	p->next = 0;

	if (y > bmp->ct)
		p->flags |= FLOOD_TODO_ABOVE;

	if (y + 1 < bmp->cb)
		p->flags |= FLOOD_TODO_BELOW;

	return right + 2;
}



/**
 *  Checks a line segment, using the scratch buffer is to store a list of
 *  segments which have already been drawn in order to minimise the required
 *  number of tests.
 */
static bool check_flood_line(BITMAP *bmp, int y, int left, int right, int src_color, int dest_color,
                             Common::Array<FLOODED_LINE> &scratchMem) {
	int c;
	FLOODED_LINE *p;
	bool ret = false;

	while (left <= right) {
		c = y;

		for (;;) {
			p = FLOOD_LINE(c);

			if ((left >= p->lpos) && (left <= p->rpos)) {
				left = p->rpos + 2;
				break;
			}

			c = p->next;

			if (!c) {
				left = flooder(bmp, left, y, src_color, dest_color, scratchMem);
				ret = true;
				break;
			}
		}
	}

	return ret;
}

void floodfill(BITMAP *bmp, int x, int y, int color) {
	int src_color;
	int c;
	bool done;
	FLOODED_LINE *p;
	Common::Array<FLOODED_LINE> scratchMem;

	/* make sure we have a valid starting point */
	if ((x < bmp->cl) || (x >= bmp->cr) || (y < bmp->ct) || (y >= bmp->cb))
		return;

	/* what color to replace? */
	src_color = getpixel(bmp, x, y);
	if (src_color == color) {
		return;
	}

	/* set up the list of flooded segments */
	scratchMem.resize(bmp->cb);

	p = FLOOD_LINE(0);
	for (c = 0; c < (int)scratchMem.size(); c++) {
		p[c].flags = 0;
		p[c].lpos = INT16_MAX;
		p[c].rpos = INT16_MIN;
		p[c].y = y;
		p[c].next = 0;
	}

	/* start up the flood algorithm */
	flooder(bmp, x, y, src_color, color, scratchMem);

	/* continue as long as there are some segments still to test */
	do {
		done = true;

		/* for each line on the screen */
		for (c = 0; c < (int)scratchMem.size(); c++) {
			p = FLOOD_LINE(c);

			/* check below the segment? */
			if (p->flags & FLOOD_TODO_BELOW) {
				p->flags &= ~FLOOD_TODO_BELOW;
				if (check_flood_line(bmp, p->y + 1, p->lpos, p->rpos, src_color, color, scratchMem)) {
					done = false;
					p = FLOOD_LINE(c);
				}
			}

			/* check above the segment? */
			if (p->flags & FLOOD_TODO_ABOVE) {
				p->flags &= ~FLOOD_TODO_ABOVE;
				if (check_flood_line(bmp, p->y - 1, p->lpos, p->rpos, src_color, color, scratchMem)) {
					done = false;
					/* special case shortcut for going backwards */
					if ((c < bmp->cb) && (c > 0))
						c -= 2;
				}
			}
		}

	} while (!done);
}

} // namespace AGS3