File: renderer.hpp

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 (424 lines) | stat: -rw-r--r-- 14,203 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
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
414
415
416
417
418
419
420
421
422
423
424
/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2015 Artem Pavlenko
 *
 * 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
 *
 *****************************************************************************/

#ifndef RENDERER_HPP
#define RENDERER_HPP

// stl
#include <sstream>
#include <iomanip>
#include <fstream>
#include <memory>

// mapnik
#include <mapnik/map.hpp>
#include <mapnik/image_util.hpp>
#include <mapnik/image_reader.hpp>
#include <mapnik/util/variant.hpp>
#include <mapnik/agg_renderer.hpp>
#if defined(GRID_RENDERER)
#include <mapnik/grid/grid_renderer.hpp>
#endif

#if defined(HAVE_CAIRO)
#include <mapnik/cairo/cairo_renderer.hpp>
#include <mapnik/cairo/cairo_image_util.hpp>
#ifdef CAIRO_HAS_SVG_SURFACE
#include <cairo-svg.h>
#endif
#ifdef CAIRO_HAS_PS_SURFACE
#include <cairo-ps.h>
#endif
#ifdef CAIRO_HAS_PDF_SURFACE
#include <cairo-pdf.h>
#endif
#endif

#if defined(SVG_RENDERER)
#include <mapnik/svg/output/svg_renderer.hpp>
#endif

// boost
#include <boost/filesystem.hpp>

namespace visual_tests
{

template <typename ImageType>
struct raster_renderer_base
{
    using image_type = ImageType;

    static constexpr const char * ext = ".png";
    static constexpr const bool support_tiles = true;

    unsigned compare(image_type const & actual, boost::filesystem::path const& reference) const
    {
        std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(reference.string(), "png"));
        if (!reader.get())
        {
            throw mapnik::image_reader_exception("Failed to load: " + reference.string());
        }

        mapnik::image_any ref_image_any = reader->read(0, 0, reader->width(), reader->height());
        ImageType const & reference_image = mapnik::util::get<ImageType>(ref_image_any);

        return mapnik::compare(actual, reference_image, 0, true);
    }

    void save(image_type const & image, boost::filesystem::path const& path) const
    {
        mapnik::save_to_file(image, path.string(), "png32");
    }
};

struct vector_renderer_base
{
    using image_type = std::string;

    static constexpr const bool support_tiles = false;

    unsigned compare(image_type const & actual, boost::filesystem::path const& reference) const
    {
        std::ifstream stream(reference.string().c_str(), std::ios_base::in | std::ios_base::binary);
        if (!stream)
        {
            throw std::runtime_error("Could not open: " + reference.string());
        }
        std::string expected(std::istreambuf_iterator<char>(stream.rdbuf()), std::istreambuf_iterator<char>());
        return std::max(actual.size(), expected.size()) - std::min(actual.size(), expected.size());
    }

    void save(image_type const & image, boost::filesystem::path const& path) const
    {
        std::ofstream file(path.string().c_str(), std::ios::out | std::ios::trunc | std::ios::binary);
        if (!file)
        {
            throw std::runtime_error("Cannot open file for writing: " + path.string());
        }
        file << image;
    }
};

struct agg_renderer : raster_renderer_base<mapnik::image_rgba8>
{
    static constexpr const char * name = "agg";

    image_type render(mapnik::Map const & map, double scale_factor) const
    {
        image_type image(map.width(), map.height());
        mapnik::agg_renderer<image_type> ren(map, image, scale_factor);
        ren.apply();
        return image;
    }
};

#if defined(HAVE_CAIRO)
struct cairo_renderer : raster_renderer_base<mapnik::image_rgba8>
{
    static constexpr const char * name = "cairo";

    image_type render(mapnik::Map const & map, double scale_factor) const
    {
        mapnik::cairo_surface_ptr image_surface(
            cairo_image_surface_create(CAIRO_FORMAT_ARGB32, map.width(), map.height()),
            mapnik::cairo_surface_closer());
        mapnik::cairo_ptr image_context(mapnik::create_context(image_surface));
        mapnik::cairo_renderer<mapnik::cairo_ptr> ren(map, image_context, scale_factor);
        ren.apply();
        image_type image(map.width(), map.height());
        mapnik::cairo_image_to_rgba8(image, image_surface);
        return image;
    }
};

using surface_create_type = cairo_surface_t *(&)(cairo_write_func_t, void *, double, double);

