ag-psd 14.3.2 → 14.3.3

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/dist/bundle.js CHANGED
@@ -42,7 +42,7 @@ function readAbr(buffer, options) {
42
42
  if (version === 1 || version === 2) {
43
43
  throw new Error("Unsupported ABR version (" + version + ")"); // TODO: ...
44
44
  }
45
- else if (version === 6 || version === 7 || version === 10) {
45
+ else if (version === 6 || version === 7 || version === 9 || version === 10) {
46
46
  var minorVersion = psdReader_1.readInt16(reader);
47
47
  if (minorVersion !== 1 && minorVersion !== 2)
48
48
  throw new Error('Unsupported ABR minor version');
@@ -254,7 +254,7 @@ function readAbr(buffer, options) {
254
254
  }
255
255
  }
256
256
  else {
257
- throw new Error('Unsupported ABR version');
257
+ throw new Error("Unsupported ABR version (" + version + ")");
258
258
  }
259
259
  return { samples: samples, patterns: patterns, brushes: brushes };
260
260
  }
@@ -3584,6 +3584,10 @@ exports.BlnM = helpers_1.createEnum('BlnM', 'normal', {
3584
3584
  'saturation': 'Strt',
3585
3585
  'color': 'Clr ',
3586
3586
  'luminosity': 'Lmns',
3587
+ // used in ABR
3588
+ 'linear height': 'linearHeight',
3589
+ 'height': 'Hght',
3590
+ 'subtraction': 'Sbtr', // 2nd version of subtract ?
3587
3591
  });
3588
3592
  exports.BESl = helpers_1.createEnum('BESl', 'inner bevel', {
3589
3593
  'inner bevel': 'InrB',
@@ -4373,9 +4377,10 @@ var MaskParams;
4373
4377
  })(MaskParams = exports.MaskParams || (exports.MaskParams = {}));
4374
4378
  var ChannelID;
4375
4379
  (function (ChannelID) {
4376
- ChannelID[ChannelID["Red"] = 0] = "Red";
4377
- ChannelID[ChannelID["Green"] = 1] = "Green";
4378
- ChannelID[ChannelID["Blue"] = 2] = "Blue";
4380
+ ChannelID[ChannelID["Color0"] = 0] = "Color0";
4381
+ ChannelID[ChannelID["Color1"] = 1] = "Color1";
4382
+ ChannelID[ChannelID["Color2"] = 2] = "Color2";
4383
+ ChannelID[ChannelID["Color3"] = 3] = "Color3";
4379
4384
  ChannelID[ChannelID["Transparency"] = -1] = "Transparency";
4380
4385
  ChannelID[ChannelID["UserMask"] = -2] = "UserMask";
4381
4386
  ChannelID[ChannelID["RealUserMask"] = -3] = "RealUserMask";
@@ -4387,12 +4392,13 @@ var Compression;
4387
4392
  Compression[Compression["ZipWithoutPrediction"] = 2] = "ZipWithoutPrediction";
4388
4393
  Compression[Compression["ZipWithPrediction"] = 3] = "ZipWithPrediction";
4389
4394
  })(Compression = exports.Compression || (exports.Compression = {}));
4390
- function offsetForChannel(channelId) {
4395
+ function offsetForChannel(channelId, cmyk) {
4391
4396
  switch (channelId) {
4392
- case 0 /* Red */: return 0;
4393
- case 1 /* Green */: return 1;
4394
- case 2 /* Blue */: return 2;
4395
- case -1 /* Transparency */: return 3;
4397
+ case 0 /* Color0 */: return 0;
4398
+ case 1 /* Color1 */: return 1;
4399
+ case 2 /* Color2 */: return 2;
4400
+ case 3 /* Color3 */: return cmyk ? 3 : channelId + 1;
4401
+ case -1 /* Transparency */: return cmyk ? 4 : 3;
4396
4402
  default: return channelId + 1;
4397
4403
  }
4398
4404
  }
@@ -4412,9 +4418,9 @@ function hasAlpha(data) {
4412
4418
  }
4413
4419
  exports.hasAlpha = hasAlpha;
4414
4420
  function resetImageData(_a) {
4415
- var width = _a.width, height = _a.height, data = _a.data;
4416
- var size = (width * height) | 0;
4421
+ var data = _a.data;
4417
4422
  var buffer = new Uint32Array(data.buffer);
4423
+ var size = buffer.length | 0;
4418
4424
  for (var p = 0; p < size; p = (p + 1) | 0) {
4419
4425
  buffer[p] = 0xff000000;
4420
4426
  }
@@ -4992,6 +4998,8 @@ addHandler(1036, function (target) { return target.thumbnail !== undefined || ta
4992
4998
  data = target.thumbnailRaw.data;
4993
4999
  }
4994
5000
  else {
5001
+ if (!target.thumbnail)
5002
+ throw new Error('Missing thumbnail');
4995
5003
  width = target.thumbnail.width;
4996
5004
  height = target.thumbnail.height;
4997
5005
  data = base64_js_1.toByteArray(target.thumbnail.toDataURL('image/jpeg', 1).substr('data:image/jpeg;base64,'.length));
@@ -5483,6 +5491,9 @@ function readPsd(reader, options) {
5483
5491
  if (!skipComposite) {
5484
5492
  readImageData(reader, psd, globalAlpha, opt);
5485
5493
  }
5494
+ // TODO: show converted color mode instead of original PSD file color mode
5495
+ // but add option to preserve file color mode (need to return image data instead of canvas in that case)
5496
+ // psd.colorMode = ColorMode.RGB; // we convert all color modes to RGB
5486
5497
  return psd;
5487
5498
  }
5488
5499
  exports.readPsd = readPsd;
@@ -5632,14 +5643,21 @@ function readLayerBlendingRanges(reader) {
5632
5643
  function readLayerChannelImageData(reader, psd, layer, channels, options) {
5633
5644
  var layerWidth = (layer.right || 0) - (layer.left || 0);
5634
5645
  var layerHeight = (layer.bottom || 0) - (layer.top || 0);
5646
+ var cmyk = psd.colorMode === 4 /* CMYK */;
5635
5647
  var imageData;
5636
5648
  if (layerWidth && layerHeight) {
5637
- imageData = helpers_1.createImageData(layerWidth, layerHeight);
5638
- helpers_1.resetImageData(imageData);
5649
+ if (cmyk) {
5650
+ imageData = { width: layerWidth, height: layerHeight, data: new Uint8ClampedArray(layerWidth * layerHeight * 5) };
5651
+ for (var p = 4; p < imageData.data.byteLength; p += 5)
5652
+ imageData.data[p] = 255;
5653
+ }
5654
+ else {
5655
+ imageData = helpers_1.createImageData(layerWidth, layerHeight);
5656
+ helpers_1.resetImageData(imageData);
5657
+ }
5639
5658
  }
5640
- if (helpers_1.RAW_IMAGE_DATA) {
5659
+ if (helpers_1.RAW_IMAGE_DATA)
5641
5660
  layer.imageDataRaw = [];
5642
- }
5643
5661
  for (var _i = 0, channels_1 = channels; _i < channels_1.length; _i++) {
5644
5662
  var channel = channels_1[_i];
5645
5663
  var compression = readUint16(reader);
@@ -5653,7 +5671,7 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
5653
5671
  var maskData = helpers_1.createImageData(maskWidth, maskHeight);
5654
5672
  helpers_1.resetImageData(maskData);
5655
5673
  var start = reader.offset;
5656
- readData(reader, maskData, compression, maskWidth, maskHeight, 0, options.large);
5674
+ readData(reader, maskData, compression, maskWidth, maskHeight, 0, options.large, 4);
5657
5675
  if (helpers_1.RAW_IMAGE_DATA) {
5658
5676
  layer.maskDataRaw = new Uint8Array(reader.view.buffer, reader.view.byteOffset + start, reader.offset - start);
5659
5677
  }
@@ -5668,7 +5686,7 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
5668
5686
  }
5669
5687
  }
5670
5688
  else {
5671
- var offset = helpers_1.offsetForChannel(channel.id);
5689
+ var offset = helpers_1.offsetForChannel(channel.id, cmyk);
5672
5690
  var targetData = imageData;
5673
5691
  if (offset < 0) {
5674
5692
  targetData = undefined;
@@ -5677,7 +5695,7 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
5677
5695
  }
5678
5696
  }
5679
5697
  var start = reader.offset;
5680
- readData(reader, targetData, compression, layerWidth, layerHeight, offset, options.large);
5698
+ readData(reader, targetData, compression, layerWidth, layerHeight, offset, options.large, cmyk ? 5 : 4);
5681
5699
  if (helpers_1.RAW_IMAGE_DATA) {
5682
5700
  layer.imageDataRaw[channel.id] = new Uint8Array(reader.view.buffer, reader.view.byteOffset + start, reader.offset - start);
5683
5701
  }
@@ -5687,6 +5705,11 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
5687
5705
  }
5688
5706
  }
5689
5707
  if (imageData) {
5708
+ if (cmyk) {
5709
+ var cmykData = imageData;
5710
+ imageData = helpers_1.createImageData(cmykData.width, cmykData.height);
5711
+ cmykToRgb(cmykData, imageData, false);
5712
+ }
5690
5713
  if (options.useImageData) {
5691
5714
  layer.imageData = imageData;
5692
5715
  }
@@ -5696,12 +5719,12 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
5696
5719
  }
5697
5720
  }
5698
5721
  }
5699
- function readData(reader, data, compression, width, height, offset, large) {
5722
+ function readData(reader, data, compression, width, height, offset, large, step) {
5700
5723
  if (compression === 0 /* RawData */) {
5701
- readDataRaw(reader, data, offset, width, height);
5724
+ readDataRaw(reader, data, offset, width, height, step);
5702
5725
  }
5703
5726
  else if (compression === 1 /* RleCompressed */) {
5704
- readDataRLE(reader, data, width, height, 4, [offset], large);
5727
+ readDataRLE(reader, data, width, height, step, [offset], large);
5705
5728
  }
5706
5729
  else {
5707
5730
  throw new Error("Compression type not supported: " + compression);
@@ -5758,46 +5781,78 @@ function readImageData(reader, psd, globalAlpha, options) {
5758
5781
  throw new Error("Compression type not supported: " + compression);
5759
5782
  var imageData = helpers_1.createImageData(psd.width, psd.height);
5760
5783
  helpers_1.resetImageData(imageData);
5761
- if (psd.colorMode === 0 /* Bitmap */) {
5762
- var bytes = void 0;
5763
- if (compression === 0 /* RawData */) {
5764
- bytes = readBytes(reader, Math.ceil(psd.width / 8) * psd.height);
5765
- }
5766
- else if (compression === 1 /* RleCompressed */) {
5767
- bytes = new Uint8Array(psd.width * psd.height);
5768
- readDataRLE(reader, { data: bytes, width: psd.width, height: psd.height }, psd.width, psd.height, 1, [0], options.large);
5769
- }
5770
- else {
5771
- throw new Error("Bitmap compression not supported: " + compression);
5772
- }
5773
- helpers_1.decodeBitmap(bytes, imageData.data, psd.width, psd.height);
5774
- }
5775
- else {
5776
- var channels = psd.colorMode === 1 /* Grayscale */ ? [0] : [0, 1, 2];
5777
- if (psd.channels && psd.channels > 3) {
5778
- for (var i = 3; i < psd.channels; i++) {
5779
- // TODO: store these channels in additional image data
5780
- channels.push(i);
5784
+ switch (psd.colorMode) {
5785
+ case 0 /* Bitmap */: {
5786
+ var bytes = void 0;
5787
+ if (compression === 0 /* RawData */) {
5788
+ bytes = readBytes(reader, Math.ceil(psd.width / 8) * psd.height);
5781
5789
  }
5782
- }
5783
- else if (globalAlpha) {
5784
- channels.push(3);
5785
- }
5786
- if (compression === 0 /* RawData */) {
5787
- for (var i = 0; i < channels.length; i++) {
5788
- readDataRaw(reader, imageData, channels[i], psd.width, psd.height);
5790
+ else if (compression === 1 /* RleCompressed */) {
5791
+ bytes = new Uint8Array(psd.width * psd.height);
5792
+ readDataRLE(reader, { data: bytes, width: psd.width, height: psd.height }, psd.width, psd.height, 1, [0], options.large);
5793
+ }
5794
+ else {
5795
+ throw new Error("Bitmap compression not supported: " + compression);
5789
5796
  }
5797
+ helpers_1.decodeBitmap(bytes, imageData.data, psd.width, psd.height);
5798
+ break;
5790
5799
  }
5791
- else if (compression === 1 /* RleCompressed */) {
5792
- var start = reader.offset;
5793
- readDataRLE(reader, imageData, psd.width, psd.height, 4, channels, options.large);
5794
- if (helpers_1.RAW_IMAGE_DATA) {
5795
- psd.imageDataRaw = new Uint8Array(reader.view.buffer, reader.view.byteOffset + start, reader.offset - start);
5800
+ case 3 /* RGB */:
5801
+ case 1 /* Grayscale */: {
5802
+ var channels = psd.colorMode === 1 /* Grayscale */ ? [0] : [0, 1, 2];
5803
+ if (psd.channels && psd.channels > 3) {
5804
+ for (var i = 3; i < psd.channels; i++) {
5805
+ // TODO: store these channels in additional image data
5806
+ channels.push(i);
5807
+ }
5808
+ }
5809
+ else if (globalAlpha) {
5810
+ channels.push(3);
5796
5811
  }
5812
+ if (compression === 0 /* RawData */) {
5813
+ for (var i = 0; i < channels.length; i++) {
5814
+ readDataRaw(reader, imageData, channels[i], psd.width, psd.height, 4);
5815
+ }
5816
+ }
5817
+ else if (compression === 1 /* RleCompressed */) {
5818
+ var start = reader.offset;
5819
+ readDataRLE(reader, imageData, psd.width, psd.height, 4, channels, options.large);
5820
+ if (helpers_1.RAW_IMAGE_DATA)
5821
+ psd.imageDataRaw = new Uint8Array(reader.view.buffer, reader.view.byteOffset + start, reader.offset - start);
5822
+ }
5823
+ if (psd.colorMode === 1 /* Grayscale */) {
5824
+ setupGrayscale(imageData);
5825
+ }
5826
+ break;
5797
5827
  }
5798
- if (psd.colorMode === 1 /* Grayscale */) {
5799
- setupGrayscale(imageData);
5828
+ case 4 /* CMYK */: {
5829
+ if (psd.channels !== 4)
5830
+ throw new Error("Invalid channel count");
5831
+ var channels = [0, 1, 2, 3];
5832
+ if (globalAlpha)
5833
+ channels.push(4);
5834
+ if (compression === 0 /* RawData */) {
5835
+ throw new Error("Not implemented");
5836
+ // TODO: ...
5837
+ // for (let i = 0; i < channels.length; i++) {
5838
+ // readDataRaw(reader, imageData, channels[i], psd.width, psd.height);
5839
+ // }
5840
+ }
5841
+ else if (compression === 1 /* RleCompressed */) {
5842
+ var cmykImageData = {
5843
+ width: imageData.width,
5844
+ height: imageData.height,
5845
+ data: new Uint8Array(imageData.width * imageData.height * 5),
5846
+ };
5847
+ var start = reader.offset;
5848
+ readDataRLE(reader, cmykImageData, psd.width, psd.height, 5, channels, options.large);
5849
+ cmykToRgb(cmykImageData, imageData, true);
5850
+ if (helpers_1.RAW_IMAGE_DATA)
5851
+ psd.imageDataRaw = new Uint8Array(reader.view.buffer, reader.view.byteOffset + start, reader.offset - start);
5852
+ }
5853
+ break;
5800
5854
  }
5855
+ default: throw new Error("Color mode not supported: " + psd.colorMode);
5801
5856
  }
5802
5857
  if (options.useImageData) {
5803
5858
  psd.imageData = imageData;
@@ -5807,12 +5862,37 @@ function readImageData(reader, psd, globalAlpha, options) {
5807
5862
  psd.canvas.getContext('2d').putImageData(imageData, 0, 0);
5808
5863
  }
5809
5864
  }
5810
- function readDataRaw(reader, pixelData, offset, width, height) {
5865
+ function cmykToRgb(cmyk, rgb, reverseAlpha) {
5866
+ var size = rgb.width * rgb.height * 4;
5867
+ var srcData = cmyk.data;
5868
+ var dstData = rgb.data;
5869
+ for (var src = 0, dst = 0; dst < size; src += 5, dst += 4) {
5870
+ var c = srcData[src];
5871
+ var m = srcData[src + 1];
5872
+ var y = srcData[src + 2];
5873
+ var k = srcData[src + 3];
5874
+ dstData[dst] = ((((c * k) | 0) / 255) | 0);
5875
+ dstData[dst + 1] = ((((m * k) | 0) / 255) | 0);
5876
+ dstData[dst + 2] = ((((y * k) | 0) / 255) | 0);
5877
+ dstData[dst + 3] = reverseAlpha ? 255 - srcData[src + 4] : srcData[src + 4];
5878
+ }
5879
+ // for (let src = 0, dst = 0; dst < size; src += 5, dst += 4) {
5880
+ // const c = 1 - (srcData[src + 0] / 255);
5881
+ // const m = 1 - (srcData[src + 1] / 255);
5882
+ // const y = 1 - (srcData[src + 2] / 255);
5883
+ // // const k = srcData[src + 3] / 255;
5884
+ // dstData[dst + 0] = ((1 - c * 0.8) * 255) | 0;
5885
+ // dstData[dst + 1] = ((1 - m * 0.8) * 255) | 0;
5886
+ // dstData[dst + 2] = ((1 - y * 0.8) * 255) | 0;
5887
+ // dstData[dst + 3] = reverseAlpha ? 255 - srcData[src + 4] : srcData[src + 4];
5888
+ // }
5889
+ }
5890
+ function readDataRaw(reader, pixelData, offset, width, height, step) {
5811
5891
  var size = width * height;
5812
5892
  var buffer = readBytes(reader, size);
5813
- if (pixelData && offset < 4) {
5893
+ if (pixelData && offset < step) {
5814
5894
  var data = pixelData.data;
5815
- for (var i = 0, p = offset | 0; i < size; i++, p = (p + 4) | 0) {
5895
+ for (var i = 0, p = offset | 0; i < size; i++, p = (p + step) | 0) {
5816
5896
  data[p] = buffer[i];
5817
5897
  }
5818
5898
  }
@@ -5836,9 +5916,10 @@ function readDataRLE(reader, pixelData, _width, height, step, offsets, large) {
5836
5916
  }
5837
5917
  }
5838
5918
  }
5919
+ var extraLimit = (step - 1) | 0; // 3 for rgb, 4 for cmyk
5839
5920
  for (var c = 0, li = 0; c < offsets.length; c++) {
5840
5921
  var offset = offsets[c] | 0;
5841
- var extra = c > 3 || offset > 3;
5922
+ var extra = c > extraLimit || offset > extraLimit;
5842
5923
  if (!data || extra) {
5843
5924
  for (var y = 0; y < height; y++, li++) {
5844
5925
  skipBytes(reader, lengths[li]);
@@ -5951,11 +6032,22 @@ function readPattern(reader) {
5951
6032
  var x = readInt16(reader);
5952
6033
  var y = readInt16(reader);
5953
6034
  // we only support RGB and grayscale for now
5954
- if (colorMode !== 3 /* RGB */ && colorMode !== 1 /* Grayscale */)
5955
- throw new Error('Unsupported pattern color mode');
6035
+ if (colorMode !== 3 /* RGB */ && colorMode !== 1 /* Grayscale */ && colorMode !== 2 /* Indexed */) {
6036
+ throw new Error("Unsupported pattern color mode: " + colorMode);
6037
+ }
5956
6038
  var name = readUnicodeString(reader);
5957
6039
  var id = readPascalString(reader, 1);
5958
- // TODO: index color table here (only for indexed color mode, not supported right now)
6040
+ var palette = [];
6041
+ if (colorMode === 2 /* Indexed */) {
6042
+ for (var i = 0; i < 256; i++) {
6043
+ palette.push({
6044
+ r: readUint8(reader),
6045
+ g: readUint8(reader),
6046
+ b: readUint8(reader),
6047
+ });
6048
+ }
6049
+ skipBytes(reader, 4); // no idea what this is
6050
+ }
5959
6051
  // virtual memory array list
5960
6052
  var version2 = readUint32(reader);
5961
6053
  if (version2 !== 3)
@@ -5974,60 +6066,67 @@ function readPattern(reader) {
5974
6066
  }
5975
6067
  for (var i = 0, ch = 0; i < (channelsCount + 2); i++) {
5976
6068
  var has = readUint32(reader);
5977
- if (has) {
5978
- var length_2 = readUint32(reader);
5979
- var pixelDepth = readUint32(reader);
5980
- var ctop = readUint32(reader);
5981
- var cleft = readUint32(reader);
5982
- var cbottom = readUint32(reader);
5983
- var cright = readUint32(reader);
5984
- var pixelDepth2 = readUint16(reader);
5985
- var compressionMode = readUint8(reader); // 0 - raw, 1 - zip
5986
- var dataLength = length_2 - (4 + 16 + 2 + 1);
5987
- var cdata = readBytes(reader, dataLength);
5988
- if (pixelDepth !== 8 || pixelDepth2 !== 8)
5989
- throw new Error('16bit pixel depth not supported for palettes');
5990
- var w = cright - cleft;
5991
- var h = cbottom - ctop;
5992
- var ox = cleft - left;
5993
- var oy = ctop - top;
5994
- if (compressionMode === 0) {
5995
- if (colorMode === 3 /* RGB */ && ch < 3) {
5996
- for (var y_1 = 0; y_1 < h; y_1++) {
5997
- for (var x_1 = 0; x_1 < w; x_1++) {
5998
- var src = x_1 + y_1 * w;
5999
- var dst = (ox + x_1 + (y_1 + oy) * width) * 4;
6000
- data[dst + ch] = cdata[src];
6001
- }
6069
+ if (!has)
6070
+ continue;
6071
+ var length_2 = readUint32(reader);
6072
+ var pixelDepth = readUint32(reader);
6073
+ var ctop = readUint32(reader);
6074
+ var cleft = readUint32(reader);
6075
+ var cbottom = readUint32(reader);
6076
+ var cright = readUint32(reader);
6077
+ var pixelDepth2 = readUint16(reader);
6078
+ var compressionMode = readUint8(reader); // 0 - raw, 1 - zip
6079
+ var dataLength = length_2 - (4 + 16 + 2 + 1);
6080
+ var cdata = readBytes(reader, dataLength);
6081
+ if (pixelDepth !== 8 || pixelDepth2 !== 8) {
6082
+ throw new Error('16bit pixel depth not supported for patterns');
6083
+ }
6084
+ var w = cright - cleft;
6085
+ var h = cbottom - ctop;
6086
+ var ox = cleft - left;
6087
+ var oy = ctop - top;
6088
+ if (compressionMode === 0) {
6089
+ if (colorMode === 3 /* RGB */ && ch < 3) {
6090
+ for (var y_1 = 0; y_1 < h; y_1++) {
6091
+ for (var x_1 = 0; x_1 < w; x_1++) {
6092
+ var src = x_1 + y_1 * w;
6093
+ var dst = (ox + x_1 + (y_1 + oy) * width) * 4;
6094
+ data[dst + ch] = cdata[src];
6002
6095
  }
6003
6096
  }
6004
- if (colorMode === 1 /* Grayscale */ && ch < 1) {
6005
- for (var y_2 = 0; y_2 < h; y_2++) {
6006
- for (var x_2 = 0; x_2 < w; x_2++) {
6007
- var src = x_2 + y_2 * w;
6008
- var dst = (ox + x_2 + (y_2 + oy) * width) * 4;
6009
- var value = cdata[src];
6010
- data[dst + 0] = value;
6011
- data[dst + 1] = value;
6012
- data[dst + 2] = value;
6013
- }
6097
+ }
6098
+ if (colorMode === 1 /* Grayscale */ && ch < 1) {
6099
+ for (var y_2 = 0; y_2 < h; y_2++) {
6100
+ for (var x_2 = 0; x_2 < w; x_2++) {
6101
+ var src = x_2 + y_2 * w;
6102
+ var dst = (ox + x_2 + (y_2 + oy) * width) * 4;
6103
+ var value = cdata[src];
6104
+ data[dst + 0] = value;
6105
+ data[dst + 1] = value;
6106
+ data[dst + 2] = value;
6014
6107
  }
6015
6108
  }
6016
6109
  }
6017
- else if (compressionMode === 1) {
6018
- // console.log({ colorMode });
6019
- // require('fs').writeFileSync('zip.bin', Buffer.from(cdata));
6020
- // const data = require('zlib').inflateRawSync(cdata);
6021
- // const data = require('zlib').unzipSync(cdata);
6022
- // console.log(data);
6023
- // throw new Error('Zip compression not supported for palettes');
6024
- throw new Error('Unsupported palette compression mode');
6110
+ if (colorMode === 2 /* Indexed */) {
6111
+ // TODO:
6112
+ throw new Error('Indexed pattern color mode not implemented');
6025
6113
  }
6026
- else {
6027
- throw new Error('Invalid palette compression mode');
6028
- }
6029
- ch++;
6030
6114
  }
6115
+ else if (compressionMode === 1) {
6116
+ // console.log({ colorMode });
6117
+ // require('fs').writeFileSync('zip.bin', Buffer.from(cdata));
6118
+ // const data = require('zlib').inflateRawSync(cdata);
6119
+ // const data = require('zlib').unzipSync(cdata);
6120
+ // console.log(data);
6121
+ // throw new Error('Zip compression not supported for pattern');
6122
+ // throw new Error('Unsupported pattern compression');
6123
+ console.error('Unsupported pattern compression');
6124
+ name += ' (failed to decode)';
6125
+ }
6126
+ else {
6127
+ throw new Error('Invalid pattern compression mode');
6128
+ }
6129
+ ch++;
6031
6130
  }
6032
6131
  // TODO: use canvas instead of data ?
6033
6132
  return { id: id, name: name, x: x, y: y, bounds: { x: left, y: top, w: width, h: height }, data: data };
@@ -6227,7 +6326,7 @@ function writePsd(writer, psd, options) {
6227
6326
  writeUint32(writer, psd.height);
6228
6327
  writeUint32(writer, psd.width);
6229
6328
  writeUint16(writer, 8); // bits per channel
6230
- writeUint16(writer, 3 /* RGB */);
6329
+ writeUint16(writer, 3 /* RGB */); // we only support saving RGB right now
6231
6330
  // color mode data
6232
6331
  writeSection(writer, 1, function () {
6233
6332
  // TODO: implement
@@ -6575,9 +6674,9 @@ function getLayerChannels(tempBuffer, layer, background, options) {
6575
6674
  var _b = layer.top, top = _b === void 0 ? 0 : _b, _c = layer.left, left = _c === void 0 ? 0 : _c, _d = layer.right, right = _d === void 0 ? 0 : _d, _e = layer.bottom, bottom = _e === void 0 ? 0 : _e;
6576
6675
  var channels = [
6577
6676
  { channelId: -1 /* Transparency */, compression: 0 /* RawData */, buffer: undefined, length: 2 },
6578
- { channelId: 0 /* Red */, compression: 0 /* RawData */, buffer: undefined, length: 2 },
6579
- { channelId: 1 /* Green */, compression: 0 /* RawData */, buffer: undefined, length: 2 },
6580
- { channelId: 2 /* Blue */, compression: 0 /* RawData */, buffer: undefined, length: 2 },
6677
+ { channelId: 0 /* Color0 */, compression: 0 /* RawData */, buffer: undefined, length: 2 },
6678
+ { channelId: 1 /* Color1 */, compression: 0 /* RawData */, buffer: undefined, length: 2 },
6679
+ { channelId: 2 /* Color2 */, compression: 0 /* RawData */, buffer: undefined, length: 2 },
6581
6680
  ];
6582
6681
  var _f = getLayerDimentions(layer), width = _f.width, height = _f.height;
6583
6682
  if (!(layer.canvas || layer.imageData) || !width || !height) {
@@ -6609,15 +6708,15 @@ function getLayerChannels(tempBuffer, layer, background, options) {
6609
6708
  }
6610
6709
  }
6611
6710
  var channelIds = [
6612
- 0 /* Red */,
6613
- 1 /* Green */,
6614
- 2 /* Blue */,
6711
+ 0 /* Color0 */,
6712
+ 1 /* Color1 */,
6713
+ 2 /* Color2 */,
6615
6714
  ];
6616
6715
  if (!background || options.noBackground || layer.mask || helpers_1.hasAlpha(data) || (helpers_1.RAW_IMAGE_DATA && ((_a = layer.imageDataRaw) === null || _a === void 0 ? void 0 : _a['-1']))) {
6617
6716
  channelIds.unshift(-1 /* Transparency */);
6618
6717
  }
6619
6718
  channels = channelIds.map(function (channel) {
6620
- var offset = helpers_1.offsetForChannel(channel);
6719
+ var offset = helpers_1.offsetForChannel(channel, false); // TODO: psd.colorMode === ColorMode.CMYK);
6621
6720
  var buffer = helpers_1.writeDataRLE(tempBuffer, data, width, height, [offset], !!options.psb);
6622
6721
  if (helpers_1.RAW_IMAGE_DATA && layer.imageDataRaw) {
6623
6722
  // console.log('written raw layer image data');