// Abstract base class for tiling an image (G22Tiling)
abstract srecord noeq G22AbstractTiler
(Img image) is Steppable {
delegate Tile to G22tiling.
int w, h, runner;
gettable int size; // =w*h
// iterator that provides points to be looked at
// (e.g. a WeightlessShuffledIterator)
IndexIterator pointIterator;
// Temporary stack of points to be looked at
// (e.g. neighbors of a tile just discovered).
// May or may not be used.
new IntBuffer stack;
G22Tiling
tiling; // the tiling we are making
int tileCounter;
bool verbose;
// The one method subclasses need to override
abstract int getColor(int pos);
// calculations for positions (pixel index)
int x(int pos) { ret pos % w; }
int y(int pos) { ret pos / w; }
int pos(int x, int y) { ret y*w+x; }
Pt pt(int pos) { ret Pt(x(pos), y(pos)); }
bool validPos(int x, int y) { ret x >= 0 && y >= 0 && x < w && y < h; }
run { stepAll(this); }
public bool step() {
init();
int pos = pointIterator.nextIndex();
if (pos < 0) false;
// Is point already covered by a tile?
if (tiling.tileMatrix[pos] != 0) true;
// Create new tile
int x = x(pos), y = y(pos);
int iTile = tileCounter++;
new Tile tile;
tile.position = rect(x, y, 1, 1);
tiling.tiles.add(tile);
tiling.tileMatrix[pos] = iTile+1;
true;
}
void init {
if (tiling != null) ret;
w = image.getWidth(); h = image.getHeight();
size = w*h;
tiling = new G22Tiling(image);
tiling.initTileMatrix();
pointIterator = WeightlessShuffledIterator(size);
}
G22Tiling
get() {
if (tiling == null) run();
ret tiling;
}
}