ag-psd 19.0.0 → 20.0.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.
Files changed (53) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README_PSD.md +6 -0
  3. package/TODO +5 -0
  4. package/dist/abr.js +4 -4
  5. package/dist/abr.js.map +1 -1
  6. package/dist/additionalInfo.d.ts +3 -3
  7. package/dist/additionalInfo.js +99 -10
  8. package/dist/additionalInfo.js.map +1 -1
  9. package/dist/bundle.js +408 -151
  10. package/dist/descriptor.js +21 -16
  11. package/dist/descriptor.js.map +1 -1
  12. package/dist/helpers.d.ts +2 -7
  13. package/dist/helpers.js +33 -10
  14. package/dist/helpers.js.map +1 -1
  15. package/dist/imageResources.js +37 -0
  16. package/dist/imageResources.js.map +1 -1
  17. package/dist/psd.d.ts +46 -3
  18. package/dist/psd.js +8 -1
  19. package/dist/psd.js.map +1 -1
  20. package/dist/psdReader.d.ts +11 -5
  21. package/dist/psdReader.js +179 -97
  22. package/dist/psdReader.js.map +1 -1
  23. package/dist/psdWriter.js +27 -13
  24. package/dist/psdWriter.js.map +1 -1
  25. package/dist-es/abr.js +4 -4
  26. package/dist-es/abr.js.map +1 -1
  27. package/dist-es/additionalInfo.d.ts +3 -3
  28. package/dist-es/additionalInfo.js +102 -13
  29. package/dist-es/additionalInfo.js.map +1 -1
  30. package/dist-es/descriptor.js +21 -16
  31. package/dist-es/descriptor.js.map +1 -1
  32. package/dist-es/helpers.d.ts +2 -7
  33. package/dist-es/helpers.js +31 -9
  34. package/dist-es/helpers.js.map +1 -1
  35. package/dist-es/imageResources.js +37 -0
  36. package/dist-es/imageResources.js.map +1 -1
  37. package/dist-es/psd.d.ts +46 -3
  38. package/dist-es/psd.js +7 -0
  39. package/dist-es/psd.js.map +1 -1
  40. package/dist-es/psdReader.d.ts +11 -5
  41. package/dist-es/psdReader.js +178 -99
  42. package/dist-es/psdReader.js.map +1 -1
  43. package/dist-es/psdWriter.js +28 -14
  44. package/dist-es/psdWriter.js.map +1 -1
  45. package/package.json +1 -1
  46. package/src/abr.ts +4 -4
  47. package/src/additionalInfo.ts +142 -49
  48. package/src/descriptor.ts +14 -9
  49. package/src/helpers.ts +35 -18
  50. package/src/imageResources.ts +53 -0
  51. package/src/psd.ts +41 -5
  52. package/src/psdReader.ts +170 -126
  53. package/src/psdWriter.ts +36 -23
package/dist/psdReader.js CHANGED
@@ -11,7 +11,7 @@ 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.readDataZipWithoutPrediction = 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;
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
15
  var pako_1 = require("pako");
16
16
  var helpers_1 = require("./helpers");
17
17
  var additionalInfo_1 = require("./additionalInfo");
@@ -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, options) {
189
+ function readPsd(reader, readOptions) {
190
190
  var _a;
191
- if (options === void 0) { options = {}; }
191
+ if (readOptions === void 0) { readOptions = {}; }
192
192
  // header
193
193
  checkSignature(reader, '8BPS');
194
194
  var version = readUint16(reader);
@@ -202,19 +202,19 @@ 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 (bitsPerChannel > 32)
209
- throw new Error("Invalid bitsPerChannel count");
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 opt = __assign(__assign({}, options), { large: version === 2 });
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 (opt.throwForMissingFeatures)
217
+ if (options.throwForMissingFeatures)
218
218
  throw new Error('Color mode data not supported');
219
219
  skipBytes(reader, left());
220
220
  });
