PhotoshopAPI

repository·master·Indexed 43 days ago

https://github.com/emildohne/photoshopapi

A high-performance C++20 library with Python bindings for reading, writing, and modifying Photoshop files (*.psd and *.psb). It supports 8, 16, and 32-bit depths, RGB, CMYK, and Grayscale color modes, and focuses on layer editing as a first-class citizen. Key features include support for editable text layers, pixel masks, and Smart Object transformations (affine, non-affine, simple, and quilt warps).

Tokens
25.6K
Snippets
64
Records
147
Agent score
80%

What's inside PhotoshopAPI

  1. Overview of Photoshop Test Documents

    master

    The PhotoshopTest/documents directory contains a collection of test files used to verify that PhotoshopAPI accurately and efficiently parses various Photoshop file formats and combinations. These documents are used by the testing suite to validate full parsing and the correctness of specific internal sections or algorithms.

    Key Details:

    • Generation Version: All test documents were generated using Photoshop Version 22.3.2 (Photoshop 2022).
    • Resolution: To maintain repository size, individual test documents are typically low-resolution (rarely exceeding 64x64).
  2. Overview of Geometry (2D) structures

    master

    The PhotoshopAPI provides specialized 2D Geometry structures designed to facilitate image transformations, such as warping.

    Key characteristics:

    • Optimization: These structures are optimized for execution speed rather than maximum flexibility.
    • Current Scope: Currently, these structures are primarily used for manipulating SmartObject layers.
    • Future Support: Planned updates may extend these capabilities to allow more generic image transformations across different layer types.

    To work with geometry, you will need to explore operations, meshes, Bezier curves, and SmartObject warping specifically.

  3. Overview of Python Binding Classes

    master

    The Python bindings provide a 'pythonic' interface to the underlying C++ API. While they share the same core logic, the function signatures and structures may differ from the C++ implementation to better suit Python usage patterns. The main classes you will interact with include:

    • LayeredFile: For reading and writing Photoshop files.
    • ImageLayer_16bit: For handling high-bit depth layer data.
    • ImageLayer_8bit: For handling standard bit depth layer data.
    • Enums: For constants like blend modes.
    • Utility: For helper functions.
  4. Understand the Pascal String format in Photoshop files

    master

    Photoshop uses Pascal strings to describe char[] arrays. These strings consist of a prefixed size byte followed by the string data, and they are often padded to reach a specific byte alignment.

    Structure Breakdown:

    • Size (1 byte, uint8_t): Specifies the number of characters in the string. Note that this value represents the data length and does not include the padding bytes. You must deduce the exact padding requirements from the specific file format documentation.
    • String (char[]): The actual string data encoded as characters.
    • Padding (void): Extra bytes added to align the total memory length. The padding applies to the sum of the uint8_t size marker and the string data.

    Example Calculation: If a string has a size marker of 62 and requires 2 bytes of padding, the total memory length will be 64 bytes (1 byte for size + 62 bytes for string + 2 bytes for padding = 65? No, the documentation states: 'a 62 long string (with size set to 62) and a padding of 2-bytes will end up having a total memory length of 64 as the combined size is 63-bytes not 62').

    Correction based on documentation logic: The total length is calculated as (1 + Size) + Padding.

  5. Understand the Transfer Function structure

    master

    The Transfer Function describes fixed points on a 13-segment curve used for color/tone mapping.

    Value Scaling

    Values are represented as integers ranging from 0 to 1000, where 1000 corresponds to 100.0%. For example, to represent 65.3%, use the value 653.

    Handling Empty Segments

    If a specific curve segment is empty, it must be marked with the value -1.

    Structure Layout

    The structure has a total size of 28 bytes. The following table defines the fields:

    SizeTypeVariableDescription
    2int16_t0 ValValue for the 0 Element (Required)
    2int16_t5 ValValue for the 5 Element (Optional)
    2int16_t10 ValValue for the 10 Element (Optional)
    2int16_t20 ValValue for the 20 Element (Optional)
    2int16_t30 ValValue for the 30 Element (Optional)
    2int16_t40 ValValue for the 40 Element (Optional)
    2int16_t50 ValValue for the 50 Element (Optional)
    2int16_t60 ValValue for the 60 Element (Optional)
    2int16_t70 ValValue for the 70 Element (Optional)
    2int16_t80 ValValue for the 80 Element (Optional)
    2int16_t90 ValValue for the 90 Element (Optional)
    2int16_t95 ValValue for the 95 Element (Optional)
    2int16_t100 ValValue for the 100 Element (Required)
    2boolPrinterCurve0 to use printer default curve; 1 to override with this curve
  6. Understand Smart Object Warp structures

    master

    The SmartObject namespace provides several specialized structures for defining warp transformations on Smart Objects. Depending on the desired effect, you can use one of the following three warp types:

    1. SmartObject::Warp: The standard warp structure for general mesh-based distortions.
    2. SmartObject::PuppetWarp: A specialized structure designed for puppet warp transformations, typically used for more complex, localized deformations.
    3. SmartObject::PerspectiveWarp: A structure specifically for applying perspective transformations to a Smart Object.
  7. Understand Photoshop compression behavior for 16- and 32-bit files

    master

    For 16-bit and 32-bit files, Photoshop uses prediction encoded zip compression for the main data, regardless of whether the compressed size is actually smaller than the uncompressed data.

    However, the merged image data section (used for compatibility) is always saved uncompressed. This can result in significantly larger file sizes because the merged section misses out on the performance benefits of Zip Prediction.

  8. How In-Memory Compression works in PhotoshopAPI

    master

    PhotoshopAPI uses the c-blosc2 library to perform on-the-fly compression and decompression of image data in system memory. This approach solves two problems:

    1. Memory Overhead: Storing uncompressed image data (especially for large PSB files with many layers) can lead to massive memory consumption. In-memory compression significantly reduces this footprint.
    2. Performance: Because the compression/decompression is highly optimized, it can actually be faster than a standard memcpy() when passing data, leading to faster overall parsing speeds.

    The core abstraction responsible for this behavior is the ImageChannel class, which is used internally by both LayeredFile and PhotoshopFile.