ag-psd 15.0.4 → 15.1.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/CHANGELOG.md +7 -0
- package/README_PSD.md +4 -1
- package/dist/additionalInfo.js +7 -13
- package/dist/additionalInfo.js.map +1 -1
- package/dist/bundle.js +21 -14
- package/dist/descriptor.d.ts +4 -0
- package/dist/descriptor.js +7 -1
- package/dist/descriptor.js.map +1 -1
- package/dist/psd.d.ts +7 -2
- package/dist/psdWriter.js +7 -0
- package/dist/psdWriter.js.map +1 -1
- package/dist-es/additionalInfo.js +7 -13
- package/dist-es/additionalInfo.js.map +1 -1
- package/dist-es/descriptor.d.ts +4 -0
- package/dist-es/descriptor.js +7 -1
- package/dist-es/descriptor.js.map +1 -1
- package/dist-es/psd.d.ts +7 -2
- package/dist-es/psdWriter.js +7 -0
- package/dist-es/psdWriter.js.map +1 -1
- package/package.json +1 -1
- package/src/abr.ts +540 -0
- package/src/additionalInfo.ts +2677 -0
- package/src/csh.ts +44 -0
- package/src/descriptor.ts +1812 -0
- package/src/effectsHelpers.ts +305 -0
- package/src/engineData.ts +359 -0
- package/src/helpers.ts +387 -0
- package/src/imageResources.ts +1439 -0
- package/src/index.ts +44 -0
- package/src/initializeCanvas.ts +25 -0
- package/src/jpeg.ts +1166 -0
- package/src/psd.ts +1185 -0
- package/src/psdReader.ts +1091 -0
- package/src/psdWriter.ts +760 -0
- package/src/text.ts +751 -0
- package/src/utf8.ts +160 -0
|
@@ -0,0 +1,2677 @@
|
|
|
1
|
+
import { fromByteArray, toByteArray } from 'base64-js';
|
|
2
|
+
import { readEffects, writeEffects } from './effectsHelpers';
|
|
3
|
+
import { clamp, createEnum, layerColors, MOCK_HANDLERS } from './helpers';
|
|
4
|
+
import {
|
|
5
|
+
LayerAdditionalInfo, BezierPath, Psd, ReadOptions, BrightnessAdjustment, ExposureAdjustment, VibranceAdjustment,
|
|
6
|
+
ColorBalanceAdjustment, BlackAndWhiteAdjustment, PhotoFilterAdjustment, ChannelMixerChannel,
|
|
7
|
+
ChannelMixerAdjustment, PosterizeAdjustment, ThresholdAdjustment, GradientMapAdjustment, CMYK,
|
|
8
|
+
SelectiveColorAdjustment, ColorLookupAdjustment, LevelsAdjustmentChannel, LevelsAdjustment,
|
|
9
|
+
CurvesAdjustment, CurvesAdjustmentChannel, HueSaturationAdjustment, HueSaturationAdjustmentChannel,
|
|
10
|
+
PresetInfo, Color, ColorBalanceValues, WriteOptions, LinkedFile, PlacedLayerType, Warp, KeyDescriptorItem,
|
|
11
|
+
BooleanOperation, LayerEffectsInfo, Annotation, LayerVectorMask, AnimationFrame, Timeline,
|
|
12
|
+
} from './psd';
|
|
13
|
+
import {
|
|
14
|
+
PsdReader, readSignature, readUnicodeString, skipBytes, readUint32, readUint8, readFloat64, readUint16,
|
|
15
|
+
readBytes, readInt16, checkSignature, readFloat32, readFixedPointPath32, readSection, readColor, readInt32,
|
|
16
|
+
readPascalString, readUnicodeStringWithLength, readAsciiString, readPattern,
|
|
17
|
+
} from './psdReader';
|
|
18
|
+
import {
|
|
19
|
+
PsdWriter, writeZeros, writeSignature, writeBytes, writeUint32, writeUint16, writeFloat64, writeUint8,
|
|
20
|
+
writeInt16, writeFloat32, writeFixedPointPath32, writeUnicodeString, writeSection, writeUnicodeStringWithPadding,
|
|
21
|
+
writeColor, writePascalString, writeInt32,
|
|
22
|
+
} from './psdWriter';
|
|
23
|
+
import {
|
|
24
|
+
Annt, BlnM, DescriptorColor, DescriptorUnitsValue, parsePercent, parseUnits, parseUnitsOrNumber, QuiltWarpDescriptor,
|
|
25
|
+
strokeStyleLineAlignment, strokeStyleLineCapType, strokeStyleLineJoinType, TextDescriptor, textGridding,
|
|
26
|
+
unitsPercent, unitsValue, WarpDescriptor, warpStyle, writeVersionAndDescriptor,
|
|
27
|
+
readVersionAndDescriptor, StrokeDescriptor, Ornt, horzVrtcToXY, LmfxDescriptor, Lfx2Descriptor,
|
|
28
|
+
FrameListDescriptor, TimelineDescriptor, FrameDescriptor, xyToHorzVrtc, serializeEffects,
|
|
29
|
+
parseEffects, parseColor, serializeColor, serializeVectorContent, parseVectorContent, parseTrackList, serializeTrackList, FractionDescriptor,
|
|
30
|
+
} from './descriptor';
|
|
31
|
+
import { serializeEngineData, parseEngineData } from './engineData';
|
|
32
|
+
import { encodeEngineData, decodeEngineData } from './text';
|
|
33
|
+
|
|
34
|
+
export interface ExtendedWriteOptions extends WriteOptions {
|
|
35
|
+
layerIds: Set<number>;
|
|
36
|
+
layerToId: Map<any, number>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
type HasMethod = (target: LayerAdditionalInfo) => boolean;
|
|
40
|
+
type ReadMethod = (reader: PsdReader, target: LayerAdditionalInfo, left: () => number, psd: Psd, options: ReadOptions) => void;
|
|
41
|
+
type WriteMethod = (writer: PsdWriter, target: LayerAdditionalInfo, psd: Psd, options: ExtendedWriteOptions) => void;
|
|
42
|
+
|
|
43
|
+
export interface InfoHandler {
|
|
44
|
+
key: string;
|
|
45
|
+
has: HasMethod;
|
|
46
|
+
read: ReadMethod;
|
|
47
|
+
write: WriteMethod;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const infoHandlers: InfoHandler[] = [];
|
|
51
|
+
export const infoHandlersMap: { [key: string]: InfoHandler; } = {};
|
|
52
|
+
|
|
53
|
+
function addHandler(key: string, has: HasMethod, read: ReadMethod, write: WriteMethod) {
|
|
54
|
+
const handler: InfoHandler = { key, has, read, write };
|
|
55
|
+
infoHandlers.push(handler);
|
|
56
|
+
infoHandlersMap[handler.key] = handler;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function addHandlerAlias(key: string, target: string) {
|
|
60
|
+
infoHandlersMap[key] = infoHandlersMap[target];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function hasKey(key: keyof LayerAdditionalInfo) {
|
|
64
|
+
return (target: LayerAdditionalInfo) => target[key] !== undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function readLength64(reader: PsdReader) {
|
|
68
|
+
if (readUint32(reader)) throw new Error(`Resource size above 4 GB limit at ${reader.offset.toString(16)}`);
|
|
69
|
+
return readUint32(reader);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function writeLength64(writer: PsdWriter, length: number) {
|
|
73
|
+
writeUint32(writer, 0);
|
|
74
|
+
writeUint32(writer, length);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
addHandler(
|
|
78
|
+
'TySh',
|
|
79
|
+
hasKey('text'),
|
|
80
|
+
(reader, target, leftBytes) => {
|
|
81
|
+
if (readInt16(reader) !== 1) throw new Error(`Invalid TySh version`);
|
|
82
|
+
|
|
83
|
+
const transform: number[] = [];
|
|
84
|
+
for (let i = 0; i < 6; i++) transform.push(readFloat64(reader));
|
|
85
|
+
|
|
86
|
+
if (readInt16(reader) !== 50) throw new Error(`Invalid TySh text version`);
|
|
87
|
+
const text: TextDescriptor = readVersionAndDescriptor(reader);
|
|
88
|
+
|
|
89
|
+
if (readInt16(reader) !== 1) throw new Error(`Invalid TySh warp version`);
|
|
90
|
+
const warp: WarpDescriptor = readVersionAndDescriptor(reader);
|
|
91
|
+
|
|
92
|
+
target.text = {
|
|
93
|
+
transform,
|
|
94
|
+
left: readFloat32(reader),
|
|
95
|
+
top: readFloat32(reader),
|
|
96
|
+
right: readFloat32(reader),
|
|
97
|
+
bottom: readFloat32(reader),
|
|
98
|
+
text: text['Txt '].replace(/\r/g, '\n'),
|
|
99
|
+
index: text.TextIndex || 0,
|
|
100
|
+
gridding: textGridding.decode(text.textGridding),
|
|
101
|
+
antiAlias: Annt.decode(text.AntA),
|
|
102
|
+
orientation: Ornt.decode(text.Ornt),
|
|
103
|
+
warp: {
|
|
104
|
+
style: warpStyle.decode(warp.warpStyle),
|
|
105
|
+
value: warp.warpValue || 0,
|
|
106
|
+
perspective: warp.warpPerspective || 0,
|
|
107
|
+
perspectiveOther: warp.warpPerspectiveOther || 0,
|
|
108
|
+
rotate: Ornt.decode(warp.warpRotate),
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
if (text.EngineData) {
|
|
113
|
+
const engineData = decodeEngineData(parseEngineData(text.EngineData));
|
|
114
|
+
|
|
115
|
+
// const before = parseEngineData(text.EngineData);
|
|
116
|
+
// const after = encodeEngineData(engineData);
|
|
117
|
+
// require('fs').writeFileSync('before.txt', require('util').inspect(before, false, 99, false), 'utf8');
|
|
118
|
+
// require('fs').writeFileSync('after.txt', require('util').inspect(after, false, 99, false), 'utf8');
|
|
119
|
+
|
|
120
|
+
// console.log(require('util').inspect(parseEngineData(text.EngineData), false, 99, true));
|
|
121
|
+
target.text = { ...target.text, ...engineData };
|
|
122
|
+
// console.log(require('util').inspect(target.text, false, 99, true));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
skipBytes(reader, leftBytes());
|
|
126
|
+
},
|
|
127
|
+
(writer, target) => {
|
|
128
|
+
const text = target.text!;
|
|
129
|
+
const warp = text.warp || {};
|
|
130
|
+
const transform = text.transform || [1, 0, 0, 1, 0, 0];
|
|
131
|
+
|
|
132
|
+
const textDescriptor: TextDescriptor = {
|
|
133
|
+
'Txt ': (text.text || '').replace(/\r?\n/g, '\r'),
|
|
134
|
+
textGridding: textGridding.encode(text.gridding),
|
|
135
|
+
Ornt: Ornt.encode(text.orientation),
|
|
136
|
+
AntA: Annt.encode(text.antiAlias),
|
|
137
|
+
TextIndex: text.index || 0,
|
|
138
|
+
EngineData: serializeEngineData(encodeEngineData(text)),
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
writeInt16(writer, 1); // version
|
|
142
|
+
|
|
143
|
+
for (let i = 0; i < 6; i++) {
|
|
144
|
+
writeFloat64(writer, transform[i]);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
writeInt16(writer, 50); // text version
|
|
148
|
+
writeVersionAndDescriptor(writer, '', 'TxLr', textDescriptor);
|
|
149
|
+
|
|
150
|
+
writeInt16(writer, 1); // warp version
|
|
151
|
+
writeVersionAndDescriptor(writer, '', 'warp', encodeWarp(warp));
|
|
152
|
+
|
|
153
|
+
writeFloat32(writer, text.left!);
|
|
154
|
+
writeFloat32(writer, text.top!);
|
|
155
|
+
writeFloat32(writer, text.right!);
|
|
156
|
+
writeFloat32(writer, text.bottom!);
|
|
157
|
+
|
|
158
|
+
// writeZeros(writer, 2);
|
|
159
|
+
},
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
// vector fills
|
|
163
|
+
|
|
164
|
+
addHandler(
|
|
165
|
+
'SoCo',
|
|
166
|
+
target => target.vectorFill !== undefined && target.vectorStroke === undefined &&
|
|
167
|
+
target.vectorFill.type === 'color',
|
|
168
|
+
(reader, target) => {
|
|
169
|
+
const descriptor = readVersionAndDescriptor(reader);
|
|
170
|
+
target.vectorFill = parseVectorContent(descriptor);
|
|
171
|
+
},
|
|
172
|
+
(writer, target) => {
|
|
173
|
+
const { descriptor } = serializeVectorContent(target.vectorFill!);
|
|
174
|
+
writeVersionAndDescriptor(writer, '', 'null', descriptor);
|
|
175
|
+
},
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
addHandler(
|
|
179
|
+
'GdFl',
|
|
180
|
+
target => target.vectorFill !== undefined && target.vectorStroke === undefined &&
|
|
181
|
+
(target.vectorFill.type === 'solid' || target.vectorFill.type === 'noise'),
|
|
182
|
+
(reader, target, left) => {
|
|
183
|
+
const descriptor = readVersionAndDescriptor(reader);
|
|
184
|
+
target.vectorFill = parseVectorContent(descriptor);
|
|
185
|
+
skipBytes(reader, left());
|
|
186
|
+
},
|
|
187
|
+
(writer, target) => {
|
|
188
|
+
const { descriptor } = serializeVectorContent(target.vectorFill!);
|
|
189
|
+
writeVersionAndDescriptor(writer, '', 'null', descriptor);
|
|
190
|
+
},
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
addHandler(
|
|
194
|
+
'PtFl',
|
|
195
|
+
target => target.vectorFill !== undefined && target.vectorStroke === undefined &&
|
|
196
|
+
target.vectorFill.type === 'pattern',
|
|
197
|
+
(reader, target) => {
|
|
198
|
+
const descriptor = readVersionAndDescriptor(reader);
|
|
199
|
+
target.vectorFill = parseVectorContent(descriptor);
|
|
200
|
+
},
|
|
201
|
+
(writer, target) => {
|
|
202
|
+
const { descriptor } = serializeVectorContent(target.vectorFill!);
|
|
203
|
+
writeVersionAndDescriptor(writer, '', 'null', descriptor);
|
|
204
|
+
},
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
addHandler(
|
|
208
|
+
'vscg',
|
|
209
|
+
target => target.vectorFill !== undefined && target.vectorStroke !== undefined,
|
|
210
|
+
(reader, target, left) => {
|
|
211
|
+
readSignature(reader); // key
|
|
212
|
+
const desc = readVersionAndDescriptor(reader);
|
|
213
|
+
target.vectorFill = parseVectorContent(desc);
|
|
214
|
+
skipBytes(reader, left());
|
|
215
|
+
},
|
|
216
|
+
(writer, target) => {
|
|
217
|
+
const { descriptor, key } = serializeVectorContent(target.vectorFill!);
|
|
218
|
+
writeSignature(writer, key);
|
|
219
|
+
writeVersionAndDescriptor(writer, '', 'null', descriptor);
|
|
220
|
+
},
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
export function readBezierKnot(reader: PsdReader, width: number, height: number) {
|
|
224
|
+
const y0 = readFixedPointPath32(reader) * height;
|
|
225
|
+
const x0 = readFixedPointPath32(reader) * width;
|
|
226
|
+
const y1 = readFixedPointPath32(reader) * height;
|
|
227
|
+
const x1 = readFixedPointPath32(reader) * width;
|
|
228
|
+
const y2 = readFixedPointPath32(reader) * height;
|
|
229
|
+
const x2 = readFixedPointPath32(reader) * width;
|
|
230
|
+
return [x0, y0, x1, y1, x2, y2];
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function writeBezierKnot(writer: PsdWriter, points: number[], width: number, height: number) {
|
|
234
|
+
writeFixedPointPath32(writer, points[1] / height); // y0
|
|
235
|
+
writeFixedPointPath32(writer, points[0] / width); // x0
|
|
236
|
+
writeFixedPointPath32(writer, points[3] / height); // y1
|
|
237
|
+
writeFixedPointPath32(writer, points[2] / width); // x1
|
|
238
|
+
writeFixedPointPath32(writer, points[5] / height); // y2
|
|
239
|
+
writeFixedPointPath32(writer, points[4] / width); // x2
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export const booleanOperations: BooleanOperation[] = ['exclude', 'combine', 'subtract', 'intersect'];
|
|
243
|
+
|
|
244
|
+
export function readVectorMask(reader: PsdReader, vectorMask: LayerVectorMask, width: number, height: number, size: number) {
|
|
245
|
+
const end = reader.offset + size;
|
|
246
|
+
const paths = vectorMask.paths;
|
|
247
|
+
let path: BezierPath | undefined = undefined;
|
|
248
|
+
|
|
249
|
+
while ((end - reader.offset) >= 26) {
|
|
250
|
+
const selector = readUint16(reader);
|
|
251
|
+
|
|
252
|
+
switch (selector) {
|
|
253
|
+
case 0: // Closed subpath length record
|
|
254
|
+
case 3: { // Open subpath length record
|
|
255
|
+
readUint16(reader); // count
|
|
256
|
+
const boolOp = readInt16(reader);
|
|
257
|
+
readUint16(reader); // always 1 ?
|
|
258
|
+
skipBytes(reader, 18);
|
|
259
|
+
// TODO: 'combine' here might be wrong
|
|
260
|
+
path = { open: selector === 3, operation: boolOp === -1 ? 'combine' : booleanOperations[boolOp], knots: [] };
|
|
261
|
+
paths.push(path);
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
case 1: // Closed subpath Bezier knot, linked
|
|
265
|
+
case 2: // Closed subpath Bezier knot, unlinked
|
|
266
|
+
case 4: // Open subpath Bezier knot, linked
|
|
267
|
+
case 5: // Open subpath Bezier knot, unlinked
|
|
268
|
+
path!.knots.push({ linked: (selector === 1 || selector === 4), points: readBezierKnot(reader, width, height) });
|
|
269
|
+
break;
|
|
270
|
+
case 6: // Path fill rule record
|
|
271
|
+
skipBytes(reader, 24);
|
|
272
|
+
break;
|
|
273
|
+
case 7: { // Clipboard record
|
|
274
|
+
// TODO: check if these need to be multiplied by document size
|
|
275
|
+
const top = readFixedPointPath32(reader);
|
|
276
|
+
const left = readFixedPointPath32(reader);
|
|
277
|
+
const bottom = readFixedPointPath32(reader);
|
|
278
|
+
const right = readFixedPointPath32(reader);
|
|
279
|
+
const resolution = readFixedPointPath32(reader);
|
|
280
|
+
skipBytes(reader, 4);
|
|
281
|
+
vectorMask.clipboard = { top, left, bottom, right, resolution };
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
case 8: // Initial fill rule record
|
|
285
|
+
vectorMask.fillStartsWithAllPixels = !!readUint16(reader);
|
|
286
|
+
skipBytes(reader, 22);
|
|
287
|
+
break;
|
|
288
|
+
default: throw new Error('Invalid vmsk section');
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return paths;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
addHandler(
|
|
296
|
+
'vmsk',
|
|
297
|
+
hasKey('vectorMask'),
|
|
298
|
+
(reader, target, left, { width, height }) => {
|
|
299
|
+
if (readUint32(reader) !== 3) throw new Error('Invalid vmsk version');
|
|
300
|
+
|
|
301
|
+
target.vectorMask = { paths: [] };
|
|
302
|
+
const vectorMask = target.vectorMask;
|
|
303
|
+
|
|
304
|
+
const flags = readUint32(reader);
|
|
305
|
+
vectorMask.invert = (flags & 1) !== 0;
|
|
306
|
+
vectorMask.notLink = (flags & 2) !== 0;
|
|
307
|
+
vectorMask.disable = (flags & 4) !== 0;
|
|
308
|
+
|
|
309
|
+
readVectorMask(reader, vectorMask, width, height, left());
|
|
310
|
+
|
|
311
|
+
// drawBezierPaths(vectorMask.paths, width, height, 'out.png');
|
|
312
|
+
|
|
313
|
+
skipBytes(reader, left());
|
|
314
|
+
},
|
|
315
|
+
(writer, target, { width, height }) => {
|
|
316
|
+
const vectorMask = target.vectorMask!;
|
|
317
|
+
const flags =
|
|
318
|
+
(vectorMask.invert ? 1 : 0) |
|
|
319
|
+
(vectorMask.notLink ? 2 : 0) |
|
|
320
|
+
(vectorMask.disable ? 4 : 0);
|
|
321
|
+
|
|
322
|
+
writeUint32(writer, 3); // version
|
|
323
|
+
writeUint32(writer, flags);
|
|
324
|
+
|
|
325
|
+
// initial entry
|
|
326
|
+
writeUint16(writer, 6);
|
|
327
|
+
writeZeros(writer, 24);
|
|
328
|
+
|
|
329
|
+
const clipboard = vectorMask.clipboard;
|
|
330
|
+
if (clipboard) {
|
|
331
|
+
writeUint16(writer, 7);
|
|
332
|
+
writeFixedPointPath32(writer, clipboard.top);
|
|
333
|
+
writeFixedPointPath32(writer, clipboard.left);
|
|
334
|
+
writeFixedPointPath32(writer, clipboard.bottom);
|
|
335
|
+
writeFixedPointPath32(writer, clipboard.right);
|
|
336
|
+
writeFixedPointPath32(writer, clipboard.resolution);
|
|
337
|
+
writeZeros(writer, 4);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (vectorMask.fillStartsWithAllPixels !== undefined) {
|
|
341
|
+
writeUint16(writer, 8);
|
|
342
|
+
writeUint16(writer, vectorMask.fillStartsWithAllPixels ? 1 : 0);
|
|
343
|
+
writeZeros(writer, 22);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
for (const path of vectorMask.paths) {
|
|
347
|
+
writeUint16(writer, path.open ? 3 : 0);
|
|
348
|
+
writeUint16(writer, path.knots.length);
|
|
349
|
+
writeUint16(writer, Math.abs(booleanOperations.indexOf(path.operation))); // default to 1 if not found
|
|
350
|
+
writeUint16(writer, 1);
|
|
351
|
+
writeZeros(writer, 18); // TODO: these are sometimes non-zero
|
|
352
|
+
|
|
353
|
+
const linkedKnot = path.open ? 4 : 1;
|
|
354
|
+
const unlinkedKnot = path.open ? 5 : 2;
|
|
355
|
+
|
|
356
|
+
for (const { linked, points } of path.knots) {
|
|
357
|
+
writeUint16(writer, linked ? linkedKnot : unlinkedKnot);
|
|
358
|
+
writeBezierKnot(writer, points, width, height);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
},
|
|
362
|
+
);
|
|
363
|
+
|
|
364
|
+
// TODO: need to write vmsk if has outline ?
|
|
365
|
+
addHandlerAlias('vsms', 'vmsk');
|
|
366
|
+
// addHandlerAlias('vmsk', 'vsms');
|
|
367
|
+
|
|
368
|
+
interface VogkDescriptor {
|
|
369
|
+
keyDescriptorList: {
|
|
370
|
+
keyShapeInvalidated?: boolean;
|
|
371
|
+
keyOriginType?: number;
|
|
372
|
+
keyOriginResolution?: number;
|
|
373
|
+
keyOriginRRectRadii?: {
|
|
374
|
+
unitValueQuadVersion: number;
|
|
375
|
+
topRight: DescriptorUnitsValue;
|
|
376
|
+
topLeft: DescriptorUnitsValue;
|
|
377
|
+
bottomLeft: DescriptorUnitsValue;
|
|
378
|
+
bottomRight: DescriptorUnitsValue;
|
|
379
|
+
};
|
|
380
|
+
keyOriginShapeBBox?: {
|
|
381
|
+
unitValueQuadVersion: number;
|
|
382
|
+
'Top ': DescriptorUnitsValue;
|
|
383
|
+
Left: DescriptorUnitsValue;
|
|
384
|
+
Btom: DescriptorUnitsValue;
|
|
385
|
+
Rght: DescriptorUnitsValue;
|
|
386
|
+
};
|
|
387
|
+
keyOriginBoxCorners?: {
|
|
388
|
+
rectangleCornerA: { Hrzn: number; Vrtc: number; };
|
|
389
|
+
rectangleCornerB: { Hrzn: number; Vrtc: number; };
|
|
390
|
+
rectangleCornerC: { Hrzn: number; Vrtc: number; };
|
|
391
|
+
rectangleCornerD: { Hrzn: number; Vrtc: number; };
|
|
392
|
+
};
|
|
393
|
+
Trnf?: { xx: number; xy: number; yx: number; yy: number; tx: number; ty: number; },
|
|
394
|
+
keyOriginIndex: number;
|
|
395
|
+
}[];
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
addHandler(
|
|
399
|
+
'vogk',
|
|
400
|
+
hasKey('vectorOrigination'),
|
|
401
|
+
(reader, target, left) => {
|
|
402
|
+
if (readInt32(reader) !== 1) throw new Error(`Invalid vogk version`);
|
|
403
|
+
const desc = readVersionAndDescriptor(reader) as VogkDescriptor;
|
|
404
|
+
// console.log(require('util').inspect(desc, false, 99, true));
|
|
405
|
+
|
|
406
|
+
target.vectorOrigination = { keyDescriptorList: [] };
|
|
407
|
+
|
|
408
|
+
for (const i of desc.keyDescriptorList) {
|
|
409
|
+
const item: KeyDescriptorItem = {};
|
|
410
|
+
|
|
411
|
+
if (i.keyShapeInvalidated != null) item.keyShapeInvalidated = i.keyShapeInvalidated;
|
|
412
|
+
if (i.keyOriginType != null) item.keyOriginType = i.keyOriginType;
|
|
413
|
+
if (i.keyOriginResolution != null) item.keyOriginResolution = i.keyOriginResolution;
|
|
414
|
+
if (i.keyOriginShapeBBox) {
|
|
415
|
+
item.keyOriginShapeBoundingBox = {
|
|
416
|
+
top: parseUnits(i.keyOriginShapeBBox['Top ']),
|
|
417
|
+
left: parseUnits(i.keyOriginShapeBBox.Left),
|
|
418
|
+
bottom: parseUnits(i.keyOriginShapeBBox.Btom),
|
|
419
|
+
right: parseUnits(i.keyOriginShapeBBox.Rght),
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
const rectRadii = i.keyOriginRRectRadii;
|
|
423
|
+
if (rectRadii) {
|
|
424
|
+
item.keyOriginRRectRadii = {
|
|
425
|
+
topRight: parseUnits(rectRadii.topRight),
|
|
426
|
+
topLeft: parseUnits(rectRadii.topLeft),
|
|
427
|
+
bottomLeft: parseUnits(rectRadii.bottomLeft),
|
|
428
|
+
bottomRight: parseUnits(rectRadii.bottomRight),
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
const corners = i.keyOriginBoxCorners;
|
|
432
|
+
if (corners) {
|
|
433
|
+
item.keyOriginBoxCorners = [
|
|
434
|
+
{ x: corners.rectangleCornerA.Hrzn, y: corners.rectangleCornerA.Vrtc },
|
|
435
|
+
{ x: corners.rectangleCornerB.Hrzn, y: corners.rectangleCornerB.Vrtc },
|
|
436
|
+
{ x: corners.rectangleCornerC.Hrzn, y: corners.rectangleCornerC.Vrtc },
|
|
437
|
+
{ x: corners.rectangleCornerD.Hrzn, y: corners.rectangleCornerD.Vrtc },
|
|
438
|
+
];
|
|
439
|
+
}
|
|
440
|
+
const trnf = i.Trnf;
|
|
441
|
+
if (trnf) {
|
|
442
|
+
item.transform = [trnf.xx, trnf.xy, trnf.xy, trnf.yy, trnf.tx, trnf.ty];
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
target.vectorOrigination.keyDescriptorList.push(item);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
skipBytes(reader, left());
|
|
449
|
+
},
|
|
450
|
+
(writer, target) => {
|
|
451
|
+
target;
|
|
452
|
+
const orig = target.vectorOrigination!;
|
|
453
|
+
const desc: VogkDescriptor = { keyDescriptorList: [] };
|
|
454
|
+
|
|
455
|
+
for (let i = 0; i < orig.keyDescriptorList.length; i++) {
|
|
456
|
+
const item = orig.keyDescriptorList[i];
|
|
457
|
+
|
|
458
|
+
if (item.keyShapeInvalidated) {
|
|
459
|
+
desc.keyDescriptorList.push({ keyShapeInvalidated: true, keyOriginIndex: i });
|
|
460
|
+
} else {
|
|
461
|
+
desc.keyDescriptorList.push({} as any); // we're adding keyOriginIndex at the end
|
|
462
|
+
|
|
463
|
+
const out = desc.keyDescriptorList[desc.keyDescriptorList.length - 1];
|
|
464
|
+
|
|
465
|
+
if (item.keyOriginType != null) out.keyOriginType = item.keyOriginType;
|
|
466
|
+
if (item.keyOriginResolution != null) out.keyOriginResolution = item.keyOriginResolution;
|
|
467
|
+
|
|
468
|
+
const radii = item.keyOriginRRectRadii;
|
|
469
|
+
if (radii) {
|
|
470
|
+
out.keyOriginRRectRadii = {
|
|
471
|
+
unitValueQuadVersion: 1,
|
|
472
|
+
topRight: unitsValue(radii.topRight, 'topRight'),
|
|
473
|
+
topLeft: unitsValue(radii.topLeft, 'topLeft'),
|
|
474
|
+
bottomLeft: unitsValue(radii.bottomLeft, 'bottomLeft'),
|
|
475
|
+
bottomRight: unitsValue(radii.bottomRight, 'bottomRight'),
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
const box = item.keyOriginShapeBoundingBox;
|
|
480
|
+
if (box) {
|
|
481
|
+
out.keyOriginShapeBBox = {
|
|
482
|
+
unitValueQuadVersion: 1,
|
|
483
|
+
'Top ': unitsValue(box.top, 'top'),
|
|
484
|
+
Left: unitsValue(box.left, 'left'),
|
|
485
|
+
Btom: unitsValue(box.bottom, 'bottom'),
|
|
486
|
+
Rght: unitsValue(box.right, 'right'),
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
const corners = item.keyOriginBoxCorners;
|
|
491
|
+
if (corners && corners.length === 4) {
|
|
492
|
+
out.keyOriginBoxCorners = {
|
|
493
|
+
rectangleCornerA: { Hrzn: corners[0].x, Vrtc: corners[0].y },
|
|
494
|
+
rectangleCornerB: { Hrzn: corners[1].x, Vrtc: corners[1].y },
|
|
495
|
+
rectangleCornerC: { Hrzn: corners[2].x, Vrtc: corners[2].y },
|
|
496
|
+
rectangleCornerD: { Hrzn: corners[3].x, Vrtc: corners[3].y },
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const transform = item.transform;
|
|
501
|
+
if (transform && transform.length === 6) {
|
|
502
|
+
out.Trnf = {
|
|
503
|
+
xx: transform[0],
|
|
504
|
+
xy: transform[1],
|
|
505
|
+
yx: transform[2],
|
|
506
|
+
yy: transform[3],
|
|
507
|
+
tx: transform[4],
|
|
508
|
+
ty: transform[5],
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
out.keyOriginIndex = i;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
writeInt32(writer, 1); // version
|
|
517
|
+
writeVersionAndDescriptor(writer, '', 'null', desc);
|
|
518
|
+
}
|
|
519
|
+
);
|
|
520
|
+
|
|
521
|
+
addHandler(
|
|
522
|
+
'lmfx',
|
|
523
|
+
target => target.effects !== undefined && hasMultiEffects(target.effects),
|
|
524
|
+
(reader, target, left, _, options) => {
|
|
525
|
+
const version = readUint32(reader);
|
|
526
|
+
if (version !== 0) throw new Error('Invalid lmfx version');
|
|
527
|
+
|
|
528
|
+
const desc: LmfxDescriptor = readVersionAndDescriptor(reader);
|
|
529
|
+
// console.log(require('util').inspect(info, false, 99, true));
|
|
530
|
+
|
|
531
|
+
// discard if read in 'lrFX' or 'lfx2' section
|
|
532
|
+
target.effects = parseEffects(desc, !!options.logMissingFeatures);
|
|
533
|
+
|
|
534
|
+
skipBytes(reader, left());
|
|
535
|
+
},
|
|
536
|
+
(writer, target, _, options) => {
|
|
537
|
+
const desc = serializeEffects(target.effects!, !!options.logMissingFeatures, true);
|
|
538
|
+
|
|
539
|
+
writeUint32(writer, 0); // version
|
|
540
|
+
writeVersionAndDescriptor(writer, '', 'null', desc);
|
|
541
|
+
},
|
|
542
|
+
);
|
|
543
|
+
|
|
544
|
+
addHandler(
|
|
545
|
+
'lrFX',
|
|
546
|
+
hasKey('effects'),
|
|
547
|
+
(reader, target, left) => {
|
|
548
|
+
if (!target.effects) target.effects = readEffects(reader);
|
|
549
|
+
|
|
550
|
+
skipBytes(reader, left());
|
|
551
|
+
},
|
|
552
|
+
(writer, target) => {
|
|
553
|
+
writeEffects(writer, target.effects!);
|
|
554
|
+
},
|
|
555
|
+
);
|
|
556
|
+
|
|
557
|
+
addHandler(
|
|
558
|
+
'luni',
|
|
559
|
+
hasKey('name'),
|
|
560
|
+
(reader, target, left) => {
|
|
561
|
+
target.name = readUnicodeString(reader);
|
|
562
|
+
skipBytes(reader, left());
|
|
563
|
+
},
|
|
564
|
+
(writer, target) => {
|
|
565
|
+
writeUnicodeString(writer, target.name!);
|
|
566
|
+
// writeUint16(writer, 0); // padding (but not extending string length)
|
|
567
|
+
},
|
|
568
|
+
);
|
|
569
|
+
|
|
570
|
+
addHandler(
|
|
571
|
+
'lnsr',
|
|
572
|
+
hasKey('nameSource'),
|
|
573
|
+
(reader, target) => target.nameSource = readSignature(reader),
|
|
574
|
+
(writer, target) => writeSignature(writer, target.nameSource!),
|
|
575
|
+
);
|
|
576
|
+
|
|
577
|
+
addHandler(
|
|
578
|
+
'lyid',
|
|
579
|
+
hasKey('id'),
|
|
580
|
+
(reader, target) => target.id = readUint32(reader),
|
|
581
|
+
(writer, target, _psd, options) => {
|
|
582
|
+
let id = target.id!;
|
|
583
|
+
while (options.layerIds.has(id)) id += 100; // make sure we don't have duplicate layer ids
|
|
584
|
+
writeUint32(writer, id);
|
|
585
|
+
options.layerIds.add(id);
|
|
586
|
+
options.layerToId.set(target, id);
|
|
587
|
+
},
|
|
588
|
+
);
|
|
589
|
+
|
|
590
|
+
addHandler(
|
|
591
|
+
'lsct',
|
|
592
|
+
hasKey('sectionDivider'),
|
|
593
|
+
(reader, target, left) => {
|
|
594
|
+
target.sectionDivider = { type: readUint32(reader) };
|
|
595
|
+
|
|
596
|
+
if (left()) {
|
|
597
|
+
checkSignature(reader, '8BIM');
|
|
598
|
+
target.sectionDivider.key = readSignature(reader);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
if (left()) {
|
|
602
|
+
target.sectionDivider.subType = readUint32(reader);
|
|
603
|
+
}
|
|
604
|
+
},
|
|
605
|
+
(writer, target) => {
|
|
606
|
+
writeUint32(writer, target.sectionDivider!.type);
|
|
607
|
+
|
|
608
|
+
if (target.sectionDivider!.key) {
|
|
609
|
+
writeSignature(writer, '8BIM');
|
|
610
|
+
writeSignature(writer, target.sectionDivider!.key);
|
|
611
|
+
|
|
612
|
+
if (target.sectionDivider!.subType !== undefined) {
|
|
613
|
+
writeUint32(writer, target.sectionDivider!.subType);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
},
|
|
617
|
+
);
|
|
618
|
+
|
|
619
|
+
// it seems lsdk is used when there's a layer is nested more than 6 levels, but I don't know why?
|
|
620
|
+
// maybe some limitation of old version of PS?
|
|
621
|
+
addHandlerAlias('lsdk', 'lsct');
|
|
622
|
+
|
|
623
|
+
addHandler(
|
|
624
|
+
'clbl',
|
|
625
|
+
hasKey('blendClippendElements'),
|
|
626
|
+
(reader, target) => {
|
|
627
|
+
target.blendClippendElements = !!readUint8(reader);
|
|
628
|
+
skipBytes(reader, 3);
|
|
629
|
+
},
|
|
630
|
+
(writer, target) => {
|
|
631
|
+
writeUint8(writer, target.blendClippendElements ? 1 : 0);
|
|
632
|
+
writeZeros(writer, 3);
|
|
633
|
+
},
|
|
634
|
+
);
|
|
635
|
+
|
|
636
|
+
addHandler(
|
|
637
|
+
'infx',
|
|
638
|
+
hasKey('blendInteriorElements'),
|
|
639
|
+
(reader, target) => {
|
|
640
|
+
target.blendInteriorElements = !!readUint8(reader);
|
|
641
|
+
skipBytes(reader, 3);
|
|
642
|
+
},
|
|
643
|
+
(writer, target) => {
|
|
644
|
+
writeUint8(writer, target.blendInteriorElements ? 1 : 0);
|
|
645
|
+
writeZeros(writer, 3);
|
|
646
|
+
},
|
|
647
|
+
);
|
|
648
|
+
|
|
649
|
+
addHandler(
|
|
650
|
+
'knko',
|
|
651
|
+
hasKey('knockout'),
|
|
652
|
+
(reader, target) => {
|
|
653
|
+
target.knockout = !!readUint8(reader);
|
|
654
|
+
skipBytes(reader, 3);
|
|
655
|
+
},
|
|
656
|
+
(writer, target) => {
|
|
657
|
+
writeUint8(writer, target.knockout ? 1 : 0);
|
|
658
|
+
writeZeros(writer, 3);
|
|
659
|
+
},
|
|
660
|
+
);
|
|
661
|
+
|
|
662
|
+
addHandler(
|
|
663
|
+
'lmgm',
|
|
664
|
+
hasKey('layerMaskAsGlobalMask'),
|
|
665
|
+
(reader, target) => {
|
|
666
|
+
target.layerMaskAsGlobalMask = !!readUint8(reader);
|
|
667
|
+
skipBytes(reader, 3);
|
|
668
|
+
},
|
|
669
|
+
(writer, target) => {
|
|
670
|
+
writeUint8(writer, target.layerMaskAsGlobalMask ? 1 : 0);
|
|
671
|
+
writeZeros(writer, 3);
|
|
672
|
+
},
|
|
673
|
+
);
|
|
674
|
+
|
|
675
|
+
addHandler(
|
|
676
|
+
'lspf',
|
|
677
|
+
hasKey('protected'),
|
|
678
|
+
(reader, target) => {
|
|
679
|
+
const flags = readUint32(reader);
|
|
680
|
+
target.protected = {
|
|
681
|
+
transparency: (flags & 0x01) !== 0,
|
|
682
|
+
composite: (flags & 0x02) !== 0,
|
|
683
|
+
position: (flags & 0x04) !== 0,
|
|
684
|
+
};
|
|
685
|
+
|
|
686
|
+
if (flags & 0x08) target.protected.artboards = true;
|
|
687
|
+
},
|
|
688
|
+
(writer, target) => {
|
|
689
|
+
const flags =
|
|
690
|
+
(target.protected!.transparency ? 0x01 : 0) |
|
|
691
|
+
(target.protected!.composite ? 0x02 : 0) |
|
|
692
|
+
(target.protected!.position ? 0x04 : 0) |
|
|
693
|
+
(target.protected!.artboards ? 0x08 : 0);
|
|
694
|
+
|
|
695
|
+
writeUint32(writer, flags);
|
|
696
|
+
},
|
|
697
|
+
);
|
|
698
|
+
|
|
699
|
+
addHandler(
|
|
700
|
+
'lclr',
|
|
701
|
+
hasKey('layerColor'),
|
|
702
|
+
(reader, target) => {
|
|
703
|
+
const color = readUint16(reader);
|
|
704
|
+
skipBytes(reader, 6);
|
|
705
|
+
target.layerColor = layerColors[color];
|
|
706
|
+
},
|
|
707
|
+
(writer, target) => {
|
|
708
|
+
const index = layerColors.indexOf(target.layerColor!);
|
|
709
|
+
writeUint16(writer, index === -1 ? 0 : index);
|
|
710
|
+
writeZeros(writer, 6);
|
|
711
|
+
},
|
|
712
|
+
);
|
|
713
|
+
|
|
714
|
+
interface CustomDescriptor {
|
|
715
|
+
layerTime?: number;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
addHandler(
|
|
719
|
+
'shmd',
|
|
720
|
+
target => target.timestamp !== undefined || target.animationFrames !== undefined ||
|
|
721
|
+
target.animationFrameFlags !== undefined || target.timeline !== undefined,
|
|
722
|
+
(reader, target, left, _, options) => {
|
|
723
|
+
const count = readUint32(reader);
|
|
724
|
+
|
|
725
|
+
for (let i = 0; i < count; i++) {
|
|
726
|
+
checkSignature(reader, '8BIM');
|
|
727
|
+
const key = readSignature(reader);
|
|
728
|
+
readUint8(reader); // copy
|
|
729
|
+
skipBytes(reader, 3);
|
|
730
|
+
|
|
731
|
+
readSection(reader, 1, left => {
|
|
732
|
+
if (key === 'cust') {
|
|
733
|
+
const desc = readVersionAndDescriptor(reader) as CustomDescriptor;
|
|
734
|
+
// console.log('cust', target.name, require('util').inspect(desc, false, 99, true));
|
|
735
|
+
if (desc.layerTime !== undefined) target.timestamp = desc.layerTime;
|
|
736
|
+
} else if (key === 'mlst') {
|
|
737
|
+
const desc = readVersionAndDescriptor(reader) as FrameListDescriptor;
|
|
738
|
+
// console.log('mlst', target.name, require('util').inspect(desc, false, 99, true));
|
|
739
|
+
|
|
740
|
+
target.animationFrames = [];
|
|
741
|
+
|
|
742
|
+
for (let i = 0; i < desc.LaSt.length; i++) {
|
|
743
|
+
const f = desc.LaSt[i];
|
|
744
|
+
const frame: AnimationFrame = { frames: f.FrLs };
|
|
745
|
+
if (f.enab !== undefined) frame.enable = f.enab;
|
|
746
|
+
if (f.Ofst) frame.offset = horzVrtcToXY(f.Ofst);
|
|
747
|
+
if (f.FXRf) frame.referencePoint = horzVrtcToXY(f.FXRf);
|
|
748
|
+
if (f.Lefx) frame.effects = parseEffects(f.Lefx, !!options.logMissingFeatures);
|
|
749
|
+
if (f.blendOptions && f.blendOptions.Opct) frame.opacity = parsePercent(f.blendOptions.Opct);
|
|
750
|
+
target.animationFrames.push(frame);
|
|
751
|
+
}
|
|
752
|
+
} else if (key === 'mdyn') {
|
|
753
|
+
// frame flags
|
|
754
|
+
readUint16(reader); // unknown
|
|
755
|
+
const propagate = readUint8(reader);
|
|
756
|
+
const flags = readUint8(reader);
|
|
757
|
+
|
|
758
|
+
target.animationFrameFlags = {
|
|
759
|
+
propagateFrameOne: !propagate,
|
|
760
|
+
unifyLayerPosition: (flags & 1) !== 0,
|
|
761
|
+
unifyLayerStyle: (flags & 2) !== 0,
|
|
762
|
+
unifyLayerVisibility: (flags & 4) !== 0,
|
|
763
|
+
};
|
|
764
|
+
} else if (key === 'tmln') {
|
|
765
|
+
const desc = readVersionAndDescriptor(reader) as TimelineDescriptor;
|
|
766
|
+
const timeScope = desc.timeScope;
|
|
767
|
+
// console.log('tmln', target.name, target.id, require('util').inspect(desc, false, 99, true));
|
|
768
|
+
|
|
769
|
+
const timeline: Timeline = {
|
|
770
|
+
start: timeScope.Strt,
|
|
771
|
+
duration: timeScope.duration,
|
|
772
|
+
inTime: timeScope.inTime,
|
|
773
|
+
outTime: timeScope.outTime,
|
|
774
|
+
autoScope: desc.autoScope,
|
|
775
|
+
audioLevel: desc.audioLevel,
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
if (desc.trackList) {
|
|
779
|
+
timeline.tracks = parseTrackList(desc.trackList, !!options.logMissingFeatures);
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
target.timeline = timeline;
|
|
783
|
+
// console.log('tmln:result', target.name, target.id, require('util').inspect(timeline, false, 99, true));
|
|
784
|
+
} else {
|
|
785
|
+
options.logDevFeatures && console.log('Unhandled "shmd" section key', key);
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
skipBytes(reader, left());
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
skipBytes(reader, left());
|
|
793
|
+
},
|
|
794
|
+
(writer, target, _, options) => {
|
|
795
|
+
const { animationFrames, animationFrameFlags, timestamp, timeline } = target;
|
|
796
|
+
|
|
797
|
+
let count = 0;
|
|
798
|
+
if (animationFrames) count++;
|
|
799
|
+
if (animationFrameFlags) count++;
|
|
800
|
+
if (timeline) count++;
|
|
801
|
+
if (timestamp !== undefined) count++;
|
|
802
|
+
writeUint32(writer, count);
|
|
803
|
+
|
|
804
|
+
if (animationFrames) {
|
|
805
|
+
writeSignature(writer, '8BIM');
|
|
806
|
+
writeSignature(writer, 'mlst');
|
|
807
|
+
writeUint8(writer, 0); // copy (always false)
|
|
808
|
+
writeZeros(writer, 3);
|
|
809
|
+
writeSection(writer, 2, () => {
|
|
810
|
+
const desc: FrameListDescriptor = {
|
|
811
|
+
LaID: target.id ?? 0,
|
|
812
|
+
LaSt: [],
|
|
813
|
+
};
|
|
814
|
+
|
|
815
|
+
for (let i = 0; i < animationFrames.length; i++) {
|
|
816
|
+
const f = animationFrames[i];
|
|
817
|
+
const frame: FrameDescriptor = {} as any;
|
|
818
|
+
if (f.enable !== undefined) frame.enab = f.enable;
|
|
819
|
+
frame.FrLs = f.frames;
|
|
820
|
+
if (f.offset) frame.Ofst = xyToHorzVrtc(f.offset);
|
|
821
|
+
if (f.referencePoint) frame.FXRf = xyToHorzVrtc(f.referencePoint);
|
|
822
|
+
if (f.effects) frame.Lefx = serializeEffects(f.effects, false, false);
|
|
823
|
+
if (f.opacity !== undefined) frame.blendOptions = { Opct: unitsPercent(f.opacity) };
|
|
824
|
+
desc.LaSt.push(frame);
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
writeVersionAndDescriptor(writer, '', 'null', desc);
|
|
828
|
+
}, true);
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
if (animationFrameFlags) {
|
|
832
|
+
writeSignature(writer, '8BIM');
|
|
833
|
+
writeSignature(writer, 'mdyn');
|
|
834
|
+
writeUint8(writer, 0); // copy (always false)
|
|
835
|
+
writeZeros(writer, 3);
|
|
836
|
+
writeSection(writer, 2, () => {
|
|
837
|
+
writeUint16(writer, 0); // unknown
|
|
838
|
+
writeUint8(writer, animationFrameFlags.propagateFrameOne ? 0x0 : 0xf);
|
|
839
|
+
writeUint8(writer,
|
|
840
|
+
(animationFrameFlags.unifyLayerPosition ? 1 : 0) |
|
|
841
|
+
(animationFrameFlags.unifyLayerStyle ? 2 : 0) |
|
|
842
|
+
(animationFrameFlags.unifyLayerVisibility ? 4 : 0));
|
|
843
|
+
});
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
if (timeline) {
|
|
847
|
+
writeSignature(writer, '8BIM');
|
|
848
|
+
writeSignature(writer, 'tmln');
|
|
849
|
+
writeUint8(writer, 0); // copy (always false)
|
|
850
|
+
writeZeros(writer, 3);
|
|
851
|
+
writeSection(writer, 2, () => {
|
|
852
|
+
const desc: TimelineDescriptor = {
|
|
853
|
+
Vrsn: 1,
|
|
854
|
+
timeScope: {
|
|
855
|
+
Vrsn: 1,
|
|
856
|
+
Strt: timeline.start,
|
|
857
|
+
duration: timeline.duration,
|
|
858
|
+
inTime: timeline.inTime,
|
|
859
|
+
outTime: timeline.outTime,
|
|
860
|
+
},
|
|
861
|
+
autoScope: timeline.autoScope,
|
|
862
|
+
audioLevel: timeline.audioLevel,
|
|
863
|
+
} as any;
|
|
864
|
+
|
|
865
|
+
if (timeline.tracks) {
|
|
866
|
+
desc.trackList = serializeTrackList(timeline.tracks);
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
const id = options.layerToId.get(target) || target.id || 0;
|
|
870
|
+
if (!id) throw new Error('You need to provide layer.id value whan writing document with animations');
|
|
871
|
+
desc.LyrI = id;
|
|
872
|
+
|
|
873
|
+
// console.log('WRITE:tmln', target.name, target.id, require('util').inspect(desc, false, 99, true));
|
|
874
|
+
writeVersionAndDescriptor(writer, '', 'null', desc, 'anim');
|
|
875
|
+
}, true);
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
if (timestamp !== undefined) {
|
|
879
|
+
writeSignature(writer, '8BIM');
|
|
880
|
+
writeSignature(writer, 'cust');
|
|
881
|
+
writeUint8(writer, 0); // copy (always false)
|
|
882
|
+
writeZeros(writer, 3);
|
|
883
|
+
writeSection(writer, 2, () => {
|
|
884
|
+
const desc: CustomDescriptor = {
|
|
885
|
+
layerTime: timestamp,
|
|
886
|
+
};
|
|
887
|
+
writeVersionAndDescriptor(writer, '', 'metadata', desc);
|
|
888
|
+
}, true);
|
|
889
|
+
}
|
|
890
|
+
},
|
|
891
|
+
);
|
|
892
|
+
|
|
893
|
+
addHandler(
|
|
894
|
+
'vstk',
|
|
895
|
+
hasKey('vectorStroke'),
|
|
896
|
+
(reader, target, left) => {
|
|
897
|
+
const desc = readVersionAndDescriptor(reader) as StrokeDescriptor;
|
|
898
|
+
// console.log(require('util').inspect(desc, false, 99, true));
|
|
899
|
+
|
|
900
|
+
target.vectorStroke = {
|
|
901
|
+
strokeEnabled: desc.strokeEnabled,
|
|
902
|
+
fillEnabled: desc.fillEnabled,
|
|
903
|
+
lineWidth: parseUnits(desc.strokeStyleLineWidth),
|
|
904
|
+
lineDashOffset: parseUnits(desc.strokeStyleLineDashOffset),
|
|
905
|
+
miterLimit: desc.strokeStyleMiterLimit,
|
|
906
|
+
lineCapType: strokeStyleLineCapType.decode(desc.strokeStyleLineCapType),
|
|
907
|
+
lineJoinType: strokeStyleLineJoinType.decode(desc.strokeStyleLineJoinType),
|
|
908
|
+
lineAlignment: strokeStyleLineAlignment.decode(desc.strokeStyleLineAlignment),
|
|
909
|
+
scaleLock: desc.strokeStyleScaleLock,
|
|
910
|
+
strokeAdjust: desc.strokeStyleStrokeAdjust,
|
|
911
|
+
lineDashSet: desc.strokeStyleLineDashSet.map(parseUnits),
|
|
912
|
+
blendMode: BlnM.decode(desc.strokeStyleBlendMode),
|
|
913
|
+
opacity: parsePercent(desc.strokeStyleOpacity),
|
|
914
|
+
content: parseVectorContent(desc.strokeStyleContent),
|
|
915
|
+
resolution: desc.strokeStyleResolution,
|
|
916
|
+
};
|
|
917
|
+
|
|
918
|
+
skipBytes(reader, left());
|
|
919
|
+
},
|
|
920
|
+
(writer, target) => {
|
|
921
|
+
const stroke = target.vectorStroke!;
|
|
922
|
+
const descriptor: StrokeDescriptor = {
|
|
923
|
+
strokeStyleVersion: 2,
|
|
924
|
+
strokeEnabled: !!stroke.strokeEnabled,
|
|
925
|
+
fillEnabled: !!stroke.fillEnabled,
|
|
926
|
+
strokeStyleLineWidth: stroke.lineWidth || { value: 3, units: 'Points' },
|
|
927
|
+
strokeStyleLineDashOffset: stroke.lineDashOffset || { value: 0, units: 'Points' },
|
|
928
|
+
strokeStyleMiterLimit: stroke.miterLimit ?? 100,
|
|
929
|
+
strokeStyleLineCapType: strokeStyleLineCapType.encode(stroke.lineCapType),
|
|
930
|
+
strokeStyleLineJoinType: strokeStyleLineJoinType.encode(stroke.lineJoinType),
|
|
931
|
+
strokeStyleLineAlignment: strokeStyleLineAlignment.encode(stroke.lineAlignment),
|
|
932
|
+
strokeStyleScaleLock: !!stroke.scaleLock,
|
|
933
|
+
strokeStyleStrokeAdjust: !!stroke.strokeAdjust,
|
|
934
|
+
strokeStyleLineDashSet: stroke.lineDashSet || [],
|
|
935
|
+
strokeStyleBlendMode: BlnM.encode(stroke.blendMode),
|
|
936
|
+
strokeStyleOpacity: unitsPercent(stroke.opacity ?? 1),
|
|
937
|
+
strokeStyleContent: serializeVectorContent(
|
|
938
|
+
stroke.content || { type: 'color', color: { r: 0, g: 0, b: 0 } }).descriptor,
|
|
939
|
+
strokeStyleResolution: stroke.resolution ?? 72,
|
|
940
|
+
};
|
|
941
|
+
|
|
942
|
+
writeVersionAndDescriptor(writer, '', 'strokeStyle', descriptor);
|
|
943
|
+
},
|
|
944
|
+
);
|
|
945
|
+
|
|
946
|
+
interface ArtbDescriptor {
|
|
947
|
+
artboardRect: { 'Top ': number; Left: number; Btom: number; Rght: number; };
|
|
948
|
+
guideIndeces: any[];
|
|
949
|
+
artboardPresetName: string;
|
|
950
|
+
'Clr ': DescriptorColor;
|
|
951
|
+
artboardBackgroundType: number;
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
addHandler(
|
|
955
|
+
'artb', // per-layer arboard info
|
|
956
|
+
hasKey('artboard'),
|
|
957
|
+
(reader, target, left) => {
|
|
958
|
+
const desc = readVersionAndDescriptor(reader) as ArtbDescriptor;
|
|
959
|
+
const rect = desc.artboardRect;
|
|
960
|
+
target.artboard = {
|
|
961
|
+
rect: { top: rect['Top '], left: rect.Left, bottom: rect.Btom, right: rect.Rght },
|
|
962
|
+
guideIndices: desc.guideIndeces,
|
|
963
|
+
presetName: desc.artboardPresetName,
|
|
964
|
+
color: parseColor(desc['Clr ']),
|
|
965
|
+
backgroundType: desc.artboardBackgroundType,
|
|
966
|
+
};
|
|
967
|
+
|
|
968
|
+
skipBytes(reader, left());
|
|
969
|
+
},
|
|
970
|
+
(writer, target) => {
|
|
971
|
+
const artboard = target.artboard!;
|
|
972
|
+
const rect = artboard.rect;
|
|
973
|
+
const desc: ArtbDescriptor = {
|
|
974
|
+
artboardRect: { 'Top ': rect.top, Left: rect.left, Btom: rect.bottom, Rght: rect.right },
|
|
975
|
+
guideIndeces: artboard.guideIndices || [],
|
|
976
|
+
artboardPresetName: artboard.presetName || '',
|
|
977
|
+
'Clr ': serializeColor(artboard.color),
|
|
978
|
+
artboardBackgroundType: artboard.backgroundType ?? 1,
|
|
979
|
+
};
|
|
980
|
+
|
|
981
|
+
writeVersionAndDescriptor(writer, '', 'artboard', desc);
|
|
982
|
+
},
|
|
983
|
+
);
|
|
984
|
+
|
|
985
|
+
addHandler(
|
|
986
|
+
'sn2P',
|
|
987
|
+
hasKey('usingAlignedRendering'),
|
|
988
|
+
(reader, target) => target.usingAlignedRendering = !!readUint32(reader),
|
|
989
|
+
(writer, target) => writeUint32(writer, target.usingAlignedRendering ? 1 : 0),
|
|
990
|
+
);
|
|
991
|
+
|
|
992
|
+
const placedLayerTypes: PlacedLayerType[] = ['unknown', 'vector', 'raster', 'image stack'];
|
|
993
|
+
|
|
994
|
+
function parseWarp(warp: WarpDescriptor & QuiltWarpDescriptor): Warp {
|
|
995
|
+
const result: Warp = {
|
|
996
|
+
style: warpStyle.decode(warp.warpStyle),
|
|
997
|
+
...(warp.warpValues ? { values: warp.warpValues } : { value: warp.warpValue || 0 }),
|
|
998
|
+
perspective: warp.warpPerspective || 0,
|
|
999
|
+
perspectiveOther: warp.warpPerspectiveOther || 0,
|
|
1000
|
+
rotate: Ornt.decode(warp.warpRotate),
|
|
1001
|
+
bounds: warp.bounds && {
|
|
1002
|
+
top: parseUnitsOrNumber(warp.bounds['Top ']),
|
|
1003
|
+
left: parseUnitsOrNumber(warp.bounds.Left),
|
|
1004
|
+
bottom: parseUnitsOrNumber(warp.bounds.Btom),
|
|
1005
|
+
right: parseUnitsOrNumber(warp.bounds.Rght),
|
|
1006
|
+
},
|
|
1007
|
+
uOrder: warp.uOrder,
|
|
1008
|
+
vOrder: warp.vOrder,
|
|
1009
|
+
};
|
|
1010
|
+
|
|
1011
|
+
if (warp.deformNumRows != null || warp.deformNumCols != null) {
|
|
1012
|
+
result.deformNumRows = warp.deformNumRows;
|
|
1013
|
+
result.deformNumCols = warp.deformNumCols;
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
const envelopeWarp = warp.customEnvelopeWarp;
|
|
1017
|
+
if (envelopeWarp) {
|
|
1018
|
+
result.customEnvelopeWarp = {
|
|
1019
|
+
meshPoints: [],
|
|
1020
|
+
};
|
|
1021
|
+
|
|
1022
|
+
const xs = envelopeWarp.meshPoints.find(i => i.type === 'Hrzn')?.values || [];
|
|
1023
|
+
const ys = envelopeWarp.meshPoints.find(i => i.type === 'Vrtc')?.values || [];
|
|
1024
|
+
|
|
1025
|
+
for (let i = 0; i < xs.length; i++) {
|
|
1026
|
+
result.customEnvelopeWarp!.meshPoints.push({ x: xs[i], y: ys[i] });
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
if (envelopeWarp.quiltSliceX || envelopeWarp.quiltSliceY) {
|
|
1030
|
+
result.customEnvelopeWarp.quiltSliceX = envelopeWarp.quiltSliceX?.[0]?.values || [];
|
|
1031
|
+
result.customEnvelopeWarp.quiltSliceY = envelopeWarp.quiltSliceY?.[0]?.values || [];
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
return result;
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
function isQuiltWarp(warp: Warp) {
|
|
1039
|
+
return warp.deformNumCols != null || warp.deformNumRows != null ||
|
|
1040
|
+
warp.customEnvelopeWarp?.quiltSliceX || warp.customEnvelopeWarp?.quiltSliceY;
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
function encodeWarp(warp: Warp): WarpDescriptor {
|
|
1044
|
+
const bounds = warp.bounds;
|
|
1045
|
+
const desc: WarpDescriptor = {
|
|
1046
|
+
warpStyle: warpStyle.encode(warp.style),
|
|
1047
|
+
...(warp.values ? { warpValues: warp.values } : { warpValue: warp.value }),
|
|
1048
|
+
warpPerspective: warp.perspective || 0,
|
|
1049
|
+
warpPerspectiveOther: warp.perspectiveOther || 0,
|
|
1050
|
+
warpRotate: Ornt.encode(warp.rotate),
|
|
1051
|
+
bounds: {
|
|
1052
|
+
'Top ': unitsValue(bounds && bounds.top || { units: 'Pixels', value: 0 }, 'bounds.top'),
|
|
1053
|
+
Left: unitsValue(bounds && bounds.left || { units: 'Pixels', value: 0 }, 'bounds.left'),
|
|
1054
|
+
Btom: unitsValue(bounds && bounds.bottom || { units: 'Pixels', value: 0 }, 'bounds.bottom'),
|
|
1055
|
+
Rght: unitsValue(bounds && bounds.right || { units: 'Pixels', value: 0 }, 'bounds.right'),
|
|
1056
|
+
},
|
|
1057
|
+
uOrder: warp.uOrder || 0,
|
|
1058
|
+
vOrder: warp.vOrder || 0,
|
|
1059
|
+
};
|
|
1060
|
+
|
|
1061
|
+
const isQuilt = isQuiltWarp(warp);
|
|
1062
|
+
|
|
1063
|
+
if (isQuilt) {
|
|
1064
|
+
const desc2 = desc as QuiltWarpDescriptor;
|
|
1065
|
+
desc2.deformNumRows = warp.deformNumRows || 0;
|
|
1066
|
+
desc2.deformNumCols = warp.deformNumCols || 0;
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
const customEnvelopeWarp = warp.customEnvelopeWarp;
|
|
1070
|
+
if (customEnvelopeWarp) {
|
|
1071
|
+
const meshPoints = customEnvelopeWarp.meshPoints || [];
|
|
1072
|
+
|
|
1073
|
+
if (isQuilt) {
|
|
1074
|
+
const desc2 = desc as QuiltWarpDescriptor;
|
|
1075
|
+
desc2.customEnvelopeWarp = {
|
|
1076
|
+
quiltSliceX: [{
|
|
1077
|
+
type: 'quiltSliceX',
|
|
1078
|
+
values: customEnvelopeWarp.quiltSliceX || [],
|
|
1079
|
+
}],
|
|
1080
|
+
quiltSliceY: [{
|
|
1081
|
+
type: 'quiltSliceY',
|
|
1082
|
+
values: customEnvelopeWarp.quiltSliceY || [],
|
|
1083
|
+
}],
|
|
1084
|
+
meshPoints: [
|
|
1085
|
+
{ type: 'Hrzn', values: meshPoints.map(p => p.x) },
|
|
1086
|
+
{ type: 'Vrtc', values: meshPoints.map(p => p.y) },
|
|
1087
|
+
],
|
|
1088
|
+
};
|
|
1089
|
+
} else {
|
|
1090
|
+
desc.customEnvelopeWarp = {
|
|
1091
|
+
meshPoints: [
|
|
1092
|
+
{ type: 'Hrzn', values: meshPoints.map(p => p.x) },
|
|
1093
|
+
{ type: 'Vrtc', values: meshPoints.map(p => p.y) },
|
|
1094
|
+
],
|
|
1095
|
+
};
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
return desc;
|
|
1100
|
+
}
|
|
1101
|
+
|
|
1102
|
+
addHandler(
|
|
1103
|
+
'PlLd',
|
|
1104
|
+
hasKey('placedLayer'),
|
|
1105
|
+
(reader, target, left) => {
|
|
1106
|
+
if (readSignature(reader) !== 'plcL') throw new Error(`Invalid PlLd signature`);
|
|
1107
|
+
if (readInt32(reader) !== 3) throw new Error(`Invalid PlLd version`);
|
|
1108
|
+
const id = readPascalString(reader, 1);
|
|
1109
|
+
const pageNumber = readInt32(reader);
|
|
1110
|
+
const totalPages = readInt32(reader); // TODO: check how this works ?
|
|
1111
|
+
readInt32(reader); // anitAliasPolicy 16
|
|
1112
|
+
const placedLayerType = readInt32(reader); // 0 = unknown, 1 = vector, 2 = raster, 3 = image stack
|
|
1113
|
+
if (!placedLayerTypes[placedLayerType]) throw new Error('Invalid PlLd type');
|
|
1114
|
+
const transform: number[] = [];
|
|
1115
|
+
for (let i = 0; i < 8; i++) transform.push(readFloat64(reader)); // x, y of 4 corners of the transform
|
|
1116
|
+
const warpVersion = readInt32(reader);
|
|
1117
|
+
if (warpVersion !== 0) throw new Error(`Invalid Warp version ${warpVersion}`);
|
|
1118
|
+
const warp: WarpDescriptor & QuiltWarpDescriptor = readVersionAndDescriptor(reader);
|
|
1119
|
+
|
|
1120
|
+
target.placedLayer = target.placedLayer || { // skip if SoLd already set it
|
|
1121
|
+
id,
|
|
1122
|
+
type: placedLayerTypes[placedLayerType],
|
|
1123
|
+
pageNumber,
|
|
1124
|
+
totalPages,
|
|
1125
|
+
transform,
|
|
1126
|
+
warp: parseWarp(warp),
|
|
1127
|
+
};
|
|
1128
|
+
|
|
1129
|
+
// console.log('PlLd warp', require('util').inspect(warp, false, 99, true));
|
|
1130
|
+
// console.log('PlLd', require('util').inspect(target.placedLayer, false, 99, true));
|
|
1131
|
+
|
|
1132
|
+
skipBytes(reader, left());
|
|
1133
|
+
},
|
|
1134
|
+
(writer, target) => {
|
|
1135
|
+
const placed = target.placedLayer!;
|
|
1136
|
+
writeSignature(writer, 'plcL');
|
|
1137
|
+
writeInt32(writer, 3); // version
|
|
1138
|
+
writePascalString(writer, placed.id, 1);
|
|
1139
|
+
writeInt32(writer, 1); // pageNumber
|
|
1140
|
+
writeInt32(writer, 1); // totalPages
|
|
1141
|
+
writeInt32(writer, 16); // anitAliasPolicy
|
|
1142
|
+
if (placedLayerTypes.indexOf(placed.type) === -1) throw new Error('Invalid placedLayer type');
|
|
1143
|
+
writeInt32(writer, placedLayerTypes.indexOf(placed.type));
|
|
1144
|
+
for (let i = 0; i < 8; i++) writeFloat64(writer, placed.transform[i]);
|
|
1145
|
+
writeInt32(writer, 0); // warp version
|
|
1146
|
+
const isQuilt = placed.warp && isQuiltWarp(placed.warp);
|
|
1147
|
+
const type = isQuilt ? 'quiltWarp' : 'warp';
|
|
1148
|
+
writeVersionAndDescriptor(writer, '', type, encodeWarp(placed.warp || {}), type);
|
|
1149
|
+
},
|
|
1150
|
+
);
|
|
1151
|
+
|
|
1152
|
+
interface SoLdDescriptor {
|
|
1153
|
+
Idnt: string;
|
|
1154
|
+
placed: string;
|
|
1155
|
+
PgNm: number;
|
|
1156
|
+
totalPages: number;
|
|
1157
|
+
Crop?: number;
|
|
1158
|
+
frameStep: FractionDescriptor;
|
|
1159
|
+
duration: FractionDescriptor;
|
|
1160
|
+
frameCount: number;
|
|
1161
|
+
Annt: number;
|
|
1162
|
+
Type: number;
|
|
1163
|
+
Trnf: number[];
|
|
1164
|
+
nonAffineTransform: number[];
|
|
1165
|
+
quiltWarp?: QuiltWarpDescriptor;
|
|
1166
|
+
warp: WarpDescriptor;
|
|
1167
|
+
'Sz ': { Wdth: number; Hght: number; };
|
|
1168
|
+
Rslt: DescriptorUnitsValue;
|
|
1169
|
+
comp?: number;
|
|
1170
|
+
compInfo?: { compID: number; originalCompID: number; };
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
addHandler(
|
|
1174
|
+
'SoLd',
|
|
1175
|
+
hasKey('placedLayer'),
|
|
1176
|
+
(reader, target, left) => {
|
|
1177
|
+
if (readSignature(reader) !== 'soLD') throw new Error(`Invalid SoLd type`);
|
|
1178
|
+
const version = readInt32(reader);
|
|
1179
|
+
if (version !== 4 && version !== 5) throw new Error(`Invalid SoLd version`);
|
|
1180
|
+
const desc: SoLdDescriptor = readVersionAndDescriptor(reader);
|
|
1181
|
+
// console.log('SoLd', require('util').inspect(desc, false, 99, true));
|
|
1182
|
+
// console.log('SoLd.warp', require('util').inspect(desc.warp, false, 99, true));
|
|
1183
|
+
// console.log('SoLd.quiltWarp', require('util').inspect(desc.quiltWarp, false, 99, true));
|
|
1184
|
+
|
|
1185
|
+
target.placedLayer = {
|
|
1186
|
+
id: desc.Idnt,
|
|
1187
|
+
placed: desc.placed,
|
|
1188
|
+
type: placedLayerTypes[desc.Type],
|
|
1189
|
+
pageNumber: desc.PgNm,
|
|
1190
|
+
totalPages: desc.totalPages,
|
|
1191
|
+
frameStep: desc.frameStep,
|
|
1192
|
+
duration: desc.duration,
|
|
1193
|
+
frameCount: desc.frameCount,
|
|
1194
|
+
transform: desc.Trnf,
|
|
1195
|
+
width: desc['Sz '].Wdth,
|
|
1196
|
+
height: desc['Sz '].Hght,
|
|
1197
|
+
resolution: parseUnits(desc.Rslt),
|
|
1198
|
+
warp: parseWarp((desc.quiltWarp || desc.warp) as any),
|
|
1199
|
+
};
|
|
1200
|
+
|
|
1201
|
+
if (desc.nonAffineTransform && desc.nonAffineTransform.some((x, i) => x !== desc.Trnf[i])) {
|
|
1202
|
+
target.placedLayer.nonAffineTransform = desc.nonAffineTransform;
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
if (desc.Crop) target.placedLayer.crop = desc.Crop;
|
|
1206
|
+
if (desc.comp) target.placedLayer.comp = desc.comp;
|
|
1207
|
+
if (desc.compInfo) target.placedLayer.compInfo = desc.compInfo;
|
|
1208
|
+
|
|
1209
|
+
skipBytes(reader, left()); // HACK
|
|
1210
|
+
},
|
|
1211
|
+
(writer, target) => {
|
|
1212
|
+
writeSignature(writer, 'soLD');
|
|
1213
|
+
writeInt32(writer, 4); // version
|
|
1214
|
+
|
|
1215
|
+
const placed = target.placedLayer!;
|
|
1216
|
+
const desc: SoLdDescriptor = {
|
|
1217
|
+
Idnt: placed.id,
|
|
1218
|
+
placed: placed.placed ?? placed.id,
|
|
1219
|
+
PgNm: placed.pageNumber || 1,
|
|
1220
|
+
totalPages: placed.totalPages || 1,
|
|
1221
|
+
...(placed.crop ? { Crop: placed.crop } : {}),
|
|
1222
|
+
frameStep: placed.frameStep || { numerator: 0, denominator: 600 },
|
|
1223
|
+
duration: placed.duration || { numerator: 0, denominator: 600 },
|
|
1224
|
+
frameCount: placed.frameCount || 0,
|
|
1225
|
+
Annt: 16,
|
|
1226
|
+
Type: placedLayerTypes.indexOf(placed.type),
|
|
1227
|
+
Trnf: placed.transform,
|
|
1228
|
+
nonAffineTransform: placed.nonAffineTransform ?? placed.transform,
|
|
1229
|
+
quiltWarp: {} as any,
|
|
1230
|
+
warp: encodeWarp(placed.warp || {}),
|
|
1231
|
+
'Sz ': {
|
|
1232
|
+
Wdth: placed.width || 0, // TODO: find size ?
|
|
1233
|
+
Hght: placed.height || 0, // TODO: find size ?
|
|
1234
|
+
},
|
|
1235
|
+
Rslt: placed.resolution ? unitsValue(placed.resolution, 'resolution') : { units: 'Density', value: 72 },
|
|
1236
|
+
};
|
|
1237
|
+
|
|
1238
|
+
if (placed.warp && isQuiltWarp(placed.warp)) {
|
|
1239
|
+
const quiltWarp = encodeWarp(placed.warp) as QuiltWarpDescriptor;
|
|
1240
|
+
desc.quiltWarp = quiltWarp;
|
|
1241
|
+
desc.warp = {
|
|
1242
|
+
warpStyle: 'warpStyle.warpNone',
|
|
1243
|
+
warpValue: quiltWarp.warpValue,
|
|
1244
|
+
warpPerspective: quiltWarp.warpPerspective,
|
|
1245
|
+
warpPerspectiveOther: quiltWarp.warpPerspectiveOther,
|
|
1246
|
+
warpRotate: quiltWarp.warpRotate,
|
|
1247
|
+
bounds: quiltWarp.bounds,
|
|
1248
|
+
uOrder: quiltWarp.uOrder,
|
|
1249
|
+
vOrder: quiltWarp.vOrder,
|
|
1250
|
+
};
|
|
1251
|
+
} else {
|
|
1252
|
+
delete desc.quiltWarp;
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
if (placed.comp) desc.comp = placed.comp;
|
|
1256
|
+
if (placed.compInfo) desc.compInfo = placed.compInfo;
|
|
1257
|
+
|
|
1258
|
+
writeVersionAndDescriptor(writer, '', 'null', desc, desc.quiltWarp ? 'quiltWarp' : 'warp');
|
|
1259
|
+
},
|
|
1260
|
+
);
|
|
1261
|
+
|
|
1262
|
+
addHandlerAlias('SoLE', 'SoLd');
|
|
1263
|
+
|
|
1264
|
+
addHandler(
|
|
1265
|
+
'fxrp',
|
|
1266
|
+
hasKey('referencePoint'),
|
|
1267
|
+
(reader, target) => {
|
|
1268
|
+
target.referencePoint = {
|
|
1269
|
+
x: readFloat64(reader),
|
|
1270
|
+
y: readFloat64(reader),
|
|
1271
|
+
};
|
|
1272
|
+
},
|
|
1273
|
+
(writer, target) => {
|
|
1274
|
+
writeFloat64(writer, target.referencePoint!.x);
|
|
1275
|
+
writeFloat64(writer, target.referencePoint!.y);
|
|
1276
|
+
},
|
|
1277
|
+
);
|
|
1278
|
+
|
|
1279
|
+
if (MOCK_HANDLERS) {
|
|
1280
|
+
addHandler(
|
|
1281
|
+
'Patt',
|
|
1282
|
+
target => (target as any)._Patt !== undefined,
|
|
1283
|
+
(reader, target, left) => {
|
|
1284
|
+
// console.log('additional info: Patt');
|
|
1285
|
+
(target as any)._Patt = readBytes(reader, left());
|
|
1286
|
+
},
|
|
1287
|
+
(writer, target) => false && writeBytes(writer, (target as any)._Patt),
|
|
1288
|
+
);
|
|
1289
|
+
} else {
|
|
1290
|
+
addHandler(
|
|
1291
|
+
'Patt', // TODO: handle also Pat2 & Pat3
|
|
1292
|
+
target => !target,
|
|
1293
|
+
(reader, target, left) => {
|
|
1294
|
+
if (!left()) return;
|
|
1295
|
+
|
|
1296
|
+
skipBytes(reader, left()); return; // not supported yet
|
|
1297
|
+
target; readPattern;
|
|
1298
|
+
|
|
1299
|
+
// if (!target.patterns) target.patterns = [];
|
|
1300
|
+
// target.patterns.push(readPattern(reader));
|
|
1301
|
+
// skipBytes(reader, left());
|
|
1302
|
+
},
|
|
1303
|
+
(_writer, _target) => {
|
|
1304
|
+
},
|
|
1305
|
+
);
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
function readRect(reader: PsdReader) {
|
|
1309
|
+
const top = readInt32(reader);
|
|
1310
|
+
const left = readInt32(reader);
|
|
1311
|
+
const bottom = readInt32(reader);
|
|
1312
|
+
const right = readInt32(reader);
|
|
1313
|
+
return { top, left, bottom, right };
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
function writeRect(writer: PsdWriter, rect: { left: number; top: number; right: number; bottom: number }) {
|
|
1317
|
+
writeInt32(writer, rect.top);
|
|
1318
|
+
writeInt32(writer, rect.left);
|
|
1319
|
+
writeInt32(writer, rect.bottom);
|
|
1320
|
+
writeInt32(writer, rect.right);
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
addHandler(
|
|
1324
|
+
'Anno',
|
|
1325
|
+
target => (target as Psd).annotations !== undefined,
|
|
1326
|
+
(reader, target, left) => {
|
|
1327
|
+
const major = readUint16(reader);
|
|
1328
|
+
const minor = readUint16(reader);
|
|
1329
|
+
if (major !== 2 || minor !== 1) throw new Error('Invalid Anno version');
|
|
1330
|
+
const count = readUint32(reader);
|
|
1331
|
+
const annotations: Annotation[] = [];
|
|
1332
|
+
|
|
1333
|
+
for (let i = 0; i < count; i++) {
|
|
1334
|
+
/*const length =*/ readUint32(reader);
|
|
1335
|
+
const type = readSignature(reader);
|
|
1336
|
+
const open = !!readUint8(reader);
|
|
1337
|
+
/*const flags =*/ readUint8(reader); // always 28
|
|
1338
|
+
/*const optionalBlocks =*/ readUint16(reader);
|
|
1339
|
+
const iconLocation = readRect(reader);
|
|
1340
|
+
const popupLocation = readRect(reader);
|
|
1341
|
+
const color = readColor(reader);
|
|
1342
|
+
const author = readPascalString(reader, 2);
|
|
1343
|
+
const name = readPascalString(reader, 2);
|
|
1344
|
+
const date = readPascalString(reader, 2);
|
|
1345
|
+
/*const contentLength =*/ readUint32(reader);
|
|
1346
|
+
/*const dataType =*/ readSignature(reader);
|
|
1347
|
+
const dataLength = readUint32(reader);
|
|
1348
|
+
let data: string | Uint8Array;
|
|
1349
|
+
|
|
1350
|
+
if (type === 'txtA') {
|
|
1351
|
+
if (dataLength >= 2 && readUint16(reader) === 0xfeff) {
|
|
1352
|
+
data = readUnicodeStringWithLength(reader, (dataLength - 2) / 2);
|
|
1353
|
+
} else {
|
|
1354
|
+
reader.offset -= 2;
|
|
1355
|
+
data = readAsciiString(reader, dataLength);
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
data = data.replace(/\r/g, '\n');
|
|
1359
|
+
} else if (type === 'sndA') {
|
|
1360
|
+
data = readBytes(reader, dataLength);
|
|
1361
|
+
} else {
|
|
1362
|
+
throw new Error('Unknown annotation type');
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
annotations.push({
|
|
1366
|
+
type: type === 'txtA' ? 'text' : 'sound', open, iconLocation, popupLocation, color, author, name, date, data,
|
|
1367
|
+
});
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
(target as Psd).annotations = annotations;
|
|
1371
|
+
skipBytes(reader, left());
|
|
1372
|
+
},
|
|
1373
|
+
(writer, target) => {
|
|
1374
|
+
const annotations = (target as Psd).annotations!;
|
|
1375
|
+
|
|
1376
|
+
writeUint16(writer, 2);
|
|
1377
|
+
writeUint16(writer, 1);
|
|
1378
|
+
writeUint32(writer, annotations.length);
|
|
1379
|
+
|
|
1380
|
+
for (const annotation of annotations) {
|
|
1381
|
+
const sound = annotation.type === 'sound';
|
|
1382
|
+
|
|
1383
|
+
if (sound && !(annotation.data instanceof Uint8Array)) throw new Error('Sound annotation data should be Uint8Array');
|
|
1384
|
+
if (!sound && typeof annotation.data !== 'string') throw new Error('Text annotation data should be string');
|
|
1385
|
+
|
|
1386
|
+
const lengthOffset = writer.offset;
|
|
1387
|
+
writeUint32(writer, 0); // length
|
|
1388
|
+
writeSignature(writer, sound ? 'sndA' : 'txtA');
|
|
1389
|
+
writeUint8(writer, annotation.open ? 1 : 0);
|
|
1390
|
+
writeUint8(writer, 28);
|
|
1391
|
+
writeUint16(writer, 1);
|
|
1392
|
+
writeRect(writer, annotation.iconLocation);
|
|
1393
|
+
writeRect(writer, annotation.popupLocation);
|
|
1394
|
+
writeColor(writer, annotation.color);
|
|
1395
|
+
writePascalString(writer, annotation.author || '', 2);
|
|
1396
|
+
writePascalString(writer, annotation.name || '', 2);
|
|
1397
|
+
writePascalString(writer, annotation.date || '', 2);
|
|
1398
|
+
const contentOffset = writer.offset;
|
|
1399
|
+
writeUint32(writer, 0); // content length
|
|
1400
|
+
writeSignature(writer, sound ? 'sndM' : 'txtC');
|
|
1401
|
+
writeUint32(writer, 0); // data length
|
|
1402
|
+
const dataOffset = writer.offset;
|
|
1403
|
+
|
|
1404
|
+
if (sound) {
|
|
1405
|
+
writeBytes(writer, annotation.data as Uint8Array);
|
|
1406
|
+
} else {
|
|
1407
|
+
writeUint16(writer, 0xfeff); // unicode string indicator
|
|
1408
|
+
const text = (annotation.data as string).replace(/\n/g, '\r');
|
|
1409
|
+
for (let i = 0; i < text.length; i++) writeUint16(writer, text.charCodeAt(i));
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
writer.view.setUint32(lengthOffset, writer.offset - lengthOffset, false);
|
|
1413
|
+
writer.view.setUint32(contentOffset, writer.offset - contentOffset, false);
|
|
1414
|
+
writer.view.setUint32(dataOffset - 4, writer.offset - dataOffset, false);
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
);
|
|
1418
|
+
|
|
1419
|
+
interface FileOpenDescriptor {
|
|
1420
|
+
compInfo: { compID: number; originalCompID: number; };
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
addHandler(
|
|
1424
|
+
'lnk2',
|
|
1425
|
+
(target: any) => !!(target as Psd).linkedFiles && (target as Psd).linkedFiles!.length > 0,
|
|
1426
|
+
(reader, target, left, _, options) => {
|
|
1427
|
+
const psd = target as Psd;
|
|
1428
|
+
psd.linkedFiles = psd.linkedFiles || [];
|
|
1429
|
+
|
|
1430
|
+
while (left() > 8) {
|
|
1431
|
+
let size = readLength64(reader); // size
|
|
1432
|
+
const startOffset = reader.offset;
|
|
1433
|
+
const type = readSignature(reader) as 'liFD' | 'liFE' | 'liFA';
|
|
1434
|
+
const version = readInt32(reader);
|
|
1435
|
+
const id = readPascalString(reader, 1);
|
|
1436
|
+
const name = readUnicodeString(reader);
|
|
1437
|
+
const fileType = readSignature(reader).trim(); // ' ' if empty
|
|
1438
|
+
const fileCreator = readSignature(reader).trim(); // ' ' or '\0\0\0\0' if empty
|
|
1439
|
+
const dataSize = readLength64(reader);
|
|
1440
|
+
const hasFileOpenDescriptor = readUint8(reader);
|
|
1441
|
+
const fileOpenDescriptor = hasFileOpenDescriptor ? readVersionAndDescriptor(reader) as FileOpenDescriptor : undefined;
|
|
1442
|
+
const linkedFileDescriptor = type === 'liFE' ? readVersionAndDescriptor(reader) : undefined;
|
|
1443
|
+
const file: LinkedFile = { id, name, data: undefined };
|
|
1444
|
+
|
|
1445
|
+
if (fileType) file.type = fileType;
|
|
1446
|
+
if (fileCreator) file.creator = fileCreator;
|
|
1447
|
+
if (fileOpenDescriptor) file.descriptor = fileOpenDescriptor;
|
|
1448
|
+
|
|
1449
|
+
if (type === 'liFE' && version > 3) {
|
|
1450
|
+
const year = readInt32(reader);
|
|
1451
|
+
const month = readUint8(reader);
|
|
1452
|
+
const day = readUint8(reader);
|
|
1453
|
+
const hour = readUint8(reader);
|
|
1454
|
+
const minute = readUint8(reader);
|
|
1455
|
+
const seconds = readFloat64(reader);
|
|
1456
|
+
const wholeSeconds = Math.floor(seconds);
|
|
1457
|
+
const ms = (seconds - wholeSeconds) * 1000;
|
|
1458
|
+
file.time = (new Date(year, month, day, hour, minute, wholeSeconds, ms)).toISOString();
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
const fileSize = type === 'liFE' ? readLength64(reader) : 0;
|
|
1462
|
+
if (type === 'liFA') skipBytes(reader, 8);
|
|
1463
|
+
if (type === 'liFD') file.data = readBytes(reader, dataSize);
|
|
1464
|
+
if (version >= 5) file.childDocumentID = readUnicodeString(reader);
|
|
1465
|
+
if (version >= 6) file.assetModTime = readFloat64(reader);
|
|
1466
|
+
if (version >= 7) file.assetLockedState = readUint8(reader);
|
|
1467
|
+
if (type === 'liFE') file.data = readBytes(reader, fileSize);
|
|
1468
|
+
|
|
1469
|
+
if (options.skipLinkedFilesData) file.data = undefined;
|
|
1470
|
+
|
|
1471
|
+
psd.linkedFiles.push(file);
|
|
1472
|
+
linkedFileDescriptor;
|
|
1473
|
+
|
|
1474
|
+
while (size % 4) size++;
|
|
1475
|
+
reader.offset = startOffset + size;
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
skipBytes(reader, left()); // ?
|
|
1479
|
+
},
|
|
1480
|
+
(writer, target) => {
|
|
1481
|
+
const psd = target as Psd;
|
|
1482
|
+
|
|
1483
|
+
for (const file of psd.linkedFiles!) {
|
|
1484
|
+
let version = 2;
|
|
1485
|
+
|
|
1486
|
+
if (file.assetLockedState != null) version = 7;
|
|
1487
|
+
else if (file.assetModTime != null) version = 6;
|
|
1488
|
+
else if (file.childDocumentID != null) version = 5;
|
|
1489
|
+
// TODO: else if (file.time != null) version = 3; (only for liFE)
|
|
1490
|
+
|
|
1491
|
+
writeUint32(writer, 0);
|
|
1492
|
+
writeUint32(writer, 0); // size
|
|
1493
|
+
const sizeOffset = writer.offset;
|
|
1494
|
+
writeSignature(writer, file.data ? 'liFD' : 'liFA');
|
|
1495
|
+
writeInt32(writer, version);
|
|
1496
|
+
writePascalString(writer, file.id || '', 1);
|
|
1497
|
+
writeUnicodeStringWithPadding(writer, file.name || '');
|
|
1498
|
+
writeSignature(writer, file.type ? `${file.type} `.substring(0, 4) : ' ');
|
|
1499
|
+
writeSignature(writer, file.creator ? `${file.creator} `.substring(0, 4) : '\0\0\0\0');
|
|
1500
|
+
writeLength64(writer, file.data ? file.data.byteLength : 0);
|
|
1501
|
+
|
|
1502
|
+
if (file.descriptor && file.descriptor.compInfo) {
|
|
1503
|
+
const desc: FileOpenDescriptor = {
|
|
1504
|
+
compInfo: file.descriptor.compInfo,
|
|
1505
|
+
};
|
|
1506
|
+
|
|
1507
|
+
writeUint8(writer, 1);
|
|
1508
|
+
writeVersionAndDescriptor(writer, '', 'null', desc);
|
|
1509
|
+
} else {
|
|
1510
|
+
writeUint8(writer, 0);
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
if (file.data) writeBytes(writer, file.data);
|
|
1514
|
+
else writeLength64(writer, 0);
|
|
1515
|
+
if (version >= 5) writeUnicodeStringWithPadding(writer, file.childDocumentID || '');
|
|
1516
|
+
if (version >= 6) writeFloat64(writer, file.assetModTime || 0);
|
|
1517
|
+
if (version >= 7) writeUint8(writer, file.assetLockedState || 0);
|
|
1518
|
+
|
|
1519
|
+
let size = writer.offset - sizeOffset;
|
|
1520
|
+
writer.view.setUint32(sizeOffset - 4, size, false); // write size
|
|
1521
|
+
|
|
1522
|
+
while (size % 4) {
|
|
1523
|
+
size++;
|
|
1524
|
+
writeUint8(writer, 0);
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
},
|
|
1528
|
+
);
|
|
1529
|
+
|
|
1530
|
+
addHandlerAlias('lnkD', 'lnk2');
|
|
1531
|
+
addHandlerAlias('lnk3', 'lnk2');
|
|
1532
|
+
addHandlerAlias('lnkE', 'lnk2');
|
|
1533
|
+
|
|
1534
|
+
interface ExtensionDesc {
|
|
1535
|
+
generatorSettings: {
|
|
1536
|
+
generator_45_assets: { json: string; };
|
|
1537
|
+
layerTime: number;
|
|
1538
|
+
};
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
addHandler(
|
|
1542
|
+
'pths',
|
|
1543
|
+
hasKey('pathList'),
|
|
1544
|
+
(reader, target) => {
|
|
1545
|
+
const descriptor = readVersionAndDescriptor(reader);
|
|
1546
|
+
|
|
1547
|
+
target.pathList = []; // TODO: read paths (find example with non-empty list)
|
|
1548
|
+
|
|
1549
|
+
descriptor;
|
|
1550
|
+
// console.log('pths', descriptor); // TODO: remove this
|
|
1551
|
+
},
|
|
1552
|
+
(writer, _target) => {
|
|
1553
|
+
const descriptor = {
|
|
1554
|
+
pathList: [], // TODO: write paths
|
|
1555
|
+
};
|
|
1556
|
+
|
|
1557
|
+
writeVersionAndDescriptor(writer, '', 'pathsDataClass', descriptor);
|
|
1558
|
+
},
|
|
1559
|
+
);
|
|
1560
|
+
|
|
1561
|
+
addHandler(
|
|
1562
|
+
'lyvr',
|
|
1563
|
+
hasKey('version'),
|
|
1564
|
+
(reader, target) => target.version = readUint32(reader),
|
|
1565
|
+
(writer, target) => writeUint32(writer, target.version!),
|
|
1566
|
+
);
|
|
1567
|
+
|
|
1568
|
+
function adjustmentType(type: string) {
|
|
1569
|
+
return (target: LayerAdditionalInfo) => !!target.adjustment && target.adjustment.type === type;
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
addHandler(
|
|
1573
|
+
'brit',
|
|
1574
|
+
adjustmentType('brightness/contrast'),
|
|
1575
|
+
(reader, target, left) => {
|
|
1576
|
+
if (!target.adjustment) { // ignore if got one from CgEd block
|
|
1577
|
+
target.adjustment = {
|
|
1578
|
+
type: 'brightness/contrast',
|
|
1579
|
+
brightness: readInt16(reader),
|
|
1580
|
+
contrast: readInt16(reader),
|
|
1581
|
+
meanValue: readInt16(reader),
|
|
1582
|
+
labColorOnly: !!readUint8(reader),
|
|
1583
|
+
useLegacy: true,
|
|
1584
|
+
};
|
|
1585
|
+
}
|
|
1586
|
+
|
|
1587
|
+
skipBytes(reader, left());
|
|
1588
|
+
},
|
|
1589
|
+
(writer, target) => {
|
|
1590
|
+
const info = target.adjustment as BrightnessAdjustment;
|
|
1591
|
+
writeInt16(writer, info.brightness || 0);
|
|
1592
|
+
writeInt16(writer, info.contrast || 0);
|
|
1593
|
+
writeInt16(writer, info.meanValue ?? 127);
|
|
1594
|
+
writeUint8(writer, info.labColorOnly ? 1 : 0);
|
|
1595
|
+
writeZeros(writer, 1);
|
|
1596
|
+
},
|
|
1597
|
+
);
|
|
1598
|
+
|
|
1599
|
+
function readLevelsChannel(reader: PsdReader): LevelsAdjustmentChannel {
|
|
1600
|
+
const shadowInput = readInt16(reader);
|
|
1601
|
+
const highlightInput = readInt16(reader);
|
|
1602
|
+
const shadowOutput = readInt16(reader);
|
|
1603
|
+
const highlightOutput = readInt16(reader);
|
|
1604
|
+
const midtoneInput = readInt16(reader) / 100;
|
|
1605
|
+
return { shadowInput, highlightInput, shadowOutput, highlightOutput, midtoneInput };
|
|
1606
|
+
}
|
|
1607
|
+
|
|
1608
|
+
function writeLevelsChannel(writer: PsdWriter, channel: LevelsAdjustmentChannel) {
|
|
1609
|
+
writeInt16(writer, channel.shadowInput);
|
|
1610
|
+
writeInt16(writer, channel.highlightInput);
|
|
1611
|
+
writeInt16(writer, channel.shadowOutput);
|
|
1612
|
+
writeInt16(writer, channel.highlightOutput);
|
|
1613
|
+
writeInt16(writer, Math.round(channel.midtoneInput * 100));
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1616
|
+
addHandler(
|
|
1617
|
+
'levl',
|
|
1618
|
+
adjustmentType('levels'),
|
|
1619
|
+
(reader, target, left) => {
|
|
1620
|
+
if (readUint16(reader) !== 2) throw new Error('Invalid levl version');
|
|
1621
|
+
|
|
1622
|
+
target.adjustment = {
|
|
1623
|
+
...target.adjustment as PresetInfo,
|
|
1624
|
+
type: 'levels',
|
|
1625
|
+
rgb: readLevelsChannel(reader),
|
|
1626
|
+
red: readLevelsChannel(reader),
|
|
1627
|
+
green: readLevelsChannel(reader),
|
|
1628
|
+
blue: readLevelsChannel(reader),
|
|
1629
|
+
};
|
|
1630
|
+
|
|
1631
|
+
skipBytes(reader, left());
|
|
1632
|
+
},
|
|
1633
|
+
(writer, target) => {
|
|
1634
|
+
const info = target.adjustment as LevelsAdjustment;
|
|
1635
|
+
const defaultChannel = {
|
|
1636
|
+
shadowInput: 0,
|
|
1637
|
+
highlightInput: 255,
|
|
1638
|
+
shadowOutput: 0,
|
|
1639
|
+
highlightOutput: 255,
|
|
1640
|
+
midtoneInput: 1,
|
|
1641
|
+
};
|
|
1642
|
+
|
|
1643
|
+
writeUint16(writer, 2); // version
|
|
1644
|
+
writeLevelsChannel(writer, info.rgb || defaultChannel);
|
|
1645
|
+
writeLevelsChannel(writer, info.red || defaultChannel);
|
|
1646
|
+
writeLevelsChannel(writer, info.blue || defaultChannel);
|
|
1647
|
+
writeLevelsChannel(writer, info.green || defaultChannel);
|
|
1648
|
+
for (let i = 0; i < 59; i++) writeLevelsChannel(writer, defaultChannel);
|
|
1649
|
+
},
|
|
1650
|
+
);
|
|
1651
|
+
|
|
1652
|
+
function readCurveChannel(reader: PsdReader) {
|
|
1653
|
+
const nodes = readUint16(reader);
|
|
1654
|
+
const channel: CurvesAdjustmentChannel = [];
|
|
1655
|
+
|
|
1656
|
+
for (let j = 0; j < nodes; j++) {
|
|
1657
|
+
const output = readInt16(reader);
|
|
1658
|
+
const input = readInt16(reader);
|
|
1659
|
+
channel.push({ input, output });
|
|
1660
|
+
}
|
|
1661
|
+
|
|
1662
|
+
return channel;
|
|
1663
|
+
}
|
|
1664
|
+
|
|
1665
|
+
function writeCurveChannel(writer: PsdWriter, channel: CurvesAdjustmentChannel) {
|
|
1666
|
+
writeUint16(writer, channel.length);
|
|
1667
|
+
|
|
1668
|
+
for (const n of channel) {
|
|
1669
|
+
writeUint16(writer, n.output);
|
|
1670
|
+
writeUint16(writer, n.input);
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1673
|
+
|
|
1674
|
+
addHandler(
|
|
1675
|
+
'curv',
|
|
1676
|
+
adjustmentType('curves'),
|
|
1677
|
+
(reader, target, left) => {
|
|
1678
|
+
readUint8(reader);
|
|
1679
|
+
if (readUint16(reader) !== 1) throw new Error('Invalid curv version');
|
|
1680
|
+
readUint16(reader);
|
|
1681
|
+
const channels = readUint16(reader);
|
|
1682
|
+
const info: CurvesAdjustment = { type: 'curves' };
|
|
1683
|
+
|
|
1684
|
+
if (channels & 1) info.rgb = readCurveChannel(reader);
|
|
1685
|
+
if (channels & 2) info.red = readCurveChannel(reader);
|
|
1686
|
+
if (channels & 4) info.green = readCurveChannel(reader);
|
|
1687
|
+
if (channels & 8) info.blue = readCurveChannel(reader);
|
|
1688
|
+
|
|
1689
|
+
target.adjustment = {
|
|
1690
|
+
...target.adjustment as PresetInfo,
|
|
1691
|
+
...info,
|
|
1692
|
+
};
|
|
1693
|
+
|
|
1694
|
+
// ignoring, duplicate information
|
|
1695
|
+
// checkSignature(reader, 'Crv ');
|
|
1696
|
+
|
|
1697
|
+
// const cVersion = readUint16(reader);
|
|
1698
|
+
// readUint16(reader);
|
|
1699
|
+
// const channelCount = readUint16(reader);
|
|
1700
|
+
|
|
1701
|
+
// for (let i = 0; i < channelCount; i++) {
|
|
1702
|
+
// const index = readUint16(reader);
|
|
1703
|
+
// const nodes = readUint16(reader);
|
|
1704
|
+
|
|
1705
|
+
// for (let j = 0; j < nodes; j++) {
|
|
1706
|
+
// const output = readInt16(reader);
|
|
1707
|
+
// const input = readInt16(reader);
|
|
1708
|
+
// }
|
|
1709
|
+
// }
|
|
1710
|
+
|
|
1711
|
+
skipBytes(reader, left());
|
|
1712
|
+
},
|
|
1713
|
+
(writer, target) => {
|
|
1714
|
+
const info = target.adjustment as CurvesAdjustment;
|
|
1715
|
+
const { rgb, red, green, blue } = info;
|
|
1716
|
+
let channels = 0;
|
|
1717
|
+
let channelCount = 0;
|
|
1718
|
+
|
|
1719
|
+
if (rgb && rgb.length) { channels |= 1; channelCount++; }
|
|
1720
|
+
if (red && red.length) { channels |= 2; channelCount++; }
|
|
1721
|
+
if (green && green.length) { channels |= 4; channelCount++; }
|
|
1722
|
+
if (blue && blue.length) { channels |= 8; channelCount++; }
|
|
1723
|
+
|
|
1724
|
+
writeUint8(writer, 0);
|
|
1725
|
+
writeUint16(writer, 1); // version
|
|
1726
|
+
writeUint16(writer, 0);
|
|
1727
|
+
writeUint16(writer, channels);
|
|
1728
|
+
|
|
1729
|
+
if (rgb && rgb.length) writeCurveChannel(writer, rgb);
|
|
1730
|
+
if (red && red.length) writeCurveChannel(writer, red);
|
|
1731
|
+
if (green && green.length) writeCurveChannel(writer, green);
|
|
1732
|
+
if (blue && blue.length) writeCurveChannel(writer, blue);
|
|
1733
|
+
|
|
1734
|
+
writeSignature(writer, 'Crv ');
|
|
1735
|
+
writeUint16(writer, 4); // version
|
|
1736
|
+
writeUint16(writer, 0);
|
|
1737
|
+
writeUint16(writer, channelCount);
|
|
1738
|
+
|
|
1739
|
+
if (rgb && rgb.length) { writeUint16(writer, 0); writeCurveChannel(writer, rgb); }
|
|
1740
|
+
if (red && red.length) { writeUint16(writer, 1); writeCurveChannel(writer, red); }
|
|
1741
|
+
if (green && green.length) { writeUint16(writer, 2); writeCurveChannel(writer, green); }
|
|
1742
|
+
if (blue && blue.length) { writeUint16(writer, 3); writeCurveChannel(writer, blue); }
|
|
1743
|
+
|
|
1744
|
+
writeZeros(writer, 2);
|
|
1745
|
+
},
|
|
1746
|
+
);
|
|
1747
|
+
|
|
1748
|
+
addHandler(
|
|
1749
|
+
'expA',
|
|
1750
|
+
adjustmentType('exposure'),
|
|
1751
|
+
(reader, target, left) => {
|
|
1752
|
+
if (readUint16(reader) !== 1) throw new Error('Invalid expA version');
|
|
1753
|
+
|
|
1754
|
+
target.adjustment = {
|
|
1755
|
+
...target.adjustment as PresetInfo,
|
|
1756
|
+
type: 'exposure',
|
|
1757
|
+
exposure: readFloat32(reader),
|
|
1758
|
+
offset: readFloat32(reader),
|
|
1759
|
+
gamma: readFloat32(reader),
|
|
1760
|
+
};
|
|
1761
|
+
|
|
1762
|
+
skipBytes(reader, left());
|
|
1763
|
+
},
|
|
1764
|
+
(writer, target) => {
|
|
1765
|
+
const info = target.adjustment as ExposureAdjustment;
|
|
1766
|
+
writeUint16(writer, 1); // version
|
|
1767
|
+
writeFloat32(writer, info.exposure!);
|
|
1768
|
+
writeFloat32(writer, info.offset!);
|
|
1769
|
+
writeFloat32(writer, info.gamma!);
|
|
1770
|
+
writeZeros(writer, 2);
|
|
1771
|
+
},
|
|
1772
|
+
);
|
|
1773
|
+
|
|
1774
|
+
interface VibranceDescriptor {
|
|
1775
|
+
vibrance?: number;
|
|
1776
|
+
Strt?: number;
|
|
1777
|
+
}
|
|
1778
|
+
|
|
1779
|
+
addHandler(
|
|
1780
|
+
'vibA',
|
|
1781
|
+
adjustmentType('vibrance'),
|
|
1782
|
+
(reader, target, left) => {
|
|
1783
|
+
const desc: VibranceDescriptor = readVersionAndDescriptor(reader);
|
|
1784
|
+
target.adjustment = { type: 'vibrance' };
|
|
1785
|
+
if (desc.vibrance !== undefined) target.adjustment.vibrance = desc.vibrance;
|
|
1786
|
+
if (desc.Strt !== undefined) target.adjustment.saturation = desc.Strt;
|
|
1787
|
+
|
|
1788
|
+
skipBytes(reader, left());
|
|
1789
|
+
},
|
|
1790
|
+
(writer, target) => {
|
|
1791
|
+
const info = target.adjustment as VibranceAdjustment;
|
|
1792
|
+
const desc: VibranceDescriptor = {};
|
|
1793
|
+
if (info.vibrance !== undefined) desc.vibrance = info.vibrance;
|
|
1794
|
+
if (info.saturation !== undefined) desc.Strt = info.saturation;
|
|
1795
|
+
|
|
1796
|
+
writeVersionAndDescriptor(writer, '', 'null', desc);
|
|
1797
|
+
},
|
|
1798
|
+
);
|
|
1799
|
+
|
|
1800
|
+
function readHueChannel(reader: PsdReader): HueSaturationAdjustmentChannel {
|
|
1801
|
+
return {
|
|
1802
|
+
a: readInt16(reader),
|
|
1803
|
+
b: readInt16(reader),
|
|
1804
|
+
c: readInt16(reader),
|
|
1805
|
+
d: readInt16(reader),
|
|
1806
|
+
hue: readInt16(reader),
|
|
1807
|
+
saturation: readInt16(reader),
|
|
1808
|
+
lightness: readInt16(reader),
|
|
1809
|
+
};
|
|
1810
|
+
}
|
|
1811
|
+
|
|
1812
|
+
function writeHueChannel(writer: PsdWriter, channel: HueSaturationAdjustmentChannel | undefined) {
|
|
1813
|
+
const c = channel || {} as Partial<HueSaturationAdjustmentChannel>;
|
|
1814
|
+
writeInt16(writer, c.a || 0);
|
|
1815
|
+
writeInt16(writer, c.b || 0);
|
|
1816
|
+
writeInt16(writer, c.c || 0);
|
|
1817
|
+
writeInt16(writer, c.d || 0);
|
|
1818
|
+
writeInt16(writer, c.hue || 0);
|
|
1819
|
+
writeInt16(writer, c.saturation || 0);
|
|
1820
|
+
writeInt16(writer, c.lightness || 0);
|
|
1821
|
+
}
|
|
1822
|
+
|
|
1823
|
+
addHandler(
|
|
1824
|
+
'hue2',
|
|
1825
|
+
adjustmentType('hue/saturation'),
|
|
1826
|
+
(reader, target, left) => {
|
|
1827
|
+
if (readUint16(reader) !== 2) throw new Error('Invalid hue2 version');
|
|
1828
|
+
|
|
1829
|
+
target.adjustment = {
|
|
1830
|
+
...target.adjustment as PresetInfo,
|
|
1831
|
+
type: 'hue/saturation',
|
|
1832
|
+
master: readHueChannel(reader),
|
|
1833
|
+
reds: readHueChannel(reader),
|
|
1834
|
+
yellows: readHueChannel(reader),
|
|
1835
|
+
greens: readHueChannel(reader),
|
|
1836
|
+
cyans: readHueChannel(reader),
|
|
1837
|
+
blues: readHueChannel(reader),
|
|
1838
|
+
magentas: readHueChannel(reader),
|
|
1839
|
+
};
|
|
1840
|
+
|
|
1841
|
+
skipBytes(reader, left());
|
|
1842
|
+
},
|
|
1843
|
+
(writer, target) => {
|
|
1844
|
+
const info = target.adjustment as HueSaturationAdjustment;
|
|
1845
|
+
|
|
1846
|
+
writeUint16(writer, 2); // version
|
|
1847
|
+
writeHueChannel(writer, info.master);
|
|
1848
|
+
writeHueChannel(writer, info.reds);
|
|
1849
|
+
writeHueChannel(writer, info.yellows);
|
|
1850
|
+
writeHueChannel(writer, info.greens);
|
|
1851
|
+
writeHueChannel(writer, info.cyans);
|
|
1852
|
+
writeHueChannel(writer, info.blues);
|
|
1853
|
+
writeHueChannel(writer, info.magentas);
|
|
1854
|
+
},
|
|
1855
|
+
);
|
|
1856
|
+
|
|
1857
|
+
function readColorBalance(reader: PsdReader): ColorBalanceValues {
|
|
1858
|
+
return {
|
|
1859
|
+
cyanRed: readInt16(reader),
|
|
1860
|
+
magentaGreen: readInt16(reader),
|
|
1861
|
+
yellowBlue: readInt16(reader),
|
|
1862
|
+
};
|
|
1863
|
+
}
|
|
1864
|
+
|
|
1865
|
+
function writeColorBalance(writer: PsdWriter, value: Partial<ColorBalanceValues>) {
|
|
1866
|
+
writeInt16(writer, value.cyanRed || 0);
|
|
1867
|
+
writeInt16(writer, value.magentaGreen || 0);
|
|
1868
|
+
writeInt16(writer, value.yellowBlue || 0);
|
|
1869
|
+
}
|
|
1870
|
+
|
|
1871
|
+
addHandler(
|
|
1872
|
+
'blnc',
|
|
1873
|
+
adjustmentType('color balance'),
|
|
1874
|
+
(reader, target, left) => {
|
|
1875
|
+
target.adjustment = {
|
|
1876
|
+
type: 'color balance',
|
|
1877
|
+
shadows: readColorBalance(reader),
|
|
1878
|
+
midtones: readColorBalance(reader),
|
|
1879
|
+
highlights: readColorBalance(reader),
|
|
1880
|
+
preserveLuminosity: !!readUint8(reader),
|
|
1881
|
+
};
|
|
1882
|
+
|
|
1883
|
+
skipBytes(reader, left());
|
|
1884
|
+
},
|
|
1885
|
+
(writer, target) => {
|
|
1886
|
+
const info = target.adjustment as ColorBalanceAdjustment;
|
|
1887
|
+
writeColorBalance(writer, info.shadows || {});
|
|
1888
|
+
writeColorBalance(writer, info.midtones || {});
|
|
1889
|
+
writeColorBalance(writer, info.highlights || {});
|
|
1890
|
+
writeUint8(writer, info.preserveLuminosity ? 1 : 0);
|
|
1891
|
+
writeZeros(writer, 1);
|
|
1892
|
+
},
|
|
1893
|
+
);
|
|
1894
|
+
|
|
1895
|
+
interface BlackAndWhiteDescriptor {
|
|
1896
|
+
'Rd ': number;
|
|
1897
|
+
Yllw: number;
|
|
1898
|
+
'Grn ': number;
|
|
1899
|
+
'Cyn ': number;
|
|
1900
|
+
'Bl ': number;
|
|
1901
|
+
Mgnt: number;
|
|
1902
|
+
useTint: boolean;
|
|
1903
|
+
tintColor?: DescriptorColor;
|
|
1904
|
+
bwPresetKind: number;
|
|
1905
|
+
blackAndWhitePresetFileName: string;
|
|
1906
|
+
}
|
|
1907
|
+
|
|
1908
|
+
addHandler(
|
|
1909
|
+
'blwh',
|
|
1910
|
+
adjustmentType('black & white'),
|
|
1911
|
+
(reader, target, left) => {
|
|
1912
|
+
const desc: BlackAndWhiteDescriptor = readVersionAndDescriptor(reader);
|
|
1913
|
+
target.adjustment = {
|
|
1914
|
+
type: 'black & white',
|
|
1915
|
+
reds: desc['Rd '],
|
|
1916
|
+
yellows: desc.Yllw,
|
|
1917
|
+
greens: desc['Grn '],
|
|
1918
|
+
cyans: desc['Cyn '],
|
|
1919
|
+
blues: desc['Bl '],
|
|
1920
|
+
magentas: desc.Mgnt,
|
|
1921
|
+
useTint: !!desc.useTint,
|
|
1922
|
+
presetKind: desc.bwPresetKind,
|
|
1923
|
+
presetFileName: desc.blackAndWhitePresetFileName,
|
|
1924
|
+
};
|
|
1925
|
+
|
|
1926
|
+
if (desc.tintColor !== undefined) target.adjustment.tintColor = parseColor(desc.tintColor);
|
|
1927
|
+
|
|
1928
|
+
skipBytes(reader, left());
|
|
1929
|
+
},
|
|
1930
|
+
(writer, target) => {
|
|
1931
|
+
const info = target.adjustment as BlackAndWhiteAdjustment;
|
|
1932
|
+
const desc: BlackAndWhiteDescriptor = {
|
|
1933
|
+
'Rd ': info.reds || 0,
|
|
1934
|
+
Yllw: info.yellows || 0,
|
|
1935
|
+
'Grn ': info.greens || 0,
|
|
1936
|
+
'Cyn ': info.cyans || 0,
|
|
1937
|
+
'Bl ': info.blues || 0,
|
|
1938
|
+
Mgnt: info.magentas || 0,
|
|
1939
|
+
useTint: !!info.useTint,
|
|
1940
|
+
tintColor: serializeColor(info.tintColor),
|
|
1941
|
+
bwPresetKind: info.presetKind || 0,
|
|
1942
|
+
blackAndWhitePresetFileName: info.presetFileName || '',
|
|
1943
|
+
};
|
|
1944
|
+
|
|
1945
|
+
writeVersionAndDescriptor(writer, '', 'null', desc);
|
|
1946
|
+
},
|
|
1947
|
+
);
|
|
1948
|
+
|
|
1949
|
+
addHandler(
|
|
1950
|
+
'phfl',
|
|
1951
|
+
adjustmentType('photo filter'),
|
|
1952
|
+
(reader, target, left) => {
|
|
1953
|
+
const version = readUint16(reader);
|
|
1954
|
+
if (version !== 2 && version !== 3) throw new Error('Invalid phfl version');
|
|
1955
|
+
|
|
1956
|
+
let color: Color;
|
|
1957
|
+
|
|
1958
|
+
if (version === 2) {
|
|
1959
|
+
color = readColor(reader);
|
|
1960
|
+
} else { // version 3
|
|
1961
|
+
// TODO: test this, this is probably wrong
|
|
1962
|
+
color = {
|
|
1963
|
+
l: readInt32(reader) / 100,
|
|
1964
|
+
a: readInt32(reader) / 100,
|
|
1965
|
+
b: readInt32(reader) / 100,
|
|
1966
|
+
};
|
|
1967
|
+
}
|
|
1968
|
+
|
|
1969
|
+
target.adjustment = {
|
|
1970
|
+
type: 'photo filter',
|
|
1971
|
+
color,
|
|
1972
|
+
density: readUint32(reader) / 100,
|
|
1973
|
+
preserveLuminosity: !!readUint8(reader),
|
|
1974
|
+
};
|
|
1975
|
+
|
|
1976
|
+
skipBytes(reader, left());
|
|
1977
|
+
},
|
|
1978
|
+
(writer, target) => {
|
|
1979
|
+
const info = target.adjustment as PhotoFilterAdjustment;
|
|
1980
|
+
writeUint16(writer, 2); // version
|
|
1981
|
+
writeColor(writer, info.color || { l: 0, a: 0, b: 0 });
|
|
1982
|
+
writeUint32(writer, (info.density || 0) * 100);
|
|
1983
|
+
writeUint8(writer, info.preserveLuminosity ? 1 : 0);
|
|
1984
|
+
writeZeros(writer, 3);
|
|
1985
|
+
},
|
|
1986
|
+
);
|
|
1987
|
+
|
|
1988
|
+
function readMixrChannel(reader: PsdReader): ChannelMixerChannel {
|
|
1989
|
+
const red = readInt16(reader);
|
|
1990
|
+
const green = readInt16(reader);
|
|
1991
|
+
const blue = readInt16(reader);
|
|
1992
|
+
skipBytes(reader, 2);
|
|
1993
|
+
const constant = readInt16(reader);
|
|
1994
|
+
return { red, green, blue, constant };
|
|
1995
|
+
}
|
|
1996
|
+
|
|
1997
|
+
function writeMixrChannel(writer: PsdWriter, channel: ChannelMixerChannel | undefined) {
|
|
1998
|
+
const c = channel || {} as Partial<ChannelMixerChannel>;
|
|
1999
|
+
writeInt16(writer, c.red!);
|
|
2000
|
+
writeInt16(writer, c.green!);
|
|
2001
|
+
writeInt16(writer, c.blue!);
|
|
2002
|
+
writeZeros(writer, 2);
|
|
2003
|
+
writeInt16(writer, c.constant!);
|
|
2004
|
+
}
|
|
2005
|
+
|
|
2006
|
+
addHandler(
|
|
2007
|
+
'mixr',
|
|
2008
|
+
adjustmentType('channel mixer'),
|
|
2009
|
+
(reader, target, left) => {
|
|
2010
|
+
if (readUint16(reader) !== 1) throw new Error('Invalid mixr version');
|
|
2011
|
+
|
|
2012
|
+
const adjustment: ChannelMixerAdjustment = target.adjustment = {
|
|
2013
|
+
...target.adjustment as PresetInfo,
|
|
2014
|
+
type: 'channel mixer',
|
|
2015
|
+
monochrome: !!readUint16(reader),
|
|
2016
|
+
};
|
|
2017
|
+
|
|
2018
|
+
if (!adjustment.monochrome) {
|
|
2019
|
+
adjustment.red = readMixrChannel(reader);
|
|
2020
|
+
adjustment.green = readMixrChannel(reader);
|
|
2021
|
+
adjustment.blue = readMixrChannel(reader);
|
|
2022
|
+
}
|
|
2023
|
+
|
|
2024
|
+
adjustment.gray = readMixrChannel(reader);
|
|
2025
|
+
|
|
2026
|
+
skipBytes(reader, left());
|
|
2027
|
+
},
|
|
2028
|
+
(writer, target) => {
|
|
2029
|
+
const info = target.adjustment as ChannelMixerAdjustment;
|
|
2030
|
+
writeUint16(writer, 1); // version
|
|
2031
|
+
writeUint16(writer, info.monochrome ? 1 : 0);
|
|
2032
|
+
|
|
2033
|
+
if (info.monochrome) {
|
|
2034
|
+
writeMixrChannel(writer, info.gray);
|
|
2035
|
+
writeZeros(writer, 3 * 5 * 2);
|
|
2036
|
+
} else {
|
|
2037
|
+
writeMixrChannel(writer, info.red);
|
|
2038
|
+
writeMixrChannel(writer, info.green);
|
|
2039
|
+
writeMixrChannel(writer, info.blue);
|
|
2040
|
+
writeMixrChannel(writer, info.gray);
|
|
2041
|
+
}
|
|
2042
|
+
},
|
|
2043
|
+
);
|
|
2044
|
+
|
|
2045
|
+
const colorLookupType = createEnum<'3dlut' | 'abstractProfile' | 'deviceLinkProfile'>('colorLookupType', '3DLUT', {
|
|
2046
|
+
'3dlut': '3DLUT',
|
|
2047
|
+
abstractProfile: 'abstractProfile',
|
|
2048
|
+
deviceLinkProfile: 'deviceLinkProfile',
|
|
2049
|
+
});
|
|
2050
|
+
|
|
2051
|
+
const LUTFormatType = createEnum<'look' | 'cube' | '3dl'>('LUTFormatType', 'look', {
|
|
2052
|
+
look: 'LUTFormatLOOK',
|
|
2053
|
+
cube: 'LUTFormatCUBE',
|
|
2054
|
+
'3dl': 'LUTFormat3DL',
|
|
2055
|
+
});
|
|
2056
|
+
|
|
2057
|
+
const colorLookupOrder = createEnum<'rgb' | 'bgr'>('colorLookupOrder', 'rgb', {
|
|
2058
|
+
rgb: 'rgbOrder',
|
|
2059
|
+
bgr: 'bgrOrder',
|
|
2060
|
+
});
|
|
2061
|
+
|
|
2062
|
+
interface ColorLookupDescriptor {
|
|
2063
|
+
lookupType?: string;
|
|
2064
|
+
'Nm '?: string;
|
|
2065
|
+
Dthr?: boolean;
|
|
2066
|
+
profile?: Uint8Array;
|
|
2067
|
+
LUTFormat?: string;
|
|
2068
|
+
dataOrder?: string;
|
|
2069
|
+
tableOrder?: string;
|
|
2070
|
+
LUT3DFileData?: Uint8Array;
|
|
2071
|
+
LUT3DFileName?: string;
|
|
2072
|
+
}
|
|
2073
|
+
|
|
2074
|
+
addHandler(
|
|
2075
|
+
'clrL',
|
|
2076
|
+
adjustmentType('color lookup'),
|
|
2077
|
+
(reader, target, left) => {
|
|
2078
|
+
if (readUint16(reader) !== 1) throw new Error('Invalid clrL version');
|
|
2079
|
+
|
|
2080
|
+
const desc: ColorLookupDescriptor = readVersionAndDescriptor(reader);
|
|
2081
|
+
target.adjustment = { type: 'color lookup' };
|
|
2082
|
+
const info = target.adjustment;
|
|
2083
|
+
|
|
2084
|
+
if (desc.lookupType !== undefined) info.lookupType = colorLookupType.decode(desc.lookupType);
|
|
2085
|
+
if (desc['Nm '] !== undefined) info.name = desc['Nm '];
|
|
2086
|
+
if (desc.Dthr !== undefined) info.dither = desc.Dthr;
|
|
2087
|
+
if (desc.profile !== undefined) info.profile = desc.profile;
|
|
2088
|
+
if (desc.LUTFormat !== undefined) info.lutFormat = LUTFormatType.decode(desc.LUTFormat);
|
|
2089
|
+
if (desc.dataOrder !== undefined) info.dataOrder = colorLookupOrder.decode(desc.dataOrder);
|
|
2090
|
+
if (desc.tableOrder !== undefined) info.tableOrder = colorLookupOrder.decode(desc.tableOrder);
|
|
2091
|
+
if (desc.LUT3DFileData !== undefined) info.lut3DFileData = desc.LUT3DFileData;
|
|
2092
|
+
if (desc.LUT3DFileName !== undefined) info.lut3DFileName = desc.LUT3DFileName;
|
|
2093
|
+
|
|
2094
|
+
skipBytes(reader, left());
|
|
2095
|
+
},
|
|
2096
|
+
(writer, target) => {
|
|
2097
|
+
const info = target.adjustment as ColorLookupAdjustment;
|
|
2098
|
+
const desc: ColorLookupDescriptor = {};
|
|
2099
|
+
|
|
2100
|
+
if (info.lookupType !== undefined) desc.lookupType = colorLookupType.encode(info.lookupType);
|
|
2101
|
+
if (info.name !== undefined) desc['Nm '] = info.name;
|
|
2102
|
+
if (info.dither !== undefined) desc.Dthr = info.dither;
|
|
2103
|
+
if (info.profile !== undefined) desc.profile = info.profile;
|
|
2104
|
+
if (info.lutFormat !== undefined) desc.LUTFormat = LUTFormatType.encode(info.lutFormat);
|
|
2105
|
+
if (info.dataOrder !== undefined) desc.dataOrder = colorLookupOrder.encode(info.dataOrder);
|
|
2106
|
+
if (info.tableOrder !== undefined) desc.tableOrder = colorLookupOrder.encode(info.tableOrder);
|
|
2107
|
+
if (info.lut3DFileData !== undefined) desc.LUT3DFileData = info.lut3DFileData;
|
|
2108
|
+
if (info.lut3DFileName !== undefined) desc.LUT3DFileName = info.lut3DFileName;
|
|
2109
|
+
|
|
2110
|
+
writeUint16(writer, 1); // version
|
|
2111
|
+
writeVersionAndDescriptor(writer, '', 'null', desc);
|
|
2112
|
+
},
|
|
2113
|
+
);
|
|
2114
|
+
|
|
2115
|
+
addHandler(
|
|
2116
|
+
'nvrt',
|
|
2117
|
+
adjustmentType('invert'),
|
|
2118
|
+
(reader, target, left) => {
|
|
2119
|
+
target.adjustment = { type: 'invert' };
|
|
2120
|
+
skipBytes(reader, left());
|
|
2121
|
+
},
|
|
2122
|
+
() => {
|
|
2123
|
+
// nothing to write here
|
|
2124
|
+
},
|
|
2125
|
+
);
|
|
2126
|
+
|
|
2127
|
+
addHandler(
|
|
2128
|
+
'post',
|
|
2129
|
+
adjustmentType('posterize'),
|
|
2130
|
+
(reader, target, left) => {
|
|
2131
|
+
target.adjustment = {
|
|
2132
|
+
type: 'posterize',
|
|
2133
|
+
levels: readUint16(reader),
|
|
2134
|
+
};
|
|
2135
|
+
skipBytes(reader, left());
|
|
2136
|
+
},
|
|
2137
|
+
(writer, target) => {
|
|
2138
|
+
const info = target.adjustment as PosterizeAdjustment;
|
|
2139
|
+
writeUint16(writer, info.levels ?? 4);
|
|
2140
|
+
writeZeros(writer, 2);
|
|
2141
|
+
},
|
|
2142
|
+
);
|
|
2143
|
+
|
|
2144
|
+
addHandler(
|
|
2145
|
+
'thrs',
|
|
2146
|
+
adjustmentType('threshold'),
|
|
2147
|
+
(reader, target, left) => {
|
|
2148
|
+
target.adjustment = {
|
|
2149
|
+
type: 'threshold',
|
|
2150
|
+
level: readUint16(reader),
|
|
2151
|
+
};
|
|
2152
|
+
skipBytes(reader, left());
|
|
2153
|
+
},
|
|
2154
|
+
(writer, target) => {
|
|
2155
|
+
const info = target.adjustment as ThresholdAdjustment;
|
|
2156
|
+
writeUint16(writer, info.level ?? 128);
|
|
2157
|
+
writeZeros(writer, 2);
|
|
2158
|
+
},
|
|
2159
|
+
);
|
|
2160
|
+
|
|
2161
|
+
const grdmColorModels = ['', '', '', 'rgb', 'hsb', '', 'lab'];
|
|
2162
|
+
|
|
2163
|
+
addHandler(
|
|
2164
|
+
'grdm',
|
|
2165
|
+
adjustmentType('gradient map'),
|
|
2166
|
+
(reader, target, left) => {
|
|
2167
|
+
if (readUint16(reader) !== 1) throw new Error('Invalid grdm version');
|
|
2168
|
+
|
|
2169
|
+
const info: GradientMapAdjustment = {
|
|
2170
|
+
type: 'gradient map',
|
|
2171
|
+
gradientType: 'solid',
|
|
2172
|
+
};
|
|
2173
|
+
|
|
2174
|
+
info.reverse = !!readUint8(reader);
|
|
2175
|
+
info.dither = !!readUint8(reader);
|
|
2176
|
+
info.name = readUnicodeString(reader);
|
|
2177
|
+
info.colorStops = [];
|
|
2178
|
+
info.opacityStops = [];
|
|
2179
|
+
|
|
2180
|
+
const stopsCount = readUint16(reader);
|
|
2181
|
+
|
|
2182
|
+
for (let i = 0; i < stopsCount; i++) {
|
|
2183
|
+
info.colorStops.push({
|
|
2184
|
+
location: readUint32(reader),
|
|
2185
|
+
midpoint: readUint32(reader) / 100,
|
|
2186
|
+
color: readColor(reader),
|
|
2187
|
+
});
|
|
2188
|
+
skipBytes(reader, 2);
|
|
2189
|
+
}
|
|
2190
|
+
|
|
2191
|
+
const opacityStopsCount = readUint16(reader);
|
|
2192
|
+
|
|
2193
|
+
for (let i = 0; i < opacityStopsCount; i++) {
|
|
2194
|
+
info.opacityStops.push({
|
|
2195
|
+
location: readUint32(reader),
|
|
2196
|
+
midpoint: readUint32(reader) / 100,
|
|
2197
|
+
opacity: readUint16(reader) / 0xff,
|
|
2198
|
+
});
|
|
2199
|
+
}
|
|
2200
|
+
|
|
2201
|
+
const expansionCount = readUint16(reader);
|
|
2202
|
+
if (expansionCount !== 2) throw new Error('Invalid grdm expansion count');
|
|
2203
|
+
|
|
2204
|
+
const interpolation = readUint16(reader);
|
|
2205
|
+
info.smoothness = interpolation / 4096;
|
|
2206
|
+
|
|
2207
|
+
const length = readUint16(reader);
|
|
2208
|
+
if (length !== 32) throw new Error('Invalid grdm length');
|
|
2209
|
+
|
|
2210
|
+
info.gradientType = readUint16(reader) ? 'noise' : 'solid';
|
|
2211
|
+
info.randomSeed = readUint32(reader);
|
|
2212
|
+
info.addTransparency = !!readUint16(reader);
|
|
2213
|
+
info.restrictColors = !!readUint16(reader);
|
|
2214
|
+
info.roughness = readUint32(reader) / 4096;
|
|
2215
|
+
info.colorModel = (grdmColorModels[readUint16(reader)] || 'rgb') as 'rgb' | 'hsb' | 'lab';
|
|
2216
|
+
|
|
2217
|
+
info.min = [
|
|
2218
|
+
readUint16(reader) / 0x8000,
|
|
2219
|
+
readUint16(reader) / 0x8000,
|
|
2220
|
+
readUint16(reader) / 0x8000,
|
|
2221
|
+
readUint16(reader) / 0x8000,
|
|
2222
|
+
];
|
|
2223
|
+
|
|
2224
|
+
info.max = [
|
|
2225
|
+
readUint16(reader) / 0x8000,
|
|
2226
|
+
readUint16(reader) / 0x8000,
|
|
2227
|
+
readUint16(reader) / 0x8000,
|
|
2228
|
+
readUint16(reader) / 0x8000,
|
|
2229
|
+
];
|
|
2230
|
+
|
|
2231
|
+
skipBytes(reader, left());
|
|
2232
|
+
|
|
2233
|
+
for (const s of info.colorStops) s.location /= interpolation;
|
|
2234
|
+
for (const s of info.opacityStops) s.location /= interpolation;
|
|
2235
|
+
|
|
2236
|
+
target.adjustment = info;
|
|
2237
|
+
},
|
|
2238
|
+
(writer, target) => {
|
|
2239
|
+
const info = target.adjustment as GradientMapAdjustment;
|
|
2240
|
+
|
|
2241
|
+
writeUint16(writer, 1); // version
|
|
2242
|
+
writeUint8(writer, info.reverse ? 1 : 0);
|
|
2243
|
+
writeUint8(writer, info.dither ? 1 : 0);
|
|
2244
|
+
writeUnicodeStringWithPadding(writer, info.name || '');
|
|
2245
|
+
writeUint16(writer, info.colorStops && info.colorStops.length || 0);
|
|
2246
|
+
|
|
2247
|
+
const interpolation = Math.round((info.smoothness ?? 1) * 4096);
|
|
2248
|
+
|
|
2249
|
+
for (const s of info.colorStops || []) {
|
|
2250
|
+
writeUint32(writer, Math.round(s.location * interpolation));
|
|
2251
|
+
writeUint32(writer, Math.round(s.midpoint * 100));
|
|
2252
|
+
writeColor(writer, s.color);
|
|
2253
|
+
writeZeros(writer, 2);
|
|
2254
|
+
}
|
|
2255
|
+
|
|
2256
|
+
writeUint16(writer, info.opacityStops && info.opacityStops.length || 0);
|
|
2257
|
+
|
|
2258
|
+
for (const s of info.opacityStops || []) {
|
|
2259
|
+
writeUint32(writer, Math.round(s.location * interpolation));
|
|
2260
|
+
writeUint32(writer, Math.round(s.midpoint * 100));
|
|
2261
|
+
writeUint16(writer, Math.round(s.opacity * 0xff));
|
|
2262
|
+
}
|
|
2263
|
+
|
|
2264
|
+
writeUint16(writer, 2); // expansion count
|
|
2265
|
+
writeUint16(writer, interpolation);
|
|
2266
|
+
writeUint16(writer, 32); // length
|
|
2267
|
+
writeUint16(writer, info.gradientType === 'noise' ? 1 : 0);
|
|
2268
|
+
writeUint32(writer, info.randomSeed || 0);
|
|
2269
|
+
writeUint16(writer, info.addTransparency ? 1 : 0);
|
|
2270
|
+
writeUint16(writer, info.restrictColors ? 1 : 0);
|
|
2271
|
+
writeUint32(writer, Math.round((info.roughness ?? 1) * 4096));
|
|
2272
|
+
const colorModel = grdmColorModels.indexOf(info.colorModel ?? 'rgb');
|
|
2273
|
+
writeUint16(writer, colorModel === -1 ? 3 : colorModel);
|
|
2274
|
+
|
|
2275
|
+
for (let i = 0; i < 4; i++)
|
|
2276
|
+
writeUint16(writer, Math.round((info.min && info.min[i] || 0) * 0x8000));
|
|
2277
|
+
|
|
2278
|
+
for (let i = 0; i < 4; i++)
|
|
2279
|
+
writeUint16(writer, Math.round((info.max && info.max[i] || 0) * 0x8000));
|
|
2280
|
+
|
|
2281
|
+
writeZeros(writer, 4);
|
|
2282
|
+
},
|
|
2283
|
+
);
|
|
2284
|
+
|
|
2285
|
+
function readSelectiveColors(reader: PsdReader): CMYK {
|
|
2286
|
+
return {
|
|
2287
|
+
c: readInt16(reader),
|
|
2288
|
+
m: readInt16(reader),
|
|
2289
|
+
y: readInt16(reader),
|
|
2290
|
+
k: readInt16(reader),
|
|
2291
|
+
};
|
|
2292
|
+
}
|
|
2293
|
+
|
|
2294
|
+
function writeSelectiveColors(writer: PsdWriter, cmyk: CMYK | undefined) {
|
|
2295
|
+
const c = cmyk || {} as Partial<CMYK>;
|
|
2296
|
+
writeInt16(writer, c.c!);
|
|
2297
|
+
writeInt16(writer, c.m!);
|
|
2298
|
+
writeInt16(writer, c.y!);
|
|
2299
|
+
writeInt16(writer, c.k!);
|
|
2300
|
+
}
|
|
2301
|
+
|
|
2302
|
+
addHandler(
|
|
2303
|
+
'selc',
|
|
2304
|
+
adjustmentType('selective color'),
|
|
2305
|
+
(reader, target) => {
|
|
2306
|
+
if (readUint16(reader) !== 1) throw new Error('Invalid selc version');
|
|
2307
|
+
|
|
2308
|
+
const mode = readUint16(reader) ? 'absolute' : 'relative';
|
|
2309
|
+
skipBytes(reader, 8);
|
|
2310
|
+
|
|
2311
|
+
target.adjustment = {
|
|
2312
|
+
type: 'selective color',
|
|
2313
|
+
mode,
|
|
2314
|
+
reds: readSelectiveColors(reader),
|
|
2315
|
+
yellows: readSelectiveColors(reader),
|
|
2316
|
+
greens: readSelectiveColors(reader),
|
|
2317
|
+
cyans: readSelectiveColors(reader),
|
|
2318
|
+
blues: readSelectiveColors(reader),
|
|
2319
|
+
magentas: readSelectiveColors(reader),
|
|
2320
|
+
whites: readSelectiveColors(reader),
|
|
2321
|
+
neutrals: readSelectiveColors(reader),
|
|
2322
|
+
blacks: readSelectiveColors(reader),
|
|
2323
|
+
};
|
|
2324
|
+
},
|
|
2325
|
+
(writer, target) => {
|
|
2326
|
+
const info = target.adjustment as SelectiveColorAdjustment;
|
|
2327
|
+
|
|
2328
|
+
writeUint16(writer, 1); // version
|
|
2329
|
+
writeUint16(writer, info.mode === 'absolute' ? 1 : 0);
|
|
2330
|
+
writeZeros(writer, 8);
|
|
2331
|
+
writeSelectiveColors(writer, info.reds);
|
|
2332
|
+
writeSelectiveColors(writer, info.yellows);
|
|
2333
|
+
writeSelectiveColors(writer, info.greens);
|
|
2334
|
+
writeSelectiveColors(writer, info.cyans);
|
|
2335
|
+
writeSelectiveColors(writer, info.blues);
|
|
2336
|
+
writeSelectiveColors(writer, info.magentas);
|
|
2337
|
+
writeSelectiveColors(writer, info.whites);
|
|
2338
|
+
writeSelectiveColors(writer, info.neutrals);
|
|
2339
|
+
writeSelectiveColors(writer, info.blacks);
|
|
2340
|
+
},
|
|
2341
|
+
);
|
|
2342
|
+
|
|
2343
|
+
interface BrightnessContrastDescriptor {
|
|
2344
|
+
Vrsn: number;
|
|
2345
|
+
Brgh: number;
|
|
2346
|
+
Cntr: number;
|
|
2347
|
+
means: number;
|
|
2348
|
+
'Lab ': boolean;
|
|
2349
|
+
useLegacy: boolean;
|
|
2350
|
+
Auto: boolean;
|
|
2351
|
+
}
|
|
2352
|
+
|
|
2353
|
+
interface PresetDescriptor {
|
|
2354
|
+
Vrsn: number;
|
|
2355
|
+
presetKind: number;
|
|
2356
|
+
presetFileName: string;
|
|
2357
|
+
}
|
|
2358
|
+
|
|
2359
|
+
interface CurvesPresetDescriptor {
|
|
2360
|
+
Vrsn: number;
|
|
2361
|
+
curvesPresetKind: number;
|
|
2362
|
+
curvesPresetFileName: string;
|
|
2363
|
+
}
|
|
2364
|
+
|
|
2365
|
+
interface MixerPresetDescriptor {
|
|
2366
|
+
Vrsn: number;
|
|
2367
|
+
mixerPresetKind: number;
|
|
2368
|
+
mixerPresetFileName: string;
|
|
2369
|
+
}
|
|
2370
|
+
|
|
2371
|
+
addHandler(
|
|
2372
|
+
'CgEd',
|
|
2373
|
+
target => {
|
|
2374
|
+
const a = target.adjustment;
|
|
2375
|
+
|
|
2376
|
+
if (!a) return false;
|
|
2377
|
+
|
|
2378
|
+
return (a.type === 'brightness/contrast' && !a.useLegacy) ||
|
|
2379
|
+
((a.type === 'levels' || a.type === 'curves' || a.type === 'exposure' || a.type === 'channel mixer' ||
|
|
2380
|
+
a.type === 'hue/saturation') && a.presetFileName !== undefined);
|
|
2381
|
+
},
|
|
2382
|
+
(reader, target, left) => {
|
|
2383
|
+
const desc = readVersionAndDescriptor(reader) as
|
|
2384
|
+
BrightnessContrastDescriptor | PresetDescriptor | CurvesPresetDescriptor | MixerPresetDescriptor;
|
|
2385
|
+
if (desc.Vrsn !== 1) throw new Error('Invalid CgEd version');
|
|
2386
|
+
|
|
2387
|
+
// this section can specify preset file name for other adjustment types
|
|
2388
|
+
if ('presetFileName' in desc) {
|
|
2389
|
+
target.adjustment = {
|
|
2390
|
+
...target.adjustment as LevelsAdjustment | ExposureAdjustment | HueSaturationAdjustment,
|
|
2391
|
+
presetKind: desc.presetKind,
|
|
2392
|
+
presetFileName: desc.presetFileName,
|
|
2393
|
+
};
|
|
2394
|
+
} else if ('curvesPresetFileName' in desc) {
|
|
2395
|
+
target.adjustment = {
|
|
2396
|
+
...target.adjustment as CurvesAdjustment,
|
|
2397
|
+
presetKind: desc.curvesPresetKind,
|
|
2398
|
+
presetFileName: desc.curvesPresetFileName,
|
|
2399
|
+
};
|
|
2400
|
+
} else if ('mixerPresetFileName' in desc) {
|
|
2401
|
+
target.adjustment = {
|
|
2402
|
+
...target.adjustment as CurvesAdjustment,
|
|
2403
|
+
presetKind: desc.mixerPresetKind,
|
|
2404
|
+
presetFileName: desc.mixerPresetFileName,
|
|
2405
|
+
};
|
|
2406
|
+
} else {
|
|
2407
|
+
target.adjustment = {
|
|
2408
|
+
type: 'brightness/contrast',
|
|
2409
|
+
brightness: desc.Brgh,
|
|
2410
|
+
contrast: desc.Cntr,
|
|
2411
|
+
meanValue: desc.means,
|
|
2412
|
+
useLegacy: !!desc.useLegacy,
|
|
2413
|
+
labColorOnly: !!desc['Lab '],
|
|
2414
|
+
auto: !!desc.Auto,
|
|
2415
|
+
};
|
|
2416
|
+
}
|
|
2417
|
+
|
|
2418
|
+
skipBytes(reader, left());
|
|
2419
|
+
},
|
|
2420
|
+
(writer, target) => {
|
|
2421
|
+
const info = target.adjustment!;
|
|
2422
|
+
|
|
2423
|
+
if (info.type === 'levels' || info.type === 'exposure' || info.type === 'hue/saturation') {
|
|
2424
|
+
const desc: PresetDescriptor = {
|
|
2425
|
+
Vrsn: 1,
|
|
2426
|
+
presetKind: info.presetKind ?? 1,
|
|
2427
|
+
presetFileName: info.presetFileName || '',
|
|
2428
|
+
};
|
|
2429
|
+
writeVersionAndDescriptor(writer, '', 'null', desc);
|
|
2430
|
+
} else if (info.type === 'curves') {
|
|
2431
|
+
const desc: CurvesPresetDescriptor = {
|
|
2432
|
+
Vrsn: 1,
|
|
2433
|
+
curvesPresetKind: info.presetKind ?? 1,
|
|
2434
|
+
curvesPresetFileName: info.presetFileName || '',
|
|
2435
|
+
};
|
|
2436
|
+
writeVersionAndDescriptor(writer, '', 'null', desc);
|
|
2437
|
+
} else if (info.type === 'channel mixer') {
|
|
2438
|
+
const desc: MixerPresetDescriptor = {
|
|
2439
|
+
Vrsn: 1,
|
|
2440
|
+
mixerPresetKind: info.presetKind ?? 1,
|
|
2441
|
+
mixerPresetFileName: info.presetFileName || '',
|
|
2442
|
+
};
|
|
2443
|
+
writeVersionAndDescriptor(writer, '', 'null', desc);
|
|
2444
|
+
} else if (info.type === 'brightness/contrast') {
|
|
2445
|
+
const desc: BrightnessContrastDescriptor = {
|
|
2446
|
+
Vrsn: 1,
|
|
2447
|
+
Brgh: info.brightness || 0,
|
|
2448
|
+
Cntr: info.contrast || 0,
|
|
2449
|
+
means: info.meanValue ?? 127,
|
|
2450
|
+
'Lab ': !!info.labColorOnly,
|
|
2451
|
+
useLegacy: !!info.useLegacy,
|
|
2452
|
+
Auto: !!info.auto,
|
|
2453
|
+
};
|
|
2454
|
+
writeVersionAndDescriptor(writer, '', 'null', desc);
|
|
2455
|
+
} else {
|
|
2456
|
+
throw new Error('Unhandled CgEd case');
|
|
2457
|
+
}
|
|
2458
|
+
},
|
|
2459
|
+
);
|
|
2460
|
+
|
|
2461
|
+
addHandler(
|
|
2462
|
+
'Txt2',
|
|
2463
|
+
hasKey('engineData'),
|
|
2464
|
+
(reader, target, left) => {
|
|
2465
|
+
const data = readBytes(reader, left());
|
|
2466
|
+
target.engineData = fromByteArray(data);
|
|
2467
|
+
// const engineData = parseEngineData(data);
|
|
2468
|
+
// console.log(require('util').inspect(engineData, false, 99, true));
|
|
2469
|
+
// require('fs').writeFileSync('resources/engineData2Simple.txt', require('util').inspect(engineData, false, 99, false), 'utf8');
|
|
2470
|
+
// require('fs').writeFileSync('test_data.json', JSON.stringify(ed, null, 2), 'utf8');
|
|
2471
|
+
},
|
|
2472
|
+
(writer, target) => {
|
|
2473
|
+
const buffer = toByteArray(target.engineData!);
|
|
2474
|
+
writeBytes(writer, buffer);
|
|
2475
|
+
},
|
|
2476
|
+
);
|
|
2477
|
+
|
|
2478
|
+
addHandler(
|
|
2479
|
+
'FMsk',
|
|
2480
|
+
hasKey('filterMask'),
|
|
2481
|
+
(reader, target) => {
|
|
2482
|
+
target.filterMask = {
|
|
2483
|
+
colorSpace: readColor(reader),
|
|
2484
|
+
opacity: readUint16(reader) / 0xff,
|
|
2485
|
+
};
|
|
2486
|
+
},
|
|
2487
|
+
(writer, target) => {
|
|
2488
|
+
writeColor(writer, target.filterMask!.colorSpace);
|
|
2489
|
+
writeUint16(writer, clamp(target.filterMask!.opacity ?? 1, 0, 1) * 0xff);
|
|
2490
|
+
},
|
|
2491
|
+
);
|
|
2492
|
+
|
|
2493
|
+
interface ArtdDescriptor {
|
|
2494
|
+
'Cnt ': number;
|
|
2495
|
+
autoExpandOffset: { Hrzn: number; Vrtc: number; };
|
|
2496
|
+
origin: { Hrzn: number; Vrtc: number; };
|
|
2497
|
+
autoExpandEnabled: boolean;
|
|
2498
|
+
autoNestEnabled: boolean;
|
|
2499
|
+
autoPositionEnabled: boolean;
|
|
2500
|
+
shrinkwrapOnSaveEnabled?: boolean;
|
|
2501
|
+
docDefaultNewArtboardBackgroundColor: DescriptorColor;
|
|
2502
|
+
docDefaultNewArtboardBackgroundType: number;
|
|
2503
|
+
}
|
|
2504
|
+
|
|
2505
|
+
addHandler(
|
|
2506
|
+
'artd', // document-wide artboard info
|
|
2507
|
+
target => (target as Psd).artboards !== undefined,
|
|
2508
|
+
(reader, target, left) => {
|
|
2509
|
+
const desc = readVersionAndDescriptor(reader) as ArtdDescriptor;
|
|
2510
|
+
(target as Psd).artboards = {
|
|
2511
|
+
count: desc['Cnt '],
|
|
2512
|
+
autoExpandOffset: { horizontal: desc.autoExpandOffset.Hrzn, vertical: desc.autoExpandOffset.Vrtc },
|
|
2513
|
+
origin: { horizontal: desc.origin.Hrzn, vertical: desc.origin.Vrtc },
|
|
2514
|
+
autoExpandEnabled: desc.autoExpandEnabled,
|
|
2515
|
+
autoNestEnabled: desc.autoNestEnabled,
|
|
2516
|
+
autoPositionEnabled: desc.autoPositionEnabled,
|
|
2517
|
+
shrinkwrapOnSaveEnabled: !!desc.shrinkwrapOnSaveEnabled,
|
|
2518
|
+
docDefaultNewArtboardBackgroundColor: parseColor(desc.docDefaultNewArtboardBackgroundColor),
|
|
2519
|
+
docDefaultNewArtboardBackgroundType: desc.docDefaultNewArtboardBackgroundType,
|
|
2520
|
+
};
|
|
2521
|
+
|
|
2522
|
+
skipBytes(reader, left());
|
|
2523
|
+
},
|
|
2524
|
+
(writer, target) => {
|
|
2525
|
+
const artb = (target as Psd).artboards!;
|
|
2526
|
+
const desc: ArtdDescriptor = {
|
|
2527
|
+
'Cnt ': artb.count,
|
|
2528
|
+
autoExpandOffset: artb.autoExpandOffset ? { Hrzn: artb.autoExpandOffset.horizontal, Vrtc: artb.autoExpandOffset.vertical } : { Hrzn: 0, Vrtc: 0 },
|
|
2529
|
+
origin: artb.origin ? { Hrzn: artb.origin.horizontal, Vrtc: artb.origin.vertical } : { Hrzn: 0, Vrtc: 0 },
|
|
2530
|
+
autoExpandEnabled: artb.autoExpandEnabled ?? true,
|
|
2531
|
+
autoNestEnabled: artb.autoNestEnabled ?? true,
|
|
2532
|
+
autoPositionEnabled: artb.autoPositionEnabled ?? true,
|
|
2533
|
+
shrinkwrapOnSaveEnabled: artb.shrinkwrapOnSaveEnabled ?? true,
|
|
2534
|
+
docDefaultNewArtboardBackgroundColor: serializeColor(artb.docDefaultNewArtboardBackgroundColor),
|
|
2535
|
+
docDefaultNewArtboardBackgroundType: artb.docDefaultNewArtboardBackgroundType ?? 1,
|
|
2536
|
+
};
|
|
2537
|
+
writeVersionAndDescriptor(writer, '', 'null', desc, 'artd');
|
|
2538
|
+
},
|
|
2539
|
+
);
|
|
2540
|
+
|
|
2541
|
+
export function hasMultiEffects(effects: LayerEffectsInfo) {
|
|
2542
|
+
return Object.keys(effects).map(key => (effects as any)[key]).some(v => Array.isArray(v) && v.length > 1);
|
|
2543
|
+
}
|
|
2544
|
+
|
|
2545
|
+
addHandler(
|
|
2546
|
+
'lfx2',
|
|
2547
|
+
target => target.effects !== undefined && !hasMultiEffects(target.effects),
|
|
2548
|
+
(reader, target, left, _, options) => {
|
|
2549
|
+
const version = readUint32(reader);
|
|
2550
|
+
if (version !== 0) throw new Error(`Invalid lfx2 version`);
|
|
2551
|
+
|
|
2552
|
+
const desc: Lfx2Descriptor = readVersionAndDescriptor(reader);
|
|
2553
|
+
// console.log(require('util').inspect(desc, false, 99, true));
|
|
2554
|
+
|
|
2555
|
+
// TODO: don't discard if we got it from lmfx
|
|
2556
|
+
// discard if read in 'lrFX' section
|
|
2557
|
+
target.effects = parseEffects(desc, !!options.logMissingFeatures);
|
|
2558
|
+
|
|
2559
|
+
skipBytes(reader, left());
|
|
2560
|
+
},
|
|
2561
|
+
(writer, target, _, options) => {
|
|
2562
|
+
const desc = serializeEffects(target.effects!, !!options.logMissingFeatures, false);
|
|
2563
|
+
// console.log(require('util').inspect(desc, false, 99, true));
|
|
2564
|
+
|
|
2565
|
+
writeUint32(writer, 0); // version
|
|
2566
|
+
writeVersionAndDescriptor(writer, '', 'null', desc);
|
|
2567
|
+
},
|
|
2568
|
+
);
|
|
2569
|
+
|
|
2570
|
+
interface CinfDescriptor {
|
|
2571
|
+
Vrsn: { major: number; minor: number; fix: number; };
|
|
2572
|
+
psVersion?: { major: number; minor: number; fix: number; };
|
|
2573
|
+
description: string;
|
|
2574
|
+
reason: string;
|
|
2575
|
+
Engn: string; // 'Engn.compCore';
|
|
2576
|
+
enableCompCore: string; // 'enable.feature';
|
|
2577
|
+
enableCompCoreGPU: string; // 'enable.feature';
|
|
2578
|
+
enableCompCoreThreads?: string; // 'enable.feature';
|
|
2579
|
+
compCoreSupport: string; // 'reason.supported';
|
|
2580
|
+
compCoreGPUSupport: string; // 'reason.featureDisabled';
|
|
2581
|
+
}
|
|
2582
|
+
|
|
2583
|
+
addHandler(
|
|
2584
|
+
'cinf',
|
|
2585
|
+
hasKey('compositorUsed'),
|
|
2586
|
+
(reader, target, left) => {
|
|
2587
|
+
const desc = readVersionAndDescriptor(reader) as CinfDescriptor;
|
|
2588
|
+
// console.log(require('util').inspect(desc, false, 99, true));
|
|
2589
|
+
|
|
2590
|
+
target.compositorUsed = {
|
|
2591
|
+
description: desc.description,
|
|
2592
|
+
reason: desc.reason,
|
|
2593
|
+
engine: desc.Engn.split('.')[1],
|
|
2594
|
+
enableCompCore: desc.enableCompCore.split('.')[1],
|
|
2595
|
+
enableCompCoreGPU: desc.enableCompCoreGPU.split('.')[1],
|
|
2596
|
+
compCoreSupport: desc.compCoreSupport.split('.')[1],
|
|
2597
|
+
compCoreGPUSupport: desc.compCoreGPUSupport.split('.')[1],
|
|
2598
|
+
};
|
|
2599
|
+
|
|
2600
|
+
skipBytes(reader, left());
|
|
2601
|
+
},
|
|
2602
|
+
(writer, target) => {
|
|
2603
|
+
const cinf = target.compositorUsed!;
|
|
2604
|
+
const desc: CinfDescriptor = {
|
|
2605
|
+
Vrsn: { major: 1, minor: 0, fix: 0 }, // TEMP
|
|
2606
|
+
// psVersion: { major: 22, minor: 3, fix: 1 }, // TESTING
|
|
2607
|
+
description: cinf.description,
|
|
2608
|
+
reason: cinf.reason,
|
|
2609
|
+
Engn: `Engn.${cinf.engine}`,
|
|
2610
|
+
enableCompCore: `enable.${cinf.enableCompCore}`,
|
|
2611
|
+
enableCompCoreGPU: `enable.${cinf.enableCompCoreGPU}`,
|
|
2612
|
+
// enableCompCoreThreads: `enable.feature`, // TESTING
|
|
2613
|
+
compCoreSupport: `reason.${cinf.compCoreSupport}`,
|
|
2614
|
+
compCoreGPUSupport: `reason.${cinf.compCoreGPUSupport}`,
|
|
2615
|
+
};
|
|
2616
|
+
writeVersionAndDescriptor(writer, '', 'null', desc);
|
|
2617
|
+
},
|
|
2618
|
+
);
|
|
2619
|
+
|
|
2620
|
+
// extension settings ?, ignore it
|
|
2621
|
+
addHandler(
|
|
2622
|
+
'extn',
|
|
2623
|
+
target => (target as any)._extn !== undefined,
|
|
2624
|
+
(reader, target) => {
|
|
2625
|
+
const desc: ExtensionDesc = readVersionAndDescriptor(reader);
|
|
2626
|
+
// console.log(require('util').inspect(desc, false, 99, true));
|
|
2627
|
+
|
|
2628
|
+
if (MOCK_HANDLERS) (target as any)._extn = desc;
|
|
2629
|
+
},
|
|
2630
|
+
(writer, target) => {
|
|
2631
|
+
// TODO: need to add correct types for desc fields (resources/src.psd)
|
|
2632
|
+
if (MOCK_HANDLERS) writeVersionAndDescriptor(writer, '', 'null', (target as any)._extn);
|
|
2633
|
+
},
|
|
2634
|
+
);
|
|
2635
|
+
|
|
2636
|
+
addHandler(
|
|
2637
|
+
'iOpa',
|
|
2638
|
+
hasKey('fillOpacity'),
|
|
2639
|
+
(reader, target) => {
|
|
2640
|
+
target.fillOpacity = readUint8(reader) / 0xff;
|
|
2641
|
+
skipBytes(reader, 3);
|
|
2642
|
+
},
|
|
2643
|
+
(writer, target) => {
|
|
2644
|
+
writeUint8(writer, target.fillOpacity! * 0xff);
|
|
2645
|
+
writeZeros(writer, 3);
|
|
2646
|
+
},
|
|
2647
|
+
);
|
|
2648
|
+
|
|
2649
|
+
addHandler(
|
|
2650
|
+
'brst',
|
|
2651
|
+
hasKey('channelBlendingRestrictions'),
|
|
2652
|
+
(reader, target, left) => {
|
|
2653
|
+
target.channelBlendingRestrictions = [];
|
|
2654
|
+
|
|
2655
|
+
while (left() > 4) {
|
|
2656
|
+
target.channelBlendingRestrictions.push(readInt32(reader));
|
|
2657
|
+
}
|
|
2658
|
+
},
|
|
2659
|
+
(writer, target) => {
|
|
2660
|
+
for (const channel of target.channelBlendingRestrictions!) {
|
|
2661
|
+
writeInt32(writer, channel);
|
|
2662
|
+
}
|
|
2663
|
+
},
|
|
2664
|
+
);
|
|
2665
|
+
|
|
2666
|
+
addHandler(
|
|
2667
|
+
'tsly',
|
|
2668
|
+
hasKey('transparencyShapesLayer'),
|
|
2669
|
+
(reader, target) => {
|
|
2670
|
+
target.transparencyShapesLayer = !!readUint8(reader);
|
|
2671
|
+
skipBytes(reader, 3);
|
|
2672
|
+
},
|
|
2673
|
+
(writer, target) => {
|
|
2674
|
+
writeUint8(writer, target.transparencyShapesLayer ? 1 : 0);
|
|
2675
|
+
writeZeros(writer, 3);
|
|
2676
|
+
},
|
|
2677
|
+
);
|