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
|
|
|
*
|
2014-11-20 15:25:50 +01:00
|
|
|
* Copyright (C) 2014 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
|
|
|
|
2013-04-12 13:46:40 +02:00
|
|
|
// mapnik
|
2012-04-08 02:20:56 +02:00
|
|
|
#include <mapnik/debug.hpp>
|
2006-10-04 13:22:18 +02:00
|
|
|
#include <mapnik/image_reader.hpp>
|
2006-08-31 23:32:07 +02:00
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
#include <png.h>
|
2006-08-31 23:32:07 +02:00
|
|
|
}
|
2013-04-12 13:46:40 +02:00
|
|
|
// boost
|
2014-10-22 01:37:27 +02:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-local-typedef"
|
2013-04-12 13:46:40 +02:00
|
|
|
#include <boost/iostreams/device/file.hpp>
|
2013-04-15 15:01:33 +02:00
|
|
|
#include <boost/iostreams/device/array.hpp>
|
2013-04-12 13:46:40 +02:00
|
|
|
#include <boost/iostreams/stream.hpp>
|
2014-10-22 01:37:27 +02:00
|
|
|
#pragma GCC diagnostic pop
|
2009-07-03 11:25:18 +02:00
|
|
|
|
2013-09-20 16:01:58 +02:00
|
|
|
// stl
|
2014-01-29 00:05:10 +01:00
|
|
|
#include <cstring>
|
2013-09-20 16:01:58 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
namespace mapnik
|
|
|
|
{
|
2013-04-12 13:46:40 +02:00
|
|
|
|
|
|
|
template <typename T>
|
2013-04-09 12:39:16 +02:00
|
|
|
class png_reader : public image_reader
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2014-07-07 19:23:15 +02:00
|
|
|
using source_type = T;
|
|
|
|
using input_stream = boost::iostreams::stream<source_type>;
|
2013-04-09 12:39:16 +02:00
|
|
|
|
2013-04-10 02:37:44 +02:00
|
|
|
struct png_struct_guard
|
|
|
|
{
|
|
|
|
png_struct_guard(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
|
|
|
|
: p_(png_ptr_ptr),
|
|
|
|
i_(info_ptr_ptr) {}
|
|
|
|
|
|
|
|
~png_struct_guard()
|
|
|
|
{
|
|
|
|
png_destroy_read_struct(p_,i_,0);
|
|
|
|
}
|
|
|
|
png_structpp p_;
|
|
|
|
png_infopp i_;
|
|
|
|
};
|
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
private:
|
2013-04-12 13:46:40 +02:00
|
|
|
|
|
|
|
source_type source_;
|
2013-05-13 11:26:04 +02:00
|
|
|
input_stream stream_;
|
2010-06-02 13:03:30 +02:00
|
|
|
unsigned width_;
|
|
|
|
unsigned height_;
|
|
|
|
int bit_depth_;
|
|
|
|
int color_type_;
|
2014-06-12 03:50:58 +02:00
|
|
|
bool has_alpha_;
|
2010-06-02 13:03:30 +02:00
|
|
|
public:
|
2013-04-12 13:46:40 +02:00
|
|
|
explicit png_reader(std::string const& file_name);
|
2013-05-13 11:26:04 +02:00
|
|
|
png_reader(char const* data, std::size_t size);
|
2010-06-02 13:03:30 +02:00
|
|
|
~png_reader();
|
2014-12-03 10:44:56 +01:00
|
|
|
unsigned width() const final;
|
|
|
|
unsigned height() const final;
|
2014-12-10 12:53:39 +01:00
|
|
|
boost::optional<box2d<double> > bounding_box() const final;
|
2014-12-03 10:44:56 +01:00
|
|
|
inline bool has_alpha() const final { return has_alpha_; }
|
2015-01-22 03:31:02 +01:00
|
|
|
void read(unsigned x,unsigned y,image_rgba8& image) final;
|
2015-01-22 02:40:12 +01:00
|
|
|
image_any read(unsigned x, unsigned y, unsigned width, unsigned height) final;
|
2010-06-02 13:03:30 +02:00
|
|
|
private:
|
|
|
|
void init();
|
2013-04-12 13:46:40 +02:00
|
|
|
static void png_read_data(png_structp png_ptr, png_bytep data, png_size_t length);
|
2010-06-02 13:03:30 +02:00
|
|
|
};
|
2012-02-02 02:53:35 +01:00
|
|
|
|
|
|
|
namespace
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2013-04-12 13:46:40 +02:00
|
|
|
|
2012-09-03 19:27:48 +02:00
|
|
|
image_reader* create_png_reader(std::string const& file)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2013-04-12 17:10:09 +02:00
|
|
|
return new png_reader<boost::iostreams::file_source>(file);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2013-04-12 13:46:40 +02:00
|
|
|
image_reader* create_png_reader2(char const * data, std::size_t size)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2013-04-12 13:46:40 +02:00
|
|
|
return new png_reader<boost::iostreams::array_source>(data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool registered = register_image_reader("png",create_png_reader);
|
|
|
|
const bool registered2 = register_image_reader("png", create_png_reader2);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-04-05 10:27:45 +02:00
|
|
|
|
2013-07-24 00:46:54 +02:00
|
|
|
void user_error_fn(png_structp /*png_ptr*/, png_const_charp error_msg)
|
2012-06-16 21:50:46 +02:00
|
|
|
{
|
2013-07-15 06:19:03 +02:00
|
|
|
throw image_reader_exception(std::string("failed to read invalid png: '") + error_msg + "'");
|
2012-06-16 21:50:46 +02:00
|
|
|
}
|
|
|
|
|
2013-07-24 00:46:54 +02:00
|
|
|
void user_warning_fn(png_structp /*png_ptr*/, png_const_charp warning_msg)
|
2012-06-16 21:50:46 +02:00
|
|
|
{
|
|
|
|
MAPNIK_LOG_DEBUG(png_reader) << "libpng warning: '" << warning_msg << "'";
|
|
|
|
}
|
|
|
|
|
2013-04-12 13:46:40 +02:00
|
|
|
template <typename T>
|
|
|
|
void png_reader<T>::png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2013-05-13 11:26:04 +02:00
|
|
|
input_stream * fin = reinterpret_cast<input_stream*>(png_get_io_ptr(png_ptr));
|
2013-04-12 13:46:40 +02:00
|
|
|
fin->read(reinterpret_cast<char*>(data), length);
|
2013-10-01 20:47:47 +02:00
|
|
|
std::streamsize read_count = fin->gcount();
|
|
|
|
if (read_count < 0 || static_cast<png_size_t>(read_count) != length)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
png_error(png_ptr, "Read Error");
|
2006-04-05 10:27:45 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2013-04-12 13:46:40 +02:00
|
|
|
template <typename T>
|
|
|
|
png_reader<T>::png_reader(std::string const& file_name)
|
|
|
|
: source_(file_name,std::ios_base::in | std::ios_base::binary),
|
|
|
|
stream_(source_),
|
|
|
|
width_(0),
|
|
|
|
height_(0),
|
|
|
|
bit_depth_(0),
|
2014-06-12 03:50:58 +02:00
|
|
|
color_type_(0),
|
|
|
|
has_alpha_(false)
|
2013-04-12 13:46:40 +02:00
|
|
|
{
|
2013-11-07 01:26:42 +01:00
|
|
|
if (!source_.is_open()) throw image_reader_exception("PNG reader: cannot open file '"+ file_name + "'");
|
|
|
|
if (!stream_) throw image_reader_exception("PNG reader: cannot open file '"+ file_name + "'");
|
2013-04-12 13:46:40 +02:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
png_reader<T>::png_reader(char const* data, std::size_t size)
|
|
|
|
: source_(data,size),
|
|
|
|
stream_(source_),
|
|
|
|
width_(0),
|
|
|
|
height_(0),
|
|
|
|
bit_depth_(0),
|
2014-06-12 03:50:58 +02:00
|
|
|
color_type_(0),
|
|
|
|
has_alpha_(false)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2013-04-09 12:39:16 +02:00
|
|
|
|
2013-11-07 00:34:26 +01:00
|
|
|
if (!stream_) throw image_reader_exception("PNG reader: cannot open image stream");
|
2013-04-12 13:46:40 +02:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
png_reader<T>::~png_reader() {}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void png_reader<T>::init()
|
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
png_byte header[8];
|
2014-01-29 00:05:10 +01:00
|
|
|
std::memset(header,0,8);
|
2013-04-12 13:46:40 +02:00
|
|
|
stream_.read(reinterpret_cast<char*>(header),8);
|
|
|
|
if ( stream_.gcount() != 8)
|
2011-04-02 01:54:58 +02:00
|
|
|
{
|
2013-11-07 00:34:26 +01:00
|
|
|
throw image_reader_exception("PNG reader: Could not read image");
|
2011-04-02 01:54:58 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
int is_png=!png_sig_cmp(header,0,8);
|
|
|
|
if (!is_png)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2013-06-04 00:38:30 +02:00
|
|
|
throw image_reader_exception("File or stream is not a png");
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
png_structp png_ptr = png_create_read_struct
|
|
|
|
(PNG_LIBPNG_VER_STRING,0,0,0);
|
2006-04-05 10:27:45 +02:00
|
|
|
|
2012-02-02 02:53:35 +01:00
|
|
|
if (!png_ptr)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
throw image_reader_exception("failed to allocate png_ptr");
|
|
|
|
}
|
2012-06-16 21:50:46 +02:00
|
|
|
|
|
|
|
// catch errors in a custom way to avoid the need for setjmp
|
|
|
|
png_set_error_fn(png_ptr, png_get_error_ptr(png_ptr), user_error_fn, user_warning_fn);
|
|
|
|
|
|
|
|
png_infop info_ptr;
|
2013-04-10 02:37:44 +02:00
|
|
|
png_struct_guard sguard(&png_ptr,&info_ptr);
|
|
|
|
info_ptr = png_create_info_struct(png_ptr);
|
2013-04-10 02:43:15 +02:00
|
|
|
if (!info_ptr) throw image_reader_exception("failed to create info_ptr");
|
2012-06-04 23:19:00 +02:00
|
|
|
|
2013-04-12 13:46:40 +02:00
|
|
|
png_set_read_fn(png_ptr, (png_voidp)&stream_, png_read_data);
|
2006-04-05 10:27:45 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
png_set_sig_bytes(png_ptr,8);
|
|
|
|
png_read_info(png_ptr, info_ptr);
|
2006-04-05 10:27:45 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
png_uint_32 width, height;
|
|
|
|
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth_, &color_type_,0,0,0);
|
2014-06-12 03:50:58 +02:00
|
|
|
has_alpha_ = (color_type_ & PNG_COLOR_MASK_ALPHA) || png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS);
|
2010-06-02 13:03:30 +02:00
|
|
|
width_=width;
|
|
|
|
height_=height;
|
2012-04-08 02:20:56 +02:00
|
|
|
|
2012-04-09 21:41:56 +02:00
|
|
|
MAPNIK_LOG_DEBUG(png_reader) << "png_reader: bit_depth=" << bit_depth_ << ",color_type=" << color_type_;
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
|
2013-04-12 13:46:40 +02:00
|
|
|
template <typename T>
|
|
|
|
unsigned png_reader<T>::width() const
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
return width_;
|
|
|
|
}
|
|
|
|
|
2013-04-12 13:46:40 +02:00
|
|
|
template <typename T>
|
|
|
|
unsigned png_reader<T>::height() const
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
return height_;
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2014-12-10 10:44:51 +01:00
|
|
|
template <typename T>
|
2014-12-10 12:53:39 +01:00
|
|
|
boost::optional<box2d<double> > png_reader<T>::bounding_box() const
|
2014-12-10 10:44:51 +01:00
|
|
|
{
|
2014-12-10 12:53:39 +01:00
|
|
|
return boost::optional<box2d<double> >();
|
2014-12-10 10:44:51 +01:00
|
|
|
}
|
|
|
|
|
2013-04-12 13:46:40 +02:00
|
|
|
template <typename T>
|
2015-01-22 03:31:02 +01:00
|
|
|
void png_reader<T>::read(unsigned x0, unsigned y0,image_rgba8& image)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2013-04-12 13:46:40 +02:00
|
|
|
stream_.clear();
|
|
|
|
stream_.seekg(0, std::ios_base::beg);
|
2010-06-02 13:03:30 +02:00
|
|
|
|
|
|
|
png_structp png_ptr = png_create_read_struct
|
|
|
|
(PNG_LIBPNG_VER_STRING,0,0,0);
|
2006-04-05 10:27:45 +02:00
|
|
|
|
2012-02-02 02:53:35 +01:00
|
|
|
if (!png_ptr)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
throw image_reader_exception("failed to allocate png_ptr");
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2006-04-05 10:27:45 +02:00
|
|
|
|
2012-06-16 21:50:46 +02:00
|
|
|
// catch errors in a custom way to avoid the need for setjmp
|
|
|
|
png_set_error_fn(png_ptr, png_get_error_ptr(png_ptr), user_error_fn, user_warning_fn);
|
|
|
|
|
|
|
|
png_infop info_ptr;
|
2013-04-10 02:37:44 +02:00
|
|
|
png_struct_guard sguard(&png_ptr,&info_ptr);
|
|
|
|
info_ptr = png_create_info_struct(png_ptr);
|
2013-04-10 02:43:15 +02:00
|
|
|
if (!info_ptr) throw image_reader_exception("failed to create info_ptr");
|
2012-06-04 23:19:00 +02:00
|
|
|
|
2013-04-12 13:46:40 +02:00
|
|
|
png_set_read_fn(png_ptr, (png_voidp)&stream_, png_read_data);
|
2010-06-02 13:03:30 +02:00
|
|
|
png_read_info(png_ptr, info_ptr);
|
|
|
|
|
|
|
|
if (color_type_ == PNG_COLOR_TYPE_PALETTE)
|
|
|
|
png_set_expand(png_ptr);
|
|
|
|
if (color_type_ == PNG_COLOR_TYPE_GRAY && bit_depth_ < 8)
|
|
|
|
png_set_expand(png_ptr);
|
|
|
|
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
|
|
|
|
png_set_expand(png_ptr);
|
|
|
|
if (bit_depth_ == 16)
|
|
|
|
png_set_strip_16(png_ptr);
|
|
|
|
if (color_type_ == PNG_COLOR_TYPE_GRAY ||
|
|
|
|
color_type_ == PNG_COLOR_TYPE_GRAY_ALPHA)
|
|
|
|
png_set_gray_to_rgb(png_ptr);
|
|
|
|
|
|
|
|
// quick hack -- only work in >=libpng 1.2.7
|
|
|
|
png_set_add_alpha(png_ptr,0xff,PNG_FILLER_AFTER); //rgba
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
double gamma;
|
|
|
|
if (png_get_gAMA(png_ptr, info_ptr, &gamma))
|
|
|
|
png_set_gamma(png_ptr, 2.2, gamma);
|
|
|
|
|
2012-04-10 16:47:29 +02:00
|
|
|
if (x0 == 0 && y0 == 0 && image.width() >= width_ && image.height() >= height_)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2012-04-10 16:47:29 +02:00
|
|
|
|
|
|
|
if (png_get_interlace_type(png_ptr,info_ptr) == PNG_INTERLACE_ADAM7)
|
|
|
|
{
|
|
|
|
png_set_interlace_handling(png_ptr); // FIXME: libpng bug?
|
|
|
|
// according to docs png_read_image
|
|
|
|
// "..automatically handles interlacing,
|
|
|
|
// so you don't need to call png_set_interlace_handling()"
|
|
|
|
}
|
|
|
|
png_read_update_info(png_ptr, info_ptr);
|
|
|
|
// we can read whole image at once
|
|
|
|
// alloc row pointers
|
2013-09-20 16:01:58 +02:00
|
|
|
const std::unique_ptr<png_bytep[]> rows(new png_bytep[height_]);
|
2012-04-10 16:47:29 +02:00
|
|
|
for (unsigned i=0; i<height_; ++i)
|
2015-05-04 12:49:11 +02:00
|
|
|
rows[i] = (png_bytep)image.get_row(i);
|
2012-04-10 16:47:29 +02:00
|
|
|
png_read_image(png_ptr, rows.get());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
png_read_update_info(png_ptr, info_ptr);
|
2013-04-08 18:25:22 +02:00
|
|
|
unsigned w=std::min(unsigned(image.width()),width_ - x0);
|
|
|
|
unsigned h=std::min(unsigned(image.height()),height_ - y0);
|
2012-04-10 16:47:29 +02:00
|
|
|
unsigned rowbytes=png_get_rowbytes(png_ptr, info_ptr);
|
2013-09-20 16:01:58 +02:00
|
|
|
const std::unique_ptr<png_byte[]> row(new png_byte[rowbytes]);
|
2012-04-10 16:47:29 +02:00
|
|
|
//START read image rows
|
2013-04-08 18:25:22 +02:00
|
|
|
for (unsigned i = 0;i < height_; ++i)
|
2006-04-05 10:27:45 +02:00
|
|
|
{
|
2012-04-10 16:47:29 +02:00
|
|
|
png_read_row(png_ptr,row.get(),0);
|
2013-04-08 18:25:22 +02:00
|
|
|
if (i >= y0 && i < (y0 + h))
|
2012-04-10 16:47:29 +02:00
|
|
|
{
|
2015-05-04 12:49:11 +02:00
|
|
|
image.set_row(i-y0,reinterpret_cast<unsigned*>(&row[x0 * 4]),w);
|
2012-04-10 16:47:29 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
}
|
2012-04-10 16:47:29 +02:00
|
|
|
//END
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
png_read_end(png_ptr,0);
|
|
|
|
}
|
2014-12-03 10:44:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2015-01-22 02:40:12 +01:00
|
|
|
image_any png_reader<T>::read(unsigned x, unsigned y, unsigned width, unsigned height)
|
2014-12-03 10:44:56 +01:00
|
|
|
{
|
2015-01-22 03:31:02 +01:00
|
|
|
image_rgba8 data(width,height);
|
2014-12-03 10:56:19 +01:00
|
|
|
read(x, y, data);
|
2015-01-22 02:40:12 +01:00
|
|
|
return image_any(std::move(data));
|
2014-12-03 10:44:56 +01:00
|
|
|
}
|
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|