@@ -241,22 +241,22 @@ function readPsd(reader, options) {
241
241
  readPascalString(reader, 2); // name
242
242
  readSection(reader, 2, function (left) {
243
243
  var handler = imageResources_1.resourceHandlersMap[id];
244
- var skip = id === 1036 && !!opt.skipThumbnail;
244
+ var skip = id === 1036 && !!options.skipThumbnail;
245
245
  if (!psd.imageResources) {
246
246
  psd.imageResources = {};
247
247
  }
248
248
  if (handler && !skip) {
249
249
  try {
250
- handler.read(reader, psd.imageResources, left, opt);
250
+ handler.read(reader, psd.imageResources, left, options);
251
251
  }
252
252
  catch (e) {
253
- if (opt.throwForMissingFeatures)
253
+ if (options.throwForMissingFeatures)
254
254
  throw e;
255
255
  skipBytes(reader, left());
256
256
  }
257
257
  }
258
258
  else {
259
- // options.logMissingFeatures && console.log(`Unhandled image resource: ${id}`);
259
+ // options.logMissingFeatures && console.log(`Unhandled image resource: ${id} (${left()})`);
260
260
  skipBytes(reader, left());
261
261
  }
262
262
  });
@@ -266,9 +266,11 @@ function readPsd(reader, options) {
266
266
  }
267
267
  });
268
268
  // layer and mask info
269
- var globalAlpha = false;
270
269
  readSection(reader, 1, function (left) {
271
- globalAlpha = readLayerInfo(reader, psd, opt);
270
+ readSection(reader, 2, function (left) {
271
+ readLayerInfo(reader, psd, options);
272
+ skipBytes(reader, left());
273
+ }, undefined, options.large);
272
274
  // SAI does not include this section
273
275
  if (left() > 0) {
274
276
  var globalLayerMaskInfo = readGlobalLayerMaskInfo(reader);
@@ -287,18 +289,18 @@ function readPsd(reader, options) {
287
289
  skipBytes(reader, 1);
288
290
  }
289
291
  if (left() >= 12) {
290
- readAdditionalLayerInfo(reader, psd, psd, opt);
292
+ readAdditionalLayerInfo(reader, psd, psd, options);
291
293
  }
292
294
  else {
293
295
  // opt.logMissingFeatures && console.log('skipping leftover bytes', left());
294
296
  skipBytes(reader, left());
295
297
  }
296
298
  }
297
- }, undefined, opt.large);
299
+ }, undefined, options.large);
298
300
  var hasChildren = psd.children && psd.children.length;
299
- var skipComposite = opt.skipCompositeImageData && (opt.skipLayerImageData || hasChildren);
301
+ var skipComposite = options.skipCompositeImageData && (options.skipLayerImageData || hasChildren);
300
302
  if (!skipComposite) {
301
- readImageData(reader, psd, globalAlpha, opt);
303
+ readImageData(reader, psd, options);
302
304
  }
303
305
  // TODO: show converted color mode instead of original PSD file color mode
304
306
  // but add option to preserve file color mode (need to return image data instead of canvas in that case)
@@ -307,52 +309,48 @@ function readPsd(reader, options) {
307
309
  }
308
310
  exports.readPsd = readPsd;
