mapnik/test/visual/renderer.hpp

260 lines
8.3 KiB
C++
Raw Normal View History

2015-02-17 17:28:49 +01:00
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2015 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#ifndef RENDERER_HPP
#define RENDERER_HPP
#include "compare_images.hpp"
// stl
#include <sstream>
#include <iomanip>
2015-05-23 20:08:51 +02:00
#include <fstream>
2015-02-17 17:28:49 +01:00
// mapnik
#include <mapnik/map.hpp>
#include <mapnik/agg_renderer.hpp>
#if defined(GRID_RENDERER)
#include <mapnik/grid/grid_renderer.hpp>
#endif
2015-02-17 17:28:49 +01:00
#if defined(HAVE_CAIRO)
#include <mapnik/cairo/cairo_renderer.hpp>
#include <mapnik/cairo/cairo_image_util.hpp>
#endif
2015-05-23 20:08:51 +02:00
#if defined(SVG_RENDERER)
#include <mapnik/svg/output/svg_renderer.hpp>
#endif
2015-02-17 17:28:49 +01:00
// boost
#include <boost/filesystem.hpp>
namespace visual_tests
{
template <typename ImageType>
struct renderer_base
{
using image_type = ImageType;
2015-05-23 20:08:51 +02:00
static constexpr const char * ext = ".png";
2015-02-17 17:28:49 +01:00
unsigned compare(image_type const & actual, boost::filesystem::path const& reference) const
{
return compare_images(actual, reference.string());
}
void save(image_type const & image, boost::filesystem::path const& path) const
{
mapnik::save_to_file(image, path.string(), "png32");
}
};
struct agg_renderer : renderer_base<mapnik::image_rgba8>
{
static constexpr const char * name = "agg";
image_type render(mapnik::Map const & map, double scale_factor) const
{
image_type image(map.width(), map.height());
mapnik::agg_renderer<image_type> ren(map, image, scale_factor);
ren.apply();
return image;
}
};
#if defined(HAVE_CAIRO)
struct cairo_renderer : renderer_base<mapnik::image_rgba8>
{
static constexpr const char * name = "cairo";
image_type render(mapnik::Map const & map, double scale_factor) const
{
mapnik::cairo_surface_ptr image_surface(
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, map.width(), map.height()),
mapnik::cairo_surface_closer());
mapnik::cairo_ptr image_context(mapnik::create_context(image_surface));
mapnik::cairo_renderer<mapnik::cairo_ptr> ren(map, image_context, scale_factor);
ren.apply();
image_type image(map.width(), map.height());
mapnik::cairo_image_to_rgba8(image, image_surface);
return image;
}
};
#endif
2015-05-23 20:08:51 +02:00
#if defined(SVG_RENDERER)
struct svg_renderer : renderer_base<std::string>
{
static constexpr const char * name = "svg";
static constexpr const char * ext = ".svg";
image_type render(mapnik::Map const & map, double scale_factor) const
{
std::stringstream ss;
std::ostream_iterator<char> output_stream_iterator(ss);
mapnik::svg_renderer<std::ostream_iterator<char>> ren(map, output_stream_iterator, scale_factor);
ren.apply();
return ss.str();
}
unsigned compare(image_type const & actual, boost::filesystem::path const& reference) const
{
std::ifstream stream(reference.string().c_str(),std::ios_base::in|std::ios_base::binary);
if (!stream.is_open())
{
throw std::runtime_error("could not open: '" + reference.string() + "'");
}
std::string expected(std::istreambuf_iterator<char>(stream.rdbuf()),(std::istreambuf_iterator<char>()));
stream.close();
return std::max(actual.size(), expected.size()) - std::min(actual.size(), expected.size());
2015-05-23 20:08:51 +02:00
}
void save(image_type const & image, boost::filesystem::path const& path) const
{
std::ofstream file(path.string().c_str(), std::ios::out | std::ios::trunc | std::ios::binary);
if (!file) {
throw std::runtime_error((std::string("cannot open file for writing file ") + path.string()).c_str());
} else {
file << image;
file.close();
}
}
};
#endif
#if defined(GRID_RENDERER)
struct grid_renderer : renderer_base<mapnik::image_rgba8>
2015-02-17 17:28:49 +01:00
{
static constexpr const char * name = "grid";
void convert(mapnik::grid::data_type const & grid, image_type & image) const
{
for (std::size_t y = 0; y < grid.height(); ++y)
{
mapnik::grid::value_type const * grid_row = grid.get_row(y);
image_type::pixel_type * image_row = image.get_row(y);
for (std::size_t x = 0; x < grid.width(); ++x)
{
mapnik::grid::value_type val = grid_row[x];
if (val == mapnik::grid::base_mask)
{
image_row[x] = 0;
continue;
}
if (val < 0)
{
throw std::runtime_error("grid renderer: feature id is negative.");
}
val *= 100;
if (val > 0x00ffffff)
{
throw std::runtime_error("grid renderer: feature id is too high.");
}
image_row[x] = val | 0xff000000;
}
}
}
2015-02-17 17:28:49 +01:00
image_type render(mapnik::Map const & map, double scale_factor) const
{
mapnik::grid grid(map.width(), map.height(), "__id__");
mapnik::grid_renderer<mapnik::grid> ren(map, grid, scale_factor);
ren.apply();
2015-02-17 17:28:49 +01:00
image_type image(map.width(), map.height());
convert(grid.data(), image);
2015-02-17 17:28:49 +01:00
return image;
}
};
#endif
2015-02-17 17:28:49 +01:00
template <typename Renderer>
class renderer
{
public:
renderer(boost::filesystem::path const & _output_dir, boost::filesystem::path const & _reference_dir, bool _overwrite)
: ren(), output_dir(_output_dir), reference_dir(_reference_dir), overwrite(_overwrite)
2015-02-17 17:28:49 +01:00
{
}
result test(std::string const & name, mapnik::Map const & map, double scale_factor) const
{
typename Renderer::image_type image(ren.render(map, scale_factor));
2015-05-23 20:08:51 +02:00
boost::filesystem::path reference = reference_dir / image_file_name(name, map.width(), map.height(), scale_factor, true, Renderer::ext);
2015-05-19 19:11:44 +02:00
bool reference_exists = boost::filesystem::exists(reference);
2015-02-17 17:28:49 +01:00
result res;
res.state = reference_exists ? STATE_OK : STATE_OVERWRITE;
2015-02-17 17:28:49 +01:00
res.name = name;
res.renderer_name = Renderer::name;
res.scale_factor = scale_factor;
res.size = map_size(map.width(), map.height());
res.reference_image_path = reference;
res.diff = reference_exists ? ren.compare(image, reference) : 0;
2015-02-17 17:28:49 +01:00
if (res.diff)
{
2015-05-19 19:11:44 +02:00
boost::filesystem::create_directories(output_dir);
2015-05-23 20:08:51 +02:00
boost::filesystem::path path = output_dir / image_file_name(name, map.width(), map.height(), scale_factor, false, Renderer::ext);
2015-02-17 17:28:49 +01:00
res.actual_image_path = path;
res.state = STATE_FAIL;
ren.save(image, path);
}
2015-02-17 17:28:49 +01:00
if ((res.diff && overwrite) || !reference_exists)
{
ren.save(image, reference);
res.state = STATE_OVERWRITE;
2015-02-17 17:28:49 +01:00
}
return res;
}
private:
std::string image_file_name(std::string const & test_name,
double width,
double height,
double scale_factor,
2015-05-23 20:08:51 +02:00
bool reference,
std::string const& ext) const
2015-02-17 17:28:49 +01:00
{
std::stringstream s;
s << test_name << '-' << width << '-' << height << '-'
<< std::fixed << std::setprecision(1) << scale_factor
2015-05-23 20:08:51 +02:00
<< '-' << Renderer::name << (reference ? "-reference" : "") << ext;
2015-02-17 17:28:49 +01:00
return s.str();
}
2015-05-25 18:41:21 +02:00
const Renderer ren;
2015-02-17 17:28:49 +01:00
boost::filesystem::path const & output_dir;
boost::filesystem::path const & reference_dir;
const bool overwrite;
};
}
#endif