All files / png / encode / _passExtraction.ts

100.00% Branches 5/5
100.00% Lines 34/34
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
x6
 
 
x6
x6
x6
x6
 
x50
x107
x57
x57
x57
x57
x57
x57
x57
x57
x980
x980
x980
x980
x1851
x1851
x1851
x1851
x1851
x1851
x1851
x980
x980
x57
x57
x50
x87
x50
x50



































import { images, moveTo } from "../_common_pass.ts";
import type { PNGOptions } from "../types.ts";

export function passExtraction(
  input: Uint8Array,
  pSize: number,
  options: PNGOptions,
): [number, number][] {
  switch (options.interlace) {
    case 1: {
      const sizes = images(options);
      const offsets: number[] = new Array(sizes.length)
        .fill(0)
        .map((_, i) => sizes.slice(0, i).reduce((x, y) => x + y[0] * y[1], 0));
      const hasMoved: boolean[] = new Array(options.width * options.height)
        .fill(false);
      const tempPixel = new Uint8Array(pSize);
      for (let i = 0; i < hasMoved.length; ++i) {
        let j = i;
        while (!hasMoved[i]) {
          const k = moveTo(j, offsets, options);
          if (j === k) hasMoved[i] = true;
          else {
            tempPixel.set(input.subarray(k * pSize, k * pSize + pSize));
            input.set(input.subarray(i * pSize, i * pSize + pSize), k * pSize);
            input.set(tempPixel, i * pSize);
            hasMoved[k] = true;
            j = k;
          }
        }
      }
      return sizes;
    }
    default: // 0
      return images(options);
  }
}