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
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* Copyright (C) 2006 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
|
|
|
//$Id: graphics.cpp 17 2005-03-08 23:58:43Z pavlenko $
|
|
|
|
|
2007-10-08 19:42:41 +02:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/graphics.hpp>
|
|
|
|
#include <mapnik/image_util.hpp>
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2009-01-15 23:51:12 +01:00
|
|
|
// cairo
|
|
|
|
#ifdef HAVE_CAIRO
|
|
|
|
#include <cairomm/surface.h>
|
|
|
|
#endif
|
|
|
|
|
2009-07-08 23:26:40 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
namespace mapnik
|
|
|
|
{
|
2009-12-16 21:02:06 +01:00
|
|
|
image_32::image_32(int width,int height)
|
2005-06-14 17:06:59 +02:00
|
|
|
:width_(width),
|
2006-10-04 13:22:18 +02:00
|
|
|
height_(height),
|
|
|
|
data_(width,height) {}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2009-12-16 21:02:06 +01:00
|
|
|
image_32::image_32(const image_32& rhs)
|
2005-06-14 17:06:59 +02:00
|
|
|
:width_(rhs.width_),
|
2006-10-04 13:22:18 +02:00
|
|
|
height_(rhs.height_),
|
2008-02-19 12:27:06 +01:00
|
|
|
data_(rhs.data_) {}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2009-01-28 22:11:10 +01:00
|
|
|
#ifdef HAVE_CAIRO
|
2009-12-16 21:02:06 +01:00
|
|
|
image_32::image_32(Cairo::RefPtr<Cairo::ImageSurface> rhs)
|
2009-01-15 23:51:12 +01:00
|
|
|
:width_(rhs->get_width()),
|
|
|
|
height_(rhs->get_height()),
|
|
|
|
data_(rhs->get_width(),rhs->get_height())
|
|
|
|
{
|
|
|
|
if (rhs->get_format() != Cairo::FORMAT_ARGB32)
|
|
|
|
{
|
|
|
|
std::cerr << "Unable to convert this Cairo format\n";
|
|
|
|
return; // throw exception ??
|
|
|
|
}
|
|
|
|
|
|
|
|
int stride = rhs->get_stride() / 4;
|
|
|
|
|
|
|
|
unsigned int out_row[width_];
|
|
|
|
const unsigned int *in_row = (const unsigned int *)rhs->get_data();
|
|
|
|
|
|
|
|
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-12-16 21:02:06 +01:00
|
|
|
#define DE_ALPHA(x) do { \
|
|
|
|
if (a == 0) x = 0; \
|
|
|
|
else x = x * 255 / a; \
|
|
|
|
if (x > 255) x = 255; \
|
2009-01-15 23:51:12 +01:00
|
|
|
} while(0)
|
2009-12-16 21:02:06 +01:00
|
|
|
|
2009-01-15 23:51:12 +01:00
|
|
|
DE_ALPHA(r);
|
|
|
|
DE_ALPHA(g);
|
|
|
|
DE_ALPHA(b);
|
|
|
|
|
|
|
|
out_row[column] = color(r, g, b, a).rgba();
|
|
|
|
}
|
|
|
|
data_.setRow(row, out_row, width_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-12-16 21:02:06 +01:00
|
|
|
image_32::~image_32() {}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2009-12-16 21:02:06 +01:00
|
|
|
const image_data_32& image_32::data() const
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
|
|
|
return data_;
|
|
|
|
}
|
|
|
|
|
2009-12-16 21:02:06 +01:00
|
|
|
void image_32::set_background(const color& background)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
|
|
|
background_=background;
|
|
|
|
data_.set(background_.rgba());
|
|
|
|
}
|
|
|
|
|
2009-12-16 21:02:06 +01:00
|
|
|
const color& image_32::get_background() const
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
|
|
|
return background_;
|
|
|
|
}
|
|
|
|
}
|