All files / png / decode / _passExtraction.ts

100.00% Branches 4/4
100.00% Lines 30/30
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
x2
 
 
x2
x2
x2
x2
x2
x2
 
x11
x26
x15
x15
x15
x15
x15
x605
x605
x605
x605
 
x605
 
x15
x15
x15
x15
x27
x16
x16
x16
x16
x11
x11

































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

export function passExtraction(
  output: Uint8Array,
  mid: Uint8Array,
  options: PNGOptions,
  pSize: number,
  sizes: [number, number][],
): number {
  switch (options.interlace) {
    case 1: {
      const offsets: number[] = new Array(sizes.length)
        .fill(0)
        .map((_, i) => sizes.slice(0, i).reduce((x, y) => x + y[0] * y[1], 0));
      let p = 0;
      for (let i = 0; i < mid.length; i += pSize, ++p) {
        const x = moveTo(p, offsets, options) * pSize;
        output.set(
          mid.subarray(x, x + pSize),
          p * pSize,
        );
      }

      const offset = output.length - mid.length;
      if (offset) output.set(output.subarray(0, mid.length), offset);
      return offset;
    }
    default: {
      const offset = output.length - mid.length;
      output.set(mid, offset);
      return offset;
    }
  }
}