ag-psd 19.0.1 → 20.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 +10 -0
- package/README_PSD.md +20 -2
- package/TODO +7 -0
- package/dist/abr.js +4 -4
- package/dist/abr.js.map +1 -1
- package/dist/additionalInfo.d.ts +3 -3
- package/dist/additionalInfo.js +107 -12
- package/dist/additionalInfo.js.map +1 -1
- package/dist/bundle.js +15305 -7643
- package/dist/descriptor.js +21 -16
- package/dist/descriptor.js.map +1 -1
- package/dist/engineData2.d.ts +1 -0
- package/dist/engineData2.js +349 -0
- package/dist/engineData2.js.map +1 -0
- package/dist/helpers.d.ts +2 -7
- package/dist/helpers.js +45 -12
- package/dist/helpers.js.map +1 -1
- package/dist/imageResources.js +42 -1
- package/dist/imageResources.js.map +1 -1
- package/dist/psd.d.ts +46 -3
- package/dist/psd.js +8 -1
- package/dist/psd.js.map +1 -1
- package/dist/psdReader.d.ts +11 -5
- package/dist/psdReader.js +213 -100
- package/dist/psdReader.js.map +1 -1
- package/dist/psdWriter.js +24 -4
- package/dist/psdWriter.js.map +1 -1
- package/dist/utf8.js +10 -4
- package/dist/utf8.js.map +1 -1
- package/dist-es/abr.js +4 -4
- package/dist-es/abr.js.map +1 -1
- package/dist-es/additionalInfo.d.ts +3 -3
- package/dist-es/additionalInfo.js +110 -15
- package/dist-es/additionalInfo.js.map +1 -1
- package/dist-es/descriptor.js +21 -16
- package/dist-es/descriptor.js.map +1 -1
- package/dist-es/engineData2.d.ts +1 -0
- package/dist-es/engineData2.js +345 -0
- package/dist-es/engineData2.js.map +1 -0
- package/dist-es/helpers.d.ts +2 -7
- package/dist-es/helpers.js +43 -11
- package/dist-es/helpers.js.map +1 -1
- package/dist-es/imageResources.js +42 -1
- package/dist-es/imageResources.js.map +1 -1
- package/dist-es/psd.d.ts +46 -3
- package/dist-es/psd.js +7 -0
- package/dist-es/psd.js.map +1 -1
- package/dist-es/psdReader.d.ts +11 -5
- package/dist-es/psdReader.js +212 -102
- package/dist-es/psdReader.js.map +1 -1
- package/dist-es/psdWriter.js +25 -5
- package/dist-es/psdWriter.js.map +1 -1
- package/dist-es/utf8.js +10 -4
- package/dist-es/utf8.js.map +1 -1
- package/package.json +6 -7
- package/src/abr.ts +4 -4
- package/src/additionalInfo.ts +156 -51
- package/src/descriptor.ts +14 -9
- package/src/engineData2.ts +367 -0
- package/src/helpers.ts +47 -20
- package/src/imageResources.ts +59 -2
- package/src/psd.ts +41 -5
- package/src/psdReader.ts +210 -128
- package/src/psdWriter.ts +33 -14
- package/src/utf8.ts +12 -4
package/dist/psdReader.js
CHANGED
|
@@ -11,8 +11,8 @@ var __assign = (this && this.__assign) || function () {
|
|
|
11
11
|
return __assign.apply(this, arguments);
|
|
12
12
|
};
|
|
13
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
exports.readPattern = exports.readColor = exports.readSection = exports.readDataRLE = exports.
|
|
15
|
-
var
|
|
14
|
+
exports.readPattern = exports.readColor = exports.readSection = exports.readDataRLE = exports.readDataZip = exports.readAdditionalLayerInfo = exports.readGlobalLayerMaskInfo = exports.readLayerInfo = exports.readPsd = exports.checkSignature = exports.skipBytes = exports.readAsciiString = exports.readUnicodeStringWithLengthLE = exports.readUnicodeStringWithLength = exports.readUnicodeString = exports.readPascalString = exports.readSignature = exports.readBytes = exports.readFixedPointPath32 = exports.readFixedPoint32 = exports.readFloat64 = exports.readFloat32 = exports.readUint32 = exports.readInt32LE = exports.readInt32 = exports.readUint16LE = exports.readUint16 = exports.readInt16 = exports.peekUint8 = exports.readUint8 = exports.warnOrThrow = exports.createReader = exports.supportedColorModes = void 0;
|
|
15
|
+
var zlib_1 = require("zlib");
|
|
16
16
|
var helpers_1 = require("./helpers");
|
|
17
17
|
var additionalInfo_1 = require("./additionalInfo");
|
|
18
18
|
var imageResources_1 = require("./imageResources");
|
|
@@ -186,9 +186,9 @@ function readShortString(reader, length) {
|
|
|
186
186
|
function isValidSignature(sig) {
|
|
187
187
|
return sig === '8BIM' || sig === 'MeSa' || sig === 'AgHg' || sig === 'PHUT' || sig === 'DCSR';
|
|
188
188
|
}
|
|
189
|
-
function readPsd(reader,
|
|
189
|
+
function readPsd(reader, readOptions) {
|
|
190
190
|
var _a;
|
|
191
|
-
if (
|
|
191
|
+
if (readOptions === void 0) { readOptions = {}; }
|
|
192
192
|
// header
|
|
193
193
|
checkSignature(reader, '8BPS');
|
|
194
194
|
var version = readUint16(reader);
|
|
@@ -202,20 +202,27 @@ function readPsd(reader, options) {
|
|
|
202
202
|
var colorMode = readUint16(reader);
|
|
203
203
|
var maxSize = version === 1 ? 30000 : 300000;
|
|
204
204
|
if (width > maxSize || height > maxSize)
|
|
205
|
-
throw new Error("Invalid size");
|
|
205
|
+
throw new Error("Invalid size: ".concat(width, "x").concat(height));
|
|
206
206
|
if (channels > 16)
|
|
207
|
-
throw new Error("Invalid channel count");
|
|
208
|
-
if (
|
|
209
|
-
throw new Error("Invalid bitsPerChannel
|
|
207
|
+
throw new Error("Invalid channel count: ".concat(channels));
|
|
208
|
+
if (![1, 8, 16, 32].includes(bitsPerChannel))
|
|
209
|
+
throw new Error("Invalid bitsPerChannel: ".concat(bitsPerChannel));
|
|
210
210
|
if (exports.supportedColorModes.indexOf(colorMode) === -1)
|
|
211
211
|
throw new Error("Color mode not supported: ".concat((_a = colorModes[colorMode]) !== null && _a !== void 0 ? _a : colorMode));
|
|
212
212
|
var psd = { width: width, height: height, channels: channels, bitsPerChannel: bitsPerChannel, colorMode: colorMode };
|
|
213
|
-
var
|
|
213
|
+
var options = __assign(__assign({}, readOptions), { large: version === 2, globalAlpha: false });
|
|
214
214
|
var fixOffsets = [0, 1, -1, 2, -2, 3, -3, 4, -4];
|
|
215
215
|
// color mode data
|
|
216
216
|
readSection(reader, 1, function (left) {
|
|
217
|
-
if (
|
|
218
|
-
|
|
217
|
+
if (!left())
|
|
218
|
+
return;
|
|
219
|
+
// const numbers: number[] = [];
|
|
220
|
+
// console.log('color mode', left());
|
|
221
|
+
// while (left()) {
|
|
222
|
+
// numbers.push(readUint32(reader));
|
|
223
|
+
// }
|
|
224
|
+
// console.log('color mode', numbers);
|
|
225
|
+
// if (options.throwForMissingFeatures) throw new Error('Color mode data not supported');
|
|
219
226
|
skipBytes(reader, left());
|
|
220
227
|
});
|
|
221
228
|
// image resources
|
|
@@ -241,22 +248,22 @@ function readPsd(reader, options) {
|
|
|
241
248
|
readPascalString(reader, 2); // name
|
|
242
249
|
readSection(reader, 2, function (left) {
|
|
243
250
|
var handler = imageResources_1.resourceHandlersMap[id];
|
|
244
|
-
var skip = id === 1036 && !!
|
|
251
|
+
var skip = id === 1036 && !!options.skipThumbnail;
|
|
245
252
|
if (!psd.imageResources) {
|
|
246
253
|
psd.imageResources = {};
|
|
247
254
|
}
|
|
248
255
|
if (handler && !skip) {
|
|
249
256
|
try {
|
|
250
|
-
handler.read(reader, psd.imageResources, left,
|
|
257
|
+
handler.read(reader, psd.imageResources, left, options);
|
|
251
258
|
}
|
|
252
259
|
catch (e) {
|
|
253
|
-
if (
|
|
260
|
+
if (options.throwForMissingFeatures)
|
|
254
261
|
throw e;
|
|
255
262
|
skipBytes(reader, left());
|
|
256
263
|
}
|
|
257
264
|
}
|
|
258
265
|
else {
|
|
259
|
-
// options.logMissingFeatures && console.log(`Unhandled image resource: ${id}`);
|
|
266
|
+
// options.logMissingFeatures && console.log(`Unhandled image resource: ${id} (${left()})`);
|
|
260
267
|
skipBytes(reader, left());
|
|
261
268
|
}
|
|
262
269
|
});
|
|
@@ -266,9 +273,11 @@ function readPsd(reader, options) {
|
|
|
266
273
|
}
|
|
267
274
|
});
|
|
268
275
|
// layer and mask info
|
|
269
|
-
var globalAlpha = false;
|
|
270
276
|
readSection(reader, 1, function (left) {
|
|
271
|
-
|
|
277
|
+
readSection(reader, 2, function (left) {
|
|
278
|
+
readLayerInfo(reader, psd, options);
|
|
279
|
+
skipBytes(reader, left());
|
|
280
|
+
}, undefined, options.large);
|
|
272
281
|
// SAI does not include this section
|
|
273
282
|
if (left() > 0) {
|
|
274
283
|
var globalLayerMaskInfo = readGlobalLayerMaskInfo(reader);
|
|
@@ -287,18 +296,18 @@ function readPsd(reader, options) {
|
|
|
287
296
|
skipBytes(reader, 1);
|
|
288
297
|
}
|
|
289
298
|
if (left() >= 12) {
|
|
290
|
-
readAdditionalLayerInfo(reader, psd, psd,
|
|
299
|
+
readAdditionalLayerInfo(reader, psd, psd, options);
|
|
291
300
|
}
|
|
292
301
|
else {
|
|
293
302
|
// opt.logMissingFeatures && console.log('skipping leftover bytes', left());
|
|
294
303
|
skipBytes(reader, left());
|
|
295
304
|
}
|
|
296
305
|
}
|
|
297
|
-
}, undefined,
|
|
306
|
+
}, undefined, options.large);
|
|
298
307
|
var hasChildren = psd.children && psd.children.length;
|
|
299
|
-
var skipComposite =
|
|
308
|
+
var skipComposite = options.skipCompositeImageData && (options.skipLayerImageData || hasChildren);
|
|
300
309
|
if (!skipComposite) {
|
|
301
|
-
readImageData(reader, psd,
|
|
310
|
+
readImageData(reader, psd, options);
|
|
302
311
|
}
|
|
303
312
|
// TODO: show converted color mode instead of original PSD file color mode
|
|
304
313
|
// but add option to preserve file color mode (need to return image data instead of canvas in that case)
|
|
@@ -307,52 +316,48 @@ function readPsd(reader, options) {
|
|
|
307
316
|
}
|
|
308
317
|
exports.readPsd = readPsd;
|
|
309
318
|
function readLayerInfo(reader, psd, options) {
|
|
310
|
-
var
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
var
|
|
319
|
+
var layerCount = readInt16(reader);
|
|
320
|
+
if (layerCount < 0) {
|
|
321
|
+
options.globalAlpha = true;
|
|
322
|
+
layerCount = -layerCount;
|
|
323
|
+
}
|
|
324
|
+
var layers = [];
|
|
325
|
+
var layerChannels = [];
|
|
326
|
+
for (var i = 0; i < layerCount; i++) {
|
|
327
|
+
var _a = readLayerRecord(reader, psd, options), layer = _a.layer, channels = _a.channels;
|
|
328
|
+
layers.push(layer);
|
|
329
|
+
layerChannels.push(channels);
|
|
330
|
+
}
|
|
331
|
+
if (!options.skipLayerImageData) {
|
|
319
332
|
for (var i = 0; i < layerCount; i++) {
|
|
320
|
-
|
|
321
|
-
layers.push(layer);
|
|
322
|
-
layerChannels.push(channels);
|
|
333
|
+
readLayerChannelImageData(reader, psd, layers[i], layerChannels[i], options);
|
|
323
334
|
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
335
|
+
}
|
|
336
|
+
if (!psd.children)
|
|
337
|
+
psd.children = [];
|
|
338
|
+
var stack = [psd];
|
|
339
|
+
for (var i = layers.length - 1; i >= 0; i--) {
|
|
340
|
+
var l = layers[i];
|
|
341
|
+
var type = l.sectionDivider ? l.sectionDivider.type : 0 /* SectionDividerType.Other */;
|
|
342
|
+
if (type === 1 /* SectionDividerType.OpenFolder */ || type === 2 /* SectionDividerType.ClosedFolder */) {
|
|
343
|
+
l.opened = type === 1 /* SectionDividerType.OpenFolder */;
|
|
344
|
+
l.children = [];
|
|
345
|
+
stack[stack.length - 1].children.unshift(l);
|
|
346
|
+
stack.push(l);
|
|
328
347
|
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
var type = l.sectionDivider ? l.sectionDivider.type : 0 /* SectionDividerType.Other */;
|
|
336
|
-
if (type === 1 /* SectionDividerType.OpenFolder */ || type === 2 /* SectionDividerType.ClosedFolder */) {
|
|
337
|
-
l.opened = type === 1 /* SectionDividerType.OpenFolder */;
|
|
338
|
-
l.children = [];
|
|
339
|
-
stack[stack.length - 1].children.unshift(l);
|
|
340
|
-
stack.push(l);
|
|
341
|
-
}
|
|
342
|
-
else if (type === 3 /* SectionDividerType.BoundingSectionDivider */) {
|
|
343
|
-
stack.pop();
|
|
344
|
-
// this was workaround because I didn't know what `lsdk` section was, now it's probably not needed anymore
|
|
345
|
-
// } else if (l.name === '</Layer group>' && !l.sectionDivider && !l.top && !l.left && !l.bottom && !l.right) {
|
|
346
|
-
// // sometimes layer group terminator doesn't have sectionDivider, so we just guess here (PS bug ?)
|
|
347
|
-
// stack.pop();
|
|
348
|
-
}
|
|
349
|
-
else {
|
|
350
|
-
stack[stack.length - 1].children.unshift(l);
|
|
351
|
-
}
|
|
348
|
+
else if (type === 3 /* SectionDividerType.BoundingSectionDivider */) {
|
|
349
|
+
stack.pop();
|
|
350
|
+
// this was workaround because I didn't know what `lsdk` section was, now it's probably not needed anymore
|
|
351
|
+
// } else if (l.name === '</Layer group>' && !l.sectionDivider && !l.top && !l.left && !l.bottom && !l.right) {
|
|
352
|
+
// // sometimes layer group terminator doesn't have sectionDivider, so we just guess here (PS bug ?)
|
|
353
|
+
// stack.pop();
|
|
352
354
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
+
else {
|
|
356
|
+
stack[stack.length - 1].children.unshift(l);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
355
359
|
}
|
|
360
|
+
exports.readLayerInfo = readLayerInfo;
|
|
356
361
|
function readLayerRecord(reader, psd, options) {
|
|
357
362
|
var layer = {};
|
|
358
363
|
layer.top = readInt32(reader);
|
|
@@ -453,18 +458,21 @@ function readLayerBlendingRanges(reader) {
|
|
|
453
458
|
});
|
|
454
459
|
}
|
|
455
460
|
function readLayerChannelImageData(reader, psd, layer, channels, options) {
|
|
461
|
+
var _a, _b, _c, _d;
|
|
456
462
|
var layerWidth = (layer.right || 0) - (layer.left || 0);
|
|
457
463
|
var layerHeight = (layer.bottom || 0) - (layer.top || 0);
|
|
458
464
|
var cmyk = psd.colorMode === 4 /* ColorMode.CMYK */;
|
|
459
465
|
var imageData;
|
|
460
466
|
if (layerWidth && layerHeight) {
|
|
461
467
|
if (cmyk) {
|
|
468
|
+
if (psd.bitsPerChannel !== 8)
|
|
469
|
+
throw new Error('bitsPerChannel Not supproted');
|
|
462
470
|
imageData = { width: layerWidth, height: layerHeight, data: new Uint8ClampedArray(layerWidth * layerHeight * 5) };
|
|
463
471
|
for (var p = 4; p < imageData.data.byteLength; p += 5)
|
|
464
472
|
imageData.data[p] = 255;
|
|
465
473
|
}
|
|
466
474
|
else {
|
|
467
|
-
imageData = (
|
|
475
|
+
imageData = createImageDataBitDepth(layerWidth, layerHeight, (_a = psd.bitsPerChannel) !== null && _a !== void 0 ? _a : 8);
|
|
468
476
|
(0, helpers_1.resetImageData)(imageData);
|
|
469
477
|
}
|
|
470
478
|
}
|
|
@@ -497,10 +505,10 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
|
|
|
497
505
|
var maskWidth = (mask.right || 0) - (mask.left || 0);
|
|
498
506
|
var maskHeight = (mask.bottom || 0) - (mask.top || 0);
|
|
499
507
|
if (maskWidth && maskHeight) {
|
|
500
|
-
var maskData = (
|
|
508
|
+
var maskData = createImageDataBitDepth(maskWidth, maskHeight, (_b = psd.bitsPerChannel) !== null && _b !== void 0 ? _b : 8);
|
|
501
509
|
(0, helpers_1.resetImageData)(maskData);
|
|
502
510
|
var start_1 = reader.offset;
|
|
503
|
-
readData(reader, channel.length, maskData, compression, maskWidth, maskHeight, 0, options.large, 4);
|
|
511
|
+
readData(reader, channel.length, maskData, compression, maskWidth, maskHeight, (_c = psd.bitsPerChannel) !== null && _c !== void 0 ? _c : 8, 0, options.large, 4);
|
|
504
512
|
if (helpers_1.RAW_IMAGE_DATA) {
|
|
505
513
|
layer.maskDataRaw = new Uint8Array(reader.view.buffer, reader.view.byteOffset + start_1, reader.offset - start_1);
|
|
506
514
|
}
|
|
@@ -509,8 +517,7 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
|
|
|
509
517
|
mask.imageData = maskData;
|
|
510
518
|
}
|
|
511
519
|
else {
|
|
512
|
-
mask.canvas = (0, helpers_1.
|
|
513
|
-
mask.canvas.getContext('2d').putImageData(maskData, 0, 0);
|
|
520
|
+
mask.canvas = (0, helpers_1.imageDataToCanvas)(maskData);
|
|
514
521
|
}
|
|
515
522
|
}
|
|
516
523
|
}
|
|
@@ -523,7 +530,7 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
|
|
|
523
530
|
throw new Error("Channel not supported: ".concat(channel.id));
|
|
524
531
|
}
|
|
525
532
|
}
|
|
526
|
-
readData(reader, channel.length, targetData, compression, layerWidth, layerHeight, offset, options.large, cmyk ? 5 : 4);
|
|
533
|
+
readData(reader, channel.length, targetData, compression, layerWidth, layerHeight, (_d = psd.bitsPerChannel) !== null && _d !== void 0 ? _d : 8, offset, options.large, cmyk ? 5 : 4);
|
|
527
534
|
if (helpers_1.RAW_IMAGE_DATA) {
|
|
528
535
|
layer.imageDataRaw[channel.id] = new Uint8Array(reader.view.buffer, reader.view.byteOffset + start + 2, channel.length - 2);
|
|
529
536
|
}
|
|
@@ -543,23 +550,22 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
|
|
|
543
550
|
layer.imageData = imageData;
|
|
544
551
|
}
|
|
545
552
|
else {
|
|
546
|
-
layer.canvas = (0, helpers_1.
|
|
547
|
-
layer.canvas.getContext('2d').putImageData(imageData, 0, 0);
|
|
553
|
+
layer.canvas = (0, helpers_1.imageDataToCanvas)(imageData);
|
|
548
554
|
}
|
|
549
555
|
}
|
|
550
556
|
}
|
|
551
|
-
function readData(reader, length, data, compression, width, height, offset, large, step) {
|
|
557
|
+
function readData(reader, length, data, compression, width, height, bitDepth, offset, large, step) {
|
|
552
558
|
if (compression === 0 /* Compression.RawData */) {
|
|
553
|
-
readDataRaw(reader, data, width, height, step, offset);
|
|
559
|
+
readDataRaw(reader, data, width, height, bitDepth, step, offset);
|
|
554
560
|
}
|
|
555
561
|
else if (compression === 1 /* Compression.RleCompressed */) {
|
|
556
|
-
readDataRLE(reader, data, width, height, step, [offset], large);
|
|
562
|
+
readDataRLE(reader, data, width, height, bitDepth, step, [offset], large);
|
|
557
563
|
}
|
|
558
564
|
else if (compression === 2 /* Compression.ZipWithoutPrediction */) {
|
|
559
|
-
|
|
565
|
+
readDataZip(reader, length, data, width, height, bitDepth, step, offset, false);
|
|
560
566
|
}
|
|
561
567
|
else if (compression === 3 /* Compression.ZipWithPrediction */) {
|
|
562
|
-
|
|
568
|
+
readDataZip(reader, length, data, width, height, bitDepth, step, offset, true);
|
|
563
569
|
}
|
|
564
570
|
else {
|
|
565
571
|
throw new Error("Invalid Compression type: ".concat(compression));
|
|
@@ -580,6 +586,7 @@ function readGlobalLayerMaskInfo(reader) {
|
|
|
580
586
|
return { overlayColorSpace: overlayColorSpace, colorSpace1: colorSpace1, colorSpace2: colorSpace2, colorSpace3: colorSpace3, colorSpace4: colorSpace4, opacity: opacity, kind: kind };
|
|
581
587
|
});
|
|
582
588
|
}
|
|
589
|
+
exports.readGlobalLayerMaskInfo = readGlobalLayerMaskInfo;
|
|
583
590
|
function readAdditionalLayerInfo(reader, target, psd, options) {
|
|
584
591
|
var sig = readSignature(reader);
|
|
585
592
|
if (sig !== '8BIM' && sig !== '8B64')
|
|
@@ -608,23 +615,42 @@ function readAdditionalLayerInfo(reader, target, psd, options) {
|
|
|
608
615
|
}
|
|
609
616
|
}, false, u64);
|
|
610
617
|
}
|
|
611
|
-
|
|
618
|
+
exports.readAdditionalLayerInfo = readAdditionalLayerInfo;
|
|
619
|
+
function createImageDataBitDepth(width, height, bitDepth) {
|
|
620
|
+
if (bitDepth === 1 || bitDepth === 8) {
|
|
621
|
+
return (0, helpers_1.createImageData)(width, height);
|
|
622
|
+
}
|
|
623
|
+
else if (bitDepth === 16) {
|
|
624
|
+
return { width: width, height: height, data: new Uint16Array(width * height * 4) };
|
|
625
|
+
}
|
|
626
|
+
else if (bitDepth === 32) {
|
|
627
|
+
return { width: width, height: height, data: new Float32Array(width * height * 4) };
|
|
628
|
+
}
|
|
629
|
+
else {
|
|
630
|
+
throw new Error("Invalid bitDepth (".concat(bitDepth, ")"));
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
function readImageData(reader, psd, options) {
|
|
634
|
+
var _a, _b;
|
|
612
635
|
var compression = readUint16(reader);
|
|
636
|
+
var bitsPerChannel = (_a = psd.bitsPerChannel) !== null && _a !== void 0 ? _a : 8;
|
|
613
637
|
if (exports.supportedColorModes.indexOf(psd.colorMode) === -1)
|
|
614
638
|
throw new Error("Color mode not supported: ".concat(psd.colorMode));
|
|
615
639
|
if (compression !== 0 /* Compression.RawData */ && compression !== 1 /* Compression.RleCompressed */)
|
|
616
640
|
throw new Error("Compression type not supported: ".concat(compression));
|
|
617
|
-
var imageData = (
|
|
641
|
+
var imageData = createImageDataBitDepth(psd.width, psd.height, bitsPerChannel);
|
|
618
642
|
(0, helpers_1.resetImageData)(imageData);
|
|
619
643
|
switch (psd.colorMode) {
|
|
620
644
|
case 0 /* ColorMode.Bitmap */: {
|
|
645
|
+
if (bitsPerChannel !== 1)
|
|
646
|
+
throw new Error('Invalid bitsPerChannel for bitmap color mode');
|
|
621
647
|
var bytes = void 0;
|
|
622
648
|
if (compression === 0 /* Compression.RawData */) {
|
|
623
649
|
bytes = readBytes(reader, Math.ceil(psd.width / 8) * psd.height);
|
|
624
650
|
}
|
|
625
651
|
else if (compression === 1 /* Compression.RleCompressed */) {
|
|
626
652
|
bytes = new Uint8Array(psd.width * psd.height);
|
|
627
|
-
readDataRLE(reader, { data: bytes, width: psd.width, height: psd.height }, psd.width, psd.height, 1, [0], options.large);
|
|
653
|
+
readDataRLE(reader, { data: bytes, width: psd.width, height: psd.height }, psd.width, psd.height, 8, 1, [0], options.large);
|
|
628
654
|
}
|
|
629
655
|
else {
|
|
630
656
|
throw new Error("Bitmap compression not supported: ".concat(compression));
|
|
@@ -641,17 +667,17 @@ function readImageData(reader, psd, globalAlpha, options) {
|
|
|
641
667
|
channels.push(i);
|
|
642
668
|
}
|
|
643
669
|
}
|
|
644
|
-
else if (globalAlpha) {
|
|
670
|
+
else if (options.globalAlpha) {
|
|
645
671
|
channels.push(3);
|
|
646
672
|
}
|
|
647
673
|
if (compression === 0 /* Compression.RawData */) {
|
|
648
674
|
for (var i = 0; i < channels.length; i++) {
|
|
649
|
-
readDataRaw(reader, imageData, psd.width, psd.height, 4, channels[i]);
|
|
675
|
+
readDataRaw(reader, imageData, psd.width, psd.height, bitsPerChannel, 4, channels[i]);
|
|
650
676
|
}
|
|
651
677
|
}
|
|
652
678
|
else if (compression === 1 /* Compression.RleCompressed */) {
|
|
653
679
|
var start = reader.offset;
|
|
654
|
-
readDataRLE(reader, imageData, psd.width, psd.height, 4, channels, options.large);
|
|
680
|
+
readDataRLE(reader, imageData, psd.width, psd.height, bitsPerChannel, 4, channels, options.large);
|
|
655
681
|
if (helpers_1.RAW_IMAGE_DATA)
|
|
656
682
|
psd.imageDataRaw = new Uint8Array(reader.view.buffer, reader.view.byteOffset + start, reader.offset - start);
|
|
657
683
|
}
|
|
@@ -661,10 +687,12 @@ function readImageData(reader, psd, globalAlpha, options) {
|
|
|
661
687
|
break;
|
|
662
688
|
}
|
|
663
689
|
case 4 /* ColorMode.CMYK */: {
|
|
690
|
+
if (psd.bitsPerChannel !== 8)
|
|
691
|
+
throw new Error('bitsPerChannel Not supproted');
|
|
664
692
|
if (psd.channels !== 4)
|
|
665
693
|
throw new Error("Invalid channel count");
|
|
666
694
|
var channels = [0, 1, 2, 3];
|
|
667
|
-
if (globalAlpha)
|
|
695
|
+
if (options.globalAlpha)
|
|
668
696
|
channels.push(4);
|
|
669
697
|
if (compression === 0 /* Compression.RawData */) {
|
|
670
698
|
throw new Error("Not implemented");
|
|
@@ -680,7 +708,7 @@ function readImageData(reader, psd, globalAlpha, options) {
|
|
|
680
708
|
data: new Uint8Array(imageData.width * imageData.height * 5),
|
|
681
709
|
};
|
|
682
710
|
var start = reader.offset;
|
|
683
|
-
readDataRLE(reader, cmykImageData, psd.width, psd.height, 5, channels, options.large);
|
|
711
|
+
readDataRLE(reader, cmykImageData, psd.width, psd.height, (_b = psd.bitsPerChannel) !== null && _b !== void 0 ? _b : 8, 5, channels, options.large);
|
|
684
712
|
cmykToRgb(cmykImageData, imageData, true);
|
|
685
713
|
if (helpers_1.RAW_IMAGE_DATA)
|
|
686
714
|
psd.imageDataRaw = new Uint8Array(reader.view.buffer, reader.view.byteOffset + start, reader.offset - start);
|
|
@@ -690,7 +718,9 @@ function readImageData(reader, psd, globalAlpha, options) {
|
|
|
690
718
|
default: throw new Error("Color mode not supported: ".concat(psd.colorMode));
|
|
691
719
|
}
|
|
692
720
|
// remove weird white matte
|
|
693
|
-
if (globalAlpha) {
|
|
721
|
+
if (options.globalAlpha) {
|
|
722
|
+
if (psd.bitsPerChannel !== 8)
|
|
723
|
+
throw new Error('bitsPerChannel Not supproted');
|
|
694
724
|
var p = imageData.data;
|
|
695
725
|
var size = imageData.width * imageData.height * 4;
|
|
696
726
|
for (var i = 0; i < size; i += 4) {
|
|
@@ -709,8 +739,7 @@ function readImageData(reader, psd, globalAlpha, options) {
|
|
|
709
739
|
psd.imageData = imageData;
|
|
710
740
|
}
|
|
711
741
|
else {
|
|
712
|
-
psd.canvas = (0, helpers_1.
|
|
713
|
-
psd.canvas.getContext('2d').putImageData(imageData, 0, 0);
|
|
742
|
+
psd.canvas = (0, helpers_1.imageDataToCanvas)(imageData);
|
|
714
743
|
}
|
|
715
744
|
}
|
|
716
745
|
function cmykToRgb(cmyk, rgb, reverseAlpha) {
|
|
@@ -738,29 +767,111 @@ function cmykToRgb(cmyk, rgb, reverseAlpha) {
|
|
|
738
767
|
// dstData[dst + 3] = reverseAlpha ? 255 - srcData[src + 4] : srcData[src + 4];
|
|
739
768
|
// }
|
|
740
769
|
}
|
|
741
|
-
function
|
|
742
|
-
|
|
743
|
-
|
|
770
|
+
function verifyCompatible(a, b) {
|
|
771
|
+
if ((a.byteLength / a.length) !== (b.byteLength / b.length)) {
|
|
772
|
+
throw new Error('Invalid array types');
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
function bytesToArray(bytes, bitDepth) {
|
|
776
|
+
if (bitDepth === 8) {
|
|
777
|
+
return bytes;
|
|
778
|
+
}
|
|
779
|
+
else if (bitDepth === 16) {
|
|
780
|
+
if (bytes.byteOffset % 2) {
|
|
781
|
+
var result = new Uint16Array(bytes.byteLength / 2);
|
|
782
|
+
new Uint8Array(result.buffer, result.byteOffset, result.byteLength).set(bytes);
|
|
783
|
+
return result;
|
|
784
|
+
}
|
|
785
|
+
else {
|
|
786
|
+
return new Uint16Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / 2);
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
else if (bitDepth === 32) {
|
|
790
|
+
if (bytes.byteOffset % 4) {
|
|
791
|
+
var result = new Float32Array(bytes.byteLength / 4);
|
|
792
|
+
new Uint8Array(result.buffer, result.byteOffset, result.byteLength).set(bytes);
|
|
793
|
+
return result;
|
|
794
|
+
}
|
|
795
|
+
else {
|
|
796
|
+
return new Float32Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / 4);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
else {
|
|
800
|
+
throw new Error("Invalid bitDepth (".concat(bitDepth, ")"));
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
function copyChannelToPixelData(pixelData, channel, offset, step) {
|
|
804
|
+
verifyCompatible(pixelData.data, channel);
|
|
805
|
+
var size = pixelData.width * pixelData.height;
|
|
806
|
+
var data = pixelData.data;
|
|
807
|
+
for (var i = 0, p = offset | 0; i < size; i++, p = (p + step) | 0) {
|
|
808
|
+
data[p] = channel[i];
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
function readDataRaw(reader, pixelData, width, height, bitDepth, step, offset) {
|
|
812
|
+
var buffer = readBytes(reader, width * height * Math.floor(bitDepth / 8));
|
|
813
|
+
if (bitDepth == 32) {
|
|
814
|
+
for (var i = 0; i < buffer.byteLength; i += 4) {
|
|
815
|
+
var a = buffer[i + 0];
|
|
816
|
+
var b = buffer[i + 1];
|
|
817
|
+
var c = buffer[i + 2];
|
|
818
|
+
var d = buffer[i + 3];
|
|
819
|
+
buffer[i + 0] = d;
|
|
820
|
+
buffer[i + 1] = c;
|
|
821
|
+
buffer[i + 2] = b;
|
|
822
|
+
buffer[i + 3] = a;
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
var array = bytesToArray(buffer, bitDepth);
|
|
744
826
|
if (pixelData && offset < step) {
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
827
|
+
copyChannelToPixelData(pixelData, array, offset, step);
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
function decodePredicted(data, width, height, mod) {
|
|
831
|
+
for (var y = 0; y < height; y++) {
|
|
832
|
+
var offset = y * width;
|
|
833
|
+
for (var x = 1, o = offset + 1; x < width; x++, o++) {
|
|
834
|
+
data[o] = (data[o - 1] + data[o]) % mod;
|
|
748
835
|
}
|
|
749
836
|
}
|
|
750
837
|
}
|
|
751
|
-
function
|
|
838
|
+
function readDataZip(reader, length, pixelData, width, height, bitDepth, step, offset, prediction) {
|
|
752
839
|
var compressed = readBytes(reader, length);
|
|
753
|
-
var decompressed = (0,
|
|
754
|
-
var size = width * height;
|
|
840
|
+
var decompressed = (0, zlib_1.inflateSync)(compressed);
|
|
755
841
|
if (pixelData && offset < step) {
|
|
756
|
-
var
|
|
757
|
-
|
|
758
|
-
|
|
842
|
+
var array = bytesToArray(decompressed, bitDepth);
|
|
843
|
+
if (bitDepth === 8) {
|
|
844
|
+
if (prediction)
|
|
845
|
+
decodePredicted(decompressed, width, height, 0x100);
|
|
846
|
+
copyChannelToPixelData(pixelData, decompressed, offset, step);
|
|
847
|
+
}
|
|
848
|
+
else if (bitDepth === 16) {
|
|
849
|
+
if (prediction)
|
|
850
|
+
decodePredicted(array, width, height, 0x10000);
|
|
851
|
+
copyChannelToPixelData(pixelData, array, offset, step);
|
|
852
|
+
}
|
|
853
|
+
else if (bitDepth === 32) {
|
|
854
|
+
if (prediction)
|
|
855
|
+
decodePredicted(decompressed, width * 4, height, 0x100);
|
|
856
|
+
var di = offset;
|
|
857
|
+
var dst = new Uint32Array(pixelData.data.buffer, pixelData.data.byteOffset, pixelData.data.length);
|
|
858
|
+
for (var y = 0; y < height; y++) {
|
|
859
|
+
var a = width * 4 * y;
|
|
860
|
+
for (var x = 0; x < width; x++, a++, di += step) {
|
|
861
|
+
var b = a + width;
|
|
862
|
+
var c = b + width;
|
|
863
|
+
var d = c + width;
|
|
864
|
+
dst[di] = ((decompressed[a] << 24) | (decompressed[b] << 16) | (decompressed[c] << 8) | decompressed[d]) >>> 0;
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
else {
|
|
869
|
+
throw new Error('Invalid bitDepth');
|
|
759
870
|
}
|
|
760
871
|
}
|
|
761
872
|
}
|
|
762
|
-
exports.
|
|
763
|
-
function readDataRLE(reader, pixelData, _width, height, step, offsets, large) {
|
|
873
|
+
exports.readDataZip = readDataZip;
|
|
874
|
+
function readDataRLE(reader, pixelData, _width, height, bitDepth, step, offsets, large) {
|
|
764
875
|
var data = pixelData && pixelData.data;
|
|
765
876
|
var lengths;
|
|
766
877
|
if (large) {
|
|
@@ -779,6 +890,8 @@ function readDataRLE(reader, pixelData, _width, height, step, offsets, large) {
|
|
|
779
890
|
}
|
|
780
891
|
}
|
|
781
892
|
}
|
|
893
|
+
if (bitDepth !== 1 && bitDepth !== 8)
|
|
894
|
+
throw new Error("Invalid bit depth (".concat(bitDepth, ")"));
|
|
782
895
|
var extraLimit = (step - 1) | 0; // 3 for rgb, 4 for cmyk
|
|
783
896
|
for (var c = 0, li = 0; c < offsets.length; c++) {
|
|
784
897
|
var offset = offsets[c] | 0;
|