diff --git a/bindings/python/mapnik_image.cpp b/bindings/python/mapnik_image.cpp index 4459edd21..7577878c3 100644 --- a/bindings/python/mapnik_image.cpp +++ b/bindings/python/mapnik_image.cpp @@ -121,24 +121,7 @@ bool painted(mapnik::image_32 const& im) bool is_solid(mapnik::image_32 const& im) { - if (im.width() > 0 && im.height() > 0) - { - mapnik::image_data_rgba8 const & data = im.data(); - mapnik::image_data_rgba8::pixel_type const* first_row = data.getRow(0); - mapnik::image_data_rgba8::pixel_type const first_pixel = first_row[0]; - for (unsigned y = 0; y < im.height(); ++y) - { - mapnik::image_data_rgba8::pixel_type const * row = data.getRow(y); - for (unsigned x = 0; x < im.width(); ++x) - { - if (first_pixel != row[x]) - { - return false; - } - } - } - } - return true; + return mapnik::is_solid(im.data()); } unsigned get_pixel(mapnik::image_32 const& im, int x, int y) diff --git a/bindings/python/mapnik_image_view.cpp b/bindings/python/mapnik_image_view.cpp index 8894a96eb..4d448e40a 100644 --- a/bindings/python/mapnik_image_view.cpp +++ b/bindings/python/mapnik_image_view.cpp @@ -89,23 +89,7 @@ PyObject* view_tostring3(image_view_rgba8 const & view, std::string const& forma bool is_solid(image_view_rgba8 const& view) { - if (view.width() > 0 && view.height() > 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_rgba8::pixel_type const * row = view.getRow(y); - for (unsigned x = 0; x < view.width(); ++x) - { - if (first_pixel != row[x]) - { - return false; - } - } - } - } - return true; + return mapnik::is_solid(view); } void save_view1(image_view_rgba8 const& view, diff --git a/include/mapnik/image.hpp b/include/mapnik/image.hpp index ed2a1d1bc..d9ee28334 100644 --- a/include/mapnik/image.hpp +++ b/include/mapnik/image.hpp @@ -24,6 +24,7 @@ #define MAPNIK_IMAGE_HPP #include +#include namespace mapnik { diff --git a/include/mapnik/image_util.hpp b/include/mapnik/image_util.hpp index 0cec4782c..2c018a0bc 100644 --- a/include/mapnik/image_util.hpp +++ b/include/mapnik/image_util.hpp @@ -27,6 +27,7 @@ #include #include #include +#include // boost #pragma GCC diagnostic push @@ -119,9 +120,7 @@ template MAPNIK_DECL void set_premultiplied_alpha(T & image, bool status); template -void save_as_png(T const& image, - std::string const& filename, - rgba_palette const& palette); +MAPNIK_DECL bool is_solid (T const& image); inline bool is_png(std::string const& filename) { diff --git a/src/image.cpp b/src/image.cpp index f30d025f2..6e71b80c9 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -52,54 +52,10 @@ image image::read_from_file(std::string const& filename) return image(); } } -/* -namespace detail { - -struct save_to_file_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_); - } - - void operator() (image_data_gray16 const& data) const - { - if (format_ == "tiff") // only TIFF supported - { - std::ofstream file (filename_.c_str(), std::ios::out| std::ios::trunc|std::ios::binary); - if (file) - { - tiff_config config; - save_as_tiff(file, data, config); - } - else throw ImageWriterException("Could not write file to " + filename_ ); - } - else - { - throw std::runtime_error("Error: saving gray16 image as " + format_ + " is not supported"); - } - } - - template - void operator() (T const& data) const - { - throw std::runtime_error("Error: saving " + std::string(typeid(data).name()) + " as " + format_ + " is not supported"); - } - - std::string const& filename_; - std::string const& format_; -}; - -}*/ void image::save_to_file(std::string const& filename, std::string const& format) { mapnik::save_to_file(data_, filename, format); - //util::apply_visitor(detail::save_to_file_visitor(filename, format), data_); } } diff --git a/src/image_util.cpp b/src/image_util.cpp index f71fe334f..359ba06a6 100644 --- a/src/image_util.cpp +++ b/src/image_util.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -397,6 +398,75 @@ template std::string save_to_string(image_data_any const&, std::string const&, rgba_palette const& palette); +namespace detail { + +struct is_solid_visitor +{ + template + bool operator() (T const & data) + { + using pixel_type = typename T::pixel_type; + if (data.width() > 0 && data.height() > 0) + { + pixel_type const* first_row = data.getRow(0); + pixel_type const first_pixel = first_row[0]; + for (unsigned y = 0; y < data.height(); ++y) + { + pixel_type const * row = data.getRow(y); + for (unsigned x = 0; x < data.width(); ++x) + { + if (first_pixel != row[x]) + { + return false; + } + } + } + } + return true; + } +}; + +template bool is_solid_visitor::operator() (image_data_rgba8 const& data); +template bool is_solid_visitor::operator() (image_data_gray8 const& data); +template bool is_solid_visitor::operator() (image_data_gray16 const& data); +template bool is_solid_visitor::operator() (image_data_gray32f const& data); +template bool is_solid_visitor::operator() (image_view_rgba8 const& data); +template bool is_solid_visitor::operator() (image_view_gray8 const& data); +template bool is_solid_visitor::operator() (image_view_gray16 const& data); +template bool is_solid_visitor::operator() (image_view_gray32f const& data); + +template<> +bool is_solid_visitor::operator() (image_data_null const&) +{ + return true; +} + +} // end detail ns + +template +MAPNIK_DECL bool is_solid(T const& image) +{ + return util::apply_visitor(detail::is_solid_visitor(), image); +} + +template bool is_solid (image_data_any const&); +template bool is_solid (image_view_any const&); + +// Temporary until image_data_rgba8 is removed from passing +template <> +MAPNIK_DECL bool is_solid(image_data_rgba8 const& image) +{ + detail::is_solid_visitor visitor; + return visitor(image); +} + +// Temporary until image_view_rgba8 is removed from passing +template <> +MAPNIK_DECL bool is_solid(image_view_rgba8 const& image) +{ + detail::is_solid_visitor visitor; + return visitor(image); +} namespace detail {