2011-05-04 02:20:17 +02:00
|
|
|
/*****************************************************************************
|
2012-02-02 02:53:35 +01:00
|
|
|
*
|
2011-05-04 02:20:17 +02:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2016-12-20 17:57:22 +01:00
|
|
|
* Copyright (C) 2016 Artem Pavlenko
|
2011-05-04 02:20:17 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/debug.hpp>
|
2016-08-02 16:34:59 +02:00
|
|
|
#include <mapnik/value/types.hpp>
|
2013-01-14 07:01:13 +01:00
|
|
|
#include <mapnik/value.hpp> // for to_double
|
2013-01-04 08:55:09 +01:00
|
|
|
#include <mapnik/feature.hpp>
|
|
|
|
#include <mapnik/raster.hpp>
|
2011-05-04 02:20:17 +02:00
|
|
|
#include <mapnik/raster_colorizer.hpp>
|
2013-05-20 21:44:55 +02:00
|
|
|
#include <mapnik/enumeration.hpp>
|
2012-04-08 02:20:56 +02:00
|
|
|
|
|
|
|
// stl
|
2011-05-04 02:20:17 +02:00
|
|
|
#include <limits>
|
2013-01-17 22:53:48 +01:00
|
|
|
#include <cmath>
|
2011-05-04 02:20:17 +02:00
|
|
|
|
2011-05-04 07:19:58 +02:00
|
|
|
namespace mapnik
|
|
|
|
{
|
2011-05-04 02:20:17 +02:00
|
|
|
|
|
|
|
//! \brief Strings for the colorizer_mode enumeration
|
|
|
|
static const char *colorizer_mode_strings[] = {
|
|
|
|
"inherit",
|
|
|
|
"linear",
|
|
|
|
"discrete",
|
|
|
|
"exact",
|
|
|
|
""
|
|
|
|
};
|
|
|
|
|
2011-07-06 01:39:50 +02:00
|
|
|
IMPLEMENT_ENUM( colorizer_mode, colorizer_mode_strings )
|
2011-05-04 02:20:17 +02:00
|
|
|
|
|
|
|
|
2012-02-02 02:53:35 +01:00
|
|
|
colorizer_stop::colorizer_stop(float value, colorizer_mode mode,
|
|
|
|
color const& _color,
|
2012-01-13 17:30:03 +01:00
|
|
|
std::string const& label)
|
|
|
|
: value_(value)
|
2011-05-04 02:20:17 +02:00
|
|
|
, mode_(mode)
|
|
|
|
, color_(_color)
|
2011-09-16 15:32:16 +02:00
|
|
|
, label_(label)
|
2011-05-04 02:20:17 +02:00
|
|
|
{
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2011-05-04 02:20:17 +02:00
|
|
|
}
|
|
|
|
|
2012-01-13 17:30:03 +01:00
|
|
|
colorizer_stop::colorizer_stop(colorizer_stop const& stop)
|
2011-05-04 02:20:17 +02:00
|
|
|
: value_(stop.value_)
|
|
|
|
, mode_(stop.mode_)
|
|
|
|
, color_(stop.color_)
|
2011-09-16 15:32:16 +02:00
|
|
|
, label_(stop.label_)
|
2011-05-04 02:20:17 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
colorizer_stop::~colorizer_stop()
|
|
|
|
{
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2011-05-04 02:20:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool colorizer_stop::operator==(colorizer_stop const& other) const
|
|
|
|
{
|
2012-02-02 02:53:35 +01:00
|
|
|
return (value_ == other.value_) &&
|
2012-01-13 17:30:03 +01:00
|
|
|
(color_ == other.color_) &&
|
|
|
|
(mode_ == other.mode_) &&
|
|
|
|
(label_ == other.label_);
|
2011-05-04 02:20:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string colorizer_stop::to_string() const
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << color_.to_string() << " " << value_ << " " << mode_.as_string();
|
|
|
|
return ss.str();
|
2012-02-02 02:53:35 +01:00
|
|
|
}
|
2011-05-04 02:20:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-13 17:30:03 +01:00
|
|
|
raster_colorizer::raster_colorizer(colorizer_mode mode, color const& _color)
|
2011-05-04 02:20:17 +02:00
|
|
|
: default_mode_(mode)
|
|
|
|
, default_color_(_color)
|
2011-05-04 07:19:58 +02:00
|
|
|
, epsilon_(std::numeric_limits<float>::epsilon())
|
2011-05-04 02:20:17 +02:00
|
|
|
{
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2011-05-04 02:20:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
raster_colorizer::~raster_colorizer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-02-02 02:53:35 +01:00
|
|
|
bool raster_colorizer::add_stop(colorizer_stop const& stop)
|
2012-01-13 17:30:03 +01:00
|
|
|
{
|
2011-05-04 02:20:17 +02:00
|
|
|
//make sure stops are added in order of value
|
2012-02-02 02:53:35 +01:00
|
|
|
if(stops_.size())
|
2012-01-13 17:30:03 +01:00
|
|
|
{
|
2012-02-02 02:53:35 +01:00
|
|
|
if(stop.get_value() <= stops_.back().get_value())
|
2012-01-13 17:30:03 +01:00
|
|
|
{
|
2011-05-04 02:20:17 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2011-05-04 02:20:17 +02:00
|
|
|
stops_.push_back(stop);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-11 10:10:32 +01:00
|
|
|
template <typename T>
|
2015-01-22 03:31:02 +01:00
|
|
|
void raster_colorizer::colorize(image_rgba8 & out, T const& in,
|
2014-12-17 14:54:31 +01:00
|
|
|
boost::optional<double> const& nodata,
|
2014-12-11 10:10:32 +01:00
|
|
|
feature_impl const& f) const
|
2014-12-03 18:16:42 +01:00
|
|
|
{
|
2015-01-22 18:39:37 +01:00
|
|
|
using image_type = T;
|
|
|
|
using pixel_type = typename image_type::pixel_type;
|
2014-12-03 18:16:42 +01:00
|
|
|
// TODO: assuming in/out have the same width/height for now
|
2015-05-04 12:49:11 +02:00
|
|
|
std::uint32_t * out_data = out.data();
|
|
|
|
pixel_type const* in_data = in.data();
|
2014-12-03 18:16:42 +01:00
|
|
|
int len = out.width() * out.height();
|
|
|
|
for (int i=0; i<len; ++i)
|
|
|
|
{
|
2014-12-11 10:10:32 +01:00
|
|
|
pixel_type value = in_data[i];
|
2014-12-03 18:16:42 +01:00
|
|
|
if (nodata && (std::fabs(value - *nodata) < epsilon_))
|
|
|
|
{
|
2014-12-04 10:31:56 +01:00
|
|
|
out_data[i] = 0; // rgba(0,0,0,0)
|
2014-12-03 18:16:42 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out_data[i] = get_color(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-29 15:36:39 +01:00
|
|
|
inline unsigned interpolate(unsigned start, unsigned end, float fraction)
|
2011-05-04 02:20:17 +02:00
|
|
|
{
|
2016-03-10 20:44:23 +01:00
|
|
|
return static_cast<unsigned>(fraction * (static_cast<float>(end) - static_cast<float>(start)) + static_cast<float>(start));
|
2011-05-04 02:20:17 +02:00
|
|
|
}
|
|
|
|
|
2013-09-25 23:58:32 +02:00
|
|
|
unsigned raster_colorizer::get_color(float value) const
|
2012-01-13 17:30:03 +01:00
|
|
|
{
|
2011-05-04 02:20:17 +02:00
|
|
|
int stopCount = stops_.size();
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2011-05-04 02:20:17 +02:00
|
|
|
//use default color if no stops
|
2014-12-02 15:56:40 +01:00
|
|
|
if (stopCount == 0)
|
2012-01-13 17:30:03 +01:00
|
|
|
{
|
2013-09-25 23:58:32 +02:00
|
|
|
return default_color_.rgba();
|
2011-05-04 02:20:17 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2011-05-04 02:20:17 +02:00
|
|
|
//1 - Find the stop that the value is in
|
|
|
|
int stopIdx = -1;
|
|
|
|
bool foundStopIdx = false;
|
2012-02-02 02:53:35 +01:00
|
|
|
|
|
|
|
for(int i=0; i<stopCount; ++i)
|
2012-01-13 17:30:03 +01:00
|
|
|
{
|
2012-02-02 02:53:35 +01:00
|
|
|
if(value < stops_[i].get_value())
|
2012-01-13 17:30:03 +01:00
|
|
|
{
|
2011-05-04 02:20:17 +02:00
|
|
|
stopIdx = i-1;
|
|
|
|
foundStopIdx = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
|
|
|
if(!foundStopIdx)
|
2012-01-13 17:30:03 +01:00
|
|
|
{
|
2011-05-04 02:20:17 +02:00
|
|
|
stopIdx = stopCount-1;
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2011-05-04 02:20:17 +02:00
|
|
|
//2 - Find the next stop
|
|
|
|
int nextStopIdx = stopIdx + 1;
|
2012-02-02 02:53:35 +01:00
|
|
|
if(nextStopIdx >= stopCount)
|
|
|
|
{
|
2012-01-13 17:30:03 +01:00
|
|
|
//there is no next stop
|
2011-05-04 02:20:17 +02:00
|
|
|
nextStopIdx = stopCount - 1;
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2011-05-04 02:20:17 +02:00
|
|
|
//3 - Work out the mode
|
|
|
|
colorizer_mode stopMode;
|
2012-02-02 02:53:35 +01:00
|
|
|
if( stopIdx == -1 )
|
|
|
|
{
|
2012-01-13 17:30:03 +01:00
|
|
|
//before the first stop
|
2011-05-04 02:20:17 +02:00
|
|
|
stopMode = default_mode_;
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
else
|
2012-01-13 17:30:03 +01:00
|
|
|
{
|
2011-05-04 02:20:17 +02:00
|
|
|
stopMode = stops_[stopIdx].get_mode();
|
2012-02-02 02:53:35 +01:00
|
|
|
if(stopMode == COLORIZER_INHERIT)
|
2012-01-13 17:30:03 +01:00
|
|
|
{
|
2011-05-04 02:20:17 +02:00
|
|
|
stopMode = default_mode_;
|
|
|
|
}
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2011-05-04 02:20:17 +02:00
|
|
|
//4 - Calculate the colour
|
|
|
|
color stopColor;
|
|
|
|
color nextStopColor;
|
|
|
|
float stopValue = 0;
|
|
|
|
float nextStopValue = 0;
|
|
|
|
color outputColor = get_default_color();
|
2012-02-02 02:53:35 +01:00
|
|
|
if(stopIdx == -1)
|
2012-01-13 17:30:03 +01:00
|
|
|
{
|
2011-05-04 02:20:17 +02:00
|
|
|
stopColor = default_color_;
|
|
|
|
nextStopColor = stops_[nextStopIdx].get_color();
|
|
|
|
stopValue = value;
|
|
|
|
nextStopValue = stops_[nextStopIdx].get_value();
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
else
|
2012-01-13 17:30:03 +01:00
|
|
|
{
|
2011-05-04 02:20:17 +02:00
|
|
|
stopColor = stops_[stopIdx].get_color();
|
|
|
|
nextStopColor = stops_[nextStopIdx].get_color();
|
|
|
|
stopValue = stops_[stopIdx].get_value();
|
|
|
|
nextStopValue = stops_[nextStopIdx].get_value();
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
|
|
|
switch(stopMode)
|
2012-01-13 17:30:03 +01:00
|
|
|
{
|
2011-05-04 02:20:17 +02:00
|
|
|
case COLORIZER_LINEAR:
|
2012-01-13 17:30:03 +01:00
|
|
|
{
|
|
|
|
//deal with this separately so we don't have to worry about div0
|
2012-02-02 02:53:35 +01:00
|
|
|
if(nextStopValue == stopValue)
|
2011-05-04 02:20:17 +02:00
|
|
|
{
|
2012-01-13 17:30:03 +01:00
|
|
|
outputColor = stopColor;
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
else
|
2012-01-13 17:30:03 +01:00
|
|
|
{
|
|
|
|
float fraction = (value - stopValue) / (nextStopValue - stopValue);
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2012-01-13 17:30:03 +01:00
|
|
|
unsigned r = interpolate(stopColor.red(), nextStopColor.red(),fraction);
|
|
|
|
unsigned g = interpolate(stopColor.green(), nextStopColor.green(),fraction);
|
|
|
|
unsigned b = interpolate(stopColor.blue(), nextStopColor.blue(),fraction);
|
|
|
|
unsigned a = interpolate(stopColor.alpha(), nextStopColor.alpha(),fraction);
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2012-01-13 17:30:03 +01:00
|
|
|
outputColor.set_red(r);
|
|
|
|
outputColor.set_green(g);
|
|
|
|
outputColor.set_blue(b);
|
|
|
|
outputColor.set_alpha(a);
|
2011-05-04 02:20:17 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2012-01-13 17:30:03 +01:00
|
|
|
}
|
|
|
|
break;
|
2011-05-04 02:20:17 +02:00
|
|
|
case COLORIZER_DISCRETE:
|
|
|
|
outputColor = stopColor;
|
|
|
|
break;
|
|
|
|
case COLORIZER_EXACT:
|
|
|
|
default:
|
|
|
|
//approximately equal (within epsilon)
|
2013-01-17 22:53:48 +01:00
|
|
|
if(std::fabs(value - stopValue) < epsilon_)
|
2012-01-13 17:30:03 +01:00
|
|
|
{
|
2011-05-04 02:20:17 +02:00
|
|
|
outputColor = stopColor;
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
else
|
2012-01-13 17:30:03 +01:00
|
|
|
{
|
2011-05-04 02:20:17 +02:00
|
|
|
outputColor = default_color_;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
|
|
|
|
2011-05-04 02:20:17 +02:00
|
|
|
/*
|
2012-04-09 21:41:56 +02:00
|
|
|
MAPNIK_LOG_DEBUG(raster_colorizer) << "raster_colorizer: get_color " << value;
|
|
|
|
MAPNIK_LOG_DEBUG(raster_colorizer) << "\tstopIdx: " << stopIdx;
|
|
|
|
MAPNIK_LOG_DEBUG(raster_colorizer) << "\tnextStopIdx: " << nextStopIdx;
|
|
|
|
MAPNIK_LOG_DEBUG(raster_colorizer) << "\tstopValue: " << stopValue;
|
|
|
|
MAPNIK_LOG_DEBUG(raster_colorizer) << "\tnextStopValue: " << nextStopValue;
|
|
|
|
MAPNIK_LOG_DEBUG(raster_colorizer) << "\tstopColor: " << stopColor.to_string();
|
|
|
|
MAPNIK_LOG_DEBUG(raster_colorizer) << "\tnextStopColor: " << nextStopColor.to_string();
|
|
|
|
MAPNIK_LOG_DEBUG(raster_colorizer) << "\tstopMode: " << stopMode.as_string();
|
|
|
|
MAPNIK_LOG_DEBUG(raster_colorizer) << "\toutputColor: " << outputColor.to_string();
|
2012-01-13 17:30:03 +01:00
|
|
|
*/
|
2011-05-04 02:20:17 +02:00
|
|
|
|
2013-09-25 23:58:32 +02:00
|
|
|
return outputColor.rgba();
|
2011-05-04 02:20:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-22 04:08:04 +01:00
|
|
|
template void raster_colorizer::colorize(image_rgba8 & out, image_gray8 const& in,
|
2014-12-11 10:10:32 +01:00
|
|
|
boost::optional<double>const& nodata,
|
|
|
|
feature_impl const& f) const;
|
2015-02-04 22:41:58 +01:00
|
|
|
template void raster_colorizer::colorize(image_rgba8 & out, image_gray8s const& in,
|
|
|
|
boost::optional<double>const& nodata,
|
|
|
|
feature_impl const& f) const;
|
2015-01-22 04:08:04 +01:00
|
|
|
template void raster_colorizer::colorize(image_rgba8 & out, image_gray16 const& in,
|
2014-12-11 10:10:32 +01:00
|
|
|
boost::optional<double>const& nodata,
|
|
|
|
feature_impl const& f) const;
|
2015-02-04 22:41:58 +01:00
|
|
|
template void raster_colorizer::colorize(image_rgba8 & out, image_gray16s const& in,
|
|
|
|
boost::optional<double>const& nodata,
|
|
|
|
feature_impl const& f) const;
|
|
|
|
template void raster_colorizer::colorize(image_rgba8 & out, image_gray32 const& in,
|
|
|
|
boost::optional<double>const& nodata,
|
|
|
|
feature_impl const& f) const;
|
|
|
|
template void raster_colorizer::colorize(image_rgba8 & out, image_gray32s const& in,
|
|
|
|
boost::optional<double>const& nodata,
|
|
|
|
feature_impl const& f) const;
|
2015-01-22 04:08:04 +01:00
|
|
|
template void raster_colorizer::colorize(image_rgba8 & out, image_gray32f const& in,
|
2014-12-11 10:10:32 +01:00
|
|
|
boost::optional<double>const& nodata,
|
|
|
|
feature_impl const& f) const;
|
2015-02-04 22:41:58 +01:00
|
|
|
template void raster_colorizer::colorize(image_rgba8 & out, image_gray64 const& in,
|
|
|
|
boost::optional<double>const& nodata,
|
|
|
|
feature_impl const& f) const;
|
|
|
|
template void raster_colorizer::colorize(image_rgba8 & out, image_gray64s const& in,
|
|
|
|
boost::optional<double>const& nodata,
|
|
|
|
feature_impl const& f) const;
|
|
|
|
template void raster_colorizer::colorize(image_rgba8 & out, image_gray64f const& in,
|
|
|
|
boost::optional<double>const& nodata,
|
|
|
|
feature_impl const& f) const;
|
2014-12-11 10:10:32 +01:00
|
|
|
|
2011-05-04 07:19:58 +02:00
|
|
|
}
|