309
311
  function readLayerInfo(reader, psd, options) {
310
- var globalAlpha = false;
311
- readSection(reader, 2, function (left) {
312
- var layerCount = readInt16(reader);
313
- if (layerCount < 0) {
314
- globalAlpha = true;
315
- layerCount = -layerCount;
316
- }
317
- var layers = [];
318
- var layerChannels = [];
312
+ var layerCount = readInt16(reader);
313
+ if (layerCount < 0) {
314
+ options.globalAlpha = true;
315
+ layerCount = -layerCount;
316
+ }
317
+ var layers = [];
318
+ var layerChannels = [];
319
+ for (var i = 0; i < layerCount; i++) {
320
+ var _a = readLayerRecord(reader, psd, options), layer = _a.layer, channels = _a.channels;
321
+ layers.push(layer);
322
+ layerChannels.push(channels);
323
+ }
324
+ if (!options.skipLayerImageData) {
319
325
  for (var i = 0; i < layerCount; i++) {
320
- var _a = readLayerRecord(reader, psd, options), layer = _a.layer, channels = _a.channels;
321
- layers.push(layer);
322
- layerChannels.push(channels);
326
+ readLayerChannelImageData(reader, psd, layers[i], layerChannels[i], options);
323
327
  }
324
- if (!options.skipLayerImageData) {
325
- for (var i = 0; i < layerCount; i++) {
326
- readLayerChannelImageData(reader, psd, layers[i], layerChannels[i], options);
327
- }
328
+ }
329
+ if (!psd.children)
330
+ psd.children = [];
331
+ var stack = [psd];
332
+ for (var i = layers.length - 1; i >= 0; i--) {
333
+ var l = layers[i];
334
+ var type = l.sectionDivider ? l.sectionDivider.type : 0 /* SectionDividerType.Other */;
335
+ if (type === 1 /* SectionDividerType.OpenFolder */ || type === 2 /* SectionDividerType.ClosedFolder */) {
336
+ l.opened = type === 1 /* SectionDividerType.OpenFolder */;
337
+ l.children = [];
338
+ stack[stack.length - 1].children.unshift(l);
339
+ stack.push(l);
328
340
  }
329
- skipBytes(reader, left());
330
- if (!psd.children)
331
- psd.children = [];
332
- var stack = [psd];
333
- for (var i = layers.length - 1; i >= 0; i--) {
334
- var l = layers[i];
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
- }
341
+ else if (type === 3 /* SectionDividerType.BoundingSectionDivider */) {
342
+ stack.pop();
343
+ // this was workaround because I didn't know what `lsdk` section was, now it's probably not needed anymore
344
+ // } else if (l.name === '</Layer group>' && !l.sectionDivider && !l.top && !l.left && !l.bottom && !l.right) {
345
+ // // sometimes layer group terminator doesn't have sectionDivider, so we just guess here (PS bug ?)
346
+ // stack.pop();
352
347
  }
353
- }, undefined, options.large);
354
- return globalAlpha;
348
+ else {
349
+ stack[stack.length - 1].children.unshift(l);
350
+ }
351
+ }
355
352
  }
353
+ exports.readLayerInfo = readLayerInfo;
356
354
  function readLayerRecord(reader, psd, options) {
357
355
  var layer = {};
358
356
  layer.top = readInt32(reader);
@@ -453,18 +451,21 @@ function readLayerBlendingRanges(reader) {
453
451
  });
454
452
  }
455
453
  function readLayerChannelImageData(reader, psd, layer, channels, options) {
454
+ var _a, _b, _c, _d;
456
455
  var layerWidth = (layer.right || 0) - (layer.left || 0);
457
456
  var layerHeight = (layer.bottom || 0) - (layer.top || 0);
458
457
  var cmyk = psd.colorMode === 4 /* ColorMode.CMYK */;
459
458
  var imageData;
460
459
  if (layerWidth && layerHeight) {
461
460
  if (cmyk) {
461
+ if (psd.bitsPerChannel !== 8)
462
+ throw new Error('bitsPerChannel Not supproted');
462
463
  imageData = { width: layerWidth, height: layerHeight, data: new Uint8ClampedArray(layerWidth * layerHeight * 5) };
463
464
  for (var p = 4; p < imageData.data.byteLength; p += 5)
464
465
  imageData.data[p] = 255;
465
466
  }
466
467
  else {
467
- imageData = (0, helpers_1.createImageData)(layerWidth, layerHeight);
468
+ imageData = createImageDataBitDepth(layerWidth, layerHeight, (_a = psd.bitsPerChannel) !== null && _a !== void 0 ? _a : 8);
468
469
  (0, helpers_1.resetImageData)(imageData);
469
470
  }
470
471
  }
@@ -497,10 +498,10 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
497
498
  var maskWidth = (mask.right || 0) - (mask.left || 0);
498
499
  var maskHeight = (mask.bottom || 0) - (mask.top || 0);
499
500
  if (maskWidth && maskHeight) {
500
- var maskData = (0, helpers_1.createImageData)(maskWidth, maskHeight);
501
+ var maskData = createImageDataBitDepth(maskWidth, maskHeight, (_b = psd.bitsPerChannel) !== null && _b !== void 0 ? _b : 8);
501
502
  (0, helpers_1.resetImageData)(maskData);
502
503
  var start_1 = reader.offset;
503
- readData(reader, channel.length, maskData, compression, maskWidth, maskHeight, 0, options.large, 4);
504
+ readData(reader, channel.length, maskData, compression, maskWidth, maskHeight, (_c = psd.bitsPerChannel) !== null && _c !== void 0 ? _c : 8, 0, options.large, 4);
504
505
  if (helpers_1.RAW_IMAGE_DATA) {
505
506
  layer.maskDataRaw = new Uint8Array(reader.view.buffer, reader.view.byteOffset + start_1, reader.offset - start_1);
506
507
  }
@@ -509,8 +510,7 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
509
510
  mask.imageData = maskData;
510
511
  }
511
512
  else {
512
- mask.canvas = (0, helpers_1.createCanvas)(maskWidth, maskHeight);
513
- mask.canvas.getContext('2d').putImageData(maskData, 0, 0);
513
+ mask.canvas = (0, helpers_1.imageDataToCanvas)(maskData);
514
514
  }
515
515
  }
