From ed306d7cbebb339e124710ce74a698ce15bfa4b9 Mon Sep 17 00:00:00 2001 From: Robert Coup Date: Wed, 26 Jan 2011 01:19:01 +0000 Subject: [PATCH] #654 add svg2png utility --- SConstruct | 1 + utils/svg2png/SConscript | 56 +++++++++++++ utils/svg2png/svg2png.cpp | 172 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 229 insertions(+) create mode 100644 utils/svg2png/SConscript create mode 100644 utils/svg2png/svg2png.cpp diff --git a/SConstruct b/SConstruct index 4f76f7f7c..00a896bcc 100644 --- a/SConstruct +++ b/SConstruct @@ -1390,6 +1390,7 @@ if not HELP_REQUESTED: # Build shapeindex and remove its dependency from the LIBS if 'boost_program_options%s' % env['BOOST_APPEND'] in env['LIBS']: SConscript('utils/shapeindex/SConscript') + SConscript('utils/svg2png/SConscript') env['LIBS'].remove('boost_program_options%s' % env['BOOST_APPEND']) else : color_print(1,"WARNING: Cannot find boost_program_options. 'shapeindex' won't be available") diff --git a/utils/svg2png/SConscript b/utils/svg2png/SConscript new file mode 100644 index 000000000..b31d056a5 --- /dev/null +++ b/utils/svg2png/SConscript @@ -0,0 +1,56 @@ +# +# This file is part of Mapnik (c++ mapping toolkit) +# +# Copyright (C) 2006 Artem Pavlenko, Jean-Francois Doyon +# +# Mapnik 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 +# +# $Id$ + +import glob + +Import ('env') + +prefix = env['PREFIX'] +install_prefix = env['DESTDIR'] + '/' + prefix + +program_env = env.Clone() + +source = Split( + """ + svg2png.cpp + """ + ) + +headers = env['CPPPATH'] + +boost_program_options = 'boost_program_options%s' % env['BOOST_APPEND'] +boost_iostreams = 'boost_iostreams%s' % env['BOOST_APPEND'] +boost_filesystem = 'boost_filesystem%s' % env['BOOST_APPEND'] +libraries = [boost_program_options,boost_iostreams,boost_filesystem,'mapnik2'] + +boost_system = 'boost_system%s' % env['BOOST_APPEND'] + +if env['HAS_BOOST_SYSTEM']: + libraries.append(boost_system) + + +svg2png = program_env.Program('svg2png', source, CPPPATH=headers, LIBS=libraries, LINKFLAGS=env['CUSTOM_LDFLAGS']) + +if 'uninstall' not in COMMAND_LINE_TARGETS: + env.Install(install_prefix + '/bin', svg2png) + env.Alias('install', install_prefix + '/bin') + +env['create_uninstall_target'](env, install_prefix + '/bin/' + 'svg2png') \ No newline at end of file diff --git a/utils/svg2png/svg2png.cpp b/utils/svg2png/svg2png.cpp new file mode 100644 index 000000000..c429ac0fb --- /dev/null +++ b/utils/svg2png/svg2png.cpp @@ -0,0 +1,172 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2010 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 + * + *****************************************************************************/ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "agg_rasterizer_scanline_aa.h" +#include "agg_basics.h" +#include "agg_rendering_buffer.h" +#include "agg_renderer_base.h" +#include "agg_pixfmt_rgba.h" +#include "agg_scanline_u.h" + + +int main (int argc,char** argv) +{ + namespace po = boost::program_options; + + bool verbose=false; + std::string svg; + + try + { + po::options_description desc("svg2png utility"); + desc.add_options() + ("help,h", "produce usage message") + ("version,V","print version string") + ("verbose,v","verbose output") + ("svg",po::value(),"svg file to read") + ; + + po::positional_options_description p; + p.add("svg",-1); + po::variables_map vm; + po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); + po::notify(vm); + + if (vm.count("version")) + { + std::clog<<"version 0.3.0" << std::endl; + return 1; + } + + if (vm.count("help")) + { + std::clog << desc << std::endl; + return 1; + } + if (vm.count("verbose")) + { + verbose = true; + } + + if (vm.count("svg")) + { + svg=vm["svg"].as(); + } + else + { + std::clog << "please provide an svg file!" << std::endl; + return -1; + } + + boost::optional marker_ptr = mapnik::marker_cache::instance()->find(svg, false); + if (marker_ptr) { + + mapnik::marker marker = **marker_ptr; + if (marker.is_vector()) { + + int width_ = 600;//marker.width() + 10; + int height_ = 400;//marker.height() + 10; + mapnik::image_32 pixmap_(width_,height_); + agg::rasterizer_scanline_aa<> ras_ptr; + + typedef agg::pixfmt_rgba32_plain pixfmt; + + typedef agg::renderer_base renderer_base; + + + ras_ptr.reset(); + ras_ptr.gamma(agg::gamma_linear()); + agg::scanline_u8 sl; + agg::rendering_buffer buf(pixmap_.raw_data(), width_, height_, width_ * 4); + pixfmt pixf(buf); + renderer_base renb(pixf); + + mapnik::box2d const& bbox = (*marker.get_vector_data())->bounding_box(); + double x1 = bbox.minx(); + double y1 = bbox.miny(); + double x2 = bbox.maxx(); + double y2 = bbox.maxy(); + + + agg::trans_affine recenter = agg::trans_affine_translation(0.5*(marker.width()-(x1+x2)),0.5*(marker.height()-(y1+y2))); + + mapnik::svg::vertex_stl_adapter stl_storage((*marker.get_vector_data())->source()); + + mapnik::svg::svg_path_adapter svg_path(stl_storage); + mapnik::svg::svg_renderer > svg_renderer_this(svg_path, + (*marker.get_vector_data())->attributes()); + agg::trans_affine tr; + + boost::array matrix_; + matrix_[0] = 1.0; + matrix_[1] = 0.0; + matrix_[2] = 0.0; + matrix_[3] = 1.0; + matrix_[4] = 0.0; + matrix_[5] = 0.0; + tr.load_from(&matrix_[0]); + + agg::trans_affine mtx = recenter * tr; + double scale_factor_ = 1; + double opacity = 1; + mtx *= agg::trans_affine_scaling(scale_factor_); + mtx *= agg::trans_affine_translation(width_/2, height_/2); + + svg_renderer_this.render(ras_ptr, sl, renb, mtx, opacity, bbox); + + mapnik::save_to_file(pixmap_.data(),"test.png","png"); + system("open test.png"); + + + } + } + + } + catch (...) + { + std::clog << "Exception of unknown type!" << std::endl; + return -1; + } + + std::clog << "done!" << std::endl; + return 0; +} +