template <surface_create_type SurfaceCreateFunction>
struct cairo_vector_renderer : vector_renderer_base
{
    static cairo_status_t write(void *closure,
                                const unsigned char *data,
                                unsigned int length)
    {
        std::ostringstream & ss = *reinterpret_cast<std::ostringstream*>(closure);
        ss.write(reinterpret_cast<char const *>(data), length);
        return ss ? CAIRO_STATUS_SUCCESS : CAIRO_STATUS_WRITE_ERROR;
    }

    image_type render(mapnik::Map const & map, double scale_factor) const
    {
        std::ostringstream ss(std::stringstream::binary);
        mapnik::cairo_surface_ptr image_surface(
            SurfaceCreateFunction(write, &ss, map.width(), map.height()),
            mapnik::cairo_surface_closer());
        mapnik::cairo_ptr image_context(mapnik::create_context(image_surface));
        mapnik::cairo_renderer<mapnik::cairo_ptr> ren(map, image_context, scale_factor);
        ren.apply();
        cairo_surface_finish(&*image_surface);
        return ss.str();
    }
};

#ifdef CAIRO_HAS_SVG_SURFACE
struct cairo_svg_renderer : cairo_vector_renderer<cairo_svg_surface_create_for_stream>
{
    static constexpr const char * name = "cairo-svg";
    static constexpr const char * ext = ".svg";
};
#endif

#ifdef CAIRO_HAS_PS_SURFACE
struct cairo_ps_renderer : cairo_vector_renderer<cairo_ps_surface_create_for_stream>
{
    static constexpr const char * name = "cairo-ps";
    static constexpr const char * ext = ".ps";
};
#endif

#ifdef CAIRO_HAS_PDF_SURFACE
struct cairo_pdf_renderer : cairo_vector_renderer<cairo_pdf_surface_create_for_stream>
{
    static constexpr const char * name = "cairo-pdf";
    static constexpr const char * ext = ".pdf";
};
#endif
#endif

#if defined(SVG_RENDERER)
struct svg_renderer : vector_renderer_base
{
    static constexpr const char * name = "svg";
    static constexpr const char * ext = ".svg";

    image_type render(mapnik::Map const & map, double scale_factor) const
    {
        std::stringstream ss;
        std::ostream_iterator<char> output_stream_iterator(ss);
        mapnik::svg_renderer<std::ostream_iterator<char>> ren(map, output_stream_iterator, scale_factor);
        ren.apply();
        return ss.str();
    }
};
#endif

#if defined(GRID_RENDERER)
struct grid_renderer : raster_renderer_base<mapnik::image_rgba8>
{
    static constexpr const char * name = "grid";

    void convert(mapnik::grid::data_type const & grid, image_type & image) const
    {
        for (std::size_t y = 0; y < grid.height(); ++y)
        {
            mapnik::grid::value_type const * grid_row = grid.get_row(y);
            image_type::pixel_type * image_row = image.get_row(y);
            for (std::size_t x = 0; x < grid.width(); ++x)
            {
                mapnik::grid::value_type val = grid_row[x];

                if (val == mapnik::grid::base_mask)
                {
                    image_row[x] = 0;
                    continue;
                }
                if (val < 0)
                {
                    throw std::runtime_error("grid renderer: feature id is negative.");
                }

                val *= 100;

                if (val > 0x00ffffff)
                {
                    throw std::runtime_error("grid renderer: feature id is too high.");
                }

                image_row[x] = val | 0xff000000;
            }
        }
    }

    image_type render(mapnik::Map const & map, double scale_factor) const
    {
        mapnik::grid grid(map.width(), map.height(), "__id__");
        mapnik::grid_renderer<mapnik::grid> ren(map, grid, scale_factor);
        ren.apply();
        image_type image(map.width(), map.height());
        convert(grid.data(), image);
        return image;
    }
};
#endif

template <typename T>
void set_rectangle(T const & src, T & dst, std::size_t x, std::size_t y)
{
    mapnik::box2d<int> ext0(0, 0, dst.width(), dst.height());
    mapnik::box2d<int> ext1(x, y, x + src.width(), y + src.height());

    if (ext0.intersects(ext1))
    {
        mapnik::box2d<int> box = ext0.intersect(ext1);
        for (std::size_t pix_y = box.miny(); pix_y < static_cast<std::size_t>(box.maxy()); ++pix_y)
        {
            typename T::pixel_type * row_to =  dst.get_row(pix_y);
            typename T::pixel_type const * row_from = src.get_row(pix_y - y);

            for (std::size_t pix_x = box.minx(); pix_x < static_cast<std::size_t>(box.maxx()); ++pix_x)
            {
                row_to[pix_x] = row_from[pix_x - x];
            }
        }
    }
}

