ag-psd 14.3.1 → 14.3.5
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 +3 -0
- package/README.md +2 -1
- package/dist/abr.js +4 -4
- package/dist/additionalInfo.js +6 -3
- package/dist/bundle.js +230 -125
- package/dist/descriptor.js +5 -1
- package/dist/helpers.d.ts +6 -5
- package/dist/helpers.js +13 -11
- package/dist/imageResources.js +3 -1
- package/dist/psd.d.ts +1 -0
- package/dist/psd.js +1 -1
- package/dist/psdReader.d.ts +1 -0
- package/dist/psdReader.js +199 -105
- package/dist/psdWriter.js +9 -9
- package/dist-es/abr.js +4 -4
- package/dist-es/additionalInfo.js +6 -3
- package/dist-es/descriptor.js +5 -1
- package/dist-es/helpers.d.ts +6 -5
- package/dist-es/helpers.js +13 -11
- package/dist-es/imageResources.js +3 -1
- package/dist-es/psd.d.ts +1 -0
- package/dist-es/psd.js +1 -1
- package/dist-es/psdReader.d.ts +1 -0
- package/dist-es/psdReader.js +199 -105
- package/dist-es/psdWriter.js +9 -9
- package/package.json +1 -1
package/dist/bundle.js
CHANGED
|
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
exports.readAbr = void 0;
|
|
5
5
|
var descriptor_1 = require("./descriptor");
|
|
6
6
|
var psdReader_1 = require("./psdReader");
|
|
7
|
-
var dynamicsControl = ['off', 'fade', 'pen pressure', 'pen tilt', 'stylus
|
|
7
|
+
var dynamicsControl = ['off', 'fade', 'pen pressure', 'pen tilt', 'stylus wheel', 'initial direction', 'direction', 'initial rotation', 'rotation'];
|
|
8
8
|
function parseDynamics(desc) {
|
|
9
9
|
return {
|
|
10
10
|
control: dynamicsControl[desc.bVTy],
|
|
@@ -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(
|
|
257
|
+
throw new Error("Unsupported ABR version (" + version + ")");
|
|
258
258
|
}
|
|
259
259
|
return { samples: samples, patterns: patterns, brushes: brushes };
|
|
260
260
|
}
|
|
@@ -1041,6 +1041,9 @@ addHandler('SoLd', hasKey('placedLayer'), function (reader, target, left) {
|
|
|
1041
1041
|
resolution: descriptor_1.parseUnits(desc.Rslt),
|
|
1042
1042
|
warp: parseWarp((desc.quiltWarp || desc.warp)),
|
|
1043
1043
|
};
|
|
1044
|
+
if (desc.nonAffineTransform && desc.nonAffineTransform.some(function (x, i) { return x !== desc.Trnf[i]; })) {
|
|
1045
|
+
target.placedLayer.nonAffineTransform = desc.nonAffineTransform;
|
|
1046
|
+
}
|
|
1044
1047
|
if (desc.Crop)
|
|
1045
1048
|
target.placedLayer.crop = desc.Crop;
|
|
1046
1049
|
if (desc.comp)
|
|
@@ -1049,7 +1052,7 @@ addHandler('SoLd', hasKey('placedLayer'), function (reader, target, left) {
|
|
|
1049
1052
|
target.placedLayer.compInfo = desc.compInfo;
|
|
1050
1053
|
psdReader_1.skipBytes(reader, left()); // HACK
|
|
1051
1054
|
}, function (writer, target) {
|
|
1052
|
-
var _a;
|
|
1055
|
+
var _a, _b;
|
|
1053
1056
|
psdWriter_1.writeSignature(writer, 'soLD');
|
|
1054
1057
|
psdWriter_1.writeInt32(writer, 4); // version
|
|
1055
1058
|
var placed = target.placedLayer;
|
|
@@ -1059,7 +1062,7 @@ addHandler('SoLd', hasKey('placedLayer'), function (reader, target, left) {
|
|
|
1059
1062
|
}, duration: {
|
|
1060
1063
|
numerator: 0,
|
|
1061
1064
|
denominator: 600
|
|
1062
|
-
}, frameCount: 1, Annt: 16, Type: placedLayerTypes.indexOf(placed.type), Trnf: placed.transform, nonAffineTransform: placed.transform, quiltWarp: {}, warp: encodeWarp(placed.warp || {}), 'Sz ': {
|
|
1065
|
+
}, frameCount: 1, Annt: 16, Type: placedLayerTypes.indexOf(placed.type), Trnf: placed.transform, nonAffineTransform: (_b = placed.nonAffineTransform) !== null && _b !== void 0 ? _b : placed.transform, quiltWarp: {}, warp: encodeWarp(placed.warp || {}), 'Sz ': {
|
|
1063
1066
|
Wdth: placed.width || 0,
|
|
1064
1067
|
Hght: placed.height || 0, // TODO: find size ?
|
|
1065
1068
|
}, Rslt: placed.resolution ? descriptor_1.unitsValue(placed.resolution, 'resolution') : { units: 'Density', value: 72 } });
|
|
@@ -3581,6 +3584,10 @@ exports.BlnM = helpers_1.createEnum('BlnM', 'normal', {
|
|
|
3581
3584
|
'saturation': 'Strt',
|
|
3582
3585
|
'color': 'Clr ',
|
|
3583
3586
|
'luminosity': 'Lmns',
|
|
3587
|
+
// used in ABR
|
|
3588
|
+
'linear height': 'linearHeight',
|
|
3589
|
+
'height': 'Hght',
|
|
3590
|
+
'subtraction': 'Sbtr', // 2nd version of subtract ?
|
|
3584
3591
|
});
|
|
3585
3592
|
exports.BESl = helpers_1.createEnum('BESl', 'inner bevel', {
|
|
3586
3593
|
'inner bevel': 'InrB',
|
|
@@ -4370,9 +4377,10 @@ var MaskParams;
|
|
|
4370
4377
|
})(MaskParams = exports.MaskParams || (exports.MaskParams = {}));
|
|
4371
4378
|
var ChannelID;
|
|
4372
4379
|
(function (ChannelID) {
|
|
4373
|
-
ChannelID[ChannelID["
|
|
4374
|
-
ChannelID[ChannelID["
|
|
4375
|
-
ChannelID[ChannelID["
|
|
4380
|
+
ChannelID[ChannelID["Color0"] = 0] = "Color0";
|
|
4381
|
+
ChannelID[ChannelID["Color1"] = 1] = "Color1";
|
|
4382
|
+
ChannelID[ChannelID["Color2"] = 2] = "Color2";
|
|
4383
|
+
ChannelID[ChannelID["Color3"] = 3] = "Color3";
|
|
4376
4384
|
ChannelID[ChannelID["Transparency"] = -1] = "Transparency";
|
|
4377
4385
|
ChannelID[ChannelID["UserMask"] = -2] = "UserMask";
|
|
4378
4386
|
ChannelID[ChannelID["RealUserMask"] = -3] = "RealUserMask";
|
|
@@ -4384,12 +4392,13 @@ var Compression;
|
|
|
4384
4392
|
Compression[Compression["ZipWithoutPrediction"] = 2] = "ZipWithoutPrediction";
|
|
4385
4393
|
Compression[Compression["ZipWithPrediction"] = 3] = "ZipWithPrediction";
|
|
4386
4394
|
})(Compression = exports.Compression || (exports.Compression = {}));
|
|
4387
|
-
function offsetForChannel(channelId) {
|
|
4395
|
+
function offsetForChannel(channelId, cmyk) {
|
|
4388
4396
|
switch (channelId) {
|
|
4389
|
-
case 0 /*
|
|
4390
|
-
case 1 /*
|
|
4391
|
-
case 2 /*
|
|
4392
|
-
case
|
|
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;
|
|
4393
4402
|
default: return channelId + 1;
|
|
4394
4403
|
}
|
|
4395
4404
|
}
|
|
@@ -4409,9 +4418,9 @@ function hasAlpha(data) {
|
|
|
4409
4418
|
}
|
|
4410
4419
|
exports.hasAlpha = hasAlpha;
|
|
4411
4420
|
function resetImageData(_a) {
|
|
4412
|
-
var
|
|
4413
|
-
var size = (width * height) | 0;
|
|
4421
|
+
var data = _a.data;
|
|
4414
4422
|
var buffer = new Uint32Array(data.buffer);
|
|
4423
|
+
var size = buffer.length | 0;
|
|
4415
4424
|
for (var p = 0; p < size; p = (p + 1) | 0) {
|
|
4416
4425
|
buffer[p] = 0xff000000;
|
|
4417
4426
|
}
|
|
@@ -4989,6 +4998,8 @@ addHandler(1036, function (target) { return target.thumbnail !== undefined || ta
|
|
|
4989
4998
|
data = target.thumbnailRaw.data;
|
|
4990
4999
|
}
|
|
4991
5000
|
else {
|
|
5001
|
+
if (!target.thumbnail)
|
|
5002
|
+
throw new Error('Missing thumbnail');
|
|
4992
5003
|
width = target.thumbnail.width;
|
|
4993
5004
|
height = target.thumbnail.height;
|
|
4994
5005
|
data = base64_js_1.toByteArray(target.thumbnail.toDataURL('image/jpeg', 1).substr('data:image/jpeg;base64,'.length));
|
|
@@ -5267,7 +5278,7 @@ function setupGrayscale(data) {
|
|
|
5267
5278
|
}
|
|
5268
5279
|
function createReader(buffer, offset, length) {
|
|
5269
5280
|
var view = new DataView(buffer, offset, length);
|
|
5270
|
-
return { view: view, offset: 0 };
|
|
5281
|
+
return { view: view, offset: 0, strict: false };
|
|
5271
5282
|
}
|
|
5272
5283
|
exports.createReader = createReader;
|
|
5273
5284
|
function readUint8(reader) {
|
|
@@ -5480,6 +5491,9 @@ function readPsd(reader, options) {
|
|
|
5480
5491
|
if (!skipComposite) {
|
|
5481
5492
|
readImageData(reader, psd, globalAlpha, opt);
|
|
5482
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
|
|
5483
5497
|
return psd;
|
|
5484
5498
|
}
|
|
5485
5499
|
exports.readPsd = readPsd;
|
|
@@ -5629,14 +5643,21 @@ function readLayerBlendingRanges(reader) {
|
|
|
5629
5643
|
function readLayerChannelImageData(reader, psd, layer, channels, options) {
|
|
5630
5644
|
var layerWidth = (layer.right || 0) - (layer.left || 0);
|
|
5631
5645
|
var layerHeight = (layer.bottom || 0) - (layer.top || 0);
|
|
5646
|
+
var cmyk = psd.colorMode === 4 /* CMYK */;
|
|
5632
5647
|
var imageData;
|
|
5633
5648
|
if (layerWidth && layerHeight) {
|
|
5634
|
-
|
|
5635
|
-
|
|
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
|
+
}
|
|
5636
5658
|
}
|
|
5637
|
-
if (helpers_1.RAW_IMAGE_DATA)
|
|
5659
|
+
if (helpers_1.RAW_IMAGE_DATA)
|
|
5638
5660
|
layer.imageDataRaw = [];
|
|
5639
|
-
}
|
|
5640
5661
|
for (var _i = 0, channels_1 = channels; _i < channels_1.length; _i++) {
|
|
5641
5662
|
var channel = channels_1[_i];
|
|
5642
5663
|
var compression = readUint16(reader);
|
|
@@ -5650,7 +5671,7 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
|
|
|
5650
5671
|
var maskData = helpers_1.createImageData(maskWidth, maskHeight);
|
|
5651
5672
|
helpers_1.resetImageData(maskData);
|
|
5652
5673
|
var start = reader.offset;
|
|
5653
|
-
readData(reader, maskData, compression, maskWidth, maskHeight, 0, options.large);
|
|
5674
|
+
readData(reader, maskData, compression, maskWidth, maskHeight, 0, options.large, 4);
|
|
5654
5675
|
if (helpers_1.RAW_IMAGE_DATA) {
|
|
5655
5676
|
layer.maskDataRaw = new Uint8Array(reader.view.buffer, reader.view.byteOffset + start, reader.offset - start);
|
|
5656
5677
|
}
|
|
@@ -5665,7 +5686,7 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
|
|
|
5665
5686
|
}
|
|
5666
5687
|
}
|
|
5667
5688
|
else {
|
|
5668
|
-
var offset = helpers_1.offsetForChannel(channel.id);
|
|
5689
|
+
var offset = helpers_1.offsetForChannel(channel.id, cmyk);
|
|
5669
5690
|
var targetData = imageData;
|
|
5670
5691
|
if (offset < 0) {
|
|
5671
5692
|
targetData = undefined;
|
|
@@ -5674,7 +5695,7 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
|
|
|
5674
5695
|
}
|
|
5675
5696
|
}
|
|
5676
5697
|
var start = reader.offset;
|
|
5677
|
-
readData(reader, targetData, compression, layerWidth, layerHeight, offset, options.large);
|
|
5698
|
+
readData(reader, targetData, compression, layerWidth, layerHeight, offset, options.large, cmyk ? 5 : 4);
|
|
5678
5699
|
if (helpers_1.RAW_IMAGE_DATA) {
|
|
5679
5700
|
layer.imageDataRaw[channel.id] = new Uint8Array(reader.view.buffer, reader.view.byteOffset + start, reader.offset - start);
|
|
5680
5701
|
}
|
|
@@ -5684,6 +5705,11 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
|
|
|
5684
5705
|
}
|
|
5685
5706
|
}
|
|
5686
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
|
+
}
|
|
5687
5713
|
if (options.useImageData) {
|
|
5688
5714
|
layer.imageData = imageData;
|
|
5689
5715
|
}
|
|
@@ -5693,12 +5719,12 @@ function readLayerChannelImageData(reader, psd, layer, channels, options) {
|
|
|
5693
5719
|
}
|
|
5694
5720
|
}
|
|
5695
5721
|
}
|
|
5696
|
-
function readData(reader, data, compression, width, height, offset, large) {
|
|
5722
|
+
function readData(reader, data, compression, width, height, offset, large, step) {
|
|
5697
5723
|
if (compression === 0 /* RawData */) {
|
|
5698
|
-
readDataRaw(reader, data, offset, width, height);
|
|
5724
|
+
readDataRaw(reader, data, offset, width, height, step);
|
|
5699
5725
|
}
|
|
5700
5726
|
else if (compression === 1 /* RleCompressed */) {
|
|
5701
|
-
readDataRLE(reader, data, width, height,
|
|
5727
|
+
readDataRLE(reader, data, width, height, step, [offset], large);
|
|
5702
5728
|
}
|
|
5703
5729
|
else {
|
|
5704
5730
|
throw new Error("Compression type not supported: " + compression);
|
|
@@ -5755,46 +5781,78 @@ function readImageData(reader, psd, globalAlpha, options) {
|
|
|
5755
5781
|
throw new Error("Compression type not supported: " + compression);
|
|
5756
5782
|
var imageData = helpers_1.createImageData(psd.width, psd.height);
|
|
5757
5783
|
helpers_1.resetImageData(imageData);
|
|
5758
|
-
|
|
5759
|
-
|
|
5760
|
-
|
|
5761
|
-
|
|
5762
|
-
|
|
5763
|
-
else if (compression === 1 /* RleCompressed */) {
|
|
5764
|
-
bytes = new Uint8Array(psd.width * psd.height);
|
|
5765
|
-
readDataRLE(reader, { data: bytes, width: psd.width, height: psd.height }, psd.width, psd.height, 1, [0], options.large);
|
|
5766
|
-
}
|
|
5767
|
-
else {
|
|
5768
|
-
throw new Error("Bitmap compression not supported: " + compression);
|
|
5769
|
-
}
|
|
5770
|
-
helpers_1.decodeBitmap(bytes, imageData.data, psd.width, psd.height);
|
|
5771
|
-
}
|
|
5772
|
-
else {
|
|
5773
|
-
var channels = psd.colorMode === 1 /* Grayscale */ ? [0] : [0, 1, 2];
|
|
5774
|
-
if (psd.channels && psd.channels > 3) {
|
|
5775
|
-
for (var i = 3; i < psd.channels; i++) {
|
|
5776
|
-
// TODO: store these channels in additional image data
|
|
5777
|
-
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);
|
|
5778
5789
|
}
|
|
5779
|
-
|
|
5780
|
-
|
|
5781
|
-
|
|
5782
|
-
}
|
|
5783
|
-
if (compression === 0 /* RawData */) {
|
|
5784
|
-
for (var i = 0; i < channels.length; i++) {
|
|
5785
|
-
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);
|
|
5786
5793
|
}
|
|
5794
|
+
else {
|
|
5795
|
+
throw new Error("Bitmap compression not supported: " + compression);
|
|
5796
|
+
}
|
|
5797
|
+
helpers_1.decodeBitmap(bytes, imageData.data, psd.width, psd.height);
|
|
5798
|
+
break;
|
|
5787
5799
|
}
|
|
5788
|
-
|
|
5789
|
-
|
|
5790
|
-
|
|
5791
|
-
if (
|
|
5792
|
-
|
|
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);
|
|
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
|
+
}
|
|
5793
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;
|
|
5794
5827
|
}
|
|
5795
|
-
|
|
5796
|
-
|
|
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;
|
|
5797
5854
|
}
|
|
5855
|
+
default: throw new Error("Color mode not supported: " + psd.colorMode);
|
|
5798
5856
|
}
|
|
5799
5857
|
if (options.useImageData) {
|
|
5800
5858
|
psd.imageData = imageData;
|
|
@@ -5804,12 +5862,37 @@ function readImageData(reader, psd, globalAlpha, options) {
|
|
|
5804
5862
|
psd.canvas.getContext('2d').putImageData(imageData, 0, 0);
|
|
5805
5863
|
}
|
|
5806
5864
|
}
|
|
5807
|
-
function
|
|
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) {
|
|
5808
5891
|
var size = width * height;
|
|
5809
5892
|
var buffer = readBytes(reader, size);
|
|
5810
|
-
if (pixelData && offset <
|
|
5893
|
+
if (pixelData && offset < step) {
|
|
5811
5894
|
var data = pixelData.data;
|
|
5812
|
-
for (var i = 0, p = offset | 0; i < size; i++, p = (p +
|
|
5895
|
+
for (var i = 0, p = offset | 0; i < size; i++, p = (p + step) | 0) {
|
|
5813
5896
|
data[p] = buffer[i];
|
|
5814
5897
|
}
|
|
5815
5898
|
}
|
|
@@ -5833,9 +5916,10 @@ function readDataRLE(reader, pixelData, _width, height, step, offsets, large) {
|
|
|
5833
5916
|
}
|
|
5834
5917
|
}
|
|
5835
5918
|
}
|
|
5919
|
+
var extraLimit = (step - 1) | 0; // 3 for rgb, 4 for cmyk
|
|
5836
5920
|
for (var c = 0, li = 0; c < offsets.length; c++) {
|
|
5837
5921
|
var offset = offsets[c] | 0;
|
|
5838
|
-
var extra = c >
|
|
5922
|
+
var extra = c > extraLimit || offset > extraLimit;
|
|
5839
5923
|
if (!data || extra) {
|
|
5840
5924
|
for (var y = 0; y < height; y++, li++) {
|
|
5841
5925
|
skipBytes(reader, lengths[li]);
|
|
@@ -5886,10 +5970,13 @@ function readSection(reader, round, func, skipEmpty, eightBytes) {
|
|
|
5886
5970
|
return undefined;
|
|
5887
5971
|
var end = reader.offset + length;
|
|
5888
5972
|
var result = func(function () { return end - reader.offset; });
|
|
5889
|
-
if (reader.offset > end)
|
|
5973
|
+
if (reader.offset > end) {
|
|
5890
5974
|
throw new Error('Exceeded section limits');
|
|
5891
|
-
|
|
5892
|
-
|
|
5975
|
+
}
|
|
5976
|
+
if (reader.offset !== end && reader.strict) {
|
|
5977
|
+
// throw new Error(`Unread section data: ${end - reader.offset} bytes at 0x${reader.offset.toString(16)}`);
|
|
5978
|
+
console.warn('Unread section data');
|
|
5979
|
+
}
|
|
5893
5980
|
while (end % round)
|
|
5894
5981
|
end++;
|
|
5895
5982
|
reader.offset = end;
|
|
@@ -5948,11 +6035,22 @@ function readPattern(reader) {
|
|
|
5948
6035
|
var x = readInt16(reader);
|
|
5949
6036
|
var y = readInt16(reader);
|
|
5950
6037
|
// we only support RGB and grayscale for now
|
|
5951
|
-
if (colorMode !== 3 /* RGB */ && colorMode !== 1 /* Grayscale */)
|
|
5952
|
-
throw new Error(
|
|
6038
|
+
if (colorMode !== 3 /* RGB */ && colorMode !== 1 /* Grayscale */ && colorMode !== 2 /* Indexed */) {
|
|
6039
|
+
throw new Error("Unsupported pattern color mode: " + colorMode);
|
|
6040
|
+
}
|
|
5953
6041
|
var name = readUnicodeString(reader);
|
|
5954
6042
|
var id = readPascalString(reader, 1);
|
|
5955
|
-
|
|
6043
|
+
var palette = [];
|
|
6044
|
+
if (colorMode === 2 /* Indexed */) {
|
|
6045
|
+
for (var i = 0; i < 256; i++) {
|
|
6046
|
+
palette.push({
|
|
6047
|
+
r: readUint8(reader),
|
|
6048
|
+
g: readUint8(reader),
|
|
6049
|
+
b: readUint8(reader),
|
|
6050
|
+
});
|
|
6051
|
+
}
|
|
6052
|
+
skipBytes(reader, 4); // no idea what this is
|
|
6053
|
+
}
|
|
5956
6054
|
// virtual memory array list
|
|
5957
6055
|
var version2 = readUint32(reader);
|
|
5958
6056
|
if (version2 !== 3)
|
|
@@ -5971,60 +6069,67 @@ function readPattern(reader) {
|
|
|
5971
6069
|
}
|
|
5972
6070
|
for (var i = 0, ch = 0; i < (channelsCount + 2); i++) {
|
|
5973
6071
|
var has = readUint32(reader);
|
|
5974
|
-
if (has)
|
|
5975
|
-
|
|
5976
|
-
|
|
5977
|
-
|
|
5978
|
-
|
|
5979
|
-
|
|
5980
|
-
|
|
5981
|
-
|
|
5982
|
-
|
|
5983
|
-
|
|
5984
|
-
|
|
5985
|
-
|
|
5986
|
-
|
|
5987
|
-
|
|
5988
|
-
|
|
5989
|
-
|
|
5990
|
-
|
|
5991
|
-
|
|
5992
|
-
|
|
5993
|
-
|
|
5994
|
-
|
|
5995
|
-
|
|
5996
|
-
|
|
5997
|
-
|
|
5998
|
-
|
|
6072
|
+
if (!has)
|
|
6073
|
+
continue;
|
|
6074
|
+
var length_2 = readUint32(reader);
|
|
6075
|
+
var pixelDepth = readUint32(reader);
|
|
6076
|
+
var ctop = readUint32(reader);
|
|
6077
|
+
var cleft = readUint32(reader);
|
|
6078
|
+
var cbottom = readUint32(reader);
|
|
6079
|
+
var cright = readUint32(reader);
|
|
6080
|
+
var pixelDepth2 = readUint16(reader);
|
|
6081
|
+
var compressionMode = readUint8(reader); // 0 - raw, 1 - zip
|
|
6082
|
+
var dataLength = length_2 - (4 + 16 + 2 + 1);
|
|
6083
|
+
var cdata = readBytes(reader, dataLength);
|
|
6084
|
+
if (pixelDepth !== 8 || pixelDepth2 !== 8) {
|
|
6085
|
+
throw new Error('16bit pixel depth not supported for patterns');
|
|
6086
|
+
}
|
|
6087
|
+
var w = cright - cleft;
|
|
6088
|
+
var h = cbottom - ctop;
|
|
6089
|
+
var ox = cleft - left;
|
|
6090
|
+
var oy = ctop - top;
|
|
6091
|
+
if (compressionMode === 0) {
|
|
6092
|
+
if (colorMode === 3 /* RGB */ && ch < 3) {
|
|
6093
|
+
for (var y_1 = 0; y_1 < h; y_1++) {
|
|
6094
|
+
for (var x_1 = 0; x_1 < w; x_1++) {
|
|
6095
|
+
var src = x_1 + y_1 * w;
|
|
6096
|
+
var dst = (ox + x_1 + (y_1 + oy) * width) * 4;
|
|
6097
|
+
data[dst + ch] = cdata[src];
|
|
5999
6098
|
}
|
|
6000
6099
|
}
|
|
6001
|
-
|
|
6002
|
-
|
|
6003
|
-
|
|
6004
|
-
|
|
6005
|
-
|
|
6006
|
-
|
|
6007
|
-
|
|
6008
|
-
|
|
6009
|
-
|
|
6010
|
-
|
|
6100
|
+
}
|
|
6101
|
+
if (colorMode === 1 /* Grayscale */ && ch < 1) {
|
|
6102
|
+
for (var y_2 = 0; y_2 < h; y_2++) {
|
|
6103
|
+
for (var x_2 = 0; x_2 < w; x_2++) {
|
|
6104
|
+
var src = x_2 + y_2 * w;
|
|
6105
|
+
var dst = (ox + x_2 + (y_2 + oy) * width) * 4;
|
|
6106
|
+
var value = cdata[src];
|
|
6107
|
+
data[dst + 0] = value;
|
|
6108
|
+
data[dst + 1] = value;
|
|
6109
|
+
data[dst + 2] = value;
|
|
6011
6110
|
}
|
|
6012
6111
|
}
|
|
6013
6112
|
}
|
|
6014
|
-
|
|
6015
|
-
//
|
|
6016
|
-
|
|
6017
|
-
// const data = require('zlib').inflateRawSync(cdata);
|
|
6018
|
-
// const data = require('zlib').unzipSync(cdata);
|
|
6019
|
-
// console.log(data);
|
|
6020
|
-
// throw new Error('Zip compression not supported for palettes');
|
|
6021
|
-
throw new Error('Unsupported palette compression mode');
|
|
6022
|
-
}
|
|
6023
|
-
else {
|
|
6024
|
-
throw new Error('Invalid palette compression mode');
|
|
6113
|
+
if (colorMode === 2 /* Indexed */) {
|
|
6114
|
+
// TODO:
|
|
6115
|
+
throw new Error('Indexed pattern color mode not implemented');
|
|
6025
6116
|
}
|
|
6026
|
-
ch++;
|
|
6027
6117
|
}
|
|
6118
|
+
else if (compressionMode === 1) {
|
|
6119
|
+
// console.log({ colorMode });
|
|
6120
|
+
// require('fs').writeFileSync('zip.bin', Buffer.from(cdata));
|
|
6121
|
+
// const data = require('zlib').inflateRawSync(cdata);
|
|
6122
|
+
// const data = require('zlib').unzipSync(cdata);
|
|
6123
|
+
// console.log(data);
|
|
6124
|
+
// throw new Error('Zip compression not supported for pattern');
|
|
6125
|
+
// throw new Error('Unsupported pattern compression');
|
|
6126
|
+
console.error('Unsupported pattern compression');
|
|
6127
|
+
name += ' (failed to decode)';
|
|
6128
|
+
}
|
|
6129
|
+
else {
|
|
6130
|
+
throw new Error('Invalid pattern compression mode');
|
|
6131
|
+
}
|
|
6132
|
+
ch++;
|
|
6028
6133
|
}
|
|
6029
6134
|
// TODO: use canvas instead of data ?
|
|
6030
6135
|
return { id: id, name: name, x: x, y: y, bounds: { x: left, y: top, w: width, h: height }, data: data };
|
|
@@ -6224,7 +6329,7 @@ function writePsd(writer, psd, options) {
|
|
|
6224
6329
|
writeUint32(writer, psd.height);
|
|
6225
6330
|
writeUint32(writer, psd.width);
|
|
6226
6331
|
writeUint16(writer, 8); // bits per channel
|
|
6227
|
-
writeUint16(writer, 3 /* RGB */);
|
|
6332
|
+
writeUint16(writer, 3 /* RGB */); // we only support saving RGB right now
|
|
6228
6333
|
// color mode data
|
|
6229
6334
|
writeSection(writer, 1, function () {
|
|
6230
6335
|
// TODO: implement
|
|
@@ -6572,9 +6677,9 @@ function getLayerChannels(tempBuffer, layer, background, options) {
|
|
|
6572
6677
|
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;
|
|
6573
6678
|
var channels = [
|
|
6574
6679
|
{ channelId: -1 /* Transparency */, compression: 0 /* RawData */, buffer: undefined, length: 2 },
|
|
6575
|
-
{ channelId: 0 /*
|
|
6576
|
-
{ channelId: 1 /*
|
|
6577
|
-
{ channelId: 2 /*
|
|
6680
|
+
{ channelId: 0 /* Color0 */, compression: 0 /* RawData */, buffer: undefined, length: 2 },
|
|
6681
|
+
{ channelId: 1 /* Color1 */, compression: 0 /* RawData */, buffer: undefined, length: 2 },
|
|
6682
|
+
{ channelId: 2 /* Color2 */, compression: 0 /* RawData */, buffer: undefined, length: 2 },
|
|
6578
6683
|
];
|
|
6579
6684
|
var _f = getLayerDimentions(layer), width = _f.width, height = _f.height;
|
|
6580
6685
|
if (!(layer.canvas || layer.imageData) || !width || !height) {
|
|
@@ -6606,15 +6711,15 @@ function getLayerChannels(tempBuffer, layer, background, options) {
|
|
|
6606
6711
|
}
|
|
6607
6712
|
}
|
|
6608
6713
|
var channelIds = [
|
|
6609
|
-
0 /*
|
|
6610
|
-
1 /*
|
|
6611
|
-
2 /*
|
|
6714
|
+
0 /* Color0 */,
|
|
6715
|
+
1 /* Color1 */,
|
|
6716
|
+
2 /* Color2 */,
|
|
6612
6717
|
];
|
|
6613
6718
|
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']))) {
|
|
6614
6719
|
channelIds.unshift(-1 /* Transparency */);
|
|
6615
6720
|
}
|
|
6616
6721
|
channels = channelIds.map(function (channel) {
|
|
6617
|
-
var offset = helpers_1.offsetForChannel(channel);
|
|
6722
|
+
var offset = helpers_1.offsetForChannel(channel, false); // TODO: psd.colorMode === ColorMode.CMYK);
|
|
6618
6723
|
var buffer = helpers_1.writeDataRLE(tempBuffer, data, width, height, [offset], !!options.psb);
|
|
6619
6724
|
if (helpers_1.RAW_IMAGE_DATA && layer.imageDataRaw) {
|
|
6620
6725
|
// console.log('written raw layer image data');
|