2011-11-28 16:59:19 +01:00
|
|
|
/*****************************************************************************
|
2012-02-02 02:53:35 +01:00
|
|
|
*
|
2011-11-28 16:59:19 +01:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2021-01-05 15:39:07 +01:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2011-11-28 16:59:19 +01: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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef MAPNIK_TIFF_IO_HPP
|
|
|
|
#define MAPNIK_TIFF_IO_HPP
|
|
|
|
|
|
|
|
#include <mapnik/global.hpp>
|
2013-05-10 17:50:28 +02:00
|
|
|
#include <mapnik/image_util.hpp>
|
2015-01-22 02:40:12 +01:00
|
|
|
#include <mapnik/image_any.hpp>
|
2014-12-04 00:41:49 +01:00
|
|
|
#include <mapnik/util/variant.hpp>
|
2011-11-28 16:59:19 +01:00
|
|
|
|
2020-11-19 15:30:30 +01:00
|
|
|
#include <mapnik/warning.hpp>
|
|
|
|
MAPNIK_DISABLE_WARNING_PUSH
|
2016-03-10 21:58:29 +01:00
|
|
|
#include <mapnik/warning_ignore.hpp>
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
extern "C" {
|
2012-02-02 02:53:35 +01:00
|
|
|
#include <tiffio.h>
|
2022-01-26 10:43:31 +01:00
|
|
|
#define RealTIFFOpen TIFFClientOpen
|
2012-02-02 02:53:35 +01:00
|
|
|
#define RealTIFFClose TIFFClose
|
2011-11-28 16:59:19 +01:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:30:30 +01:00
|
|
|
MAPNIK_DISABLE_WARNING_POP
|
2016-03-10 21:58:29 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
// std
|
2015-01-25 04:16:36 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2014-12-15 03:52:32 +01:00
|
|
|
#define TIFF_WRITE_SCANLINE 0
|
|
|
|
#define TIFF_WRITE_STRIPPED 1
|
2022-01-26 10:43:31 +01:00
|
|
|
#define TIFF_WRITE_TILED 2
|
2014-12-15 03:52:32 +01:00
|
|
|
|
2011-11-28 16:59:19 +01:00
|
|
|
namespace mapnik {
|
|
|
|
|
2014-12-09 17:30:50 +01:00
|
|
|
static inline tsize_t tiff_write_proc(thandle_t fd, tdata_t buf, tsize_t size)
|
2011-11-28 16:59:19 +01:00
|
|
|
{
|
2013-05-10 17:50:28 +02:00
|
|
|
std::ostream* out = reinterpret_cast<std::ostream*>(fd);
|
|
|
|
std::ios::pos_type pos = out->tellp();
|
|
|
|
std::streamsize request_size = size;
|
|
|
|
if (static_cast<tsize_t>(request_size) != size)
|
|
|
|
return static_cast<tsize_t>(-1);
|
|
|
|
out->write(reinterpret_cast<const char*>(buf), size);
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
if (static_cast<std::streamsize>(pos) == -1)
|
2013-05-10 17:50:28 +02:00
|
|
|
{
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
return static_cast<tsize_t>(out->tellp() - pos);
|
2013-05-10 17:50:28 +02:00
|
|
|
}
|
2011-11-28 16:59:19 +01:00
|
|
|
}
|
|
|
|
|
2014-12-09 17:30:50 +01:00
|
|
|
static inline toff_t tiff_seek_proc(thandle_t fd, toff_t off, int whence)
|
2011-11-28 16:59:19 +01:00
|
|
|
{
|
2013-05-10 17:50:28 +02:00
|
|
|
std::ostream* out = reinterpret_cast<std::ostream*>(fd);
|
2011-11-28 16:59:19 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
if (out->fail())
|
2013-05-10 17:50:28 +02:00
|
|
|
return static_cast<toff_t>(-1);
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
if (static_cast<std::streamsize>(out->tellp()) == -1)
|
|
|
|
return static_cast<toff_t>(0);
|
2011-11-28 16:59:19 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
switch (whence)
|
2011-11-28 16:59:19 +01:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
case SEEK_SET:
|
|
|
|
out->seekp(off, std::ios_base::beg);
|
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
|
|
|
out->seekp(off, std::ios_base::cur);
|
|
|
|
break;
|
|
|
|
case SEEK_END:
|
|
|
|
out->seekp(off, std::ios_base::end);
|
|
|
|
break;
|
2011-11-28 16:59:19 +01:00
|
|
|
}
|
2013-05-10 17:50:28 +02:00
|
|
|
// grow std::stringstream buffer (re: libtiff/tif_stream.cxx)
|
|
|
|
std::ios::pos_type pos = out->tellp();
|
|
|
|
// second check needed for clang (libcxx doesn't set failbit when seeking beyond the current buffer size
|
2013-09-16 08:08:04 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
if (out->fail() || static_cast<std::streamoff>(off) != pos)
|
2013-05-10 17:50:28 +02:00
|
|
|
{
|
|
|
|
std::ios::iostate old_state;
|
2022-01-26 10:43:31 +01:00
|
|
|
std::ios::pos_type origin;
|
2013-05-10 17:50:28 +02:00
|
|
|
old_state = out->rdstate();
|
|
|
|
// reset the fail bit or else tellp() won't work below
|
|
|
|
out->clear(out->rdstate() & ~std::ios::failbit);
|
2022-01-26 10:43:31 +01:00
|
|
|
switch (whence)
|
2013-05-10 17:50:28 +02:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
case SEEK_SET:
|
|
|
|
default:
|
|
|
|
origin = 0L;
|
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
|
|
|
origin = out->tellp();
|
|
|
|
break;
|
|
|
|
case SEEK_END:
|
|
|
|
out->seekp(0, std::ios::end);
|
|
|
|
origin = out->tellp();
|
|
|
|
break;
|
2013-05-10 17:50:28 +02:00
|
|
|
}
|
|
|
|
// restore original stream state
|
|
|
|
out->clear(old_state);
|
|
|
|
|
|
|
|
// only do something if desired seek position is valid
|
2022-01-26 10:43:31 +01:00
|
|
|
if ((static_cast<uint64_t>(origin) + off) > 0L)
|
2013-05-10 17:50:28 +02:00
|
|
|
{
|
|
|
|
uint64_t num_fill;
|
|
|
|
// clear the fail bit
|
|
|
|
out->clear(out->rdstate() & ~std::ios::failbit);
|
|
|
|
// extend the stream to the expected size
|
|
|
|
out->seekp(0, std::ios::end);
|
|
|
|
num_fill = (static_cast<uint64_t>(origin)) + off - out->tellp();
|
2022-01-26 10:43:31 +01:00
|
|
|
for (uint64_t i = 0; i < num_fill; ++i)
|
2013-05-10 17:50:28 +02:00
|
|
|
out->put('\0');
|
|
|
|
|
|
|
|
// retry the seek
|
|
|
|
out->seekp(static_cast<std::ios::off_type>(static_cast<uint64_t>(origin) + off), std::ios::beg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return static_cast<toff_t>(out->tellp());
|
2011-11-28 16:59:19 +01:00
|
|
|
}
|
|
|
|
|
2014-12-09 17:30:50 +01:00
|
|
|
static inline int tiff_close_proc(thandle_t fd)
|
2011-11-28 16:59:19 +01:00
|
|
|
{
|
|
|
|
std::ostream* out = (std::ostream*)fd;
|
|
|
|
out->flush();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-09 17:30:50 +01:00
|
|
|
static inline toff_t tiff_size_proc(thandle_t fd)
|
2011-11-28 16:59:19 +01:00
|
|
|
{
|
2013-05-10 17:50:28 +02:00
|
|
|
std::ostream* out = reinterpret_cast<std::ostream*>(fd);
|
|
|
|
std::ios::pos_type pos = out->tellp();
|
|
|
|
out->seekp(0, std::ios::end);
|
|
|
|
std::ios::pos_type len = out->tellp();
|
|
|
|
out->seekp(pos);
|
2013-09-16 08:08:04 +02:00
|
|
|
return static_cast<toff_t>(len);
|
2011-11-28 16:59:19 +01:00
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
static inline tsize_t tiff_dummy_read_proc(thandle_t, tdata_t, tsize_t)
|
2011-11-28 16:59:19 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
static inline void tiff_dummy_unmap_proc(thandle_t, tdata_t, toff_t) {}
|
2011-11-28 16:59:19 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
static inline int tiff_dummy_map_proc(thandle_t, tdata_t*, toff_t*)
|
2011-11-28 16:59:19 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-04 23:11:33 +01:00
|
|
|
struct tiff_config
|
|
|
|
{
|
|
|
|
tiff_config()
|
2022-01-26 10:43:31 +01:00
|
|
|
: compression(COMPRESSION_ADOBE_DEFLATE)
|
|
|
|
, zlevel(4)
|
|
|
|
, tile_width(0)
|
|
|
|
, tile_height(0)
|
|
|
|
, rows_per_strip(0)
|
|
|
|
, method(TIFF_WRITE_STRIPPED)
|
|
|
|
{}
|
2014-12-04 23:11:33 +01:00
|
|
|
|
|
|
|
int compression;
|
|
|
|
int zlevel;
|
2022-01-26 10:43:31 +01:00
|
|
|
int tile_width; // Tile width of zero means tile the width of the image
|
2014-12-15 03:52:32 +01:00
|
|
|
int tile_height; // Tile height of zero means tile the height of the image
|
|
|
|
int rows_per_strip;
|
|
|
|
int method; // The method to use to write the TIFF.
|
2014-12-04 23:11:33 +01:00
|
|
|
};
|
|
|
|
|
2015-01-07 11:35:21 +01:00
|
|
|
struct tag_setter
|
2014-12-04 00:41:49 +01:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
tag_setter(TIFF* output, tiff_config const& config)
|
|
|
|
: output_(output)
|
|
|
|
, config_(config)
|
|
|
|
{}
|
2014-12-04 00:41:49 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
template<typename T>
|
|
|
|
void operator()(T const&) const
|
2014-12-04 00:41:49 +01:00
|
|
|
{
|
|
|
|
// Assume this would be null type
|
2015-05-14 13:53:21 +02:00
|
|
|
throw image_writer_exception("Could not write TIFF - unknown image type provided");
|
2014-12-04 00:41:49 +01:00
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
inline void operator()(image_rgba8 const& data) const
|
2014-12-04 00:41:49 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
|
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
|
2014-12-04 16:48:19 +01:00
|
|
|
TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 8);
|
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 4);
|
2015-05-04 12:49:11 +02:00
|
|
|
if (data.get_premultiplied())
|
2015-01-15 01:29:00 +01:00
|
|
|
{
|
2022-08-04 11:06:04 +02:00
|
|
|
std::uint16_t extras[] = {EXTRASAMPLE_ASSOCALPHA};
|
2015-01-15 01:29:00 +01:00
|
|
|
TIFFSetField(output_, TIFFTAG_EXTRASAMPLES, 1, extras);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-04 11:06:04 +02:00
|
|
|
std::uint16_t extras[] = {EXTRASAMPLE_UNASSALPHA};
|
2015-01-15 01:29:00 +01:00
|
|
|
TIFFSetField(output_, TIFFTAG_EXTRASAMPLES, 1, extras);
|
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
if (config_.compression == COMPRESSION_DEFLATE || config_.compression == COMPRESSION_ADOBE_DEFLATE ||
|
|
|
|
config_.compression == COMPRESSION_LZW)
|
2014-12-04 23:11:33 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);
|
|
|
|
}
|
2014-12-04 00:41:49 +01:00
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
inline void operator()(image_gray64 const&) const
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
|
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
|
|
|
|
TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 64);
|
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
|
2022-01-26 10:43:31 +01:00
|
|
|
if (config_.compression == COMPRESSION_DEFLATE || config_.compression == COMPRESSION_ADOBE_DEFLATE ||
|
|
|
|
config_.compression == COMPRESSION_LZW)
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
inline void operator()(image_gray64s const&) const
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
|
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
|
|
|
|
TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 64);
|
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
|
2022-01-26 10:43:31 +01:00
|
|
|
if (config_.compression == COMPRESSION_DEFLATE || config_.compression == COMPRESSION_ADOBE_DEFLATE ||
|
|
|
|
config_.compression == COMPRESSION_LZW)
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
inline void operator()(image_gray64f const&) const
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
|
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
|
|
|
|
TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 64);
|
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
|
2022-01-26 10:43:31 +01:00
|
|
|
if (config_.compression == COMPRESSION_DEFLATE || config_.compression == COMPRESSION_ADOBE_DEFLATE ||
|
|
|
|
config_.compression == COMPRESSION_LZW)
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_FLOATINGPOINT);
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
inline void operator()(image_gray32 const&) const
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
|
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
|
|
|
|
TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 32);
|
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
|
2022-01-26 10:43:31 +01:00
|
|
|
if (config_.compression == COMPRESSION_DEFLATE || config_.compression == COMPRESSION_ADOBE_DEFLATE ||
|
|
|
|
config_.compression == COMPRESSION_LZW)
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
inline void operator()(image_gray32s const&) const
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
|
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
|
|
|
|
TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 32);
|
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
|
2022-01-26 10:43:31 +01:00
|
|
|
if (config_.compression == COMPRESSION_DEFLATE || config_.compression == COMPRESSION_ADOBE_DEFLATE ||
|
|
|
|
config_.compression == COMPRESSION_LZW)
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
inline void operator()(image_gray32f const&) const
|
2014-12-04 00:41:49 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
|
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
|
|
|
|
TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 32);
|
2014-12-04 23:11:33 +01:00
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
|
2022-01-26 10:43:31 +01:00
|
|
|
if (config_.compression == COMPRESSION_DEFLATE || config_.compression == COMPRESSION_ADOBE_DEFLATE ||
|
|
|
|
config_.compression == COMPRESSION_LZW)
|
2014-12-04 23:11:33 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_FLOATINGPOINT);
|
|
|
|
}
|
2014-12-04 00:41:49 +01:00
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
inline void operator()(image_gray16 const&) const
|
2014-12-04 00:41:49 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
|
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
|
|
|
|
TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 16);
|
2014-12-04 23:11:33 +01:00
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
|
2022-01-26 10:43:31 +01:00
|
|
|
if (config_.compression == COMPRESSION_DEFLATE || config_.compression == COMPRESSION_ADOBE_DEFLATE ||
|
|
|
|
config_.compression == COMPRESSION_LZW)
|
2014-12-04 23:11:33 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);
|
|
|
|
}
|
2014-12-04 00:41:49 +01:00
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
inline void operator()(image_gray16s const&) const
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
|
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
|
|
|
|
TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 16);
|
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
|
2022-01-26 10:43:31 +01:00
|
|
|
if (config_.compression == COMPRESSION_DEFLATE || config_.compression == COMPRESSION_ADOBE_DEFLATE ||
|
|
|
|
config_.compression == COMPRESSION_LZW)
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
inline void operator()(image_gray8 const&) const
|
2014-12-04 00:41:49 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
|
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
|
|
|
|
TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 8);
|
2014-12-04 23:11:33 +01:00
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
|
2022-01-26 10:43:31 +01:00
|
|
|
if (config_.compression == COMPRESSION_DEFLATE || config_.compression == COMPRESSION_ADOBE_DEFLATE ||
|
|
|
|
config_.compression == COMPRESSION_LZW)
|
2014-12-04 23:11:33 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);
|
|
|
|
}
|
2014-12-04 00:41:49 +01:00
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
inline void operator()(image_gray8s const&) const
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
|
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
|
|
|
|
TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 8);
|
|
|
|
TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
|
2022-01-26 10:43:31 +01:00
|
|
|
if (config_.compression == COMPRESSION_DEFLATE || config_.compression == COMPRESSION_ADOBE_DEFLATE ||
|
|
|
|
config_.compression == COMPRESSION_LZW)
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
inline void operator()(image_null const&) const
|
2014-12-04 00:41:49 +01:00
|
|
|
{
|
|
|
|
// Assume this would be null type
|
2015-05-14 13:53:21 +02:00
|
|
|
throw image_writer_exception("Could not write TIFF - Null image provided");
|
2014-12-04 00:41:49 +01:00
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
private:
|
|
|
|
TIFF* output_;
|
|
|
|
tiff_config const& config_;
|
2014-12-04 00:41:49 +01:00
|
|
|
};
|
|
|
|
|
2015-01-25 03:48:15 +01:00
|
|
|
inline void set_tiff_config(TIFF* output, tiff_config const& config)
|
2014-12-04 23:11:33 +01:00
|
|
|
{
|
2014-12-05 00:13:11 +01:00
|
|
|
// Set some constant tiff information that doesn't vary based on type of data
|
2014-12-04 23:11:33 +01:00
|
|
|
// or image size
|
|
|
|
TIFFSetField(output, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
|
2014-12-09 17:30:50 +01:00
|
|
|
|
2014-12-04 23:11:33 +01:00
|
|
|
// Set the compression for the TIFF
|
|
|
|
TIFFSetField(output, TIFFTAG_COMPRESSION, config.compression);
|
2014-12-09 17:30:50 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
if (COMPRESSION_ADOBE_DEFLATE == config.compression || COMPRESSION_DEFLATE == config.compression ||
|
|
|
|
COMPRESSION_LZW == config.compression)
|
2014-12-09 17:30:50 +01:00
|
|
|
{
|
2014-12-04 23:11:33 +01:00
|
|
|
// Set the zip level for the compression
|
|
|
|
// http://en.wikipedia.org/wiki/DEFLATE#Encoder.2Fcompressor
|
|
|
|
// Changes the time spent trying to compress
|
|
|
|
TIFFSetField(output, TIFFTAG_ZIPQUALITY, config.zlevel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
template<typename T1, typename T2>
|
|
|
|
void save_as_tiff(T1& file, T2 const& image, tiff_config const& config)
|
2011-11-28 16:59:19 +01:00
|
|
|
{
|
2014-12-10 09:44:37 +01:00
|
|
|
using pixel_type = typename T2::pixel_type;
|
|
|
|
|
2011-11-28 16:59:19 +01:00
|
|
|
const int width = image.width();
|
|
|
|
const int height = image.height();
|
2013-09-16 08:08:04 +02:00
|
|
|
|
2011-12-05 11:10:13 +01:00
|
|
|
TIFF* output = RealTIFFOpen("mapnik_tiff_stream",
|
2013-05-10 17:50:28 +02:00
|
|
|
"wm",
|
2011-12-05 11:10:13 +01:00
|
|
|
(thandle_t)&file,
|
|
|
|
tiff_dummy_read_proc,
|
|
|
|
tiff_write_proc,
|
|
|
|
tiff_seek_proc,
|
|
|
|
tiff_close_proc,
|
|
|
|
tiff_size_proc,
|
|
|
|
tiff_dummy_map_proc,
|
|
|
|
tiff_dummy_unmap_proc);
|
2022-01-26 10:43:31 +01:00
|
|
|
if (!output)
|
2011-12-05 11:10:13 +01:00
|
|
|
{
|
2015-05-14 13:53:21 +02:00
|
|
|
throw image_writer_exception("Could not write TIFF");
|
2011-12-05 11:10:13 +01:00
|
|
|
}
|
2014-12-04 11:02:42 +01:00
|
|
|
|
2011-11-28 16:59:19 +01:00
|
|
|
TIFFSetField(output, TIFFTAG_IMAGEWIDTH, width);
|
|
|
|
TIFFSetField(output, TIFFTAG_IMAGELENGTH, height);
|
2014-12-04 16:48:19 +01:00
|
|
|
TIFFSetField(output, TIFFTAG_IMAGEDEPTH, 1);
|
2014-12-04 23:11:33 +01:00
|
|
|
set_tiff_config(output, config);
|
2014-12-09 17:30:50 +01:00
|
|
|
|
2014-12-04 00:41:49 +01:00
|
|
|
// Set tags that vary based on the type of data being provided.
|
2014-12-04 23:11:33 +01:00
|
|
|
tag_setter set(output, config);
|
2014-12-04 00:41:49 +01:00
|
|
|
set(image);
|
2014-12-04 11:02:42 +01:00
|
|
|
|
2014-12-15 03:52:32 +01:00
|
|
|
// Use specific types of writing methods.
|
|
|
|
if (TIFF_WRITE_SCANLINE == config.method)
|
2014-12-04 00:41:49 +01:00
|
|
|
{
|
2014-12-04 16:48:19 +01:00
|
|
|
// Process Scanline
|
2014-12-04 00:41:49 +01:00
|
|
|
TIFFSetField(output, TIFFTAG_ROWSPERSTRIP, 1);
|
|
|
|
|
|
|
|
int next_scanline = 0;
|
2022-01-26 10:43:31 +01:00
|
|
|
std::unique_ptr<pixel_type[]> row(new pixel_type[width]);
|
2014-12-04 00:41:49 +01:00
|
|
|
while (next_scanline < height)
|
|
|
|
{
|
2015-05-04 12:49:11 +02:00
|
|
|
std::copy(image.get_row(next_scanline), image.get_row(next_scanline) + width, row.get());
|
2014-12-10 09:44:37 +01:00
|
|
|
TIFFWriteScanline(output, row.get(), next_scanline, 0);
|
2014-12-04 00:41:49 +01:00
|
|
|
++next_scanline;
|
|
|
|
}
|
|
|
|
}
|
2014-12-15 03:52:32 +01:00
|
|
|
else if (TIFF_WRITE_STRIPPED == config.method)
|
|
|
|
{
|
|
|
|
std::size_t rows_per_strip = config.rows_per_strip;
|
2015-01-07 11:35:21 +01:00
|
|
|
if (0 == rows_per_strip)
|
2014-12-15 03:52:32 +01:00
|
|
|
{
|
|
|
|
rows_per_strip = height;
|
|
|
|
}
|
|
|
|
TIFFSetField(output, TIFFTAG_ROWSPERSTRIP, rows_per_strip);
|
|
|
|
std::size_t strip_size = width * rows_per_strip;
|
|
|
|
std::unique_ptr<pixel_type[]> strip_buffer(new pixel_type[strip_size]);
|
2022-01-26 10:43:31 +01:00
|
|
|
for (int y = 0; y < height; y += rows_per_strip)
|
2014-12-15 03:52:32 +01:00
|
|
|
{
|
|
|
|
int ty1 = std::min(height, static_cast<int>(y + rows_per_strip)) - y;
|
|
|
|
int row = y;
|
|
|
|
for (int ty = 0; ty < ty1; ++ty, ++row)
|
|
|
|
{
|
2015-05-04 12:49:11 +02:00
|
|
|
std::copy(image.get_row(row), image.get_row(row) + width, strip_buffer.get() + ty * width);
|
2014-12-15 03:52:32 +01:00
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
if (TIFFWriteEncodedStrip(output,
|
|
|
|
TIFFComputeStrip(output, y, 0),
|
|
|
|
strip_buffer.get(),
|
|
|
|
strip_size * sizeof(pixel_type)) == -1)
|
2014-12-15 03:52:32 +01:00
|
|
|
{
|
2015-05-14 13:53:21 +02:00
|
|
|
throw image_writer_exception("Could not write TIFF - TIFF Tile Write failed");
|
2014-12-15 03:52:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (TIFF_WRITE_TILED == config.method)
|
2014-12-04 00:41:49 +01:00
|
|
|
{
|
2014-12-15 03:52:32 +01:00
|
|
|
int tile_width = config.tile_width;
|
|
|
|
int tile_height = config.tile_height;
|
2015-01-07 11:35:21 +01:00
|
|
|
|
|
|
|
if (0 == tile_height)
|
2014-12-15 03:52:32 +01:00
|
|
|
{
|
|
|
|
tile_height = height;
|
|
|
|
if (height % 16 > 0)
|
|
|
|
{
|
|
|
|
tile_height = height + 16 - (height % 16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (0 == tile_width)
|
|
|
|
{
|
|
|
|
tile_width = width;
|
|
|
|
if (width % 16 > 0)
|
|
|
|
{
|
|
|
|
tile_width = width + 16 - (width % 16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TIFFSetField(output, TIFFTAG_TILEWIDTH, tile_width);
|
|
|
|
TIFFSetField(output, TIFFTAG_TILELENGTH, tile_height);
|
2014-12-04 16:48:19 +01:00
|
|
|
TIFFSetField(output, TIFFTAG_TILEDEPTH, 1);
|
2014-12-15 03:52:32 +01:00
|
|
|
std::size_t tile_size = tile_width * tile_height;
|
2022-01-26 10:43:31 +01:00
|
|
|
std::unique_ptr<pixel_type[]> image_out(new pixel_type[tile_size]);
|
2014-12-15 03:52:32 +01:00
|
|
|
int end_y = (height / tile_height + 1) * tile_height;
|
|
|
|
int end_x = (width / tile_width + 1) * tile_width;
|
|
|
|
end_y = std::min(end_y, height);
|
|
|
|
end_x = std::min(end_x, width);
|
2015-01-07 11:35:21 +01:00
|
|
|
|
2014-12-15 03:52:32 +01:00
|
|
|
for (int y = 0; y < end_y; y += tile_height)
|
|
|
|
{
|
|
|
|
int ty1 = std::min(height, y + tile_height) - y;
|
|
|
|
|
|
|
|
for (int x = 0; x < end_x; x += tile_width)
|
|
|
|
{
|
|
|
|
// Prefill the entire array with zeros.
|
2015-01-22 18:39:37 +01:00
|
|
|
std::fill(image_out.get(), image_out.get() + tile_size, 0);
|
2014-12-15 03:52:32 +01:00
|
|
|
int tx1 = std::min(width, x + tile_width);
|
|
|
|
int row = y;
|
|
|
|
for (int ty = 0; ty < ty1; ++ty, ++row)
|
|
|
|
{
|
2015-05-04 12:49:11 +02:00
|
|
|
std::copy(image.get_row(row, x), image.get_row(row, tx1), image_out.get() + ty * tile_width);
|
2014-12-15 03:52:32 +01:00
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
if (TIFFWriteEncodedTile(output,
|
|
|
|
TIFFComputeTile(output, x, y, 0, 0),
|
|
|
|
image_out.get(),
|
|
|
|
tile_size * sizeof(pixel_type)) == -1)
|
2014-12-15 03:52:32 +01:00
|
|
|
{
|
2015-05-14 13:53:21 +02:00
|
|
|
throw image_writer_exception("Could not write TIFF - TIFF Tile Write failed");
|
2014-12-15 03:52:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-04 00:41:49 +01:00
|
|
|
}
|
2011-12-05 11:10:13 +01:00
|
|
|
// TODO - handle palette images
|
|
|
|
// std::vector<mapnik::rgb> const& palette
|
2013-07-24 12:02:06 +02:00
|
|
|
|
|
|
|
// unsigned short r[256], g[256], b[256];
|
|
|
|
// for (int i = 0; i < (1 << 24); ++i)
|
|
|
|
// {
|
|
|
|
// r[i] = (unsigned short)palette[i * 3 + 0] << 8;
|
|
|
|
// g[i] = (unsigned short)palette[i * 3 + 1] << 8;
|
|
|
|
// b[i] = (unsigned short)palette[i * 3 + 2] << 8;
|
|
|
|
// }
|
|
|
|
// TIFFSetField(output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
|
|
|
|
// TIFFSetField(output, TIFFTAG_COLORMAP, r, g, b);
|
2011-12-05 11:10:13 +01:00
|
|
|
|
|
|
|
RealTIFFClose(output);
|
2011-11-28 16:59:19 +01:00
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
} // namespace mapnik
|
2011-11-28 16:59:19 +01:00
|
|
|
|
|
|
|
#endif // MAPNIK_TIFF_IO_HPP
|