template <typename Renderer>
class renderer
{
public:
    using renderer_type = Renderer;
    using image_type = typename Renderer::image_type;

    renderer(boost::filesystem::path const & _output_dir, boost::filesystem::path const & _reference_dir, bool _overwrite)
        : ren(), output_dir(_output_dir), reference_dir(_reference_dir), overwrite(_overwrite)
    {
    }

    image_type render(mapnik::Map const & map, double scale_factor) const
    {
        return ren.render(map, scale_factor);
    }

    image_type render(mapnik::Map & map, double scale_factor, map_size const & tiles) const
    {
        mapnik::box2d<double> box = map.get_current_extent();
        image_type image(map.width(), map.height());
        map.resize(image.width() / tiles.width, image.height() / tiles.height);
        double tile_box_width = box.width() / tiles.width;
        double tile_box_height = box.height() / tiles.height;
        for (std::size_t tile_y = 0; tile_y < tiles.height; tile_y++)
        {
            for (std::size_t tile_x = 0; tile_x < tiles.width; tile_x++)
            {
                mapnik::box2d<double> tile_box(
                    box.minx() + tile_x * tile_box_width,
                    box.miny() + tile_y * tile_box_height,
                    box.minx() + (tile_x + 1) * tile_box_width,
                    box.miny() + (tile_y + 1) * tile_box_height);
                map.zoom_to_box(tile_box);
                image_type tile(ren.render(map, scale_factor));
                set_rectangle(tile, image, tile_x * tile.width(), (tiles.height - 1 - tile_y) * tile.height());
            }
        }
        return image;
    }

    result report(image_type const & image,
                  std::string const & name,
                  map_size const & size,
                  map_size const & tiles,
                  double scale_factor) const
    {
        boost::filesystem::path reference = reference_dir / image_file_name(name, size, tiles, scale_factor, true);
        bool reference_exists = boost::filesystem::exists(reference);
        result res;

        res.state = reference_exists ? STATE_OK : STATE_OVERWRITE;
        res.name = name;
        res.renderer_name = Renderer::name;
        res.scale_factor = scale_factor;
        res.size = size;
        res.tiles = tiles;
        res.reference_image_path = reference;
        res.diff = reference_exists ? ren.compare(image, reference) : 0;

        if (res.diff)
        {
            boost::filesystem::create_directories(output_dir);
            boost::filesystem::path path = output_dir / image_file_name(name, size, tiles, scale_factor, false);
            res.actual_image_path = path;
            res.state = STATE_FAIL;
            ren.save(image, path);
        }

        if ((res.diff && overwrite) || !reference_exists)
        {
            ren.save(image, reference);
            res.state = STATE_OVERWRITE;
        }

        return res;
    }

private:
    std::string image_file_name(std::string const & test_name,
                                map_size const & size,
                                map_size const & tiles,
                                double scale_factor,
                                bool reference) const
    {
        std::stringstream s;
        s << test_name << '-' << size.width << '-' << size.height << '-';
        if (tiles.width > 1 || tiles.height > 1)
        {
            s << tiles.width << 'x' << tiles.height << '-';
        }
        s << std::fixed << std::setprecision(1) << scale_factor << '-' << Renderer::name;
        if (reference)
        {
            s << "-reference";
        }
        s << Renderer::ext;
        return s.str();
    }

    const Renderer ren;
    const boost::filesystem::path output_dir;
    const boost::filesystem::path reference_dir;
    const bool overwrite;
};

using renderer_type = mapnik::util::variant<renderer<agg_renderer>
#if defined(HAVE_CAIRO)
                                            ,renderer<cairo_renderer>
#ifdef CAIRO_HAS_SVG_SURFACE
                                            ,renderer<cairo_svg_renderer>
#endif
#ifdef CAIRO_HAS_PS_SURFACE
                                            ,renderer<cairo_ps_renderer>
#endif
#ifdef CAIRO_HAS_PDF_SURFACE
                                            ,renderer<cairo_pdf_renderer>
#endif
#endif
#if defined(SVG_RENDERER)
                                            ,renderer<svg_renderer>
#endif
#if defined(GRID_RENDERER)
                                            ,renderer<grid_renderer>
#endif
                                            >;

}

#endif