2015-01-08 22:55:51 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2015-06-16 12:49:16 +02:00
|
|
|
* Copyright (C) 2015 Artem Pavlenko
|
2015-01-08 22:55:51 +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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
// mapnik
|
|
|
|
#if defined(HAVE_TIFF)
|
|
|
|
#include <mapnik/tiff_io.hpp>
|
|
|
|
#endif
|
|
|
|
|
2015-01-09 01:31:14 +01:00
|
|
|
#include <mapnik/image_util.hpp>
|
2015-01-08 22:55:51 +01:00
|
|
|
#include <mapnik/image_util_tiff.hpp>
|
2015-01-22 18:39:37 +01:00
|
|
|
#include <mapnik/image.hpp>
|
2015-01-09 01:31:14 +01:00
|
|
|
#include <mapnik/image_view.hpp>
|
2015-05-15 17:54:15 +02:00
|
|
|
#include <mapnik/image_options.hpp>
|
2015-01-09 01:31:14 +01:00
|
|
|
#include <mapnik/util/conversions.hpp>
|
2015-01-08 22:55:51 +01:00
|
|
|
|
|
|
|
// stl
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace mapnik
|
|
|
|
{
|
|
|
|
|
|
|
|
#if defined(HAVE_TIFF)
|
|
|
|
void handle_tiff_options(std::string const& type,
|
|
|
|
tiff_config & config)
|
|
|
|
{
|
|
|
|
if (type == "tiff")
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (type.length() > 4)
|
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
|
|
|
|
for (auto const& kv : parse_image_options(type))
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
auto const& key = kv.first;
|
|
|
|
auto const& val = kv.second;
|
2015-05-15 22:25:52 +02:00
|
|
|
if (key == "tiff") continue;
|
2015-05-18 12:21:56 +02:00
|
|
|
else if (key == "compression")
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
if (val && !(*val).empty())
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
if (*val == "deflate")
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
|
|
|
config.compression = COMPRESSION_DEFLATE;
|
|
|
|
}
|
2015-05-15 17:54:15 +02:00
|
|
|
else if (*val == "adobedeflate")
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
|
|
|
config.compression = COMPRESSION_ADOBE_DEFLATE;
|
|
|
|
}
|
2015-05-15 17:54:15 +02:00
|
|
|
else if (*val == "lzw")
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
|
|
|
config.compression = COMPRESSION_LZW;
|
|
|
|
}
|
2015-05-15 17:54:15 +02:00
|
|
|
else if (*val == "none")
|
2015-05-14 13:53:21 +02:00
|
|
|
{
|
2015-01-08 22:55:51 +01:00
|
|
|
config.compression = COMPRESSION_NONE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
throw image_writer_exception("invalid tiff compression: '" + *val + "'");
|
2015-01-08 22:55:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 17:54:15 +02:00
|
|
|
else if (key == "method")
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
if (val && !(*val).empty())
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
if (*val == "scanline")
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
|
|
|
config.method = TIFF_WRITE_SCANLINE;
|
|
|
|
}
|
2015-05-15 17:54:15 +02:00
|
|
|
else if (*val == "strip" || *val == "stripped")
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
|
|
|
config.method = TIFF_WRITE_STRIPPED;
|
|
|
|
}
|
2015-05-15 17:54:15 +02:00
|
|
|
else if (*val == "tiled")
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
|
|
|
config.method = TIFF_WRITE_TILED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
throw image_writer_exception("invalid tiff method: '" + *val + "'");
|
2015-01-08 22:55:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 17:54:15 +02:00
|
|
|
else if (key == "zlevel")
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
if (val && !(*val).empty())
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
if (!mapnik::util::string2int(*val,config.zlevel) || config.zlevel < 0 || config.zlevel > 9)
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
throw image_writer_exception("invalid tiff zlevel: '" + *val + "'");
|
2015-01-08 22:55:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 17:54:15 +02:00
|
|
|
else if (key == "tile_height")
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
if (val && !(*val).empty())
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
if (!mapnik::util::string2int(*val,config.tile_height) || config.tile_height < 0 )
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
throw image_writer_exception("invalid tiff tile_height: '" + *val + "'");
|
2015-01-08 22:55:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 17:54:15 +02:00
|
|
|
else if (key == "tile_width")
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
if (val && !(*val).empty())
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
if (!mapnik::util::string2int(*val,config.tile_width) || config.tile_width < 0 )
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
throw image_writer_exception("invalid tiff tile_width: '" + *val + "'");
|
2015-01-08 22:55:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 17:54:15 +02:00
|
|
|
else if (key == "rows_per_strip")
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
if (val && !(*val).empty())
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
if (!mapnik::util::string2int(*val,config.rows_per_strip) || config.rows_per_strip < 0 )
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
throw image_writer_exception("invalid tiff rows_per_strip: '" + *val + "'");
|
2015-01-08 22:55:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-15 17:54:15 +02:00
|
|
|
throw image_writer_exception("unhandled tiff option: " + key);
|
2015-01-08 22:55:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tiff_saver::tiff_saver(std::ostream & stream, std::string const& t):
|
|
|
|
stream_(stream), t_(t) {}
|
2015-03-20 03:28:16 +01:00
|
|
|
|
2015-01-09 01:31:14 +01:00
|
|
|
template<>
|
2015-01-22 17:58:01 +01:00
|
|
|
void tiff_saver::operator()<image_null> (image_null const& image) const
|
2015-01-08 22:55:51 +01:00
|
|
|
{
|
2015-05-14 13:53:21 +02:00
|
|
|
throw image_writer_exception("null images not supported");
|
2015-01-08 22:55:51 +01:00
|
|
|
}
|
|
|
|
|
2015-03-20 03:28:16 +01:00
|
|
|
template<>
|
|
|
|
void tiff_saver::operator()<image_view_null> (image_view_null const& image) const
|
|
|
|
{
|
2015-05-14 13:53:21 +02:00
|
|
|
throw image_writer_exception("null image views not supported");
|
2015-03-20 03:28:16 +01:00
|
|
|
}
|
|
|
|
|
2015-01-08 22:55:51 +01:00
|
|
|
template <typename T>
|
|
|
|
void tiff_saver::operator() (T const& image) const
|
|
|
|
{
|
|
|
|
#if defined(HAVE_TIFF)
|
|
|
|
tiff_config opts;
|
|
|
|
handle_tiff_options(t_, opts);
|
|
|
|
save_as_tiff(stream_, image, opts);
|
|
|
|
#else
|
2015-05-14 13:53:21 +02:00
|
|
|
throw image_writer_exception("tiff output is not enabled in your build of Mapnik");
|
2015-01-08 22:55:51 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-02-04 22:41:58 +01:00
|
|
|
template void tiff_saver::operator()<image_rgba8> (image_rgba8 const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_gray8> (image_gray8 const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_gray8s> (image_gray8s const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_gray16> (image_gray16 const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_gray16s> (image_gray16s const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_gray32> (image_gray32 const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_gray32s> (image_gray32s const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_gray32f> (image_gray32f const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_gray64> (image_gray64 const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_gray64s> (image_gray64s const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_gray64f> (image_gray64f const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_view_rgba8> (image_view_rgba8 const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_view_gray8> (image_view_gray8 const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_view_gray8s> (image_view_gray8s const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_view_gray16> (image_view_gray16 const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_view_gray16s> (image_view_gray16s const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_view_gray32> (image_view_gray32 const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_view_gray32s> (image_view_gray32s const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_view_gray32f> (image_view_gray32f const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_view_gray64> (image_view_gray64 const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_view_gray64s> (image_view_gray64s const& image) const;
|
|
|
|
template void tiff_saver::operator()<image_view_gray64f> (image_view_gray64f const& image) const;
|
2015-01-08 22:55:51 +01:00
|
|
|
|
|
|
|
} // end ns
|