@pixerate/editor 0.87.0 → 0.89.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/THIRD_PARTY_NOTICES.md +8 -2
- package/dist/agent/index.cjs +28 -28
- package/dist/agent/index.js +3 -3
- package/dist/canvas/index.cjs +3 -3
- package/dist/canvas/index.cjs.map +1 -1
- package/dist/canvas/index.js +16 -16
- package/dist/{chunk-TYOAJDFD.cjs → chunk-66IMF6OD.cjs} +3 -3
- package/dist/{chunk-TYOAJDFD.cjs.map → chunk-66IMF6OD.cjs.map} +1 -1
- package/dist/chunk-D4ZCU72J.js +604 -0
- package/dist/chunk-D4ZCU72J.js.map +1 -0
- package/dist/{chunk-IMSX4IVG.js → chunk-FWEO3HSL.js} +4 -4
- package/dist/chunk-H543GD6T.cjs +1696 -0
- package/dist/chunk-H543GD6T.cjs.map +1 -0
- package/dist/{chunk-ROXFP2LQ.cjs → chunk-HNDSPZ5T.cjs} +4 -4
- package/dist/{chunk-ROXFP2LQ.cjs.map → chunk-HNDSPZ5T.cjs.map} +1 -1
- package/dist/{chunk-MXF4DP2C.cjs → chunk-J3B45HTA.cjs} +74 -74
- package/dist/{chunk-MXF4DP2C.cjs.map → chunk-J3B45HTA.cjs.map} +1 -1
- package/dist/{chunk-GNO5PEMH.js → chunk-OK7T2LHK.js} +2 -2
- package/dist/chunk-P5TPUNBZ.js +1696 -0
- package/dist/chunk-P5TPUNBZ.js.map +1 -0
- package/dist/chunk-PDCITYBT.cjs +416 -0
- package/dist/chunk-PDCITYBT.cjs.map +1 -0
- package/dist/{chunk-RKJDBXTY.cjs → chunk-T4XWVPJK.cjs} +3 -3
- package/dist/{chunk-RKJDBXTY.cjs.map → chunk-T4XWVPJK.cjs.map} +1 -1
- package/dist/{chunk-ZQNDSUZO.js → chunk-UKI64KUD.js} +6 -6
- package/dist/{chunk-SVW2UNUC.js → chunk-WON34HKM.js} +2 -2
- package/dist/chunk-YLB2WVNA.js +416 -0
- package/dist/chunk-YLB2WVNA.js.map +1 -0
- package/dist/chunk-ZOOEX3WT.cjs +604 -0
- package/dist/chunk-ZOOEX3WT.cjs.map +1 -0
- package/dist/extensions/index.cjs +6 -6
- package/dist/extensions/index.cjs.map +1 -1
- package/dist/extensions/index.js +5 -5
- package/dist/image-editor/index.cjs +4387 -690
- package/dist/image-editor/index.cjs.map +1 -1
- package/dist/image-editor/index.d.cts +1677 -590
- package/dist/image-editor/index.d.ts +1677 -590
- package/dist/image-editor/index.js +4273 -576
- package/dist/image-editor/index.js.map +1 -1
- package/dist/index.cjs +27 -27
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +10 -10
- package/dist/paint/index.cjs +38 -0
- package/dist/paint/index.cjs.map +1 -0
- package/dist/paint/index.d.cts +167 -0
- package/dist/paint/index.d.ts +167 -0
- package/dist/paint/index.js +38 -0
- package/dist/paint/index.js.map +1 -0
- package/dist/raster/index.cjs +121 -0
- package/dist/raster/index.cjs.map +1 -0
- package/dist/raster/index.d.cts +365 -0
- package/dist/raster/index.d.ts +365 -0
- package/dist/raster/index.js +121 -0
- package/dist/raster/index.js.map +1 -0
- package/dist/serializers/index.cjs +4 -4
- package/dist/serializers/index.cjs.map +1 -1
- package/dist/serializers/index.js +3 -3
- package/dist/surface-fJhkmNQn.d.cts +181 -0
- package/dist/surface-fJhkmNQn.d.ts +181 -0
- package/dist/video-editor/index.cjs +930 -503
- package/dist/video-editor/index.cjs.map +1 -1
- package/dist/video-editor/index.d.cts +158 -11
- package/dist/video-editor/index.d.ts +158 -11
- package/dist/video-editor/index.js +841 -414
- package/dist/video-editor/index.js.map +1 -1
- package/package.json +13 -2
- package/skills/pixerate-image-editor/SKILL.md +111 -11
- package/skills/pixerate-video-editor/SKILL.md +11 -2
- /package/dist/{chunk-IMSX4IVG.js.map → chunk-FWEO3HSL.js.map} +0 -0
- /package/dist/{chunk-GNO5PEMH.js.map → chunk-OK7T2LHK.js.map} +0 -0
- /package/dist/{chunk-ZQNDSUZO.js.map → chunk-UKI64KUD.js.map} +0 -0
- /package/dist/{chunk-SVW2UNUC.js.map → chunk-WON34HKM.js.map} +0 -0
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
// src/raster/surface.ts
|
|
2
|
+
var TILE = 256;
|
|
3
|
+
var TILE_SHIFT = 8;
|
|
4
|
+
var TILE_PIXELS = TILE * TILE;
|
|
5
|
+
var KEY_OFFSET = 1 << 15;
|
|
6
|
+
var KEY_SPAN = 1 << 16;
|
|
7
|
+
function tileKey(tx, ty) {
|
|
8
|
+
return (ty + KEY_OFFSET) * KEY_SPAN + (tx + KEY_OFFSET);
|
|
9
|
+
}
|
|
10
|
+
function tileCoord(key) {
|
|
11
|
+
return { tx: key % KEY_SPAN - KEY_OFFSET, ty: Math.floor(key / KEY_SPAN) - KEY_OFFSET };
|
|
12
|
+
}
|
|
13
|
+
function tileRect(tx, ty) {
|
|
14
|
+
return { x: tx * TILE, y: ty * TILE, width: TILE, height: TILE };
|
|
15
|
+
}
|
|
16
|
+
var nextSurfaceId = 1;
|
|
17
|
+
var TiledSurface = class {
|
|
18
|
+
constructor(channels, tiles, defaultValue = 0) {
|
|
19
|
+
this.channels = channels;
|
|
20
|
+
this.tiles = tiles;
|
|
21
|
+
this.defaultValue = defaultValue;
|
|
22
|
+
}
|
|
23
|
+
channels;
|
|
24
|
+
tiles;
|
|
25
|
+
defaultValue;
|
|
26
|
+
/** Unique per instance: a cache key that changes whenever the pixels can have changed. */
|
|
27
|
+
id = nextSurfaceId++;
|
|
28
|
+
boundsCache;
|
|
29
|
+
/** Bounds of the stored (non-default) tiles, tile-aligned; `null` when there are none. */
|
|
30
|
+
get bounds() {
|
|
31
|
+
if (this.boundsCache === void 0) this.boundsCache = tileUnion(this.tiles.keys());
|
|
32
|
+
return this.boundsCache;
|
|
33
|
+
}
|
|
34
|
+
get tileCount() {
|
|
35
|
+
return this.tiles.size;
|
|
36
|
+
}
|
|
37
|
+
getTile(tx, ty) {
|
|
38
|
+
return this.tiles.get(tileKey(tx, ty));
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
function tileUnion(keys) {
|
|
42
|
+
let x0 = Infinity;
|
|
43
|
+
let y0 = Infinity;
|
|
44
|
+
let x1 = -Infinity;
|
|
45
|
+
let y1 = -Infinity;
|
|
46
|
+
for (const key of keys) {
|
|
47
|
+
const { tx, ty } = tileCoord(key);
|
|
48
|
+
if (tx < x0) x0 = tx;
|
|
49
|
+
if (ty < y0) y0 = ty;
|
|
50
|
+
if (tx > x1) x1 = tx;
|
|
51
|
+
if (ty > y1) y1 = ty;
|
|
52
|
+
}
|
|
53
|
+
if (x0 === Infinity) return null;
|
|
54
|
+
return { x: x0 * TILE, y: y0 * TILE, width: (x1 - x0 + 1) * TILE, height: (y1 - y0 + 1) * TILE };
|
|
55
|
+
}
|
|
56
|
+
var uniformTiles = /* @__PURE__ */ new Map();
|
|
57
|
+
var UNIFORM_LIMIT = 64;
|
|
58
|
+
function packPixel(data, offset, channels) {
|
|
59
|
+
return channels === 1 ? data[offset] : (data[offset] << 24 | data[offset + 1] << 16 | data[offset + 2] << 8 | data[offset + 3]) >>> 0;
|
|
60
|
+
}
|
|
61
|
+
function uniformTile(channels, value) {
|
|
62
|
+
const key = `${channels}:${value}`;
|
|
63
|
+
let tile = uniformTiles.get(key);
|
|
64
|
+
if (tile) return tile;
|
|
65
|
+
const data = new Uint8ClampedArray(TILE_PIXELS * channels);
|
|
66
|
+
if (channels === 1) data.fill(value);
|
|
67
|
+
else if (value !== 0) {
|
|
68
|
+
const r = value >>> 24;
|
|
69
|
+
const g = value >>> 16 & 255;
|
|
70
|
+
const b = value >>> 8 & 255;
|
|
71
|
+
const a = value & 255;
|
|
72
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
73
|
+
data[i] = r;
|
|
74
|
+
data[i + 1] = g;
|
|
75
|
+
data[i + 2] = b;
|
|
76
|
+
data[i + 3] = a;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
tile = { data, uniform: value };
|
|
80
|
+
if (uniformTiles.size >= UNIFORM_LIMIT) uniformTiles.delete(uniformTiles.keys().next().value);
|
|
81
|
+
uniformTiles.set(key, tile);
|
|
82
|
+
return tile;
|
|
83
|
+
}
|
|
84
|
+
function packRgba(r, g, b, a) {
|
|
85
|
+
return (r << 24 | g << 16 | b << 8 | a) >>> 0;
|
|
86
|
+
}
|
|
87
|
+
function defaultPacked(channels, defaultValue) {
|
|
88
|
+
return channels === 1 ? defaultValue : packRgba(defaultValue, defaultValue, defaultValue, defaultValue);
|
|
89
|
+
}
|
|
90
|
+
function uniformValue(data, channels) {
|
|
91
|
+
if (channels === 1) {
|
|
92
|
+
const v = data[0];
|
|
93
|
+
for (let i = 1; i < data.length; i++) if (data[i] !== v) return null;
|
|
94
|
+
return v;
|
|
95
|
+
}
|
|
96
|
+
const words = new Uint32Array(data.buffer, data.byteOffset, data.length >> 2);
|
|
97
|
+
const w = words[0];
|
|
98
|
+
for (let i = 1; i < words.length; i++) if (words[i] !== w) return null;
|
|
99
|
+
return packPixel(data, 0, 4);
|
|
100
|
+
}
|
|
101
|
+
function finishTile(data, channels, defaultValue) {
|
|
102
|
+
const u = uniformValue(data, channels);
|
|
103
|
+
if (u === null) return { data, uniform: null };
|
|
104
|
+
if (u === defaultPacked(channels, defaultValue)) return null;
|
|
105
|
+
return uniformTile(channels, u);
|
|
106
|
+
}
|
|
107
|
+
function emptySurface(channels = 4, defaultValue = 0) {
|
|
108
|
+
return new TiledSurface(channels, /* @__PURE__ */ new Map(), defaultValue);
|
|
109
|
+
}
|
|
110
|
+
function solidSurface(channels, rect, value, defaultValue = 0) {
|
|
111
|
+
const bytes = new Uint8ClampedArray(channels);
|
|
112
|
+
if (channels === 1) bytes[0] = value;
|
|
113
|
+
else {
|
|
114
|
+
bytes[0] = value >>> 24;
|
|
115
|
+
bytes[1] = value >>> 16 & 255;
|
|
116
|
+
bytes[2] = value >>> 8 & 255;
|
|
117
|
+
bytes[3] = value & 255;
|
|
118
|
+
}
|
|
119
|
+
return fillRect(emptySurface(channels, defaultValue), rect, bytes);
|
|
120
|
+
}
|
|
121
|
+
function surfaceFromPixels(pixels, channels = 4, x = 0, y = 0, defaultValue = 0) {
|
|
122
|
+
return writeRegion(emptySurface(channels, defaultValue), { x, y, width: pixels.width, height: pixels.height }, pixels.data);
|
|
123
|
+
}
|
|
124
|
+
function tilesInRect(rect) {
|
|
125
|
+
const out = [];
|
|
126
|
+
if (rect.width <= 0 || rect.height <= 0) return out;
|
|
127
|
+
const tx0 = Math.floor(rect.x / TILE);
|
|
128
|
+
const ty0 = Math.floor(rect.y / TILE);
|
|
129
|
+
const tx1 = Math.floor((rect.x + rect.width - 1) / TILE);
|
|
130
|
+
const ty1 = Math.floor((rect.y + rect.height - 1) / TILE);
|
|
131
|
+
for (let ty = ty0; ty <= ty1; ty++) for (let tx = tx0; tx <= tx1; tx++) out.push({ tx, ty });
|
|
132
|
+
return out;
|
|
133
|
+
}
|
|
134
|
+
function readRegion(surface, rect, out) {
|
|
135
|
+
const ch = surface.channels;
|
|
136
|
+
const w = rect.width;
|
|
137
|
+
const h = rect.height;
|
|
138
|
+
const dst = out ?? new Uint8ClampedArray(w * h * ch);
|
|
139
|
+
if (surface.defaultValue !== 0) dst.fill(surface.defaultValue);
|
|
140
|
+
else if (out) dst.fill(0);
|
|
141
|
+
for (const { tx, ty } of tilesInRect(rect)) {
|
|
142
|
+
const tile = surface.getTile(tx, ty);
|
|
143
|
+
if (!tile) continue;
|
|
144
|
+
const ox = tx * TILE;
|
|
145
|
+
const oy = ty * TILE;
|
|
146
|
+
const x0 = Math.max(rect.x, ox);
|
|
147
|
+
const y0 = Math.max(rect.y, oy);
|
|
148
|
+
const x1 = Math.min(rect.x + w, ox + TILE);
|
|
149
|
+
const y1 = Math.min(rect.y + h, oy + TILE);
|
|
150
|
+
const span = (x1 - x0) * ch;
|
|
151
|
+
for (let yy = y0; yy < y1; yy++) {
|
|
152
|
+
const s = ((yy - oy) * TILE + (x0 - ox)) * ch;
|
|
153
|
+
const d = ((yy - rect.y) * w + (x0 - rect.x)) * ch;
|
|
154
|
+
dst.set(tile.data.subarray(s, s + span), d);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return dst;
|
|
158
|
+
}
|
|
159
|
+
function readPixel(surface, x, y) {
|
|
160
|
+
const tile = surface.getTile(Math.floor(x / TILE), Math.floor(y / TILE));
|
|
161
|
+
const ch = surface.channels;
|
|
162
|
+
if (!tile) return new Array(ch).fill(surface.defaultValue);
|
|
163
|
+
const i = ((y & TILE - 1) * TILE + (x & TILE - 1)) * ch;
|
|
164
|
+
return Array.from(tile.data.subarray(i, i + ch));
|
|
165
|
+
}
|
|
166
|
+
function contentBounds(surface) {
|
|
167
|
+
const ch = surface.channels;
|
|
168
|
+
const def = surface.defaultValue;
|
|
169
|
+
let x0 = Infinity;
|
|
170
|
+
let y0 = Infinity;
|
|
171
|
+
let x1 = -Infinity;
|
|
172
|
+
let y1 = -Infinity;
|
|
173
|
+
for (const [key, tile] of surface.tiles) {
|
|
174
|
+
const { tx, ty } = tileCoord(key);
|
|
175
|
+
const ox = tx * TILE;
|
|
176
|
+
const oy = ty * TILE;
|
|
177
|
+
if (tile.uniform !== null) {
|
|
178
|
+
if (ox < x0) x0 = ox;
|
|
179
|
+
if (oy < y0) y0 = oy;
|
|
180
|
+
if (ox + TILE - 1 > x1) x1 = ox + TILE - 1;
|
|
181
|
+
if (oy + TILE - 1 > y1) y1 = oy + TILE - 1;
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
const d = tile.data;
|
|
185
|
+
for (let y = 0; y < TILE; y++) {
|
|
186
|
+
let first = -1;
|
|
187
|
+
let last = -1;
|
|
188
|
+
const row = y * TILE * ch;
|
|
189
|
+
for (let x = 0; x < TILE; x++) {
|
|
190
|
+
const i = row + x * ch;
|
|
191
|
+
let differs = false;
|
|
192
|
+
for (let c = 0; c < ch; c++) if (d[i + c] !== def) differs = true;
|
|
193
|
+
if (differs) {
|
|
194
|
+
if (first < 0) first = x;
|
|
195
|
+
last = x;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (first < 0) continue;
|
|
199
|
+
if (ox + first < x0) x0 = ox + first;
|
|
200
|
+
if (ox + last > x1) x1 = ox + last;
|
|
201
|
+
if (oy + y < y0) y0 = oy + y;
|
|
202
|
+
if (oy + y > y1) y1 = oy + y;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (x0 === Infinity) return null;
|
|
206
|
+
return { x: x0, y: y0, width: x1 - x0 + 1, height: y1 - y0 + 1 };
|
|
207
|
+
}
|
|
208
|
+
var SurfaceDraft = class {
|
|
209
|
+
constructor(base) {
|
|
210
|
+
this.base = base;
|
|
211
|
+
}
|
|
212
|
+
base;
|
|
213
|
+
copies = /* @__PURE__ */ new Map();
|
|
214
|
+
get channels() {
|
|
215
|
+
return this.base.channels;
|
|
216
|
+
}
|
|
217
|
+
/** The writable data of tile (tx, ty), copied from the base on first use. */
|
|
218
|
+
tile(tx, ty) {
|
|
219
|
+
const key = tileKey(tx, ty);
|
|
220
|
+
let data = this.copies.get(key);
|
|
221
|
+
if (data) return data;
|
|
222
|
+
const src = this.base.tiles.get(key);
|
|
223
|
+
if (src) data = new Uint8ClampedArray(src.data);
|
|
224
|
+
else {
|
|
225
|
+
data = new Uint8ClampedArray(TILE_PIXELS * this.base.channels);
|
|
226
|
+
if (this.base.defaultValue !== 0) data.fill(this.base.defaultValue);
|
|
227
|
+
}
|
|
228
|
+
this.copies.set(key, data);
|
|
229
|
+
return data;
|
|
230
|
+
}
|
|
231
|
+
/** Replaces a whole tile with `data` (owned by the draft from now on). */
|
|
232
|
+
replace(tx, ty, data) {
|
|
233
|
+
this.copies.set(tileKey(tx, ty), data);
|
|
234
|
+
}
|
|
235
|
+
/** The current data of a tile without copying it (base tile, draft copy or `null` for default). */
|
|
236
|
+
peek(tx, ty) {
|
|
237
|
+
const key = tileKey(tx, ty);
|
|
238
|
+
return this.copies.get(key) ?? this.base.tiles.get(key)?.data ?? null;
|
|
239
|
+
}
|
|
240
|
+
/** Keys of the tiles changed so far. */
|
|
241
|
+
get touched() {
|
|
242
|
+
return [...this.copies.keys()];
|
|
243
|
+
}
|
|
244
|
+
/** A new surface with the draft's tiles; the draft must not be used afterwards. */
|
|
245
|
+
commit() {
|
|
246
|
+
if (!this.copies.size) return this.base;
|
|
247
|
+
const tiles = new Map(this.base.tiles);
|
|
248
|
+
const { channels, defaultValue } = this.base;
|
|
249
|
+
for (const [key, data] of this.copies) {
|
|
250
|
+
const tile = finishTile(data, channels, defaultValue);
|
|
251
|
+
if (tile) tiles.set(key, tile);
|
|
252
|
+
else tiles.delete(key);
|
|
253
|
+
}
|
|
254
|
+
return new TiledSurface(channels, tiles, defaultValue);
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
function writeRegion(surface, rect, data) {
|
|
258
|
+
const ch = surface.channels;
|
|
259
|
+
const w = rect.width;
|
|
260
|
+
const h = rect.height;
|
|
261
|
+
if (w <= 0 || h <= 0) return surface;
|
|
262
|
+
if (data.length < w * h * ch) throw new Error(`writeRegion: expected ${w * h * ch} bytes, got ${data.length}`);
|
|
263
|
+
const draft = new SurfaceDraft(surface);
|
|
264
|
+
const src = data;
|
|
265
|
+
const canSubarray = typeof src.subarray === "function";
|
|
266
|
+
for (const { tx, ty } of tilesInRect(rect)) {
|
|
267
|
+
const ox = tx * TILE;
|
|
268
|
+
const oy = ty * TILE;
|
|
269
|
+
const x0 = Math.max(rect.x, ox);
|
|
270
|
+
const y0 = Math.max(rect.y, oy);
|
|
271
|
+
const x1 = Math.min(rect.x + w, ox + TILE);
|
|
272
|
+
const y1 = Math.min(rect.y + h, oy + TILE);
|
|
273
|
+
const full = x0 === ox && y0 === oy && x1 === ox + TILE && y1 === oy + TILE;
|
|
274
|
+
const t = full ? new Uint8ClampedArray(TILE_PIXELS * ch) : draft.tile(tx, ty);
|
|
275
|
+
const span = (x1 - x0) * ch;
|
|
276
|
+
for (let yy = y0; yy < y1; yy++) {
|
|
277
|
+
const s = ((yy - rect.y) * w + (x0 - rect.x)) * ch;
|
|
278
|
+
const d = ((yy - oy) * TILE + (x0 - ox)) * ch;
|
|
279
|
+
if (canSubarray) t.set(src.subarray(s, s + span), d);
|
|
280
|
+
else for (let k = 0; k < span; k++) t[d + k] = src[s + k];
|
|
281
|
+
}
|
|
282
|
+
if (full) draft.replace(tx, ty, t);
|
|
283
|
+
}
|
|
284
|
+
return draft.commit();
|
|
285
|
+
}
|
|
286
|
+
function fillRect(surface, rect, bytes) {
|
|
287
|
+
const ch = surface.channels;
|
|
288
|
+
if (rect.width <= 0 || rect.height <= 0) return surface;
|
|
289
|
+
const tiles = new Map(surface.tiles);
|
|
290
|
+
const draft = new SurfaceDraft(surface);
|
|
291
|
+
const value = packPixel(bytes, 0, ch);
|
|
292
|
+
const isDefault = value === defaultPacked(ch, surface.defaultValue);
|
|
293
|
+
for (const { tx, ty } of tilesInRect(rect)) {
|
|
294
|
+
const ox = tx * TILE;
|
|
295
|
+
const oy = ty * TILE;
|
|
296
|
+
const x0 = Math.max(rect.x, ox);
|
|
297
|
+
const y0 = Math.max(rect.y, oy);
|
|
298
|
+
const x1 = Math.min(rect.x + rect.width, ox + TILE);
|
|
299
|
+
const y1 = Math.min(rect.y + rect.height, oy + TILE);
|
|
300
|
+
const key = tileKey(tx, ty);
|
|
301
|
+
if (x0 === ox && y0 === oy && x1 === ox + TILE && y1 === oy + TILE) {
|
|
302
|
+
if (isDefault) tiles.delete(key);
|
|
303
|
+
else tiles.set(key, uniformTile(ch, value));
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
const t = draft.tile(tx, ty);
|
|
307
|
+
for (let yy = y0; yy < y1; yy++) {
|
|
308
|
+
let d = ((yy - oy) * TILE + (x0 - ox)) * ch;
|
|
309
|
+
for (let xx = x0; xx < x1; xx++, d += ch) for (let c = 0; c < ch; c++) t[d + c] = bytes[c];
|
|
310
|
+
}
|
|
311
|
+
const tile = finishTile(t, ch, surface.defaultValue);
|
|
312
|
+
if (tile) tiles.set(key, tile);
|
|
313
|
+
else tiles.delete(key);
|
|
314
|
+
}
|
|
315
|
+
return new TiledSurface(ch, tiles, surface.defaultValue);
|
|
316
|
+
}
|
|
317
|
+
function translateSurface(surface, dx, dy) {
|
|
318
|
+
dx = Math.round(dx);
|
|
319
|
+
dy = Math.round(dy);
|
|
320
|
+
if (!dx && !dy) return surface;
|
|
321
|
+
if (dx % TILE === 0 && dy % TILE === 0) {
|
|
322
|
+
const tiles = /* @__PURE__ */ new Map();
|
|
323
|
+
for (const [key, tile] of surface.tiles) {
|
|
324
|
+
const { tx, ty } = tileCoord(key);
|
|
325
|
+
tiles.set(tileKey(tx + dx / TILE, ty + dy / TILE), tile);
|
|
326
|
+
}
|
|
327
|
+
return new TiledSurface(surface.channels, tiles, surface.defaultValue);
|
|
328
|
+
}
|
|
329
|
+
const b = surface.bounds;
|
|
330
|
+
if (!b) return surface;
|
|
331
|
+
const data = readRegion(surface, b);
|
|
332
|
+
return writeRegion(emptySurface(surface.channels, surface.defaultValue), { ...b, x: b.x + dx, y: b.y + dy }, data);
|
|
333
|
+
}
|
|
334
|
+
function surfaceBytes(surface) {
|
|
335
|
+
let n = 0;
|
|
336
|
+
for (const tile of surface.tiles.values()) if (tile.uniform === null) n += tile.data.byteLength;
|
|
337
|
+
return n;
|
|
338
|
+
}
|
|
339
|
+
function collectTiles(surface, into) {
|
|
340
|
+
for (const tile of surface.tiles.values()) if (tile.uniform === null) into.add(tile);
|
|
341
|
+
}
|
|
342
|
+
function surfacesEqual(a, b) {
|
|
343
|
+
if (a === b) return true;
|
|
344
|
+
if (a.channels !== b.channels || a.defaultValue !== b.defaultValue || a.tiles.size !== b.tiles.size) return false;
|
|
345
|
+
for (const [key, ta] of a.tiles) {
|
|
346
|
+
const tb = b.tiles.get(key);
|
|
347
|
+
if (!tb) return false;
|
|
348
|
+
if (ta === tb) continue;
|
|
349
|
+
if (ta.uniform !== null || tb.uniform !== null) {
|
|
350
|
+
if (ta.uniform !== tb.uniform) return false;
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
const x = ta.data;
|
|
354
|
+
const y = tb.data;
|
|
355
|
+
for (let i = 0; i < x.length; i++) if (x[i] !== y[i]) return false;
|
|
356
|
+
}
|
|
357
|
+
return true;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// src/raster/blend.ts
|
|
361
|
+
var BLEND_MODES = [
|
|
362
|
+
{ id: "normal", label: "Normal", canvas: "source-over" },
|
|
363
|
+
{ id: "darken", label: "Darken", canvas: "darken" },
|
|
364
|
+
{ id: "multiply", label: "Multiply", canvas: "multiply" },
|
|
365
|
+
{ id: "color-burn", label: "Color Burn", canvas: "color-burn" },
|
|
366
|
+
{ id: "linear-burn", label: "Linear Burn", canvas: null },
|
|
367
|
+
{ id: "darker-color", label: "Darker Color", canvas: null },
|
|
368
|
+
{ id: "lighten", label: "Lighten", canvas: "lighten" },
|
|
369
|
+
{ id: "screen", label: "Screen", canvas: "screen" },
|
|
370
|
+
{ id: "color-dodge", label: "Color Dodge", canvas: "color-dodge" },
|
|
371
|
+
{ id: "linear-dodge", label: "Linear Dodge (Add)", canvas: null },
|
|
372
|
+
{ id: "lighter-color", label: "Lighter Color", canvas: null },
|
|
373
|
+
{ id: "overlay", label: "Overlay", canvas: "overlay" },
|
|
374
|
+
// Canvas soft-light is the W3C formula, not Photoshop's: use the scalar path.
|
|
375
|
+
{ id: "soft-light", label: "Soft Light", canvas: null },
|
|
376
|
+
{ id: "hard-light", label: "Hard Light", canvas: "hard-light" },
|
|
377
|
+
{ id: "vivid-light", label: "Vivid Light", canvas: null },
|
|
378
|
+
{ id: "linear-light", label: "Linear Light", canvas: null },
|
|
379
|
+
{ id: "pin-light", label: "Pin Light", canvas: null },
|
|
380
|
+
{ id: "hard-mix", label: "Hard Mix", canvas: null },
|
|
381
|
+
{ id: "difference", label: "Difference", canvas: "difference" },
|
|
382
|
+
{ id: "exclusion", label: "Exclusion", canvas: "exclusion" },
|
|
383
|
+
{ id: "subtract", label: "Subtract", canvas: null },
|
|
384
|
+
{ id: "divide", label: "Divide", canvas: null },
|
|
385
|
+
{ id: "hue", label: "Hue", canvas: "hue" },
|
|
386
|
+
{ id: "saturation", label: "Saturation", canvas: "saturation" },
|
|
387
|
+
{ id: "color", label: "Color", canvas: "color" },
|
|
388
|
+
{ id: "luminosity", label: "Luminosity", canvas: "luminosity" }
|
|
389
|
+
];
|
|
390
|
+
var BLEND_INFO = new Map(BLEND_MODES.map((m) => [m.id, m]));
|
|
391
|
+
function isBlendMode(value) {
|
|
392
|
+
return typeof value === "string" && BLEND_INFO.has(value);
|
|
393
|
+
}
|
|
394
|
+
function normalizeBlendMode(value) {
|
|
395
|
+
return isBlendMode(value) ? value : "normal";
|
|
396
|
+
}
|
|
397
|
+
function canvasCompositeOperation(mode) {
|
|
398
|
+
return BLEND_INFO.get(mode ?? "normal")?.canvas ?? null;
|
|
399
|
+
}
|
|
400
|
+
function isSeparableBlendMode(mode) {
|
|
401
|
+
return !(mode === "hue" || mode === "saturation" || mode === "color" || mode === "luminosity" || mode === "darker-color" || mode === "lighter-color");
|
|
402
|
+
}
|
|
403
|
+
function colorBurn(cb, cs) {
|
|
404
|
+
if (cb >= 1) return 1;
|
|
405
|
+
if (cs <= 0) return 0;
|
|
406
|
+
return 1 - Math.min(1, (1 - cb) / cs);
|
|
407
|
+
}
|
|
408
|
+
function colorDodge(cb, cs) {
|
|
409
|
+
if (cb <= 0) return 0;
|
|
410
|
+
if (cs >= 1) return 1;
|
|
411
|
+
return Math.min(1, cb / (1 - cs));
|
|
412
|
+
}
|
|
413
|
+
function hardLight(cb, cs) {
|
|
414
|
+
if (cs <= 0.5) return cb * 2 * cs;
|
|
415
|
+
const s = 2 * cs - 1;
|
|
416
|
+
return cb + s - cb * s;
|
|
417
|
+
}
|
|
418
|
+
function softLight(cb, cs) {
|
|
419
|
+
return cs <= 0.5 ? 2 * cb * cs + cb * cb * (1 - 2 * cs) : 2 * cb * (1 - cs) + Math.sqrt(Math.max(0, cb)) * (2 * cs - 1);
|
|
420
|
+
}
|
|
421
|
+
function vividLightHardMix(cb, cs) {
|
|
422
|
+
const EDGE = 1e-4;
|
|
423
|
+
if (cs <= 0.5) {
|
|
424
|
+
if (cb >= 1 - EDGE) return 1;
|
|
425
|
+
if (cs <= 0) return 0;
|
|
426
|
+
return 1 - Math.min(1, (1 - cb) / (2 * cs));
|
|
427
|
+
}
|
|
428
|
+
if (cb <= EDGE) return 0;
|
|
429
|
+
if (cs >= 1) return 1;
|
|
430
|
+
return Math.min(1, cb / (2 * (1 - cs)));
|
|
431
|
+
}
|
|
432
|
+
function blendChannel(mode, cb, cs) {
|
|
433
|
+
switch (mode) {
|
|
434
|
+
case "normal":
|
|
435
|
+
return cs;
|
|
436
|
+
case "darken":
|
|
437
|
+
return Math.min(cb, cs);
|
|
438
|
+
case "multiply":
|
|
439
|
+
return cb * cs;
|
|
440
|
+
case "color-burn":
|
|
441
|
+
return colorBurn(cb, cs);
|
|
442
|
+
case "linear-burn":
|
|
443
|
+
return Math.max(0, cb + cs - 1);
|
|
444
|
+
case "lighten":
|
|
445
|
+
return Math.max(cb, cs);
|
|
446
|
+
case "screen":
|
|
447
|
+
return cb + cs - cb * cs;
|
|
448
|
+
case "color-dodge":
|
|
449
|
+
return colorDodge(cb, cs);
|
|
450
|
+
case "linear-dodge":
|
|
451
|
+
return Math.min(1, cb + cs);
|
|
452
|
+
case "overlay":
|
|
453
|
+
return hardLight(cs, cb);
|
|
454
|
+
case "soft-light":
|
|
455
|
+
return softLight(cb, cs);
|
|
456
|
+
case "hard-light":
|
|
457
|
+
return hardLight(cb, cs);
|
|
458
|
+
case "vivid-light":
|
|
459
|
+
if (cs <= 0) return 0;
|
|
460
|
+
if (cs >= 1) return 1;
|
|
461
|
+
return cs <= 0.5 ? colorBurn(cb, 2 * cs) : colorDodge(cb, 2 * (cs - 0.5));
|
|
462
|
+
case "linear-light":
|
|
463
|
+
return Math.min(1, Math.max(0, cb + 2 * cs - 1));
|
|
464
|
+
case "pin-light":
|
|
465
|
+
return cs <= 0.5 ? Math.min(cb, 2 * cs) : Math.max(cb, 2 * cs - 1);
|
|
466
|
+
case "hard-mix":
|
|
467
|
+
return vividLightHardMix(cb, cs) >= 0.5 - 1e-6 ? 1 : 0;
|
|
468
|
+
case "difference":
|
|
469
|
+
return Math.abs(cb - cs);
|
|
470
|
+
case "exclusion":
|
|
471
|
+
return cb + cs - 2 * cb * cs;
|
|
472
|
+
case "subtract":
|
|
473
|
+
return Math.max(0, cb - cs);
|
|
474
|
+
case "divide":
|
|
475
|
+
if (cs <= 0) return cb <= 0 ? 0 : 1;
|
|
476
|
+
return Math.min(1, cb / cs);
|
|
477
|
+
default:
|
|
478
|
+
return cs;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
function blendLum(c) {
|
|
482
|
+
return 0.3 * c[0] + 0.59 * c[1] + 0.11 * c[2];
|
|
483
|
+
}
|
|
484
|
+
function clipColor(c) {
|
|
485
|
+
const l = blendLum(c);
|
|
486
|
+
const n = Math.min(c[0], c[1], c[2]);
|
|
487
|
+
const x = Math.max(c[0], c[1], c[2]);
|
|
488
|
+
let out = [c[0], c[1], c[2]];
|
|
489
|
+
if (n < 0) {
|
|
490
|
+
const d = l - n;
|
|
491
|
+
out = out.map((v) => l > 0 && d > 0 ? l + (v - l) * l / d : l);
|
|
492
|
+
}
|
|
493
|
+
if (x > 1) {
|
|
494
|
+
const d = x - l;
|
|
495
|
+
out = out.map((v) => l < 1 && d > 0 ? l + (v - l) * (1 - l) / d : l);
|
|
496
|
+
}
|
|
497
|
+
return out;
|
|
498
|
+
}
|
|
499
|
+
function setLum(c, l) {
|
|
500
|
+
const d = l - blendLum(c);
|
|
501
|
+
return clipColor([c[0] + d, c[1] + d, c[2] + d]);
|
|
502
|
+
}
|
|
503
|
+
function sat(c) {
|
|
504
|
+
return Math.max(c[0], c[1], c[2]) - Math.min(c[0], c[1], c[2]);
|
|
505
|
+
}
|
|
506
|
+
function setSat(c, s) {
|
|
507
|
+
const idx = [0, 1, 2].sort((a, b) => c[a] - c[b]);
|
|
508
|
+
const [imin, imid, imax] = idx;
|
|
509
|
+
const out = [0, 0, 0];
|
|
510
|
+
if (c[imax] > c[imin]) {
|
|
511
|
+
out[imid] = (c[imid] - c[imin]) * s / (c[imax] - c[imin]);
|
|
512
|
+
out[imax] = s;
|
|
513
|
+
}
|
|
514
|
+
out[imin] = 0;
|
|
515
|
+
return out;
|
|
516
|
+
}
|
|
517
|
+
function blendRgb(mode, cb, cs) {
|
|
518
|
+
switch (mode) {
|
|
519
|
+
case "hue":
|
|
520
|
+
return setLum(setSat(cs, sat(cb)), blendLum(cb));
|
|
521
|
+
case "saturation":
|
|
522
|
+
return setLum(setSat(cb, sat(cs)), blendLum(cb));
|
|
523
|
+
case "color":
|
|
524
|
+
return setLum(cs, blendLum(cb));
|
|
525
|
+
case "luminosity":
|
|
526
|
+
return setLum(cb, blendLum(cs));
|
|
527
|
+
case "darker-color":
|
|
528
|
+
return blendLum(cs) < blendLum(cb) ? [cs[0], cs[1], cs[2]] : [cb[0], cb[1], cb[2]];
|
|
529
|
+
case "lighter-color":
|
|
530
|
+
return blendLum(cs) > blendLum(cb) ? [cs[0], cs[1], cs[2]] : [cb[0], cb[1], cb[2]];
|
|
531
|
+
default:
|
|
532
|
+
return [blendChannel(mode, cb[0], cs[0]), blendChannel(mode, cb[1], cs[1]), blendChannel(mode, cb[2], cs[2])];
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
function blendPixel(mode, backdrop, source, alpha = 1) {
|
|
536
|
+
const ab = backdrop[3] ?? 1;
|
|
537
|
+
const as = (source[3] ?? 1) * alpha;
|
|
538
|
+
if (!(as > 0)) return [backdrop[0], backdrop[1], backdrop[2], ab];
|
|
539
|
+
const b = blendRgb(mode, backdrop, source);
|
|
540
|
+
const ao = as + ab * (1 - as);
|
|
541
|
+
if (ao <= 0) return [0, 0, 0, 0];
|
|
542
|
+
const out = [0, 0, 0, ao];
|
|
543
|
+
for (let i = 0; i < 3; i++) {
|
|
544
|
+
out[i] = ((1 - as) * ab * backdrop[i] + (1 - ab) * as * source[i] + as * ab * b[i]) / ao;
|
|
545
|
+
}
|
|
546
|
+
return out;
|
|
547
|
+
}
|
|
548
|
+
function blendImageData(backdrop, source, mode, alpha = 1) {
|
|
549
|
+
const d = backdrop.data;
|
|
550
|
+
const s = source.data;
|
|
551
|
+
const len = Math.min(d.length, s.length);
|
|
552
|
+
const cb = [0, 0, 0, 0];
|
|
553
|
+
const cs = [0, 0, 0, 0];
|
|
554
|
+
for (let i = 0; i < len; i += 4) {
|
|
555
|
+
if (s[i + 3] === 0) continue;
|
|
556
|
+
for (let k = 0; k < 4; k++) {
|
|
557
|
+
cb[k] = d[i + k] / 255;
|
|
558
|
+
cs[k] = s[i + k] / 255;
|
|
559
|
+
}
|
|
560
|
+
const o = blendPixel(mode, cb, cs, alpha);
|
|
561
|
+
d[i] = Math.round(o[0] * 255);
|
|
562
|
+
d[i + 1] = Math.round(o[1] * 255);
|
|
563
|
+
d[i + 2] = Math.round(o[2] * 255);
|
|
564
|
+
d[i + 3] = Math.round(o[3] * 255);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
export {
|
|
569
|
+
TILE,
|
|
570
|
+
TILE_SHIFT,
|
|
571
|
+
tileKey,
|
|
572
|
+
tileCoord,
|
|
573
|
+
tileRect,
|
|
574
|
+
TiledSurface,
|
|
575
|
+
uniformTile,
|
|
576
|
+
packRgba,
|
|
577
|
+
finishTile,
|
|
578
|
+
emptySurface,
|
|
579
|
+
solidSurface,
|
|
580
|
+
surfaceFromPixels,
|
|
581
|
+
tilesInRect,
|
|
582
|
+
readRegion,
|
|
583
|
+
readPixel,
|
|
584
|
+
contentBounds,
|
|
585
|
+
SurfaceDraft,
|
|
586
|
+
writeRegion,
|
|
587
|
+
fillRect,
|
|
588
|
+
translateSurface,
|
|
589
|
+
surfaceBytes,
|
|
590
|
+
collectTiles,
|
|
591
|
+
surfacesEqual,
|
|
592
|
+
BLEND_MODES,
|
|
593
|
+
isBlendMode,
|
|
594
|
+
normalizeBlendMode,
|
|
595
|
+
canvasCompositeOperation,
|
|
596
|
+
isSeparableBlendMode,
|
|
597
|
+
blendChannel,
|
|
598
|
+
blendLum,
|
|
599
|
+
setLum,
|
|
600
|
+
blendRgb,
|
|
601
|
+
blendPixel,
|
|
602
|
+
blendImageData
|
|
603
|
+
};
|
|
604
|
+
//# sourceMappingURL=chunk-D4ZCU72J.js.map
|