"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.resourceHandlersMap = exports.resourceHandlers = void 0; var base64_js_1 = require("base64-js"); var psdReader_1 = require("./psdReader"); var psdWriter_1 = require("./psdWriter"); var helpers_1 = require("./helpers"); var utf8_1 = require("./utf8"); var descriptor_1 = require("./descriptor"); exports.resourceHandlers = []; exports.resourceHandlersMap = {}; function addHandler(key, has, read, write) { var handler = { key: key, has: has, read: read, write: write }; exports.resourceHandlers.push(handler); exports.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 = (0, psdReader_1.readBytes)(reader, length); return (0, utf8_1.decodeString)(buffer); } function writeUtf8String(writer, value) { var buffer = (0, utf8_1.encodeString)(value); (0, psdWriter_1.writeBytes)(writer, buffer); } helpers_1.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 = (0, psdReader_1.readBytes)(reader, left()); }, function (writer, target) { (0, psdWriter_1.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 = (0, psdReader_1.readUint8)(reader); captionDigest += hex[byte >> 4]; captionDigest += hex[byte & 0xf]; } target.captionDigest = captionDigest; }, function (writer, target) { for (var i = 0; i < 16; i++) { (0, psdWriter_1.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 = (0, helpers_1.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 = (0, descriptor_1.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', }; } (0, descriptor_1.writeVersionAndDescriptor)(writer, '', 'printOutput', desc); }); helpers_1.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 = (0, psdReader_1.readBytes)(reader, left()); // TODO: // const desc = readVersionAndDescriptor(reader); // console.log('1083', require('util').inspect(desc, false, 99, true)); }, function (writer, target) { (0, psdWriter_1.writeBytes)(writer, target._ir1083); }); addHandler(1005, function (target) { return target.resolutionInfo !== undefined; }, function (reader, target) { var horizontalResolution = (0, psdReader_1.readFixedPoint32)(reader); var horizontalResolutionUnit = (0, psdReader_1.readUint16)(reader); var widthUnit = (0, psdReader_1.readUint16)(reader); var verticalResolution = (0, psdReader_1.readFixedPoint32)(reader); var verticalResolutionUnit = (0, psdReader_1.readUint16)(reader); var heightUnit = (0, psdReader_1.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; (0, psdWriter_1.writeFixedPoint32)(writer, info.horizontalResolution || 0); (0, psdWriter_1.writeUint16)(writer, Math.max(1, RESOLUTION_UNITS.indexOf(info.horizontalResolutionUnit))); (0, psdWriter_1.writeUint16)(writer, Math.max(1, MEASUREMENT_UNITS.indexOf(info.widthUnit))); (0, psdWriter_1.writeFixedPoint32)(writer, info.verticalResolution || 0); (0, psdWriter_1.writeUint16)(writer, Math.max(1, RESOLUTION_UNITS.indexOf(info.verticalResolutionUnit))); (0, psdWriter_1.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[(0, psdReader_1.readInt16)(reader)], x: (0, psdReader_1.readFloat32)(reader), y: (0, psdReader_1.readFloat32)(reader), scale: (0, psdReader_1.readFloat32)(reader), }; }, function (writer, target) { var _a = target.printScale, style = _a.style, x = _a.x, y = _a.y, scale = _a.scale; (0, psdWriter_1.writeInt16)(writer, Math.max(0, printScaleStyles.indexOf(style))); (0, psdWriter_1.writeFloat32)(writer, x || 0); (0, psdWriter_1.writeFloat32)(writer, y || 0); (0, psdWriter_1.writeFloat32)(writer, scale || 0); }); addHandler(1006, function (target) { return target.alphaChannelNames !== undefined; }, function (reader, target, left) { target.alphaChannelNames = []; while (left()) { var value = (0, psdReader_1.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]; (0, psdWriter_1.writePascalString)(writer, name_1, 1); } }); addHandler(1045, function (target) { return target.alphaChannelNames !== undefined; }, function (reader, target, left) { target.alphaChannelNames = []; while (left()) { target.alphaChannelNames.push((0, psdReader_1.readUnicodeString)(reader)); } }, function (writer, target) { for (var _i = 0, _a = target.alphaChannelNames; _i < _a.length; _i++) { var name_2 = _a[_i]; (0, psdWriter_1.writeUnicodeStringWithPadding)(writer, name_2); } }); helpers_1.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 = (0, psdReader_1.readBytes)(reader, left()); }, function (writer, target) { (0, psdWriter_1.writeBytes)(writer, target._ir1077); }); addHandler(1053, function (target) { return target.alphaIdentifiers !== undefined; }, function (reader, target, left) { target.alphaIdentifiers = []; while (left() >= 4) { target.alphaIdentifiers.push((0, psdReader_1.readUint32)(reader)); } }, function (writer, target) { for (var _i = 0, _a = target.alphaIdentifiers; _i < _a.length; _i++) { var id = _a[_i]; (0, psdWriter_1.writeUint32)(writer, id); } }); addHandler(1010, function (target) { return target.backgroundColor !== undefined; }, function (reader, target) { return target.backgroundColor = (0, psdReader_1.readColor)(reader); }, function (writer, target) { return (0, psdWriter_1.writeColor)(writer, target.backgroundColor); }); addHandler(1037, function (target) { return target.globalAngle !== undefined; }, function (reader, target) { return target.globalAngle = (0, psdReader_1.readUint32)(reader); }, function (writer, target) { return (0, psdWriter_1.writeUint32)(writer, target.globalAngle); }); addHandler(1049, function (target) { return target.globalAltitude !== undefined; }, function (reader, target) { return target.globalAltitude = (0, psdReader_1.readUint32)(reader); }, function (writer, target) { return (0, psdWriter_1.writeUint32)(writer, target.globalAltitude); }); addHandler(1011, function (target) { return target.printFlags !== undefined; }, function (reader, target) { target.printFlags = { labels: !!(0, psdReader_1.readUint8)(reader), cropMarks: !!(0, psdReader_1.readUint8)(reader), colorBars: !!(0, psdReader_1.readUint8)(reader), registrationMarks: !!(0, psdReader_1.readUint8)(reader), negative: !!(0, psdReader_1.readUint8)(reader), flip: !!(0, psdReader_1.readUint8)(reader), interpolate: !!(0, psdReader_1.readUint8)(reader), caption: !!(0, psdReader_1.readUint8)(reader), printFlags: !!(0, psdReader_1.readUint8)(reader), }; }, function (writer, target) { var flags = target.printFlags; (0, psdWriter_1.writeUint8)(writer, flags.labels ? 1 : 0); (0, psdWriter_1.writeUint8)(writer, flags.cropMarks ? 1 : 0); (0, psdWriter_1.writeUint8)(writer, flags.colorBars ? 1 : 0); (0, psdWriter_1.writeUint8)(writer, flags.registrationMarks ? 1 : 0); (0, psdWriter_1.writeUint8)(writer, flags.negative ? 1 : 0); (0, psdWriter_1.writeUint8)(writer, flags.flip ? 1 : 0); (0, psdWriter_1.writeUint8)(writer, flags.interpolate ? 1 : 0); (0, psdWriter_1.writeUint8)(writer, flags.caption ? 1 : 0); (0, psdWriter_1.writeUint8)(writer, flags.printFlags ? 1 : 0); }); helpers_1.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 = (0, psdReader_1.readBytes)(reader, left()); }, function (writer, target) { (0, psdWriter_1.writeBytes)(writer, target._ir10000); }); helpers_1.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 = (0, psdReader_1.readBytes)(reader, left()); }, function (writer, target) { (0, psdWriter_1.writeBytes)(writer, target._ir1013); }); helpers_1.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 = (0, psdReader_1.readBytes)(reader, left()); }, function (writer, target) { (0, psdWriter_1.writeBytes)(writer, target._ir1016); }); addHandler(1080, // Count Information function (// Count Information target) { return target.countInformation !== undefined; }, function (reader, target) { var desc = (0, descriptor_1.readVersionAndDescriptor)(reader); target.countInformation = desc.countGroupList.map(function (g) { return ({ color: { r: g['Rd '], g: g['Grn '], b: g['Bl '] }, name: g['Nm '], size: g['Rds '], fontSize: g.fontSize, visible: g.Vsbl, points: g.countObjectList.map(function (p) { return ({ x: p['X '], y: p['Y '] }); }), }); }); }, function (writer, target) { var desc = { Vrsn: 1, countGroupList: target.countInformation.map(function (g) { return ({ 'Rd ': g.color.r, 'Grn ': g.color.g, 'Bl ': g.color.b, 'Nm ': g.name, 'Rds ': g.size, fontSize: g.fontSize, Vsbl: g.visible, countObjectList: g.points.map(function (p) { return ({ 'X ': p.x, 'Y ': p.y }); }), }); }), }; (0, descriptor_1.writeVersionAndDescriptor)(writer, '', 'Cnt ', desc); }); addHandler(1024, function (target) { return target.layerState !== undefined; }, function (reader, target) { return target.layerState = (0, psdReader_1.readUint16)(reader); }, function (writer, target) { return (0, psdWriter_1.writeUint16)(writer, target.layerState); }); addHandler(1026, function (target) { return target.layersGroup !== undefined; }, function (reader, target, left) { target.layersGroup = []; while (left()) { target.layersGroup.push((0, psdReader_1.readUint16)(reader)); } }, function (writer, target) { for (var _i = 0, _a = target.layersGroup; _i < _a.length; _i++) { var g = _a[_i]; (0, psdWriter_1.writeUint16)(writer, g); } }); addHandler(1072, function (target) { return target.layerGroupsEnabledId !== undefined; }, function (reader, target, left) { target.layerGroupsEnabledId = []; while (left()) { target.layerGroupsEnabledId.push((0, psdReader_1.readUint8)(reader)); } }, function (writer, target) { for (var _i = 0, _a = target.layerGroupsEnabledId; _i < _a.length; _i++) { var id = _a[_i]; (0, psdWriter_1.writeUint8)(writer, id); } }); addHandler(1069, function (target) { return target.layerSelectionIds !== undefined; }, function (reader, target) { var count = (0, psdReader_1.readUint16)(reader); target.layerSelectionIds = []; while (count--) { target.layerSelectionIds.push((0, psdReader_1.readUint32)(reader)); } }, function (writer, target) { (0, psdWriter_1.writeUint16)(writer, target.layerSelectionIds.length); for (var _i = 0, _a = target.layerSelectionIds; _i < _a.length; _i++) { var id = _a[_i]; (0, psdWriter_1.writeUint32)(writer, id); } }); addHandler(1032, function (target) { return target.gridAndGuidesInformation !== undefined; }, function (reader, target) { var version = (0, psdReader_1.readUint32)(reader); var horizontal = (0, psdReader_1.readUint32)(reader); var vertical = (0, psdReader_1.readUint32)(reader); var count = (0, psdReader_1.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: (0, psdReader_1.readUint32)(reader) / 32, direction: (0, psdReader_1.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 || []; (0, psdWriter_1.writeUint32)(writer, 1); (0, psdWriter_1.writeUint32)(writer, grid.horizontal); (0, psdWriter_1.writeUint32)(writer, grid.vertical); (0, psdWriter_1.writeUint32)(writer, guides.length); for (var _i = 0, guides_1 = guides; _i < guides_1.length; _i++) { var g = guides_1[_i]; (0, psdWriter_1.writeUint32)(writer, g.location * 32); (0, psdWriter_1.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 = (0, descriptor_1.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)), }; (0, descriptor_1.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 = (0, descriptor_1.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: (0, descriptor_1.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: (0, descriptor_1.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)); (0, descriptor_1.writeVersionAndDescriptor)(writer, '', 'null', desc, 'anim'); }); addHandler(1076, // Sheet Disclosure function (// Sheet Disclosure target) { return target.sheetDisclosure !== undefined; }, function (reader, target) { var desc = (0, descriptor_1.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, }); }); } (0, descriptor_1.writeVersionAndDescriptor)(writer, '', 'null', desc); }); addHandler(1054, // URL List function (// URL List target) { return target.urlsList !== undefined; }, function (reader, target, _, options) { var count = (0, psdReader_1.readUint32)(reader); target.urlsList = []; for (var i = 0; i < count; i++) { var long = (0, psdReader_1.readSignature)(reader); if (long !== 'slic' && options.throwForMissingFeatures) throw new Error('Unknown long'); var id = (0, psdReader_1.readUint32)(reader); var url = (0, psdReader_1.readUnicodeString)(reader); target.urlsList.push({ id: id, url: url, ref: 'slice' }); } }, function (writer, target) { var list = target.urlsList; (0, psdWriter_1.writeUint32)(writer, list.length); for (var i = 0; i < list.length; i++) { (0, psdWriter_1.writeSignature)(writer, 'slic'); (0, psdWriter_1.writeUint32)(writer, list[i].id); (0, psdWriter_1.writeUnicodeString)(writer, list[i].url); } }); function boundsToBounds(bounds) { return { 'Top ': bounds.top, Left: bounds.left, Btom: bounds.bottom, Rght: bounds.right }; } function boundsFromBounds(bounds) { return { top: bounds['Top '], left: bounds.Left, bottom: bounds.Btom, right: bounds.Rght }; } function clamped(array, index) { return array[Math.max(0, Math.min(array.length - 1, index))]; } var sliceOrigins = ['autoGenerated', 'layer', 'userGenerated']; var sliceTypes = ['noImage', 'image']; var sliceAlignments = ['default']; addHandler(1050, // Slices function (// Slices target) { return target.slices ? target.slices.length : 0; }, function (reader, target) { var version = (0, psdReader_1.readUint32)(reader); if (version == 6) { if (!target.slices) target.slices = []; var top_1 = (0, psdReader_1.readInt32)(reader); var left = (0, psdReader_1.readInt32)(reader); var bottom = (0, psdReader_1.readInt32)(reader); var right = (0, psdReader_1.readInt32)(reader); var groupName = (0, psdReader_1.readUnicodeString)(reader); var count = (0, psdReader_1.readUint32)(reader); target.slices.push({ bounds: { top: top_1, left: left, bottom: bottom, right: right }, groupName: groupName, slices: [] }); var slices_1 = target.slices[target.slices.length - 1].slices; for (var i = 0; i < count; i++) { var id = (0, psdReader_1.readUint32)(reader); var groupId = (0, psdReader_1.readUint32)(reader); var origin_1 = clamped(sliceOrigins, (0, psdReader_1.readUint32)(reader)); var associatedLayerId = origin_1 == 'layer' ? (0, psdReader_1.readUint32)(reader) : 0; var name_3 = (0, psdReader_1.readUnicodeString)(reader); var type = clamped(sliceTypes, (0, psdReader_1.readUint32)(reader)); var top_2 = (0, psdReader_1.readInt32)(reader); var left_1 = (0, psdReader_1.readInt32)(reader); var bottom_1 = (0, psdReader_1.readInt32)(reader); var right_1 = (0, psdReader_1.readInt32)(reader); var url = (0, psdReader_1.readUnicodeString)(reader); var target_1 = (0, psdReader_1.readUnicodeString)(reader); var message = (0, psdReader_1.readUnicodeString)(reader); var altTag = (0, psdReader_1.readUnicodeString)(reader); var cellTextIsHTML = !!(0, psdReader_1.readUint8)(reader); var cellText = (0, psdReader_1.readUnicodeString)(reader); var horizontalAlignment = clamped(sliceAlignments, (0, psdReader_1.readUint32)(reader)); var verticalAlignment = clamped(sliceAlignments, (0, psdReader_1.readUint32)(reader)); var a = (0, psdReader_1.readUint8)(reader); var r = (0, psdReader_1.readUint8)(reader); var g = (0, psdReader_1.readUint8)(reader); var b = (0, psdReader_1.readUint8)(reader); var backgroundColorType = ((a + r + g + b) === 0) ? 'none' : (a === 0 ? 'matte' : 'color'); slices_1.push({ id: id, groupId: groupId, origin: origin_1, associatedLayerId: associatedLayerId, name: name_3, target: target_1, message: message, altTag: altTag, cellTextIsHTML: cellTextIsHTML, cellText: cellText, horizontalAlignment: horizontalAlignment, verticalAlignment: verticalAlignment, type: type, url: url, bounds: { top: top_2, left: left_1, bottom: bottom_1, right: right_1 }, backgroundColorType: backgroundColorType, backgroundColor: { r: r, g: g, b: b, a: a }, }); // console.log(require('util').inspect(slices[slices.length - 1], false, 99, true)); } var desc = (0, descriptor_1.readVersionAndDescriptor)(reader); desc.slices.forEach(function (d) { var slice = slices_1.find(function (s) { return d.sliceID == s.id; }); if (slice) { slice.topOutset = d.topOutset; slice.leftOutset = d.leftOutset; slice.bottomOutset = d.bottomOutset; slice.rightOutset = d.rightOutset; } }); // console.log(require('util').inspect(desc, false, 99, true)); // console.log(require('util').inspect(target.slices, false, 99, true)); } else if (version == 7 || version == 8) { var desc = (0, descriptor_1.readVersionAndDescriptor)(reader); // console.log(require('util').inspect(desc, false, 99, true)); if (!target.slices) target.slices = []; target.slices.push({ groupName: desc.baseName, bounds: boundsFromBounds(desc.bounds), slices: desc.slices.map(function (s) { return ({ name: '', id: s.sliceID, groupId: s.groupID, associatedLayerId: 0, origin: descriptor_1.ESliceOrigin.decode(s.origin), type: descriptor_1.ESliceType.decode(s.Type), bounds: boundsFromBounds(s.bounds), url: s.url, target: s.null, message: s.Msge, altTag: s.altTag, cellTextIsHTML: s.cellTextIsHTML, cellText: s.cellText, horizontalAlignment: descriptor_1.ESliceHorzAlign.decode(s.horzAlign), verticalAlignment: descriptor_1.ESliceVertAlign.decode(s.vertAlign), backgroundColorType: descriptor_1.ESliceBGColorType.decode(s.bgColorType), backgroundColor: s.bgColor ? { r: s.bgColor['Rd '], g: s.bgColor['Grn '], b: s.bgColor['Bl '], a: s.bgColor.alpha } : { r: 0, g: 0, b: 0, a: 0 }, topOutset: s.topOutset || 0, leftOutset: s.leftOutset || 0, bottomOutset: s.bottomOutset || 0, rightOutset: s.rightOutset || 0, }); }), }); } else { throw new Error("Invalid slices version (".concat(version, ")")); } }, function (writer, target, index) { var _a = target.slices[index], bounds = _a.bounds, groupName = _a.groupName, slices = _a.slices; (0, psdWriter_1.writeUint32)(writer, 6); // version (0, psdWriter_1.writeInt32)(writer, bounds.top); (0, psdWriter_1.writeInt32)(writer, bounds.left); (0, psdWriter_1.writeInt32)(writer, bounds.bottom); (0, psdWriter_1.writeInt32)(writer, bounds.right); (0, psdWriter_1.writeUnicodeString)(writer, groupName); (0, psdWriter_1.writeUint32)(writer, slices.length); for (var i = 0; i < slices.length; i++) { var slice = slices[i]; var _b = slice.backgroundColor, a = _b.a, r = _b.r, g = _b.g, b = _b.b; if (slice.backgroundColorType === 'none') { a = r = g = b = 0; } else if (slice.backgroundColorType === 'matte') { a = 0; r = g = b = 255; } (0, psdWriter_1.writeUint32)(writer, slice.id); (0, psdWriter_1.writeUint32)(writer, slice.groupId); (0, psdWriter_1.writeUint32)(writer, sliceOrigins.indexOf(slice.origin)); if (slice.origin === 'layer') (0, psdWriter_1.writeUint32)(writer, slice.associatedLayerId); (0, psdWriter_1.writeUnicodeString)(writer, slice.name); (0, psdWriter_1.writeUint32)(writer, sliceTypes.indexOf(slice.type)); (0, psdWriter_1.writeInt32)(writer, slice.bounds.top); (0, psdWriter_1.writeInt32)(writer, slice.bounds.left); (0, psdWriter_1.writeInt32)(writer, slice.bounds.bottom); (0, psdWriter_1.writeInt32)(writer, slice.bounds.right); (0, psdWriter_1.writeUnicodeString)(writer, slice.url); (0, psdWriter_1.writeUnicodeString)(writer, slice.target); (0, psdWriter_1.writeUnicodeString)(writer, slice.message); (0, psdWriter_1.writeUnicodeString)(writer, slice.altTag); (0, psdWriter_1.writeUint8)(writer, slice.cellTextIsHTML ? 1 : 0); (0, psdWriter_1.writeUnicodeString)(writer, slice.cellText); (0, psdWriter_1.writeUint32)(writer, sliceAlignments.indexOf(slice.horizontalAlignment)); (0, psdWriter_1.writeUint32)(writer, sliceAlignments.indexOf(slice.verticalAlignment)); (0, psdWriter_1.writeUint8)(writer, a); (0, psdWriter_1.writeUint8)(writer, r); (0, psdWriter_1.writeUint8)(writer, g); (0, psdWriter_1.writeUint8)(writer, b); } var desc = { bounds: boundsToBounds(bounds), slices: [], }; slices.forEach(function (s) { var slice = __assign(__assign({ sliceID: s.id, groupID: s.groupId, origin: descriptor_1.ESliceOrigin.encode(s.origin), Type: descriptor_1.ESliceType.encode(s.type), bounds: boundsToBounds(s.bounds) }, (s.name ? { 'Nm ': s.name } : {})), { url: s.url, null: s.target, Msge: s.message, altTag: s.altTag, cellTextIsHTML: s.cellTextIsHTML, cellText: s.cellText, horzAlign: descriptor_1.ESliceHorzAlign.encode(s.horizontalAlignment), vertAlign: descriptor_1.ESliceVertAlign.encode(s.verticalAlignment), bgColorType: descriptor_1.ESliceBGColorType.encode(s.backgroundColorType) }); if (s.backgroundColorType === 'color') { var _a = s.backgroundColor, r = _a.r, g = _a.g, b = _a.b, a = _a.a; slice.bgColor = { 'Rd ': r, 'Grn ': g, 'Bl ': b, alpha: a }; } slice.topOutset = s.topOutset || 0; slice.leftOutset = s.leftOutset || 0; slice.bottomOutset = s.bottomOutset || 0; slice.rightOutset = s.rightOutset || 0; desc.slices.push(slice); }); (0, descriptor_1.writeVersionAndDescriptor)(writer, '', 'null', desc, 'slices'); }); addHandler(1064, function (target) { return target.pixelAspectRatio !== undefined; }, function (reader, target) { if ((0, psdReader_1.readUint32)(reader) > 2) throw new Error('Invalid pixelAspectRatio version'); target.pixelAspectRatio = { aspect: (0, psdReader_1.readFloat64)(reader) }; }, function (writer, target) { (0, psdWriter_1.writeUint32)(writer, 2); // version (0, psdWriter_1.writeFloat64)(writer, target.pixelAspectRatio.aspect); }); addHandler(1041, function (target) { return target.iccUntaggedProfile !== undefined; }, function (reader, target) { target.iccUntaggedProfile = !!(0, psdReader_1.readUint8)(reader); }, function (writer, target) { (0, psdWriter_1.writeUint8)(writer, target.iccUntaggedProfile ? 1 : 0); }); helpers_1.MOCK_HANDLERS && addHandler(1039, // ICC Profile function (// ICC Profile target) { return target._ir1039 !== undefined; }, function (reader, target, left) { // TODO: this is raw bytes, just return as a byte array LOG_MOCK_HANDLERS && console.log('image resource 1039', left()); target._ir1039 = (0, psdReader_1.readBytes)(reader, left()); }, function (writer, target) { (0, psdWriter_1.writeBytes)(writer, target._ir1039); }); addHandler(1044, function (target) { return target.idsSeedNumber !== undefined; }, function (reader, target) { return target.idsSeedNumber = (0, psdReader_1.readUint32)(reader); }, function (writer, target) { return (0, psdWriter_1.writeUint32)(writer, target.idsSeedNumber); }); addHandler(1036, function (target) { return target.thumbnail !== undefined || target.thumbnailRaw !== undefined; }, function (reader, target, left, options) { var format = (0, psdReader_1.readUint32)(reader); // 1 = kJpegRGB, 0 = kRawRGB var width = (0, psdReader_1.readUint32)(reader); var height = (0, psdReader_1.readUint32)(reader); (0, psdReader_1.readUint32)(reader); // widthBytes = (width * bits_per_pixel + 31) / 32 * 4. (0, psdReader_1.readUint32)(reader); // totalSize = widthBytes * height * planes (0, psdReader_1.readUint32)(reader); // sizeAfterCompression var bitsPerPixel = (0, psdReader_1.readUint16)(reader); // 24 var planes = (0, psdReader_1.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, ")")); (0, psdReader_1.skipBytes)(reader, left()); return; } var size = left(); var data = (0, psdReader_1.readBytes)(reader, size); if (options.useRawThumbnail) { target.thumbnailRaw = { width: width, height: height, data: data }; } else if (data.byteLength) { target.thumbnail = (0, helpers_1.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 = (0, base64_js_1.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; (0, psdWriter_1.writeUint32)(writer, 1); // 1 = kJpegRGB (0, psdWriter_1.writeUint32)(writer, width); (0, psdWriter_1.writeUint32)(writer, height); (0, psdWriter_1.writeUint32)(writer, widthBytes); (0, psdWriter_1.writeUint32)(writer, totalSize); (0, psdWriter_1.writeUint32)(writer, sizeAfterCompression); (0, psdWriter_1.writeUint16)(writer, bitsPerPixel); (0, psdWriter_1.writeUint16)(writer, planes); (0, psdWriter_1.writeBytes)(writer, data); }); addHandler(1057, function (target) { return target.versionInfo !== undefined; }, function (reader, target, left) { var version = (0, psdReader_1.readUint32)(reader); if (version !== 1) throw new Error('Invalid versionInfo version'); target.versionInfo = { hasRealMergedData: !!(0, psdReader_1.readUint8)(reader), writerName: (0, psdReader_1.readUnicodeString)(reader), readerName: (0, psdReader_1.readUnicodeString)(reader), fileVersion: (0, psdReader_1.readUint32)(reader), }; (0, psdReader_1.skipBytes)(reader, left()); }, function (writer, target) { var versionInfo = target.versionInfo; (0, psdWriter_1.writeUint32)(writer, 1); // version (0, psdWriter_1.writeUint8)(writer, versionInfo.hasRealMergedData ? 1 : 0); (0, psdWriter_1.writeUnicodeString)(writer, versionInfo.writerName); (0, psdWriter_1.writeUnicodeString)(writer, versionInfo.readerName); (0, psdWriter_1.writeUint32)(writer, versionInfo.fileVersion); }); helpers_1.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 = (0, psdReader_1.readBytes)(reader, left()); }, function (writer, target) { (0, psdWriter_1.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 = (0, descriptor_1.readVersionAndDescriptor)(reader); // console.log(require('util').inspect(desc, false, 99, true)); target.pathSelectionState = desc['null']; }, function (writer, target) { var desc = { 'null': target.pathSelectionState }; (0, descriptor_1.writeVersionAndDescriptor)(writer, '', 'null', desc); }); helpers_1.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 = (0, psdReader_1.readBytes)(reader, left()); }, function (writer, target) { (0, psdWriter_1.writeBytes)(writer, target._ir1025); }); var FrmD = (0, helpers_1.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 = (0, psdReader_1.readSignature)(reader); if (key === 'mani') { (0, psdReader_1.checkSignature)(reader, 'IRFR'); (0, psdReader_1.readSection)(reader, 1, function (left) { var _loop_1 = function () { (0, psdReader_1.checkSignature)(reader, '8BIM'); var key_1 = (0, psdReader_1.readSignature)(reader); (0, psdReader_1.readSection)(reader, 1, function (left) { if (key_1 === 'AnDs') { var desc = (0, descriptor_1.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 = (0, psdReader_1.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 = (0, psdReader_1.readBytes)(reader, left()); logDevFeatures && console.log('#4000 mopt', bytes); } else { logMissingFeatures && console.log('Unhandled key in #4000:', key); } }, function (writer, target) { if (target.animations) { (0, psdWriter_1.writeSignature)(writer, 'mani'); (0, psdWriter_1.writeSignature)(writer, 'IRFR'); (0, psdWriter_1.writeSection)(writer, 1, function () { (0, psdWriter_1.writeSignature)(writer, '8BIM'); (0, psdWriter_1.writeSignature)(writer, 'AnDs'); (0, psdWriter_1.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); } (0, descriptor_1.writeVersionAndDescriptor)(writer, '', 'null', desc); }); // writeSignature(writer, '8BIM'); // writeSignature(writer, 'Roll'); // writeSection(writer, 1, () => { // writeZeros(writer, 8); // }); }); } }); // TODO: Unfinished helpers_1.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 (helpers_1.MOCK_HANDLERS) { LOG_MOCK_HANDLERS && console.log('image resource 4001', left()); target._ir4001 = (0, psdReader_1.readBytes)(reader, left()); return; } var key = (0, psdReader_1.readSignature)(reader); if (key === 'mfri') { var version = (0, psdReader_1.readUint32)(reader); if (version !== 2) throw new Error('Invalid mfri version'); var length_1 = (0, psdReader_1.readUint32)(reader); var bytes = (0, psdReader_1.readBytes)(reader, length_1); logDevFeatures && console.log('mfri', bytes); } else if (key === 'mset') { var desc = (0, descriptor_1.readVersionAndDescriptor)(reader); logDevFeatures && console.log('mset', desc); } else { logMissingFeatures && console.log('Unhandled key in #4001', key); } }, function (writer, target) { (0, psdWriter_1.writeBytes)(writer, target._ir4001); }); // TODO: Unfinished helpers_1.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 = (0, psdReader_1.readBytes)(reader, left()); }, function (writer, target) { (0, psdWriter_1.writeBytes)(writer, target._ir4002); }); //# sourceMappingURL=imageResources.js.map