@nodetool-ai/image-editor 0.8.4 → 0.8.6
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/README.md +39 -0
- package/dist/.nodetool-build-stamp +1 -1
- package/dist/psd/encoder.d.ts +63 -0
- package/dist/psd/encoder.d.ts.map +1 -0
- package/dist/psd/encoder.js +392 -0
- package/dist/psd/encoder.js.map +1 -0
- package/dist/psd/index.d.ts +8 -0
- package/dist/psd/index.d.ts.map +1 -0
- package/dist/psd/index.js +8 -0
- package/dist/psd/index.js.map +1 -0
- package/dist/psd/sketch.d.ts +59 -0
- package/dist/psd/sketch.d.ts.map +1 -0
- package/dist/psd/sketch.js +120 -0
- package/dist/psd/sketch.js.map +1 -0
- package/dist/render/compose.d.ts +59 -0
- package/dist/render/compose.d.ts.map +1 -0
- package/dist/render/compose.js +231 -0
- package/dist/render/compose.js.map +1 -0
- package/dist/render/document.d.ts +79 -0
- package/dist/render/document.d.ts.map +1 -0
- package/dist/render/document.js +211 -0
- package/dist/render/document.js.map +1 -0
- package/dist/render/index.d.ts +12 -0
- package/dist/render/index.d.ts.map +1 -0
- package/dist/render/index.js +12 -0
- package/dist/render/index.js.map +1 -0
- package/dist/render/normalize.d.ts +31 -0
- package/dist/render/normalize.d.ts.map +1 -0
- package/dist/render/normalize.js +228 -0
- package/dist/render/normalize.js.map +1 -0
- package/dist/render/types.d.ts +127 -0
- package/dist/render/types.d.ts.map +1 -0
- package/dist/render/types.js +10 -0
- package/dist/render/types.js.map +1 -0
- package/package.json +16 -1
package/README.md
CHANGED
|
@@ -79,6 +79,45 @@ ops (`fillRegion`, `adjustImage`, `pickPixel`, selection builders) need no
|
|
|
79
79
|
canvas. Drawing ops (`drawGradient`, `drawShape`, `transformRaster`, `cropRaster`)
|
|
80
80
|
take a `RasterContext2D` — a DOM or skia 2D context.
|
|
81
81
|
|
|
82
|
+
## Compositing
|
|
83
|
+
|
|
84
|
+
`@nodetool-ai/image-editor/render.js` holds the rules that turn a sketch
|
|
85
|
+
document into pixels: load-time normalization of transforms and effects
|
|
86
|
+
(`normalizeLayerTransform`, `normalizeLayerEffects`), visibility through groups
|
|
87
|
+
and group opacity, where a layer raster lands, affine and quad transforms, and
|
|
88
|
+
the CSS filter chain behind layer effects. The web editor's Canvas2D runtime
|
|
89
|
+
composites through these functions.
|
|
90
|
+
|
|
91
|
+
`renderSketchDocument(document, host, options)` composites a whole persisted
|
|
92
|
+
document in one pass, as the editor's PNG export does. The host supplies
|
|
93
|
+
surfaces, image decoding and the blend table, so the module needs no canvas
|
|
94
|
+
library and no storage access:
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
import { createCanvas, loadImage } from "@napi-rs/canvas";
|
|
98
|
+
import { blendModeToCanvasOp } from "@nodetool-ai/gpu";
|
|
99
|
+
import { renderSketchDocument } from "@nodetool-ai/image-editor/render.js";
|
|
100
|
+
|
|
101
|
+
const result = await renderSketchDocument(imageDocument, {
|
|
102
|
+
createSurface: (w, h) => createCanvas(w, h),
|
|
103
|
+
loadImage: (source) => loadImage(decodeDataUrl(source)),
|
|
104
|
+
compositeOperation: blendModeToCanvasOp,
|
|
105
|
+
supportsFilter: true
|
|
106
|
+
}, { width: 512 });
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
`options.layerId` renders one layer without its opacity or blend mode, and
|
|
110
|
+
each entry of `result.layers` reports whether that layer was drawn.
|
|
111
|
+
`@nodetool-ai/agents/sketch-render` is the complete Node host behind
|
|
112
|
+
`nodetool sketch render` and the `render_sketch` tool.
|
|
113
|
+
|
|
114
|
+
## PSD
|
|
115
|
+
|
|
116
|
+
`@nodetool-ai/image-editor/psd.js` writes layered Photoshop files: `encodePsd`
|
|
117
|
+
for a layer tree, and `encodeSketchPsd` for a sketch, given each layer's pixels
|
|
118
|
+
at document size and the flattened composite. The editor's Export PSD and the
|
|
119
|
+
headless renderer both use it.
|
|
120
|
+
|
|
82
121
|
## Usage
|
|
83
122
|
|
|
84
123
|
```ts
|
|
@@ -1 +1 @@
|
|
|
1
|
-
2026-10-
|
|
1
|
+
2026-10-18T07:13:36.093Z
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* psdEncoder – minimal layered Photoshop (PSD, version 1) writer.
|
|
3
|
+
* `decodePsd` in `psdDecoder.ts` reads files back.
|
|
4
|
+
*
|
|
5
|
+
* Writes 8-bit RGB documents with:
|
|
6
|
+
* - pixel layers carrying name, visibility, opacity, blend mode, position and
|
|
7
|
+
* transparency lock,
|
|
8
|
+
* - layer groups (section dividers, pass-through blending),
|
|
9
|
+
* - a flattened RGBA composite, so viewers that ignore layers still show the
|
|
10
|
+
* image.
|
|
11
|
+
*
|
|
12
|
+
* Channel data is PackBits (RLE) compressed. Layers are given bottom to top,
|
|
13
|
+
* the order PSD stores them in.
|
|
14
|
+
*
|
|
15
|
+
* Format reference: Adobe Photoshop File Formats Specification,
|
|
16
|
+
* "Layer and Mask Information Section".
|
|
17
|
+
*/
|
|
18
|
+
import type { BlendMode } from "@nodetool-ai/protocol/blend-modes";
|
|
19
|
+
/** PSD (version 1) caps each dimension at 30,000 pixels. */
|
|
20
|
+
export declare const PSD_MAX_DIMENSION = 30000;
|
|
21
|
+
export interface PsdPixelLayer {
|
|
22
|
+
kind: "layer";
|
|
23
|
+
name: string;
|
|
24
|
+
visible: boolean;
|
|
25
|
+
/** 0–1. */
|
|
26
|
+
opacity: number;
|
|
27
|
+
blendMode: BlendMode;
|
|
28
|
+
/** When true, painting in Photoshop keeps the layer's transparency. */
|
|
29
|
+
transparencyLocked?: boolean;
|
|
30
|
+
/** Document-space position of the pixel rectangle. */
|
|
31
|
+
left: number;
|
|
32
|
+
top: number;
|
|
33
|
+
width: number;
|
|
34
|
+
height: number;
|
|
35
|
+
/** Straight (non-premultiplied) RGBA, `width * height * 4` bytes. */
|
|
36
|
+
pixels: Uint8ClampedArray;
|
|
37
|
+
}
|
|
38
|
+
export interface PsdGroup {
|
|
39
|
+
kind: "group";
|
|
40
|
+
name: string;
|
|
41
|
+
visible: boolean;
|
|
42
|
+
/** 0–1. */
|
|
43
|
+
opacity: number;
|
|
44
|
+
/** Collapsed in Photoshop's layers panel. */
|
|
45
|
+
collapsed: boolean;
|
|
46
|
+
/** Bottom to top. */
|
|
47
|
+
children: PsdNode[];
|
|
48
|
+
}
|
|
49
|
+
export type PsdNode = PsdPixelLayer | PsdGroup;
|
|
50
|
+
export interface PsdDocument {
|
|
51
|
+
width: number;
|
|
52
|
+
height: number;
|
|
53
|
+
/** Flattened straight RGBA, `width * height * 4` bytes. */
|
|
54
|
+
composite: Uint8ClampedArray;
|
|
55
|
+
/** Bottom to top. */
|
|
56
|
+
layers: PsdNode[];
|
|
57
|
+
}
|
|
58
|
+
export declare function psdBlendKey(mode: BlendMode): string;
|
|
59
|
+
/** The sketch blend mode for a PSD blend key, or null when it has none. */
|
|
60
|
+
export declare function blendModeFromPsdKey(key: string): BlendMode | null;
|
|
61
|
+
/** Encode a layered PSD file. */
|
|
62
|
+
export declare function encodePsd(doc: PsdDocument): Uint8Array<ArrayBuffer>;
|
|
63
|
+
//# sourceMappingURL=encoder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encoder.d.ts","sourceRoot":"","sources":["../../src/psd/encoder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAC;AAEnE,4DAA4D;AAC5D,eAAO,MAAM,iBAAiB,QAAQ,CAAC;AAEvC,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,OAAO,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,WAAW;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,SAAS,CAAC;IACrB,uEAAuE;IACvE,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,sDAAsD;IACtD,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,qEAAqE;IACrE,MAAM,EAAE,iBAAiB,CAAC;CAC3B;AAED,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,OAAO,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,WAAW;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,6CAA6C;IAC7C,SAAS,EAAE,OAAO,CAAC;IACnB,qBAAqB;IACrB,QAAQ,EAAE,OAAO,EAAE,CAAC;CACrB;AAED,MAAM,MAAM,OAAO,GAAG,aAAa,GAAG,QAAQ,CAAC;AAE/C,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,2DAA2D;IAC3D,SAAS,EAAE,iBAAiB,CAAC;IAC7B,qBAAqB;IACrB,MAAM,EAAE,OAAO,EAAE,CAAC;CACnB;AAkBD,wBAAgB,WAAW,CAAC,IAAI,EAAE,SAAS,GAAG,MAAM,CAEnD;AAED,2EAA2E;AAC3E,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAQjE;AAsTD,iCAAiC;AACjC,wBAAgB,SAAS,CAAC,GAAG,EAAE,WAAW,GAAG,UAAU,CAAC,WAAW,CAAC,CAoHnE"}
|
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* psdEncoder – minimal layered Photoshop (PSD, version 1) writer.
|
|
3
|
+
* `decodePsd` in `psdDecoder.ts` reads files back.
|
|
4
|
+
*
|
|
5
|
+
* Writes 8-bit RGB documents with:
|
|
6
|
+
* - pixel layers carrying name, visibility, opacity, blend mode, position and
|
|
7
|
+
* transparency lock,
|
|
8
|
+
* - layer groups (section dividers, pass-through blending),
|
|
9
|
+
* - a flattened RGBA composite, so viewers that ignore layers still show the
|
|
10
|
+
* image.
|
|
11
|
+
*
|
|
12
|
+
* Channel data is PackBits (RLE) compressed. Layers are given bottom to top,
|
|
13
|
+
* the order PSD stores them in.
|
|
14
|
+
*
|
|
15
|
+
* Format reference: Adobe Photoshop File Formats Specification,
|
|
16
|
+
* "Layer and Mask Information Section".
|
|
17
|
+
*/
|
|
18
|
+
/** PSD (version 1) caps each dimension at 30,000 pixels. */
|
|
19
|
+
export const PSD_MAX_DIMENSION = 30000;
|
|
20
|
+
const BLEND_KEYS = {
|
|
21
|
+
normal: "norm",
|
|
22
|
+
multiply: "mul ",
|
|
23
|
+
screen: "scrn",
|
|
24
|
+
overlay: "over",
|
|
25
|
+
darken: "dark",
|
|
26
|
+
lighten: "lite",
|
|
27
|
+
"color-dodge": "div ",
|
|
28
|
+
"color-burn": "idiv",
|
|
29
|
+
"hard-light": "hLit",
|
|
30
|
+
"soft-light": "sLit",
|
|
31
|
+
difference: "diff",
|
|
32
|
+
exclusion: "smud",
|
|
33
|
+
add: "lddg"
|
|
34
|
+
};
|
|
35
|
+
export function psdBlendKey(mode) {
|
|
36
|
+
return BLEND_KEYS[mode] ?? "norm";
|
|
37
|
+
}
|
|
38
|
+
/** The sketch blend mode for a PSD blend key, or null when it has none. */
|
|
39
|
+
export function blendModeFromPsdKey(key) {
|
|
40
|
+
for (const [mode, psdKey] of Object.entries(BLEND_KEYS)) {
|
|
41
|
+
if (psdKey === key) {
|
|
42
|
+
// SAFETY: BLEND_KEYS is keyed by BlendMode, so every entry's key is one.
|
|
43
|
+
return mode;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
// ─── Byte writer ─────────────────────────────────────────────────────────────
|
|
49
|
+
class ByteWriter {
|
|
50
|
+
buffer = new Uint8Array(1 << 16);
|
|
51
|
+
view = new DataView(this.buffer.buffer);
|
|
52
|
+
length = 0;
|
|
53
|
+
ensure(extra) {
|
|
54
|
+
const needed = this.length + extra;
|
|
55
|
+
if (needed <= this.buffer.length) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
let size = this.buffer.length * 2;
|
|
59
|
+
while (size < needed) {
|
|
60
|
+
size *= 2;
|
|
61
|
+
}
|
|
62
|
+
const next = new Uint8Array(size);
|
|
63
|
+
next.set(this.buffer.subarray(0, this.length));
|
|
64
|
+
this.buffer = next;
|
|
65
|
+
this.view = new DataView(next.buffer);
|
|
66
|
+
}
|
|
67
|
+
u8(value) {
|
|
68
|
+
this.ensure(1);
|
|
69
|
+
this.buffer[this.length++] = value;
|
|
70
|
+
}
|
|
71
|
+
u16(value) {
|
|
72
|
+
this.ensure(2);
|
|
73
|
+
this.view.setUint16(this.length, value);
|
|
74
|
+
this.length += 2;
|
|
75
|
+
}
|
|
76
|
+
i16(value) {
|
|
77
|
+
this.ensure(2);
|
|
78
|
+
this.view.setInt16(this.length, value);
|
|
79
|
+
this.length += 2;
|
|
80
|
+
}
|
|
81
|
+
u32(value) {
|
|
82
|
+
this.ensure(4);
|
|
83
|
+
this.view.setUint32(this.length, value);
|
|
84
|
+
this.length += 4;
|
|
85
|
+
}
|
|
86
|
+
i32(value) {
|
|
87
|
+
this.ensure(4);
|
|
88
|
+
this.view.setInt32(this.length, value);
|
|
89
|
+
this.length += 4;
|
|
90
|
+
}
|
|
91
|
+
ascii(text) {
|
|
92
|
+
for (let i = 0; i < text.length; i++) {
|
|
93
|
+
this.u8(text.charCodeAt(i) & 0x7f);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
bytes(data) {
|
|
97
|
+
this.ensure(data.length);
|
|
98
|
+
this.buffer.set(data, this.length);
|
|
99
|
+
this.length += data.length;
|
|
100
|
+
}
|
|
101
|
+
zeros(count) {
|
|
102
|
+
this.ensure(count);
|
|
103
|
+
this.buffer.fill(0, this.length, this.length + count);
|
|
104
|
+
this.length += count;
|
|
105
|
+
}
|
|
106
|
+
/** Reserve a u32 length field; returns its offset for {@link patchLength}. */
|
|
107
|
+
reserveLength() {
|
|
108
|
+
const offset = this.length;
|
|
109
|
+
this.u32(0);
|
|
110
|
+
return offset;
|
|
111
|
+
}
|
|
112
|
+
/** Write the byte count since the reserved field into it. */
|
|
113
|
+
patchLength(offset) {
|
|
114
|
+
this.view.setUint32(offset, this.length - offset - 4);
|
|
115
|
+
}
|
|
116
|
+
/** Pad with zeros until the length since `start` is a multiple of `n`. */
|
|
117
|
+
padFrom(start, n) {
|
|
118
|
+
const rem = (this.length - start) % n;
|
|
119
|
+
if (rem !== 0) {
|
|
120
|
+
this.zeros(n - rem);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
result() {
|
|
124
|
+
return this.buffer.slice(0, this.length);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
// ─── PackBits ────────────────────────────────────────────────────────────────
|
|
128
|
+
/**
|
|
129
|
+
* PackBits-encode one row of one channel. `src` is interleaved RGBA, so the
|
|
130
|
+
* row's samples are `src[start + i * 4]`. Returns the encoded byte count.
|
|
131
|
+
*/
|
|
132
|
+
function packBitsRow(src, start, count, out, outOffset) {
|
|
133
|
+
let o = outOffset;
|
|
134
|
+
let i = 0;
|
|
135
|
+
const at = (k) => src[start + k * 4];
|
|
136
|
+
while (i < count) {
|
|
137
|
+
let run = 1;
|
|
138
|
+
while (i + run < count && run < 128 && at(i + run) === at(i)) {
|
|
139
|
+
run++;
|
|
140
|
+
}
|
|
141
|
+
if (run >= 3) {
|
|
142
|
+
out[o++] = 257 - run;
|
|
143
|
+
out[o++] = at(i);
|
|
144
|
+
i += run;
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
let end = i;
|
|
148
|
+
while (end < count && end - i < 128) {
|
|
149
|
+
if (end + 2 < count &&
|
|
150
|
+
at(end) === at(end + 1) &&
|
|
151
|
+
at(end) === at(end + 2)) {
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
end++;
|
|
155
|
+
}
|
|
156
|
+
out[o++] = end - i - 1;
|
|
157
|
+
for (let k = i; k < end; k++) {
|
|
158
|
+
out[o++] = at(k);
|
|
159
|
+
}
|
|
160
|
+
i = end;
|
|
161
|
+
}
|
|
162
|
+
return o - outOffset;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* RLE-compress one channel (`channelIndex` 0–3 of interleaved RGBA) into
|
|
166
|
+
* per-row byte counts followed by the packed rows.
|
|
167
|
+
*/
|
|
168
|
+
function encodeChannel(pixels, width, height, channelIndex) {
|
|
169
|
+
const rowCounts = new Uint16Array(height);
|
|
170
|
+
const maxRow = width + Math.ceil(width / 128);
|
|
171
|
+
const scratch = new Uint8Array(maxRow * height);
|
|
172
|
+
let length = 0;
|
|
173
|
+
for (let y = 0; y < height; y++) {
|
|
174
|
+
const n = packBitsRow(pixels, y * width * 4 + channelIndex, width, scratch, length);
|
|
175
|
+
rowCounts[y] = n;
|
|
176
|
+
length += n;
|
|
177
|
+
}
|
|
178
|
+
return { rowCounts, data: scratch.subarray(0, length) };
|
|
179
|
+
}
|
|
180
|
+
// ─── Layer records ───────────────────────────────────────────────────────────
|
|
181
|
+
/** PSD channel ids in the order each record lists them: alpha, R, G, B. */
|
|
182
|
+
const LAYER_CHANNELS = [
|
|
183
|
+
{ id: -1, rgbaIndex: 3 },
|
|
184
|
+
{ id: 0, rgbaIndex: 0 },
|
|
185
|
+
{ id: 1, rgbaIndex: 1 },
|
|
186
|
+
{ id: 2, rgbaIndex: 2 }
|
|
187
|
+
];
|
|
188
|
+
const SECTION_OPEN_FOLDER = 1;
|
|
189
|
+
const SECTION_CLOSED_FOLDER = 2;
|
|
190
|
+
const SECTION_DIVIDER = 3;
|
|
191
|
+
/** Flatten the tree into PSD record order (bottom to top, dividers first). */
|
|
192
|
+
function flattenNodes(nodes, out) {
|
|
193
|
+
for (const node of nodes) {
|
|
194
|
+
if (node.kind === "layer") {
|
|
195
|
+
out.push({
|
|
196
|
+
name: node.name,
|
|
197
|
+
visible: node.visible,
|
|
198
|
+
opacity: node.opacity,
|
|
199
|
+
blendKey: psdBlendKey(node.blendMode),
|
|
200
|
+
transparencyLocked: node.transparencyLocked ?? false,
|
|
201
|
+
left: node.left,
|
|
202
|
+
top: node.top,
|
|
203
|
+
width: node.width,
|
|
204
|
+
height: node.height,
|
|
205
|
+
pixels: node.width > 0 && node.height > 0 ? node.pixels : null
|
|
206
|
+
});
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
const empty = {
|
|
210
|
+
blendKey: "pass",
|
|
211
|
+
transparencyLocked: false,
|
|
212
|
+
left: 0,
|
|
213
|
+
top: 0,
|
|
214
|
+
width: 0,
|
|
215
|
+
height: 0,
|
|
216
|
+
pixels: null
|
|
217
|
+
};
|
|
218
|
+
out.push({
|
|
219
|
+
...empty,
|
|
220
|
+
name: "</Layer group>",
|
|
221
|
+
visible: true,
|
|
222
|
+
opacity: 1,
|
|
223
|
+
blendKey: "norm",
|
|
224
|
+
sectionType: SECTION_DIVIDER
|
|
225
|
+
});
|
|
226
|
+
flattenNodes(node.children, out);
|
|
227
|
+
out.push({
|
|
228
|
+
...empty,
|
|
229
|
+
name: node.name,
|
|
230
|
+
visible: node.visible,
|
|
231
|
+
opacity: node.opacity,
|
|
232
|
+
sectionType: node.collapsed ? SECTION_CLOSED_FOLDER : SECTION_OPEN_FOLDER
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
function toByte(opacity) {
|
|
237
|
+
return Math.round(Math.min(1, Math.max(0, opacity)) * 255);
|
|
238
|
+
}
|
|
239
|
+
function writePascalName(w, name) {
|
|
240
|
+
// Legacy name field: ASCII only; `luni` carries the full Unicode name.
|
|
241
|
+
const ascii = name.replace(/[^\x20-\x7e]/g, "?").slice(0, 255);
|
|
242
|
+
const start = w.length;
|
|
243
|
+
w.u8(ascii.length);
|
|
244
|
+
w.ascii(ascii);
|
|
245
|
+
w.padFrom(start, 4);
|
|
246
|
+
}
|
|
247
|
+
function writeUnicodeName(w, name) {
|
|
248
|
+
w.ascii("8BIM");
|
|
249
|
+
w.ascii("luni");
|
|
250
|
+
const lengthOffset = w.reserveLength();
|
|
251
|
+
const start = w.length;
|
|
252
|
+
w.u32(name.length);
|
|
253
|
+
for (let i = 0; i < name.length; i++) {
|
|
254
|
+
w.u16(name.charCodeAt(i));
|
|
255
|
+
}
|
|
256
|
+
w.padFrom(start, 4);
|
|
257
|
+
w.patchLength(lengthOffset);
|
|
258
|
+
}
|
|
259
|
+
function writeSectionDivider(w, record) {
|
|
260
|
+
if (record.sectionType === undefined) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
w.ascii("8BIM");
|
|
264
|
+
w.ascii("lsct");
|
|
265
|
+
if (record.sectionType === SECTION_DIVIDER) {
|
|
266
|
+
w.u32(4);
|
|
267
|
+
w.u32(record.sectionType);
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
w.u32(12);
|
|
271
|
+
w.u32(record.sectionType);
|
|
272
|
+
w.ascii("8BIM");
|
|
273
|
+
w.ascii(record.blendKey);
|
|
274
|
+
}
|
|
275
|
+
// ─── Document ────────────────────────────────────────────────────────────────
|
|
276
|
+
function validateDocument(doc) {
|
|
277
|
+
const { width, height } = doc;
|
|
278
|
+
if (!Number.isInteger(width) ||
|
|
279
|
+
!Number.isInteger(height) ||
|
|
280
|
+
width < 1 ||
|
|
281
|
+
height < 1 ||
|
|
282
|
+
width > PSD_MAX_DIMENSION ||
|
|
283
|
+
height > PSD_MAX_DIMENSION) {
|
|
284
|
+
throw new Error(`PSD export supports 1–${PSD_MAX_DIMENSION} px per side, got ${width}×${height}`);
|
|
285
|
+
}
|
|
286
|
+
if (doc.composite.length !== width * height * 4) {
|
|
287
|
+
throw new Error("PSD composite size does not match the document size");
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/** Encode a layered PSD file. */
|
|
291
|
+
export function encodePsd(doc) {
|
|
292
|
+
validateDocument(doc);
|
|
293
|
+
const records = [];
|
|
294
|
+
flattenNodes(doc.layers, records);
|
|
295
|
+
const channelData = records.map((record) => {
|
|
296
|
+
if (!record.pixels) {
|
|
297
|
+
return null;
|
|
298
|
+
}
|
|
299
|
+
if (record.pixels.length !== record.width * record.height * 4) {
|
|
300
|
+
throw new Error(`PSD layer "${record.name}" has a mismatched pixel buffer`);
|
|
301
|
+
}
|
|
302
|
+
const pixels = record.pixels;
|
|
303
|
+
return LAYER_CHANNELS.map((channel) => encodeChannel(pixels, record.width, record.height, channel.rgbaIndex));
|
|
304
|
+
});
|
|
305
|
+
const w = new ByteWriter();
|
|
306
|
+
// Header
|
|
307
|
+
w.ascii("8BPS");
|
|
308
|
+
w.u16(1);
|
|
309
|
+
w.zeros(6);
|
|
310
|
+
w.u16(4);
|
|
311
|
+
w.u32(doc.height);
|
|
312
|
+
w.u32(doc.width);
|
|
313
|
+
w.u16(8);
|
|
314
|
+
w.u16(3);
|
|
315
|
+
// Color mode data and image resources: none.
|
|
316
|
+
w.u32(0);
|
|
317
|
+
w.u32(0);
|
|
318
|
+
// Layer and mask information
|
|
319
|
+
const layerMaskOffset = w.reserveLength();
|
|
320
|
+
const layerInfoOffset = w.reserveLength();
|
|
321
|
+
const layerInfoStart = w.length;
|
|
322
|
+
// Negative count: the composite's first alpha channel is its transparency.
|
|
323
|
+
w.i16(records.length === 0 ? 0 : -records.length);
|
|
324
|
+
records.forEach((record, index) => {
|
|
325
|
+
const channels = channelData[index];
|
|
326
|
+
w.i32(record.top);
|
|
327
|
+
w.i32(record.left);
|
|
328
|
+
w.i32(record.top + (channels ? record.height : 0));
|
|
329
|
+
w.i32(record.left + (channels ? record.width : 0));
|
|
330
|
+
w.u16(LAYER_CHANNELS.length);
|
|
331
|
+
LAYER_CHANNELS.forEach((channel, c) => {
|
|
332
|
+
w.i16(channel.id);
|
|
333
|
+
const encoded = channels?.[c];
|
|
334
|
+
w.u32(encoded ? 2 + encoded.rowCounts.length * 2 + encoded.data.length : 2);
|
|
335
|
+
});
|
|
336
|
+
w.ascii("8BIM");
|
|
337
|
+
w.ascii(record.blendKey);
|
|
338
|
+
w.u8(toByte(record.opacity));
|
|
339
|
+
w.u8(0);
|
|
340
|
+
let flags = 0;
|
|
341
|
+
if (record.transparencyLocked) {
|
|
342
|
+
flags |= 0x01;
|
|
343
|
+
}
|
|
344
|
+
if (!record.visible) {
|
|
345
|
+
flags |= 0x02;
|
|
346
|
+
}
|
|
347
|
+
if (record.sectionType !== undefined) {
|
|
348
|
+
// Bit 3 says bit 4 is meaningful; bit 4 marks pixel data irrelevant.
|
|
349
|
+
flags |= 0x18;
|
|
350
|
+
}
|
|
351
|
+
w.u8(flags);
|
|
352
|
+
w.u8(0);
|
|
353
|
+
const extraOffset = w.reserveLength();
|
|
354
|
+
w.u32(0); // layer mask data
|
|
355
|
+
w.u32(0); // blending ranges
|
|
356
|
+
writePascalName(w, record.name);
|
|
357
|
+
writeUnicodeName(w, record.name);
|
|
358
|
+
writeSectionDivider(w, record);
|
|
359
|
+
w.patchLength(extraOffset);
|
|
360
|
+
});
|
|
361
|
+
channelData.forEach((channels) => {
|
|
362
|
+
for (let c = 0; c < LAYER_CHANNELS.length; c++) {
|
|
363
|
+
const encoded = channels?.[c];
|
|
364
|
+
if (!encoded) {
|
|
365
|
+
w.u16(0);
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
w.u16(1);
|
|
369
|
+
for (const count of encoded.rowCounts) {
|
|
370
|
+
w.u16(count);
|
|
371
|
+
}
|
|
372
|
+
w.bytes(encoded.data);
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
w.padFrom(layerInfoStart, 2);
|
|
376
|
+
w.patchLength(layerInfoOffset);
|
|
377
|
+
w.u32(0); // global layer mask info
|
|
378
|
+
w.patchLength(layerMaskOffset);
|
|
379
|
+
// Image data: the flattened composite, channels R, G, B, A.
|
|
380
|
+
const composite = [0, 1, 2, 3].map((c) => encodeChannel(doc.composite, doc.width, doc.height, c));
|
|
381
|
+
w.u16(1);
|
|
382
|
+
for (const channel of composite) {
|
|
383
|
+
for (const count of channel.rowCounts) {
|
|
384
|
+
w.u16(count);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
for (const channel of composite) {
|
|
388
|
+
w.bytes(channel.data);
|
|
389
|
+
}
|
|
390
|
+
return w.result();
|
|
391
|
+
}
|
|
392
|
+
//# sourceMappingURL=encoder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encoder.js","sourceRoot":"","sources":["../../src/psd/encoder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAIH,4DAA4D;AAC5D,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,CAAC;AA2CvC,MAAM,UAAU,GAAG;IACjB,MAAM,EAAE,MAAM;IACd,QAAQ,EAAE,MAAM;IAChB,MAAM,EAAE,MAAM;IACd,OAAO,EAAE,MAAM;IACf,MAAM,EAAE,MAAM;IACd,OAAO,EAAE,MAAM;IACf,aAAa,EAAE,MAAM;IACrB,YAAY,EAAE,MAAM;IACpB,YAAY,EAAE,MAAM;IACpB,YAAY,EAAE,MAAM;IACpB,UAAU,EAAE,MAAM;IAClB,SAAS,EAAE,MAAM;IACjB,GAAG,EAAE,MAAM;CACwB,CAAC;AAEtC,MAAM,UAAU,WAAW,CAAC,IAAe;IACzC,OAAO,UAAU,CAAC,IAAI,CAAC,IAAI,MAAM,CAAC;AACpC,CAAC;AAED,2EAA2E;AAC3E,MAAM,UAAU,mBAAmB,CAAC,GAAW;IAC7C,KAAK,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC;QACxD,IAAI,MAAM,KAAK,GAAG,EAAE,CAAC;YACnB,yEAAyE;YACzE,OAAO,IAAiB,CAAC;QAC3B,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,gFAAgF;AAEhF,MAAM,UAAU;IACN,MAAM,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI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AAC;QACf,CAAC;IACH,CAAC;IACD,KAAK,MAAM,OAAO,IAAI,SAAS,EAAE,CAAC;QAChC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC;IAED,OAAO,CAAC,CAAC,MAAM,EAAE,CAAC;AACpB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @nodetool-ai/image-editor/psd — the layered PSD writer and the sketch →
|
|
3
|
+
* PSD layer tree. The editor's "Export PSD" and the headless renderer both
|
|
4
|
+
* encode through here.
|
|
5
|
+
*/
|
|
6
|
+
export * from "./encoder.js";
|
|
7
|
+
export * from "./sketch.js";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/psd/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @nodetool-ai/image-editor/psd — the layered PSD writer and the sketch →
|
|
3
|
+
* PSD layer tree. The editor's "Export PSD" and the headless renderer both
|
|
4
|
+
* encode through here.
|
|
5
|
+
*/
|
|
6
|
+
export * from "./encoder.js";
|
|
7
|
+
export * from "./sketch.js";
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/psd/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sketch document → layered PSD.
|
|
3
|
+
*
|
|
4
|
+
* Each layer becomes its own Photoshop layer with name, visibility, opacity,
|
|
5
|
+
* blend mode and position. Vector layers, effects and transforms are baked
|
|
6
|
+
* into pixels by the caller's renderer, so Photoshop shows what the editor
|
|
7
|
+
* shows. Groups become Photoshop layer groups. The inpainting mask layer is
|
|
8
|
+
* kept as a hidden layer, matching the flattened export, which leaves it out.
|
|
9
|
+
*
|
|
10
|
+
* The editor's "Export PSD" and the headless renderer both encode through
|
|
11
|
+
* here.
|
|
12
|
+
*/
|
|
13
|
+
import type { BlendMode } from "@nodetool-ai/protocol/blend-modes";
|
|
14
|
+
import { type PsdNode } from "./encoder.js";
|
|
15
|
+
/** Straight RGBA pixels at document size. */
|
|
16
|
+
export interface DocumentPixels {
|
|
17
|
+
width: number;
|
|
18
|
+
height: number;
|
|
19
|
+
data: Uint8ClampedArray;
|
|
20
|
+
}
|
|
21
|
+
export interface SketchPsdSources {
|
|
22
|
+
/**
|
|
23
|
+
* One layer rendered at document size with its transform and effects but
|
|
24
|
+
* without opacity or blend mode, or null when it has no pixels.
|
|
25
|
+
*/
|
|
26
|
+
renderLayer: (layerId: string) => DocumentPixels | null;
|
|
27
|
+
/** The flattened document, as the PNG export renders it. */
|
|
28
|
+
composite: DocumentPixels;
|
|
29
|
+
}
|
|
30
|
+
/** The layer fields the PSD layer tree reads. */
|
|
31
|
+
export interface PsdSourceLayer {
|
|
32
|
+
id: string;
|
|
33
|
+
name: string;
|
|
34
|
+
type: string;
|
|
35
|
+
visible: boolean;
|
|
36
|
+
opacity: number;
|
|
37
|
+
blendMode: BlendMode;
|
|
38
|
+
alphaLock: boolean;
|
|
39
|
+
collapsed?: boolean;
|
|
40
|
+
parentId?: string | null;
|
|
41
|
+
}
|
|
42
|
+
export interface PsdSourceDocument {
|
|
43
|
+
layers: readonly PsdSourceLayer[];
|
|
44
|
+
}
|
|
45
|
+
interface CroppedPixels {
|
|
46
|
+
left: number;
|
|
47
|
+
top: number;
|
|
48
|
+
width: number;
|
|
49
|
+
height: number;
|
|
50
|
+
pixels: Uint8ClampedArray;
|
|
51
|
+
}
|
|
52
|
+
/** Crop document-size pixels to the bounding box of non-transparent pixels. */
|
|
53
|
+
export declare function cropToOpaqueBounds(image: DocumentPixels): CroppedPixels;
|
|
54
|
+
/** Build the PSD layer tree, bottom to top, from the document's layer array. */
|
|
55
|
+
export declare function buildPsdLayerTree(doc: PsdSourceDocument, renderLayer: SketchPsdSources["renderLayer"]): PsdNode[];
|
|
56
|
+
/** Encode the document as a layered PSD file. */
|
|
57
|
+
export declare function encodeSketchPsd(doc: PsdSourceDocument, sources: SketchPsdSources): Uint8Array<ArrayBuffer>;
|
|
58
|
+
export {};
|
|
59
|
+
//# sourceMappingURL=sketch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sketch.d.ts","sourceRoot":"","sources":["../../src/psd/sketch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAC;AACnE,OAAO,EAAa,KAAK,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvD,6CAA6C;AAC7C,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,iBAAiB,CAAC;CACzB;AAED,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,WAAW,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,cAAc,GAAG,IAAI,CAAC;IACxD,4DAA4D;IAC5D,SAAS,EAAE,cAAc,CAAC;CAC3B;AAED,iDAAiD;AACjD,MAAM,WAAW,cAAc;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,SAAS,CAAC;IACrB,SAAS,EAAE,OAAO,CAAC;IACnB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,GAAG,I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