TIFF I/O unit tests - check tiff_reader
is able to read various permutations of TIFF format (rgb8|rgba8|gray8 + interleave:band|pixel + tiled|stripped)
(ref #3669 #3670)
This commit is contained in:
parent
ef9b1025fa
commit
6ac4ce66b3
2 changed files with 396 additions and 247 deletions
|
@ -1 +1 @@
|
|||
Subproject commit dac50a321bdcc92c6183ded08200eb8fa117532c
|
||||
Subproject commit c059a8612770669c7267c288fa63be6bb8490df0
|
|
@ -5,8 +5,12 @@
|
|||
|
||||
#include "catch.hpp"
|
||||
|
||||
#include <mapnik/color.hpp>
|
||||
#include <mapnik/image_util.hpp>
|
||||
#include <mapnik/image_view.hpp>
|
||||
#include <mapnik/image_reader.hpp>
|
||||
#include <mapnik/util/file_io.hpp>
|
||||
#include <mapnik/util/fs.hpp>
|
||||
|
||||
#include "../../../src/tiff_reader.cpp"
|
||||
|
||||
|
@ -56,8 +60,153 @@
|
|||
REQUIRE( subimage.width() == 1 ); \
|
||||
REQUIRE( subimage.height() == 1 ); \
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
template <typename Image>
|
||||
struct test_image_traits
|
||||
{
|
||||
using value_type = mapnik::color;
|
||||
static value_type const& get_value(mapnik::color const& c)
|
||||
{
|
||||
return c;
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct test_image_traits<mapnik::image_gray8>
|
||||
{
|
||||
using value_type = std::uint8_t;
|
||||
static value_type get_value(mapnik::color const& c)
|
||||
{
|
||||
return c.green(); // use green channel for gray scale images
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <typename Image>
|
||||
Image generate_test_image()
|
||||
{
|
||||
|
||||
std::size_t tile_size = 16;
|
||||
Image im(64, 64);
|
||||
mapnik::color colors[] = {{mapnik::color("red")},
|
||||
{mapnik::color("blue")},
|
||||
{mapnik::color("green")},
|
||||
{mapnik::color("yellow")}};
|
||||
std::size_t index = 0;
|
||||
for (std::size_t j = 0; j < im.height()/tile_size; ++j)
|
||||
{
|
||||
++index;
|
||||
for (std::size_t i = 0; i < im.width()/tile_size; ++i)
|
||||
{
|
||||
++index;
|
||||
for (std::size_t x = 0; x < tile_size; ++x)
|
||||
{
|
||||
for (std::size_t y = 0; y < tile_size; ++y)
|
||||
{
|
||||
auto value = test_image_traits<Image>::get_value(colors[index % 4]);
|
||||
mapnik::set_pixel(im, i * tile_size + x, j * tile_size + y, value );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return im;
|
||||
}
|
||||
|
||||
template <typename Image1, typename Image2>
|
||||
bool identical(Image1 const& im1, Image2 const& im2)
|
||||
{
|
||||
std::cerr << "gotcha! Image1 w/h" << im1.width() << ":" << im1.height()
|
||||
<< " Image2 w/h" << im2.width() << ":" << im2.height()
|
||||
<< std::endl;
|
||||
|
||||
if ((im1.width() != im2.width()) || (im1.height() != im2.height()))
|
||||
return false;
|
||||
|
||||
for (std::size_t i = 0; i < im1.width(); ++i)
|
||||
{
|
||||
for (std::size_t j = 0; j < im1.height(); ++j)
|
||||
{
|
||||
if (im1(i,j) != im2(i,j))
|
||||
{
|
||||
std::cerr << "FAIL at (" << i << "," << j << ") " << int(im1(i,j)) << "<>" << int(im2(i,j)) << std::endl;
|
||||
//mapnik::save_to_file(im1, "/tmp/image-fail-1.tif");
|
||||
//mapnik::save_to_file(im2, "/tmp/image-fail-2.tif", "tiff");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("tiff io")
|
||||
{
|
||||
SECTION("tiff-reader rgb(a)8")
|
||||
{
|
||||
// generate expected rgba8 image
|
||||
auto im = generate_test_image<mapnik::image_rgba8>();
|
||||
|
||||
for (auto const& path : mapnik::util::list_directory("test/data/tiff/"))
|
||||
{
|
||||
if (boost::iends_with(path,".tif")
|
||||
&& boost::istarts_with(mapnik::util::basename(path), "tiff_rgb"))
|
||||
{
|
||||
mapnik::tiff_reader<std::filebuf> tiff_reader(path);
|
||||
auto width = tiff_reader.width();
|
||||
auto height = tiff_reader.height();
|
||||
{
|
||||
// read whole image
|
||||
auto tiff = tiff_reader.read(0, 0, width, height);
|
||||
CHECK(tiff.is<mapnik::image_rgba8>());
|
||||
auto im2 = tiff.get<mapnik::image_rgba8>();
|
||||
REQUIRE(identical(im, im2));
|
||||
}
|
||||
{
|
||||
// read a portion
|
||||
auto tiff = tiff_reader.read(11, 13, width - 11, height - 13);
|
||||
CHECK(tiff.is<mapnik::image_rgba8>());
|
||||
auto im2 = tiff.get<mapnik::image_rgba8>();
|
||||
auto view = mapnik::image_view_rgba8(11, 13, width, height, im);
|
||||
REQUIRE(identical(view, im2));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("tiff-reader gray8")
|
||||
{
|
||||
// generate expected gray8 image
|
||||
auto im = generate_test_image<mapnik::image_gray8>();
|
||||
|
||||
for (auto const& path : mapnik::util::list_directory("test/data/tiff/"))
|
||||
{
|
||||
if (boost::iends_with(path,".tif")
|
||||
&& boost::istarts_with(mapnik::util::basename(path), "tiff_gray"))
|
||||
{
|
||||
std::cerr << path << std::endl;
|
||||
mapnik::tiff_reader<std::filebuf> tiff_reader(path);
|
||||
auto width = tiff_reader.width();
|
||||
auto height = tiff_reader.height();
|
||||
{
|
||||
// read whole image
|
||||
auto tiff = tiff_reader.read(0, 0, width, height);
|
||||
CHECK(tiff.is<mapnik::image_gray8>());
|
||||
auto im2 = tiff.get<mapnik::image_gray8>();
|
||||
REQUIRE(identical(im, im2));
|
||||
}
|
||||
{
|
||||
// read a portion
|
||||
auto tiff = tiff_reader.read(11, 13, width - 11, height - 13);
|
||||
CHECK(tiff.is<mapnik::image_gray8>());
|
||||
auto im2 = tiff.get<mapnik::image_gray8>();
|
||||
auto view = mapnik::image_view_gray8(11, 13, width, height, im);
|
||||
REQUIRE(identical(view, im2));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("scan rgb8 striped")
|
||||
{
|
||||
|
@ -173,8 +322,8 @@ SECTION("rgb8 tiled") {
|
|||
REQUIRE( tiff_reader.rows_per_strip() == 0 );
|
||||
REQUIRE( tiff_reader.bits_per_sample() == 8 );
|
||||
REQUIRE( tiff_reader.is_tiled() == true );
|
||||
REQUIRE( tiff_reader.tile_width() == 256 );
|
||||
REQUIRE( tiff_reader.tile_height() == 256 );
|
||||
REQUIRE( tiff_reader.tile_width() == 32 );
|
||||
REQUIRE( tiff_reader.tile_height() == 32 );
|
||||
REQUIRE( tiff_reader.photometric() == PHOTOMETRIC_RGB );
|
||||
REQUIRE( tiff_reader.compression() == COMPRESSION_LZW );
|
||||
mapnik::image_any data = reader->read(0, 0, reader->width(), reader->height());
|
||||
|
|
Loading…
Reference in a new issue