import { createEnum, normalizeLabAB, denormalizeLabAB } from './helpers'; import { setProperty } from './engineData'; import { AntiAlias, BevelDirection, BevelStyle, BevelTechnique, BlendMode, Color, ColorStop, EffectContour, EffectNoiseGradient, EffectPattern, EffectSolidGradient, ExtraGradientInfo, ExtraPatternInfo, GlowSource, GlowTechnique, GradientStyle, InterpolationMethod, LayerEffectBevel, LayerEffectGradientOverlay, LayerEffectInnerGlow, LayerEffectPatternOverlay, LayerEffectSatin, LayerEffectShadow, LayerEffectsInfo, LayerEffectSolidFill, LayerEffectsOuterGlow, LayerEffectStroke, LineAlignment, LineCapType, LineJoinType, Orientation, TextGridding, TimelineKey, TimelineKeyInterpolation, TimelineTrack, TimelineTrackType, Units, UnitsBounds, UnitsValue, VectorContent, WarpStyle } from './psd'; import { PsdReader, readSignature, readUnicodeString, readUint32, readUint8, readFloat64, readBytes, readAsciiString, readInt32, readFloat32, readInt32LE, readUnicodeStringWithLengthLE } from './psdReader'; import { PsdWriter, writeSignature, writeBytes, writeUint32, writeFloat64, writeUint8, writeUnicodeStringWithPadding, writeInt32, writeFloat32, writeInt32LE, writeUnicodeStringWithoutLengthLE } from './psdWriter'; interface Dict { [key: string]: string; } interface NameClassID { name: string; classID: string; } interface ExtTypeDict { [key: string]: NameClassID; } const unitsMap = new Map([ ['#Ang', 'Angle'], ['#Rsl', 'Density'], ['#Rlt', 'Distance'], ['#Nne', 'None'], ['#Prc', 'Percent'], ['#Pxl', 'Pixels'], ['#Mlm', 'Millimeters'], ['#Pnt', 'Points'], ['RrPi', 'Picas'], ['RrIn', 'Inches'], ['RrCm', 'Centimeters'], ]); const unitsMapRev = new Map(); unitsMap.forEach((value, key) => unitsMapRev.set(value, key)); function makeType(name: string, classID: string) { return { name, classID }; } const nullType = makeType('', 'null'); const USE_CHINESE = false; // Testing const fieldToExtType: ExtTypeDict = { strokeStyleContent: makeType('', 'solidColorLayer'), printProofSetup: makeType(USE_CHINESE ? '校样设置' : 'Proof Setup', 'proofSetup'), Grad: makeType(USE_CHINESE ? '渐变' : 'Gradient', 'Grdn'), Trnf: makeType(USE_CHINESE ? '变换' : 'Transform', 'Trnf'), patternFill: makeType('', 'patternFill'), ebbl: makeType('', 'ebbl'), SoFi: makeType('', 'SoFi'), GrFl: makeType('', 'GrFl'), sdwC: makeType('', 'RGBC'), hglC: makeType('', 'RGBC'), 'Clr ': makeType('', 'RGBC'), 'tintColor': makeType('', 'RGBC'), Ofst: makeType('', 'Pnt '), ChFX: makeType('', 'ChFX'), MpgS: makeType('', 'ShpC'), DrSh: makeType('', 'DrSh'), IrSh: makeType('', 'IrSh'), OrGl: makeType('', 'OrGl'), IrGl: makeType('', 'IrGl'), TrnS: makeType('', 'ShpC'), Ptrn: makeType('', 'Ptrn'), FrFX: makeType('', 'FrFX'), phase: makeType('', 'Pnt '), frameStep: nullType, duration: nullType, workInTime: nullType, workOutTime: nullType, audioClipGroupList: nullType, bounds: makeType('', 'Rctn'), customEnvelopeWarp: makeType('', 'customEnvelopeWarp'), warp: makeType('', 'warp'), 'Sz ': makeType('', 'Pnt '), origin: makeType('', 'Pnt '), autoExpandOffset: makeType('', 'Pnt '), keyOriginShapeBBox: makeType('', 'unitRect'), Vrsn: nullType, psVersion: nullType, docDefaultNewArtboardBackgroundColor: makeType('', 'RGBC'), artboardRect: makeType('', 'classFloatRect'), framedGroupRect: makeType('', 'classFloatRect'), keyOriginRRectRadii: makeType('', 'radii'), keyOriginBoxCorners: nullType, rectangleCornerA: makeType('', 'Pnt '), rectangleCornerB: makeType('', 'Pnt '), rectangleCornerC: makeType('', 'Pnt '), rectangleCornerD: makeType('', 'Pnt '), keyOriginPolyPreviousTightBoxCorners: nullType, keyOriginPolyTrueRectCorners: nullType, keyOriginLineStart: makeType('', 'Pnt '), keyOriginLineEnd: makeType('', 'Pnt '), compInfo: nullType, quiltWarp: makeType('', 'quiltWarp'), generatorSettings: nullType, crema: nullType, FrIn: nullType, blendOptions: nullType, FXRf: nullType, Lefx: nullType, time: nullType, animKey: nullType, timeScope: nullType, inTime: nullType, outTime: nullType, sheetStyle: nullType, translation: nullType, Skew: nullType, boundingBox: makeType('', 'boundingBox'), 'Lnk ': makeType('', 'ExternalFileLink'), frameReader: makeType('', 'FrameReader'), effectParams: makeType('', 'motionTrackEffectParams'), Impr: makeType('None', 'none'), Anch: makeType('', 'Pnt '), 'Fwd ': makeType('', 'Pnt '), 'Bwd ': makeType('', 'Pnt '), FlrC: makeType('', 'Pnt '), meshBoundaryPath: makeType('', 'pathClass'), filterFX: makeType('', 'filterFXStyle'), Fltr: makeType('', 'rigidTransform'), FrgC: makeType('', 'RGBC'), BckC: makeType('', 'RGBC'), sdwM: makeType('Parameters', 'adaptCorrectTones'), hglM: makeType('Parameters', 'adaptCorrectTones'), customShape: makeType('', 'customShape'), origFXRefPoint: nullType, FXRefPoint: nullType, vectorMask: nullType, layerSpecific: nullType, LSpc: nullType, textLayerSettings: makeType('', 'warp'), placedLayerSettings: nullType, IMsk: nullType, VMsk: nullType, TxtC: nullType, ClMg: makeType('', 'ClMg'), interpretation: makeType('', 'footageInterpretation'), bgColor: makeType('', 'NativeQuad'), }; const fieldToArrayExtType: ExtTypeDict = { 'Crv ': makeType('', 'CrPt'), Clrs: makeType('', 'Clrt'), Trns: makeType('', 'TrnS'), keyDescriptorList: nullType, keyRootDescriptorList: nullType, solidFillMulti: makeType('', 'SoFi'), gradientFillMulti: makeType('', 'GrFl'), dropShadowMulti: makeType('', 'DrSh'), innerShadowMulti: makeType('', 'IrSh'), frameFXMulti: makeType('', 'FrFX'), FrIn: nullType, FSts: nullType, LaSt: nullType, sheetTimelineOptions: nullType, trackList: makeType('', 'animationTrack'), globalTrackList: makeType('', 'animationTrack'), keyList: nullType, audioClipGroupList: nullType, audioClipList: nullType, countObjectList: makeType('', 'countObject'), countGroupList: makeType('', 'countGroup'), slices: makeType('', 'slice'), 'Pts ': makeType('', 'Pthp'), SbpL: makeType('', 'SbpL'), pathComponents: makeType('', 'PaCm'), filterFXList: makeType('', 'filterFX'), puppetShapeList: makeType('', 'puppetShape'), channelDenoise: makeType('', 'channelDenoiseParams'), ShrP: makeType('', 'Pnt '), layerSettings: nullType, list: nullType, Adjs: makeType('', 'CrvA'), vertices: makeType('', 'Pnt '), warpedVertices: makeType('', 'Pnt '), quads: makeType('', 'quadIndices'), }; const typeToField: { [key: string]: string[]; } = { 'TEXT': [ 'Txt ', 'printerName', 'Nm ', 'Idnt', 'blackAndWhitePresetFileName', 'LUT3DFileName', 'exportOption', 'scaleSettings', 'BackgroundImagePath', 'ImageSubfolderName', 'SavedIteratedName', 'presetFileName', 'curvesPresetFileName', 'mixerPresetFileName', 'placed', 'description', 'reason', 'artboardPresetName', 'json', 'clipID', 'relPath', 'fullPath', 'mediaDescriptor', 'Msge', 'altTag', 'url', 'cellText', 'preset', 'KnNm', 'FPth', 'comment', 'originalPath', 'Bk ', 'pathUnicodeName', ], 'tdta': [ 'EngineData', 'LUT3DFileData', 'indexArray', 'originalVertexArray', 'deformedVertexArray', 'LqMe', 'bookKey', ], 'long': [ 'TextIndex', 'RndS', 'Mdpn', 'Smth', 'Lctn', 'strokeStyleVersion', 'LaID', 'Vrsn', 'Cnt ', 'bookID', 'cropRectBottom', 'cropRectLeft', 'cropRectRight', 'cropRectTop', // save for web output settings 'Version', 'BackgroundColorBlue', 'BackgroundColorGreen', 'BackgroundColorRed', 'BackgroundColorState', 'AttributeCase', 'Encoding', 'DuplicateFileNameBehavior', 'ImageMapLocation', 'ImageMapType', 'Indent', 'LineEndings', 'SpacersEmptyCells', 'SpacersHorizontal', 'SpacersVertical', 'StylesFormat', 'TDWidthHeight', 'TagCase', 'ColorPolicy', 'MetadataPolicy', 'Brgh', 'Cntr', 'means', 'vibrance', 'Strt', 'bwPresetKind', 'comp', 'compID', 'originalCompID', 'curvesPresetKind', 'mixerPresetKind', 'uOrder', 'vOrder', 'PgNm', 'totalPages', 'Crop', 'numerator', 'denominator', 'frameCount', 'Annt', 'keyOriginType', 'unitValueQuadVersion', 'keyOriginIndex', 'major', 'minor', 'fix', 'docDefaultNewArtboardBackgroundType', 'artboardBackgroundType', 'numModifyingFX', 'deformNumRows', 'deformNumCols', 'FrID', 'FrDl', 'FsID', 'LCnt', 'AFrm', 'AFSt', 'numBefore', 'numAfter', 'Spcn', 'minOpacity', 'maxOpacity', 'BlnM', 'sheetID', 'gblA', 'globalAltitude', 'descVersion', 'frameReaderType', 'LyrI', 'zoomOrigin', 'fontSize', 'Rds ', 'sliceID', 'topOutset', 'leftOutset', 'bottomOutset', 'rightOutset', 'filterID', 'meshQuality', 'meshExpansion', 'meshRigidity', 'VrsM', 'VrsN', 'NmbG', 'WLMn', 'WLMx', 'AmMn', 'AmMx', 'SclH', 'SclV', 'Lvl ', 'TlNm', 'TlOf', 'FlRs', 'Thsh', 'ShrS', 'ShrE', 'FlRs', 'Vrnc', 'Strg', 'ExtS', 'ExtD', 'HrzS', 'VrtS', 'NmbR', 'EdgF', 'Ang1', 'Ang2', 'Ang3', 'Ang4', 'lastAppliedComp', 'capturedInfo', 'pixelSourceType', 'framedGroupType', 'keyOriginPolySides', 'keyOriginLineArrConc', 'noisePreSeed', 'associatedLayer', 'placedLayerCompID', 'VAlign', ], 'enum': [ 'textGridding', 'Ornt', 'warpStyle', 'warpRotate', 'Inte', 'Bltn', 'ClrS', 'BlrQ', 'bvlT', 'bvlS', 'bvlD', 'Md ', 'glwS', 'GrdF', 'GlwT', 'RplS', 'BlrM', 'SmBM', 'strokeStyleLineCapType', 'strokeStyleLineJoinType', 'strokeStyleLineAlignment', 'strokeStyleBlendMode', 'PntT', 'Styl', 'lookupType', 'LUTFormat', 'dataOrder', 'tableOrder', 'enableCompCore', 'enableCompCoreGPU', 'compCoreSupport', 'compCoreGPUSupport', 'Engn', 'enableCompCoreThreads', 'gs99', 'FrDs', 'trackID', 'animInterpStyle', 'horzAlign', 'vertAlign', 'bgColorType', 'shapeOperation', 'UndA', 'Wvtp', 'Drct', 'WndM', 'Edg ', 'FlCl', 'IntE', 'IntC', 'Cnvr', 'Fl ', 'Dstr', 'MztT', 'Lns ', 'ExtT', 'DspM', 'ExtR', 'ZZTy', 'SphM', 'SmBQ', 'placedLayerOCIOConversion', 'gradientsInterpolationMethod', 'pathSymmetryMode', 'interpretAlpha', 'knockout', 'TxPT', 'TxPR', ], 'bool': [ 'PstS', 'printSixteenBit', 'masterFXSwitch', 'enab', 'uglg', 'antialiasGloss', 'useShape', 'useTexture', 'uglg', 'antialiasGloss', 'useShape', 'Vsbl', 'useTexture', 'Algn', 'Rvrs', 'Dthr', 'Invr', 'VctC', 'ShTr', 'layerConceals', 'strokeEnabled', 'fillEnabled', 'strokeStyleScaleLock', 'strokeStyleStrokeAdjust', 'hardProof', 'MpBl', 'paperWhite', 'useLegacy', 'Auto', 'Lab ', 'useTint', 'keyShapeInvalidated', 'autoExpandEnabled', 'autoNestEnabled', 'autoPositionEnabled', 'shrinkwrapOnSaveEnabled', 'present', 'showInDialog', 'overprint', 'sheetDisclosed', 'lightsDisclosed', 'meshesDisclosed', 'materialsDisclosed', 'hasMotion', 'muted', 'Effc', 'selected', 'autoScope', 'fillCanvas', 'cellTextIsHTML', 'Smoo', 'Clsp', 'validAtPosition', 'rigidType', 'hasoptions', 'filterMaskEnable', 'filterMaskLinked', 'filterMaskExtendWithWhite', 'removeJPEGArtifact', 'Mnch', 'ExtF', 'ExtM', 'moreAccurate', 'GpuY', 'LIWy', 'Cnty', 'Lnkd', 'showAlteredVideo', 'lightingOn', 'LockPPI', 'keyOriginLineArrowSt', 'keyOriginLineArrowEnd', 'Expn', 'substitutesUsed', 'blendClipped', 'blendInterior', 'TxMP', ], 'doub': [ 'warpValue', 'warpPerspective', 'warpPerspectiveOther', 'Intr', 'Wdth', 'Hght', 'Gmm ', 'strokeStyleMiterLimit', 'strokeStyleResolution', 'layerTime', 'keyOriginResolution', 'xx', 'xy', 'yx', 'yy', 'tx', 'ty', 'FrGA', 'frameRate', 'audioLevel', 'rotation', 'X ', 'Y ', 'redFloat', 'greenFloat', 'blueFloat', 'imageResolution', 'PuX0', 'PuX1', 'PuX2', 'PuX3', 'PuY0', 'PuY1', 'PuY2', 'PuY3', 'stylization', 'cleanliness', 'brushScale', 'microBrush', 'specularity', 'PreferredResolution', 'FrGL', 'keyOriginLineWeight', 'keyOriginLineArrWdth', 'keyOriginLineArrLngth', 'keyOriginPolyPixelHSF', 'keyOriginPolyCornerRadius', 'gradientAspectRatio', ], 'UntF': [ 'sdwO', 'hglO', 'lagl', 'Lald', 'srgR', 'blur', 'Sftn', 'Opct', 'Dstn', 'Angl', 'Ckmt', 'Nose', 'Inpr', 'ShdN', 'strokeStyleLineWidth', 'strokeStyleLineDashOffset', 'strokeStyleOpacity', 'H ', 'Top ', 'Left', 'Btom', 'Rght', 'Rslt', 'topRight', 'topLeft', 'bottomLeft', 'bottomRight', 'ClNs', 'Shrp', 'fillOpacity', 'keyOriginPolyStarRatio', ], 'VlLs': [ 'Crv ', 'Clrs', 'Mnm ', 'Mxm ', 'Trns', 'pathList', 'keyRootDescriptorList', 'strokeStyleLineDashSet', 'FrLs', 'slices', 'LaSt', 'Trnf', 'nonAffineTransform', 'keyDescriptorList', 'guideIndeces', 'gradientFillMulti', 'solidFillMulti', 'frameFXMulti', 'innerShadowMulti', 'dropShadowMulti', 'FrIn', 'FSts', 'FsFr', 'sheetTimelineOptions', 'audioClipList', 'trackList', 'globalTrackList', 'keyList', 'audioClipList', 'warpValues', 'selectedPin', 'Pts ', 'SbpL', 'pathComponents', 'pinOffsets', 'posFinalPins', 'pinVertexIndices', 'PinP', 'PnRt', 'PnOv', 'PnDp', 'filterFXList', 'puppetShapeList', 'ShrP', 'channelDenoise', 'Mtrx', 'layerSettings', 'list', 'compList', 'Adjs', 'vertices', 'warpedVertices', 'quads', 'indices', 'Mpng', 'textLayerSettingsWarpRect', ], 'ObAr': ['meshPoints', 'quiltSliceX', 'quiltSliceY'], 'obj ': ['null', 'Chnl'], 'Pth ': ['DspF'], 'alis': ['alis'], }; const channels = [ 'Rd ', 'Grn ', 'Bl ', 'Yllw', 'Ylw ', 'Cyn ', 'Mgnt', 'Blck', 'Gry ', 'Lmnc', 'A ', 'B ', ]; // color classes whose channel values are written as doubles ('GRYC' and 'LABC' are kept for backwards compatibility) const doubleColorClasses = ['Grsc', 'LbCl', 'GRYC', 'LABC']; // fallback for unknown keys, based on the type of the value export const cameraRawFilterClassID = 'Adobe Camera Raw Filter'; // types of Camera Raw Filter settings (keys clash with other descriptors so they're typed by class) export const cameraRawFilterKeyTypes: Dict = { CrVe: 'TEXT', PrVN: 'long', PrVe: 'long', WBal: 'enum', Ex12: 'doub', ShpR: 'doub', 'Crv ': 'VlLs', CrvR: 'VlLs', CrvG: 'VlLs', CrvB: 'VlLs', }; function guessOSType(value: any): string | undefined { if (typeof value === 'boolean') return 'bool'; if (typeof value === 'number' && !Number.isInteger(value)) return 'doub'; if (Array.isArray(value)) return 'VlLs'; if (value && typeof value === 'object') { if (typeof value._classID === 'string') return 'Objc'; if (typeof value.classID === 'string') return 'type'; // class read from 'type' or 'GlbC' if (typeof value.low === 'number' && typeof value.high === 'number') return 'comp'; if (typeof value.units === 'string' && typeof value.value === 'number') return 'UntF'; } return undefined; } const fieldToArrayType: Dict = { 'Mnm ': 'long', 'Mxm ': 'long', HtmlFileNameComponents: 'long', // save for web output settings SavingFileNameComponents: 'long', SliceFileNameComponents: 'long', Inpt: 'long', // levels filter Otpt: 'long', // levels filter FrLs: 'long', strokeStyleLineDashSet: 'UntF', Trnf: 'doub', nonAffineTransform: 'doub', keyDescriptorList: 'Objc', keyRootDescriptorList: 'Objc', pathList: 'Objc', gradientFillMulti: 'Objc', solidFillMulti: 'Objc', frameFXMulti: 'Objc', innerShadowMulti: 'Objc', dropShadowMulti: 'Objc', LaSt: 'Objc', FrIn: 'Objc', FSts: 'Objc', FsFr: 'long', blendOptions: 'Objc', sheetTimelineOptions: 'Objc', keyList: 'Objc', warpValues: 'doub', selectedPin: 'long', 'Pts ': 'Objc', SbpL: 'Objc', pathComponents: 'Objc', pinOffsets: 'doub', posFinalPins: 'doub', pinVertexIndices: 'long', PinP: 'doub', PnRt: 'long', PnOv: 'bool', PnDp: 'doub', filterFXList: 'Objc', puppetShapeList: 'Objc', ShrP: 'Objc', channelDenoise: 'Objc', Mtrx: 'long', compList: 'long', textLayerSettingsWarpRect: 'doub', Chnl: 'enum', indices: 'long', Mpng: 'long', }; const fieldToType: Dict = {}; for (const type of Object.keys(typeToField)) { for (const field of typeToField[type]) { fieldToType[field] = type; } } for (const field of Object.keys(fieldToExtType)) { if (!fieldToType[field]) fieldToType[field] = 'Objc'; } for (const field of Object.keys(fieldToArrayExtType)) { fieldToArrayType[field] = 'Objc'; } const pixelSourceOriginRoot = 'PixelSource.origin'; // layer comps settings ('cmls') and animation frames ('mlst'), offsets are stored as 'null' class with integer Hrzn/Vrtc export const layerStateRoot = 'layerState'; function getTypeByKey(key: string, value: any, root: string, parent: any) { if (key === 'presetKind') { return typeof value === 'string' ? 'enum' : 'long'; } if (key === 'null' && root === 'slices') { return 'TEXT'; } else if (key === 'groupID') { return root === 'slices' ? 'long' : 'TEXT'; } else if (key === 'Sz ') { return ('Wdth' in value) ? 'Objc' : (('units' in value) ? 'UntF' : 'doub'); } else if (key === 'Type') { return typeof value === 'string' ? 'enum' : 'long'; } else if (key === 'AntA') { return typeof value === 'string' ? 'enum' : 'bool'; } else if ((key === 'Hrzn' || key === 'Vrtc') && (parent.Type === 'keyType.Pstn' || parent._classID === 'Ofst' || root === pixelSourceOriginRoot)) { return 'long'; } else if (key === 'Hrzn' || key === 'Vrtc' || key === 'Top ' || key === 'Left' || key === 'Btom' || key === 'Rght') { if (root === 'slices') return 'long'; return typeof value === 'number' ? 'doub' : 'UntF'; } else if (key === 'Vrsn') { return typeof value === 'number' ? 'long' : 'Objc'; } else if (key === 'Rd ' || key === 'Grn ' || key === 'Bl ') { return root === 'artd' ? 'long' : 'doub'; } else if (key === 'Trnf') { return Array.isArray(value) ? 'VlLs' : 'Objc'; } else if (key === 'alpha' && root === 'slices') { return 'long'; } else if ((key === 'Inpt' || key === 'Otpt') && parent._classID === 'HsbP') { return 'enum'; } else if ((key === 'Inpt' || key === 'Otpt') && parent._classID === 'LvlA') { return 'VlLs'; } else if (key === 'Intr' && typeof value === 'boolean') { return 'bool'; // interpolation in print style } else if (key === 'PgPs') { return 'enum'; } else if (key === 'LghD' && parent._classID === 'oilPaint') { return 'long'; } else { return fieldToType[key]; } } export function readAsciiStringOrClassId(reader: PsdReader) { const length = readInt32(reader); return readAsciiString(reader, length || 4); } export function writeAsciiStringOrClassId(writer: PsdWriter, value: string) { if (value.length === 4 && value !== 'warp' && value !== 'time' && value !== 'hold' && value !== 'list' && value !== 'json') { // write classId writeInt32(writer, 0); writeSignature(writer, value); } else { // write ascii string writeInt32(writer, value.length); for (let i = 0; i < value.length; i++) { writeUint8(writer, value.charCodeAt(i)); } } } export function readDescriptorStructure(reader: PsdReader, includeClass: boolean, depth = 0) { const struct = readClassStructure(reader); const object: any = includeClass ? { _name: struct.name, _classID: struct.classID } : {}; // console.log('>> ', struct); const itemsCount = readUint32(reader); for (let i = 0; i < itemsCount; i++) { const key = readAsciiStringOrClassId(reader); const type = readSignature(reader); // console.log(`> '${key}' '${type}'`); const data = readOSType(reader, type, includeClass, depth + 1); // if (!getTypeByKey(key, data)) console.log(`> '${key}' '${type}'`, data); setProperty(object, key, data); // key comes from the file, '__proto__' must not change the prototype } return object; } export const outputSettingsRoot = 'outputSettings'; export function writeDescriptorStructure(writer: PsdWriter, name: string, classId: string, value: any, root: string) { if (writer.logMissingFeatures && !classId) writer.log('Missing classId for: ', name, classId, value); // write class structure writeUnicodeStringWithPadding(writer, name); if (root === outputSettingsRoot && classId === 'null') { // Photoshop writes 'null' class id as 4 character string in save for web output settings writeInt32(writer, 4); writeSignature(writer, classId); } else { writeAsciiStringOrClassId(writer, classId); } const allKeys = Object.keys(value); const keys = allKeys.filter(key => key !== '_name' && key !== '_classID' && value[key] !== undefined); writeUint32(writer, keys.length); for (const key of keys) { let type: string | undefined = getTypeByKey(key, value[key], root, value); let childRoot = root; let extType = fieldToExtType[key]; if (classId === cameraRawFilterClassID) { type = cameraRawFilterKeyTypes[key] ?? 'long'; // other settings are integer levels childRoot = cameraRawFilterClassID; // lists (curves) contain integer levels } if (key === 'bounds' && root === 'text') { extType = makeType('', 'bounds'); } else if (key === 'origin') { type = root === 'slices' ? 'enum' : 'Objc'; if (classId === 'PixelSource') { extType = nullType; // stored as 'null' class with integer Hrzn/Vrtc childRoot = pixelSourceOriginRoot; } } else if (key === 'Ofst' && root === layerStateRoot && typeof value[key].Hrzn === 'number') { extType = nullType; childRoot = pixelSourceOriginRoot; } else if ((key === 'Cyn ' || key === 'Mgnt' || key === 'Ylw ' || key === 'Blck') && value._classID === 'CMYC') { type = 'doub'; } else if (/^PN[a-z][a-z]$/.test(key)) { type = 'TEXT'; } else if (/^PT[a-z][a-z]$/.test(key)) { type = 'long'; } else if (/^PF[a-z][a-z]$/.test(key)) { type = 'doub'; } else if ((key === 'Rds ' || key === 'Thsh') && typeof value[key] === 'number' && value._classID === 'SmrB') { type = 'doub'; } else if (key === 'ClSz' || key === 'Rds ' || key === 'Amnt') { type = typeof value[key] === 'number' ? 'long' : 'UntF'; } else if ((key === 'sdwM' || key === 'hglM') && typeof value[key] === 'string') { type = 'enum'; } else if (key === 'blur' && typeof value[key] === 'string') { type = 'enum'; } else if (key === 'Hght' && typeof value[key] === 'number' && value._classID === 'Embs') { type = 'long'; } else if (key === 'Angl' && typeof value[key] === 'number' && (value._classID === 'Embs' || value._classID === 'smartSharpen' || value._classID === 'Twrl' || value._classID === 'MtnB')) { type = 'long'; } else if (key === 'Angl' && typeof value[key] === 'number') { type = 'doub'; // ??? } else if (key === 'bounds' && root === 'slices') { type = 'Objc'; extType = makeType('', 'Rct1'); } else if (key === 'Scl ') { if (typeof value[key] === 'object' && 'Hrzn' in value[key]) { type = 'Objc'; extType = nullType; } else if (typeof value[key] === 'number') { type = 'long'; } else { type = 'UntF'; } } else if (key === 'audioClipGroupList' && keys.length === 1) { type = 'VlLs'; } else if ((key === 'Strt' || key === 'Brgh') && 'H ' in value) { type = 'doub'; } else if (key === 'Wdth' && typeof value[key] === 'object') { type = 'UntF'; } else if (key === 'Ofst' && typeof value[key] === 'number') { type = 'long'; } else if (key === 'Strt' && typeof value[key] === 'object') { type = 'Objc'; extType = nullType; } else if (channels.indexOf(key) !== -1) { type = ((classId === 'RGBC' && root !== 'artd') || doubleColorClasses.indexOf(classId) !== -1) ? 'doub' : 'long'; } else if (key === 'profile') { type = (classId === 'printOutput' || classId === 'proofSetup') ? 'TEXT' : 'tdta'; } else if (key === 'alis' && typeof value[key] === 'string') { // 'alis' is read as a binary string (one char per byte), strings with wider chars can only be stored as TEXT type = /[^\x00-\xff]/.test(value[key]) ? 'TEXT' : 'alis'; } else if (key === 'strokeStyleContent') { if (value[key]['Clr ']) { extType = makeType('', 'solidColorLayer'); } else if (value[key].Grad) { extType = makeType('', 'gradientLayer'); } else if (value[key].Ptrn) { extType = makeType('', 'patternLayer'); } else { writer.logMissingFeatures && writer.log('Invalid strokeStyleContent value', value[key]); } } else if (key === 'bounds' && root === 'quiltWarp') { extType = makeType('', 'classFloatRect'); } // guess color class from its fields (colors created by serializeColor always have _classID set) if (extType && extType.classID === 'RGBC') { if ('H ' in value[key]) extType = { classID: 'HSBC', name: '' }; else if ('Cyn ' in value[key]) extType = { classID: 'CMYC', name: '' }; else if ('Lmnc' in value[key]) extType = { classID: 'LbCl', name: '' }; else if ('Gry ' in value[key]) extType = { classID: 'Grsc', name: '' }; } if (!type) type = guessOSType(value[key]); writeAsciiStringOrClassId(writer, key); writeSignature(writer, type || 'long'); writeOSType(writer, type || 'long', value[key], key, extType, childRoot); if (writer.logMissingFeatures && !type) writer.log(`Missing descriptor field type for: '${key}' in`, value); } } // descriptors nest via lists (VlLs) and nested descriptors (Objc); a crafted file can nest them // arbitrarily deep, which would overflow the call stack (RangeError), so the depth is capped const maxDescriptorDepth = 1000; function readOSType(reader: PsdReader, type: string, includeClass: boolean, depth: number) { if (depth > maxDescriptorDepth) throw new Error('Descriptor is nested too deeply'); switch (type) { case 'obj ': // Reference return readReferenceStructure(reader); case 'Objc': // Descriptor case 'GlbO': // GlobalObject same as Descriptor return readDescriptorStructure(reader, includeClass, depth + 1); case 'VlLs': { // List const length = readInt32(reader); const items: any[] = []; for (let i = 0; i < length; i++) { const itemType = readSignature(reader); // console.log(' >', itemType); items.push(readOSType(reader, itemType, includeClass, depth + 1)); } return items; } case 'doub': // Double return readFloat64(reader); case 'UntF': { // Unit double const units = readSignature(reader); const value = readFloat64(reader); const unit = unitsMap.get(units); if (!unit) throw new Error(`Invalid units: ${units}`); return { units: unit, value }; } case 'UnFl': { // Unit float const units = readSignature(reader); const value = readFloat32(reader); const unit = unitsMap.get(units); if (!unit) throw new Error(`Invalid units: ${units}`); return { units: unit, value }; } case 'TEXT': // String return readUnicodeString(reader); case 'enum': { // Enumerated const enumType = readAsciiStringOrClassId(reader); const value = readAsciiStringOrClassId(reader); return `${enumType}.${value}`; } case 'long': // Integer return readInt32(reader); case 'comp': { // Large Integer (big endian 64 bit integer, high 32 bits first) const high = readUint32(reader); const low = readUint32(reader); return { low, high }; } case 'bool': // Boolean return !!readUint8(reader); case 'type': // Class case 'GlbC': // Class return readClassStructure(reader); case 'alis': { // Alias const length = readInt32(reader); return readAsciiString(reader, length); } case 'tdta': { // Raw Data const length = readInt32(reader); // descriptors have no section bounds here, but data declared larger than the rest of // the file can't be read, reject it instead of allocating a zero-filled buffer for it if (length > (reader.view.byteLength - reader.offset)) throw new Error(`Invalid tdta length: ${length}`); return readBytes(reader, length); } case 'ObAr': { // Object array readInt32(reader); // version: 16 readUnicodeString(reader); // name: '' readAsciiStringOrClassId(reader); // classID: 'rationalPoint' const length = readInt32(reader); const items: any[] = []; for (let i = 0; i < length; i++) { const type1 = readAsciiStringOrClassId(reader); // type Hrzn | Vrtc readSignature(reader); // UnFl readSignature(reader); // units ? '#Pxl' const valuesCount = readInt32(reader); const values: number[] = []; for (let j = 0; j < valuesCount; j++) { values.push(readFloat64(reader)); } items.push({ type: type1, values }); } return items; } case 'Pth ': { // File path /*const length =*/ readInt32(reader); // total size of all fields below const sig = readSignature(reader); /*const pathSize =*/ readInt32LE(reader); // the same as length const charsCount = readInt32LE(reader); const path = readUnicodeStringWithLengthLE(reader, charsCount); return { sig, path }; } default: throw new Error(`Invalid TySh descriptor OSType: ${type} at ${reader.offset.toString(16)}`); } } const ObArTypes: { [key: string]: string | undefined; } = { meshPoints: 'rationalPoint', quiltSliceX: 'UntF', quiltSliceY: 'UntF', }; function writeOSType(writer: PsdWriter, type: string, value: any, key: string, extType: NameClassID | undefined, root: string) { switch (type) { case 'obj ': // Reference writeReferenceStructure(writer, key, value); break; case 'Objc': // Descriptor case 'GlbO': { // GlobalObject same as Descriptor if (typeof value !== 'object') throw new Error(`Invalid struct value: ${JSON.stringify(value)}, key: ${key}`); if (!extType && !value._classID) throw new Error(`Missing ext type for: '${key}' (${JSON.stringify(value)})`); const name = value._name || extType?.name || ''; const classID = value._classID || extType!.classID; writeDescriptorStructure(writer, name, classID, value, root); break; } case 'VlLs': // List if (!Array.isArray(value)) throw new Error(`Invalid list value: ${JSON.stringify(value)}, key: ${key}`); writeInt32(writer, value.length); for (let i = 0; i < value.length; i++) { const type = root === cameraRawFilterClassID ? 'long' : (fieldToArrayType[key] ?? guessOSType(value[i])); writeSignature(writer, type || 'long'); writeOSType(writer, type || 'long', value[i], `${key}[]`, fieldToArrayExtType[key], root); if (writer.logMissingFeatures && !type) writer.log(`Missing descriptor array type for: '${key}' in`, value); } break; case 'doub': // Double if (typeof value !== 'number') throw new Error(`Invalid number value: ${JSON.stringify(value)}, key: ${key}`); writeFloat64(writer, value); break; case 'UntF': { // Unit double const unit = unitsMapRev.get(value.units); if (!unit) throw new Error(`Invalid units: ${value.units} in ${key}`); writeSignature(writer, unit); writeFloat64(writer, value.value); break; } case 'UnFl': { // Unit float const unit = unitsMapRev.get(value.units); if (!unit) throw new Error(`Invalid units: ${value.units} in ${key}`); writeSignature(writer, unit); writeFloat32(writer, value.value); break; } case 'TEXT': // String writeUnicodeStringWithPadding(writer, value); break; case 'enum': { // Enumerated if (typeof value !== 'string') throw new Error(`Invalid enum value: ${JSON.stringify(value)}, key: ${key}`); const [_type, val] = value.split('.'); writeAsciiStringOrClassId(writer, _type); writeAsciiStringOrClassId(writer, val); break; } case 'long': // Integer if (typeof value !== 'number') throw new Error(`Invalid integer value: ${JSON.stringify(value)}, key: ${key}`); writeInt32(writer, Math.round(value)); // round instead of truncating fractional values break; case 'comp': // Large Integer if (!value || typeof value !== 'object') throw new Error(`Invalid large integer value: ${JSON.stringify(value)}, key: ${key}`); writeUint32(writer, value.high >>> 0); writeUint32(writer, value.low >>> 0); break; case 'bool': // Boolean if (typeof value !== 'boolean') throw new Error(`Invalid boolean value: ${JSON.stringify(value)}, key: ${key}`); writeUint8(writer, value ? 1 : 0); break; case 'type': // Class case 'GlbC': // Class if (!value || typeof value !== 'object') throw new Error(`Invalid class value: ${JSON.stringify(value)}, key: ${key}`); writeUnicodeStringWithPadding(writer, value.name || ''); writeAsciiStringOrClassId(writer, value.classID); break; case 'alis': { // Alias (read as a binary string, one char per byte) if (typeof value !== 'string') throw new Error(`Invalid alias value: ${JSON.stringify(value)}, key: ${key}`); writeInt32(writer, value.length); for (let i = 0; i < value.length; i++) { writeUint8(writer, value.charCodeAt(i) & 0xff); } break; } case 'tdta': // Raw Data writeInt32(writer, value.byteLength); writeBytes(writer, value); break; case 'ObAr': { // Object array writeInt32(writer, 16); // version writeUnicodeStringWithPadding(writer, ''); // name const type = ObArTypes[key]; if (!type) throw new Error(`Not implemented ObArType for: ${key}`); writeAsciiStringOrClassId(writer, type); writeInt32(writer, value.length); for (let i = 0; i < value.length; i++) { writeAsciiStringOrClassId(writer, value[i].type); // Hrzn | Vrtc writeSignature(writer, 'UnFl'); writeSignature(writer, '#Pxl'); writeInt32(writer, value[i].values.length); for (let j = 0; j < value[i].values.length; j++) { writeFloat64(writer, value[i].values[j]); } } break; } case 'Pth ': { // File path const path = value.path + '\0'; // Photoshop stores trailing null character (stripped when reading) const length = 4 + 4 + 4 + path.length * 2; writeInt32(writer, length); writeSignature(writer, value.sig); writeInt32LE(writer, length); writeInt32LE(writer, path.length); writeUnicodeStringWithoutLengthLE(writer, path); break; } default: throw new Error(`Not implemented descriptor OSType: ${type}`); } } // Reference item with explicit reference type and class, can be used in place of a value in reference arrays // passed to writeVersionAndDescriptor (references are read as values only: strings, numbers and class objects) export interface DescriptorReferenceItem { type: 'prop' | 'Clss' | 'Enmr' | 'rele' | 'Idnt' | 'indx' | 'name'; _name?: string; // class name _classID?: string; keyID?: string; // prop typeID?: string; // Enmr enumValue?: string; // Enmr value?: string | number; // rele, Idnt, indx, name } const referenceTypes = ['prop', 'Clss', 'Enmr', 'rele', 'Idnt', 'indx', 'name']; function isReferenceItem(value: any): value is DescriptorReferenceItem { return !!value && typeof value === 'object' && referenceTypes.indexOf(value.type) !== -1; } function readReferenceStructure(reader: PsdReader) { const itemsCount = readInt32(reader); const items: any[] = []; for (let i = 0; i < itemsCount; i++) { const type = readSignature(reader); switch (type) { case 'prop': // Property readClassStructure(reader); items.push(readAsciiStringOrClassId(reader)); break; case 'Clss': // Class items.push(readClassStructure(reader)); break; case 'Enmr': { // Enumerated Reference readClassStructure(reader); const typeID = readAsciiStringOrClassId(reader); const enumValue = readAsciiStringOrClassId(reader); items.push(`${typeID}.${enumValue}`); break; } case 'rele': // Offset readClassStructure(reader); items.push(readUint32(reader)); break; case 'Idnt': // Identifier case 'indx': // Index items.push(readInt32(reader)); break; case 'name': // Name readClassStructure(reader); items.push(readUnicodeString(reader)); break; default: throw new Error(`Invalid descriptor reference type: ${type}`); } } return items; } function guessReferenceItem(value: any): DescriptorReferenceItem { if (typeof value === 'string') { // enumerated references are read as `${typeID}.${enumValue}` where typeID is a 4 character ID or a string ID // (values like 'My.Path' or 'Logo.v2' can't be typeID/enum value, so they are written as name references) const match = /^([a-z ]{4}|[a-z]{5,})\.([a-z ]+)$/i.exec(value); if (match) { return { type: 'Enmr', _name: '', _classID: match[1], typeID: match[1], enumValue: match[2] }; } else { return { type: 'name', _name: '', _classID: 'Lyr ', value }; } } else if (typeof value === 'number') { return { type: 'Idnt', value }; } else if (value && typeof value === 'object' && typeof value.classID === 'string') { return { type: 'Clss', _name: value.name || '', _classID: value.classID }; } else { throw new Error(`Invalid descriptor reference value: ${JSON.stringify(value)}`); } } // path selection state (image resource 1088) is a reference to paths, enumerated references there use 'Path' class // (name references use 'Lyr ' class, they reference layer of the vector mask) export function pathSelectionStateToReference(items: string[]): DescriptorReferenceItem[] { return items.map(value => { const item = guessReferenceItem(value); if (item.type === 'Enmr') item._classID = 'Path'; return item; }); } function writeReferenceStructure(writer: PsdWriter, _key: string, items: any[]) { if (!Array.isArray(items)) throw new Error(`Invalid reference value: ${JSON.stringify(items)}, key: ${_key}`); writeInt32(writer, items.length); for (const value of items) { const item = isReferenceItem(value) ? value : guessReferenceItem(value); writeSignature(writer, item.type); switch (item.type) { case 'prop': // Property writeClassStructure(writer, item._name || '', item._classID!); writeAsciiStringOrClassId(writer, item.keyID!); break; case 'Clss': // Class writeClassStructure(writer, item._name || '', item._classID!); break; case 'Enmr': // Enumerated Reference writeClassStructure(writer, item._name || '', item._classID!); writeAsciiStringOrClassId(writer, item.typeID!); writeAsciiStringOrClassId(writer, item.enumValue!); break; case 'rele': // Offset writeClassStructure(writer, item._name || '', item._classID!); writeUint32(writer, item.value as number); break; case 'Idnt': // Identifier case 'indx': // Index writeInt32(writer, item.value as number); break; case 'name': // Name writeClassStructure(writer, item._name || '', item._classID!); writeUnicodeStringWithPadding(writer, `${item.value}`); break; default: throw new Error(`Invalid descriptor reference type: ${item.type}`); } } return items; } function readClassStructure(reader: PsdReader) { const name = readUnicodeString(reader); const classID = readAsciiStringOrClassId(reader); return { name, classID }; } export function writeClassStructure(writer: PsdWriter, name: string, classID: string) { writeUnicodeStringWithPadding(writer, name); writeAsciiStringOrClassId(writer, classID); } export function readVersionAndDescriptor(reader: PsdReader, includeClass = false) { const version = readUint32(reader); if (version !== 16) throw new Error(`Invalid descriptor version: ${version}`); const desc = readDescriptorStructure(reader, includeClass); // console.log(require('util').inspect(desc, false, 99, true)); return desc; } export function writeVersionAndDescriptor(writer: PsdWriter, name: string, classID: string, descriptor: any, root = '') { writeUint32(writer, 16); // version writeDescriptorStructure(writer, name, classID, descriptor, root); } export type DescriptorUnits = 'Angle' | 'Density' | 'Distance' | 'None' | 'Percent' | 'Pixels' | 'Millimeters' | 'Points' | 'Picas' | 'Inches' | 'Centimeters'; export interface DescriptorUnitsValue { units: DescriptorUnits; value: number; } export type DescriptorColor = { _name: ''; _classID: 'RGBC'; 'Rd ': number; 'Grn ': number; 'Bl ': number; } | { _name: ''; _classID: 'HSBC'; // ??? 'H ': DescriptorUnitsValue; Strt: number; Brgh: number; } | { _name: ''; _classID: 'CMYC'; 'Cyn ': number; Mgnt: number; 'Ylw ': number; Blck: number; } | { _name: ''; _classID: 'Grsc' | 'GRYC'; // 'GRYC' was written by older versions of ag-psd 'Gry ': number; } | { _name: ''; _classID: 'LbCl' | 'LABC'; // 'LABC' was written by older versions of ag-psd Lmnc: number; 'A ': number; 'B ': number; } | { _name: ''; _classID: 'RGBC'; redFloat: number; greenFloat: number; blueFloat: number; }; export interface DesciptorPattern { 'Nm ': string; Idnt: string; } export type DesciptorGradient = { 'Nm ': string; GrdF: 'GrdF.CstS'; Intr: number; Clrs: { 'Clr '?: DescriptorColor; Type?: string; // 'Clry.UsrS' | 'Clry.FrgC' | 'Clry.BckC' Lctn: number; Mdpn: number; }[]; Trns: { Opct: DescriptorUnitsValue; Lctn: number; Mdpn: number; }[]; } | { GrdF: 'GrdF.ClNs'; Smth: number; 'Nm ': string; ClrS: string; RndS: number; VctC?: boolean; ShTr?: boolean; 'Mnm ': number[]; 'Mxm ': number[]; }; export interface DescriptorColorContent { 'Clr ': DescriptorColor; } export interface DescriptorGradientContent { Dthr?: boolean; gradientsInterpolationMethod?: string; // 'gradientInterpolationMethodType.Smoo' gradientAspectRatio?: number; Angl?: DescriptorUnitsValue; Type: string; Grad: DesciptorGradient; Rvrs?: boolean; 'Scl '?: DescriptorUnitsValue; Algn?: boolean; Ofst?: { Hrzn: DescriptorUnitsValue; Vrtc: DescriptorUnitsValue; }; noisePreSeed?: number; } export interface DescriptorPatternContent { Ptrn: DesciptorPattern; Algn?: boolean; Lnkd?: boolean; phase?: { Hrzn: number | DescriptorUnitsValue; Vrtc: number | DescriptorUnitsValue; }; 'Scl '?: DescriptorUnitsValue; Angl?: DescriptorUnitsValue; } export type DescriptorVectorContent = DescriptorColorContent | DescriptorGradientContent | DescriptorPatternContent; export interface StrokeDescriptor { strokeStyleVersion: number; strokeEnabled: boolean; fillEnabled: boolean; strokeStyleLineWidth: DescriptorUnitsValue; strokeStyleLineDashOffset: DescriptorUnitsValue; strokeStyleMiterLimit: number; strokeStyleLineCapType: string; strokeStyleLineJoinType: string; strokeStyleLineAlignment: string; strokeStyleScaleLock: boolean; strokeStyleStrokeAdjust: boolean; strokeStyleLineDashSet: DescriptorUnitsValue[]; strokeStyleBlendMode: string; strokeStyleOpacity: DescriptorUnitsValue; strokeStyleContent: DescriptorVectorContent; strokeStyleResolution: number; } export interface BoundsDescriptor { Left: DescriptorUnitsValue; 'Top ': DescriptorUnitsValue; Rght: DescriptorUnitsValue; Btom: DescriptorUnitsValue; } export interface TextDescriptor { 'Txt ': string; TxtC?: HrznVrtcDescriptor; substitutesUsed?: boolean; textGridding: string; Ornt: string; AntA: string; TxPT?: string; TxPR?: string; TxMP?: boolean; bounds?: BoundsDescriptor; boundingBox?: BoundsDescriptor; TextIndex: number; VAlign?: number; EngineData?: Uint8Array; } export interface WarpDescriptor { warpStyle: string; warpValue?: number; warpValues?: number[] warpPerspective: number; warpPerspectiveOther: number; warpRotate: string; bounds?: { 'Top ': DescriptorUnitsValue; Left: DescriptorUnitsValue; Btom: DescriptorUnitsValue; Rght: DescriptorUnitsValue; } | { _classID: 'classFloatRect', 'Top ': number, Left: number, Btom: number, Rght: number, }, uOrder: number; vOrder: number; customEnvelopeWarp?: { _name: ''; _classID: 'customEnvelopeWarp'; meshPoints: { type: 'Hrzn' | 'Vrtc'; values: number[]; }[]; }; } export interface QuiltWarpDescriptor extends WarpDescriptor { deformNumRows: number; deformNumCols: number; customEnvelopeWarp: { _name: ''; _classID: 'customEnvelopeWarp'; quiltSliceX: { type: 'quiltSliceX'; values: number[]; }[]; quiltSliceY: { type: 'quiltSliceY'; values: number[]; }[]; meshPoints: { type: 'Hrzn' | 'Vrtc'; values: number[]; }[]; }; } export interface FractionDescriptor { numerator: number; denominator: number; } export interface HrznVrtcDescriptor { Hrzn: number; Vrtc: number; } export interface FrameDescriptor { FrLs: number[]; enab?: boolean; IMsk?: { Ofst: HrznVrtcDescriptor }; VMsk?: { Ofst: HrznVrtcDescriptor }; Ofst?: HrznVrtcDescriptor; FXRf?: HrznVrtcDescriptor; LSpc?: LayerSpecificDescriptor; Lefx?: Lfx2Descriptor; blendOptions?: BlendOptionsDescriptor; } export interface BlendOptionsDescriptor { 'Md '?: string; Opct?: DescriptorUnitsValue; fillOpacity?: DescriptorUnitsValue; blendInterior?: boolean; blendClipped?: boolean; knockout?: string; } export interface LayerSpecificDescriptor { textLayerSettings?: { warpStyle: string; warpValue: number; warpPerspective: number; warpPerspectiveOther: number; warpRotate: string; textLayerSettingsWarpRect: number[]; }; placedLayerSettings?: { placedLayerCompID: number; }; } export interface FrameListDescriptor { LaID: number; // layer ID LaSt: FrameDescriptor[]; } export interface PxScDescriptor { _name: ''; _classID: 'PixelSource'; pixelSourceType: 1986285651; // vdPS descVersion: 1; origin: { Hrzn: number; Vrtc: number; }; interpretation: { _name: ''; _classID: 'footageInterpretation'; Vrsn: 1; interpretAlpha: 'alphaInterpretation.straight'; profile: Uint8Array; }; frameReader: { _name: ''; _classID: 'FrameReader'; frameReaderType: 1364477522; // QTFR descVersion: 1; 'Lnk ': { _name: ''; _classID: 'ExternalFileLink'; descVersion: 2; 'Nm ': string; fullPath: string; originalPath: string; alis: string; relPath: string; }; mediaDescriptor: string; }; showAlteredVideo: boolean; } export function horzVrtcToXY(hv: HrznVrtcDescriptor): { x: number; y: number; } { return { x: hv.Hrzn, y: hv.Vrtc }; } export function xyToHorzVrtc(xy: { x: number; y: number; }): HrznVrtcDescriptor { return { Hrzn: xy.x, Vrtc: xy.y }; } export function descBoundsToBounds(desc: BoundsDescriptor): UnitsBounds { return { top: parseUnits(desc['Top ']), left: parseUnits(desc.Left), right: parseUnits(desc.Rght), bottom: parseUnits(desc.Btom), }; } export function boundsToDescBounds(bounds: UnitsBounds): BoundsDescriptor { return { Left: unitsValue(bounds.left, 'bounds.left'), ['Top ']: unitsValue(bounds.top, 'bounds.top'), Rght: unitsValue(bounds.right, 'bounds.right'), Btom: unitsValue(bounds.bottom, 'bounds.bottom'), }; } export type TimelineAnimKeyDescriptor = { Type: 'keyType.Opct'; Opct: DescriptorUnitsValue; } | { Type: 'keyType.Trnf'; 'Scl ': HrznVrtcDescriptor; Skew: HrznVrtcDescriptor; rotation: number; translation: HrznVrtcDescriptor; } | { Type: 'keyType.Pstn'; Hrzn: number; Vrtc: number; } | { Type: 'keyType.sheetStyle'; sheetStyle: { Vrsn: number; Lefx?: Lfx2Descriptor; blendOptions: {}; }; } | { Type: 'keyType.globalLighting'; gblA: number; globalAltitude: number; }; export interface TimelineKeyDescriptor { Vrsn: 1; animInterpStyle: 'animInterpStyle.Lnr ' | 'animInterpStyle.hold'; time: FractionDescriptor; animKey: TimelineAnimKeyDescriptor; selected: boolean; } export interface TimelineTrackDescriptor { trackID: 'stdTrackID.globalLightingTrack' | 'stdTrackID.opacityTrack' | 'stdTrackID.styleTrack' | 'stdTrackID.sheetTransformTrack' | 'stdTrackID.sheetPositionTrack'; Vrsn: 1; enab: boolean; Effc: boolean; effectParams?: { keyList: TimelineKeyDescriptor[]; fillCanvas: boolean; zoomOrigin: number; }; keyList: TimelineKeyDescriptor[]; } export interface TimeScopeDescriptor { Vrsn: 1; Strt: FractionDescriptor; duration: FractionDescriptor; inTime: FractionDescriptor; outTime: FractionDescriptor; } export interface TimelineDescriptor { Vrsn: 1; timeScope: TimeScopeDescriptor; autoScope: boolean; audioLevel: number; LyrI: number; trackList?: TimelineTrackDescriptor[]; } export interface EffectDescriptor extends Partial, Partial { enab?: boolean; Styl: string; PntT?: string; 'Md '?: string; Opct?: DescriptorUnitsValue; 'Sz '?: DescriptorUnitsValue; 'Clr '?: DescriptorColor; present?: boolean; showInDialog?: boolean; overprint?: boolean; uglg?: boolean; // useGlobalLight // more fields here used in parseEffectObject } export interface Lfx2Descriptor { 'Scl '?: DescriptorUnitsValue; masterFXSwitch?: boolean; DrSh?: EffectDescriptor; IrSh?: EffectDescriptor; OrGl?: EffectDescriptor; IrGl?: EffectDescriptor; ebbl?: EffectDescriptor; SoFi?: EffectDescriptor; patternFill?: EffectDescriptor; GrFl?: EffectDescriptor; ChFX?: EffectDescriptor; FrFX?: EffectDescriptor; } export interface LmfxDescriptor { 'Scl '?: DescriptorUnitsValue; masterFXSwitch?: boolean; dropShadowMulti?: EffectDescriptor[]; innerShadowMulti?: EffectDescriptor[]; OrGl?: EffectDescriptor; solidFillMulti?: EffectDescriptor[]; gradientFillMulti?: EffectDescriptor[]; patternFill?: EffectDescriptor; // ??? frameFXMulti?: EffectDescriptor[]; IrGl?: EffectDescriptor; ebbl?: EffectDescriptor; ChFX?: EffectDescriptor; numModifyingFX?: number; // number of effects with enabled = true } function parseFxObject(fx: EffectDescriptor) { const stroke: LayerEffectStroke = { enabled: !!fx.enab, position: FStl.decode(fx.Styl), fillType: FrFl.decode(fx.PntT!), blendMode: BlnM.decode(fx['Md ']!), opacity: parsePercent(fx.Opct), size: parseUnitsOrNumber(fx['Sz '] as DescriptorUnitsValue | number), // stored as number (pixels) in old versions }; if (fx.present !== undefined) stroke.present = fx.present; if (fx.showInDialog !== undefined) stroke.showInDialog = fx.showInDialog; if (fx.overprint !== undefined) stroke.overprint = fx.overprint; if (fx['Clr ']) stroke.color = parseColor(fx['Clr ']); if (fx.Grad) stroke.gradient = parseGradientContent(fx as any); if (fx.Ptrn) stroke.pattern = parsePatternContent(fx as any); return stroke; } function serializeFxObject(stroke: LayerEffectStroke) { let FrFX: EffectDescriptor = {} as any; FrFX.enab = !!stroke.enabled; if (stroke.present !== undefined) FrFX.present = !!stroke.present; if (stroke.showInDialog !== undefined) FrFX.showInDialog = !!stroke.showInDialog; FrFX.Styl = FStl.encode(stroke.position); FrFX.PntT = FrFl.encode(stroke.fillType); FrFX['Md '] = BlnM.encode(stroke.blendMode); FrFX.Opct = unitsPercent(stroke.opacity); FrFX['Sz '] = unitsValue(stroke.size, 'size'); if (stroke.color) FrFX['Clr '] = serializeColor(stroke.color); if (stroke.gradient) FrFX = { ...FrFX, ...serializeGradientContent(stroke.gradient) }; if (stroke.pattern) FrFX = { ...FrFX, ...serializePatternContent(stroke.pattern) }; if (stroke.overprint !== undefined) FrFX.overprint = !!stroke.overprint; return FrFX; } export function serializeEffects(e: LayerEffectsInfo, writer: PsdWriter, multi: boolean) { const info: Lfx2Descriptor & LmfxDescriptor = multi ? { 'Scl ': unitsPercentF(e.scale ?? 1), masterFXSwitch: !e.disabled, } : { masterFXSwitch: !e.disabled, 'Scl ': unitsPercentF(e.scale ?? 1), }; const arrayKeys: (keyof LayerEffectsInfo)[] = ['dropShadow', 'innerShadow', 'solidFill', 'gradientOverlay', 'stroke']; for (const key of arrayKeys) { if (e[key] && !Array.isArray(e[key])) throw new Error(`${key} should be an array`); } const useMulti = (arr: undefined | T[]): arr is T[] => !!arr && arr.length > 1 && multi; const useSingle = (arr: undefined | T[]): arr is T[] => !!arr && arr.length >= 1 && (!multi || arr.length === 1); if (useSingle(e.dropShadow)) info.DrSh = serializeEffectObject(e.dropShadow[0], 'dropShadow', writer); if (useMulti(e.dropShadow)) info.dropShadowMulti = e.dropShadow.map(i => serializeEffectObject(i, 'dropShadow', writer)); if (useSingle(e.innerShadow)) info.IrSh = serializeEffectObject(e.innerShadow[0], 'innerShadow', writer); if (useMulti(e.innerShadow)) info.innerShadowMulti = e.innerShadow.map(i => serializeEffectObject(i, 'innerShadow', writer)); if (e.outerGlow) info.OrGl = serializeEffectObject(e.outerGlow, 'outerGlow', writer); if (useMulti(e.solidFill)) info.solidFillMulti = e.solidFill.map(i => serializeEffectObject(i, 'solidFill', writer)); if (useMulti(e.gradientOverlay)) info.gradientFillMulti = e.gradientOverlay.map(i => serializeEffectObject(i, 'gradientOverlay', writer)); if (useMulti(e.stroke)) info.frameFXMulti = e.stroke.map(i => serializeFxObject(i)); if (e.innerGlow) info.IrGl = serializeEffectObject(e.innerGlow, 'innerGlow', writer); if (e.bevel) info.ebbl = serializeEffectObject(e.bevel, 'bevel', writer); if (useSingle(e.solidFill)) info.SoFi = serializeEffectObject(e.solidFill[0], 'solidFill', writer); if (e.patternOverlay) info.patternFill = serializeEffectObject(e.patternOverlay, 'patternOverlay', writer); if (useSingle(e.gradientOverlay)) info.GrFl = serializeEffectObject(e.gradientOverlay[0], 'gradientOverlay', writer); if (e.satin) info.ChFX = serializeEffectObject(e.satin, 'satin', writer); if (useSingle(e.stroke)) info.FrFX = serializeFxObject(e.stroke?.[0]); if (multi) { info.numModifyingFX = 0; for (const key of Object.keys(e)) { const value = (e as any)[key]; if (value == null || typeof value !== 'object') continue; if (Array.isArray(value)) { for (const effect of value) { if (effect?.enabled) info.numModifyingFX++; } } else if (value.enabled) { info.numModifyingFX++; } } } return info; } export function parseEffects(info: Lfx2Descriptor & LmfxDescriptor, reader: PsdReader) { const effects: LayerEffectsInfo = {}; const { _name, _classID, 'Scl ': scale, masterFXSwitch, DrSh, dropShadowMulti, IrSh, innerShadowMulti, OrGl, IrGl, ebbl, SoFi, solidFillMulti, patternFill, GrFl, gradientFillMulti, ChFX, FrFX, frameFXMulti, numModifyingFX, ...rest } = info as typeof info & { _name?: string; _classID?: string; }; if (!masterFXSwitch) effects.disabled = true; if (scale) effects.scale = parsePercent(scale); if (DrSh) effects.dropShadow = [parseEffectObject(DrSh, reader)]; if (dropShadowMulti) effects.dropShadow = dropShadowMulti.map(i => parseEffectObject(i, reader)); if (IrSh) effects.innerShadow = [parseEffectObject(IrSh, reader)]; if (innerShadowMulti) effects.innerShadow = innerShadowMulti.map(i => parseEffectObject(i, reader)); if (OrGl) effects.outerGlow = parseEffectObject(OrGl, reader); if (IrGl) effects.innerGlow = parseEffectObject(IrGl, reader); if (ebbl) effects.bevel = parseEffectObject(ebbl, reader, 'bevel'); if (SoFi) effects.solidFill = [parseEffectObject(SoFi, reader)]; if (solidFillMulti) effects.solidFill = solidFillMulti.map(i => parseEffectObject(i, reader)); if (patternFill) effects.patternOverlay = parseEffectObject(patternFill, reader); if (GrFl) effects.gradientOverlay = [parseEffectObject(GrFl, reader)]; if (gradientFillMulti) effects.gradientOverlay = gradientFillMulti.map(i => parseEffectObject(i, reader)); if (ChFX) effects.satin = parseEffectObject(ChFX, reader); if (FrFX) effects.stroke = [parseFxObject(FrFX)]; if (frameFXMulti) effects.stroke = frameFXMulti.map(i => parseFxObject(i)); if (reader.logMissingFeatures && Object.keys(rest).length) reader.log('Unhandled effect keys:', rest); return effects; } function parseKeyList(keyList: TimelineKeyDescriptor[], reader: PsdReader) { const keys: TimelineKey[] = []; for (let j = 0; j < keyList.length; j++) { const key = keyList[j]; const { time: { denominator, numerator }, selected, animKey } = key; const time = { numerator, denominator }; const interpolation = animInterpStyleEnum.decode(key.animInterpStyle); switch (animKey.Type) { case 'keyType.Opct': keys.push({ interpolation, time, selected, type: 'opacity', value: parsePercent(animKey.Opct) }); break; case 'keyType.Pstn': keys.push({ interpolation, time, selected, type: 'position', x: animKey.Hrzn, y: animKey.Vrtc }); break; case 'keyType.Trnf': keys.push({ interpolation, time, selected, type: 'transform', scale: horzVrtcToXY(animKey['Scl ']), skew: horzVrtcToXY(animKey.Skew), rotation: animKey.rotation, translation: horzVrtcToXY(animKey.translation) }); break; case 'keyType.sheetStyle': { const key: TimelineKey = { interpolation, time, selected, type: 'style' }; if (animKey.sheetStyle.Lefx) key.style = parseEffects(animKey.sheetStyle.Lefx, reader); keys.push(key); break; } case 'keyType.globalLighting': { keys.push({ interpolation, time, selected, type: 'globalLighting', globalAngle: animKey.gblA, globalAltitude: animKey.globalAltitude }); break; } default: throw new Error(`Unsupported keyType value`); } } return keys; } function serializeKeyList(keys: TimelineKey[], writer: PsdWriter): TimelineKeyDescriptor[] { const keyList: TimelineKeyDescriptor[] = []; for (let j = 0; j < keys.length; j++) { const key = keys[j]; const { time, selected = false, interpolation } = key; const animInterpStyle = animInterpStyleEnum.encode(interpolation) as 'animInterpStyle.Lnr ' | 'animInterpStyle.hold'; let animKey: TimelineAnimKeyDescriptor; switch (key.type) { case 'opacity': animKey = { Type: 'keyType.Opct', Opct: unitsPercent(key.value) }; break; case 'position': animKey = { Type: 'keyType.Pstn', Hrzn: key.x, Vrtc: key.y }; break; case 'transform': animKey = { Type: 'keyType.Trnf', 'Scl ': xyToHorzVrtc(key.scale), Skew: xyToHorzVrtc(key.skew), rotation: key.rotation, translation: xyToHorzVrtc(key.translation) }; break; case 'style': animKey = { Type: 'keyType.sheetStyle', sheetStyle: { Vrsn: 1, blendOptions: {} } }; if (key.style) animKey.sheetStyle = { Vrsn: 1, Lefx: serializeEffects(key.style, writer, false), blendOptions: {} }; break; case 'globalLighting': { animKey = { Type: 'keyType.globalLighting', gblA: key.globalAngle, globalAltitude: key.globalAltitude }; break; } default: throw new Error(`Unsupported keyType value`); } keyList.push({ Vrsn: 1, animInterpStyle, time, animKey, selected }); } return keyList; } export function parseTrackList(trackList: TimelineTrackDescriptor[], reader: PsdReader) { const tracks: TimelineTrack[] = []; for (let i = 0; i < trackList.length; i++) { const tr = trackList[i]; const track: TimelineTrack = { type: stdTrackID.decode(tr.trackID), enabled: tr.enab, keys: parseKeyList(tr.keyList, reader), }; if (tr.effectParams) { track.effectParams = { fillCanvas: tr.effectParams.fillCanvas, zoomOrigin: tr.effectParams.zoomOrigin, keys: parseKeyList(tr.effectParams.keyList, reader), }; } tracks.push(track); } return tracks; } export function serializeTrackList(tracks: TimelineTrack[], writer: PsdWriter): TimelineTrackDescriptor[] { const trackList: TimelineTrackDescriptor[] = []; for (let i = 0; i < tracks.length; i++) { const t = tracks[i]; trackList.push({ trackID: stdTrackID.encode(t.type) as any, Vrsn: 1, enab: !!t.enabled, Effc: !!t.effectParams, ...(t.effectParams ? { effectParams: { keyList: serializeKeyList(t.effectParams.keys, writer), fillCanvas: t.effectParams.fillCanvas, zoomOrigin: t.effectParams.zoomOrigin, } } : {}), keyList: serializeKeyList(t.keys, writer), }); } return trackList; } type AllEffects = LayerEffectShadow & LayerEffectsOuterGlow & LayerEffectStroke & LayerEffectInnerGlow & LayerEffectBevel & LayerEffectSolidFill & LayerEffectPatternOverlay & LayerEffectSatin & LayerEffectGradientOverlay; function parseEffectObject(obj: any, reader: PsdReader, objName?: string) { const result: AllEffects = {} as any; for (const key of Object.keys(obj)) { const val = obj[key]; switch (key) { case 'enab': result.enabled = !!val; break; case 'uglg': result.useGlobalLight = !!val; break; case 'AntA': result.antialiased = !!val; break; case 'Algn': result.align = !!val; break; case 'Dthr': result.dither = !!val; break; case 'Invr': result.invert = !!val; break; case 'Rvrs': result.reverse = !!val; break; case 'Clr ': result.color = parseColor(val); break; case 'hglC': result.highlightColor = parseColor(val); break; case 'sdwC': result.shadowColor = parseColor(val); break; case 'Styl': result.position = FStl.decode(val); break; case 'Md ': result.blendMode = BlnM.decode(val); break; case 'hglM': result.highlightBlendMode = BlnM.decode(val); break; case 'sdwM': result.shadowBlendMode = BlnM.decode(val); break; case 'bvlS': result.style = BESl.decode(val); break; case 'bvlD': result.direction = BESs.decode(val); break; case 'bvlT': result.technique = bvlT.decode(val) as any; break; case 'GlwT': result.technique = BETE.decode(val) as any; break; case 'glwS': result.source = IGSr.decode(val); break; case 'Type': result.type = GrdT.decode(val); break; case 'gs99': // gradient overlay case 'gradientsInterpolationMethod': // glows result.interpolationMethod = gradientInterpolationMethodType.decode(val); break; case 'Opct': result.opacity = parsePercent(val); break; case 'hglO': result.highlightOpacity = parsePercent(val); break; case 'sdwO': result.shadowOpacity = parsePercent(val); break; case 'lagl': result.angle = parseAngle(val); break; case 'Angl': result.angle = parseAngle(val); break; case 'Lald': result.altitude = parseAngle(val); break; case 'Sftn': result.soften = parseUnits(val); break; case 'srgR': result.strength = parsePercent(val); break; case 'blur': result.size = parseUnits(val); break; case 'Nose': result.noise = parsePercent(val); break; case 'Inpr': result.range = parsePercent(val); break; case 'Ckmt': result.choke = parseUnits(val); break; case 'ShdN': result.jitter = parsePercent(val); break; case 'Dstn': result.distance = parseUnits(val); break; case 'Scl ': result.scale = parsePercent(val); break; case 'Ptrn': result.pattern = { name: val['Nm '], id: val.Idnt }; break; case 'phase': result.phase = parsePhase(val); break; case 'Ofst': result.offset = { x: parsePercent(val.Hrzn), y: parsePercent(val.Vrtc) }; break; case 'MpgS': case 'TrnS': { const contour: EffectContour = { name: val['Nm '], curve: (val['Crv '] as any[]).map(p => p.Cnty !== undefined ? { x: p.Hrzn, y: p.Vrtc, curved: p.Cnty } : { x: p.Hrzn, y: p.Vrtc }), }; // bevel can have both: gloss contour (TrnS) and contour of the shape (MpgS) if (objName === 'bevel' && key === 'MpgS') { result.shapeContour = contour; } else { result.contour = contour; } break; } case 'Grad': result.gradient = parseGradient(val); break; case 'useTexture': case 'useShape': case 'layerConceals': case 'present': case 'showInDialog': case 'antialiasGloss': result[key] = val; break; case '_name': case '_classID': break; default: reader.logMissingFeatures && reader.log(`Invalid effect key: '${key}', value:`, val); } } return result; } function serializeEffectObject(obj: any, objName: string, writer: PsdWriter) { const result: any = { enab: false, }; if (objName === 'dropShadow') { result.TrnS = { 'Nm ': '', 'Crv ': [] }; } for (const objKey of Object.keys(obj)) { const key: keyof AllEffects = objKey as any; const val = obj[key]; switch (key) { case 'enabled': result.enab = !!val; break; case 'useGlobalLight': result.uglg = !!val; break; case 'antialiased': result.AntA = !!val; break; case 'align': result.Algn = !!val; break; case 'dither': result.Dthr = !!val; break; case 'invert': result.Invr = !!val; break; case 'reverse': result.Rvrs = !!val; break; case 'color': result['Clr '] = serializeColor(val); break; case 'highlightColor': result.hglC = serializeColor(val); break; case 'shadowColor': result.sdwC = serializeColor(val); break; case 'position': result.Styl = FStl.encode(val); break; case 'blendMode': result['Md '] = BlnM.encode(val); break; case 'highlightBlendMode': result.hglM = BlnM.encode(val); break; case 'shadowBlendMode': result.sdwM = BlnM.encode(val); break; case 'style': result.bvlS = BESl.encode(val); break; case 'direction': result.bvlD = BESs.encode(val); break; case 'technique': if (objName === 'bevel') { result.bvlT = bvlT.encode(val); } else { result.GlwT = BETE.encode(val); } break; case 'source': result.glwS = IGSr.encode(val); break; case 'type': result.Type = GrdT.encode(val); break; case 'interpolationMethod': result[(objName === 'outerGlow' || objName === 'innerGlow') ? 'gradientsInterpolationMethod' : 'gs99'] = gradientInterpolationMethodType.encode(val); break; case 'opacity': result.Opct = unitsPercent(val); break; case 'highlightOpacity': result.hglO = unitsPercent(val); break; case 'shadowOpacity': result.sdwO = unitsPercent(val); break; case 'angle': if (objName === 'gradientOverlay' || objName === 'patternOverlay') { result.Angl = unitsAngle(val); } else { result.lagl = unitsAngle(val); } break; case 'altitude': result.Lald = unitsAngle(val); break; case 'soften': result.Sftn = unitsValue(val, key); break; case 'strength': result.srgR = unitsPercent(val); break; case 'size': result.blur = unitsValue(val, key); break; case 'noise': result.Nose = unitsPercent(val); break; case 'range': result.Inpr = unitsPercent(val); break; case 'choke': result.Ckmt = unitsValue(val, key); break; case 'jitter': result.ShdN = unitsPercent(val); break; case 'distance': result.Dstn = unitsValue(val, key); break; case 'scale': result['Scl '] = unitsPercent(val); break; case 'pattern': result.Ptrn = { 'Nm ': val.name, Idnt: val.id }; break; case 'phase': result.phase = { Hrzn: val.x, Vrtc: val.y }; break; case 'offset': result.Ofst = { Hrzn: unitsPercentF(val.x), Vrtc: unitsPercentF(val.y) }; break; case 'contour': result[objName === 'satin' ? 'MpgS' : 'TrnS'] = serializeContour(val); break; case 'shapeContour': result.MpgS = serializeContour(val); break; case 'gradient': result.Grad = serializeGradient(val); break; case 'useTexture': case 'useShape': case 'layerConceals': case 'present': case 'showInDialog': case 'antialiasGloss': result[key] = val; break; default: writer.logMissingFeatures && writer.log(`Invalid effect key: '${key}', value:`, val); } } return result; } function serializeContour(contour: EffectContour) { return { 'Nm ': contour.name, 'Crv ': contour.curve.map(p => p.curved !== undefined ? { Hrzn: p.x, Vrtc: p.y, Cnty: !!p.curved } : { Hrzn: p.x, Vrtc: p.y }), }; } function parseGradient(grad: DesciptorGradient): EffectSolidGradient | EffectNoiseGradient { if (grad.GrdF === 'GrdF.CstS') { // stop locations are always in 0..4096 range, independent of smoothness (Intr) const samples = 4096; return { type: 'solid', name: grad['Nm '], smoothness: grad.Intr / 4096, colorStops: grad.Clrs.map(s => { const stop: ColorStop = { color: s['Clr '] ? parseColor(s['Clr ']) : { r: 0, g: 0, b: 0 }, location: s.Lctn / samples, midpoint: s.Mdpn / 100, }; if (s.Type) { try { const type = Clry.decode(s.Type); if (type !== 'user') stop.type = type; } catch { } // unknown stop type, read as user color } return stop; }), opacityStops: grad.Trns.map(s => ({ opacity: parsePercent(s.Opct), location: s.Lctn / samples, midpoint: s.Mdpn / 100, })), }; } else { return { type: 'noise', name: grad['Nm '], roughness: grad.Smth / 4096, colorModel: ClrS.decode(grad.ClrS), randomSeed: grad.RndS, restrictColors: !!grad.VctC, addTransparency: !!grad.ShTr, min: grad['Mnm '].map(x => x / 100), max: grad['Mxm '].map(x => x / 100), }; } } function serializeGradient(grad: EffectSolidGradient | EffectNoiseGradient): DesciptorGradient { if (grad.type === 'solid') { // stop locations are always in 0..4096 range, independent of smoothness (Intr) const samples = 4096; return { 'Nm ': grad.name || '', GrdF: 'GrdF.CstS', Intr: Math.round((grad.smoothness ?? 1) * 4096), Clrs: grad.colorStops.map(s => ({ 'Clr ': serializeColor(s.color), Type: Clry.encode(s.type ?? 'user'), Lctn: Math.round(s.location * samples), Mdpn: Math.round((s.midpoint ?? 0.5) * 100), })), Trns: grad.opacityStops.map(s => ({ Opct: unitsPercentF(s.opacity), Lctn: Math.round(s.location * samples), Mdpn: Math.round((s.midpoint ?? 0.5) * 100), })), }; } else { return { GrdF: 'GrdF.ClNs', 'Nm ': grad.name || '', ShTr: !!grad.addTransparency, VctC: !!grad.restrictColors, ClrS: ClrS.encode(grad.colorModel), RndS: grad.randomSeed || 0, Smth: Math.round((grad.roughness ?? 1) * 4096), 'Mnm ': (grad.min || [0, 0, 0, 0]).map(x => Math.round(x * 100)), 'Mxm ': (grad.max || [1, 1, 1, 1]).map(x => Math.round(x * 100)), }; } } function parseGradientContent(descriptor: DescriptorGradientContent) { const result = parseGradient(descriptor.Grad) as (EffectSolidGradient | EffectNoiseGradient) & ExtraGradientInfo; result.style = GrdT.decode(descriptor.Type); if (descriptor.Dthr !== undefined) result.dither = descriptor.Dthr; if (descriptor.gradientsInterpolationMethod !== undefined) result.interpolationMethod = gradientInterpolationMethodType.decode(descriptor.gradientsInterpolationMethod); if (descriptor.Rvrs !== undefined) result.reverse = descriptor.Rvrs; if (descriptor.Angl !== undefined) result.angle = parseAngle(descriptor.Angl); if (descriptor['Scl '] !== undefined) result.scale = parsePercent(descriptor['Scl ']); if (descriptor.Algn !== undefined) result.align = descriptor.Algn; if (descriptor.Ofst !== undefined) { result.offset = { x: parsePercent(descriptor.Ofst.Hrzn), y: parsePercent(descriptor.Ofst.Vrtc) }; } if (descriptor.gradientAspectRatio !== undefined) result.aspectRatio = descriptor.gradientAspectRatio; if (descriptor.noisePreSeed !== undefined) result.noisePreSeed = descriptor.noisePreSeed; return result; } // phase is usually stored as plain numbers, but some files store it as unit values function parsePhase({ Hrzn, Vrtc }: { Hrzn: number | DescriptorUnitsValue; Vrtc: number | DescriptorUnitsValue; }) { return { x: typeof Hrzn === 'number' ? Hrzn : Hrzn.value, y: typeof Vrtc === 'number' ? Vrtc : Vrtc.value, }; } function parsePatternContent(descriptor: DescriptorPatternContent) { const result: EffectPattern & ExtraPatternInfo = { name: descriptor.Ptrn['Nm '], id: descriptor.Ptrn.Idnt, }; if (descriptor.Algn !== undefined) result.align = descriptor.Algn; if (descriptor.Lnkd !== undefined) result.linked = descriptor.Lnkd; if (descriptor.phase !== undefined) result.phase = parsePhase(descriptor.phase); if (descriptor['Scl '] !== undefined) result.scale = parsePercent(descriptor['Scl ']); if (descriptor.Angl !== undefined) result.angle = parseAngle(descriptor.Angl); return result; } export function parseVectorContent(descriptor: DescriptorVectorContent): VectorContent { if ('Grad' in descriptor) { return parseGradientContent(descriptor); } else if ('Ptrn' in descriptor) { return { type: 'pattern', ...parsePatternContent(descriptor) }; } else if ('Clr ' in descriptor) { return { type: 'color', color: parseColor(descriptor['Clr ']) }; } else { throw new Error('Invalid vector content'); } } function serializeGradientContent(content: (EffectSolidGradient | EffectNoiseGradient) & ExtraGradientInfo) { const result: DescriptorGradientContent = {} as any; if (content.dither !== undefined) result.Dthr = content.dither; if (content.interpolationMethod !== undefined) result.gradientsInterpolationMethod = gradientInterpolationMethodType.encode(content.interpolationMethod); if (content.reverse !== undefined) result.Rvrs = content.reverse; if (content.aspectRatio !== undefined) result.gradientAspectRatio = content.aspectRatio; if (content.angle !== undefined) result.Angl = unitsAngle(content.angle); result.Type = GrdT.encode(content.style); if (content.align !== undefined) result.Algn = content.align; if (content.scale !== undefined) result['Scl '] = unitsPercentF(content.scale); if (content.offset) { result.Ofst = { Hrzn: unitsPercentF(content.offset.x), Vrtc: unitsPercentF(content.offset.y), }; } if (content.noisePreSeed !== undefined) result.noisePreSeed = content.noisePreSeed; result.Grad = serializeGradient(content); return result; } function serializePatternContent(content: EffectPattern & ExtraPatternInfo) { const result: DescriptorPatternContent = { Ptrn: { 'Nm ': content.name || '', Idnt: content.id || '', } }; if (content.align !== undefined) result.Algn = !!content.align; if (content.linked !== undefined) result.Lnkd = !!content.linked; if (content.phase !== undefined) result.phase = { Hrzn: content.phase.x, Vrtc: content.phase.y }; if (content.scale !== undefined) result['Scl '] = unitsPercentF(content.scale); if (content.angle !== undefined) result.Angl = unitsAngle(content.angle); return result; } export function serializeVectorContent(content: VectorContent): { descriptor: DescriptorVectorContent; key: string; } { if (content.type === 'color') { return { key: 'SoCo', descriptor: { 'Clr ': serializeColor(content.color) } }; } else if (content.type === 'pattern') { return { key: 'PtFl', descriptor: serializePatternContent(content) }; } else { return { key: 'GdFl', descriptor: serializeGradientContent(content) }; } } // Descriptor color values: HSB saturation/brightness 0..100, CMYK 0..100, Gray 0..100, Lab L 0..100, a/b -128..127 // Color values (see psd.ts): HSB 0..1, CMYK 0..255, Gray 0..255, Lab L 0..1, a/b -1..1 (same as readColor/writeColor) const colorConversions: { [key: string]: ((x: number) => number)[] | undefined; } = { // key: [descriptor -> color, color -> descriptor] 'H ': [x => x / 360, x => x * 360], // angle Strt: [x => x / 100, x => x * 100], Brgh: [x => x / 100, x => x * 100], 'Cyn ': [x => x * 255 / 100, x => x * 100 / 255], Mgnt: [x => x * 255 / 100, x => x * 100 / 255], 'Ylw ': [x => x * 255 / 100, x => x * 100 / 255], Blck: [x => x * 255 / 100, x => x * 100 / 255], 'Gry ': [x => x * 255 / 100, x => x * 100 / 255], Lmnc: [x => x / 100, x => x * 100], 'A ': [normalizeLabAB, denormalizeLabAB], 'B ': [normalizeLabAB, denormalizeLabAB], }; function colorToDescriptorValue(key: string, value: number) { const conversion = colorConversions[key]; if (!conversion) return value; const [from, to] = conversion; const result = to(value); if (!isFinite(result)) return result; // prefer shortest number that converts back to the same value (avoids values like 30.200000000000003) for (let precision = 1; precision < 17; precision++) { const candidate = +result.toPrecision(precision); if (from(candidate) === value) return candidate; } return result; } function isBookColor(color: any) { return color._classID === 'BkCl' || 'Bk ' in color || 'bookID' in color; } export function parseColor(color: DescriptorColor): Color { const result = parseColorValues(color); if (isBookColor(color)) { const book = color as any as { 'Bk '?: string; 'Nm '?: string; bookID?: number; bookKey?: Uint8Array | string; }; const key = book.bookKey; result.book = { book: book['Bk '] ?? '', name: book['Nm '] ?? '', bookID: book.bookID ?? 0, bookKey: typeof key === 'string' ? key : (key ? String.fromCharCode(...Array.from(key)) : ''), }; } return result; } function parseColorValues(color: DescriptorColor): Color { if ('H ' in color) { return { h: parsePercentOrAngle(color['H ']), s: color.Strt / 100, b: color.Brgh / 100 }; } else if ('Rd ' in color) { return { r: color['Rd '], g: color['Grn '], b: color['Bl '] }; } else if ('Cyn ' in color) { return { c: color['Cyn '] * 255 / 100, m: color.Mgnt * 255 / 100, y: color['Ylw '] * 255 / 100, k: color.Blck * 255 / 100, }; } else if ('Gry ' in color) { return { k: color['Gry '] * 255 / 100 }; } else if ('Lmnc' in color) { return { l: color.Lmnc / 100, a: normalizeLabAB(color['A ']), b: normalizeLabAB(color['B ']) }; } else if ('redFloat' in color) { return { fr: color.redFloat, fg: color.greenFloat, fb: color.blueFloat }; } else if (isBookColor(color)) { return { r: 0, g: 0, b: 0 }; // book (Pantone) color without color values } else { throw new Error('Unsupported color descriptor'); } } export function serializeColor(color: Color | undefined): DescriptorColor { const result = serializeColorValues(color); if (color?.book) { const { book, name, bookID, bookKey } = color.book; const key = new Uint8Array(bookKey.length); for (let i = 0; i < key.length; i++) key[i] = bookKey.charCodeAt(i) & 0xff; Object.assign(result, { 'Bk ': book, 'Nm ': name, bookID, bookKey: key }); } return result; } function serializeColorValues(color: Color | undefined): DescriptorColor { if (!color) { return { _name: '', _classID: 'RGBC', 'Rd ': 0, 'Grn ': 0, 'Bl ': 0 }; } else if ('r' in color) { return { _name: '', _classID: 'RGBC', 'Rd ': color.r || 0, 'Grn ': color.g || 0, 'Bl ': color.b || 0 }; } else if ('fr' in color) { return { _name: '', _classID: 'RGBC', redFloat: color.fr, greenFloat: color.fg, blueFloat: color.fb }; } else if ('h' in color) { return { _name: '', _classID: 'HSBC', 'H ': unitsAngle(colorToDescriptorValue('H ', color.h || 0)), Strt: colorToDescriptorValue('Strt', color.s || 0), Brgh: colorToDescriptorValue('Brgh', color.b || 0), }; } else if ('c' in color) { return { _name: '', _classID: 'CMYC', 'Cyn ': colorToDescriptorValue('Cyn ', color.c || 0), Mgnt: colorToDescriptorValue('Mgnt', color.m || 0), 'Ylw ': colorToDescriptorValue('Ylw ', color.y || 0), Blck: colorToDescriptorValue('Blck', color.k || 0), }; } else if ('l' in color) { return { _name: '', _classID: 'LbCl', Lmnc: colorToDescriptorValue('Lmnc', color.l || 0), 'A ': colorToDescriptorValue('A ', color.a || 0), 'B ': colorToDescriptorValue('B ', color.b || 0), }; } else if ('k' in color) { return { _name: '', _classID: 'Grsc', 'Gry ': colorToDescriptorValue('Gry ', color.k || 0) }; } else { throw new Error('Invalid color value'); } } export function parseAngle(x: DescriptorUnitsValue) { if (x === undefined) return 0; if (x.units !== 'Angle') throw new Error(`Invalid units: ${x.units}`); return x.value; } export function parsePercent(x: DescriptorUnitsValue | undefined) { if (x === undefined) return 1; if (x.units !== 'Percent') throw new Error(`Invalid units: ${x.units}`); return x.value / 100; } export function parsePercentOrAngle(x: DescriptorUnitsValue | undefined) { if (x === undefined) return 1; if (x.units === 'Percent') return x.value / 100; if (x.units === 'Angle') return x.value / 360; throw new Error(`Invalid units: ${x.units}`); } export function parseUnits({ units, value }: DescriptorUnitsValue): UnitsValue { if ( units !== 'Pixels' && units !== 'Millimeters' && units !== 'Points' && units !== 'None' && units !== 'Picas' && units !== 'Inches' && units !== 'Centimeters' && units !== 'Density' && units !== 'Distance' ) { throw new Error(`Invalid units: ${JSON.stringify({ units, value })}`); } return { value, units }; } export function parseUnitsOrNumber(value: DescriptorUnitsValue | number, units: Units = 'Pixels'): UnitsValue { if (typeof value === 'number') return { value, units }; return parseUnits(value); } export function parseUnitsToNumber({ units, value }: DescriptorUnitsValue, expectedUnits: string): number { if (units !== expectedUnits) throw new Error(`Invalid units: ${JSON.stringify({ units, value })}`); return value; } export function unitsAngle(value: number | undefined): DescriptorUnitsValue { return { units: 'Angle', value: value || 0 }; } export function unitsPercent(value: number | undefined): DescriptorUnitsValue { return { units: 'Percent', value: Math.round((value || 0) * 100) }; } export function unitsPercentF(value: number | undefined): DescriptorUnitsValue { return { units: 'Percent', value: (value || 0) * 100 }; } export function unitsValue(x: UnitsValue | undefined, key: string): DescriptorUnitsValue { if (x == null) return { units: 'Pixels', value: 0 }; if (typeof x !== 'object') throw new Error(`Invalid value: ${JSON.stringify(x)} (key: ${key}) (should have value and units)`); const { units, value } = x; if (typeof value !== 'number') throw new Error(`Invalid value in ${JSON.stringify(x)} (key: ${key})`); if ( units !== 'Pixels' && units !== 'Millimeters' && units !== 'Points' && units !== 'None' && units !== 'Picas' && units !== 'Inches' && units !== 'Centimeters' && units !== 'Density' && units !== 'Distance' ) { throw new Error(`Invalid units in ${JSON.stringify(x)} (key: ${key})`); } return { units, value }; } export function frac({ numerator, denominator }: FractionDescriptor) { return { numerator, denominator }; } export const textGridding = createEnum('textGridding', 'none', { none: 'None', round: 'Rnd ', }); export const Ornt = createEnum('Ornt', 'horizontal', { horizontal: 'Hrzn', vertical: 'Vrtc', }); export const Annt = createEnum('Annt', 'sharp', { none: 'Anno', sharp: 'antiAliasSharp', crisp: 'AnCr', strong: 'AnSt', smooth: 'AnSm', platform: 'antiAliasPlatformGray', platformLCD: 'antiAliasPlatformLCD', }); export const warpStyle = createEnum('warpStyle', 'none', { none: 'warpNone', arc: 'warpArc', arcLower: 'warpArcLower', arcUpper: 'warpArcUpper', arch: 'warpArch', bulge: 'warpBulge', shellLower: 'warpShellLower', shellUpper: 'warpShellUpper', flag: 'warpFlag', wave: 'warpWave', fish: 'warpFish', rise: 'warpRise', fisheye: 'warpFisheye', inflate: 'warpInflate', squeeze: 'warpSqueeze', twist: 'warpTwist', cylinder: 'warpCylinder', custom: 'warpCustom', }); export const BlnM = createEnum('BlnM', 'normal', { 'normal': 'Nrml', 'dissolve': 'Dslv', 'darken': 'Drkn', 'multiply': 'Mltp', 'color burn': 'CBrn', 'linear burn': 'linearBurn', 'darker color': 'darkerColor', 'lighten': 'Lghn', 'screen': 'Scrn', 'color dodge': 'CDdg', 'linear dodge': 'linearDodge', 'lighter color': 'lighterColor', 'overlay': 'Ovrl', 'soft light': 'SftL', 'hard light': 'HrdL', 'vivid light': 'vividLight', 'linear light': 'linearLight', 'pin light': 'pinLight', 'hard mix': 'hardMix', 'difference': 'Dfrn', 'exclusion': 'Xclu', 'subtract': 'blendSubtraction', 'divide': 'blendDivide', 'hue': 'H ', 'saturation': 'Strt', 'color': 'Clr ', 'luminosity': 'Lmns', // used in ABR 'linear height': 'linearHeight', 'height': 'Hght', 'subtraction': 'Sbtr', // 2nd version of subtract ? 'pass through': 'passThrough', // only used in layer comps and animation frames }); export const BESl = createEnum('BESl', 'inner bevel', { 'inner bevel': 'InrB', 'outer bevel': 'OtrB', 'emboss': 'Embs', 'pillow emboss': 'PlEb', 'stroke emboss': 'strokeEmboss', }); export const bvlT = createEnum('bvlT', 'smooth', { 'smooth': 'SfBL', 'chisel hard': 'PrBL', 'chisel soft': 'Slmt', }); export const BESs = createEnum('BESs', 'up', { up: 'In ', down: 'Out ', }); export const BETE = createEnum('BETE', 'softer', { softer: 'SfBL', precise: 'PrBL', }); export const IGSr = createEnum('IGSr', 'edge', { edge: 'SrcE', center: 'SrcC', }); export const GrdT = createEnum('GrdT', 'linear', { linear: 'Lnr ', radial: 'Rdl ', angle: 'Angl', reflected: 'Rflc', diamond: 'Dmnd', }); export const animInterpStyleEnum = createEnum('animInterpStyle', 'linear', { linear: 'Lnr ', hold: 'hold', }); export const stdTrackID = createEnum('stdTrackID', 'opacity', { opacity: 'opacityTrack', style: 'styleTrack', sheetTransform: 'sheetTransformTrack', sheetPosition: 'sheetPositionTrack', globalLighting: 'globalLightingTrack', }); export const gradientInterpolationMethodType = createEnum('gradientInterpolationMethodType', 'perceptual', { perceptual: 'Perc', linear: 'Lnr ', classic: 'Gcls', smooth: 'Smoo', // TODO: stripes }); export const Clry = createEnum<'user' | 'foreground' | 'background'>('Clry', 'user', { user: 'UsrS', foreground: 'FrgC', background: 'BckC', }); export const ClrS = createEnum<'rgb' | 'hsb' | 'lab' | 'hsl'>('ClrS', 'rgb', { rgb: 'RGBC', hsb: 'HSBl', lab: 'LbCl', hsl: 'HSLC', }); export const FStl = createEnum<'inside' | 'center' | 'outside'>('FStl', 'outside', { outside: 'OutF', center: 'CtrF', inside: 'InsF' }); export const FrFl = createEnum<'color' | 'gradient' | 'pattern'>('FrFl', 'color', { color: 'SClr', gradient: 'GrFl', pattern: 'Ptrn', }); export const ESliceType = createEnum<'image' | 'noImage'>('ESliceType', 'image', { image: 'Img ', noImage: 'noImage', }); export const ESliceHorzAlign = createEnum<'default'>('ESliceHorzAlign', 'default', { default: 'default', }); export const ESliceVertAlign = createEnum<'default'>('ESliceVertAlign', 'default', { default: 'default', }); export const ESliceOrigin = createEnum<'userGenerated' | 'autoGenerated' | 'layer'>('ESliceOrigin', 'userGenerated', { userGenerated: 'userGenerated', autoGenerated: 'autoGenerated', layer: 'layer', }); export const ESliceBGColorType = createEnum<'none' | 'matte' | 'color'>('ESliceBGColorType', 'none', { none: 'None', matte: 'matte', color: 'Clr ', }); export const strokeStyleLineCapType = createEnum('strokeStyleLineCapType', 'butt', { butt: 'strokeStyleButtCap', round: 'strokeStyleRoundCap', square: 'strokeStyleSquareCap', }); export const strokeStyleLineJoinType = createEnum('strokeStyleLineJoinType', 'miter', { miter: 'strokeStyleMiterJoin', round: 'strokeStyleRoundJoin', bevel: 'strokeStyleBevelJoin', }); export const strokeStyleLineAlignment = createEnum('strokeStyleLineAlignment', 'inside', { inside: 'strokeStyleAlignInside', center: 'strokeStyleAlignCenter', outside: 'strokeStyleAlignOutside', }); export const BlrM = createEnum<'spin' | 'zoom'>('BlrM', 'spin', { spin: 'Spn ', zoom: 'Zm ', }); export const BlrQ = createEnum<'draft' | 'good' | 'best'>('BlrQ', 'good', { draft: 'Drft', good: 'Gd ', best: 'Bst ', }); export const SmBM = createEnum<'normal' | 'edge only' | 'overlay edge'>('SmBM', 'normal', { normal: 'SBMN', 'edge only': 'SBME', 'overlay edge': 'SBMO', }); export const SmBQ = createEnum<'low' | 'medium' | 'high'>('SmBQ', 'medium', { low: 'SBQL', medium: 'SBQM', high: 'SBQH', }); export const DspM = createEnum<'stretch to fit' | 'tile'>('DspM', 'stretch to fit', { 'stretch to fit': 'StrF', 'tile': 'Tile', }); export const UndA = createEnum<'wrap around' | 'repeat edge pixels'>('UndA', 'repeat edge pixels', { 'wrap around': 'WrpA', 'repeat edge pixels': 'RptE', }); export const Cnvr = createEnum<'rectangular to polar' | 'polar to rectangular'>('Cnvr', 'rectangular to polar', { 'rectangular to polar': 'RctP', 'polar to rectangular': 'PlrR', }); export const RplS = createEnum<'small' | 'medium' | 'large'>('RplS', 'medium', { small: 'Sml ', medium: 'Mdm ', large: 'Lrg ', }); export const SphM = createEnum<'normal' | 'horizontal only' | 'vertical only'>('SphM', 'normal', { 'normal': 'Nrml', 'horizontal only': 'HrzO', 'vertical only': 'VrtO', }); export const Wvtp = createEnum<'sine' | 'triangle' | 'square'>('Wvtp', 'sine', { sine: 'WvSn', triangle: 'WvTr', square: 'WvSq', }); export const ZZTy = createEnum<'around center' | 'out from center' | 'pond ripples'>('ZZTy', 'pond ripples', { 'around center': 'ArnC', 'out from center': 'OtFr', 'pond ripples': 'PndR', }); export const Dstr = createEnum<'uniform' | 'gaussian'>('Dstr', 'uniform', { uniform: 'Unfr', gaussian: 'Gsn ', }); export const Chnl = createEnum<'red' | 'green' | 'blue' | 'composite'>('Chnl', 'composite', { red: 'Rd ', green: 'Grn ', blue: 'Bl ', composite: 'Cmps', }); export const MztT = createEnum<'fine dots' | 'medium dots' | 'grainy dots' | 'coarse dots' | 'short lines' | 'medium lines' | 'long lines' | 'short strokes' | 'medium strokes' | 'long strokes'>('MztT', 'fine dots', { 'fine dots': 'FnDt', 'medium dots': 'MdmD', 'grainy dots': 'GrnD', 'coarse dots': 'CrsD', 'short lines': 'ShrL', 'medium lines': 'MdmL', 'long lines': 'LngL', 'short strokes': 'ShSt', 'medium strokes': 'MdmS', 'long strokes': 'LngS', }); export const Lns = createEnum<'50-300mm zoom' | '32mm prime' | '105mm prime' | 'movie prime'>('Lns ', '50-300mm zoom', { '50-300mm zoom': 'Zm ', '32mm prime': 'Nkn ', '105mm prime': 'Nkn1', 'movie prime': 'PnVs', }); export const blurType = createEnum<'gaussian blur' | 'lens blur' | 'motion blur'>('blurType', 'gaussian blur', { 'gaussian blur': 'GsnB', 'lens blur': 'lensBlur', 'motion blur': 'MtnB', }); export const DfsM = createEnum<'normal' | 'darken only' | 'lighten only' | 'anisotropic'>('DfsM', 'normal', { 'normal': 'Nrml', 'darken only': 'DrkO', 'lighten only': 'LghO', 'anisotropic': 'anisotropic', }); export const ExtT = createEnum<'blocks' | 'pyramids'>('ExtT', 'blocks', { blocks: 'Blks', pyramids: 'Pyrm', }); export const ExtR = createEnum<'random' | 'level-based'>('ExtR', 'random', { random: 'Rndm', 'level-based': 'LvlB', }); export const FlCl = createEnum<'background color' | 'foreground color' | 'inverse image' | 'unaltered image'>('FlCl', 'background color', { 'background color': 'FlBc', 'foreground color': 'FlFr', 'inverse image': 'FlIn', 'unaltered image': 'FlSm', }); export const CntE = createEnum<'lower' | 'upper'>('CntE', 'upper', { lower: 'Lwr ', upper: 'Upr ', }); export const WndM = createEnum<'wind' | 'blast' | 'stagger'>('WndM', 'wind', { wind: 'Wnd ', blast: 'Blst', stagger: 'Stgr', }); export const Drct = createEnum<'left' | 'right'>('Drct', 'right', { left: 'Left', right: 'Rght', }); export const IntE = createEnum<'odd lines' | 'even lines'>('IntE', 'odd lines', { 'odd lines': 'ElmO', 'even lines': 'ElmE', }); export const IntC = createEnum<'duplication' | 'interpolation'>('IntC', 'interpolation', { duplication: 'CrtD', interpolation: 'CrtI', }); export const FlMd = createEnum<'set to transparent' | 'repeat edge pixels' | 'wrap around'>('FlMd', 'wrap around', { 'set to transparent': 'Bckg', 'repeat edge pixels': 'Rpt ', 'wrap around': 'Wrp ', }); export const prjM = createEnum<'fisheye' | 'perspective' | 'auto' | 'full spherical'>('prjM', 'fisheye', { 'fisheye': 'fisP', 'perspective': 'perP', 'auto': 'auto', 'full spherical': 'fusP', }); export const presetKindType = createEnum<'custom' | 'default'>('presetKindType', 'custom', { custom: 'presetKindCustom', default: 'presetKindDefault', }); export const patternColorHandling = createEnum<'convert' | 'avoid conversion' | 'luminance only'>('', 'convert', { 'convert': 'conv', 'avoid conversion': 'avod', 'luminance only': 'lumi', });