# acly/krita-ai-tools architecture

> Architecture diagram of the GitHub repository acly/krita-ai-tools, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/acly/krita-ai-tools
- Repository: https://github.com/acly/krita-ai-tools

## Overview

##### Main Components
- Segmentation Plugin Module: Represent the segmentation directory as the core of the plugin’s implementation. This module is subdivided into several components:
– SegmentationToolPlugin: The interface layer that connects the tool with Krita’s plugin API.
– SegmentationToolHelper: Provides support functionality for the segmentation tools.
– SegmentationToolShared: Contains code shared between different tools.
– Tools:
- SelectSegmentFromPointTool – Implements the “click to select” functionality.
- Resources and Configuration:
– JSON configuration file (kritatoolsegmentation.json) that likely describes tool properties and integration metadata.
- Build and Installation Scripts:
– CMakeLists.txt files (top-level for integration with Krita, and one inside the segmentation folder for building the plugin components).
– Installation scripts (“scripts/install-krita-with-segmentation.sh”) for patching Krita on Linux.
- External Dependencies and Models:
– External segmentation models (e.g., BiRefNet, Segment Anything Model, MobileSAM) that are downloaded/selected from the models directory in Krita’s asset location.
– GPU/CPU inference requirements and dependencies like CUDA, cuDNN in the Linux instructions.

##### Out Relationships and Interactions
- Within the segmentation module, create separate nodes for the user tools (point tool, rectangle tool) and indicate that both utilize common/shared classes (SegmentationToolHelper and SegmentationToolShared).
- A node for deployment/installation which involves build (CMake) and installation scripts that patch Krita.

##### Architectural Patterns and Design Principles
- Modular design pattern: the segmentation functionality is split into distinct components (helpers, shared code, individual tool implementations).
- Integration pattern: the plugin is built as an add-on alongside Krita via the CMake build process, meaning it seamlessly augments Krita’s existing architecture.
- Extensibility: configuration files (JSON and QRC) and potential for alternative segmentation models pave the way for future enhancements or substitutions of algorithms.

##### Technologies, Frameworks, and Libraries
- CMake – for building the plugin as part of Krita.
- Qt – inferred by the presence of .qrc files for resources.
- External AI models: references to Segment Anything, MobileSAM, BiRefNet, and an inference implementation (dlimgedit).
- CUDA/cuDNN – for GPU acceleration (as mentioned in the README for Linux GPU support).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Host Application
    subgraph "Krita Host"
        A["Krita Host Integration"]:::host
    end

    %% Plugin Module
    subgraph "Segmentation Plugin Module"
        B["Segmentation Plugin Module"]:::plugin
        B1["SegmentationToolPlugin"]:::plugin
        B2["SegmentationToolHelper"]:::helper
        B3["SegmentationToolShared"]:::helper
        B4["SelectSegmentFromPointTool"]:::pluginTool
        B5["SelectSegmentFromRectTool"]:::pluginTool
    end

    %% Resources and Configuration
    subgraph "Resources and Configuration"
        R1["Icons (Directory)"]:::resource
        R2["Qt Resource File (kritatoolsegmentation.qrc)"]:::resource
        R3["Configuration (kritatoolsegmentation.json)"]:::resource
    end

    %% Build and Installation Scripts
    subgraph "Build and Installation Scripts"
        S1["Build Script (segmentation/CMakeLists.txt)"]:::build
        S2["Installation Script (install-krita-with-segmentation.sh)"]:::build
    end

    %% External Dependencies
    subgraph "External Dependencies"
        EX["Segmentation Models & Dependencies"]:::external
    end

    %% Connections between components
    A -->|"CMakeIntegration"| B
    A -->|"buildsWith"| S1
    S1 -->|"integrates"| B

    B -->|"contains"| B1
    B -->|"contains"| B2
    B -->|"contains"| B3
    B -->|"contains"| B4
    B -->|"contains"| B5

    B1 -->|"uses"| B2
    B1 -->|"sharesWith"| B3
    B4 -->|"utilizes"| B2
    B4 -->|"utilizes"| B3
    B5 -->|"utilizes"| B2
    B5 -->|"utilizes"| B3

    B1 -->|"loads"| R2
    B1 -->|"reads"| R3

    B1 -->|"callsModels"| EX
    B4 -->|"callsModels"| EX
    B5 -->|"callsModels"| EX

    %% Click Events
    click A "https://github.com/acly/krita-ai-tools/blob/main/CMakeLists.txt"
    click B "https://github.com/acly/krita-ai-tools/tree/main/segmentation/"
    click B1 "https://github.com/acly/krita-ai-tools/blob/main/segmentation/SegmentationToolPlugin.cpp"
    click B2 "https://github.com/acly/krita-ai-tools/blob/main/segmentation/SegmentationToolHelper.cpp"
    click B3 "https://github.com/acly/krita-ai-tools/blob/main/segmentation/SegmentationToolShared.cpp"
    click B4 "https://github.com/acly/krita-ai-tools/blob/main/segmentation/SelectSegmentFromPointTool.cpp"
    click B5 "https://github.com/acly/krita-ai-tools/blob/main/segmentation/SelectSegmentFromRectTool.cpp"
    click R1 "https://github.com/acly/krita-ai-tools/tree/main/segmentation/icons/"
    click R2 "https://github.com/acly/krita-ai-tools/blob/main/segmentation/kritatoolsegmentation.qrc"
    click R3 "https://github.com/acly/krita-ai-tools/blob/main/segmentation/kritatoolsegmentation.json"
    click S1 "https://github.com/acly/krita-ai-tools/blob/main/segmentation/CMakeLists.txt"
    click S2 "https://github.com/acly/krita-ai-tools/blob/main/scripts/install-krita-with-segmentation.sh"

    %% Styles
    classDef host fill:#b3cde0,stroke:#03396c,stroke-width:2px;
    classDef plugin fill:#ccebc5,stroke:#2b9348,stroke-width:2px;
    classDef helper fill:#ffdf80,stroke:#d35400,stroke-width:2px;
    classDef pluginTool fill:#d5e8d4,stroke:#82b366,stroke-width:2px;
    classDef resource fill:#f4cccc,stroke:#cc0000,stroke-width:2px;
    classDef build fill:#fce5cd,stroke:#e69138,stroke-width:2px;
    classDef external fill:#d9d2e9,stroke:#8e44ad,stroke-width:2px;
```
