2006-03-31 12:32:02 +02:00
|
|
|
/*****************************************************************************
|
2012-02-02 02:53:35 +01:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2011-10-23 15:04:25 +02:00
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +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,
|
2005-06-14 17:06:59 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 12:32:02 +02:00
|
|
|
* 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
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
*****************************************************************************/
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-10-08 19:42:41 +02:00
|
|
|
// mapnik
|
2012-04-10 00:51:04 +02:00
|
|
|
#include <mapnik/debug.hpp>
|
2007-10-08 19:42:41 +02:00
|
|
|
#include <mapnik/graphics.hpp>
|
|
|
|
#include <mapnik/image_util.hpp>
|
2010-09-24 17:37:19 +02:00
|
|
|
#include <mapnik/global.hpp>
|
2012-04-04 01:49:05 +02:00
|
|
|
#include <mapnik/color.hpp>
|
2012-06-21 21:44:57 +02:00
|
|
|
|
2012-05-09 13:39:23 +02:00
|
|
|
// agg
|
2012-06-21 21:44:57 +02:00
|
|
|
#include "agg_rendering_buffer.h"
|
2012-05-09 13:39:23 +02:00
|
|
|
#include "agg_pixfmt_rgba.h"
|
2013-07-24 00:44:39 +02:00
|
|
|
#include "agg_color_rgba.h"
|
2012-06-21 21:44:57 +02:00
|
|
|
|
2013-01-09 18:00:30 +01:00
|
|
|
// boost
|
|
|
|
#include <boost/scoped_array.hpp>
|
|
|
|
|
2009-01-15 23:51:12 +01:00
|
|
|
// cairo
|
|
|
|
#ifdef HAVE_CAIRO
|
2013-01-09 18:00:30 +01:00
|
|
|
#include <mapnik/cairo_context.hpp>
|
2009-01-15 23:51:12 +01:00
|
|
|
#endif
|
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
namespace mapnik
|
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
image_32::image_32(int width,int height)
|
|
|
|
:width_(width),
|
|
|
|
height_(height),
|
2011-09-11 08:24:26 +02:00
|
|
|
data_(width,height),
|
2013-07-02 22:01:23 +02:00
|
|
|
painted_(false),
|
|
|
|
premultiplied_(false) {}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
image_32::image_32(const image_32& rhs)
|
|
|
|
:width_(rhs.width_),
|
|
|
|
height_(rhs.height_),
|
2011-09-11 08:24:26 +02:00
|
|
|
data_(rhs.data_),
|
2013-07-02 22:01:23 +02:00
|
|
|
painted_(rhs.painted_),
|
|
|
|
premultiplied_(rhs.premultiplied_) {}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2009-01-28 22:11:10 +01:00
|
|
|
#ifdef HAVE_CAIRO
|
2013-01-09 18:00:30 +01:00
|
|
|
image_32::image_32(cairo_surface_ptr const& surface)
|
|
|
|
:width_(cairo_image_surface_get_width(&*surface)),
|
|
|
|
height_(cairo_image_surface_get_height(&*surface)),
|
2013-07-02 22:01:23 +02:00
|
|
|
data_(width_, height_),
|
|
|
|
premultiplied_(false)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2011-09-11 08:24:26 +02:00
|
|
|
painted_ = true;
|
2013-01-09 18:00:30 +01:00
|
|
|
if ( cairo_image_surface_get_format(&*surface) != CAIRO_FORMAT_ARGB32)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2012-04-10 00:51:04 +02:00
|
|
|
MAPNIK_LOG_WARN(graphics) << "Unable to convert this Cairo format";
|
2013-01-09 18:00:30 +01:00
|
|
|
throw;
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2009-01-15 23:51:12 +01:00
|
|
|
|
2013-01-09 18:00:30 +01:00
|
|
|
int stride = cairo_image_surface_get_stride(&*surface) / 4;
|
2009-01-15 23:51:12 +01:00
|
|
|
|
2011-04-02 05:07:45 +02:00
|
|
|
boost::scoped_array<unsigned int> out_row(new unsigned int[width_]);
|
2013-01-09 18:00:30 +01:00
|
|
|
const unsigned int *in_row = (const unsigned int *)cairo_image_surface_get_data(&*surface);
|
2009-01-15 23:51:12 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
for (unsigned int row = 0; row < height_; row++, in_row += stride)
|
|
|
|
{
|
|
|
|
for (unsigned int column = 0; column < width_; column++)
|
|
|
|
{
|
|
|
|
unsigned int in = in_row[column];
|
|
|
|
unsigned int a = (in >> 24) & 0xff;
|
|
|
|
unsigned int r = (in >> 16) & 0xff;
|
|
|
|
unsigned int g = (in >> 8) & 0xff;
|
|
|
|
unsigned int b = (in >> 0) & 0xff;
|
2009-01-15 23:51:12 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
#define DE_ALPHA(x) do { \
|
|
|
|
if (a == 0) x = 0; \
|
|
|
|
else x = x * 255 / a; \
|
|
|
|
if (x > 255) x = 255; \
|
|
|
|
} while(0)
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
DE_ALPHA(r);
|
|
|
|
DE_ALPHA(g);
|
|
|
|
DE_ALPHA(b);
|
2009-01-15 23:51:12 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
out_row[column] = color(r, g, b, a).rgba();
|
2009-01-15 23:51:12 +01:00
|
|
|
}
|
2011-04-02 05:07:45 +02:00
|
|
|
data_.setRow(row, out_row.get(), width_);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
}
|
2009-01-15 23:51:12 +01:00
|
|
|
#endif
|
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
image_32::~image_32() {}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2010-09-24 17:37:19 +02:00
|
|
|
void image_32::set_grayscale_to_alpha()
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2010-10-24 08:44:48 +02:00
|
|
|
for (unsigned int y = 0; y < height_; ++y)
|
2010-09-24 19:08:12 +02:00
|
|
|
{
|
|
|
|
unsigned int* row_from = data_.getRow(y);
|
2010-10-24 08:44:48 +02:00
|
|
|
for (unsigned int x = 0; x < width_; ++x)
|
2010-09-24 19:08:12 +02:00
|
|
|
{
|
|
|
|
unsigned rgba = row_from[x];
|
2010-10-24 08:44:48 +02:00
|
|
|
// TODO - big endian support
|
2010-09-24 19:08:12 +02:00
|
|
|
unsigned r = rgba & 0xff;
|
|
|
|
unsigned g = (rgba >> 8 ) & 0xff;
|
|
|
|
unsigned b = (rgba >> 16) & 0xff;
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-09-24 19:08:12 +02:00
|
|
|
// magic numbers for grayscale
|
2012-09-01 05:31:06 +02:00
|
|
|
unsigned a = static_cast<unsigned>(std::ceil((r * .3) + (g * .59) + (b * .11)));
|
2010-09-24 17:37:19 +02:00
|
|
|
|
2010-09-24 19:08:12 +02:00
|
|
|
row_from[x] = (a << 24)| (255 << 16) | (255 << 8) | (255) ;
|
|
|
|
}
|
|
|
|
}
|
2010-09-24 17:37:19 +02:00
|
|
|
}
|
|
|
|
|
2012-04-04 01:49:05 +02:00
|
|
|
void image_32::set_color_to_alpha(const color& c)
|
2010-09-24 17:37:19 +02:00
|
|
|
{
|
2012-04-04 01:49:05 +02:00
|
|
|
for (unsigned y = 0; y < height_; ++y)
|
|
|
|
{
|
|
|
|
unsigned int* row_from = data_.getRow(y);
|
|
|
|
for (unsigned x = 0; x < width_; ++x)
|
|
|
|
{
|
|
|
|
unsigned rgba = row_from[x];
|
|
|
|
unsigned r = rgba & 0xff;
|
|
|
|
unsigned g = (rgba >> 8 ) & 0xff;
|
|
|
|
unsigned b = (rgba >> 16) & 0xff;
|
|
|
|
if (r == c.red() && g == c.green() && b == c.blue())
|
|
|
|
{
|
|
|
|
row_from[x] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-24 17:37:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void image_32::set_alpha(float opacity)
|
|
|
|
{
|
2013-01-09 18:00:30 +01:00
|
|
|
for (unsigned int y = 0; y < height_; ++y)
|
2010-09-24 18:12:23 +02:00
|
|
|
{
|
2013-01-09 18:00:30 +01:00
|
|
|
unsigned int* row_to = data_.getRow(y);
|
|
|
|
for (unsigned int x = 0; x < width_; ++x)
|
2010-09-24 18:12:23 +02:00
|
|
|
{
|
2013-01-09 18:00:30 +01:00
|
|
|
unsigned rgba = row_to[x];
|
2010-09-24 18:12:23 +02:00
|
|
|
|
|
|
|
#ifdef MAPNIK_BIG_ENDIAN
|
2013-01-09 18:00:30 +01:00
|
|
|
unsigned a0 = (rgba & 0xff);
|
|
|
|
unsigned a1 = int( (rgba & 0xff) * opacity );
|
2010-09-24 18:12:23 +02:00
|
|
|
|
2013-01-09 18:00:30 +01:00
|
|
|
if (a0 == a1) continue;
|
2010-09-24 18:12:23 +02:00
|
|
|
|
2013-01-09 18:00:30 +01:00
|
|
|
unsigned r = (rgba >> 24) & 0xff;
|
|
|
|
unsigned g = (rgba >> 16 ) & 0xff;
|
|
|
|
unsigned b = (rgba >> 8) & 0xff;
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2013-01-09 18:00:30 +01:00
|
|
|
row_to[x] = (a1) | (b << 8) | (g << 16) | (r << 24) ;
|
2010-09-24 18:12:23 +02:00
|
|
|
|
|
|
|
#else
|
2013-01-09 18:00:30 +01:00
|
|
|
unsigned a0 = (rgba >> 24) & 0xff;
|
|
|
|
unsigned a1 = int( ((rgba >> 24) & 0xff) * opacity );
|
|
|
|
//unsigned a1 = opacity;
|
|
|
|
if (a0 == a1) continue;
|
2010-09-24 18:12:23 +02:00
|
|
|
|
2013-01-09 18:00:30 +01:00
|
|
|
unsigned r = rgba & 0xff;
|
|
|
|
unsigned g = (rgba >> 8 ) & 0xff;
|
|
|
|
unsigned b = (rgba >> 16) & 0xff;
|
2010-09-24 18:12:23 +02:00
|
|
|
|
2013-01-09 18:00:30 +01:00
|
|
|
row_to[x] = (a1 << 24)| (b << 16) | (g << 8) | (r) ;
|
2010-09-24 18:12:23 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2011-09-11 08:24:26 +02:00
|
|
|
void image_32::set_background(const color& c)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2011-09-11 08:24:26 +02:00
|
|
|
background_=c;
|
|
|
|
data_.set(background_->rgba());
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2011-09-11 08:24:26 +02:00
|
|
|
boost::optional<color> const& image_32::get_background() const
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
return background_;
|
|
|
|
}
|
2011-09-11 08:24:26 +02:00
|
|
|
|
2012-06-21 21:44:57 +02:00
|
|
|
void image_32::premultiply()
|
|
|
|
{
|
|
|
|
agg::rendering_buffer buffer(data_.getBytes(),width_,height_,width_ * 4);
|
|
|
|
agg::pixfmt_rgba32 pixf(buffer);
|
|
|
|
pixf.premultiply();
|
2013-07-02 22:01:23 +02:00
|
|
|
premultiplied_ = true;
|
2012-06-21 21:44:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void image_32::demultiply()
|
|
|
|
{
|
|
|
|
agg::rendering_buffer buffer(data_.getBytes(),width_,height_,width_ * 4);
|
2013-07-24 00:44:39 +02:00
|
|
|
agg::pixfmt_rgba32_pre pixf(buffer);
|
2012-06-21 21:44:57 +02:00
|
|
|
pixf.demultiply();
|
2013-07-02 22:01:23 +02:00
|
|
|
premultiplied_ = false;
|
2012-06-21 21:44:57 +02:00
|
|
|
}
|
|
|
|
|
2012-05-09 13:39:23 +02:00
|
|
|
void image_32::composite_pixel(unsigned op, int x,int y, unsigned c, unsigned cover, double opacity)
|
|
|
|
{
|
|
|
|
typedef agg::rgba8 color_type;
|
|
|
|
typedef color_type::value_type value_type;
|
|
|
|
typedef agg::order_rgba order_type;
|
|
|
|
typedef agg::comp_op_adaptor_rgba<color_type,order_type> blender_type;
|
2013-01-09 18:00:30 +01:00
|
|
|
|
2012-05-09 13:39:23 +02:00
|
|
|
if (checkBounds(x,y))
|
|
|
|
{
|
|
|
|
unsigned rgba = data_(x,y);
|
|
|
|
unsigned ca = (unsigned)(((c >> 24) & 0xff) * opacity);
|
|
|
|
unsigned cb = (c >> 16 ) & 0xff;
|
|
|
|
unsigned cg = (c >> 8) & 0xff;
|
|
|
|
unsigned cr = (c & 0xff);
|
2013-01-09 18:00:30 +01:00
|
|
|
blender_type::blend_pix(op, (value_type*)&rgba, cr, cg, cb, ca, cover);
|
|
|
|
data_(x,y) = rgba;
|
2012-05-09 13:39:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|