mapnik/test/unit/imaging/image_io_test.cpp

159 lines
5.6 KiB
C++
Raw Normal View History

2015-04-24 14:40:22 +02:00
#include "catch.hpp"
#include <iostream>
#include <cstring>
2015-04-24 14:40:22 +02:00
#include <mapnik/image.hpp>
#include <mapnik/image_reader.hpp>
#include <mapnik/image_util.hpp>
#include <mapnik/image_util_jpeg.hpp>
2015-04-24 14:40:22 +02:00
#include <mapnik/util/fs.hpp>
#if defined(HAVE_CAIRO)
#include <mapnik/cairo/cairo_context.hpp>
#include <mapnik/cairo/cairo_image_util.hpp>
#endif
#include <boost/format.hpp>
2015-04-24 14:40:22 +02:00
TEST_CASE("image io") {
SECTION("readers") {
std::string should_throw;
boost::optional<std::string> type;
try
{
mapnik::image_rgba8 im_og;
auto im_size = mapnik::image_rgba8::pixel_size * im_og.width() * im_og.height();
2015-09-16 11:15:21 +02:00
mapnik::detail::buffer buf(im_og.bytes(), im_size);
mapnik::image_rgba8 im2(im_og.width(), im_og.height(), buf.data());
CHECK( im2.bytes() == im_og.bytes() );
2015-04-24 14:40:22 +02:00
#if defined(HAVE_JPEG)
should_throw = "./test/data/images/blank.jpg";
2015-04-24 14:40:22 +02:00
REQUIRE( mapnik::util::exists( should_throw ) );
type = mapnik::type_from_filename(should_throw);
REQUIRE( type );
REQUIRE_THROWS(std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(should_throw,*type)));
// actually a png so jpeg reader should throw
should_throw = "./test/data/images/landusepattern.jpg";
REQUIRE( mapnik::util::exists( should_throw ) );
type = mapnik::type_from_filename(should_throw);
REQUIRE( type );
try
{
std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(should_throw,*type));
REQUIRE(false);
}
catch (std::exception const& ex)
{
2015-06-11 23:04:20 +02:00
REQUIRE( std::string(ex.what()) == std::string("JPEG Reader: libjpeg could not read image: Not a JPEG file: starts with 0x89 0x50") );
}
2015-04-24 14:40:22 +02:00
#endif
REQUIRE_THROWS(mapnik::image_rgba8 im(-10,-10)); // should throw rather than overflow
2015-04-24 14:40:22 +02:00
#if defined(HAVE_CAIRO)
mapnik::cairo_surface_ptr image_surface(
cairo_image_surface_create(CAIRO_FORMAT_ARGB32,256,257),
mapnik::cairo_surface_closer());
mapnik::image_rgba8 im_data(cairo_image_surface_get_width(&*image_surface), cairo_image_surface_get_height(&*image_surface));
im_data.set(1);
REQUIRE( (unsigned)im_data(0,0) == unsigned(1) );
// Should set back to fully transparent
mapnik::cairo_image_to_rgba8(im_data, image_surface);
REQUIRE( (unsigned)im_data(0,0) == unsigned(0) );
#endif
#if defined(HAVE_PNG)
should_throw = "./test/data/images/blank.png";
2015-04-24 14:40:22 +02:00
REQUIRE( mapnik::util::exists( should_throw ) );
type = mapnik::type_from_filename(should_throw);
REQUIRE( type );
REQUIRE_THROWS(std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(should_throw,*type)));
2015-04-24 14:40:22 +02:00
should_throw = "./test/data/images/xcode-CgBI.png";
2015-04-24 14:40:22 +02:00
REQUIRE( mapnik::util::exists( should_throw ) );
type = mapnik::type_from_filename(should_throw);
REQUIRE( type );
REQUIRE_THROWS(std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(should_throw,*type)));
2015-04-24 14:40:22 +02:00
#endif
#if defined(HAVE_TIFF)
should_throw = "./test/data/images/blank.tiff";
2015-04-24 14:40:22 +02:00
REQUIRE( mapnik::util::exists( should_throw ) );
type = mapnik::type_from_filename(should_throw);
REQUIRE( type );
REQUIRE_THROWS(std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(should_throw,*type)));
2015-04-24 14:40:22 +02:00
#endif
#if defined(HAVE_WEBP)
should_throw = "./test/data/images/blank.webp";
2015-04-24 14:40:22 +02:00
REQUIRE( mapnik::util::exists( should_throw ) );
type = mapnik::type_from_filename(should_throw);
REQUIRE( type );
REQUIRE_THROWS(std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(should_throw,*type)));
2015-04-24 14:40:22 +02:00
#endif
}
catch (std::exception const & ex)
{
std::clog << ex.what() << "\n";
REQUIRE(false);
}
2015-05-12 01:19:24 +02:00
} // END SECTION
SECTION("writers options")
{
#if defined(HAVE_JPEG)
// test we can parse both jpegXX and quality=XX options
REQUIRE_THROWS(mapnik::detail::parse_jpeg_quality("jpegXX"));
REQUIRE_THROWS(mapnik::detail::parse_jpeg_quality("jpeg:quality=XX"));
int q0 = mapnik::detail::parse_jpeg_quality("jpeg50");
int q1 = mapnik::detail::parse_jpeg_quality("jpeg:quality=50");
REQUIRE(q0 == q1);
#endif
} // END SECTION
2015-05-12 01:19:24 +02:00
SECTION("image_util : save_to_file/save_to_stream/save_to_string")
{
mapnik::image_rgba8 im(256,256);
std::string named_color = "lightblue";
mapnik::fill(im, mapnik::color(named_color).rgba());
////////////////////////////////////////////////////
std::vector<std::string> supported_types;
#if defined(HAVE_PNG)
supported_types.push_back("png");
#endif
#if defined(HAVE_JPEG)
supported_types.push_back("jpeg");
#endif
#if defined(HAVE_TIFF)
supported_types.push_back("tiff");
#endif
#if defined(HAVE_WEBP)
supported_types.push_back("webp");
#endif
for (auto const& type : supported_types)
{
std::string filename = (boost::format("/tmp/mapnik-%1%.%2%") % named_color % type).str();
mapnik::save_to_file(im, filename);
std::string str = mapnik::save_to_string(im, type);
std::ostringstream ss;
mapnik::save_to_stream(im, ss, type);
CHECK(str.length() == ss.str().length());
std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(filename,type));
unsigned w = reader->width();
unsigned h = reader->height();
auto im2 = reader->read(0, 0, w, h);
CHECK(im2.size() == im.size());
if (type == "png" || type == "tiff")
{
CHECK(0 == std::memcmp(im2.bytes(), im.bytes(), im.width() * im.height()));
}
}
}
2015-05-12 01:19:24 +02:00
} // END TEST_CASE