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
|
|
|
|
2006-10-04 13:22:18 +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>
|
2013-06-03 04:28:24 +02:00
|
|
|
|
2014-10-22 01:37:27 +02:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#include <tiffio.h>
|
|
|
|
}
|
2013-06-03 04:28:24 +02:00
|
|
|
|
2014-10-22 01:37:27 +02:00
|
|
|
// boost
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-local-typedef"
|
2013-05-13 11:26:28 +02:00
|
|
|
#include <boost/iostreams/device/file.hpp>
|
|
|
|
#include <boost/iostreams/device/array.hpp>
|
|
|
|
#include <boost/iostreams/stream.hpp>
|
2014-10-22 01:37:27 +02:00
|
|
|
#pragma GCC diagnostic pop
|
2009-12-16 21:02:06 +01:00
|
|
|
|
2014-10-22 01:37:27 +02:00
|
|
|
// stl
|
|
|
|
#include <memory>
|
2006-08-31 23:32:07 +02:00
|
|
|
|
2013-08-30 10:46:09 +02:00
|
|
|
namespace mapnik { namespace impl {
|
2006-04-05 10:27:45 +02:00
|
|
|
|
2014-12-17 18:31:29 +01:00
|
|
|
static toff_t tiff_seek_proc(thandle_t handle, toff_t off, int whence)
|
2013-05-13 11:26:28 +02:00
|
|
|
{
|
2014-12-17 18:31:29 +01:00
|
|
|
std::istream* in = reinterpret_cast<std::istream*>(handle);
|
2013-05-13 11:26:28 +02:00
|
|
|
|
|
|
|
switch(whence)
|
|
|
|
{
|
|
|
|
case SEEK_SET:
|
|
|
|
in->seekg(off, std::ios_base::beg);
|
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
|
|
|
in->seekg(off, std::ios_base::cur);
|
|
|
|
break;
|
|
|
|
case SEEK_END:
|
|
|
|
in->seekg(off, std::ios_base::end);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return static_cast<toff_t>(in->tellg());
|
|
|
|
}
|
|
|
|
|
2013-08-30 10:46:09 +02:00
|
|
|
static int tiff_close_proc(thandle_t)
|
2013-05-13 11:26:28 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-17 18:31:29 +01:00
|
|
|
static toff_t tiff_size_proc(thandle_t handle)
|
2013-05-13 11:26:28 +02:00
|
|
|
{
|
2014-12-17 18:31:29 +01:00
|
|
|
std::istream* in = reinterpret_cast<std::istream*>(handle);
|
2013-05-13 11:26:28 +02:00
|
|
|
std::ios::pos_type pos = in->tellg();
|
|
|
|
in->seekg(0, std::ios::end);
|
|
|
|
std::ios::pos_type len = in->tellg();
|
|
|
|
in->seekg(pos);
|
|
|
|
return static_cast<toff_t>(len);
|
|
|
|
}
|
|
|
|
|
2014-12-17 18:31:29 +01:00
|
|
|
static tsize_t tiff_read_proc(thandle_t handle, tdata_t buf, tsize_t size)
|
2013-05-13 11:26:28 +02:00
|
|
|
{
|
2014-12-17 18:31:29 +01:00
|
|
|
std::istream * in = reinterpret_cast<std::istream*>(handle);
|
2013-05-13 11:26:28 +02:00
|
|
|
std::streamsize request_size = size;
|
2013-05-13 20:00:21 +02:00
|
|
|
if (static_cast<tsize_t>(request_size) != size)
|
|
|
|
return static_cast<tsize_t>(-1);
|
2013-05-13 11:26:28 +02:00
|
|
|
in->read(reinterpret_cast<char*>(buf), request_size);
|
2013-05-13 20:00:21 +02:00
|
|
|
return static_cast<tsize_t>(in->gcount());
|
2013-05-13 11:26:28 +02:00
|
|
|
}
|
|
|
|
|
2013-08-30 10:46:09 +02:00
|
|
|
static tsize_t tiff_write_proc(thandle_t , tdata_t , tsize_t)
|
2013-05-13 11:26:28 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-30 10:46:09 +02:00
|
|
|
static void tiff_unmap_proc(thandle_t, tdata_t, toff_t)
|
2013-05-13 11:26:28 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-30 10:46:09 +02:00
|
|
|
static int tiff_map_proc(thandle_t, tdata_t* , toff_t*)
|
2013-05-13 11:26:28 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2010-06-02 13:03:30 +02:00
|
|
|
class tiff_reader : public image_reader
|
|
|
|
{
|
2014-07-07 19:23:15 +02:00
|
|
|
using tiff_ptr = std::shared_ptr<TIFF>;
|
|
|
|
using source_type = T;
|
|
|
|
using input_stream = boost::iostreams::stream<source_type>;
|
2013-05-13 11:26:28 +02:00
|
|
|
|
2012-09-24 14:35:22 +02:00
|
|
|
struct tiff_closer
|
|
|
|
{
|
|
|
|
void operator() (TIFF * tif)
|
|
|
|
{
|
|
|
|
if (tif != 0)
|
|
|
|
{
|
|
|
|
TIFFClose(tif);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
private:
|
2013-05-13 11:26:28 +02:00
|
|
|
source_type source_;
|
|
|
|
input_stream stream_;
|
2014-12-09 14:34:13 +01:00
|
|
|
tiff_ptr tif_;
|
2010-06-02 13:03:30 +02:00
|
|
|
int read_method_;
|
|
|
|
int rows_per_strip_;
|
|
|
|
int tile_width_;
|
|
|
|
int tile_height_;
|
2014-12-09 14:34:13 +01:00
|
|
|
std::size_t width_;
|
|
|
|
std::size_t height_;
|
2014-12-10 12:53:39 +01:00
|
|
|
boost::optional<box2d<double> > bbox_;
|
2014-12-04 00:37:27 +01:00
|
|
|
unsigned bps_;
|
|
|
|
unsigned photometric_;
|
|
|
|
unsigned bands_;
|
2014-12-09 06:17:02 +01:00
|
|
|
unsigned planar_config_;
|
|
|
|
unsigned compression_;
|
2014-12-09 14:34:13 +01:00
|
|
|
bool premultiplied_alpha_;
|
|
|
|
bool has_alpha_;
|
|
|
|
bool is_tiled_;
|
2014-12-04 00:37:27 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
public:
|
|
|
|
enum TiffType {
|
|
|
|
generic=1,
|
|
|
|
stripped,
|
|
|
|
tiled
|
2005-06-14 17:06:59 +02:00
|
|
|
};
|
2012-09-03 19:27:48 +02:00
|
|
|
explicit tiff_reader(std::string const& file_name);
|
2013-05-13 11:26:28 +02:00
|
|
|
tiff_reader(char const* data, std::size_t size);
|
2010-06-02 13:03:30 +02:00
|
|
|
virtual ~tiff_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;
|
2014-12-08 20:52:52 +01:00
|
|
|
// methods specific to tiff reader
|
2014-12-08 23:16:56 +01:00
|
|
|
unsigned bits_per_sample() const { return bps_; }
|
|
|
|
unsigned photometric() const { return photometric_; }
|
2014-12-08 20:52:52 +01:00
|
|
|
bool is_tiled() const { return is_tiled_; }
|
2014-12-08 21:23:06 +01:00
|
|
|
unsigned tile_width() const { return tile_width_; }
|
|
|
|
unsigned tile_height() const { return tile_height_; }
|
2014-12-09 06:17:02 +01:00
|
|
|
unsigned rows_per_strip() const { return rows_per_strip_; }
|
|
|
|
unsigned planar_config() const { return planar_config_; }
|
|
|
|
unsigned compression() const { return compression_; }
|
2010-06-02 13:03:30 +02:00
|
|
|
private:
|
|
|
|
tiff_reader(const tiff_reader&);
|
|
|
|
tiff_reader& operator=(const tiff_reader&);
|
|
|
|
void init();
|
2015-01-22 03:31:02 +01:00
|
|
|
void read_generic(unsigned x,unsigned y,image_rgba8& image);
|
|
|
|
void read_stripped(unsigned x,unsigned y,image_rgba8& image);
|
2014-12-08 15:51:00 +01:00
|
|
|
|
|
|
|
template <typename ImageData>
|
|
|
|
void read_tiled(unsigned x,unsigned y, ImageData & image);
|
|
|
|
|
2014-12-05 17:24:22 +01:00
|
|
|
template <typename ImageData>
|
2015-01-22 02:40:12 +01:00
|
|
|
image_any read_any_gray(unsigned x, unsigned y, unsigned width, unsigned height);
|
2014-12-08 15:51:00 +01:00
|
|
|
|
2013-05-13 11:26:28 +02:00
|
|
|
TIFF* open(std::istream & input);
|
2010-06-02 13:03:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2013-05-13 11:26:28 +02:00
|
|
|
|
2012-09-03 19:27:48 +02:00
|
|
|
image_reader* create_tiff_reader(std::string const& file)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2013-05-13 11:26:28 +02:00
|
|
|
return new tiff_reader<boost::iostreams::file_source>(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
image_reader* create_tiff_reader2(char const * data, std::size_t size)
|
|
|
|
{
|
|
|
|
return new tiff_reader<boost::iostreams::array_source>(data, size);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
const bool registered = register_image_reader("tiff",create_tiff_reader);
|
2013-05-13 11:26:28 +02:00
|
|
|
const bool registered2 = register_image_reader("tiff", create_tiff_reader2);
|
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
|
2013-05-13 11:26:28 +02:00
|
|
|
template <typename T>
|
|
|
|
tiff_reader<T>::tiff_reader(std::string const& file_name)
|
|
|
|
: source_(file_name, std::ios_base::in | std::ios_base::binary),
|
|
|
|
stream_(source_),
|
2014-12-09 14:34:13 +01:00
|
|
|
tif_(nullptr),
|
2010-06-02 13:03:30 +02:00
|
|
|
read_method_(generic),
|
|
|
|
rows_per_strip_(0),
|
|
|
|
tile_width_(0),
|
2012-09-28 17:37:21 +02:00
|
|
|
tile_height_(0),
|
2014-12-09 14:34:13 +01:00
|
|
|
width_(0),
|
|
|
|
height_(0),
|
2014-12-04 00:37:27 +01:00
|
|
|
bps_(0),
|
|
|
|
photometric_(0),
|
2014-12-08 20:52:52 +01:00
|
|
|
bands_(1),
|
2014-12-09 06:17:02 +01:00
|
|
|
planar_config_(PLANARCONFIG_CONTIG),
|
2014-12-09 14:34:13 +01:00
|
|
|
compression_(COMPRESSION_NONE),
|
|
|
|
premultiplied_alpha_(false),
|
|
|
|
has_alpha_(false),
|
|
|
|
is_tiled_(false)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2013-05-13 11:26:28 +02:00
|
|
|
if (!stream_) throw image_reader_exception("TIFF reader: cannot open file "+ file_name);
|
2010-06-02 13:03:30 +02:00
|
|
|
init();
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2013-05-13 11:26:28 +02:00
|
|
|
template <typename T>
|
|
|
|
tiff_reader<T>::tiff_reader(char const* data, std::size_t size)
|
|
|
|
: source_(data, size),
|
|
|
|
stream_(source_),
|
2014-12-09 14:34:13 +01:00
|
|
|
tif_(nullptr),
|
2013-05-13 11:26:28 +02:00
|
|
|
read_method_(generic),
|
|
|
|
rows_per_strip_(0),
|
|
|
|
tile_width_(0),
|
|
|
|
tile_height_(0),
|
2014-12-09 14:34:13 +01:00
|
|
|
width_(0),
|
|
|
|
height_(0),
|
2014-12-04 00:37:27 +01:00
|
|
|
bps_(0),
|
|
|
|
photometric_(0),
|
2014-12-08 20:52:52 +01:00
|
|
|
bands_(1),
|
2014-12-09 06:17:02 +01:00
|
|
|
planar_config_(PLANARCONFIG_CONTIG),
|
2014-12-09 14:34:13 +01:00
|
|
|
compression_(COMPRESSION_NONE),
|
|
|
|
premultiplied_alpha_(false),
|
|
|
|
has_alpha_(false),
|
|
|
|
is_tiled_(false)
|
2013-05-13 11:26:28 +02:00
|
|
|
{
|
|
|
|
if (!stream_) throw image_reader_exception("TIFF reader: cannot open image stream ");
|
2014-12-17 18:31:29 +01:00
|
|
|
stream_.rdbuf()->pubsetbuf(0, 0);
|
2013-05-13 11:26:28 +02:00
|
|
|
stream_.seekg(0, std::ios::beg);
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void tiff_reader<T>::init()
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2014-09-05 13:30:33 +02:00
|
|
|
// avoid calling TIFFs global structures
|
2013-04-10 04:16:05 +02:00
|
|
|
TIFFSetWarningHandler(0);
|
|
|
|
TIFFSetErrorHandler(0);
|
2013-05-13 11:26:28 +02:00
|
|
|
|
|
|
|
TIFF* tif = open(stream_);
|
|
|
|
|
|
|
|
if (!tif) throw image_reader_exception("Can't open tiff file");
|
2013-04-10 04:08:50 +02:00
|
|
|
|
2014-12-04 00:37:27 +01:00
|
|
|
TIFFGetField(tif,TIFFTAG_BITSPERSAMPLE,&bps_);
|
|
|
|
TIFFGetField(tif,TIFFTAG_PHOTOMETRIC,&photometric_);
|
|
|
|
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &bands_);
|
|
|
|
|
|
|
|
MAPNIK_LOG_DEBUG(tiff_reader) << "bits per sample: " << bps_;
|
|
|
|
MAPNIK_LOG_DEBUG(tiff_reader) << "photometric: " << photometric_;
|
|
|
|
MAPNIK_LOG_DEBUG(tiff_reader) << "bands: " << bands_;
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2014-12-04 00:37:27 +01:00
|
|
|
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width_);
|
|
|
|
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height_);
|
2014-12-05 17:24:22 +01:00
|
|
|
|
2014-12-09 06:17:02 +01:00
|
|
|
if (width_ > 10000 || height_ > 10000)
|
|
|
|
{
|
|
|
|
throw image_reader_exception("Can't allocate tiff > 10000x10000");
|
|
|
|
}
|
|
|
|
|
|
|
|
TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &planar_config_);
|
|
|
|
TIFFGetField(tif, TIFFTAG_COMPRESSION, &compression_ );
|
|
|
|
TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rows_per_strip_);
|
|
|
|
|
2014-12-08 15:51:00 +01:00
|
|
|
std::uint16_t orientation;
|
|
|
|
if (TIFFGetField(tif, TIFFTAG_ORIENTATION, &orientation) == 0)
|
|
|
|
{
|
|
|
|
orientation = 1;
|
|
|
|
}
|
|
|
|
MAPNIK_LOG_DEBUG(tiff_reader) << "orientation: " << orientation;
|
2014-12-05 17:24:22 +01:00
|
|
|
|
2014-12-08 20:52:52 +01:00
|
|
|
is_tiled_ = TIFFIsTiled(tif);
|
|
|
|
|
2014-12-08 21:23:06 +01:00
|
|
|
if (is_tiled_)
|
|
|
|
{
|
|
|
|
TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tile_width_);
|
|
|
|
TIFFGetField(tif, TIFFTAG_TILELENGTH, &tile_height_);
|
2014-12-09 12:01:50 +01:00
|
|
|
MAPNIK_LOG_DEBUG(tiff_reader) << "tiff is tiled";
|
2014-12-08 21:23:06 +01:00
|
|
|
read_method_ = tiled;
|
|
|
|
}
|
2014-12-09 12:01:50 +01:00
|
|
|
else if (TIFFGetField(tif,TIFFTAG_ROWSPERSTRIP,&rows_per_strip_)!=0)
|
2014-12-08 21:23:06 +01:00
|
|
|
{
|
2014-12-09 12:01:50 +01:00
|
|
|
MAPNIK_LOG_DEBUG(tiff_reader) << "tiff is stripped";
|
2014-12-08 21:23:06 +01:00
|
|
|
read_method_ = stripped;
|
|
|
|
}
|
2014-12-09 12:01:50 +01:00
|
|
|
//TIFFTAG_EXTRASAMPLES
|
2014-12-08 23:12:44 +01:00
|
|
|
uint16 extrasamples = 0;
|
|
|
|
uint16* sampleinfo = nullptr;
|
|
|
|
if (TIFFGetField(tif, TIFFTAG_EXTRASAMPLES,
|
|
|
|
&extrasamples, &sampleinfo))
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2014-12-08 23:12:44 +01:00
|
|
|
has_alpha_ = true;
|
|
|
|
if (extrasamples == 1 &&
|
|
|
|
sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA)
|
2014-12-04 00:37:27 +01:00
|
|
|
{
|
2014-12-08 23:12:44 +01:00
|
|
|
premultiplied_alpha_ = true;
|
2014-12-04 00:37:27 +01:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2014-12-10 12:53:39 +01:00
|
|
|
// Try extracting bounding box from geoTIFF tags
|
|
|
|
{
|
|
|
|
uint16 count = 0;
|
|
|
|
double *pixelscale;
|
|
|
|
double *tilepoint;
|
|
|
|
if (TIFFGetField(tif, 33550, &count, &pixelscale) == 1 && count == 3
|
|
|
|
&& TIFFGetField(tif, 33922 , &count, &tilepoint) == 1 && count == 6)
|
|
|
|
{
|
|
|
|
MAPNIK_LOG_DEBUG(tiff_reader) << "PixelScale:" << pixelscale[0] << "," << pixelscale[1] << "," << pixelscale[2];
|
|
|
|
MAPNIK_LOG_DEBUG(tiff_reader) << "TilePoint:" << tilepoint[0] << "," << tilepoint[1] << "," << tilepoint[2];
|
|
|
|
MAPNIK_LOG_DEBUG(tiff_reader) << " " << tilepoint[3] << "," << tilepoint[4] << "," << tilepoint[5];
|
|
|
|
|
|
|
|
// assuming upper-left
|
|
|
|
double lox = tilepoint[3];
|
|
|
|
double loy = tilepoint[4];
|
|
|
|
double hix = lox + pixelscale[0] * width_;
|
2014-12-12 10:18:34 +01:00
|
|
|
double hiy = loy - pixelscale[1] * height_;
|
2014-12-10 12:53:39 +01:00
|
|
|
bbox_.reset(box2d<double>(lox, loy, hix, hiy));
|
|
|
|
MAPNIK_LOG_DEBUG(tiff_reader) << "Bounding Box:" << *bbox_;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-12-09 06:17:02 +01:00
|
|
|
if (!is_tiled_ &&
|
|
|
|
compression_ == COMPRESSION_NONE &&
|
|
|
|
planar_config_ == PLANARCONFIG_CONTIG)
|
|
|
|
{
|
|
|
|
if (height_ > 128 * 1024 * 1024)
|
|
|
|
{
|
|
|
|
std::size_t line_size = (bands_ * width_ * bps_ + 7) / 8;
|
|
|
|
std::size_t default_strip_height = 8192 / line_size;
|
|
|
|
if (default_strip_height == 0) default_strip_height = 1;
|
|
|
|
std::size_t num_strips = height_ / default_strip_height;
|
|
|
|
if (num_strips > 128 * 1024 * 1024)
|
|
|
|
{
|
|
|
|
throw image_reader_exception("Can't allocate tiff");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2013-05-13 11:26:28 +02:00
|
|
|
template <typename T>
|
|
|
|
tiff_reader<T>::~tiff_reader()
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2013-05-13 11:26:28 +02:00
|
|
|
template <typename T>
|
|
|
|
unsigned tiff_reader<T>::width() const
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
return width_;
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2013-05-13 11:26:28 +02:00
|
|
|
template <typename T>
|
|
|
|
unsigned tiff_reader<T>::height() const
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
return height_;
|
|
|
|
}
|
|
|
|
|
2014-12-10 10:44:51 +01:00
|
|
|
template <typename T>
|
2014-12-10 12:53:39 +01:00
|
|
|
boost::optional<box2d<double> > tiff_reader<T>::bounding_box() const
|
2014-12-10 10:44:51 +01:00
|
|
|
{
|
2014-12-10 12:53:39 +01:00
|
|
|
return bbox_;
|
2014-12-10 10:44:51 +01:00
|
|
|
}
|
|
|
|
|
2013-05-13 11:26:28 +02:00
|
|
|
template <typename T>
|
2015-01-22 03:31:02 +01:00
|
|
|
void tiff_reader<T>::read(unsigned x,unsigned y,image_rgba8& image)
|
2012-02-02 02:53:35 +01:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
if (read_method_==stripped)
|
|
|
|
{
|
|
|
|
read_stripped(x,y,image);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
else if (read_method_==tiled)
|
|
|
|
{
|
2014-12-09 12:01:50 +01:00
|
|
|
read_tiled(x,y,image);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
read_generic(x,y,image);
|
|
|
|
}
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2014-12-03 10:44:56 +01:00
|
|
|
template <typename T>
|
2014-12-05 17:24:22 +01:00
|
|
|
template <typename ImageData>
|
2015-01-22 02:40:12 +01:00
|
|
|
image_any tiff_reader<T>::read_any_gray(unsigned x0, unsigned y0, unsigned width, unsigned height)
|
2014-12-05 17:24:22 +01:00
|
|
|
{
|
|
|
|
using image_data_type = ImageData;
|
|
|
|
using pixel_type = typename image_data_type::pixel_type;
|
2014-12-08 15:51:00 +01:00
|
|
|
if (read_method_ == tiled)
|
2014-12-05 17:24:22 +01:00
|
|
|
{
|
2014-12-08 15:51:00 +01:00
|
|
|
image_data_type data(width,height);
|
|
|
|
read_tiled<image_data_type>(x0, y0, data);
|
2015-01-22 02:40:12 +01:00
|
|
|
return image_any(std::move(data));
|
2014-12-08 15:51:00 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TIFF* tif = open(stream_);
|
|
|
|
if (tif)
|
2014-12-05 17:24:22 +01:00
|
|
|
{
|
2014-12-08 15:51:00 +01:00
|
|
|
image_data_type data(width, height);
|
|
|
|
std::size_t block_size = rows_per_strip_ > 0 ? rows_per_strip_ : tile_height_ ;
|
|
|
|
std::ptrdiff_t start_y = y0 - y0 % block_size;
|
|
|
|
std::ptrdiff_t end_y = std::min(y0 + height, static_cast<unsigned>(height_));
|
|
|
|
std::ptrdiff_t start_x = x0;
|
|
|
|
std::ptrdiff_t end_x = std::min(x0 + width, static_cast<unsigned>(width_));
|
|
|
|
std::size_t element_size = sizeof(pixel_type);
|
|
|
|
std::size_t size_to_allocate = (TIFFScanlineSize(tif) + element_size - 1)/element_size;
|
|
|
|
const std::unique_ptr<pixel_type[]> scanline(new pixel_type[size_to_allocate]);
|
|
|
|
for (std::size_t y = start_y; y < end_y; ++y)
|
2014-12-05 17:24:22 +01:00
|
|
|
{
|
2014-12-08 15:51:00 +01:00
|
|
|
if (-1 != TIFFReadScanline(tif, scanline.get(), y) && (y >= y0))
|
|
|
|
{
|
|
|
|
pixel_type * row = data.getRow(y - y0);
|
|
|
|
std::transform(scanline.get() + start_x, scanline.get() + end_x, row, [](pixel_type const& p) { return p;});
|
|
|
|
}
|
2014-12-05 17:24:22 +01:00
|
|
|
}
|
2015-01-22 02:40:12 +01:00
|
|
|
return image_any(std::move(data));
|
2014-12-05 17:24:22 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-22 02:40:12 +01:00
|
|
|
return image_any();
|
2014-12-05 17:24:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
struct rgb8
|
|
|
|
{
|
|
|
|
std::uint8_t r;
|
|
|
|
std::uint8_t g;
|
|
|
|
std::uint8_t b;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct rgb8_to_rgba8
|
|
|
|
{
|
|
|
|
std::uint32_t operator() (rgb8 const& in) const
|
|
|
|
{
|
|
|
|
return ((255 << 24) | (in.r) | (in.g << 8) | (in.b << 16));
|
|
|
|
}
|
|
|
|
};
|
2014-12-08 15:51:00 +01:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct tiff_reader_traits
|
|
|
|
{
|
|
|
|
using image_data_type = T;
|
|
|
|
using pixel_type = typename image_data_type::pixel_type;
|
2014-12-09 12:01:50 +01:00
|
|
|
static bool read_tile(TIFF * tif, unsigned x, unsigned y, pixel_type* buf, std::size_t tile_width, std::size_t tile_height)
|
2014-12-08 15:51:00 +01:00
|
|
|
{
|
2014-12-09 12:01:50 +01:00
|
|
|
return (TIFFReadEncodedTile(tif, TIFFComputeTile(tif, x,y,0,0), buf, tile_width * tile_height * sizeof(pixel_type)) != -1);
|
2014-12-08 15:51:00 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-09 12:01:50 +01:00
|
|
|
// default specialization that expands into RGBA
|
2014-12-08 15:51:00 +01:00
|
|
|
template <>
|
2015-01-22 03:31:02 +01:00
|
|
|
struct tiff_reader_traits<image_rgba8>
|
2014-12-08 15:51:00 +01:00
|
|
|
{
|
|
|
|
using pixel_type = std::uint32_t;
|
2014-12-09 12:01:50 +01:00
|
|
|
static bool read_tile(TIFF * tif, unsigned x0, unsigned y0, pixel_type* buf, std::size_t tile_width, std::size_t tile_height)
|
2014-12-08 15:51:00 +01:00
|
|
|
{
|
2014-12-09 12:01:50 +01:00
|
|
|
if (TIFFReadRGBATile(tif, x0, y0, buf) != -1)
|
2014-12-08 18:55:40 +01:00
|
|
|
{
|
2014-12-15 03:52:32 +01:00
|
|
|
for (unsigned y = 0; y < tile_height/2; ++y)
|
2014-12-08 22:43:32 +01:00
|
|
|
{
|
2014-12-15 03:52:32 +01:00
|
|
|
std::swap_ranges(buf + y * tile_width, buf + (y + 1) * tile_width, buf + (tile_height - y - 1) * tile_width);
|
2014-12-08 22:43:32 +01:00
|
|
|
}
|
2014-12-09 12:01:50 +01:00
|
|
|
return true;
|
2014-12-08 18:55:40 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-08 15:51:00 +01:00
|
|
|
};
|
|
|
|
|
2014-12-05 17:24:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2015-01-22 02:40:12 +01:00
|
|
|
image_any tiff_reader<T>::read(unsigned x0, unsigned y0, unsigned width, unsigned height)
|
2014-12-03 10:44:56 +01:00
|
|
|
{
|
2014-12-05 17:24:22 +01:00
|
|
|
switch (photometric_)
|
|
|
|
{
|
|
|
|
case PHOTOMETRIC_MINISBLACK:
|
2014-12-09 12:01:50 +01:00
|
|
|
case PHOTOMETRIC_MINISWHITE:
|
2014-12-05 17:24:22 +01:00
|
|
|
{
|
|
|
|
switch (bps_)
|
|
|
|
{
|
|
|
|
case 8:
|
|
|
|
{
|
2015-01-22 04:08:04 +01:00
|
|
|
return read_any_gray<image_gray8>(x0, y0, width, height);
|
2014-12-05 17:24:22 +01:00
|
|
|
}
|
|
|
|
case 16:
|
|
|
|
{
|
2015-01-22 04:08:04 +01:00
|
|
|
return read_any_gray<image_gray16>(x0, y0, width, height);
|
2014-12-05 17:24:22 +01:00
|
|
|
}
|
|
|
|
case 32:
|
|
|
|
{
|
2015-01-22 04:08:04 +01:00
|
|
|
return read_any_gray<image_gray32f>(x0, y0, width, height);
|
2014-12-05 17:24:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-08 15:51:00 +01:00
|
|
|
// read PHOTOMETRIC_RGB expand using RGBA interface
|
|
|
|
/*
|
2014-12-05 17:24:22 +01:00
|
|
|
case PHOTOMETRIC_RGB:
|
|
|
|
{
|
|
|
|
switch (bps_)
|
|
|
|
{
|
|
|
|
case 8:
|
|
|
|
{
|
|
|
|
TIFF* tif = open(stream_);
|
|
|
|
if (tif)
|
|
|
|
{
|
2015-01-22 03:31:02 +01:00
|
|
|
image_rgba8 data(width, height, true, premultiplied_alpha_);
|
2014-12-05 17:24:22 +01:00
|
|
|
std::size_t element_size = sizeof(detail::rgb8);
|
|
|
|
std::size_t size_to_allocate = (TIFFScanlineSize(tif) + element_size - 1)/element_size;
|
|
|
|
const std::unique_ptr<detail::rgb8[]> scanline(new detail::rgb8[size_to_allocate]);
|
|
|
|
std::ptrdiff_t start_y = y0 - y0 % rows_per_strip_;
|
2014-12-05 19:59:20 +01:00
|
|
|
std::ptrdiff_t end_y = std::min(y0 + height, static_cast<unsigned>(height_));
|
2014-12-05 17:24:22 +01:00
|
|
|
std::ptrdiff_t start_x = x0;
|
2014-12-05 19:59:20 +01:00
|
|
|
std::ptrdiff_t end_x = std::min(x0 + width, static_cast<unsigned>(width_));
|
2014-12-05 17:24:22 +01:00
|
|
|
for (std::size_t y = start_y; y < end_y; ++y)
|
|
|
|
{
|
|
|
|
if (-1 != TIFFReadScanline(tif, scanline.get(), y))
|
|
|
|
{
|
|
|
|
if (y >= y0)
|
|
|
|
{
|
2015-01-22 03:31:02 +01:00
|
|
|
image_rgba8::pixel_type * row = data.getRow(y - y0);
|
2014-12-05 17:24:22 +01:00
|
|
|
std::transform(scanline.get() + start_x, scanline.get() + end_x, row, detail::rgb8_to_rgba8());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 02:40:12 +01:00
|
|
|
return image_any(std::move(data));
|
2014-12-05 17:24:22 +01:00
|
|
|
}
|
2015-01-22 02:40:12 +01:00
|
|
|
return image_any();
|
2014-12-05 17:24:22 +01:00
|
|
|
}
|
|
|
|
case 16:
|
|
|
|
{
|
2015-01-22 03:31:02 +01:00
|
|
|
image_rgba8 data(width,height,true,premultiplied_alpha_);
|
2014-12-05 17:24:22 +01:00
|
|
|
read(x0, y0, data);
|
2015-01-22 02:40:12 +01:00
|
|
|
return image_any(std::move(data));
|
2014-12-05 17:24:22 +01:00
|
|
|
}
|
|
|
|
case 32:
|
|
|
|
{
|
2015-01-22 03:31:02 +01:00
|
|
|
image_rgba8 data(width,height,true,premultiplied_alpha_);
|
2014-12-05 17:24:22 +01:00
|
|
|
read(x0, y0, data);
|
2015-01-22 02:40:12 +01:00
|
|
|
return image_any(std::move(data));
|
2014-12-05 17:24:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-08 15:51:00 +01:00
|
|
|
*/
|
2014-12-05 17:24:22 +01:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
//PHOTOMETRIC_PALETTE = 3;
|
|
|
|
//PHOTOMETRIC_MASK = 4;
|
|
|
|
//PHOTOMETRIC_SEPARATED = 5;
|
|
|
|
//PHOTOMETRIC_YCBCR = 6;
|
|
|
|
//PHOTOMETRIC_CIELAB = 8;
|
|
|
|
//PHOTOMETRIC_ICCLAB = 9;
|
|
|
|
//PHOTOMETRIC_ITULAB = 10;
|
|
|
|
//PHOTOMETRIC_LOGL = 32844;
|
|
|
|
//PHOTOMETRIC_LOGLUV = 32845;
|
2015-01-22 03:31:02 +01:00
|
|
|
image_rgba8 data(width,height, true, premultiplied_alpha_);
|
2014-12-05 17:24:22 +01:00
|
|
|
read(x0, y0, data);
|
2015-01-22 02:40:12 +01:00
|
|
|
return image_any(std::move(data));
|
2014-12-05 17:24:22 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-22 02:40:12 +01:00
|
|
|
return image_any();
|
2014-12-03 10:44:56 +01:00
|
|
|
}
|
|
|
|
|
2013-05-13 11:26:28 +02:00
|
|
|
template <typename T>
|
2015-01-22 03:31:02 +01:00
|
|
|
void tiff_reader<T>::read_generic(unsigned, unsigned, image_rgba8& image)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2013-05-13 11:26:28 +02:00
|
|
|
TIFF* tif = open(stream_);
|
2010-06-02 13:03:30 +02:00
|
|
|
if (tif)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2014-12-08 20:52:52 +01:00
|
|
|
throw std::runtime_error("tiff_reader: TODO - tiff is not stripped or tiled");
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2013-05-13 11:26:28 +02:00
|
|
|
template <typename T>
|
2014-12-08 15:51:00 +01:00
|
|
|
template <typename ImageData>
|
|
|
|
void tiff_reader<T>::read_tiled(unsigned x0,unsigned y0, ImageData & image)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2014-12-08 15:51:00 +01:00
|
|
|
using pixel_type = typename detail::tiff_reader_traits<ImageData>::pixel_type;
|
|
|
|
|
2013-05-13 11:26:28 +02:00
|
|
|
TIFF* tif = open(stream_);
|
2010-06-02 13:03:30 +02:00
|
|
|
if (tif)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2014-12-08 15:51:00 +01:00
|
|
|
std::unique_ptr<pixel_type[]> buf(new pixel_type[tile_width_*tile_height_]);
|
|
|
|
int width = image.width();
|
|
|
|
int height = image.height();
|
2014-12-09 12:01:50 +01:00
|
|
|
int start_y = (y0 / tile_height_) * tile_height_;
|
|
|
|
int end_y = ((y0 + height) / tile_height_ + 1) * tile_height_;
|
|
|
|
int start_x = (x0 / tile_width_) * tile_width_;
|
|
|
|
int end_x = ((x0 + width) / tile_width_ + 1) * tile_width_;
|
|
|
|
end_y = std::min(end_y, int(height_));
|
|
|
|
end_x = std::min(end_x, int(width_));
|
2006-04-05 10:27:45 +02:00
|
|
|
|
2014-12-08 15:51:00 +01:00
|
|
|
for (int y = start_y; y < end_y; y += tile_height_)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2014-12-09 12:01:50 +01:00
|
|
|
int ty0 = std::max(y0, static_cast<unsigned>(y)) - y;
|
|
|
|
int ty1 = std::min(height + y0, static_cast<unsigned>(y + tile_height_)) - y;
|
2006-04-05 10:27:45 +02:00
|
|
|
|
2014-12-08 15:51:00 +01:00
|
|
|
for (int x = start_x; x < end_x; x += tile_width_)
|
|
|
|
{
|
2014-12-09 12:01:50 +01:00
|
|
|
if (!detail::tiff_reader_traits<ImageData>::read_tile(tif, x, y, buf.get(), tile_width_, tile_height_))
|
2014-12-04 00:37:27 +01:00
|
|
|
{
|
2014-12-09 02:39:44 +01:00
|
|
|
std::clog << "read_tile(...) failed at " << x << "/" << y << " for " << width_ << "/" << height_ << "\n";
|
2014-12-04 00:37:27 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-12-09 12:01:50 +01:00
|
|
|
int tx0 = std::max(x0, static_cast<unsigned>(x));
|
|
|
|
int tx1 = std::min(width + x0, static_cast<unsigned>(x + tile_width_));
|
|
|
|
int row = y + ty0 - y0;
|
|
|
|
for (int ty = ty0; ty < ty1; ++ty, ++row)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2014-12-09 12:01:50 +01:00
|
|
|
image.setRow(row, tx0 - x0, tx1 - x0, &buf[ty * tile_width_ + tx0 - x]);
|
2006-04-05 10:27:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2014-12-08 18:55:40 +01:00
|
|
|
|
2013-05-13 11:26:28 +02:00
|
|
|
template <typename T>
|
2015-01-22 03:31:02 +01:00
|
|
|
void tiff_reader<T>::read_stripped(unsigned x0,unsigned y0,image_rgba8& image)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2013-05-13 11:26:28 +02:00
|
|
|
TIFF* tif = open(stream_);
|
2010-06-02 13:03:30 +02:00
|
|
|
if (tif)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2015-01-22 03:31:02 +01:00
|
|
|
image_rgba8 strip(width_,rows_per_strip_,false);
|
2010-06-02 13:03:30 +02:00
|
|
|
int width=image.width();
|
|
|
|
int height=image.height();
|
2006-04-05 10:27:45 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
unsigned start_y=(y0/rows_per_strip_)*rows_per_strip_;
|
|
|
|
unsigned end_y=((y0+height)/rows_per_strip_+1)*rows_per_strip_;
|
2014-12-05 19:59:20 +01:00
|
|
|
bool laststrip=(static_cast<unsigned>(end_y) > height_)?true:false;
|
2010-06-02 13:03:30 +02:00
|
|
|
int row,tx0,tx1,ty0,ty1;
|
2006-04-05 10:27:45 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
tx0=x0;
|
2014-12-05 19:59:20 +01:00
|
|
|
tx1=std::min(width+x0,static_cast<unsigned>(width_));
|
2006-04-05 10:27:45 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
for (unsigned y=start_y; y < end_y; y+=rows_per_strip_)
|
|
|
|
{
|
2013-05-13 11:26:28 +02:00
|
|
|
ty0 = std::max(y0,y)-y;
|
|
|
|
ty1 = std::min(height+y0,y+rows_per_strip_)-y;
|
2006-04-05 10:27:45 +02:00
|
|
|
|
2014-12-17 18:31:29 +01:00
|
|
|
if (!TIFFReadRGBAStrip(tif,y,strip.getData()))
|
2014-12-04 00:37:27 +01:00
|
|
|
{
|
2014-12-09 06:17:02 +01:00
|
|
|
std::clog << "TIFFReadRGBAStrip failed at " << y << " for " << width_ << "/" << height_ << "\n";
|
2014-12-04 00:37:27 +01:00
|
|
|
break;
|
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
row=y+ty0-y0;
|
2006-04-05 10:27:45 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
int n0=laststrip ? 0:(rows_per_strip_-ty1);
|
|
|
|
int n1=laststrip ? (ty1-ty0-1):(rows_per_strip_-ty0-1);
|
|
|
|
for (int n=n1;n>=n0;--n)
|
|
|
|
{
|
2014-12-17 18:31:29 +01:00
|
|
|
image.setRow(row,tx0-x0,tx1-x0,&strip.getData()[n*width_+tx0]);
|
2010-06-02 13:03:30 +02:00
|
|
|
++row;
|
2006-04-05 10:27:45 +02:00
|
|
|
}
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2013-05-13 11:26:28 +02:00
|
|
|
template <typename T>
|
|
|
|
TIFF* tiff_reader<T>::open(std::istream & input)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2012-09-24 14:35:22 +02:00
|
|
|
if (!tif_)
|
2012-02-02 02:53:35 +01:00
|
|
|
{
|
2014-12-09 06:39:19 +01:00
|
|
|
tif_ = tiff_ptr(TIFFClientOpen("tiff_input_stream", "rcm",
|
2013-05-13 11:26:28 +02:00
|
|
|
reinterpret_cast<thandle_t>(&input),
|
|
|
|
impl::tiff_read_proc,
|
|
|
|
impl::tiff_write_proc,
|
|
|
|
impl::tiff_seek_proc,
|
|
|
|
impl::tiff_close_proc,
|
|
|
|
impl::tiff_size_proc,
|
|
|
|
impl::tiff_map_proc,
|
|
|
|
impl::tiff_unmap_proc), tiff_closer());
|
2006-12-20 01:22:45 +01:00
|
|
|
}
|
2012-09-24 14:35:22 +02:00
|
|
|
return tif_.get();
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2012-09-24 14:35:22 +02:00
|
|
|
} // namespace mapnik
|