OctreeQuantizer constructor

OctreeQuantizer(
  1. Image image, {
  2. int numberOfColors = 256,
})

Implementation

OctreeQuantizer(Image image, {int numberOfColors = 256})
    : _root = _OctreeNode(0, 0, null) {
  final heap = _HeapNode();
  for (final p in image) {
    final r = p.r as int;
    final g = p.g as int;
    final b = p.b as int;
    _heapAdd(heap, _nodeInsert(_root, r, g, b));
  }

  final nc = numberOfColors + 1;
  while (heap.n > nc) {
    _heapAdd(heap, _nodeFold(_popHeap(heap)!)!);
  }

  for (var i = 1; i < heap.n; i++) {
    final got = heap.buf[i]!;
    final c = got.count;
    got
      ..r = (got.r / c).round()
      ..g = (got.g / c).round()
      ..b = (got.b / c).round();
  }

  final nodes = <_OctreeNode>[];
  _getNodes(nodes, _root);

  palette = PaletteUint8(nodes.length, 3);
  final l = nodes.length;
  for (var i = 0; i < l; ++i) {
    final n = nodes[i]..paletteIndex = i;
    palette.setRgb(i, n.r, n.g, n.b);
  }
}