var l = (c, n, o) => new Promise((r, a) => {
  var h = (t) => {
    try {
      e(o.next(t));
    } catch (i) {
      a(i);
    }
  }, s = (t) => {
    try {
      e(o.throw(t));
    } catch (i) {
      a(i);
    }
  }, e = (t) => t.done ? r(t.value) : Promise.resolve(t.value).then(h, s);
  e((o = o.apply(c, n)).next());
});
function d(c, n) {
  let o = c.length - n, r = 0;
  do {
    for (let a = n; a > 0; a--)
      c[r + n] += c[r], r++;
    o -= n;
  } while (o > 0);
}
function f(c, n, o) {
  let r = 0, a = c.length;
  const h = a / o;
  for (; a > n; ) {
    for (let e = n; e > 0; --e)
      c[r + n] += c[r], ++r;
    a -= n;
  }
  const s = c.slice();
  for (let e = 0; e < h; ++e)
    for (let t = 0; t < o; ++t)
      c[o * e + t] = s[(o - t - 1) * h + e];
}
function g(c, n, o, r, a, h) {
  if (!n || n === 1)
    return c;
  for (let t = 0; t < a.length; ++t) {
    if (a[t] % 8 !== 0)
      throw new Error("When decoding with predictor, only multiple of 8 bits are supported.");
    if (a[t] !== a[0])
      throw new Error("When decoding with predictor, all samples must have the same size.");
  }
  const s = a[0] / 8, e = h === 2 ? 1 : a.length;
  for (let t = 0; t < r && !(t * e * o * s >= c.byteLength); ++t) {
    let i;
    if (n === 2) {
      switch (a[0]) {
        case 8:
          i = new Uint8Array(
            c,
            t * e * o * s,
            e * o * s
          );
          break;
        case 16:
          i = new Uint16Array(
            c,
            t * e * o * s,
            e * o * s / 2
          );
          break;
        case 32:
          i = new Uint32Array(
            c,
            t * e * o * s,
            e * o * s / 4
          );
          break;
        default:
          throw new Error(`Predictor 2 not allowed with ${a[0]} bits per sample.`);
      }
      d(i, e);
    } else
      n === 3 && (i = new Uint8Array(
        c,
        t * e * o * s,
        e * o * s
      ), f(i, e, s));
  }
  return c;
}
class w {
  decode(n, o) {
    return l(this, null, function* () {
      const r = yield this.decodeBlock(o), a = n.Predictor || 1;
      if (a !== 1) {
        const h = !n.StripOffsets, s = h ? n.TileWidth : n.ImageWidth, e = h ? n.TileLength : n.RowsPerStrip || n.ImageLength;
        return g(
          r,
          a,
          s,
          e,
          n.BitsPerSample,
          n.PlanarConfiguration
        );
      }
      return r;
    });
  }
}
export {
  w as B
};
