Moved cairo out of the image_util.*pp files and now have created a cairo_io.*pp files.

Ref #2622
This commit is contained in:
Blake Thompson 2015-01-09 16:49:31 -05:00
parent b2118b6c2e
commit 42ee4ec90d
13 changed files with 218 additions and 146 deletions

View file

@ -39,21 +39,19 @@
#include <mapnik/image_view.hpp>
#include <mapnik/image_util.hpp>
#include <mapnik/palette.hpp>
#include <mapnik/image_view.hpp>
#include <sstream>
using mapnik::image_data_rgba8;
using mapnik::image_view;
using mapnik::image_view_rgba8;
using mapnik::save_to_file;
// output 'raw' pixels
PyObject* view_tostring1(image_view<image_data_rgba8> const& view)
PyObject* view_tostring1(image_view_rgba8 const& view)
{
std::ostringstream ss(std::ios::out|std::ios::binary);
for (unsigned i=0;i<view.height();i++)
{
ss.write(reinterpret_cast<const char*>(view.getRow(i)),
view.width() * sizeof(image_view<image_data_rgba8>::pixel_type));
view.width() * sizeof(image_view_rgba8::pixel_type));
}
return
#if PY_VERSION_HEX >= 0x03000000
@ -65,7 +63,7 @@ PyObject* view_tostring1(image_view<image_data_rgba8> const& view)
}
// encode (png,jpeg)
PyObject* view_tostring2(image_view<image_data_rgba8> const & view, std::string const& format)
PyObject* view_tostring2(image_view_rgba8 const & view, std::string const& format)
{
std::string s = save_to_string(view, format);
return
@ -77,7 +75,7 @@ PyObject* view_tostring2(image_view<image_data_rgba8> const & view, std::string
(s.data(),s.size());
}
PyObject* view_tostring3(image_view<image_data_rgba8> const & view, std::string const& format, mapnik::rgba_palette const& pal)
PyObject* view_tostring3(image_view_rgba8 const & view, std::string const& format, mapnik::rgba_palette const& pal)
{
std::string s = save_to_string(view, format, pal);
return
@ -89,15 +87,15 @@ PyObject* view_tostring3(image_view<image_data_rgba8> const & view, std::string
(s.data(),s.size());
}
bool is_solid(image_view<image_data_rgba8> const& view)
bool is_solid(image_view_rgba8 const& view)
{
if (view.width() > 0 && view.height() > 0)
{
mapnik::image_view<image_data_rgba8>::pixel_type const* first_row = view.getRow(0);
mapnik::image_view<image_data_rgba8>::pixel_type const first_pixel = first_row[0];
mapnik::image_view_rgba8::pixel_type const* first_row = view.getRow(0);
mapnik::image_view_rgba8::pixel_type const first_pixel = first_row[0];
for (unsigned y = 0; y < view.height(); ++y)
{
mapnik::image_view<image_data_rgba8>::pixel_type const * row = view.getRow(y);
mapnik::image_view_rgba8::pixel_type const * row = view.getRow(y);
for (unsigned x = 0; x < view.width(); ++x)
{
if (first_pixel != row[x])
@ -110,20 +108,20 @@ bool is_solid(image_view<image_data_rgba8> const& view)
return true;
}
void save_view1(image_view<image_data_rgba8> const& view,
void save_view1(image_view_rgba8 const& view,
std::string const& filename)
{
save_to_file(view,filename);
}
void save_view2(image_view<image_data_rgba8> const& view,
void save_view2(image_view_rgba8 const& view,
std::string const& filename,
std::string const& type)
{
save_to_file(view,filename,type);
}
void save_view3(image_view<image_data_rgba8> const& view,
void save_view3(image_view_rgba8 const& view,
std::string const& filename,
std::string const& type,
mapnik::rgba_palette const& pal)
@ -135,9 +133,9 @@ void save_view3(image_view<image_data_rgba8> const& view,
void export_image_view()
{
using namespace boost::python;
class_<image_view<image_data_rgba8> >("ImageView","A view into an image.",no_init)
.def("width",&image_view<image_data_rgba8>::width)
.def("height",&image_view<image_data_rgba8>::height)
class_<image_view_rgba8>("ImageView","A view into an image.",no_init)
.def("width",&image_view_rgba8::width)
.def("height",&image_view_rgba8::height)
.def("is_solid",&is_solid)
.def("tostring",&view_tostring1)
.def("tostring",&view_tostring2)

View file

@ -137,6 +137,7 @@ void clear_cache()
}
#if defined(HAVE_CAIRO) && defined(HAVE_PYCAIRO)
#include <mapnik/cairo_io.hpp>
#include <mapnik/cairo/cairo_renderer.hpp>
#include <boost/python/type_id.hpp>
#include <boost/python/converter/registry.hpp>

View file

@ -37,6 +37,7 @@
#include <mapnik/image_util.hpp>
#include <mapnik/unicode.hpp>
#include <mapnik/save_map.hpp>
#include <mapnik/cairo_io.hpp>
#if defined(HAVE_CAIRO)
#include <mapnik/cairo/cairo_renderer.hpp>

View file

@ -0,0 +1,49 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2014 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 MAPNIK_CAIRO_IO_HPP
#define MAPNIK_CAIRO_IO_HPP
// mapnik
#include <mapnik/config.hpp>
#include <mapnik/map.hpp>
// stl
#include <string>
namespace mapnik {
#if defined(HAVE_CAIRO)
MAPNIK_DECL void save_to_cairo_file(mapnik::Map const& map,
std::string const& filename,
double scale_factor=1.0,
double scale_denominator=0.0);
MAPNIK_DECL void save_to_cairo_file(mapnik::Map const& map,
std::string const& filename,
std::string const& type,
double scale_factor=1.0,
double scale_denominator=0.0);
#endif
} // end ns
#endif // MAPNIK_CAIRO_IO_HPP

View file

@ -119,9 +119,9 @@ public:
return data_.getBytes();
}
inline image_view<image_data_rgba8> get_view(unsigned x,unsigned y, unsigned w,unsigned h)
inline image_view_rgba8 get_view(unsigned x,unsigned y, unsigned w,unsigned h)
{
return image_view<image_data_rgba8>(x,y,w,h,data_);
return image_view_rgba8(x,y,w,h,data_);
}
private:

View file

@ -62,18 +62,6 @@ public:
}
};
#if defined(HAVE_CAIRO)
MAPNIK_DECL void save_to_cairo_file(mapnik::Map const& map,
std::string const& filename,
double scale_factor=1.0,
double scale_denominator=0.0);
MAPNIK_DECL void save_to_cairo_file(mapnik::Map const& map,
std::string const& filename,
std::string const& type,
double scale_factor=1.0,
double scale_denominator=0.0);
#endif
template <typename T>
MAPNIK_DECL void save_to_file(T const& image,
std::string const& filename,

View file

@ -84,11 +84,11 @@ private:
};
extern template MAPNIK_DECL void PNGWriter::writeIDAT<image_data_gray8>(image_data_gray8 const& image);
extern template MAPNIK_DECL void PNGWriter::writeIDAT<image_view<image_data_gray8> >(image_view<image_data_gray8> const& image);
extern template MAPNIK_DECL void PNGWriter::writeIDAT<image_view_gray8>(image_view_gray8 const& image);
extern template MAPNIK_DECL void PNGWriter::writeIDAT<image_data_rgba8>(image_data_rgba8 const& image);
extern template MAPNIK_DECL void PNGWriter::writeIDAT<image_view<image_data_rgba8> >(image_view<image_data_rgba8> const& image);
extern template MAPNIK_DECL void PNGWriter::writeIDAT<image_view_rgba8>(image_view_rgba8 const& image);
extern template MAPNIK_DECL void PNGWriter::writeIDATStripAlpha<image_data_rgba8>(image_data_rgba8 const& image);
extern template MAPNIK_DECL void PNGWriter::writeIDATStripAlpha<image_view<image_data_rgba8> >(image_view<image_data_rgba8> const& image);
extern template MAPNIK_DECL void PNGWriter::writeIDATStripAlpha<image_view_rgba8>(image_view_rgba8 const& image);
}}

View file

@ -6,8 +6,8 @@ if [ ${UNAME} = 'Darwin' ]; then
else
export LD_LIBRARY_PATH="${CURRENT_DIR}/src/":${LD_LIBRARY_PATH}
fi
export PYTHONPATH="${CURRENT_DIR}/bindings/python/":$PYTHONPATH
export PYTHONPATH="${CURRENT_DIR}/bindings/python/" #:$PYTHONPATH
export MAPNIK_FONT_DIRECTORY="${CURRENT_DIR}/fonts/dejavu-fonts-ttf-2.34/ttf/"
export MAPNIK_INPUT_PLUGINS_DIRECTORY="${CURRENT_DIR}/plugins/input/"
export PATH="${CURRENT_DIR}/utils/mapnik-config":${PATH}
export PATH="${CURRENT_DIR}/utils/nik2img":${PATH}
export PATH="${CURRENT_DIR}/utils/nik2img":${PATH}

View file

@ -173,6 +173,7 @@ source = Split(
graphics.cpp
parse_path.cpp
image_reader.cpp
cairo_io.cpp
image_util.cpp
image_util_jpeg.cpp
image_util_png.cpp

131
src/cairo_io.cpp Normal file
View file

@ -0,0 +1,131 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2014 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
*
*****************************************************************************/
// mapnik
#include <mapnik/cairo_io.hpp>
#include <mapnik/image_util.hpp>
#include <mapnik/map.hpp>
#ifdef HAVE_CAIRO
#include <mapnik/cairo/cairo_renderer.hpp>
#include <cairo.h>
#ifdef CAIRO_HAS_PDF_SURFACE
#include <cairo-pdf.h>
#endif
#ifdef CAIRO_HAS_PS_SURFACE
#include <cairo-ps.h>
#endif
#ifdef CAIRO_HAS_SVG_SURFACE
#include <cairo-svg.h>
#endif
#endif
// stl
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
namespace mapnik {
#if defined(HAVE_CAIRO)
void save_to_cairo_file(mapnik::Map const& map, std::string const& filename, double scale_factor, double scale_denominator)
{
boost::optional<std::string> type = type_from_filename(filename);
if (type)
{
save_to_cairo_file(map,filename,*type,scale_factor,scale_denominator);
}
else throw ImageWriterException("Could not write file to " + filename );
}
void save_to_cairo_file(mapnik::Map const& map,
std::string const& filename,
std::string const& type,
double scale_factor,
double scale_denominator)
{
std::ofstream file (filename.c_str(), std::ios::out|std::ios::trunc|std::ios::binary);
if (file)
{
cairo_surface_ptr surface;
unsigned width = map.width();
unsigned height = map.height();
if (type == "pdf")
{
#ifdef CAIRO_HAS_PDF_SURFACE
surface = cairo_surface_ptr(cairo_pdf_surface_create(filename.c_str(),width,height),cairo_surface_closer());
#else
throw ImageWriterException("PDFSurface not supported in the cairo backend");
#endif
}
#ifdef CAIRO_HAS_SVG_SURFACE
else if (type == "svg")
{
surface = cairo_surface_ptr(cairo_svg_surface_create(filename.c_str(),width,height),cairo_surface_closer());
}
#endif
#ifdef CAIRO_HAS_PS_SURFACE
else if (type == "ps")
{
surface = cairo_surface_ptr(cairo_ps_surface_create(filename.c_str(),width,height),cairo_surface_closer());
}
#endif
#ifdef CAIRO_HAS_IMAGE_SURFACE
else if (type == "ARGB32")
{
surface = cairo_surface_ptr(cairo_image_surface_create(CAIRO_FORMAT_ARGB32,width,height),cairo_surface_closer());
}
else if (type == "RGB24")
{
surface = cairo_surface_ptr(cairo_image_surface_create(CAIRO_FORMAT_RGB24,width,height),cairo_surface_closer());
}
#endif
else
{
throw ImageWriterException("unknown file type: " + type);
}
//cairo_t * ctx = cairo_create(surface);
// TODO - expose as user option
/*
if (type == "ARGB32" || type == "RGB24")
{
context->set_antialias(Cairo::ANTIALIAS_NONE);
}
*/
mapnik::cairo_renderer<cairo_ptr> ren(map, create_context(surface), scale_factor);
ren.apply(scale_denominator);
if (type == "ARGB32" || type == "RGB24")
{
cairo_surface_write_to_png(&*surface, filename.c_str());
}
cairo_surface_finish(&*surface);
}
}
#endif
} // end ns

View file

@ -34,20 +34,6 @@
#include <mapnik/util/conversions.hpp>
#include <mapnik/util/variant.hpp>
#ifdef HAVE_CAIRO
#include <mapnik/cairo/cairo_renderer.hpp>
#include <cairo.h>
#ifdef CAIRO_HAS_PDF_SURFACE
#include <cairo-pdf.h>
#endif
#ifdef CAIRO_HAS_PS_SURFACE
#include <cairo-ps.h>
#endif
#ifdef CAIRO_HAS_SVG_SURFACE
#include <cairo-svg.h>
#endif
#endif
// boost
#include <boost/tokenizer.hpp>
@ -61,7 +47,6 @@
namespace mapnik
{
template <typename T>
std::string save_to_string(T const& image,
std::string const& type,
@ -197,88 +182,6 @@ void save_to_file(T const& image, std::string const& filename, rgba_palette cons
else throw ImageWriterException("Could not write file to " + filename );
}
#if defined(HAVE_CAIRO)
// TODO - move to separate cairo_io.hpp
void save_to_cairo_file(mapnik::Map const& map, std::string const& filename, double scale_factor, double scale_denominator)
{
boost::optional<std::string> type = type_from_filename(filename);
if (type)
{
save_to_cairo_file(map,filename,*type,scale_factor,scale_denominator);
}
else throw ImageWriterException("Could not write file to " + filename );
}
void save_to_cairo_file(mapnik::Map const& map,
std::string const& filename,
std::string const& type,
double scale_factor,
double scale_denominator)
{
std::ofstream file (filename.c_str(), std::ios::out|std::ios::trunc|std::ios::binary);
if (file)
{
cairo_surface_ptr surface;
unsigned width = map.width();
unsigned height = map.height();
if (type == "pdf")
{
#ifdef CAIRO_HAS_PDF_SURFACE
surface = cairo_surface_ptr(cairo_pdf_surface_create(filename.c_str(),width,height),cairo_surface_closer());
#else
throw ImageWriterException("PDFSurface not supported in the cairo backend");
#endif
}
#ifdef CAIRO_HAS_SVG_SURFACE
else if (type == "svg")
{
surface = cairo_surface_ptr(cairo_svg_surface_create(filename.c_str(),width,height),cairo_surface_closer());
}
#endif
#ifdef CAIRO_HAS_PS_SURFACE
else if (type == "ps")
{
surface = cairo_surface_ptr(cairo_ps_surface_create(filename.c_str(),width,height),cairo_surface_closer());
}
#endif
#ifdef CAIRO_HAS_IMAGE_SURFACE
else if (type == "ARGB32")
{
surface = cairo_surface_ptr(cairo_image_surface_create(CAIRO_FORMAT_ARGB32,width,height),cairo_surface_closer());
}
else if (type == "RGB24")
{
surface = cairo_surface_ptr(cairo_image_surface_create(CAIRO_FORMAT_RGB24,width,height),cairo_surface_closer());
}
#endif
else
{
throw ImageWriterException("unknown file type: " + type);
}
//cairo_t * ctx = cairo_create(surface);
// TODO - expose as user option
/*
if (type == "ARGB32" || type == "RGB24")
{
context->set_antialias(Cairo::ANTIALIAS_NONE);
}
*/
mapnik::cairo_renderer<cairo_ptr> ren(map, create_context(surface), scale_factor);
ren.apply(scale_denominator);
if (type == "ARGB32" || type == "RGB24")
{
cairo_surface_write_to_png(&*surface, filename.c_str());
}
cairo_surface_finish(&*surface);
}
}
#endif
template void save_to_file<image_data_rgba8>(image_data_rgba8 const&,
std::string const&,
std::string const&);
@ -302,26 +205,26 @@ template std::string save_to_string<image_data_rgba8>(image_data_rgba8 const&,
std::string const&,
rgba_palette const& palette);
template void save_to_file<image_view<image_data_rgba8> > (image_view<image_data_rgba8> const&,
template void save_to_file<image_view_rgba8> (image_view_rgba8 const&,
std::string const&,
std::string const&);
template void save_to_file<image_view<image_data_rgba8> > (image_view<image_data_rgba8> const&,
template void save_to_file<image_view_rgba8> (image_view_rgba8 const&,
std::string const&,
std::string const&,
rgba_palette const& palette);
template void save_to_file<image_view<image_data_rgba8> > (image_view<image_data_rgba8> const&,
template void save_to_file<image_view_rgba8> (image_view_rgba8 const&,
std::string const&);
template void save_to_file<image_view<image_data_rgba8> > (image_view<image_data_rgba8> const&,
template void save_to_file<image_view_rgba8> (image_view_rgba8 const&,
std::string const&,
rgba_palette const& palette);
template std::string save_to_string<image_view<image_data_rgba8> > (image_view<image_data_rgba8> const&,
template std::string save_to_string<image_view_rgba8> (image_view_rgba8 const&,
std::string const&);
template std::string save_to_string<image_view<image_data_rgba8> > (image_view<image_data_rgba8> const&,
template std::string save_to_string<image_view_rgba8> (image_view_rgba8 const&,
std::string const&,
rgba_palette const& palette);

View file

@ -381,9 +381,9 @@ template void webp_saver::operator()<image_data_rgba8> (image_data_rgba8 const&
template void webp_saver::operator()<image_data_gray8> (image_data_gray8 const& image) const;
template void webp_saver::operator()<image_data_gray16> (image_data_gray16 const& image) const;
template void webp_saver::operator()<image_data_gray32f> (image_data_gray32f const& image) const;
template void webp_saver::operator()<image_view<image_data_rgba8>> (image_view<image_data_rgba8> const& image) const;
template void webp_saver::operator()<image_view<image_data_gray8>> (image_view<image_data_gray8> const& image) const;
template void webp_saver::operator()<image_view<image_data_gray16>> (image_view<image_data_gray16> const& image) const;
template void webp_saver::operator()<image_view<image_data_gray32f>> (image_view<image_data_gray32f> const& image) const;
template void webp_saver::operator()<image_view_rgba8> (image_view_rgba8 const& image) const;
template void webp_saver::operator()<image_view_gray8> (image_view_gray8 const& image) const;
template void webp_saver::operator()<image_view_gray16> (image_view_gray16 const& image) const;
template void webp_saver::operator()<image_view_gray32f> (image_view_gray32f const& image) const;
} // end ns

View file

@ -362,10 +362,10 @@ const mz_uint8 PNGWriter::IEND_tpl[] = {
};
template void PNGWriter::writeIDAT<image_data_gray8>(image_data_gray8 const& image);
template void PNGWriter::writeIDAT<image_view<image_data_gray8> >(image_view<image_data_gray8> const& image);
template void PNGWriter::writeIDAT<image_view_gray8>(image_view_gray8 const& image);
template void PNGWriter::writeIDAT<image_data_rgba8>(image_data_rgba8 const& image);
template void PNGWriter::writeIDAT<image_view<image_data_rgba8> >(image_view<image_data_rgba8> const& image);
template void PNGWriter::writeIDAT<image_view_rgba8>(image_view_rgba8 const& image);
template void PNGWriter::writeIDATStripAlpha<image_data_rgba8>(image_data_rgba8 const& image);
template void PNGWriter::writeIDATStripAlpha<image_view<image_data_rgba8> >(image_view<image_data_rgba8> const& image);
template void PNGWriter::writeIDATStripAlpha<image_view_rgba8>(image_view_rgba8 const& image);
}}