ccc-tnt-psd2ui/npm-packages/mac-v2.4.x/ag-psd/dist/helpers.js

334 lines
42 KiB
JavaScript
Raw Normal View History

2023-07-27 07:04:13 +00:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.initializeCanvas = exports.createImageData = exports.createCanvasFromData = exports.createCanvas = exports.writeDataZipWithoutPrediction = exports.writeDataRLE = exports.writeDataRaw = exports.decodeBitmap = exports.resetImageData = exports.hasAlpha = exports.clamp = exports.offsetForChannel = exports.Compression = exports.ChannelID = exports.MaskParams = exports.LayerMaskFlags = exports.ColorSpace = exports.createEnum = exports.revMap = exports.largeAdditionalInfoKeys = exports.layerColors = exports.toBlendMode = exports.fromBlendMode = exports.RAW_IMAGE_DATA = exports.MOCK_HANDLERS = void 0;
var base64_js_1 = require("base64-js");
var pako_1 = require("pako");
exports.MOCK_HANDLERS = false;
exports.RAW_IMAGE_DATA = false;
exports.fromBlendMode = {};
exports.toBlendMode = {
'pass': 'pass through',
'norm': 'normal',
'diss': 'dissolve',
'dark': 'darken',
'mul ': 'multiply',
'idiv': 'color burn',
'lbrn': 'linear burn',
'dkCl': 'darker color',
'lite': 'lighten',
'scrn': 'screen',
'div ': 'color dodge',
'lddg': 'linear dodge',
'lgCl': 'lighter color',
'over': 'overlay',
'sLit': 'soft light',
'hLit': 'hard light',
'vLit': 'vivid light',
'lLit': 'linear light',
'pLit': 'pin light',
'hMix': 'hard mix',
'diff': 'difference',
'smud': 'exclusion',
'fsub': 'subtract',
'fdiv': 'divide',
'hue ': 'hue',
'sat ': 'saturation',
'colr': 'color',
'lum ': 'luminosity',
};
Object.keys(exports.toBlendMode).forEach(function (key) { return exports.fromBlendMode[exports.toBlendMode[key]] = key; });
exports.layerColors = [
'none', 'red', 'orange', 'yellow', 'green', 'blue', 'violet', 'gray'
];
exports.largeAdditionalInfoKeys = [
// from documentation
'LMsk', 'Lr16', 'Lr32', 'Layr', 'Mt16', 'Mt32', 'Mtrn', 'Alph', 'FMsk', 'lnk2', 'FEid', 'FXid', 'PxSD',
// from guessing
'cinf',
];
function revMap(map) {
var result = {};
Object.keys(map).forEach(function (key) { return result[map[key]] = key; });
return result;
}
exports.revMap = revMap;
function createEnum(prefix, def, map) {
var rev = revMap(map);
var decode = function (val) {
var value = val.split('.')[1];
if (value && !rev[value])
throw new Error("Unrecognized value for enum: '".concat(val, "'"));
return rev[value] || def;
};
var encode = function (val) {
if (val && !map[val])
throw new Error("Invalid value for enum: '".concat(val, "'"));
return "".concat(prefix, ".").concat(map[val] || map[def]);
};
return { decode: decode, encode: encode };
}
exports.createEnum = createEnum;
var ColorSpace;
(function (ColorSpace) {
ColorSpace[ColorSpace["RGB"] = 0] = "RGB";
ColorSpace[ColorSpace["HSB"] = 1] = "HSB";
ColorSpace[ColorSpace["CMYK"] = 2] = "CMYK";
ColorSpace[ColorSpace["Lab"] = 7] = "Lab";
ColorSpace[ColorSpace["Grayscale"] = 8] = "Grayscale";
})(ColorSpace = exports.ColorSpace || (exports.ColorSpace = {}));
var LayerMaskFlags;
(function (LayerMaskFlags) {
LayerMaskFlags[LayerMaskFlags["PositionRelativeToLayer"] = 1] = "PositionRelativeToLayer";
LayerMaskFlags[LayerMaskFlags["LayerMaskDisabled"] = 2] = "LayerMaskDisabled";
LayerMaskFlags[LayerMaskFlags["InvertLayerMaskWhenBlending"] = 4] = "InvertLayerMaskWhenBlending";
LayerMaskFlags[LayerMaskFlags["LayerMaskFromRenderingOtherData"] = 8] = "LayerMaskFromRenderingOtherData";
LayerMaskFlags[LayerMaskFlags["MaskHasParametersAppliedToIt"] = 16] = "MaskHasParametersAppliedToIt";
})(LayerMaskFlags = exports.LayerMaskFlags || (exports.LayerMaskFlags = {}));
var MaskParams;
(function (MaskParams) {
MaskParams[MaskParams["UserMaskDensity"] = 1] = "UserMaskDensity";
MaskParams[MaskParams["UserMaskFeather"] = 2] = "UserMaskFeather";
MaskParams[MaskParams["VectorMaskDensity"] = 4] = "VectorMaskDensity";
MaskParams[MaskParams["VectorMaskFeather"] = 8] = "VectorMaskFeather";
})(MaskParams = exports.MaskParams || (exports.MaskParams = {}));
var ChannelID;
(function (ChannelID) {
ChannelID[ChannelID["Color0"] = 0] = "Color0";
ChannelID[ChannelID["Color1"] = 1] = "Color1";
ChannelID[ChannelID["Color2"] = 2] = "Color2";
ChannelID[ChannelID["Color3"] = 3] = "Color3";
ChannelID[ChannelID["Transparency"] = -1] = "Transparency";
ChannelID[ChannelID["UserMask"] = -2] = "UserMask";
ChannelID[ChannelID["RealUserMask"] = -3] = "RealUserMask";
})(ChannelID = exports.ChannelID || (exports.ChannelID = {}));
var Compression;
(function (Compression) {
Compression[Compression["RawData"] = 0] = "RawData";
Compression[Compression["RleCompressed"] = 1] = "RleCompressed";
Compression[Compression["ZipWithoutPrediction"] = 2] = "ZipWithoutPrediction";
Compression[Compression["ZipWithPrediction"] = 3] = "ZipWithPrediction";
})(Compression = exports.Compression || (exports.Compression = {}));
function offsetForChannel(channelId, cmyk) {
switch (channelId) {
case 0 /* ChannelID.Color0 */: return 0;
case 1 /* ChannelID.Color1 */: return 1;
case 2 /* ChannelID.Color2 */: return 2;
case 3 /* ChannelID.Color3 */: return cmyk ? 3 : channelId + 1;
case -1 /* ChannelID.Transparency */: return cmyk ? 4 : 3;
default: return channelId + 1;
}
}
exports.offsetForChannel = offsetForChannel;
function clamp(value, min, max) {
return value < min ? min : (value > max ? max : value);
}
exports.clamp = clamp;
function hasAlpha(data) {
var size = data.width * data.height * 4;
for (var i = 3; i < size; i += 4) {
if (data.data[i] !== 255) {
return true;
}
}
return false;
}
exports.hasAlpha = hasAlpha;
function resetImageData(_a) {
var data = _a.data;
var buffer = new Uint32Array(data.buffer);
var size = buffer.length | 0;
for (var p = 0; p < size; p = (p + 1) | 0) {
buffer[p] = 0xff000000;
}
}
exports.resetImageData = resetImageData;
function decodeBitmap(input, output, width, height) {
for (var y = 0, p = 0, o = 0; y < height; y++) {
for (var x = 0; x < width;) {
var b = input[o++];
for (var i = 0; i < 8 && x < width; i++, x++) {
var v = b & 0x80 ? 0 : 255;
b = b << 1;
output[p++] = v;
output[p++] = v;
output[p++] = v;
output[p++] = 255;
}
}
}
}
exports.decodeBitmap = decodeBitmap;
function writeDataRaw(data, offset, width, height) {
if (!width || !height)
return undefined;
var array = new Uint8Array(width * height);
for (var i = 0; i < array.length; i++) {
array[i] = data.data[i * 4 + offset];
}
return array;
}
exports.writeDataRaw = writeDataRaw;
function writeDataRLE(buffer, _a, offsets, large) {
var data = _a.data, width = _a.width, height = _a.height;
if (!width || !height)
return undefined;
var stride = (4 * width) | 0;
var ol = 0;
var o = (offsets.length * (large ? 4 : 2) * height) | 0;
for (var _i = 0, offsets_1 = offsets; _i < offsets_1.length; _i++) {
var offset = offsets_1[_i];
for (var y = 0, p = offset | 0; y < height; y++) {
var strideStart = (y * stride) | 0;
var strideEnd = (strideStart + stride) | 0;
var lastIndex = (strideEnd + offset - 4) | 0;
var lastIndex2 = (lastIndex - 4) | 0;
var startOffset = o;
for (p = (strideStart + offset) | 0; p < strideEnd; p = (p + 4) | 0) {
if (p < lastIndex2) {
var value1 = data[p];
p = (p + 4) | 0;
var value2 = data[p];
p = (p + 4) | 0;
var value3 = data[p];
if (value1 === value2 && value1 === value3) {
var count = 3;
while (count < 128 && p < lastIndex && data[(p + 4) | 0] === value1) {
count = (count + 1) | 0;
p = (p + 4) | 0;
}
buffer[o++] = 1 - count;
buffer[o++] = value1;
}
else {
var countIndex = o;
var writeLast = true;
var count = 1;
buffer[o++] = 0;
buffer[o++] = value1;
while (p < lastIndex && count < 128) {
p = (p + 4) | 0;
value1 = value2;
value2 = value3;
value3 = data[p];
if (value1 === value2 && value1 === value3) {
p = (p - 12) | 0;
writeLast = false;
break;
}
else {
count++;
buffer[o++] = value1;
}
}
if (writeLast) {
if (count < 127) {
buffer[o++] = value2;
buffer[o++] = value3;
count += 2;
}
else if (count < 128) {
buffer[o++] = value2;
count++;
p = (p - 4) | 0;
}
else {
p = (p - 8) | 0;
}
}
buffer[countIndex] = count - 1;
}
}
else if (p === lastIndex) {
buffer[o++] = 0;
buffer[o++] = data[p];
}
else { // p === lastIndex2
buffer[o++] = 1;
buffer[o++] = data[p];
p = (p + 4) | 0;
buffer[o++] = data[p];
}
}
var length_1 = o - startOffset;
if (large) {
buffer[ol++] = (length_1 >> 24) & 0xff;
buffer[ol++] = (length_1 >> 16) & 0xff;
}
buffer[ol++] = (length_1 >> 8) & 0xff;
buffer[ol++] = length_1 & 0xff;
}
}
return buffer.slice(0, o);
}
exports.writeDataRLE = writeDataRLE;
function writeDataZipWithoutPrediction(_a, offsets) {
var data = _a.data, width = _a.width, height = _a.height;
var size = width * height;
var channel = new Uint8Array(size);
var buffers = [];
var totalLength = 0;
for (var _i = 0, offsets_2 = offsets; _i < offsets_2.length; _i++) {
var offset = offsets_2[_i];
for (var i = 0, o = offset; i < size; i++, o += 4) {
channel[i] = data[o];
}
var buffer = (0, pako_1.deflate)(channel);
buffers.push(buffer);
totalLength += buffer.byteLength;
}
if (buffers.length > 0) {
var buffer = new Uint8Array(totalLength);
var offset = 0;
for (var _b = 0, buffers_1 = buffers; _b < buffers_1.length; _b++) {
var b = buffers_1[_b];
buffer.set(b, offset);
offset += b.byteLength;
}
return buffer;
}
else {
return buffers[0];
}
}
exports.writeDataZipWithoutPrediction = writeDataZipWithoutPrediction;
var createCanvas = function () {
throw new Error('Canvas not initialized, use initializeCanvas method to set up createCanvas method');
};
exports.createCanvas = createCanvas;
var createCanvasFromData = function () {
throw new Error('Canvas not initialized, use initializeCanvas method to set up createCanvasFromData method');
};
exports.createCanvasFromData = createCanvasFromData;
var tempCanvas = undefined;
var createImageData = function (width, height) {
if (!tempCanvas)
tempCanvas = (0, exports.createCanvas)(1, 1);
return tempCanvas.getContext('2d').createImageData(width, height);
};
exports.createImageData = createImageData;
if (typeof document !== 'undefined') {
exports.createCanvas = function (width, height) {
var canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
return canvas;
};
exports.createCanvasFromData = function (data) {
var image = new Image();
image.src = 'data:image/jpeg;base64,' + (0, base64_js_1.fromByteArray)(data);
var canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
canvas.getContext('2d').drawImage(image, 0, 0);
return canvas;
};
}
function initializeCanvas(createCanvasMethod, createCanvasFromDataMethod, createImageDataMethod) {
exports.createCanvas = createCanvasMethod;
exports.createCanvasFromData = createCanvasFromDataMethod || exports.createCanvasFromData;
exports.createImageData = createImageDataMethod || exports.createImageData;
}
exports.initializeCanvas = initializeCanvas;
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImhlbHBlcnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBQUEsdUNBQTBDO0FBQzFDLDZCQUErQjtBQUdsQixRQUFBLGFBQWEsR0FBRyxLQUFLLENBQUM7QUFDdEIsUUFBQSxjQUFjLEdBQUcsS0FBSyxDQUFDO0FBRXZCLFFBQUEsYUFBYSxHQUE4QixFQUFFLENBQUM7QUFDOUMsUUFBQSxXQUFXLEdBQWlDO0lBQ3hELE1BQU0sRUFBRSxjQUFjO0lBQ3RCLE1BQU0sRUFBRSxRQUFRO0lBQ2hCLE1BQU0sRUFBRSxVQUFVO0lBQ2xCLE1BQU0sRUFBRSxRQUFRO0lBQ2hCLE1BQU0sRUFBRSxVQUFVO0lBQ2xCLE1BQU0sRUFBRSxZQUFZO0lBQ3BCLE1BQU0sRUFBRSxhQUFhO0lBQ3JCLE1BQU0sRUFBRSxjQUFjO0lBQ3RCLE1BQU0sRUFBRSxTQUFTO0lBQ2pCLE1BQU0sRUFBRSxRQUFRO0lBQ2hCLE1BQU0sRUFBRSxhQUFhO0lBQ3JCLE1BQU0sRUFBRSxjQUFjO0lBQ3RCLE1BQU0sRUFBRSxlQUFlO0lBQ3ZCLE1BQU0sRUFBRSxTQUFTO0lBQ2pCLE1BQU0sRUFBRSxZQUFZO0lBQ3BCLE1BQU0sRUFBRSxZQUFZO0lBQ3BCLE1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