This file is indexed.

/usr/include/mapnik/tiff_io.hpp is in libmapnik-dev 3.0.19+ds-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*****************************************************************************
 *
 * 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 MAPNIK_TIFF_IO_HPP
#define MAPNIK_TIFF_IO_HPP

#include <mapnik/global.hpp>
#include <mapnik/image_util.hpp>
#include <mapnik/image_any.hpp>
#include <mapnik/util/variant.hpp>

#pragma GCC diagnostic push
#include <mapnik/warning_ignore.hpp>

extern "C"
{
#include <tiffio.h>
#define RealTIFFOpen TIFFClientOpen
#define RealTIFFClose TIFFClose
}

#pragma GCC diagnostic pop


//std
#include <memory>

#define TIFF_WRITE_SCANLINE 0
#define TIFF_WRITE_STRIPPED 1
#define TIFF_WRITE_TILED 2

namespace mapnik {

static inline tsize_t tiff_write_proc(thandle_t fd, tdata_t buf, tsize_t size)
{
    std::ostream* out = reinterpret_cast<std::ostream*>(fd);
    std::ios::pos_type pos = out->tellp();
    std::streamsize request_size = size;
    if (static_cast<tsize_t>(request_size) != size)
        return static_cast<tsize_t>(-1);
    out->write(reinterpret_cast<const char*>(buf), size);

    if( static_cast<std::streamsize>(pos) == -1 )
    {
        return size;
    }
    else
    {
        return static_cast<tsize_t>(out->tellp()-pos);
    }
}

static inline toff_t tiff_seek_proc(thandle_t fd, toff_t off, int whence)
{
    std::ostream* out = reinterpret_cast<std::ostream*>(fd);

    if( out->fail() )
        return static_cast<toff_t>(-1);

    if( static_cast<std::streamsize>(out->tellp()) == -1)
        return static_cast< toff_t >( 0 );

    switch(whence)
    {
    case SEEK_SET:
        out->seekp(off, std::ios_base::beg);
        break;
    case SEEK_CUR:
        out->seekp(off, std::ios_base::cur);
        break;
    case SEEK_END:
        out->seekp(off, std::ios_base::end);
        break;
    }
    // grow std::stringstream buffer (re: libtiff/tif_stream.cxx)
    std::ios::pos_type pos = out->tellp();
    // second check needed for clang (libcxx doesn't set failbit when seeking beyond the current buffer size

    if( out->fail() || static_cast<std::streamoff>(off) != pos)
    {
        std::ios::iostate old_state;
        std::ios::pos_type  origin;
        old_state = out->rdstate();
        // reset the fail bit or else tellp() won't work below
        out->clear(out->rdstate() & ~std::ios::failbit);
        switch( whence )
        {
        case SEEK_SET:
        default:
            origin = 0L;
            break;
        case SEEK_CUR:
            origin = out->tellp();
            break;
        case SEEK_END:
            out->seekp(0, std::ios::end);
            origin = out->tellp();
            break;
        }
        // restore original stream state
        out->clear(old_state);

        // only do something if desired seek position is valid
        if( (static_cast<uint64_t>(origin) + off) > 0L)
        {
            uint64_t num_fill;
            // clear the fail bit
            out->clear(out->rdstate() & ~std::ios::failbit);
            // extend the stream to the expected size
            out->seekp(0, std::ios::end);
            num_fill = (static_cast<uint64_t>(origin)) + off - out->tellp();
            for( uint64_t i = 0; i < num_fill; ++i)
                out->put('\0');

            // retry the seek
            out->seekp(static_cast<std::ios::off_type>(static_cast<uint64_t>(origin) + off), std::ios::beg);
        }
    }
    return static_cast<toff_t>(out->tellp());
}

static inline int tiff_close_proc(thandle_t fd)
{
    std::ostream* out = (std::ostream*)fd;
    out->flush();
    return 0;
}

static inline toff_t tiff_size_proc(thandle_t fd)
{
    std::ostream* out = reinterpret_cast<std::ostream*>(fd);
    std::ios::pos_type pos = out->tellp();
    out->seekp(0, std::ios::end);
    std::ios::pos_type len = out->tellp();
    out->seekp(pos);
    return static_cast<toff_t>(len);
}

static inline tsize_t tiff_dummy_read_proc(thandle_t , tdata_t , tsize_t)
{
    return 0;
}

static inline void tiff_dummy_unmap_proc(thandle_t , tdata_t , toff_t) {}

static inline int tiff_dummy_map_proc(thandle_t , tdata_t*, toff_t* )
{
    return 0;
}

struct tiff_config
{
    tiff_config()
        : compression(COMPRESSION_ADOBE_DEFLATE),
        zlevel(4),
        tile_width(0),
        tile_height(0),
        rows_per_strip(0),
        method(TIFF_WRITE_STRIPPED) {}

