UNPKG

4.55 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.align = align;
7exports.pathRelativeBase = pathRelativeBase;
8exports.pathRelativeBaseMatch = pathRelativeBaseMatch;
9exports.pngHalfSize = pngHalfSize;
10exports.quoteCmd = quoteCmd;
11exports.quoteSh = quoteSh;
12exports.trimDotSlash = trimDotSlash;
13var _puka = require("puka");
14var _iconEncoder = require("@shockpkg/icon-encoder");
15// @ts-expect-error: No types.
16
17/**
18 * Trim dot flash from head of path.
19 *
20 * @param path Path string.
21 * @returns Trimmed path.
22 */
23function trimDotSlash(path) {
24 return path.replace(/^(\.\/)+/, '');
25}
26
27/**
28 * Find path relative from base, if base matches.
29 *
30 * @param path Path to match against.
31 * @param start Search start.
32 * @param nocase Match case-insensitive.
33 * @returns Returns path, or null.
34 */
35function pathRelativeBase(path, start, nocase = false) {
36 const p = trimDotSlash(nocase ? path.toLowerCase() : path);
37 const s = trimDotSlash(nocase ? start.toLowerCase() : start);
38 if (p === s) {
39 return '';
40 }
41 if (p.startsWith(`${s}/`)) {
42 return path.substring(s.length + 1);
43 }
44 return null;
45}
46
47/**
48 * Same as pathRelativeBase, but retuns true on a match, else false.
49 *
50 * @param path Path to match against.
51 * @param start Search start.
52 * @param nocase Match case-insensitive.
53 * @returns Returns true on match, else false.
54 */
55function pathRelativeBaseMatch(path, start, nocase = false) {
56 return pathRelativeBase(path, start, nocase) !== null;
57}
58
59/**
60 * Align integer.
61 *
62 * @param i Integer value.
63 * @param align Alignment amount.
64 * @returns Aligned integer.
65 */
66function align(i, align) {
67 const o = i % align;
68 return o ? align - o + i : i;
69}
70
71/**
72 * Quote string for SH.
73 *
74 * @param str String to be quoted.
75 * @returns Quoted string.
76 */
77function quoteSh(str) {
78 return (0, _puka.quoteForSh)(str);
79}
80
81/**
82 * Quote string for CMD.
83 *
84 * @param str String to be quoted.
85 * @returns Quoted string.
86 */
87function quoteCmd(str) {
88 return (0, _puka.quoteForCmd)(str);
89}
90
91/**
92 * Resize RGBA image data down by exactly half.
93 * Input dimensions must be a power of 2.
94 *
95 * @param rgba Image data.
96 * @returns Image data.
97 */
98function resizeRgbaHalf(rgba) {
99 const {
100 width: ww,
101 height: hh,
102 data: dd
103 } = rgba;
104 const w = ww / 2;
105 const h = hh / 2;
106 const d = new Uint8Array(w * h * 4);
107 const r = {
108 width: w,
109 height: h,
110 data: d
111 };
112 const w4 = w * 4;
113 const ww4 = ww * 4;
114 for (let y = 0, yy = 0; y < h; y++, yy += 2) {
115 const yi = y * w4;
116 const yyi = yy * ww4;
117 const yyj = (yy + 1) * ww4;
118 for (let x = 0, xx = 0; x < w; x++, xx += 2) {
119 const xi = x * 4;
120 const xxi = xx * 4;
121 const xxj = (xx + 1) * 4;
122 let p0 = yyi + xxi;
123 let p1 = yyi + xxj;
124 let p2 = yyj + xxi;
125 let p3 = yyj + xxj;
126 const r0 = dd[p0++];
127 const r1 = dd[p1++];
128 const r2 = dd[p2++];
129 const r3 = dd[p3++];
130 const g0 = dd[p0++];
131 const g1 = dd[p1++];
132 const g2 = dd[p2++];
133 const g3 = dd[p3++];
134 const b0 = dd[p0++];
135 const b1 = dd[p1++];
136 const b2 = dd[p2++];
137 const b3 = dd[p3++];
138 const a0 = dd[p0];
139 const a1 = dd[p1];
140 const a2 = dd[p2];
141 const a3 = dd[p3];
142 const a4 = a0 + a1 + a2 + a3;
143 let a04 = 0;
144 let a14 = 0;
145 let a24 = 0;
146 let a34 = 0;
147 if (a4) {
148 a04 = a0 / a4;
149 a14 = a1 / a4;
150 a24 = a2 / a4;
151 a34 = a3 / a4;
152 }
153 let p = yi + xi;
154 d[p++] = Math.round(r0 * a04 + r1 * a14 + r2 * a24 + r3 * a34);
155 d[p++] = Math.round(g0 * a04 + g1 * a14 + g2 * a24 + g3 * a34);
156 d[p++] = Math.round(b0 * a04 + b1 * a14 + b2 * a24 + b3 * a34);
157 d[p] = Math.round(a4 / 4);
158 }
159 }
160 return r;
161}
162
163/**
164 * Resize RGBA image data down by exactly half.
165 * Input dimensions must be a power of 2, else error is thrown.
166 * If resizing multiple steps, each step dimensions must be a power of 2.
167 *
168 * @param png PNG data.
169 * @param x Number of times to resize down by half.
170 * @returns PNG data.
171 */
172async function pngHalfSize(png, x = 1) {
173 let rgba = await (0, _iconEncoder.decodePngToRgba)(png);
174 if ((x = Math.round(x)) > 0) {
175 const p = 2 ** x;
176 const {
177 width,
178 height
179 } = rgba;
180 if (width % 1 || height % 1) {
181 throw new Error(`Image dimensions not a power of ${p}: ${width}x${height}`);
182 }
183 for (let i = 0; i < x; i++) {
184 rgba = resizeRgbaHalf(rgba);
185 }
186 }
187 return (0, _iconEncoder.encodeRgbaToPng)(rgba);
188}
189//# sourceMappingURL=util.js.map
\No newline at end of file