516
516
  }
@@ -523,7 +523,7 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
523
523
  throw new Error("Channel not supported: ".concat(channel.id));
524
524
  }
525
525
  }
526
- readData(reader, channel.length, targetData, compression, layerWidth, layerHeight, offset, options.large, cmyk ? 5 : 4);
526
+ readData(reader, channel.length, targetData, compression, layerWidth, layerHeight, (_d = psd.bitsPerChannel) !== null && _d !== void 0 ? _d : 8, offset, options.large, cmyk ? 5 : 4);
527
527
  if (helpers_1.RAW_IMAGE_DATA) {
528
528
  layer.imageDataRaw[channel.id] = new Uint8Array(reader.view.buffer, reader.view.byteOffset + start + 2, channel.length - 2);
529
529
  }
@@ -543,23 +543,22 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
543
543
  layer.imageData = imageData;
544
544
  }
545
545
  else {
546
- layer.canvas = (0, helpers_1.createCanvas)(layerWidth, layerHeight);
547
- layer.canvas.getContext('2d').putImageData(imageData, 0, 0);
546
+ layer.canvas = (0, helpers_1.imageDataToCanvas)(imageData);
548
547
  }
549
548
  }
550
549
  }
551
- function readData(reader, length, data, compression, width, height, offset, large, step) {
550
+ function readData(reader, length, data, compression, width, height, bitDepth, offset, large, step) {
552
551
  if (compression === 0 /* Compression.RawData */) {
553
- readDataRaw(reader, data, width, height, step, offset);
552
+ readDataRaw(reader, data, width, height, bitDepth, step, offset);
554
553
  }
555
554
  else if (compression === 1 /* Compression.RleCompressed */) {
556
- readDataRLE(reader, data, width, height, step, [offset], large);
555
+ readDataRLE(reader, data, width, height, bitDepth, step, [offset], large);
557
556
  }
558
557
  else if (compression === 2 /* Compression.ZipWithoutPrediction */) {
559
- readDataZipWithoutPrediction(reader, length, data, width, height, step, offset);
558
+ readDataZip(reader, length, data, width, height, bitDepth, step, offset, false);
560
559
  }
561
560
  else if (compression === 3 /* Compression.ZipWithPrediction */) {
562
- throw new Error("Compression type not supported: ".concat(compression));
561
+ readDataZip(reader, length, data, width, height, bitDepth, step, offset, true);
563
562
  }
564
563
  else {
565
564
  throw new Error("Invalid Compression type: ".concat(compression));
@@ -580,6 +579,7 @@ function readGlobalLayerMaskInfo(reader) {
580
579
  return { overlayColorSpace: overlayColorSpace, colorSpace1: colorSpace1, colorSpace2: colorSpace2, colorSpace3: colorSpace3, colorSpace4: colorSpace4, opacity: opacity, kind: kind };
581
580
  });
582
581
  }
582
+ exports.readGlobalLayerMaskInfo = readGlobalLayerMaskInfo;
583
583
  function readAdditionalLayerInfo(reader, target, psd, options) {
584
584
  var sig = readSignature(reader);
585
585
  if (sig !== '8BIM' && sig !== '8B64')
@@ -608,13 +608,30 @@ function readAdditionalLayerInfo(reader, target, psd, options) {
608
608
  }
609
609
  }, false, u64);