    int compression;
    int zlevel;
    int tile_width; // Tile width of zero means tile the width of the image
    int tile_height; // Tile height of zero means tile the height of the image
    int rows_per_strip;
    int method; // The method to use to write the TIFF.

};

struct tag_setter
{
    tag_setter(TIFF * output, tiff_config const& config)
        : output_(output),
          config_(config) {}

    template <typename T>
    void operator() (T const&) const
    {
        // Assume this would be null type
        throw image_writer_exception("Could not write TIFF - unknown image type provided");
    }

    inline void operator() (image_rgba8 const& data) const
    {
        TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
        TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
        TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 8);
        TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 4);
        if (data.get_premultiplied())
        {
            uint16 extras[] = { EXTRASAMPLE_ASSOCALPHA };
            TIFFSetField(output_, TIFFTAG_EXTRASAMPLES, 1, extras);
        }
        else
        {
            uint16 extras[] = { EXTRASAMPLE_UNASSALPHA };
            TIFFSetField(output_, TIFFTAG_EXTRASAMPLES, 1, extras);
        }
        if (config_.compression == COMPRESSION_DEFLATE
                || config_.compression == COMPRESSION_ADOBE_DEFLATE
                || config_.compression == COMPRESSION_LZW)
        {
            TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);

        }
    }
    inline void operator() (image_gray64 const&) const
    {
        TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
        TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
        TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 64);
        TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
        if (config_.compression == COMPRESSION_DEFLATE
                || config_.compression == COMPRESSION_ADOBE_DEFLATE
                || config_.compression == COMPRESSION_LZW)
        {
            TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);

        }
    }
    inline void operator() (image_gray64s const&) const
    {
        TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
        TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
        TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 64);
        TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
        if (config_.compression == COMPRESSION_DEFLATE
                || config_.compression == COMPRESSION_ADOBE_DEFLATE
                || config_.compression == COMPRESSION_LZW)
        {
            TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);

        }
    }
    inline void operator() (image_gray64f const&) const
    {
        TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
        TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
        TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 64);
        TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
        if (config_.compression == COMPRESSION_DEFLATE
                || config_.compression == COMPRESSION_ADOBE_DEFLATE
                || config_.compression == COMPRESSION_LZW)
        {
            TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_FLOATINGPOINT);
        }
    }
    inline void operator() (image_gray32 const&) const
    {
        TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
        TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
        TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 32);
        TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
        if (config_.compression == COMPRESSION_DEFLATE
                || config_.compression == COMPRESSION_ADOBE_DEFLATE
                || config_.compression == COMPRESSION_LZW)
        {
            TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);

        }
    }
    inline void operator() (image_gray32s const&) const
    {
        TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
        TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
        TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 32);
        TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
        if (config_.compression == COMPRESSION_DEFLATE
                || config_.compression == COMPRESSION_ADOBE_DEFLATE
                || config_.compression == COMPRESSION_LZW)
        {
            TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);

        }
    }
    inline void operator() (image_gray32f const&) const
    {
        TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
        TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
        TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 32);
        TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
        if (config_.compression == COMPRESSION_DEFLATE
                || config_.compression == COMPRESSION_ADOBE_DEFLATE
                || config_.compression == COMPRESSION_LZW)
        {
            TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_FLOATINGPOINT);
        }
    }
    inline void operator() (image_gray16 const&) const
    {
        TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
        TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
        TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 16);
        TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
        if (config_.compression == COMPRESSION_DEFLATE
                || config_.compression == COMPRESSION_ADOBE_DEFLATE
                || config_.compression == COMPRESSION_LZW)
        {
            TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);

        }
    }
    inline void operator() (image_gray16s const&) const
    {
        TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
        TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
        TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 16);
        TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
        if (config_.compression == COMPRESSION_DEFLATE
                || config_.compression == COMPRESSION_ADOBE_DEFLATE
                || config_.compression == COMPRESSION_LZW)
        {
            TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);

        }
    }
    inline void operator() (image_gray8 const&) const
    {
        TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
        TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
        TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 8);
        TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
        if (config_.compression == COMPRESSION_DEFLATE
                || config_.compression == COMPRESSION_ADOBE_DEFLATE
                || config_.compression == COMPRESSION_LZW)
        {
            TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);

        }
    }
    inline void operator() (image_gray8s const&) const
    {
        TIFFSetField(output_, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
        TIFFSetField(output_, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
        TIFFSetField(output_, TIFFTAG_BITSPERSAMPLE, 8);
        TIFFSetField(output_, TIFFTAG_SAMPLESPERPIXEL, 1);
        if (config_.compression == COMPRESSION_DEFLATE
                || config_.compression == COMPRESSION_ADOBE_DEFLATE
                || config_.compression == COMPRESSION_LZW)
        {
            TIFFSetField(output_, TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);

        }
    }
    inline void operator() (image_null const&) const
    {
        // Assume this would be null type
        throw image_writer_exception("Could not write TIFF - Null image provided");
    }

    private:
        TIFF * output_;
        tiff_config const& config_;
};

inline void set_tiff_config(TIFF* output, tiff_config const& config)
{
    // Set some constant tiff information that doesn't vary based on type of data
    // or image size
    TIFFSetField(output, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);

    // Set the compression for the TIFF
    TIFFSetField(output, TIFFTAG_COMPRESSION, config.compression);

    if (COMPRESSION_ADOBE_DEFLATE == config.compression
        || COMPRESSION_DEFLATE == config.compression
        || COMPRESSION_LZW == config.compression)
    {
        // Set the zip level for the compression
        // http://en.wikipedia.org/wiki/DEFLATE#Encoder.2Fcompressor
        // Changes the time spent trying to compress
        TIFFSetField(output, TIFFTAG_ZIPQUALITY, config.zlevel);
    }
}

template <typename T1, typename T2>
void save_as_tiff(T1 & file, T2 const& image, tiff_config const& config)
{
    using pixel_type = typename T2::pixel_type;

    const int width = image.width();
    const int height = image.height();

    TIFF* output = RealTIFFOpen("mapnik_tiff_stream",
                                "wm",
                                (thandle_t)&file,
                                tiff_dummy_read_proc,
                                tiff_write_proc,
                                tiff_seek_proc,
                                tiff_close_proc,
                                tiff_size_proc,
                                tiff_dummy_map_proc,
                                tiff_dummy_unmap_proc);
    if (! output)
    {
        throw image_writer_exception("Could not write TIFF");
    }

    TIFFSetField(output, TIFFTAG_IMAGEWIDTH, width);
    TIFFSetField(output, TIFFTAG_IMAGELENGTH, height);
    TIFFSetField(output, TIFFTAG_IMAGEDEPTH, 1);
    set_tiff_config(output, config);

    // Set tags that vary based on the type of data being provided.
    tag_setter set(output, config);
    set(image);

    // Use specific types of writing methods.
    if (TIFF_WRITE_SCANLINE == config.method)
    {
        // Process Scanline
        TIFFSetField(output, TIFFTAG_ROWSPERSTRIP, 1);

        int next_scanline = 0;
        std::unique_ptr<pixel_type[]> row (new pixel_type[width]);
        while (next_scanline < height)
        {
            std::copy(image.get_row(next_scanline), image.get_row(next_scanline) + width, row.get());
            TIFFWriteScanline(output, row.get(), next_scanline, 0);
            ++next_scanline;
        }
    }
    else if (TIFF_WRITE_STRIPPED == config.method)
    {
        std::size_t rows_per_strip = config.rows_per_strip;
        if (0 == rows_per_strip)
        {
            rows_per_strip = height;
        }
        TIFFSetField(output, TIFFTAG_ROWSPERSTRIP, rows_per_strip);
        std::size_t strip_size = width * rows_per_strip;
        std::unique_ptr<pixel_type[]> strip_buffer(new pixel_type[strip_size]);
        for (int y=0; y < height; y+=rows_per_strip)
        {
            int ty1 = std::min(height, static_cast<int>(y + rows_per_strip)) - y;
            int row = y;
            for (int ty = 0; ty < ty1; ++ty, ++row)
            {
                std::copy(image.get_row(row), image.get_row(row) + width, strip_buffer.get() + ty * width);
            }
            if (TIFFWriteEncodedStrip(output, TIFFComputeStrip(output, y, 0), strip_buffer.get(), strip_size * sizeof(pixel_type)) == -1)
            {
                throw image_writer_exception("Could not write TIFF - TIFF Tile Write failed");
            }
        }
    }
    else if (TIFF_WRITE_TILED == config.method)
    {
        int tile_width = config.tile_width;
        int tile_height = config.tile_height;

        if (0 == tile_height)
        {
            tile_height = height;
            if (height % 16 > 0)
            {
                tile_height = height + 16 - (height % 16);
            }
        }
        if (0 == tile_width)
        {
            tile_width = width;
            if (width % 16 > 0)
            {
                tile_width = width + 16 - (width % 16);
            }
        }
        TIFFSetField(output, TIFFTAG_TILEWIDTH, tile_width);
        TIFFSetField(output, TIFFTAG_TILELENGTH, tile_height);
        TIFFSetField(output, TIFFTAG_TILEDEPTH, 1);
        std::size_t tile_size = tile_width * tile_height;
        std::unique_ptr<pixel_type[]> image_out (new pixel_type[tile_size]);
        int end_y = (height / tile_height + 1) * tile_height;
        int end_x = (width / tile_width + 1) * tile_width;
        end_y = std::min(end_y, height);
        end_x = std::min(end_x, width);

        for (int y = 0; y < end_y; y += tile_height)
        {
            int ty1 = std::min(height, y + tile_height) - y;

            for (int x = 0; x < end_x; x += tile_width)
            {
                // Prefill the entire array with zeros.
                std::fill(image_out.get(), image_out.get() + tile_size, 0);
                int tx1 = std::min(width, x + tile_width);
                int row = y;
                for (int ty = 0; ty < ty1; ++ty, ++row)
                {
                    std::copy(image.get_row(row, x), image.get_row(row, tx1), image_out.get() + ty * tile_width);
                }
                if (TIFFWriteEncodedTile(output, TIFFComputeTile(output, x, y, 0, 0), image_out.get(), tile_size * sizeof(pixel_type)) == -1)
                {
                    throw image_writer_exception("Could not write TIFF - TIFF Tile Write failed");
                }
            }
        }
    }
    // TODO - handle palette images
    // std::vector<mapnik::rgb> const& palette

    //  unsigned short r[256], g[256], b[256];
    //  for (int i = 0; i < (1 << 24); ++i)
    //  {
    //  r[i] = (unsigned short)palette[i * 3 + 0] << 8;
    //  g[i] = (unsigned short)palette[i * 3 + 1] << 8;
    //  b[i] = (unsigned short)palette[i * 3 + 2] << 8;
    //  }
    //  TIFFSetField(output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
    //  TIFFSetField(output, TIFFTAG_COLORMAP, r, g, b);

    RealTIFFClose(output);
}

}

#endif // MAPNIK_TIFF_IO_HPP