This file is indexed.

/usr/include/mapnik/octree.hpp is in libmapnik-dev 3.0.9+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
/*****************************************************************************
 *
 * 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_OCTREE_HPP
#define MAPNIK_OCTREE_HPP

// mapnik
#include <mapnik/global.hpp>
#include <mapnik/palette.hpp>
#include <mapnik/util/noncopyable.hpp>

// stl
#include <algorithm>
#include <vector>
#include <deque>

namespace mapnik {

struct RGBPolicy
{
    const static unsigned MAX_LEVELS = 6;
    const static unsigned MIN_LEVELS = 2;
    inline static unsigned index_from_level(unsigned level, rgb const& c)
    {
        unsigned shift = 7 - level;
        return (((c.r >> shift) & 1) << 2)
            | (((c.g >> shift) & 1) << 1)
            | ((c.b >> shift) & 1);
    }
};

template <typename T, typename InsertPolicy = RGBPolicy >
class octree : private util::noncopyable
{
    struct node
    {
        node()
            :  reds(0),
               greens(0),
               blues(0),
               count(0),
               count_cum(0),
               children_count(0),
               index(0)
        {
            std::fill(children_,children_ + 8, nullptr);
        }

        ~node()
        {
            for (unsigned i = 0;i < 8; ++i)
            {
                if (children_[i] != 0)
                {
                    delete children_[i];
                    children_[i]=0;
                }
            }
        }

        bool is_leaf() const
        {
            return count == 0;
        }
        node * children_[8];
        std::uint64_t reds;
        std::uint64_t greens;
        std::uint64_t blues;
        unsigned count;
        double reduce_cost;
        unsigned count_cum;
        std::uint8_t children_count;
        std::uint8_t index;
    };
    struct node_cmp
    {
        bool operator() ( const node * lhs,const node* rhs) const
        {
            return lhs->reduce_cost < rhs->reduce_cost;
        }
    };

    std::deque<node*> reducible_[InsertPolicy::MAX_LEVELS];
    unsigned max_colors_;
    unsigned colors_;
    unsigned offset_;
    unsigned leaf_level_;

public:
    explicit octree(unsigned max_colors=256)
        : max_colors_(max_colors),
          colors_(0),
          offset_(0),
          leaf_level_(InsertPolicy::MAX_LEVELS),
          root_(new node())
    {}

    ~octree()
    {
        delete root_;
    }

    unsigned colors()
    {
        return colors_;
    }

    void setMaxColors(unsigned max_colors)
    {
        max_colors_ = max_colors;
    }

    void setOffset(unsigned offset)
    {
        offset_ = offset;
    }

    unsigned getOffset()
    {
        return offset_;
    }

    void insert(T const& data)
    {
        unsigned level = 0;
        node * cur_node = root_;
        while (true)
        {
            cur_node->count_cum++;
            cur_node->reds   += data.r;
            cur_node->greens += data.g;
            cur_node->blues  += data.b;

            if ( cur_node->count > 0 || level == leaf_level_)
            {
                cur_node->count  += 1;
                if (cur_node->count == 1) ++colors_;
                //if (colors_ >= max_colors_ - 1)
                //reduce();
                break;
            }

            unsigned idx = InsertPolicy::index_from_level(level,data);
            if (cur_node->children_[idx] == 0)
            {
                cur_node->children_count++;
                cur_node->children_[idx] = new node();
                if (level < leaf_level_-1)
                {
                    reducible_[level+1].push_back(cur_node->children_[idx]);
                }
            }
            cur_node = cur_node->children_[idx];
            ++level;
        }
    }

    int quantize(unsigned val) const
    {
        unsigned level = 0;
        rgb c(val);
        node * cur_node = root_;
        while (cur_node)
        {
            if (cur_node->children_count == 0)
            {
                return cur_node->index + offset_;
            }
            unsigned idx = InsertPolicy::index_from_level(level,c);
            cur_node = cur_node->children_[idx];
            ++level;
        }
        return -1;
    }

    void create_palette(std::vector<rgb> & palette)
    {
        reduce();
        palette.reserve(colors_);
        create_palette(palette, root_);
    }

    void computeCost(node *r)
    {
        r->reduce_cost = 0;
        if (r->children_count==0)
        {
            return;
        }

        double mean_r = static_cast<double>(r->reds) / r->count_cum;
        double mean_g = static_cast<double>(r->greens) / r->count_cum;
        double mean_b = static_cast<double>(r->blues) / r->count_cum;
        for (unsigned idx=0; idx < 8; ++idx)
        {
            if (r->children_[idx] != 0)
            {
                double dr,dg,db;
                computeCost(r->children_[idx]);
                dr = r->children_[idx]->reds   / r->children_[idx]->count_cum - mean_r;
                dg = r->children_[idx]->greens / r->children_[idx]->count_cum - mean_g;
                db = r->children_[idx]->blues  / r->children_[idx]->count_cum - mean_b;
                r->reduce_cost += r->children_[idx]->reduce_cost;
                r->reduce_cost += (dr*dr + dg*dg + db*db) * r->children_[idx]->count_cum;
            }
        }
    }

    void reduce()
    {
        computeCost(root_);
        reducible_[0].push_back(root_);

        // sort reducible by reduce_cost
        for (unsigned i=0;i<InsertPolicy::MAX_LEVELS;++i)
        {
            std::sort(reducible_[i].begin(), reducible_[i].end(),node_cmp());
        }
        while (colors_ > max_colors_ && colors_ > 1)
        {
            while (leaf_level_ >0  && reducible_[leaf_level_-1].size() == 0)
            {
                --leaf_level_;
            }

            if (leaf_level_ <= 0)
            {
                return;
            }

            // select best of all reducible:
            unsigned red_idx = leaf_level_-1;
            unsigned bestv = static_cast<unsigned>((*reducible_[red_idx].begin())->reduce_cost);
            for(unsigned i=red_idx; i>=InsertPolicy::MIN_LEVELS; i--)
            {
                if (!reducible_[i].empty())
                {
                    node *nd = *reducible_[i].begin();
                    unsigned gch = 0;
                    for(unsigned idx=0; idx<8; idx++)
                    {
                        if (nd->children_[idx])
                            gch += nd->children_[idx]->children_count;
                    }
                    if (gch==0 && nd->reduce_cost < bestv)
                    {
                        bestv = static_cast<unsigned>(nd->reduce_cost);
                        red_idx = i;
                    }
                }
            }

            typename std::deque<node*>::iterator pos = reducible_[red_idx].begin();
            node * cur_node = *pos;
            unsigned num_children = 0;
            for (unsigned idx=0; idx < 8; ++idx)
            {
                if (cur_node->children_[idx] != 0)
                {
                    cur_node->children_count--;
                    ++num_children;
                    cur_node->count  += cur_node->children_[idx]->count;
                    //todo: case of nonleaf children, if someday sorting by reduce_cost doesn't handle it
                    delete cur_node->children_[idx], cur_node->children_[idx]=0;
                }
            }

            reducible_[red_idx].erase(pos);
            if (num_children > 0 )
            {
                colors_ -= (num_children - 1);
            }
        }
    }

    void create_palette(std::vector<rgb> & palette, node * itr) const
    {
        if (itr->count != 0)
        {
            unsigned count = itr->count;
            palette.push_back(rgb(std::uint8_t(itr->reds/float(count)),
                                  std::uint8_t(itr->greens/float(count)),
                                  std::uint8_t(itr->blues/float(count))));
            itr->index = static_cast<unsigned>(palette.size()) - 1;
        }
        for (unsigned i=0; i < 8 ;++i)
        {
            if (itr->children_[i] != 0)
            {
                create_palette(palette, itr->children_[i]);
            }
        }
    }
private:
    node * root_;
};

} // namespace mapnik

#endif // MAPNIK_OCTREE_HPP