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-es/psdReader.js
CHANGED
|
@@ -9,8 +9,8 @@ var __assign = (this && this.__assign) || function () {
|
|
|
9
9
|
};
|
|
10
10
|
return __assign.apply(this, arguments);
|
|
11
11
|
};
|
|
12
|
-
import {
|
|
13
|
-
import { resetImageData, offsetForChannel, decodeBitmap,
|
|
12
|
+
import { inflateSync } from 'zlib';
|
|
13
|
+
import { resetImageData, offsetForChannel, decodeBitmap, createImageData, toBlendMode, RAW_IMAGE_DATA, largeAdditionalInfoKeys, imageDataToCanvas } from './helpers';
|
|
14
14
|
import { infoHandlersMap } from './additionalInfo';
|
|
15
15
|
import { resourceHandlersMap } from './imageResources';
|
|
16
16
|
export var supportedColorModes = [0 /* ColorMode.Bitmap */, 1 /* ColorMode.Grayscale */, 3 /* ColorMode.RGB */];
|
|
@@ -160,9 +160,9 @@ function readShortString(reader, length) {
|
|
|
160
160
|
function isValidSignature(sig) {
|
|
161
161
|
return sig === '8BIM' || sig === 'MeSa' || sig === 'AgHg' || sig === 'PHUT' || sig === 'DCSR';
|
|
162
162
|
}
|
|
163
|
-
export function readPsd(reader,
|
|
163
|
+
export function readPsd(reader, readOptions) {
|
|
164
164
|
var _a;
|
|
165
|
-
if (
|
|
165
|
+
if (readOptions === void 0) { readOptions = {}; }
|
|
166
166
|
// header
|
|
167
167
|
checkSignature(reader, '8BPS');
|
|
168
168
|
var version = readUint16(reader);
|
|
@@ -176,20 +176,27 @@ export function readPsd(reader, options) {
|
|
|
176
176
|
var colorMode = readUint16(reader);
|
|
177
177
|
var maxSize = version === 1 ? 30000 : 300000;
|
|
178
178
|
if (width > maxSize || height > maxSize)
|
|
179
|
-
throw new Error("Invalid size");
|
|
179
|
+
throw new Error("Invalid size: ".concat(width, "x").concat(height));
|
|
180
180
|
if (channels > 16)
|
|
181
|
-
throw new Error("Invalid channel count");
|
|
182
|
-
if (
|
|
183
|
-
throw new Error("Invalid bitsPerChannel
|
|
181
|
+
throw new Error("Invalid channel count: ".concat(channels));
|
|
182
|
+
if (![1, 8, 16, 32].includes(bitsPerChannel))
|
|
183
|
+
throw new Error("Invalid bitsPerChannel: ".concat(bitsPerChannel));
|
|
184
184
|
if (supportedColorModes.indexOf(colorMode) === -1)
|
|
185
185
|
throw new Error("Color mode not supported: ".concat((_a = colorModes[colorMode]) !== null && _a !== void 0 ? _a : colorMode));
|
|
186
186
|
var psd = { width: width, height: height, channels: channels, bitsPerChannel: bitsPerChannel, colorMode: colorMode };
|
|
187
|
-
var
|
|
187
|
+
var options = __assign(__assign({}, readOptions), { large: version === 2, globalAlpha: false });
|
|
188
188
|
var fixOffsets = [0, 1, -1, 2, -2, 3, -3, 4, -4];
|
|
189
189
|
// color mode data
|
|
190
190
|
readSection(reader, 1, function (left) {
|
|
191
|
-
if (
|
|
192
|
-
|
|
191
|
+
if (!left())
|
|
192
|
+
return;
|
|
193
|
+
// const numbers: number[] = [];
|
|
194
|
+
// console.log('color mode', left());
|
|
195
|
+
// while (left()) {
|
|
196
|
+
// numbers.push(readUint32(reader));
|
|
197
|
+
// }
|
|
198
|
+
// console.log('color mode', numbers);
|
|
199
|
+
// if (options.throwForMissingFeatures) throw new Error('Color mode data not supported');
|
|
193
200
|
skipBytes(reader, left());
|
|
194
201
|
});
|
|
195
202
|
// image resources
|
|
@@ -215,22 +222,22 @@ export function readPsd(reader, options) {
|
|
|
215
222
|
readPascalString(reader, 2); // name
|
|
216
223
|
readSection(reader, 2, function (left) {
|
|
217
224
|
var handler = resourceHandlersMap[id];
|
|
218
|
-
var skip = id === 1036 && !!
|
|
225
|
+
var skip = id === 1036 && !!options.skipThumbnail;
|
|
219
226
|
if (!psd.imageResources) {
|
|
220
227
|
psd.imageResources = {};
|
|
221
228
|
}
|
|
222
229
|
if (handler && !skip) {
|
|
223
230
|
try {
|
|
224
|
-
handler.read(reader, psd.imageResources, left,
|
|
231
|
+
handler.read(reader, psd.imageResources, left, options);
|
|
225
232
|
}
|
|
226
233
|
catch (e) {
|
|
227
|
-
if (
|
|
234
|
+
if (options.throwForMissingFeatures)
|
|
228
235
|
throw e;
|
|
229
236
|
skipBytes(reader, left());
|
|
230
237
|
}
|
|
231
238
|
}
|
|
232
239
|
else {
|
|
233
|
-
// options.logMissingFeatures && console.log(`Unhandled image resource: ${id}`);
|
|
240
|
+
// options.logMissingFeatures && console.log(`Unhandled image resource: ${id} (${left()})`);
|
|
234
241
|
skipBytes(reader, left());
|
|
235
242
|
}
|
|
236
243
|
});
|
|
@@ -240,9 +247,11 @@ export function readPsd(reader, options) {
|
|
|
240
247
|
}
|
|
241
248
|
});
|
|
242
249
|
// layer and mask info
|
|
243
|
-
var globalAlpha = false;
|
|
244
250
|
readSection(reader, 1, function (left) {
|
|
245
|
-
|
|
251
|
+
readSection(reader, 2, function (left) {
|
|
252
|
+
readLayerInfo(reader, psd, options);
|
|
253
|
+
skipBytes(reader, left());
|
|
254
|
+
}, undefined, options.large);
|
|
246
255
|
// SAI does not include this section
|
|
247
256
|
if (left() > 0) {
|
|
248
257
|
var globalLayerMaskInfo = readGlobalLayerMaskInfo(reader);
|
|
@@ -261,70 +270,65 @@ export function readPsd(reader, options) {
|
|
|
261
270
|
skipBytes(reader, 1);
|
|
262
271
|
}
|
|
263
272
|
if (left() >= 12) {
|
|
264
|
-
readAdditionalLayerInfo(reader, psd, psd,
|
|
273
|
+
readAdditionalLayerInfo(reader, psd, psd, options);
|
|
265
274
|
}
|
|
266
275
|
else {
|
|
267
276
|
// opt.logMissingFeatures && console.log('skipping leftover bytes', left());
|
|
268
277
|
skipBytes(reader, left());
|
|
269
278
|
}
|
|
270
279
|
}
|
|
271
|
-
}, undefined,
|
|
280
|
+
}, undefined, options.large);
|
|
272
281
|
var hasChildren = psd.children && psd.children.length;
|
|
273
|
-
var skipComposite =
|
|
282
|
+
var skipComposite = options.skipCompositeImageData && (options.skipLayerImageData || hasChildren);
|
|
274
283
|
if (!skipComposite) {
|
|
275
|
-
readImageData(reader, psd,
|
|
284
|
+
readImageData(reader, psd, options);
|
|
276
285
|
}
|
|
277
286
|
// TODO: show converted color mode instead of original PSD file color mode
|
|
278
287
|
// but add option to preserve file color mode (need to return image data instead of canvas in that case)
|
|
279
288
|
// psd.colorMode = ColorMode.RGB; // we convert all color modes to RGB
|
|
280
289
|
return psd;
|
|
281
290
|
}
|
|
282
|
-
function readLayerInfo(reader, psd, options) {
|
|
283
|
-
var
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
var
|
|
291
|
+
export function readLayerInfo(reader, psd, options) {
|
|
292
|
+
var layerCount = readInt16(reader);
|
|
293
|
+
if (layerCount < 0) {
|
|
294
|
+
options.globalAlpha = true;
|
|
295
|
+
layerCount = -layerCount;
|
|
296
|
+
}
|
|
297
|
+
var layers = [];
|
|
298
|
+
var layerChannels = [];
|
|
299
|
+
for (var i = 0; i < layerCount; i++) {
|
|
300
|
+
var _a = readLayerRecord(reader, psd, options), layer = _a.layer, channels = _a.channels;
|
|
301
|
+
layers.push(layer);
|
|
302
|
+
layerChannels.push(channels);
|
|
303
|
+
}
|
|
304
|
+
if (!options.skipLayerImageData) {
|
|
292
305
|
for (var i = 0; i < layerCount; i++) {
|
|
293
|
-
|
|
294
|
-
layers.push(layer);
|
|
295
|
-
layerChannels.push(channels);
|
|
306
|
+
readLayerChannelImageData(reader, psd, layers[i], layerChannels[i], options);
|
|
296
307
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
308
|
+
}
|
|
309
|
+
if (!psd.children)
|
|
310
|
+
psd.children = [];
|
|
311
|
+
var stack = [psd];
|
|
312
|
+
for (var i = layers.length - 1; i >= 0; i--) {
|
|
313
|
+
var l = layers[i];
|
|
314
|
+
var type = l.sectionDivider ? l.sectionDivider.type : 0 /* SectionDividerType.Other */;
|
|
315
|
+
if (type === 1 /* SectionDividerType.OpenFolder */ || type === 2 /* SectionDividerType.ClosedFolder */) {
|
|
316
|
+
l.opened = type === 1 /* SectionDividerType.OpenFolder */;
|
|
317
|
+
l.children = [];
|
|
318
|
+
stack[stack.length - 1].children.unshift(l);
|
|
319
|
+
stack.push(l);
|
|
301
320
|
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
var type = l.sectionDivider ? l.sectionDivider.type : 0 /* SectionDividerType.Other */;
|
|
309
|
-
if (type === 1 /* SectionDividerType.OpenFolder */ || type === 2 /* SectionDividerType.ClosedFolder */) {
|
|
310
|
-
l.opened = type === 1 /* SectionDividerType.OpenFolder */;
|
|
311
|
-
l.children = [];
|
|
312
|
-
stack[stack.length - 1].children.unshift(l);
|
|
313
|
-
stack.push(l);
|
|
314
|
-
}
|
|
315
|
-
else if (type === 3 /* SectionDividerType.BoundingSectionDivider */) {
|
|
316
|
-
stack.pop();
|
|
317
|
-
// this was workaround because I didn't know what `lsdk` section was, now it's probably not needed anymore
|
|
318
|
-
// } else if (l.name === '</Layer group>' && !l.sectionDivider && !l.top && !l.left && !l.bottom && !l.right) {
|
|
319
|
-
// // sometimes layer group terminator doesn't have sectionDivider, so we just guess here (PS bug ?)
|
|
320
|
-
// stack.pop();
|
|
321
|
-
}
|
|
322
|
-
else {
|
|
323
|
-
stack[stack.length - 1].children.unshift(l);
|
|
324
|
-
}
|
|
321
|
+
else if (type === 3 /* SectionDividerType.BoundingSectionDivider */) {
|
|
322
|
+
stack.pop();
|
|
323
|
+
// this was workaround because I didn't know what `lsdk` section was, now it's probably not needed anymore
|
|
324
|
+
// } else if (l.name === '</Layer group>' && !l.sectionDivider && !l.top && !l.left && !l.bottom && !l.right) {
|
|
325
|
+
// // sometimes layer group terminator doesn't have sectionDivider, so we just guess here (PS bug ?)
|
|
326
|
+
// stack.pop();
|
|
325
327
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
+
else {
|
|
329
|
+
stack[stack.length - 1].children.unshift(l);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
328
332
|
}
|
|
329
333
|
function readLayerRecord(reader, psd, options) {
|
|
330
334
|
var layer = {};
|
|
@@ -426,18 +430,21 @@ function readLayerBlendingRanges(reader) {
|
|
|
426
430
|
});
|
|
427
431
|
}
|
|
428
432
|
function readLayerChannelImageData(reader, psd, layer, channels, options) {
|
|
433
|
+
var _a, _b, _c, _d;
|
|
429
434
|
var layerWidth = (layer.right || 0) - (layer.left || 0);
|
|
430
435
|
var layerHeight = (layer.bottom || 0) - (layer.top || 0);
|
|
431
436
|
var cmyk = psd.colorMode === 4 /* ColorMode.CMYK */;
|
|
432
437
|
var imageData;
|
|
433
438
|
if (layerWidth && layerHeight) {
|
|
434
439
|
if (cmyk) {
|
|
440
|
+
if (psd.bitsPerChannel !== 8)
|
|
441
|
+
throw new Error('bitsPerChannel Not supproted');
|
|
435
442
|
imageData = { width: layerWidth, height: layerHeight, data: new Uint8ClampedArray(layerWidth * layerHeight * 5) };
|
|
436
443
|
for (var p = 4; p < imageData.data.byteLength; p += 5)
|
|
437
444
|
imageData.data[p] = 255;
|
|
438
445
|
}
|
|
439
446
|
else {
|
|
440
|
-
imageData =
|
|
447
|
+
imageData = createImageDataBitDepth(layerWidth, layerHeight, (_a = psd.bitsPerChannel) !== null && _a !== void 0 ? _a : 8);
|
|
441
448
|
resetImageData(imageData);
|
|
442
449
|
}
|
|
443
450
|
}
|
|
@@ -470,10 +477,10 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
|
|
|
470
477
|
var maskWidth = (mask.right || 0) - (mask.left || 0);
|
|
471
478
|
var maskHeight = (mask.bottom || 0) - (mask.top || 0);
|
|
472
479
|
if (maskWidth && maskHeight) {
|
|
473
|
-
var maskData =
|
|
480
|
+
var maskData = createImageDataBitDepth(maskWidth, maskHeight, (_b = psd.bitsPerChannel) !== null && _b !== void 0 ? _b : 8);
|
|
474
481
|
resetImageData(maskData);
|
|
475
482
|
var start_1 = reader.offset;
|
|
476
|
-
readData(reader, channel.length, maskData, compression, maskWidth, maskHeight, 0, options.large, 4);
|
|
483
|
+
readData(reader, channel.length, maskData, compression, maskWidth, maskHeight, (_c = psd.bitsPerChannel) !== null && _c !== void 0 ? _c : 8, 0, options.large, 4);
|
|
477
484
|
if (RAW_IMAGE_DATA) {
|
|
478
485
|
layer.maskDataRaw = new Uint8Array(reader.view.buffer, reader.view.byteOffset + start_1, reader.offset - start_1);
|
|
479
486
|
}
|
|
@@ -482,8 +489,7 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
|
|
|
482
489
|
mask.imageData = maskData;
|
|
483
490
|
}
|
|
484
491
|
else {
|
|
485
|
-
mask.canvas =
|
|
486
|
-
mask.canvas.getContext('2d').putImageData(maskData, 0, 0);
|
|
492
|
+
mask.canvas = imageDataToCanvas(maskData);
|
|
487
493
|
}
|
|
488
494
|
}
|
|
489
495
|
}
|
|
@@ -496,7 +502,7 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
|
|
|
496
502
|
throw new Error("Channel not supported: ".concat(channel.id));
|
|
497
503
|
}
|
|
498
504
|
}
|
|
499
|
-
readData(reader, channel.length, targetData, compression, layerWidth, layerHeight, offset, options.large, cmyk ? 5 : 4);
|
|
505
|
+
readData(reader, channel.length, targetData, compression, layerWidth, layerHeight, (_d = psd.bitsPerChannel) !== null && _d !== void 0 ? _d : 8, offset, options.large, cmyk ? 5 : 4);
|
|
500
506
|
if (RAW_IMAGE_DATA) {
|
|
501
507
|
layer.imageDataRaw[channel.id] = new Uint8Array(reader.view.buffer, reader.view.byteOffset + start + 2, channel.length - 2);
|
|
502
508
|
}
|
|
@@ -516,29 +522,28 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
|
|
|
516
522
|
layer.imageData = imageData;
|
|
517
523
|
}
|
|
518
524
|
else {
|
|
519
|
-
layer.canvas =
|
|
520
|
-
layer.canvas.getContext('2d').putImageData(imageData, 0, 0);
|
|
525
|
+
layer.canvas = imageDataToCanvas(imageData);
|
|
521
526
|
}
|
|
522
527
|
}
|
|
523
528
|
}
|
|
524
|
-
function readData(reader, length, data, compression, width, height, offset, large, step) {
|
|
529
|
+
function readData(reader, length, data, compression, width, height, bitDepth, offset, large, step) {
|
|
525
530
|
if (compression === 0 /* Compression.RawData */) {
|
|
526
|
-
readDataRaw(reader, data, width, height, step, offset);
|
|
531
|
+
readDataRaw(reader, data, width, height, bitDepth, step, offset);
|
|
527
532
|
}
|
|
528
533
|
else if (compression === 1 /* Compression.RleCompressed */) {
|
|
529
|
-
readDataRLE(reader, data, width, height, step, [offset], large);
|
|
534
|
+
readDataRLE(reader, data, width, height, bitDepth, step, [offset], large);
|
|
530
535
|
}
|
|
531
536
|
else if (compression === 2 /* Compression.ZipWithoutPrediction */) {
|
|
532
|
-
|
|
537
|
+
readDataZip(reader, length, data, width, height, bitDepth, step, offset, false);
|
|
533
538
|
}
|
|
534
539
|
else if (compression === 3 /* Compression.ZipWithPrediction */) {
|
|
535
|
-
|
|
540
|
+
readDataZip(reader, length, data, width, height, bitDepth, step, offset, true);
|
|
536
541
|
}
|
|
537
542
|
else {
|
|
538
543
|
throw new Error("Invalid Compression type: ".concat(compression));
|
|
539
544
|
}
|
|
540
545
|
}
|
|
541
|
-
function readGlobalLayerMaskInfo(reader) {
|
|
546
|
+
export function readGlobalLayerMaskInfo(reader) {
|
|
542
547
|
return readSection(reader, 1, function (left) {
|
|
543
548
|
if (!left())
|
|
544
549
|
return undefined;
|
|
@@ -553,7 +558,7 @@ function readGlobalLayerMaskInfo(reader) {
|
|
|
553
558
|
return { overlayColorSpace: overlayColorSpace, colorSpace1: colorSpace1, colorSpace2: colorSpace2, colorSpace3: colorSpace3, colorSpace4: colorSpace4, opacity: opacity, kind: kind };
|
|
554
559
|
});
|
|
555
560
|
}
|
|
556
|
-
function readAdditionalLayerInfo(reader, target, psd, options) {
|
|
561
|
+
export function readAdditionalLayerInfo(reader, target, psd, options) {
|
|
557
562
|
var sig = readSignature(reader);
|
|
558
563
|
if (sig !== '8BIM' && sig !== '8B64')
|
|
559
564
|
throw new Error("Invalid signature: '".concat(sig, "' at 0x").concat((reader.offset - 4).toString(16)));
|
|
@@ -581,23 +586,41 @@ function readAdditionalLayerInfo(reader, target, psd, options) {
|
|
|
581
586
|
}
|
|
582
587
|
}, false, u64);
|
|
583
588
|
}
|
|
584
|
-
function
|
|
589
|
+
function createImageDataBitDepth(width, height, bitDepth) {
|
|
590
|
+
if (bitDepth === 1 || bitDepth === 8) {
|
|
591
|
+
return createImageData(width, height);
|
|
592
|
+
}
|
|
593
|
+
else if (bitDepth === 16) {
|
|
594
|
+
return { width: width, height: height, data: new Uint16Array(width * height * 4) };
|
|
595
|
+
}
|
|
596
|
+
else if (bitDepth === 32) {
|
|
597
|
+
return { width: width, height: height, data: new Float32Array(width * height * 4) };
|
|
598
|
+
}
|
|
599
|
+
else {
|
|
600
|
+
throw new Error("Invalid bitDepth (".concat(bitDepth, ")"));
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
function readImageData(reader, psd, options) {
|
|
604
|
+
var _a, _b;
|
|
585
605
|
var compression = readUint16(reader);
|
|
606
|
+
var bitsPerChannel = (_a = psd.bitsPerChannel) !== null && _a !== void 0 ? _a : 8;
|
|
586
607
|
if (supportedColorModes.indexOf(psd.colorMode) === -1)
|
|
587
608
|
throw new Error("Color mode not supported: ".concat(psd.colorMode));
|
|
588
609
|
if (compression !== 0 /* Compression.RawData */ && compression !== 1 /* Compression.RleCompressed */)
|
|
589
610
|
throw new Error("Compression type not supported: ".concat(compression));
|
|
590
|
-
var imageData =
|
|
611
|
+
var imageData = createImageDataBitDepth(psd.width, psd.height, bitsPerChannel);
|
|
591
612
|
resetImageData(imageData);
|
|
592
613
|
switch (psd.colorMode) {
|
|
593
614
|
case 0 /* ColorMode.Bitmap */: {
|
|
615
|
+
if (bitsPerChannel !== 1)
|
|
616
|
+
throw new Error('Invalid bitsPerChannel for bitmap color mode');
|
|
594
617
|
var bytes = void 0;
|
|
595
618
|
if (compression === 0 /* Compression.RawData */) {
|
|
596
619
|
bytes = readBytes(reader, Math.ceil(psd.width / 8) * psd.height);
|
|
597
620
|
}
|
|
598
621
|
else if (compression === 1 /* Compression.RleCompressed */) {
|
|
599
622
|
bytes = new Uint8Array(psd.width * psd.height);
|
|
600
|
-
readDataRLE(reader, { data: bytes, width: psd.width, height: psd.height }, psd.width, psd.height, 1, [0], options.large);
|
|
623
|
+
readDataRLE(reader, { data: bytes, width: psd.width, height: psd.height }, psd.width, psd.height, 8, 1, [0], options.large);
|
|
601
624
|
}
|
|
602
625
|
else {
|
|
603
626
|
throw new Error("Bitmap compression not supported: ".concat(compression));
|
|
@@ -614,17 +637,17 @@ function readImageData(reader, psd, globalAlpha, options) {
|
|
|
614
637
|
channels.push(i);
|
|
615
638
|
}
|
|
616
639
|
}
|
|
617
|
-
else if (globalAlpha) {
|
|
640
|
+
else if (options.globalAlpha) {
|
|
618
641
|
channels.push(3);
|
|
619
642
|
}
|
|
620
643
|
if (compression === 0 /* Compression.RawData */) {
|
|
621
644
|
for (var i = 0; i < channels.length; i++) {
|
|
622
|
-
readDataRaw(reader, imageData, psd.width, psd.height, 4, channels[i]);
|
|
645
|
+
readDataRaw(reader, imageData, psd.width, psd.height, bitsPerChannel, 4, channels[i]);
|
|
623
646
|
}
|
|
624
647
|
}
|
|
625
648
|
else if (compression === 1 /* Compression.RleCompressed */) {
|
|
626
649
|
var start = reader.offset;
|
|
627
|
-
readDataRLE(reader, imageData, psd.width, psd.height, 4, channels, options.large);
|
|
650
|
+
readDataRLE(reader, imageData, psd.width, psd.height, bitsPerChannel, 4, channels, options.large);
|
|
628
651
|
if (RAW_IMAGE_DATA)
|
|
629
652
|
psd.imageDataRaw = new Uint8Array(reader.view.buffer, reader.view.byteOffset + start, reader.offset - start);
|
|
630
653
|
}
|
|
@@ -634,10 +657,12 @@ function readImageData(reader, psd, globalAlpha, options) {
|
|
|
634
657
|
break;
|
|
635
658
|
}
|
|
636
659
|
case 4 /* ColorMode.CMYK */: {
|
|
660
|
+
if (psd.bitsPerChannel !== 8)
|
|
661
|
+
throw new Error('bitsPerChannel Not supproted');
|
|
637
662
|
if (psd.channels !== 4)
|
|
638
663
|
throw new Error("Invalid channel count");
|
|
639
664
|
var channels = [0, 1, 2, 3];
|
|
640
|
-
if (globalAlpha)
|
|
665
|
+
if (options.globalAlpha)
|
|
641
666
|
channels.push(4);
|
|
642
667
|
if (compression === 0 /* Compression.RawData */) {
|
|
643
668
|
throw new Error("Not implemented");
|
|
@@ -653,7 +678,7 @@ function readImageData(reader, psd, globalAlpha, options) {
|
|
|
653
678
|
data: new Uint8Array(imageData.width * imageData.height * 5),
|
|
654
679
|
};
|
|
655
680
|
var start = reader.offset;
|
|
656
|
-
readDataRLE(reader, cmykImageData, psd.width, psd.height, 5, channels, options.large);
|
|
681
|
+
readDataRLE(reader, cmykImageData, psd.width, psd.height, (_b = psd.bitsPerChannel) !== null && _b !== void 0 ? _b : 8, 5, channels, options.large);
|
|
657
682
|
cmykToRgb(cmykImageData, imageData, true);
|
|
658
683
|
if (RAW_IMAGE_DATA)
|
|
659
684
|
psd.imageDataRaw = new Uint8Array(reader.view.buffer, reader.view.byteOffset + start, reader.offset - start);
|
|
@@ -663,7 +688,9 @@ function readImageData(reader, psd, globalAlpha, options) {
|
|
|
663
688
|
default: throw new Error("Color mode not supported: ".concat(psd.colorMode));
|
|
664
689
|
}
|
|
665
690
|
// remove weird white matte
|
|
666
|
-
if (globalAlpha) {
|
|
691
|
+
if (options.globalAlpha) {
|
|
692
|
+
if (psd.bitsPerChannel !== 8)
|
|
693
|
+
throw new Error('bitsPerChannel Not supproted');
|
|
667
694
|
var p = imageData.data;
|
|
668
695
|
var size = imageData.width * imageData.height * 4;
|
|
669
696
|
for (var i = 0; i < size; i += 4) {
|
|
@@ -682,8 +709,7 @@ function readImageData(reader, psd, globalAlpha, options) {
|
|
|
682
709
|
psd.imageData = imageData;
|
|
683
710
|
}
|
|
684
711
|
else {
|
|
685
|
-
psd.canvas =
|
|
686
|
-
psd.canvas.getContext('2d').putImageData(imageData, 0, 0);
|
|
712
|
+
psd.canvas = imageDataToCanvas(imageData);
|
|
687
713
|
}
|
|
688
714
|
}
|
|
689
715
|
function cmykToRgb(cmyk, rgb, reverseAlpha) {
|
|
@@ -711,28 +737,110 @@ function cmykToRgb(cmyk, rgb, reverseAlpha) {
|
|
|
711
737
|
// dstData[dst + 3] = reverseAlpha ? 255 - srcData[src + 4] : srcData[src + 4];
|
|
712
738
|
// }
|
|
713
739
|
}
|
|
714
|
-
function
|
|
715
|
-
|
|
716
|
-
|
|
740
|
+
function verifyCompatible(a, b) {
|
|
741
|
+
if ((a.byteLength / a.length) !== (b.byteLength / b.length)) {
|
|
742
|
+
throw new Error('Invalid array types');
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
function bytesToArray(bytes, bitDepth) {
|
|
746
|
+
if (bitDepth === 8) {
|
|
747
|
+
return bytes;
|
|
748
|
+
}
|
|
749
|
+
else if (bitDepth === 16) {
|
|
750
|
+
if (bytes.byteOffset % 2) {
|
|
751
|
+
var result = new Uint16Array(bytes.byteLength / 2);
|
|
752
|
+
new Uint8Array(result.buffer, result.byteOffset, result.byteLength).set(bytes);
|
|
753
|
+
return result;
|
|
754
|
+
}
|
|
755
|
+
else {
|
|
756
|
+
return new Uint16Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / 2);
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
else if (bitDepth === 32) {
|
|
760
|
+
if (bytes.byteOffset % 4) {
|
|
761
|
+
var result = new Float32Array(bytes.byteLength / 4);
|
|
762
|
+
new Uint8Array(result.buffer, result.byteOffset, result.byteLength).set(bytes);
|
|
763
|
+
return result;
|
|
764
|
+
}
|
|
765
|
+
else {
|
|
766
|
+
return new Float32Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / 4);
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
else {
|
|
770
|
+
throw new Error("Invalid bitDepth (".concat(bitDepth, ")"));
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
function copyChannelToPixelData(pixelData, channel, offset, step) {
|
|
774
|
+
verifyCompatible(pixelData.data, channel);
|
|
775
|
+
var size = pixelData.width * pixelData.height;
|
|
776
|
+
var data = pixelData.data;
|
|
777
|
+
for (var i = 0, p = offset | 0; i < size; i++, p = (p + step) | 0) {
|
|
778
|
+
data[p] = channel[i];
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
function readDataRaw(reader, pixelData, width, height, bitDepth, step, offset) {
|
|
782
|
+
var buffer = readBytes(reader, width * height * Math.floor(bitDepth / 8));
|
|
783
|
+
if (bitDepth == 32) {
|
|
784
|
+
for (var i = 0; i < buffer.byteLength; i += 4) {
|
|
785
|
+
var a = buffer[i + 0];
|
|
786
|
+
var b = buffer[i + 1];
|
|
787
|
+
var c = buffer[i + 2];
|
|
788
|
+
var d = buffer[i + 3];
|
|
789
|
+
buffer[i + 0] = d;
|
|
790
|
+
buffer[i + 1] = c;
|
|
791
|
+
buffer[i + 2] = b;
|
|
792
|
+
buffer[i + 3] = a;
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
var array = bytesToArray(buffer, bitDepth);
|
|
717
796
|
if (pixelData && offset < step) {
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
797
|
+
copyChannelToPixelData(pixelData, array, offset, step);
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
function decodePredicted(data, width, height, mod) {
|
|
801
|
+
for (var y = 0; y < height; y++) {
|
|
802
|
+
var offset = y * width;
|
|
803
|
+
for (var x = 1, o = offset + 1; x < width; x++, o++) {
|
|
804
|
+
data[o] = (data[o - 1] + data[o]) % mod;
|
|
721
805
|
}
|
|
722
806
|
}
|
|
723
807
|
}
|
|
724
|
-
export function
|
|
808
|
+
export function readDataZip(reader, length, pixelData, width, height, bitDepth, step, offset, prediction) {
|
|
725
809
|
var compressed = readBytes(reader, length);
|
|
726
|
-
var decompressed =
|
|
727
|
-
var size = width * height;
|
|
810
|
+
var decompressed = inflateSync(compressed);
|
|
728
811
|
if (pixelData && offset < step) {
|
|
729
|
-
var
|
|
730
|
-
|
|
731
|
-
|
|
812
|
+
var array = bytesToArray(decompressed, bitDepth);
|
|
813
|
+
if (bitDepth === 8) {
|
|
814
|
+
if (prediction)
|
|
815
|
+
decodePredicted(decompressed, width, height, 0x100);
|
|
816
|
+
copyChannelToPixelData(pixelData, decompressed, offset, step);
|
|
817
|
+
}
|
|
818
|
+
else if (bitDepth === 16) {
|
|
819
|
+
if (prediction)
|
|
820
|
+
decodePredicted(array, width, height, 0x10000);
|
|
821
|
+
copyChannelToPixelData(pixelData, array, offset, step);
|
|
822
|
+
}
|
|
823
|
+
else if (bitDepth === 32) {
|
|
824
|
+
if (prediction)
|
|
825
|
+
decodePredicted(decompressed, width * 4, height, 0x100);
|
|
826
|
+
var di = offset;
|
|
827
|
+
var dst = new Uint32Array(pixelData.data.buffer, pixelData.data.byteOffset, pixelData.data.length);
|
|
828
|
+
for (var y = 0; y < height; y++) {
|
|
829
|
+
var a = width * 4 * y;
|
|
830
|
+
for (var x = 0; x < width; x++, a++, di += step) {
|
|
831
|
+
var b = a + width;
|
|
832
|
+
var c = b + width;
|
|
833
|
+
var d = c + width;
|
|
834
|
+
dst[di] = ((decompressed[a] << 24) | (decompressed[b] << 16) | (decompressed[c] << 8) | decompressed[d]) >>> 0;
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
else {
|
|
839
|
+
throw new Error('Invalid bitDepth');
|
|
732
840
|
}
|
|
733
841
|
}
|
|
734
842
|
}
|
|
735
|
-
export function readDataRLE(reader, pixelData, _width, height, step, offsets, large) {
|
|
843
|
+
export function readDataRLE(reader, pixelData, _width, height, bitDepth, step, offsets, large) {
|
|
736
844
|
var data = pixelData && pixelData.data;
|
|
737
845
|
var lengths;
|
|
738
846
|
if (large) {
|
|
@@ -751,6 +859,8 @@ export function readDataRLE(reader, pixelData, _width, height, step, offsets, la
|
|
|
751
859
|
}
|
|
752
860
|
}
|
|
753
861
|
}
|
|
862
|
+
if (bitDepth !== 1 && bitDepth !== 8)
|
|
863
|
+
throw new Error("Invalid bit depth (".concat(bitDepth, ")"));
|
|
754
864
|
var extraLimit = (step - 1) | 0; // 3 for rgb, 4 for cmyk
|
|
755
865
|
for (var c = 0, li = 0; c < offsets.length; c++) {
|
|
756
866
|
var offset = offsets[c] | 0;
|