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

786 lines
126 KiB
JavaScript
Raw Permalink Normal View History

2023-07-27 07:04:13 +00:00
import { toByteArray } from 'base64-js';
import { readPascalString, readUnicodeString, readUint32, readUint16, readUint8, readFloat64, readBytes, skipBytes, readFloat32, readInt16, readFixedPoint32, readSignature, checkSignature, readSection, readColor } from './psdReader';
import { writePascalString, writeUnicodeString, writeUint32, writeUint8, writeFloat64, writeUint16, writeBytes, writeInt16, writeFloat32, writeFixedPoint32, writeUnicodeStringWithPadding, writeColor, writeSignature, writeSection, } from './psdWriter';
import { createCanvasFromData, createEnum, MOCK_HANDLERS } from './helpers';
import { decodeString, encodeString } from './utf8';
import { parseTrackList, readVersionAndDescriptor, serializeTrackList, writeVersionAndDescriptor } from './descriptor';
export var resourceHandlers = [];
export var resourceHandlersMap = {};
function addHandler(key, has, read, write) {
var handler = { key: key, has: has, read: read, write: write };
resourceHandlers.push(handler);
resourceHandlersMap[handler.key] = handler;
}
var LOG_MOCK_HANDLERS = false;
var RESOLUTION_UNITS = [undefined, 'PPI', 'PPCM'];
var MEASUREMENT_UNITS = [undefined, 'Inches', 'Centimeters', 'Points', 'Picas', 'Columns'];
var hex = '0123456789abcdef';
function charToNibble(code) {
return code <= 57 ? code - 48 : code - 87;
}
function byteAt(value, index) {
return (charToNibble(value.charCodeAt(index)) << 4) | charToNibble(value.charCodeAt(index + 1));
}
function readUtf8String(reader, length) {
var buffer = readBytes(reader, length);
return decodeString(buffer);
}
function writeUtf8String(writer, value) {
var buffer = encodeString(value);
writeBytes(writer, buffer);
}
MOCK_HANDLERS && addHandler(1028, // IPTC-NAA record
function (// IPTC-NAA record
target) { return target._ir1028 !== undefined; }, function (reader, target, left) {
LOG_MOCK_HANDLERS && console.log('image resource 1028', left());
target._ir1028 = readBytes(reader, left());
}, function (writer, target) {
writeBytes(writer, target._ir1028);
});
addHandler(1061, function (target) { return target.captionDigest !== undefined; }, function (reader, target) {
var captionDigest = '';
for (var i = 0; i < 16; i++) {
var byte = readUint8(reader);
captionDigest += hex[byte >> 4];
captionDigest += hex[byte & 0xf];
}
target.captionDigest = captionDigest;
}, function (writer, target) {
for (var i = 0; i < 16; i++) {
writeUint8(writer, byteAt(target.captionDigest, i * 2));
}
});
addHandler(1060, function (target) { return target.xmpMetadata !== undefined; }, function (reader, target, left) { return target.xmpMetadata = readUtf8String(reader, left()); }, function (writer, target) { return writeUtf8String(writer, target.xmpMetadata); });
var Inte = createEnum('Inte', 'perceptual', {
'perceptual': 'Img ',
'saturation': 'Grp ',
'relative colorimetric': 'Clrm',
'absolute colorimetric': 'AClr',
});
addHandler(1082, function (target) { return target.printInformation !== undefined; }, function (reader, target) {
var _a, _b;
var desc = readVersionAndDescriptor(reader);
target.printInformation = {
printerName: desc.printerName || '',
renderingIntent: Inte.decode((_a = desc.Inte) !== null && _a !== void 0 ? _a : 'Inte.Img '),
};
var info = target.printInformation;
if (desc.PstS !== undefined)
info.printerManagesColors = desc.PstS;
if (desc['Nm '] !== undefined)
info.printerProfile = desc['Nm '];
if (desc.MpBl !== undefined)
info.blackPointCompensation = desc.MpBl;
if (desc.printSixteenBit !== undefined)
info.printSixteenBit = desc.printSixteenBit;
if (desc.hardProof !== undefined)
info.hardProof = desc.hardProof;
if (desc.printProofSetup) {
if ('Bltn' in desc.printProofSetup) {
info.proofSetup = { builtin: desc.printProofSetup.Bltn.split('.')[1] };
}
else {
info.proofSetup = {
profile: desc.printProofSetup.profile,
renderingIntent: Inte.decode((_b = desc.printProofSetup.Inte) !== null && _b !== void 0 ? _b : 'Inte.Img '),
blackPointCompensation: !!desc.printProofSetup.MpBl,
paperWhite: !!desc.printProofSetup.paperWhite,
};
}
}
}, function (writer, target) {
var _a, _b;
var info = target.printInformation;
var desc = {};
if (info.printerManagesColors) {
desc.PstS = true;
}
else {
if (info.hardProof !== undefined)
desc.hardProof = !!info.hardProof;
desc.ClrS = 'ClrS.RGBC'; // TODO: ???
desc['Nm '] = (_a = info.printerProfile) !== null && _a !== void 0 ? _a : 'CIE RGB';
}
desc.Inte = Inte.encode(info.renderingIntent);
if (!info.printerManagesColors)
desc.MpBl = !!info.blackPointCompensation;
desc.printSixteenBit = !!info.printSixteenBit;
desc.printerName = info.printerName || '';
if (info.proofSetup && 'profile' in info.proofSetup) {
desc.printProofSetup = {
profile: info.proofSetup.profile || '',
Inte: Inte.encode(info.proofSetup.renderingIntent),
MpBl: !!info.proofSetup.blackPointCompensation,
paperWhite: !!info.proofSetup.paperWhite,
};
}
else {
desc.printProofSetup = {
Bltn: ((_b = info.proofSetup) === null || _b === void 0 ? void 0 : _b.builtin) ? "builtinProof.".concat(info.proofSetup.builtin) : 'builtinProof.proofCMYK',
};
}
writeVersionAndDescriptor(writer, '', 'printOutput', desc);
});
MOCK_HANDLERS && addHandler(1083, // Print style
function (// Print style
target) { return target._ir1083 !== undefined; }, function (reader, target, left) {
LOG_MOCK_HANDLERS && console.log('image resource 1083', left());
target._ir1083 = readBytes(reader, left());
// TODO:
// const desc = readVersionAndDescriptor(reader);
// console.log('1083', require('util').inspect(desc, false, 99, true));
}, function (writer, target) {
writeBytes(writer, target._ir1083);
});
addHandler(1005, function (target) { return target.resolutionInfo !== undefined; }, function (reader, target) {
var horizontalResolution = readFixedPoint32(reader);
var horizontalResolutionUnit = readUint16(reader);
var widthUnit = readUint16(reader);
var verticalResolution = readFixedPoint32(reader);
var verticalResolutionUnit = readUint16(reader);
var heightUnit = readUint16(reader);
target.resolutionInfo = {
horizontalResolution: horizontalResolution,
horizontalResolutionUnit: RESOLUTION_UNITS[horizontalResolutionUnit] || 'PPI',
widthUnit: MEASUREMENT_UNITS[widthUnit] || 'Inches',
verticalResolution: verticalResolution,
verticalResolutionUnit: RESOLUTION_UNITS[verticalResolutionUnit] || 'PPI',
heightUnit: MEASUREMENT_UNITS[heightUnit] || 'Inches',
};
}, function (writer, target) {
var info = target.resolutionInfo;
writeFixedPoint32(writer, info.horizontalResolution || 0);
writeUint16(writer, Math.max(1, RESOLUTION_UNITS.indexOf(info.horizontalResolutionUnit)));
writeUint16(writer, Math.max(1, MEASUREMENT_UNITS.indexOf(info.widthUnit)));
writeFixedPoint32(writer, info.verticalResolution || 0);
writeUint16(writer, Math.max(1, RESOLUTION_UNITS.indexOf(info.verticalResolutionUnit)));
writeUint16(writer, Math.max(1, MEASUREMENT_UNITS.indexOf(info.heightUnit)));
});
var printScaleStyles = ['centered', 'size to fit', 'user defined'];
addHandler(1062, function (target) { return target.printScale !== undefined; }, function (reader, target) {
target.printScale = {
style: printScaleStyles[readInt16(reader)],
x: readFloat32(reader),
y: readFloat32(reader),
scale: readFloat32(reader),
};
}, function (writer, target) {
var _a = target.printScale, style = _a.style, x = _a.x, y = _a.y, scale = _a.scale;
writeInt16(writer, Math.max(0, printScaleStyles.indexOf(style)));
writeFloat32(writer, x || 0);
writeFloat32(writer, y || 0);
writeFloat32(writer, scale || 0);
});
addHandler(1006, function (target) { return target.alphaChannelNames !== undefined; }, function (reader, target, left) {
target.alphaChannelNames = [];
while (left()) {
var value = readPascalString(reader, 1);
target.alphaChannelNames.push(value);
}
}, function (writer, target) {
for (var _i = 0, _a = target.alphaChannelNames; _i < _a.length; _i++) {
var name_1 = _a[_i];
writePascalString(writer, name_1, 1);
}
});
addHandler(1045, function (target) { return target.alphaChannelNames !== undefined; }, function (reader, target, left) {
target.alphaChannelNames = [];
while (left()) {
target.alphaChannelNames.push(readUnicodeString(reader));
}
}, function (writer, target) {
for (var _i = 0, _a = target.alphaChannelNames; _i < _a.length; _i++) {
var name_2 = _a[_i];
writeUnicodeStringWithPadding(writer, name_2);
}
});
MOCK_HANDLERS && addHandler(1077, function (target) { return target._ir1077 !== undefined; }, function (reader, target, left) {
LOG_MOCK_HANDLERS && console.log('image resource 1077', left());
target._ir1077 = readBytes(reader, left());
}, function (writer, target) {
writeBytes(writer, target._ir1077);
});
addHandler(1053, function (target) { return target.alphaIdentifiers !== undefined; }, function (reader, target, left) {
target.alphaIdentifiers = [];
while (left() >= 4) {
target.alphaIdentifiers.push(readUint32(reader));
}
}, function (writer, target) {
for (var _i = 0, _a = target.alphaIdentifiers; _i < _a.length; _i++) {
var id = _a[_i];
writeUint32(writer, id);
}
});
addHandler(1010, function (target) { return target.backgroundColor !== undefined; }, function (reader, target) { return target.backgroundColor = readColor(reader); }, function (writer, target) { return writeColor(writer, target.backgroundColor); });
addHandler(1037, function (target) { return target.globalAngle !== undefined; }, function (reader, target) { return target.globalAngle = readUint32(reader); }, function (writer, target) { return writeUint32(writer, target.globalAngle); });
addHandler(1049, function (target) { return target.globalAltitude !== undefined; }, function (reader, target) { return target.globalAltitude = readUint32(reader); }, function (writer, target) { return writeUint32(writer, target.globalAltitude); });
addHandler(1011, function (target) { return target.printFlags !== undefined; }, function (reader, target) {
target.printFlags = {
labels: !!readUint8(reader),
cropMarks: !!readUint8(reader),
colorBars: !!readUint8(reader),
registrationMarks: !!readUint8(reader),
negative: !!readUint8(reader),
flip: !!readUint8(reader),
interpolate: !!readUint8(reader),
caption: !!readUint8(reader),
printFlags: !!readUint8(reader),
};
}, function (writer, target) {
var flags = target.printFlags;
writeUint8(writer, flags.labels ? 1 : 0);
writeUint8(writer, flags.cropMarks ? 1 : 0);
writeUint8(writer, flags.colorBars ? 1 : 0);
writeUint8(writer, flags.registrationMarks ? 1 : 0);
writeUint8(writer, flags.negative ? 1 : 0);
writeUint8(writer, flags.flip ? 1 : 0);
writeUint8(writer, flags.interpolate ? 1 : 0);
writeUint8(writer, flags.caption ? 1 : 0);
writeUint8(writer, flags.printFlags ? 1 : 0);
});
MOCK_HANDLERS && addHandler(10000, // Print flags
function (// Print flags
target) { return target._ir10000 !== undefined; }, function (reader, target, left) {
LOG_MOCK_HANDLERS && console.log('image resource 10000', left());
target._ir10000 = readBytes(reader, left());
}, function (writer, target) {
writeBytes(writer, target._ir10000);
});
MOCK_HANDLERS && addHandler(1013, // Color halftoning
function (// Color halftoning
target) { return target._ir1013 !== undefined; }, function (reader, target, left) {
LOG_MOCK_HANDLERS && console.log('image resource 1013', left());
target._ir1013 = readBytes(reader, left());
}, function (writer, target) {
writeBytes(writer, target._ir1013);
});
MOCK_HANDLERS && addHandler(1016, // Color transfer functions
function (// Color transfer functions
target) { return target._ir1016 !== undefined; }, function (reader, target, left) {
LOG_MOCK_HANDLERS && console.log('image resource 1016', left());
target._ir1016 = readBytes(reader, left());
}, function (writer, target) {
writeBytes(writer, target._ir1016);
});
addHandler(1024, function (target) { return target.layerState !== undefined; }, function (reader, target) { return target.layerState = readUint16(reader); }, function (writer, target) { return writeUint16(writer, target.layerState); });
addHandler(1026, function (target) { return target.layersGroup !== undefined; }, function (reader, target, left) {
target.layersGroup = [];
while (left()) {
target.layersGroup.push(readUint16(reader));
}
}, function (writer, target) {
for (var _i = 0, _a = target.layersGroup; _i < _a.length; _i++) {
var g = _a[_i];
writeUint16(writer, g);
}
});
addHandler(1072, function (target) { return target.layerGroupsEnabledId !== undefined; }, function (reader, target, left) {
target.layerGroupsEnabledId = [];
while (left()) {
target.layerGroupsEnabledId.push(readUint8(reader));
}
}, function (writer, target) {
for (var _i = 0, _a = target.layerGroupsEnabledId; _i < _a.length; _i++) {
var id = _a[_i];
writeUint8(writer, id);
}
});
addHandler(1069, function (target) { return target.layerSelectionIds !== undefined; }, function (reader, target) {
var count = readUint16(reader);
target.layerSelectionIds = [];
while (count--) {
target.layerSelectionIds.push(readUint32(reader));
}
}, function (writer, target) {
writeUint16(writer, target.layerSelectionIds.length);
for (var _i = 0, _a = target.layerSelectionIds; _i < _a.length; _i++) {
var id = _a[_i];
writeUint32(writer, id);
}
});
addHandler(1032, function (target) { return target.gridAndGuidesInformation !== undefined; }, function (reader, target) {
var version = readUint32(reader);
var horizontal = readUint32(reader);
var vertical = readUint32(reader);
var count = readUint32(reader);
if (version !== 1)
throw new Error("Invalid 1032 resource version: ".concat(version));
target.gridAndGuidesInformation = {
grid: { horizontal: horizontal, vertical: vertical },
guides: [],
};
for (var i = 0; i < count; i++) {
target.gridAndGuidesInformation.guides.push({
location: readUint32(reader) / 32,
direction: readUint8(reader) ? 'horizontal' : 'vertical'
});
}
}, function (writer, target) {
var info = target.gridAndGuidesInformation;
var grid = info.grid || { horizontal: 18 * 32, vertical: 18 * 32 };
var guides = info.guides || [];
writeUint32(writer, 1);
writeUint32(writer, grid.horizontal);
writeUint32(writer, grid.vertical);
writeUint32(writer, guides.length);
for (var _i = 0, guides_1 = guides; _i < guides_1.length; _i++) {
var g = guides_1[_i];
writeUint32(writer, g.location * 32);
writeUint8(writer, g.direction === 'horizontal' ? 1 : 0);
}
});
// 0 - normal, 7 - multiply, 8 - screen, 23 - difference
var onionSkinsBlendModes = [
'normal', undefined, undefined, undefined, undefined, undefined, undefined, 'multiply',
'screen', undefined, undefined, undefined, undefined, undefined, undefined, undefined,
undefined, undefined, undefined, undefined, undefined, undefined, undefined, 'difference',
];
addHandler(1078, // Onion Skins
function (// Onion Skins
target) { return target.onionSkins !== undefined; }, function (reader, target) {
var desc = readVersionAndDescriptor(reader);
// console.log('1078', require('util').inspect(desc, false, 99, true));
target.onionSkins = {
enabled: desc.enab,
framesBefore: desc.numBefore,
framesAfter: desc.numAfter,
frameSpacing: desc.Spcn,
minOpacity: desc.minOpacity / 100,
maxOpacity: desc.maxOpacity / 100,
blendMode: onionSkinsBlendModes[desc.BlnM] || 'normal',
};
}, function (writer, target) {
var onionSkins = target.onionSkins;
var desc = {
Vrsn: 1,
enab: onionSkins.enabled,
numBefore: onionSkins.framesBefore,
numAfter: onionSkins.framesAfter,
Spcn: onionSkins.frameSpacing,
minOpacity: (onionSkins.minOpacity * 100) | 0,
maxOpacity: (onionSkins.maxOpacity * 100) | 0,
BlnM: Math.max(0, onionSkinsBlendModes.indexOf(onionSkins.blendMode)),
};
writeVersionAndDescriptor(writer, '', 'null', desc);
});
addHandler(1075, // Timeline Information
function (// Timeline Information
target) { return target.timelineInformation !== undefined; }, function (reader, target, _, options) {
var _a, _b;
var desc = readVersionAndDescriptor(reader);
// console.log('1075', require('util').inspect(desc, false, 99, true));
target.timelineInformation = {
enabled: desc.enab,
frameStep: desc.frameStep,
frameRate: desc.frameRate,
time: desc.time,
duration: desc.duration,
workInTime: desc.workInTime,
workOutTime: desc.workOutTime,
repeats: desc.LCnt,
hasMotion: desc.hasMotion,
globalTracks: parseTrackList(desc.globalTrackList, !!options.logMissingFeatures),
};
if ((_b = (_a = desc.audioClipGroupList) === null || _a === void 0 ? void 0 : _a.audioClipGroupList) === null || _b === void 0 ? void 0 : _b.length) {
target.timelineInformation.audioClipGroups = desc.audioClipGroupList.audioClipGroupList.map(function (g) { return ({
id: g.groupID,
muted: g.muted,
audioClips: g.audioClipList.map(function (_a) {
var clipID = _a.clipID, timeScope = _a.timeScope, muted = _a.muted, audioLevel = _a.audioLevel, frameReader = _a.frameReader;
return ({
id: clipID,
start: timeScope.Strt,
duration: timeScope.duration,
inTime: timeScope.inTime,
outTime: timeScope.outTime,
muted: muted,
audioLevel: audioLevel,
frameReader: {
type: frameReader.frameReaderType,
mediaDescriptor: frameReader.mediaDescriptor,
link: {
name: frameReader['Lnk ']['Nm '],
fullPath: frameReader['Lnk '].fullPath,
relativePath: frameReader['Lnk '].relPath,
},
},
});
}),
}); });
}
}, function (writer, target) {
var _a;
var timeline = target.timelineInformation;
var desc = {
Vrsn: 1,
enab: timeline.enabled,
frameStep: timeline.frameStep,
frameRate: timeline.frameRate,
time: timeline.time,
duration: timeline.duration,
workInTime: timeline.workInTime,
workOutTime: timeline.workOutTime,
LCnt: timeline.repeats,
globalTrackList: serializeTrackList(timeline.globalTracks),
audioClipGroupList: {
audioClipGroupList: (_a = timeline.audioClipGroups) === null || _a === void 0 ? void 0 : _a.map(function (a) { return ({
groupID: a.id,
muted: a.muted,
audioClipList: a.audioClips.map(function (c) { return ({
clipID: c.id,
timeScope: {
Vrsn: 1,
Strt: c.start,
duration: c.duration,
inTime: c.inTime,
outTime: c.outTime,
},
frameReader: {
frameReaderType: c.frameReader.type,
descVersion: 1,
'Lnk ': {
descVersion: 1,
'Nm ': c.frameReader.link.name,
fullPath: c.frameReader.link.fullPath,
relPath: c.frameReader.link.relativePath,
},
mediaDescriptor: c.frameReader.mediaDescriptor,
},
muted: c.muted,
audioLevel: c.audioLevel,
}); }),
}); }),
},
hasMotion: timeline.hasMotion,
};
// console.log('WRITE:1075', require('util').inspect(desc, false, 99, true));
writeVersionAndDescriptor(writer, '', 'null', desc, 'anim');
});
addHandler(1076, // Sheet Disclosure
function (// Sheet Disclosure
target) { return target.sheetDisclosure !== undefined; }, function (reader, target) {
var desc = readVersionAndDescriptor(reader);
// console.log('1076', require('util').inspect(desc, false, 99, true));
target.sheetDisclosure = {};
if (desc.sheetTimelineOptions) {
target.sheetDisclosure.sheetTimelineOptions = desc.sheetTimelineOptions.map(function (o) { return ({
sheetID: o.sheetID,
sheetDisclosed: o.sheetDisclosed,
lightsDisclosed: o.lightsDisclosed,
meshesDisclosed: o.meshesDisclosed,
materialsDisclosed: o.materialsDisclosed,
}); });
}
}, function (writer, target) {
var disclosure = target.sheetDisclosure;
var desc = { Vrsn: 1 };
if (disclosure.sheetTimelineOptions) {
desc.sheetTimelineOptions = disclosure.sheetTimelineOptions.map(function (d) { return ({
Vrsn: 2,
sheetID: d.sheetID,
sheetDisclosed: d.sheetDisclosed,
lightsDisclosed: d.lightsDisclosed,
meshesDisclosed: d.meshesDisclosed,
materialsDisclosed: d.materialsDisclosed,
}); });
}
writeVersionAndDescriptor(writer, '', 'null', desc);
});
addHandler(1054, function (target) { return target.urlsList !== undefined; }, function (reader, target, _, options) {
var count = readUint32(reader);
if (count) {
if (!options.throwForMissingFeatures)
return;
throw new Error('Not implemented: URL List');
}
// TODO: read actual URL list
target.urlsList = [];
}, function (writer, target) {
writeUint32(writer, target.urlsList.length);
// TODO: write actual URL list
if (target.urlsList.length) {
throw new Error('Not implemented: URL List');
}
});
MOCK_HANDLERS && addHandler(1050, // Slices
function (// Slices
target) { return target._ir1050 !== undefined; }, function (reader, target, left) {
LOG_MOCK_HANDLERS && console.log('image resource 1050', left());
target._ir1050 = readBytes(reader, left());
}, function (writer, target) {
writeBytes(writer, target._ir1050);
});
addHandler(1064, function (target) { return target.pixelAspectRatio !== undefined; }, function (reader, target) {
if (readUint32(reader) > 2)
throw new Error('Invalid pixelAspectRatio version');
target.pixelAspectRatio = { aspect: readFloat64(reader) };
}, function (writer, target) {
writeUint32(writer, 2); // version
writeFloat64(writer, target.pixelAspectRatio.aspect);
});
addHandler(1041, function (target) { return target.iccUntaggedProfile !== undefined; }, function (reader, target) {
target.iccUntaggedProfile = !!readUint8(reader);
}, function (writer, target) {
writeUint8(writer, target.iccUntaggedProfile ? 1 : 0);
});
MOCK_HANDLERS && addHandler(1039, // ICC Profile
function (// ICC Profile
target) { return target._ir1039 !== undefined; }, function (reader, target, left) {
LOG_MOCK_HANDLERS && console.log('image resource 1039', left());
target._ir1039 = readBytes(reader, left());
}, function (writer, target) {
writeBytes(writer, target._ir1039);
});
addHandler(1044, function (target) { return target.idsSeedNumber !== undefined; }, function (reader, target) { return target.idsSeedNumber = readUint32(reader); }, function (writer, target) { return writeUint32(writer, target.idsSeedNumber); });
addHandler(1036, function (target) { return target.thumbnail !== undefined || target.thumbnailRaw !== undefined; }, function (reader, target, left, options) {
var format = readUint32(reader); // 1 = kJpegRGB, 0 = kRawRGB
var width = readUint32(reader);
var height = readUint32(reader);
readUint32(reader); // widthBytes = (width * bits_per_pixel + 31) / 32 * 4.
readUint32(reader); // totalSize = widthBytes * height * planes
readUint32(reader); // sizeAfterCompression
var bitsPerPixel = readUint16(reader); // 24
var planes = readUint16(reader); // 1
if (format !== 1 || bitsPerPixel !== 24 || planes !== 1) {
options.logMissingFeatures && console.log("Invalid thumbnail data (format: ".concat(format, ", bitsPerPixel: ").concat(bitsPerPixel, ", planes: ").concat(planes, ")"));
skipBytes(reader, left());
return;
}
var size = left();
var data = readBytes(reader, size);
if (options.useRawThumbnail) {
target.thumbnailRaw = { width: width, height: height, data: data };
}
else if (data.byteLength) {
target.thumbnail = createCanvasFromData(data);
}
}, function (writer, target) {
var _a;
var width = 0;
var height = 0;
var data;
if (target.thumbnailRaw) {
width = target.thumbnailRaw.width;
height = target.thumbnailRaw.height;
data = target.thumbnailRaw.data;
}
else {
var dataUrl = (_a = target.thumbnail.toDataURL('image/jpeg', 1)) === null || _a === void 0 ? void 0 : _a.substring('data:image/jpeg;base64,'.length);
if (dataUrl) {
width = target.thumbnail.width;
height = target.thumbnail.height;
data = toByteArray(dataUrl);
}
else {
data = new Uint8Array(0);
}
}
var bitsPerPixel = 24;
var widthBytes = Math.floor((width * bitsPerPixel + 31) / 32) * 4;
var planes = 1;
var totalSize = widthBytes * height * planes;
var sizeAfterCompression = data.length;
writeUint32(writer, 1); // 1 = kJpegRGB
writeUint32(writer, width);
writeUint32(writer, height);
writeUint32(writer, widthBytes);
writeUint32(writer, totalSize);
writeUint32(writer, sizeAfterCompression);
writeUint16(writer, bitsPerPixel);
writeUint16(writer, planes);
writeBytes(writer, data);
});
addHandler(1057, function (target) { return target.versionInfo !== undefined; }, function (reader, target, left) {
var version = readUint32(reader);
if (version !== 1)
throw new Error('Invalid versionInfo version');
target.versionInfo = {
hasRealMergedData: !!readUint8(reader),
writerName: readUnicodeString(reader),
readerName: readUnicodeString(reader),
fileVersion: readUint32(reader),
};
skipBytes(reader, left());
}, function (writer, target) {
var versionInfo = target.versionInfo;
writeUint32(writer, 1); // version
writeUint8(writer, versionInfo.hasRealMergedData ? 1 : 0);
writeUnicodeString(writer, versionInfo.writerName);
writeUnicodeString(writer, versionInfo.readerName);
writeUint32(writer, versionInfo.fileVersion);
});
MOCK_HANDLERS && addHandler(1058, // EXIF data 1.
function (// EXIF data 1.
target) { return target._ir1058 !== undefined; }, function (reader, target, left) {
LOG_MOCK_HANDLERS && console.log('image resource 1058', left());
target._ir1058 = readBytes(reader, left());
}, function (writer, target) {
writeBytes(writer, target._ir1058);
});
addHandler(7000, function (target) { return target.imageReadyVariables !== undefined; }, function (reader, target, left) {
target.imageReadyVariables = readUtf8String(reader, left());
}, function (writer, target) {
writeUtf8String(writer, target.imageReadyVariables);
});
addHandler(7001, function (target) { return target.imageReadyDataSets !== undefined; }, function (reader, target, left) {
target.imageReadyDataSets = readUtf8String(reader, left());
}, function (writer, target) {
writeUtf8String(writer, target.imageReadyDataSets);
});
addHandler(1088, function (target) { return target.pathSelectionState !== undefined; }, function (reader, target, _left) {
var desc = readVersionAndDescriptor(reader);
// console.log(require('util').inspect(desc, false, 99, true));
target.pathSelectionState = desc['null'];
}, function (writer, target) {
var desc = { 'null': target.pathSelectionState };
writeVersionAndDescriptor(writer, '', 'null', desc);
});
MOCK_HANDLERS && addHandler(1025, function (target) { return target._ir1025 !== undefined; }, function (reader, target, left) {
LOG_MOCK_HANDLERS && console.log('image resource 1025', left());
target._ir1025 = readBytes(reader, left());
}, function (writer, target) {
writeBytes(writer, target._ir1025);
});
var FrmD = createEnum('FrmD', '', {
auto: 'Auto',
none: 'None',
dispose: 'Disp',
});
addHandler(4000, // Plug-In resource(s)
function (// Plug-In resource(s)
target) { return target.animations !== undefined; }, function (reader, target, left, _a) {
var logMissingFeatures = _a.logMissingFeatures, logDevFeatures = _a.logDevFeatures;
var key = readSignature(reader);
if (key === 'mani') {
checkSignature(reader, 'IRFR');
readSection(reader, 1, function (left) {
var _loop_1 = function () {
checkSignature(reader, '8BIM');
var key_1 = readSignature(reader);
readSection(reader, 1, function (left) {
if (key_1 === 'AnDs') {
var desc = readVersionAndDescriptor(reader);
target.animations = {
// desc.AFSt ???
frames: desc.FrIn.map(function (x) { return ({
id: x.FrID,
delay: (x.FrDl || 0) / 100,
dispose: x.FrDs ? FrmD.decode(x.FrDs) : 'auto', // missing == auto
// x.FrGA ???
}); }),
animations: desc.FSts.map(function (x) { return ({
id: x.FsID,
frames: x.FsFr,
repeats: x.LCnt,
activeFrame: x.AFrm || 0,
}); }),
};
// console.log('#4000 AnDs', require('util').inspect(desc, false, 99, true));
// console.log('#4000 AnDs:result', require('util').inspect(target.animations, false, 99, true));
}
else if (key_1 === 'Roll') {
var bytes = readBytes(reader, left());
logDevFeatures && console.log('#4000 Roll', bytes);
}
else {
logMissingFeatures && console.log('Unhandled subsection in #4000', key_1);
}
});
};
while (left()) {
_loop_1();
}
});
}
else if (key === 'mopt') {
var bytes = readBytes(reader, left());
logDevFeatures && console.log('#4000 mopt', bytes);
}
else {
logMissingFeatures && console.log('Unhandled key in #4000:', key);
}
}, function (writer, target) {
if (target.animations) {
writeSignature(writer, 'mani');
writeSignature(writer, 'IRFR');
writeSection(writer, 1, function () {
writeSignature(writer, '8BIM');
writeSignature(writer, 'AnDs');
writeSection(writer, 1, function () {
var desc = {
// AFSt: 0, // ???
FrIn: [],
FSts: [],
};
for (var i = 0; i < target.animations.frames.length; i++) {
var f = target.animations.frames[i];
var frame = {
FrID: f.id,
};
if (f.delay)
frame.FrDl = (f.delay * 100) | 0;
frame.FrDs = FrmD.encode(f.dispose);
// if (i === 0) frame.FrGA = 30; // ???
desc.FrIn.push(frame);
}
for (var i = 0; i < target.animations.animations.length; i++) {
var a = target.animations.animations[i];
var anim = {
FsID: a.id,
AFrm: a.activeFrame | 0,
FsFr: a.frames,
LCnt: a.repeats | 0,
};
desc.FSts.push(anim);
}
writeVersionAndDescriptor(writer, '', 'null', desc);
});
// writeSignature(writer, '8BIM');
// writeSignature(writer, 'Roll');
// writeSection(writer, 1, () => {
// writeZeros(writer, 8);
// });
});
}
});
// TODO: Unfinished
MOCK_HANDLERS && addHandler(4001, // Plug-In resource(s)
function (// Plug-In resource(s)
target) { return target._ir4001 !== undefined; }, function (reader, target, left, _a) {
var logMissingFeatures = _a.logMissingFeatures, logDevFeatures = _a.logDevFeatures;
if (MOCK_HANDLERS) {
LOG_MOCK_HANDLERS && console.log('image resource 4001', left());
target._ir4001 = readBytes(reader, left());
return;
}
var key = readSignature(reader);
if (key === 'mfri') {
var version = readUint32(reader);
if (version !== 2)
throw new Error('Invalid mfri version');
var length_1 = readUint32(reader);
var bytes = readBytes(reader, length_1);
logDevFeatures && console.log('mfri', bytes);
}
else if (key === 'mset') {
var desc = readVersionAndDescriptor(reader);
logDevFeatures && console.log('mset', desc);
}
else {
logMissingFeatures && console.log('Unhandled key in #4001', key);
}
}, function (writer, target) {
writeBytes(writer, target._ir4001);
});
// TODO: Unfinished
MOCK_HANDLERS && addHandler(4002, // Plug-In resource(s)
function (// Plug-In resource(s)
target) { return target._ir4002 !== undefined; }, function (reader, target, left) {
LOG_MOCK_HANDLERS && console.log('image resource 4002', left());
target._ir4002 = readBytes(reader, left());
}, function (writer, target) {
writeBytes(writer, target._ir4002);
});
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImltYWdlUmVzb3VyY2VzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxXQUFXLEVBQUUsTUFBTSxXQUFXLENBQUM7QUFFeEMsT0FBTyxFQUNLLGdCQUFnQixFQUFFLGlCQUFpQixFQUFFLFVBQVUsRUFBRSxVQUFVLEVBQUUsU0FBUyxFQUFFLFdBQVcsRUFDOUYsU0FBUyxFQUFFLFNBQVMsRUFBRSxXQUFXLEVBQUUsU0FBUyxFQUFFLGdCQUFnQixFQUFFLGFBQWEsRUFBRSxjQUFjLEVBQzdGLFdBQVcsRUFBRSxTQUFTLEVBQ3RCLE1BQU0sYUFBYSxDQUFDO0FBQ3JCLE9BQU8sRUFDSyxpQkFBaUIsRUFBRSxrQkFBa0IsRUFBRSxXQUFXLEVBQUUsVUFBVSxFQUFFLFlBQVksRUFBRSxXQUFXLEVBQ3BHLFVBQVUsRUFBRSxVQUFVLEVBQUUsWUFBWSxFQUFFLGlCQUFpQixFQUFFLDZCQUE2QixFQUFFLFVBQVUsRUFBRSxjQUFjLEVBQ2xILFlBQVksR0FDWixNQUFNLGFBQWEsQ0FBQztBQUNyQixPQUFPLEVBQUUsb0JBQW9CLEVBQUUsVUFBVSxFQUFFLGFBQWEsRUFBRSx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