/** layerToImage
  *
  * Converts a layer into an ImageMagick Image
  */
int HandlerUtils::LayerToImage(const Map& map, const Layer& layer, Magick::Image& image)
{
    std::vector<Magick::Image> tiles;

    if (HandlerUtils::GetTiles(map, tiles))
        return -1;

    Magick::Color color = Magick::ColorRGB(0, 0, 0);
    color.alpha(0);
    int width = map.GetWidth() * map.GetTileWidth();
    int height = map.GetHeight() * map.GetTileHeight();
    image.matte(true);
    image.resize(Magick::Geometry(width, height));
    image.backgroundColor(color);
    image.erase();

    return HandlerUtils::LayerToImage(map, layer, tiles, image);
}
/** MapToImage
  *
  * Converts a map into an ImageMagick Image
  */
int HandlerUtils::MapToImage(const Map& map, Magick::Image& image)
{
    std::vector<Magick::Image> tiles;

    if (HandlerUtils::GetTiles(map, tiles))
        return -1;

    Magick::Color color = Magick::ColorRGB(0, 0, 0);
    color.alpha(0);

    const Tileset& tileset = map.GetTileset();
    uint32_t tile_width, tile_height;
    tileset.GetTileDimensions(tile_width, tile_height);

    int width = map.GetWidth() * tile_width;
    int height = map.GetHeight() * tile_height;

    image.matte(true);
    image.resize(Magick::Geometry(width, height));
    image.backgroundColor(color);
    image.erase();

    try
    {
        for (const auto& layer : map.GetLayers())
        {
            if (HandlerUtils::LayerToImage(map, layer, tiles, image))
                return -1;
        }
    }
    catch (Magick::Exception& error_)
    {
        return -1;
    }

    return 0;
}