@webtoon/psd Documentation

repository·main·Indexed 23 days ago

https://github.com/webtoon/psd

A lightweight, zero-dependency TypeScript parser for Adobe Photoshop PSD and PSB files, compatible with both browser and Node.js environments. It provides the Psd class as a primary entrypoint to access document structures, including layers, groups, and slices. The library supports retrieving layer properties, decoding grayscale masks via userMask() and realUserMask(), and handling Additional Layer Info (ALI) blocks, descriptors, and channel compression types.

Tokens
9.5K
Snippets
15
Records
51
Agent score
77%

What's inside @webtoon/psd

  1. Understand Additional Layer Info (ALI) block types

    main

    In @webtoon/psd, Additional Layer Info (ALI) blocks are categorized into three main types based on whether they are recognized by the parser:

    1. KnownAliBlock<Key>: A block that @webtoon/psd can successfully parse. These must use a valid AliKey and have _isUnknown set to false.
    2. UnknownAliBlock: A block that the parser encountered but does not have a defined schema for. These contain the raw data as a Uint8Array and have _isUnknown set to true.
    3. AliBlockBase: The underlying base interface for all blocks, containing the signature ("8BIM" or "8B64") and a 4-character key string.
  2. Understand SlicesResourceBlock structure

    main

    The SlicesResourceBlock represents the slice resource data within a PSD file. It is a union type that depends on the version of Photoshop used to create the file:

    • Version 6: Used by Photoshop 6.0 and 7.0. It contains bounding box coordinates (boundTop, boundLeft, boundBottom, boundRight), a sliceGroupName, and an array of SliceEntry objects. It may optionally include a descriptor.
    • Version 7 or 8: Used by Photoshop CS and later. These versions rely on a descriptor (of type VersionedDescriptor) to define the slice data.

    Note that the versioning is non-linear: Photoshop 6.0-7.0 uses version: 6, while CS and later use version: 7 or 8.

  3. Understand Descriptor and VersionedDescriptor structures

    main

    In PSD files, Descriptors are dictionary-like data structures used by Photoshop to store arbitrary key-value pairs. A Descriptor consists of a name, a classId, and a Map of items where keys are strings and values are DescriptorValue objects.

    A VersionedDescriptor is a wrapper around a Descriptor that includes a fixed descriptorVersion (currently 16).

  4. Retrieve typed values with getDescriptorValueAsType()

    main

    Use getDescriptorValueAsType to safely extract a value from a Descriptor by its key and cast it to a specific DescriptorValueType. This function ensures the key exists and the type matches the expected type.

    Throws:

    • MissingDescriptorKey: If the provided key is not found in the descriptor's items.
    • UnexpectedDescriptorValueType: If the actual type of the value does not match the requested valueType.
    export function getDescriptorValueAsType<Type extends DescriptorValueType>(
      descriptor: Descriptor,
      key: string,
      valueType: Type
    ): DescriptorValueWithType<Type>
  5. Parse a PSD file header with parseFileHeader()

    main
    The parseFileHeader function extracts the core metadata from a PSD or PSB file header using a DataView. It validates the file signature, version, reserved bytes, channel count, dimensions, bit depth, and color mode. If the data does not conform to the expected PSD specification, it throws specific error types (e.g., InvalidSignature, InvalidVersion, InvalidPixelCount).
  6. Use the Layer class to access PSD layer properties

    main

    The Layer class represents an individual layer within a PSD file. It provides access to spatial properties (width, height, top, left), visual properties (opacity, blendMode, isHidden, isTransparencyLocked), and layer-specific data like masks and text content.

    Key properties include:

    • name: The name of the layer.
    • opacity: The layer's opacity (0-255).
    • composedOpacity: The effective opacity calculated by multiplying the layer's opacity with its parent's opacity.
    • text: Returns the text content if the layer is a text layer; otherwise undefined.
    • textProperties: Returns EngineData for text layers; otherwise undefined.
    • clipping: Returns the Clipping information for the layer.
    • additionalProperties: Accesses additionalLayerProperties from the layer's properties.
  7. Parse layer and mask information with parseLayerAndMaskInformation()

    main

    Use parseLayerAndMaskInformation to extract the layer hierarchy, group information, and global additional layer properties from a PSD file's data view.

    This function processes the Layer and Mask Information section, which includes segments for LayerInfo, GlobalLayerMaskInfo, and AdditionalLayerInfo. It returns a LayerAndMaskInformationSection object containing:

    • layers: An array of LayerFrame objects representing individual layers.
    • groups: An array of GroupFrame objects representing layer groups (folders).
    • orders: An array of markers ('G', 'L', or 'D') indicating the sequence of groups, layers, and dividers.
    • globalAdditionalLayerInformation: Properties related to global layer masks.
  8. Parse PSD file structure with getFileStructure()

    main

    Use getFileStructure to perform an initial high-level parse of a PSD or PSB file buffer. This function parses the file header to determine the file version and then identifies the byte offsets and sizes for the major sections of the file.

    It returns a FileStructure object containing DataView instances for each section, allowing you to access specific parts of the file (like image data or layer information) without parsing the entire file at once.

  9. Convert a LayerRecord to LayerProperties using createLayerProperties()

    main
    The createLayerProperties function transforms a raw LayerRecord (which contains low-level information like additionalLayerInfos) into a more accessible LayerProperties object. This helper flattens specific properties like layerText, engineData, and maskData into top-level fields and maps additionalLayerInfos into a keyed additionalLayerProperties object for easier lookup.