'Render this image stack.
Given a coordinate, return an output image with the results of all the
layers in this stack pasted on in turn.
Final argument is a dictionary used to temporarily cache results
of layers retrieved from layer_bitmap(), to speed things up in case
of repeatedly-used identical images.'

def draw_stack(self, coord, tiles):

    rendered = Blit.Color(0, 0, 0, 0)
    for layer in self.stack:
        if (('zoom' in layer) and (not in_zoom(coord, layer['zoom']))):
            continue
        (source_name, mask_name, color_name) = [layer.get(k, None) for k in ('src', 'mask', 'color')]
        if (source_name and color_name and mask_name):
            raise Core.KnownUnknown(("You    can't    specify    src,    color    and    mask    together    in    a    Sandwich    Layer:    %s,    %s,    %s" % (repr(source_name), repr(color_name), repr(mask_name))))
        if (source_name and (source_name not in tiles)):
            if (source_name in self.config.layers):
                tiles[source_name] = layer_bitmap(self.config.layers[source_name], coord)
            else:
                tiles[source_name] = local_bitmap(source_name, self.config, coord, self.layer.dim)
        if (mask_name and (mask_name not in tiles)):
            tiles[mask_name] = layer_bitmap(self.config.layers[mask_name], coord)
        if (source_name and color_name):
            foreground = make_color(color_name).blend(tiles[source_name])
        elif source_name:
            foreground = tiles[source_name]
        elif color_name:
            foreground = make_color(color_name)
        elif mask_name:
            raise Core.KnownUnknown(('You    have    to    provide    more    than    just    a    mask    to    Sandwich    Layer:    %s' % repr(mask_name)))
        else:
            raise Core.KnownUnknown('You    have    to    provide    at    least    some    combination    of    src,    color    and    mask    to    Sandwich    Layer')
        for (name, args) in layer.get('adjustments', []):
            adjustfunc = adjustment_names.get(name)(*args)
            foreground = foreground.adjust(adjustfunc)
        opacity = float(layer.get('opacity', 1.0))
        blendfunc = blend_modes.get(layer.get('mode', None), None)
        if mask_name:
            rendered = rendered.blend(foreground, tiles[mask_name], opacity, blendfunc)
        else:
            rendered = rendered.blend(foreground, None, opacity, blendfunc)
    return rendered
