File: tiff_io.cpp

package info (click to toggle)
mapnik 4.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 18,548 kB
  • sloc: cpp: 163,861; python: 1,190; sh: 690; xml: 161; makefile: 123; perl: 28; lisp: 13
file content (413 lines) | stat: -rw-r--r-- 18,985 bytes parent folder | download | duplicates (3)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// disabled on windows due to https://github.com/mapnik/mapnik/issues/2838
// TODO - get to the bottom of why including `tiff_reader.cpp` breaks windows
// or re-write image_readers to allow `#include tiff_reader.hpp`
#ifdef HAVE_TIFF

#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 <boost/interprocess/streams/bufferstream.hpp>
#include "../../../src/tiff_reader.hpp"

#if defined(MAPNIK_MEMORY_MAPPED_FILE)
using source_type = boost::interprocess::ibufferstream;
#else
using source_type = std::filebuf;
#endif

#define TIFF_ASSERT(filename)                                                                                          \
    mapnik::tiff_reader<source_type> tiff_reader(filename);                                                            \
    REQUIRE(tiff_reader.width() == 256);                                                                               \
    REQUIRE(tiff_reader.height() == 256);                                                                              \
    REQUIRE(tiff_reader.planar_config() == PLANARCONFIG_CONTIG);                                                       \
    std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(filename, "tiff"));                          \
    REQUIRE(reader->width() == 256);                                                                                   \
    REQUIRE(reader->height() == 256);                                                                                  \
    mapnik::util::file file(filename);                                                                                 \
    mapnik::tiff_reader<mapnik::util::char_array_buffer> tiff_reader2(file.data().get(), file.size());                 \
    REQUIRE(tiff_reader2.width() == 256);                                                                              \
    REQUIRE(tiff_reader2.height() == 256);                                                                             \
    std::unique_ptr<mapnik::image_reader> reader2(mapnik::get_image_reader(file.data().get(), file.size()));           \
    REQUIRE(reader2->width() == 256);                                                                                  \
    REQUIRE(reader2->height() == 256);

#define TIFF_ASSERT_ALPHA(data)                                                                                        \
    REQUIRE(tiff_reader.has_alpha() == true);                                                                          \
    REQUIRE(reader->has_alpha() == true);                                                                              \
    REQUIRE(tiff_reader2.has_alpha() == true);                                                                         \
    REQUIRE(reader2->has_alpha() == true);                                                                             \
    REQUIRE(data.get_premultiplied() == true);

#define TIFF_ASSERT_NO_ALPHA_RGB(data)                                                                                 \
    REQUIRE(tiff_reader.has_alpha() == false);                                                                         \
    REQUIRE(reader->has_alpha() == false);                                                                             \
    REQUIRE(tiff_reader2.has_alpha() == false);                                                                        \
    REQUIRE(reader2->has_alpha() == false);                                                                            \
    REQUIRE(data.get_premultiplied() == true);

#define TIFF_ASSERT_NO_ALPHA_GRAY(data)                                                                                \
    REQUIRE(tiff_reader.has_alpha() == false);                                                                         \
    REQUIRE(reader->has_alpha() == false);                                                                             \
    REQUIRE(tiff_reader2.has_alpha() == false);                                                                        \
    REQUIRE(reader2->has_alpha() == false);                                                                            \
    REQUIRE(data.get_premultiplied() == false);

#define TIFF_ASSERT_SIZE(data, reader)                                                                                 \
    REQUIRE(data.width() == reader->width());                                                                          \
    REQUIRE(data.height() == reader->height());

#define TIFF_READ_ONE_PIXEL                                                                                            \
    mapnik::image_any subimage = reader->read(1, 1, 1, 1);                                                             \
    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)
{
    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))
                return false;
        }
    }
    return true;
}

template<typename Image>
void test_tiff_reader(std::string const& pattern)
{
    // generate expected image (rgba8 or gray8)
    auto im = generate_test_image<Image>();

    for (auto const& path : mapnik::util::list_directory("test/data/tiff/"))
    {
        if (boost::iends_with(path, ".tif") && boost::istarts_with(mapnik::util::basename(path), pattern))
        {
            mapnik::tiff_reader<source_type> tiff_reader(path);
            auto width = tiff_reader.width();
            auto height = tiff_reader.height();
            {
                // whole image
                auto tiff = tiff_reader.read(0, 0, width, height);
                CHECK(tiff.is<Image>());
                auto im2 = tiff.get<Image>();
                REQUIRE(identical(im, im2));
            }
            {
                // portion
                auto tiff = tiff_reader.read(11, 13, width - 11, height - 13);
                CHECK(tiff.is<Image>());
                auto im2 = tiff.get<Image>();
                auto view = mapnik::image_view<Image>(11, 13, width, height, im);
                REQUIRE(identical(view, im2));
            }
        }
    }
}

} // namespace