610
610
  }
611
- function readImageData(reader, psd, globalAlpha, options) {
611
+ exports.readAdditionalLayerInfo = readAdditionalLayerInfo;
612
+ function createImageDataBitDepth(width, height, bitDepth) {
613
+ if (bitDepth === 1 || bitDepth === 8) {
614
+ return (0, helpers_1.createImageData)(width, height);
615
+ }
616
+ else if (bitDepth === 16) {
617
+ return { width: width, height: height, data: new Uint16Array(width * height * 4) };
618
+ }
619
+ else if (bitDepth === 32) {
620
+ return { width: width, height: height, data: new Uint32Array(width * height * 4) };
621
+ }
622
+ else {
623
+ throw new Error("Invalid bitDepth (".concat(bitDepth, ")"));
624
+ }
625
+ }
626
+ function readImageData(reader, psd, options) {
627
+ var _a, _b;
612
628
  var compression = readUint16(reader);
629
+ var bitsPerChannel = (_a = psd.bitsPerChannel) !== null && _a !== void 0 ? _a : 8;
613
630
  if (exports.supportedColorModes.indexOf(psd.colorMode) === -1)
614
631
  throw new Error("Color mode not supported: ".concat(psd.colorMode));
615
632
  if (compression !== 0 /* Compression.RawData */ && compression !== 1 /* Compression.RleCompressed */)
616
633
  throw new Error("Compression type not supported: ".concat(compression));
617
- var imageData = (0, helpers_1.createImageData)(psd.width, psd.height);
634
+ var imageData = createImageDataBitDepth(psd.width, psd.height, bitsPerChannel);
618
635
  (0, helpers_1.resetImageData)(imageData);
619
636
  switch (psd.colorMode) {
620
637
  case 0 /* ColorMode.Bitmap */: {
@@ -624,7 +641,7 @@ function readImageData(reader, psd, globalAlpha, options) {
624
641
  }
625
642
  else if (compression === 1 /* Compression.RleCompressed */) {
626
643
  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);
644
+ readDataRLE(reader, { data: bytes, width: psd.width, height: psd.height }, psd.width, psd.height, bitsPerChannel, 1, [0], options.large);
628
645
  }
629
646
  else {
630
647
  throw new Error("Bitmap compression not supported: ".concat(compression));
@@ -641,17 +658,17 @@ function readImageData(reader, psd, globalAlpha, options) {
641
658
  channels.push(i);
642
659
  }
643
660
  }
644
- else if (globalAlpha) {
661
+ else if (options.globalAlpha) {
645
662
  channels.push(3);
646
663
  }
647
664
  if (compression === 0 /* Compression.RawData */) {
648
665
  for (var i = 0; i < channels.length; i++) {
649
- readDataRaw(reader, imageData, psd.width, psd.height, 4, channels[i]);
666
+ readDataRaw(reader, imageData, psd.width, psd.height, bitsPerChannel, 4, channels[i]);
650
667
  }
651
668
  }
652
669
  else if (compression === 1 /* Compression.RleCompressed */) {
653
670
  var start = reader.offset;
654
- readDataRLE(reader, imageData, psd.width, psd.height, 4, channels, options.large);
671
+ readDataRLE(reader, imageData, psd.width, psd.height, bitsPerChannel, 4, channels, options.large);
655
672
  if (helpers_1.RAW_IMAGE_DATA)
656
673
  psd.imageDataRaw = new Uint8Array(reader.view.buffer, reader.view.byteOffset + start, reader.offset - start);
657
674
  }
@@ -661,10 +678,12 @@ function readImageData(reader, psd, globalAlpha, options) {
661
678
  break;
662
679
  }
663
680
  case 4 /* ColorMode.CMYK */: {
681
+ if (psd.bitsPerChannel !== 8)
682
+ throw new Error('bitsPerChannel Not supproted');
664
683
  if (psd.channels !== 4)
665
684
  throw new Error("Invalid channel count");
666
685
  var channels = [0, 1, 2, 3];
667
- if (globalAlpha)
686
+ if (options.globalAlpha)
668
687
  channels.push(4);
669
688
  if (compression === 0 /* Compression.RawData */) {
670
689
  throw new Error("Not implemented");
@@ -680,7 +699,7 @@ function readImageData(reader, psd, globalAlpha, options) {
680
699
  data: new Uint8Array(imageData.width * imageData.height * 5),
681
700
  };
682
701
  var start = reader.offset;
683
- readDataRLE(reader, cmykImageData, psd.width, psd.height, 5, channels, options.large);
702
+ readDataRLE(reader, cmykImageData, psd.width, psd.height, (_b = psd.bitsPerChannel) !== null && _b !== void 0 ? _b : 8, 5, channels, options.large);
684
703
  cmykToRgb(cmykImageData, imageData, true);
685
704
  if (helpers_1.RAW_IMAGE_DATA)
686
705
  psd.imageDataRaw = new Uint8Array(reader.view.buffer, reader.view.byteOffset + start, reader.offset - start);
@@ -690,7 +709,9 @@ function readImageData(reader, psd, globalAlpha, options) {
690
709
  default: throw new Error("Color mode not supported: ".concat(psd.colorMode));
691
710
  }
692
711
  // remove weird white matte
693
- if (globalAlpha) {
712
+ if (options.globalAlpha) {
713
+ if (psd.bitsPerChannel !== 8)
714
+ throw new Error('bitsPerChannel Not supproted');
694
715
  var p = imageData.data;
695
716
  var size = imageData.width * imageData.height * 4;
696
717
  for (var i = 0; i < size; i += 4) {
@@ -709,8 +730,7 @@ function readImageData(reader, psd, globalAlpha, options) {
709
730
  psd.imageData = imageData;
710
731
  }
711
732
  else {
712
- psd.canvas = (0, helpers_1.createCanvas)(psd.width, psd.height);
713
- psd.canvas.getContext('2d').putImageData(imageData, 0, 0);
733
+ psd.canvas = (0, helpers_1.imageDataToCanvas)(imageData);
714
734
  }
715
735
  }
716
736
  function cmykToRgb(cmyk, rgb, reverseAlpha) {
@@ -738,29 +758,89 @@ function cmykToRgb(cmyk, rgb, reverseAlpha) {
738
758
  // dstData[dst + 3] = reverseAlpha ? 255 - srcData[src + 4] : srcData[src + 4];
739
759
  // }
740
760
  }
741
- function readDataRaw(reader, pixelData, width, height, step, offset) {
742
- var size = width * height;
743
- var buffer = readBytes(reader, size);
761
+ function verifyCompatible(a, b) {
762
+ if ((a.byteLength / a.length) !== (b.byteLength / b.length)) {
763
+ throw new Error('Invalid array types');
764
+ }
765
+ }
766
+ function bytesToArray(bytes, bitDepth) {
767
+ if (bitDepth === 8) {
768
+ return bytes;
769
+ }
770
+ else if (bitDepth === 16) {
771
+ if (bytes.byteOffset % 2) {
772
+ var result = new Uint16Array(bytes.byteLength / 2);
773
+ new Uint8Array(result.buffer, result.byteOffset, result.byteLength).set(bytes);
774
+ return result;
775
+ }
776
+ else {
777
+ return new Uint16Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / 2);
778
+ }
779
+ }
780
+ else if (bitDepth === 32) {
781
+ if (bytes.byteOffset % 4) {
782
+ var result = new Uint32Array(bytes.byteLength / 4);
783
+ new Uint8Array(result.buffer, result.byteOffset, result.byteLength).set(bytes);
784
+ return result;
785
+ }
786
+ else {
787
+ return new Uint32Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / 4);
788
+ }
789
+ }
790
+ else {
791
+ throw new Error("Invalid bitDepth (".concat(bitDepth, ")"));
792
+ }
793
+ }
794
+ function copyChannelToPixelData(pixelData, channel, offset, step) {
795
+ verifyCompatible(pixelData.data, channel);
796
+ var size = pixelData.width * pixelData.height;
797
+ var data = pixelData.data;
798
+ for (var i = 0, p = offset | 0; i < size; i++, p = (p + step) | 0) {
799
+ data[p] = channel[i];
800
+ }
801
+ }
802
+ function readDataRaw(reader, pixelData, width, height, bitDepth, step, offset) {
803
+ var buffer = readBytes(reader, width * height * Math.floor(bitDepth / 8));
804
+ var array = bytesToArray(buffer, bitDepth);
744
805
  if (pixelData && offset < step) {
745
- var data = pixelData.data;
746
- for (var i = 0, p = offset | 0; i < size; i++, p = (p + step) | 0) {
747
- data[p] = buffer[i];
806
+ copyChannelToPixelData(pixelData, array, offset, step);
807
+ }
808
+ }
809
+ function decodePredicted(data, width, height, mod) {
810
+ for (var y = 0; y < height; y++) {
811
+ var offset = y * width;
812
+ for (var x = 1, o = offset + 1; x < width; x++, o++) {
813
+ data[o] = (data[o - 1] + data[o]) % mod;
748
814
  }
749
815
  }
750
816
  }
751
- function readDataZipWithoutPrediction(reader, length, pixelData, width, height, step, offset) {
817
+ function readDataZip(reader, length, pixelData, width, height, bitDepth, step, offset, prediction) {
752
818
  var compressed = readBytes(reader, length);
753
819
  var decompressed = (0, pako_1.inflate)(compressed);
754
- var size = width * height;
755
820
  if (pixelData && offset < step) {
756
- var data = pixelData.data;
757
- for (var i = 0, p = offset | 0; i < size; i++, p = (p + step) | 0) {
758
- data[p] = decompressed[i];
821
+ var array = bytesToArray(decompressed, bitDepth);
822
+ if (bitDepth === 8) {
823
+ if (prediction)
824
+ decodePredicted(decompressed, width, height, 0x100);
825
+ copyChannelToPixelData(pixelData, decompressed, offset, step);
826
+ }
827
+ else if (bitDepth === 16) {
828
+ if (prediction)
829
+ decodePredicted(array, width, height, 0x10000);
830
+ copyChannelToPixelData(pixelData, array, offset, step);
831
+ }
832
+ else if (bitDepth === 32) {
833
+ if (prediction)
834
+ decodePredicted(decompressed, width, height, 0x100);
835
+ copyChannelToPixelData(pixelData, array, offset, step);
836
+ }
837
+ else {
838
+ throw new Error('Invalid bitDepth');
759
839
  }
760
840
  }
761
841
  }
762
- exports.readDataZipWithoutPrediction = readDataZipWithoutPrediction;
763
- function readDataRLE(reader, pixelData, _width, height, step, offsets, large) {
842
+ exports.readDataZip = readDataZip;
843
+ function readDataRLE(reader, pixelData, _width, height, bitDepth, step, offsets, large) {
764
844
  var data = pixelData && pixelData.data;
765
845
  var lengths;
766
846
  if (large) {
@@ -779,6 +859,8 @@ function readDataRLE(reader, pixelData, _width, height, step, offsets, large) {
779
859
  }
780
860
  }
781
861
  }
862
+ if (bitDepth !== 1 && bitDepth !== 8)
863
+ throw new Error("Invalid bit depth (".concat(bitDepth, ")"));
782
864
  var extraLimit = (step - 1) | 0; // 3 for rgb, 4 for cmyk
783
865
  for (var c = 0, li = 0; c < offsets.length; c++) {
784
866
  var offset = offsets[c] | 0;