File: image_io_test.cpp

package info (click to toggle)
mapnik 3.0.12%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 17,084 kB
  • ctags: 18,454
  • sloc: cpp: 142,516; python: 1,416; sh: 769; makefile: 170; xml: 140; lisp: 13
file content (236 lines) | stat: -rw-r--r-- 8,465 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "catch.hpp"

#include <cstring>
#include <mapnik/color.hpp>
#include <mapnik/image.hpp>
#include <mapnik/image_reader.hpp>
#include <mapnik/image_util.hpp>
#include <mapnik/image_util_jpeg.hpp>
#include <mapnik/util/fs.hpp>
#if defined(HAVE_CAIRO)
#include <mapnik/cairo/cairo_context.hpp>
#include <mapnik/cairo/cairo_image_util.hpp>
#endif

#pragma GCC diagnostic push
#include <mapnik/warning_ignore.hpp>
#include <boost/format.hpp>
#include <boost/filesystem/convenience.hpp>
#pragma GCC diagnostic pop

inline void make_directory(std::string const& dir) {
    boost::filesystem::create_directories(dir);
}

namespace {
template <typename T>
void check_tiny_png_image_quantising(T const& im)
{
    std::ostringstream ss(std::ios::binary);
    mapnik::save_to_stream(im, ss, "png8");
    ss.flush();
    std::string str = ss.str();
    std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(str.data(), str.size()));
    auto w = reader->width();
    auto h = reader->height();
    CHECK(w > 0);
    CHECK(h > 0);
    auto im2 = mapnik::util::get<mapnik::image_rgba8>(reader->read(0, 0, w, h));
    for (std::size_t i = 0; i < w; ++i)
    {
        for (std::size_t j = 0; j < h; ++j)
        {
            REQUIRE(im2(i,j) == im(i,j));
        }
    }
}

}

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();
        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() );
#if defined(HAVE_JPEG)
        should_throw = "./test/data/images/blank.jpg";
        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)
        {
            REQUIRE( std::string(ex.what()) == std::string("JPEG Reader: libjpeg could not read image: Not a JPEG file: starts with 0x89 0x50") );
        }

#endif

        REQUIRE_THROWS(mapnik::image_rgba8 im(-10,-10)); // should throw rather than overflow

#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";
        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)));

        should_throw = "./test/data/images/xcode-CgBI.png";
        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)));
#endif

#if defined(HAVE_TIFF)
        should_throw = "./test/data/images/blank.tiff";
        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)));
#endif

#if defined(HAVE_WEBP)
        should_throw = "./test/data/images/blank.webp";
        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)));
#endif
    }
    catch (std::exception const & ex)
    {
        std::clog << ex.what() << "\n";
        REQUIRE(false);
    }

} // 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


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::tuple<std::string, std::string> > supported_types;
#if defined(HAVE_PNG)
    supported_types.push_back(std::make_tuple("png","png"));
    supported_types.push_back(std::make_tuple("png","png24"));
    supported_types.push_back(std::make_tuple("png","png32"));
    supported_types.push_back(std::make_tuple("png","png8"));
    supported_types.push_back(std::make_tuple("png","png256"));
#endif
#if defined(HAVE_JPEG)
    supported_types.push_back(std::make_tuple("jpeg","jpeg"));
    supported_types.push_back(std::make_tuple("jpeg","jpeg80"));
    supported_types.push_back(std::make_tuple("jpeg","jpeg90"));
#endif
#if defined(HAVE_TIFF)
    supported_types.push_back(std::make_tuple("tiff","tiff"));
#endif
#if defined(HAVE_WEBP)
    supported_types.push_back(std::make_tuple("webp","webp"));
#endif

    std::string directory_name("/tmp/mapnik-tests/");
    make_directory(directory_name);
    REQUIRE(mapnik::util::exists(directory_name));

    for (auto const& info : supported_types)
    {
        std::string extension;
        std::string format;
        std::tie(extension, format) = info;
        std::string filename = (boost::format(directory_name + "mapnik-%1%.%2%") % named_color % extension).str();
        mapnik::save_to_file(im, filename);
        std::string str = mapnik::save_to_string(im, format);
        std::ostringstream ss;
        mapnik::save_to_stream(im, ss, format);
        CHECK(str.length() == ss.str().length());
        // wrap reader in scope to ensure the file handle is
        // released before we try to remove the file
        {
            std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(filename, extension));
            unsigned w = reader->width();
            unsigned h = reader->height();
            auto im2 = reader->read(0, 0, w, h);
            CHECK(im2.size() == im.size());
            if (extension == "png" || extension == "tiff")
            {
                CHECK(0 == std::memcmp(im2.bytes(), im.bytes(), im.width() * im.height()));
            }
        }
        if (mapnik::util::exists(filename))
        {
            mapnik::util::remove(filename);
        }
    }
}

SECTION("Quantising small (less than 3 pixel images preserve original colours")
{
#if defined(HAVE_PNG)
    { // 1x1
        mapnik::image_rgba8 im(1,1); // 1 pixel
        im(0,0) = mapnik::color("green").rgba();
        check_tiny_png_image_quantising(im);
    }
    { // 1x2
        mapnik::image_rgba8 im(1,2); // 2 pixels
        mapnik::fill(im, mapnik::color("red").rgba());
        im(0,0) = mapnik::color("green").rgba();
        check_tiny_png_image_quantising(im);
    }
    { // 2x1
        mapnik::image_rgba8 im(2,1); // 2 pixels
        mapnik::fill(im, mapnik::color("red").rgba());
        im(0,0) = mapnik::color("green").rgba();
        check_tiny_png_image_quantising(im);
    }
#endif
} // END SECTION

} // END TEST_CASE