TEST_CASE("tiff io")
{
    SECTION("tiff-reader rgb8+rgba8")
    {
        test_tiff_reader<mapnik::image_rgba8>("tiff_rgb");
    }

    SECTION("tiff-reader gray8")
    {
        test_tiff_reader<mapnik::image_gray8>("tiff_gray");
    }

    SECTION("scan rgb8 striped")
    {
        std::string filename("./test/data/tiff/scan_512x512_rgb8_striped.tif");
        mapnik::tiff_reader<source_type> tiff_reader(filename);
        REQUIRE(tiff_reader.width() == 512);
        REQUIRE(tiff_reader.height() == 512);
        REQUIRE(tiff_reader.planar_config() == PLANARCONFIG_CONTIG);
        REQUIRE(tiff_reader.rows_per_strip() == 16);
        REQUIRE(tiff_reader.bits_per_sample() == 8);
        REQUIRE(tiff_reader.is_tiled() == false);
        REQUIRE(tiff_reader.tile_width() == 0);
        REQUIRE(tiff_reader.tile_height() == 0);
        REQUIRE(tiff_reader.photometric() == PHOTOMETRIC_PALETTE);
        REQUIRE(tiff_reader.compression() == COMPRESSION_NONE);
        std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(filename, "tiff"));
        REQUIRE(reader->width() == 512);
        REQUIRE(reader->height() == 512);
        mapnik::util::file file(filename);
        mapnik::tiff_reader<mapnik::util::char_array_buffer> tiff_reader2(file.data().get(), file.size());
        REQUIRE(tiff_reader2.width() == 512);
        REQUIRE(tiff_reader2.height() == 512);
        std::unique_ptr<mapnik::image_reader> reader2(mapnik::get_image_reader(file.data().get(), file.size()));
        REQUIRE(reader2->width() == 512);
        REQUIRE(reader2->height() == 512);
        mapnik::image_any data = reader->read(0, 0, reader->width(), reader->height());
        REQUIRE(data.is<mapnik::image_rgba8>() == true);
        TIFF_ASSERT_SIZE(data, reader);
        TIFF_ASSERT_NO_ALPHA_RGB(data);
        TIFF_READ_ONE_PIXEL
    }

    SECTION("scan rgb8 tiled")
    {
        std::string filename("./test/data/tiff/scan_512x512_rgb8_tiled.tif");
        mapnik::tiff_reader<source_type> tiff_reader(filename);
        REQUIRE(tiff_reader.width() == 512);
        REQUIRE(tiff_reader.height() == 512);
        REQUIRE(tiff_reader.planar_config() == PLANARCONFIG_CONTIG);
        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.photometric() == PHOTOMETRIC_PALETTE);
        REQUIRE(tiff_reader.compression() == COMPRESSION_LZW);
        std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(filename, "tiff"));
        REQUIRE(reader->width() == 512);
        REQUIRE(reader->height() == 512);
        mapnik::util::file file(filename);
        mapnik::tiff_reader<mapnik::util::char_array_buffer> tiff_reader2(file.data().get(), file.size());
        REQUIRE(tiff_reader2.width() == 512);
        REQUIRE(tiff_reader2.height() == 512);
        std::unique_ptr<mapnik::image_reader> reader2(mapnik::get_image_reader(file.data().get(), file.size()));
        REQUIRE(reader2->width() == 512);
        REQUIRE(reader2->height() == 512);
        mapnik::image_any data = reader->read(0, 0, reader->width(), reader->height());
        REQUIRE(data.is<mapnik::image_rgba8>() == true);
        TIFF_ASSERT_SIZE(data, reader);
        TIFF_ASSERT_NO_ALPHA_RGB(data);
        TIFF_READ_ONE_PIXEL
    }

    SECTION("rgba8 striped")
    {
        TIFF_ASSERT("./test/data/tiff/ndvi_256x256_rgba8_striped.tif")
        REQUIRE(tiff_reader.rows_per_strip() == 1);
        REQUIRE(tiff_reader.bits_per_sample() == 8);
        REQUIRE(tiff_reader.is_tiled() == false);
        REQUIRE(tiff_reader.tile_width() == 0);
        REQUIRE(tiff_reader.tile_height() == 0);
        REQUIRE(tiff_reader.photometric() == PHOTOMETRIC_RGB);
        REQUIRE(tiff_reader.compression() == COMPRESSION_ADOBE_DEFLATE);
        mapnik::image_any data = reader->read(0, 0, reader->width(), reader->height());
        REQUIRE(data.is<mapnik::image_rgba8>() == true);
        TIFF_ASSERT_SIZE(data, reader);
        TIFF_ASSERT_ALPHA(data);
        TIFF_READ_ONE_PIXEL
    }

    SECTION("rgba8 tiled")
    {
        TIFF_ASSERT("./test/data/tiff/ndvi_256x256_rgba8_tiled.tif")
        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.photometric() == PHOTOMETRIC_RGB);
        REQUIRE(tiff_reader.compression() == COMPRESSION_LZW);
        mapnik::image_any data = reader->read(0, 0, reader->width(), reader->height());
        REQUIRE(data.is<mapnik::image_rgba8>() == true);
        TIFF_ASSERT_SIZE(data, reader);
        TIFF_ASSERT_ALPHA(data);
        TIFF_READ_ONE_PIXEL
    }

    SECTION("rgb8 striped")
    {
        TIFF_ASSERT("./test/data/tiff/ndvi_256x256_rgb8_striped.tif")
        REQUIRE(tiff_reader.rows_per_strip() == 10);
        REQUIRE(tiff_reader.bits_per_sample() == 8);
        REQUIRE(tiff_reader.is_tiled() == false);
        REQUIRE(tiff_reader.tile_width() == 0);
        REQUIRE(tiff_reader.tile_height() == 0);
        REQUIRE(tiff_reader.photometric() == PHOTOMETRIC_RGB);
        REQUIRE(tiff_reader.compression() == COMPRESSION_NONE);
        mapnik::image_any data = reader->read(0, 0, reader->width(), reader->height());
        REQUIRE(data.is<mapnik::image_rgba8>() == true);
        TIFF_ASSERT_SIZE(data, reader);
        TIFF_ASSERT_NO_ALPHA_RGB(data);
        TIFF_READ_ONE_PIXEL
    }

    SECTION("rgb8 tiled")
    {
        TIFF_ASSERT("./test/data/tiff/ndvi_256x256_rgb8_tiled.tif")
        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() == 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());
        REQUIRE(data.is<mapnik::image_rgba8>() == true);
        TIFF_ASSERT_SIZE(data, reader);
        TIFF_ASSERT_NO_ALPHA_RGB(data);
        TIFF_READ_ONE_PIXEL
    }

    SECTION("gray8 striped")
    {
        TIFF_ASSERT("./test/data/tiff/ndvi_256x256_gray8_striped.tif")
        REQUIRE(tiff_reader.rows_per_strip() == 32);
        REQUIRE(tiff_reader.bits_per_sample() == 8);
        REQUIRE(tiff_reader.is_tiled() == false);
        REQUIRE(tiff_reader.tile_width() == 0);
        REQUIRE(tiff_reader.tile_height() == 0);
        REQUIRE(tiff_reader.photometric() == PHOTOMETRIC_MINISBLACK);
        REQUIRE(tiff_reader.compression() == COMPRESSION_NONE);
        mapnik::image_any data = reader->read(0, 0, reader->width(), reader->height());
        REQUIRE(data.is<mapnik::image_gray8>() == true);
        TIFF_ASSERT_SIZE(data, reader);
        TIFF_ASSERT_NO_ALPHA_GRAY(data);
        TIFF_READ_ONE_PIXEL
    }

    SECTION("gray8 tiled")
    {
        TIFF_ASSERT("./test/data/tiff/ndvi_256x256_gray8_tiled.tif")
        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.photometric() == PHOTOMETRIC_MINISBLACK);
        REQUIRE(tiff_reader.compression() == COMPRESSION_LZW);
        mapnik::image_any data = reader->read(0, 0, reader->width(), reader->height());
        REQUIRE(data.is<mapnik::image_gray8>() == true);
        TIFF_ASSERT_SIZE(data, reader);
        TIFF_ASSERT_NO_ALPHA_GRAY(data);
        TIFF_READ_ONE_PIXEL
    }

    SECTION("gray16 striped")
    {
        TIFF_ASSERT("./test/data/tiff/ndvi_256x256_gray16_striped.tif")
        REQUIRE(tiff_reader.rows_per_strip() == 16);
        REQUIRE(tiff_reader.bits_per_sample() == 16);
        REQUIRE(tiff_reader.is_tiled() == false);
        REQUIRE(tiff_reader.tile_width() == 0);
        REQUIRE(tiff_reader.tile_height() == 0);
        REQUIRE(tiff_reader.photometric() == PHOTOMETRIC_MINISBLACK);
        REQUIRE(tiff_reader.compression() == COMPRESSION_NONE);
        mapnik::image_any data = reader->read(0, 0, reader->width(), reader->height());
        REQUIRE(data.is<mapnik::image_gray16>() == true);
        TIFF_ASSERT_SIZE(data, reader);
        TIFF_ASSERT_NO_ALPHA_GRAY(data);
        TIFF_READ_ONE_PIXEL
    }

    SECTION("gray16 tiled")
    {
        TIFF_ASSERT("./test/data/tiff/ndvi_256x256_gray16_tiled.tif")
        REQUIRE(tiff_reader.rows_per_strip() == 0);
        REQUIRE(tiff_reader.bits_per_sample() == 16);
        REQUIRE(tiff_reader.is_tiled() == true);
        REQUIRE(tiff_reader.tile_width() == 256);
        REQUIRE(tiff_reader.tile_height() == 256);
        REQUIRE(tiff_reader.photometric() == PHOTOMETRIC_MINISBLACK);
        REQUIRE(tiff_reader.compression() == COMPRESSION_LZW);
        mapnik::image_any data = reader->read(0, 0, reader->width(), reader->height());
        REQUIRE(data.is<mapnik::image_gray16>() == true);
        TIFF_ASSERT_SIZE(data, reader);
        TIFF_ASSERT_NO_ALPHA_GRAY(data);
        TIFF_READ_ONE_PIXEL
    }

    SECTION("gray32f striped")
    {
        TIFF_ASSERT("./test/data/tiff/ndvi_256x256_gray32f_striped.tif")
        REQUIRE(tiff_reader.rows_per_strip() == 8);
        REQUIRE(tiff_reader.bits_per_sample() == 32);
        REQUIRE(tiff_reader.is_tiled() == false);
        REQUIRE(tiff_reader.tile_width() == 0);
        REQUIRE(tiff_reader.tile_height() == 0);
        REQUIRE(tiff_reader.photometric() == PHOTOMETRIC_MINISBLACK);
        REQUIRE(tiff_reader.compression() == COMPRESSION_NONE);
        mapnik::image_any data = reader->read(0, 0, reader->width(), reader->height());
        REQUIRE(data.is<mapnik::image_gray32f>() == true);
        TIFF_ASSERT_SIZE(data, reader);
        TIFF_ASSERT_NO_ALPHA_GRAY(data);
        TIFF_READ_ONE_PIXEL
    }

    SECTION("gray32f tiled")
    {
        TIFF_ASSERT("./test/data/tiff/ndvi_256x256_gray32f_tiled.tif")
        REQUIRE(tiff_reader.rows_per_strip() == 0);
        REQUIRE(tiff_reader.bits_per_sample() == 32);
        REQUIRE(tiff_reader.is_tiled() == true);
        REQUIRE(tiff_reader.tile_width() == 256);
        REQUIRE(tiff_reader.tile_height() == 256);
        REQUIRE(tiff_reader.photometric() == PHOTOMETRIC_MINISBLACK);
        REQUIRE(tiff_reader.compression() == COMPRESSION_LZW);
        mapnik::image_any data = reader->read(0, 0, reader->width(), reader->height());
        REQUIRE(data.is<mapnik::image_gray32f>() == true);
        TIFF_ASSERT_SIZE(data, reader);
        TIFF_ASSERT_NO_ALPHA_GRAY(data);
        TIFF_READ_ONE_PIXEL
    }
}

#endif