1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.align = align;
|
7 | exports.pathRelativeBase = pathRelativeBase;
|
8 | exports.pathRelativeBaseMatch = pathRelativeBaseMatch;
|
9 | exports.pngHalfSize = pngHalfSize;
|
10 | exports.quoteCmd = quoteCmd;
|
11 | exports.quoteSh = quoteSh;
|
12 | exports.trimDotSlash = trimDotSlash;
|
13 | var _puka = require("puka");
|
14 | var _iconEncoder = require("@shockpkg/icon-encoder");
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | function trimDotSlash(path) {
|
24 | return path.replace(/^(\.\/)+/, '');
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | function 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 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | function pathRelativeBaseMatch(path, start, nocase = false) {
|
56 | return pathRelativeBase(path, start, nocase) !== null;
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | function align(i, align) {
|
67 | const o = i % align;
|
68 | return o ? align - o + i : i;
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | function quoteSh(str) {
|
78 | return (0, _puka.quoteForSh)(str);
|
79 | }
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | function quoteCmd(str) {
|
88 | return (0, _puka.quoteForCmd)(str);
|
89 | }
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | function 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 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | async 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 |
|
\ | No newline at end of file |