import { t as e } from "./basedecoder-BFm2CpEb.js";
//#region ../../node_modules/geotiff/dist-module/compression/jpeg.js
var t = new Int32Array([
	0,
	1,
	8,
	16,
	9,
	2,
	3,
	10,
	17,
	24,
	32,
	25,
	18,
	11,
	4,
	5,
	12,
	19,
	26,
	33,
	40,
	48,
	41,
	34,
	27,
	20,
	13,
	6,
	7,
	14,
	21,
	28,
	35,
	42,
	49,
	56,
	57,
	50,
	43,
	36,
	29,
	22,
	15,
	23,
	30,
	37,
	44,
	51,
	58,
	59,
	52,
	45,
	38,
	31,
	39,
	46,
	53,
	60,
	61,
	54,
	47,
	55,
	62,
	63
]), n = 4017, r = 799, i = 3406, a = 2276, o = 1567, s = 3784, c = 5793, l = 2896;
function u(e, t) {
	let n = 0, r = [], i = 16;
	for (; i > 0 && !e[i - 1];) --i;
	r.push({
		children: [],
		index: 0
	});
	let a = r[0], o;
	for (let s = 0; s < i; s++) {
		for (let i = 0; i < e[s]; i++) {
			if (a = r.pop(), !a) throw Error("buildHuffmanTable: codeLength mismatch");
			for (a.children[a.index] = t[n]; a.index > 0;) if (a = r.pop(), !a) throw Error("buildHuffmanTable: codeLength mismatch");
			for (a.index++, r.push(a); r.length <= s;) r.push(o = {
				children: [],
				index: 0
			}), a.children[a.index] = o.children, a = o;
			n++;
		}
		s + 1 < i && (r.push(o = {
			children: [],
			index: 0
		}), a.children[a.index] = o.children, a = o);
	}
	return r[0].children;
}
function d(e, n, r, i, a, o, s, c, l) {
	let { mcusPerLine: u, progressive: d } = r;
	if (i.length > 1 && (u === void 0 || r.mcusPerColumn === void 0)) throw Error("Missing MCU dimensions");
	if (i.length === 1 && (i[0].blocksPerLine === void 0 || i[0].blocksPerColumn === void 0)) throw Error("Missing block dimensions");
	let f = n, p = n, m = 0, h = 0;
	function g() {
		if (h > 0) return h--, m >> h & 1;
		if (m = e[p++], m === 255) {
			let t = e[p++];
			if (t) throw Error(`unexpected marker: ${(m << 8 | t).toString(16)}`);
		}
		return h = 7, m >>> 7;
	}
	function _(e) {
		if (!e) throw Error("Huffman table not found");
		let t = e, n;
		for (; (n = g()) !== null;) {
			let e = t[n];
			if (typeof e == "number") return e;
			if (typeof e != "object") throw Error("invalid huffman sequence");
			t = e;
		}
		return null;
	}
	function v(e) {
		let t = e, n = 0;
		for (; t > 0;) {
			let e = g();
			if (e === null) return;
			n = n << 1 | e, --t;
		}
		return n;
	}
	function y(e) {
		let t = v(e);
		if (t !== void 0) return t >= 1 << e - 1 ? t : t + (-1 << e) + 1;
	}
	function b(e, n) {
		let r = _(e.huffmanTableDC);
		if (r === null) throw Error("Huffman error");
		let i = r === 0 ? 0 : y(r);
		if (i === void 0) throw Error("Unexpected end of stream");
		e.pred === void 0 && (e.pred = 0), e.pred += i, n[0] = e.pred;
		let a = 1;
		for (; a < 64;) {
			let r = _(e.huffmanTableAC);
			if (r === null) throw Error("Unexpected end of data in AC coefficient decoding");
			let i = r & 15, o = r >> 4;
			if (i === 0) {
				if (o < 15) break;
				a += 16;
			} else {
				a += o;
				let e = t[a], r = y(i);
				if (r === void 0) throw Error("Unexpected end of stream");
				n[e] = r, a++;
			}
		}
	}
	function x(e, t) {
		let n = _(e.huffmanTableDC);
		if (n === null) throw Error("Huffman error");
		let r = y(n);
		if (r === void 0) throw Error("Unexpected end of data in DC coefficient decoding");
		let i = n === 0 ? 0 : r << l;
		e.pred === void 0 && (e.pred = 0), e.pred += i, t[0] = e.pred;
	}
	function S(e, t) {
		let n = g();
		if (n === null) throw Error("Unexpected end of data in DC coefficient decoding");
		t[0] |= n << l;
	}
	let C = 0;
	function w(e, n) {
		if (C > 0) {
			C--;
			return;
		}
		let r = o, i = s;
		for (; r <= i;) {
			let i = _(e.huffmanTableAC);
			if (i === null) throw Error("Unexpected end of data in AC coefficient decoding");
			let a = i & 15, o = i >> 4;
			if (a === 0) {
				if (o < 15) {
					let e = v(o);
					if (e === void 0) throw Error("Unexpected end of data in AC coefficient decoding");
					C = e + (1 << o) - 1;
					break;
				}
				r += 16;
			} else {
				r += o;
				let e = t[r], i = y(a);
				if (i === void 0) throw Error("Unexpected end of data in AC coefficient decoding");
				n[e] = i * (1 << l), r++;
			}
		}
	}
	let T = 0, E;
	function D(e, n) {
		let r = o, i = s, a = 0;
		for (; r <= i;) {
			let i = t[r], o = n[i] < 0 ? -1 : 1;
			switch (T) {
				case 0: {
					let t = _(e.huffmanTableAC);
					if (t === null) throw Error("Unexpected end of data in AC coefficient decoding");
					let n = t & 15;
					if (a = t >> 4, n === 0) if (a < 15) {
						let e = v(a);
						if (e === void 0) throw Error("Unexpected end of data in AC coefficient decoding");
						C = e + (1 << a), T = 4;
					} else a = 16, T = 1;
					else {
						if (n !== 1) throw Error("invalid ACn encoding");
						let e = y(n);
						if (e === void 0) throw Error("Unexpected end of data in AC coefficient decoding");
						E = e, T = a ? 2 : 3;
					}
					continue;
				}
				case 1:
				case 2:
					if (n[i]) {
						let e = g();
						if (e === null) throw Error("Unexpected end of data in AC coefficient decoding");
						n[i] += (e << l) * o;
					} else a--, a === 0 && (T = T === 2 ? 3 : 0);
					break;
				case 3:
					if (n[i]) {
						let e = g();
						if (e === null) throw Error("Unexpected end of data in AC coefficient decoding");
						n[i] += (e << l) * o;
					} else n[i] = E << l, T = 0;
					break;
				case 4:
					if (n[i]) {
						let e = g();
						if (e === null) throw Error("Unexpected end of data in AC coefficient decoding");
						n[i] += (e << l) * o;
					}
					break;
				default: break;
			}
			r++;
		}
		T === 4 && (C--, C === 0 && (T = 0));
	}
	function O(e, t, n, r, i) {
		let a = n / u | 0, o = n % u, s = a * e.v + r, c = o * e.h + i;
		if (!e.blocks) throw Error("Missing blocks");
		t(e, e.blocks[s][c]);
	}
	function k(e, t, n) {
		let r = n / e.blocksPerLine | 0, i = n % e.blocksPerLine;
		if (!e.blocks) throw Error("Missing blocks");
		t(e, e.blocks[r][i]);
	}
	let A = i.length, j, M, N, P, F, I;
	I = d ? o === 0 ? c === 0 ? x : S : c === 0 ? w : D : b;
	let L = 0, R, z;
	z = A === 1 ? i[0].blocksPerLine * i[0].blocksPerColumn : u * r.mcusPerColumn;
	let B = a || z;
	for (; L < z;) {
		for (M = 0; M < A; M++) i[M].pred = 0;
		if (C = 0, A === 1) for (j = i[0], F = 0; F < B; F++) k(j, I, L), L++;
		else for (F = 0; F < B; F++) {
			for (M = 0; M < A; M++) {
				j = i[M];
				let { h: e, v: t } = j;
				for (N = 0; N < t; N++) for (P = 0; P < e; P++) O(j, I, L, N, P);
			}
			if (L++, L === z) break;
		}
		if (h = 0, R = e[p] << 8 | e[p + 1], R < 65280) throw Error("marker was not found");
		if (R >= 65488 && R <= 65495) p += 2;
		else break;
	}
	return p - f;
}
function f(e) {
	let t = [], { blocksPerLine: u, blocksPerColumn: d } = e;
	if (!u || !d || !e.blocks) throw Error("Missing component data");
	let f = u << 3, p = new Int32Array(64), m = new Uint8Array(64);
	function h(t, u, d) {
		let f = e.quantizationTable;
		if (!f) throw Error("No quantization table found");
		let p, m, h, g, _, v, y, b, x, S = d, C;
		for (C = 0; C < 64; C++) S[C] = t[C] * f[C];
		for (C = 0; C < 8; ++C) {
			let e = 8 * C;
			if (S[1 + e] === 0 && S[2 + e] === 0 && S[3 + e] === 0 && S[4 + e] === 0 && S[5 + e] === 0 && S[6 + e] === 0 && S[7 + e] === 0) {
				x = c * S[0 + e] + 512 >> 10, S[0 + e] = x, S[1 + e] = x, S[2 + e] = x, S[3 + e] = x, S[4 + e] = x, S[5 + e] = x, S[6 + e] = x, S[7 + e] = x;
				continue;
			}
			p = c * S[0 + e] + 128 >> 8, m = c * S[4 + e] + 128 >> 8, h = S[2 + e], g = S[6 + e], _ = l * (S[1 + e] - S[7 + e]) + 128 >> 8, b = l * (S[1 + e] + S[7 + e]) + 128 >> 8, v = S[3 + e] << 4, y = S[5 + e] << 4, x = p - m + 1 >> 1, p = p + m + 1 >> 1, m = x, x = h * s + g * o + 128 >> 8, h = h * o - g * s + 128 >> 8, g = x, x = _ - y + 1 >> 1, _ = _ + y + 1 >> 1, y = x, x = b + v + 1 >> 1, v = b - v + 1 >> 1, b = x, x = p - g + 1 >> 1, p = p + g + 1 >> 1, g = x, x = m - h + 1 >> 1, m = m + h + 1 >> 1, h = x, x = _ * a + b * i + 2048 >> 12, _ = _ * i - b * a + 2048 >> 12, b = x, x = v * r + y * n + 2048 >> 12, v = v * n - y * r + 2048 >> 12, y = x, S[0 + e] = p + b, S[7 + e] = p - b, S[1 + e] = m + y, S[6 + e] = m - y, S[2 + e] = h + v, S[5 + e] = h - v, S[3 + e] = g + _, S[4 + e] = g - _;
		}
		for (C = 0; C < 8; ++C) {
			let e = C;
			if (S[8 + e] === 0 && S[16 + e] === 0 && S[24 + e] === 0 && S[32 + e] === 0 && S[40 + e] === 0 && S[48 + e] === 0 && S[56 + e] === 0) {
				x = c * d[C + 0] + 8192 >> 14, S[0 + e] = x, S[8 + e] = x, S[16 + e] = x, S[24 + e] = x, S[32 + e] = x, S[40 + e] = x, S[48 + e] = x, S[56 + e] = x;
				continue;
			}
			p = c * S[0 + e] + 2048 >> 12, m = c * S[32 + e] + 2048 >> 12, h = S[16 + e], g = S[48 + e], _ = l * (S[8 + e] - S[56 + e]) + 2048 >> 12, b = l * (S[8 + e] + S[56 + e]) + 2048 >> 12, v = S[24 + e], y = S[40 + e], x = p - m + 1 >> 1, p = p + m + 1 >> 1, m = x, x = h * s + g * o + 2048 >> 12, h = h * o - g * s + 2048 >> 12, g = x, x = _ - y + 1 >> 1, _ = _ + y + 1 >> 1, y = x, x = b + v + 1 >> 1, v = b - v + 1 >> 1, b = x, x = p - g + 1 >> 1, p = p + g + 1 >> 1, g = x, x = m - h + 1 >> 1, m = m + h + 1 >> 1, h = x, x = _ * a + b * i + 2048 >> 12, _ = _ * i - b * a + 2048 >> 12, b = x, x = v * r + y * n + 2048 >> 12, v = v * n - y * r + 2048 >> 12, y = x, S[0 + e] = p + b, S[56 + e] = p - b, S[8 + e] = m + y, S[48 + e] = m - y, S[16 + e] = h + v, S[40 + e] = h - v, S[24 + e] = g + _, S[32 + e] = g - _;
		}
		for (C = 0; C < 64; ++C) {
			let e = 128 + (S[C] + 8 >> 4);
			e < 0 ? u[C] = 0 : e > 255 ? u[C] = 255 : u[C] = e;
		}
	}
	for (let n = 0; n < d; n++) {
		let r = n << 3;
		for (let e = 0; e < 8; e++) t.push(new Uint8Array(f));
		for (let i = 0; i < u; i++) {
			h(e.blocks[n][i], m, p);
			let a = 0, o = i << 3;
			for (let e = 0; e < 8; e++) {
				let n = t[r + e];
				for (let e = 0; e < 8; e++) n[o + e] = m[a++];
			}
		}
	}
	return t;
}
var p = class {
	constructor() {
		this.jfif = null, this.adobe = null, this.resetInterval = 0, this.quantizationTables = [], this.huffmanTablesAC = [], this.huffmanTablesDC = [], this.frames = [];
	}
	resetFrames() {
		this.frames = [];
	}
	parse(e) {
		let n = 0;
		function r() {
			let t = e[n] << 8 | e[n + 1];
			return n += 2, t;
		}
		function i() {
			let t = r(), i = e.subarray(n, n + t - 2);
			return n += i.length, i;
		}
		function a(e) {
			let t = 0, n = 0, r, i;
			for (i in e.components) e.components.hasOwnProperty(i) && (r = e.components[i], t < r.h && (t = r.h), n < r.v && (n = r.v));
			let a = Math.ceil(e.samplesPerLine / 8 / t), o = Math.ceil(e.scanLines / 8 / n);
			for (i in e.components) if (e.components.hasOwnProperty(i)) {
				r = e.components[i];
				let s = Math.ceil(Math.ceil(e.samplesPerLine / 8) * r.h / t), c = Math.ceil(Math.ceil(e.scanLines / 8) * r.v / n), l = a * r.h, u = o * r.v, d = [];
				for (let e = 0; e < u; e++) {
					let e = [];
					for (let t = 0; t < l; t++) e.push(new Int32Array(64));
					d.push(e);
				}
				r.blocksPerLine = s, r.blocksPerColumn = c, r.blocks = d;
			}
			e.maxH = t, e.maxV = n, e.mcusPerLine = a, e.mcusPerColumn = o;
		}
		let o = r();
		if (o !== 65496) throw Error("SOI not found");
		for (o = r(); o !== 65497;) {
			switch (o) {
				case 65280: break;
				case 65504:
				case 65505:
				case 65506:
				case 65507:
				case 65508:
				case 65509:
				case 65510:
				case 65511:
				case 65512:
				case 65513:
				case 65514:
				case 65515:
				case 65516:
				case 65517:
				case 65518:
				case 65519:
				case 65534: {
					let e = i();
					o === 65504 && e[0] === 74 && e[1] === 70 && e[2] === 73 && e[3] === 70 && e[4] === 0 && (this.jfif = {
						version: {
							major: e[5],
							minor: e[6]
						},
						densityUnits: e[7],
						xDensity: e[8] << 8 | e[9],
						yDensity: e[10] << 8 | e[11],
						thumbWidth: e[12],
						thumbHeight: e[13],
						thumbData: e.subarray(14, 14 + 3 * e[12] * e[13])
					}), o === 65518 && e[0] === 65 && e[1] === 100 && e[2] === 111 && e[3] === 98 && e[4] === 101 && e[5] === 0 && (this.adobe = {
						version: e[6],
						flags0: e[7] << 8 | e[8],
						flags1: e[9] << 8 | e[10],
						transformCode: e[11]
					});
					break;
				}
				case 65499: {
					let i = r() + n - 2;
					for (; n < i;) {
						let i = e[n++], a = new Int32Array(64);
						if (!(i >> 4)) for (let r = 0; r < 64; r++) {
							let i = t[r];
							a[i] = e[n++];
						}
						else if (i >> 4 == 1) for (let e = 0; e < 64; e++) {
							let n = t[e];
							a[n] = r();
						}
						else throw Error("DQT: invalid table spec");
						this.quantizationTables[i & 15] = a;
					}
					break;
				}
				case 65472:
				case 65473:
				case 65474: {
					r();
					let t = {
						extended: o === 65473,
						progressive: o === 65474,
						precision: e[n++],
						scanLines: r(),
						samplesPerLine: r(),
						components: {},
						componentsOrder: [],
						maxH: 0,
						maxV: 0,
						mcusPerLine: 0,
						mcusPerColumn: 0
					}, i = e[n++], s;
					for (let r = 0; r < i; r++) {
						s = e[n];
						let r = e[n + 1] >> 4, i = e[n + 1] & 15, a = e[n + 2];
						t.componentsOrder.push(s), t.components[s] = {
							h: r,
							v: i,
							quantizationIdx: a,
							blocksPerLine: 0,
							blocksPerColumn: 0,
							blocks: []
						}, n += 3;
					}
					a(t), this.frames.push(t);
					break;
				}
				case 65476: {
					let t = r();
					for (let r = 2; r < t;) {
						let t = e[n++], i = new Uint8Array(16), a = 0;
						for (let t = 0; t < 16; t++, n++) i[t] = e[n], a += i[t];
						let o = new Uint8Array(a);
						for (let t = 0; t < a; t++, n++) o[t] = e[n];
						r += 17 + a, t >> 4 ? this.huffmanTablesAC[t & 15] = u(i, o) : this.huffmanTablesDC[t & 15] = u(i, o);
					}
					break;
				}
				case 65501:
					r(), this.resetInterval = r();
					break;
				case 65498: {
					r();
					let t = e[n++], i = [], a = this.frames[0];
					for (let r = 0; r < t; r++) {
						let t = a.components[e[n++]], r = e[n++];
						t.huffmanTableDC = this.huffmanTablesDC[r >> 4], t.huffmanTableAC = this.huffmanTablesAC[r & 15], i.push(t);
					}
					let o = e[n++], s = e[n++], c = e[n++], l = d(e, n, a, i, this.resetInterval, o, s, c >> 4, c & 15);
					n += l;
					break;
				}
				case 65535:
					e[n] !== 255 && n--;
					break;
				default:
					if (e[n - 3] === 255 && e[n - 2] >= 192 && e[n - 2] <= 254) {
						n -= 3;
						break;
					}
					throw Error(`unknown JPEG marker ${o.toString(16)}`);
			}
			o = r();
		}
	}
	getResult() {
		let { frames: e } = this;
		if (this.frames.length === 0) throw Error("no frames were decoded");
		this.frames.length > 1 && console.warn("more than one frame is not supported");
		for (let e = 0; e < this.frames.length; e++) {
			let t = this.frames[e].components;
			for (let e of Object.keys(t)) {
				let n = t[e].quantizationIdx;
				typeof n == "number" && (t[e].quantizationTable = this.quantizationTables[n], delete t[e].quantizationIdx);
			}
		}
		let t = e[0];
		if (!t.maxH || !t.maxV) throw Error("Invalid frame dimensions");
		let { components: n, componentsOrder: r } = t, i = [], a = t.samplesPerLine, o = t.scanLines;
		for (let e = 0; e < r.length; e++) {
			let a = n[r[e]];
			i.push({
				lines: f(a),
				scaleX: a.h / t.maxH,
				scaleY: a.v / t.maxV
			});
		}
		let s = new Uint8Array(a * o * i.length), c = 0;
		for (let e = 0; e < o; ++e) for (let t = 0; t < a; ++t) for (let n = 0; n < i.length; ++n) {
			let r = i[n];
			s[c] = r.lines[0 | e * r.scaleY][0 | t * r.scaleX], ++c;
		}
		return s;
	}
}, m = class extends e {
	constructor(e) {
		super(e), this.reader = new p(), e.JPEGTables && this.reader.parse(e.JPEGTables);
	}
	decodeBlock(e) {
		return this.reader.resetFrames(), this.reader.parse(new Uint8Array(e)), this.reader.getResult().buffer;
	}
};
//#endregion
export { m as default };
