image class - containing image_data_any and initial basic python interface

This commit is contained in:
artemp 2015-01-06 12:44:28 +01:00
parent 2a33ead4cc
commit b0b89e76d1
4 changed files with 157 additions and 1 deletions

View file

@ -37,6 +37,7 @@
// mapnik
#include <mapnik/graphics.hpp>
#include <mapnik/palette.hpp>
#include <mapnik/image.hpp>
#include <mapnik/image_util.hpp>
#include <mapnik/image_reader.hpp>
#include <mapnik/image_compositing.hpp>
@ -48,6 +49,8 @@
#include <cairo.h>
#endif
using mapnik::image;
using mapnik::image_data_any;
using mapnik::image_32;
using mapnik::image_reader;
using mapnik::get_image_reader;
@ -222,6 +225,21 @@ std::shared_ptr<image_32> from_cairo(PycairoSurface* py_surface)
}
#endif
// ============ image any
std::shared_ptr<image> read_from_file_impl(std::string const& filename)
{
std::shared_ptr<image> img(new image);
std::unique_ptr<image_reader> reader(get_image_reader(filename));
if (reader)
{
unsigned w = reader->width();
unsigned h = reader->height();
img->set_data(reader->read(0, 0, w, h));
}
return img;
}
// =========================
void export_image()
{
using namespace boost::python;
@ -265,7 +283,15 @@ void export_image()
.value("divide", mapnik::divide)
;
class_<image_32,std::shared_ptr<image_32> >("Image","This class represents a 32 bit RGBA image.",init<int,int>())
class_<image, std::shared_ptr<image>, boost::noncopyable > ("ImageAny", "This class represents an any Image object", no_init)
.def("width",&image::width)
.def("height",&image::height)
.def("open", &read_from_file_impl)
.staticmethod("open")
.def("save",&image::save_to_file)
;
class_<image_32,std::shared_ptr<image_32>, boost::noncopyable >("Image","This class represents a 32 bit RGBA image.",init<int,int>())
.def("width",&image_32::width)
.def("height",&image_32::height)
.def("view",&image_32::get_view)

49
include/mapnik/image.hpp Normal file
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_IMAGE_HPP
#define MAPNIK_IMAGE_HPP
#include <mapnik/noncopyable.hpp>
#include <mapnik/image_data_any.hpp>
namespace mapnik {
class image
{
public:
image() = default;
inline std::size_t width() const { return data_.width(); }
inline std::size_t height() const { return data_.height(); }
void set_data(image_data_any && data);
inline image_data_any const& data() const { return data_;}
static image read_from_file(std::string const& filename);
void save_to_file(std::string const& filename, std::string const& format);
private:
image(image && other) = default;
image(image_data_any && data) noexcept;
image_data_any data_;
};
}
#endif // MAPNIK_IMAGE_HPP

View file

@ -152,6 +152,7 @@ source = Split(
miniz_png.cpp
color.cpp
conversions.cpp
image.cpp
image_compositing.cpp
image_scaling.cpp
box2d.cpp

80
src/image.cpp Normal file
View file

@ -0,0 +1,80 @@
/*****************************************************************************
*
* 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
*
*****************************************************************************/
#include <mapnik/image.hpp>
#include <mapnik/image_reader.hpp>
#include <mapnik/image_util.hpp>
namespace mapnik {
image::image(image_data_any && data) noexcept
: data_(std::move(data)) {}
void image::set_data(image_data_any && data)
{
data_ = std::move(data);
}
image image::read_from_file(std::string const& filename)
{
std::unique_ptr<image_reader> reader(get_image_reader(filename));
if (reader)
{
unsigned w = reader->width();
unsigned h = reader->height();
image_data_any data = reader->read(0, 0, w, h);
return image(std::move(data));
}
else
{
return image();
}
}
namespace detail {
struct save_to_file_visitor : mapnik::util::static_visitor<>
{
save_to_file_visitor(std::string const& filename, std::string const& format)
: filename_(filename),
format_(format) {}
void operator() (image_data_rgba8 const& data) const
{
save_to_file(data,filename_, format_);
}
template <typename T>
void operator() (T const& data) const {}
std::string const& filename_;
std::string const& format_;
};
}
void image::save_to_file(std::string const& filename, std::string const& format)
{
util::apply_visitor(detail::save_to_file_visitor(filename, format), data_);
}
}