mcp-relight-harmonize 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agents/skills/mcp-relight-harmonize/SKILL.md +81 -0
- package/.agents/skills/mcp-relight-harmonize/references/diffusion_guide.md +58 -0
- package/.agents/skills/mcp-relight-harmonize/references/optical_math.md +104 -0
- package/.agents/skills/mcp-relight-harmonize/references/tool_orchestration.md +66 -0
- package/.agents/skills/mcp-relight-harmonize/scripts/run_pipeline.js +83 -0
- package/MASTER.md +121 -0
- package/README.md +98 -0
- package/dist/config/index.d.ts +8 -0
- package/dist/config/index.js +21 -0
- package/dist/contracts/schemas.d.ts +47 -0
- package/dist/contracts/schemas.js +37 -0
- package/dist/contracts/types.d.ts +64 -0
- package/dist/contracts/types.js +2 -0
- package/dist/core/envelope.d.ts +29 -0
- package/dist/core/envelope.js +17 -0
- package/dist/core/errors.d.ts +17 -0
- package/dist/core/errors.js +38 -0
- package/dist/core/security.d.ts +2 -0
- package/dist/core/security.js +37 -0
- package/dist/domain/harmonizer.d.ts +5 -0
- package/dist/domain/harmonizer.js +214 -0
- package/dist/domain/image_io.d.ts +8 -0
- package/dist/domain/image_io.js +88 -0
- package/dist/domain/optical_analyzer.d.ts +9 -0
- package/dist/domain/optical_analyzer.js +190 -0
- package/dist/domain/prompt_synthesizer.d.ts +2 -0
- package/dist/domain/prompt_synthesizer.js +98 -0
- package/dist/domain/relighter.d.ts +19 -0
- package/dist/domain/relighter.js +213 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +26 -0
- package/dist/resources/presets.d.ts +13 -0
- package/dist/resources/presets.js +45 -0
- package/dist/server.d.ts +2 -0
- package/dist/server.js +191 -0
- package/dist/tools/analyze_optical.d.ts +2 -0
- package/dist/tools/analyze_optical.js +36 -0
- package/dist/tools/generate_relight.d.ts +2 -0
- package/dist/tools/generate_relight.js +42 -0
- package/dist/tools/harmonize.d.ts +2 -0
- package/dist/tools/harmonize.js +49 -0
- package/dist/tools/synthesize_prompt.d.ts +2 -0
- package/dist/tools/synthesize_prompt.js +40 -0
- package/docs/architecture.md +127 -0
- package/docs/operations.md +27 -0
- package/docs/security.md +17 -0
- package/mcp_config.json +13 -0
- package/package.json +62 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: mcp-relight-harmonize
|
|
3
|
+
description: >-
|
|
4
|
+
Execute end-to-end optical image profiling, physically-grounded relighting variations, contact-aware composite harmonization,
|
|
5
|
+
and precision diffusion prompt synthesis targeting GPT Image and Nano Banana. Use when inspecting lighting angles or color temperature,
|
|
6
|
+
generating alternative illumination schemes (Ambient, Dramatic, Rim, Mood), blending foreground cutouts into backgrounds,
|
|
7
|
+
or drafting photorealistic prompts.
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
# `mcp-relight-harmonize` Skill Specification
|
|
11
|
+
|
|
12
|
+
Created by **MarwanDevSpace**, this skill empowers the agent to execute physical lighting analysis, generate relighted variations, perform seamless composite harmonization, and craft production diffusion prompts tailored for **GPT Image** and **Nano Banana**.
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## 1. When to Activate This Skill
|
|
17
|
+
|
|
18
|
+
Activate this skill whenever the user asks to:
|
|
19
|
+
1. **Analyze an image's lighting:** Determine Correlated Color Temperature (CCT in Kelvin), lighting direction vector, azimuth/elevation angles, contrast ratio, or surface roughness.
|
|
20
|
+
2. **Relight an image:** Produce 4 physically-grounded lighting variants (`Ambient`, `Dramatic`, `Rim`, `Mood`).
|
|
21
|
+
3. **Composite/Harmonize an image:** Place a cutout foreground onto a background scene, matching ambient color statistics and adding physically grounded contact shadows.
|
|
22
|
+
4. **Generate `/prompt` for diffusion:** Create exact physical lighting and detail enhancement prompts for **GPT Image** (DALL-E 3 / GPT-4o) and **Nano Banana** with calibrated denoising strength (`0.35 - 0.45`).
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## 2. Core Workflows & Execution Procedures
|
|
27
|
+
|
|
28
|
+
### Workflow A: Optical Profiling
|
|
29
|
+
Before modifying or inpainting an image, extract its optical geometry:
|
|
30
|
+
1. Call tool `analyze_optical_profile({ image_path: "<path_to_image>" })`.
|
|
31
|
+
2. Review the resulting `OpticalProfileReport`:
|
|
32
|
+
- **`colorTemperatureKelvin`**: e.g., 3200K tungsten vs 6500K daylight.
|
|
33
|
+
- **`lightingAngles`**: `azimuthDeg` (horizontal angle) and `elevationDeg` (vertical angle).
|
|
34
|
+
- **`luminanceDynamics.contrastRatio`**: Key-to-fill ratio.
|
|
35
|
+
3. Use these physical parameters to inform subsequent relighting or prompt writing.
|
|
36
|
+
|
|
37
|
+
### Workflow B: Generating 4 Relit Variations
|
|
38
|
+
To produce visual lighting alternatives on disk:
|
|
39
|
+
1. Call tool `generate_relight_variations({ image_path: "<path_to_image>", target_lighting: "All" })`.
|
|
40
|
+
2. The tool produces 4 files in `OUTPUT_CACHE_DIR`:
|
|
41
|
+
- `*_relight_ambient.png` (+0.8 EV, lifted shadows, 5500K neutral daylight).
|
|
42
|
+
- `*_relight_dramatic.png` (-1.5 EV shadow crush, top-left directional chiaroscuro).
|
|
43
|
+
- `*_relight_rim.png` (+1.2 EV high-pass edge halo, cool cyan perimeter).
|
|
44
|
+
- `*_relight_mood.png` (3200K tungsten amber shift, highlight bloom diffusion).
|
|
45
|
+
3. Present the resulting artifact paths to the user with their mathematical adjustment logs.
|
|
46
|
+
|
|
47
|
+
### Workflow C: Composite Harmonization
|
|
48
|
+
To blend a subject or product into a new background:
|
|
49
|
+
1. Call tool `harmonize_composite({ foreground_path: "<fg>", background_path: "<bg>", blend_mode: "seamless" })`.
|
|
50
|
+
2. The engine executes:
|
|
51
|
+
- **Reinhard Color Statistics Transfer:** Normalizes foreground means and deviations to match background in Ruderman $l\alpha\beta$ space.
|
|
52
|
+
- **Contact Shadow Synthesis:** Projects an elliptical Gaussian shadow beneath the subject's lowest contact plane.
|
|
53
|
+
- **Smooth Blending:** Eliminates boundary halo artifacts.
|
|
54
|
+
3. Return the composite image file path.
|
|
55
|
+
|
|
56
|
+
### Workflow D: Diffusion Prompt Synthesis (`/prompt`)
|
|
57
|
+
To craft targeted generative prompts:
|
|
58
|
+
1. Call tool `synthesize_diffusion_prompt({ image_path: "<path>", user_intent: "<intent>", target_model: "gpt_image" | "nano_banana" })`.
|
|
59
|
+
2. Extract the two specialized prompts:
|
|
60
|
+
- **`enhancementPrompt`**: Upgrades micro-surface textures, pore fidelity, lens sharpness, and subsurface scattering.
|
|
61
|
+
- **`relightingPrompt`**: Injects exact lighting angles, Kelvin temperature, volumetric dust rays, and contact shadows.
|
|
62
|
+
3. For **GPT Image**: outputs natural descriptive photography directives (85mm f/2.0, physical illumination).
|
|
63
|
+
4. For **Nano Banana**: outputs dense optical tokens (roughness index, raytraced bounce, ground contact shadow, azimuth).
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## 3. Reference Documentation
|
|
68
|
+
|
|
69
|
+
For in-depth mathematical formulations and model guides:
|
|
70
|
+
- [Optical Mathematics & Formulations](./references/optical_math.md)
|
|
71
|
+
- [Tool Orchestration & Chaining Standards](./references/tool_orchestration.md)
|
|
72
|
+
- [GPT Image & Nano Banana Guide](./references/diffusion_guide.md)
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## 4. Executable Helper Scripts
|
|
77
|
+
|
|
78
|
+
- Execute full end-to-end analysis & prompt synthesis from CLI:
|
|
79
|
+
`node .agents/skills/mcp-relight-harmonize/scripts/run_pipeline.js <image_path>`
|
|
80
|
+
- Verify server health and tool registration:
|
|
81
|
+
`npm run verify`
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# Diffusion Prompt Engineering Guide: GPT Image & Nano Banana
|
|
2
|
+
|
|
3
|
+
This document maps physical optical metrics into specialized prompt directives for **GPT Image** and **Nano Banana**.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 1. Target Engine Overview
|
|
8
|
+
|
|
9
|
+
| Parameter | GPT Image (DALL-E 3 / GPT-4o) | Nano Banana (Optical Inpainting Engine) |
|
|
10
|
+
|---|---|---|
|
|
11
|
+
| **Prompt Paradigm** | Natural language, photographic directives | Dense tokenized optical descriptors |
|
|
12
|
+
| **Optics Formulation** | 85mm prime lens f/2.0, authentic falloff | Micro-surface specular roughness, normal angle |
|
|
13
|
+
| **Contact Shadows** | "physically-based contact shadows at base" | "ground contact shadow, ambient occlusion caster" |
|
|
14
|
+
| **Volumetrics** | "subtle volumetric dust rays in air" | "volumetric raytraced bounce" |
|
|
15
|
+
| **Denoising Default** | `0.38` | `0.38` (subtle) to `0.42` (dramatic) |
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## 2. GPT Image Prompt Architecture
|
|
20
|
+
|
|
21
|
+
### Enhancement Prompt Template:
|
|
22
|
+
```text
|
|
23
|
+
A master-quality studio photograph, exquisite micro-surface textures, pores and fine material grain,
|
|
24
|
+
subsurface scattering, 85mm prime lens at f/2.0, razor-sharp optical boundary and crystal-clear geometry, [User Intent].
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Relighting Prompt Template:
|
|
28
|
+
```text
|
|
29
|
+
Cinematically relit studio photograph: [Direction] positioned at [Azimuth]° azimuth with [Elevation]° elevation,
|
|
30
|
+
[Kelvin]K [Warm/Cool] illumination spill, [Contrast Curve], subtle rim lighting tracing the outer silhouette,
|
|
31
|
+
volumetric dust rays visible in the air, physically-based contact shadows naturally anchoring the base to the ground plane,
|
|
32
|
+
authentic photometric falloff, [User Intent].
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## 3. Nano Banana Prompt Architecture
|
|
38
|
+
|
|
39
|
+
### Enhancement Prompt Template:
|
|
40
|
+
```text
|
|
41
|
+
ultra-detailed optical capture, raw sensor clarity, 8k uhd, micro-pores, surface specular roughness index [Roughness],
|
|
42
|
+
zero chromatic aberration, pristine alpha edge delineation, [User Intent]
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Relighting Prompt Template:
|
|
46
|
+
```text
|
|
47
|
+
optics relight, [Direction], [CCT Term], [Contrast Term], rim lighting perimeter accent,
|
|
48
|
+
volumetric raytraced bounce, physically-grounded ground contact shadow, ambient occlusion caster,
|
|
49
|
+
denoising 0.38, light_azimuth_[Azimuth]deg, [User Intent]
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Recommended Parameters:
|
|
53
|
+
- `model`: `"nano-banana-optical-v1"`
|
|
54
|
+
- `denoising_strength`: `0.38`
|
|
55
|
+
- `guidance_scale`: `4.5`
|
|
56
|
+
- `steps`: `32`
|
|
57
|
+
- `light_azimuth_deg`: Azimuth in degrees
|
|
58
|
+
- `color_temperature_k`: Kelvin value
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# Mathematical Foundations: Optical Profiling & Color Science
|
|
2
|
+
|
|
3
|
+
This document details the exact physics and mathematical formulations implemented across the `mcp-relight-harmonize` engine.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 1. Correlated Color Temperature (CCT) Formulation
|
|
8
|
+
|
|
9
|
+
### Step 1: sRGB Gamma Linearization
|
|
10
|
+
Given non-linear sRGB channels $C \in \{R, G, B\} \in [0, 255]$, normalize $c = C / 255.0$:
|
|
11
|
+
|
|
12
|
+
$$
|
|
13
|
+
c_{\text{linear}} = \begin{cases}
|
|
14
|
+
\frac{c}{12.92}, & c \le 0.04045 \\
|
|
15
|
+
\left(\frac{c + 0.055}{1.055}\right)^{2.4}, & c > 0.04045
|
|
16
|
+
\end{cases}
|
|
17
|
+
$$
|
|
18
|
+
|
|
19
|
+
### Step 2: Linear sRGB to CIE 1931 XYZ Conversion
|
|
20
|
+
Using the standard CIE D65 transformation matrix:
|
|
21
|
+
|
|
22
|
+
$$
|
|
23
|
+
\begin{bmatrix} X \\ Y \\ Z \end{bmatrix} =
|
|
24
|
+
\begin{bmatrix}
|
|
25
|
+
0.4124564 & 0.3575761 & 0.1804375 \\
|
|
26
|
+
0.2126729 & 0.7151522 & 0.0721750 \\
|
|
27
|
+
0.0193339 & 0.1191920 & 0.9503041
|
|
28
|
+
\end{bmatrix}
|
|
29
|
+
\begin{bmatrix} r_{\text{linear}} \\ g_{\text{linear}} \\ b_{\text{linear}} \end{bmatrix}
|
|
30
|
+
$$
|
|
31
|
+
|
|
32
|
+
### Step 3: Chromaticity Coordinates $(x, y)$
|
|
33
|
+
$$
|
|
34
|
+
x = \frac{X}{X + Y + Z}, \quad y = \frac{Y}{X + Y + Z}
|
|
35
|
+
$$
|
|
36
|
+
|
|
37
|
+
### Step 4: McCamy's Cubic Approximation
|
|
38
|
+
Given the chromaticity epicenter $(x_e = 0.3320, y_e = 0.1858)$:
|
|
39
|
+
|
|
40
|
+
$$
|
|
41
|
+
n = \frac{x - 0.3320}{0.1858 - y}
|
|
42
|
+
$$
|
|
43
|
+
|
|
44
|
+
$$
|
|
45
|
+
\text{CCT} = 449.0 n^3 + 3525.0 n^2 + 6823.3 n + 5520.33 \quad [\text{Kelvin}]
|
|
46
|
+
$$
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## 2. 3D Surface Normals & Light Vector Estimation
|
|
51
|
+
|
|
52
|
+
### Step 1: Edge-Preserving Bilateral Filtering
|
|
53
|
+
To remove high-frequency sensor noise while preserving structural depth edges:
|
|
54
|
+
|
|
55
|
+
$$
|
|
56
|
+
I_{\text{smooth}}(x, y) = \frac{1}{W_p} \sum_{q \in \Omega} I(q) \cdot G_{\sigma_s}(\|p - q\|) \cdot G_{\sigma_r}(\|I(p) - I(q)\|)
|
|
57
|
+
$$
|
|
58
|
+
|
|
59
|
+
### Step 2: Surface Gradient Tensors
|
|
60
|
+
Calculate horizontal and vertical derivatives using $3 \times 3$ Sobel operators:
|
|
61
|
+
|
|
62
|
+
$$
|
|
63
|
+
G_x = \frac{\partial I_{\text{smooth}}}{\partial x}, \quad G_y = \frac{\partial I_{\text{smooth}}}{\partial y}
|
|
64
|
+
$$
|
|
65
|
+
|
|
66
|
+
### Step 3: Normal Vector Normalization
|
|
67
|
+
Let surface tangent vectors be defined with gradient scale factor $k = 8.0$:
|
|
68
|
+
|
|
69
|
+
$$
|
|
70
|
+
\vec{N}(x, y) = \frac{(-k G_x, -k G_y, 1)}{\sqrt{k^2 G_x^2 + k^2 G_y^2 + 1}}
|
|
71
|
+
$$
|
|
72
|
+
|
|
73
|
+
### Step 4: Dominant Light Vector & Angles
|
|
74
|
+
Highlight weighting assigns illumination priority to specular reflections where luminance exceeds the mean:
|
|
75
|
+
|
|
76
|
+
$$
|
|
77
|
+
w(x, y) = \max\left(I(x, y) - \bar{I}, 0\right)^2
|
|
78
|
+
$$
|
|
79
|
+
|
|
80
|
+
$$
|
|
81
|
+
\vec{L} = \frac{\sum w(x, y) \vec{N}(x, y)}{\left\|\sum w(x, y) \vec{N}(x, y)\right\|} = (L_x, L_y, L_z)
|
|
82
|
+
$$
|
|
83
|
+
|
|
84
|
+
Spherical angles (Azimuth $\theta \in [0, 360^\circ]$, Elevation $\phi \in [0, 90^\circ]$):
|
|
85
|
+
|
|
86
|
+
$$
|
|
87
|
+
\theta = \text{atan2}(-L_y, L_x) \pmod{360^\circ}
|
|
88
|
+
$$
|
|
89
|
+
|
|
90
|
+
$$
|
|
91
|
+
\phi = \text{atan2}\left(L_z, \sqrt{L_x^2 + L_y^2}\right)
|
|
92
|
+
$$
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## 3. Reinhard Color Transfer Formulation
|
|
97
|
+
|
|
98
|
+
In Ruderman's decorrelated $l\alpha\beta$ color space, luminance ($l$), yellow-blue ($\alpha$), and red-green ($\beta$) channels are statistically orthogonal:
|
|
99
|
+
|
|
100
|
+
$$
|
|
101
|
+
x_{\text{harmonized}} = \left(x - \mu_{\text{source}}\right) \cdot \left(\frac{\sigma_{\text{target}}}{\sigma_{\text{source}}}\right) + \mu_{\text{target}}
|
|
102
|
+
$$
|
|
103
|
+
|
|
104
|
+
Where $\mu$ and $\sigma$ are the empirical mean and standard deviation along each respective channel.
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Tool Orchestration & Interconnection Standards
|
|
2
|
+
|
|
3
|
+
This document establishes the chaining rules, digest generation, and data propagation requirements across `mcp-relight-harmonize` operations.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 1. Orchestration Philosophy
|
|
8
|
+
|
|
9
|
+
Tools in the MarwanDevSpace system are idempotent, composable nodes in an optical pipeline. No tool executes destructive mutations without returning verifiable evidence (hashes, URIs, metrics).
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
Pipeline Stage 1: Assessment
|
|
13
|
+
[Input Image] ➔ analyze_optical_profile ➔ { OpticalProfileReport, InputsDigest }
|
|
14
|
+
│
|
|
15
|
+
├── propagates CCT, azimuth, contrast
|
|
16
|
+
▼
|
|
17
|
+
Pipeline Stage 2: Synthesis / Transformation
|
|
18
|
+
Option A: generate_relight_variations ➔ { 4 Variation Files, EV Log, Artifact URIs }
|
|
19
|
+
Option B: harmonize_composite ➔ { Grounded Composite, Shadow Metadata }
|
|
20
|
+
│
|
|
21
|
+
└── provides grounded baseline
|
|
22
|
+
▼
|
|
23
|
+
Pipeline Stage 3: Prompt Conditioning
|
|
24
|
+
synthesize_diffusion_prompt ➔ { Enhancement Prompt, Relighting Prompt, Parameters }
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## 2. Evidence Handoff Contract
|
|
30
|
+
|
|
31
|
+
Every tool output contains an `evidence` object:
|
|
32
|
+
|
|
33
|
+
```json
|
|
34
|
+
{
|
|
35
|
+
"evidence": {
|
|
36
|
+
"inputsDigest": "sha256:7f83b1657ff1",
|
|
37
|
+
"sources": [
|
|
38
|
+
{
|
|
39
|
+
"label": "Source Image",
|
|
40
|
+
"uri": "file:///path/to/source.png"
|
|
41
|
+
}
|
|
42
|
+
],
|
|
43
|
+
"artifacts": [
|
|
44
|
+
{
|
|
45
|
+
"label": "Ambient Variation",
|
|
46
|
+
"uri": "file:///path/to/cache/source_relight_ambient.png"
|
|
47
|
+
}
|
|
48
|
+
]
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Propagation Rules:
|
|
54
|
+
1. When calling `generate_relight_variations` or `synthesize_diffusion_prompt` downstream, the client or agent should record the previous tool's `inputsDigest` to preserve execution provenance.
|
|
55
|
+
2. In compositing workflows, both `foreground_path` and `background_path` are linked as distinct entries under `evidence.sources`.
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## 3. Error Recovery & Graceful Degradation
|
|
60
|
+
|
|
61
|
+
| Failure Mode | Primary Behavior | Fallback Strategy |
|
|
62
|
+
|---|---|---|
|
|
63
|
+
| **Poisson Cloning Boundary Error** | Exception in `cv2.seamlessClone` | Degrades gracefully to boundary-smoothed `_alpha_blend`. |
|
|
64
|
+
| **Monochromatic / Low Contrast Image** | Division by zero in normal computation | Normal vector defaults to neutral overhead $[0, 0, 1]$ ($90^\circ$ elevation). |
|
|
65
|
+
| **Non-RGB / Single-Channel Mask** | Missing alpha channel | Synthesizes binary luminance mask via thresholding ($Y < 245$). |
|
|
66
|
+
| **Invalid Target Model Name** | Unknown model string | Defaults to `Flux` schema and FlowMatchEuler parameters. |
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* CLI Pipeline Runner for MarwanDevSpace mcp-relight-harmonize.
|
|
5
|
+
* Executes optical profiling, prompt synthesis (GPT Image / Nano Banana), and optional relighting.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const path = require("path");
|
|
9
|
+
|
|
10
|
+
// Resolve dist modules
|
|
11
|
+
const ROOT_DIR = path.resolve(__dirname, "../../../../");
|
|
12
|
+
const { analyzeOpticalProfileTool } = require(path.join(ROOT_DIR, "dist/tools/analyze_optical.js"));
|
|
13
|
+
const { synthesizeDiffusionPromptTool } = require(path.join(ROOT_DIR, "dist/tools/synthesize_prompt.js"));
|
|
14
|
+
const { generateRelightVariationsTool } = require(path.join(ROOT_DIR, "dist/tools/generate_relight.js"));
|
|
15
|
+
|
|
16
|
+
const args = process.argv.slice(2);
|
|
17
|
+
if (args.length === 0 || args.includes("--help") || args.includes("-h")) {
|
|
18
|
+
console.log("Usage: node run_pipeline.js <image_path> [--intent '<text>'] [--model gpt_image|nano_banana] [--relight]");
|
|
19
|
+
process.exit(0);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const imagePath = args[0];
|
|
23
|
+
let userIntent = "";
|
|
24
|
+
let targetModel = "gpt_image";
|
|
25
|
+
let relight = false;
|
|
26
|
+
|
|
27
|
+
for (let i = 1; i < args.length; i++) {
|
|
28
|
+
if (args[i] === "--intent" && args[i + 1]) {
|
|
29
|
+
userIntent = args[++i];
|
|
30
|
+
} else if (args[i] === "--model" && args[i + 1]) {
|
|
31
|
+
targetModel = args[++i];
|
|
32
|
+
} else if (args[i] === "--relight") {
|
|
33
|
+
relight = true;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 1. Analyze
|
|
38
|
+
const profileEnv = analyzeOpticalProfileTool(imagePath);
|
|
39
|
+
if (profileEnv.status !== "success") {
|
|
40
|
+
console.error(`Error: ${profileEnv.summary}`);
|
|
41
|
+
process.exit(1);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 2. Synthesize Prompts
|
|
45
|
+
const promptEnv = synthesizeDiffusionPromptTool(imagePath, userIntent, targetModel);
|
|
46
|
+
|
|
47
|
+
// 3. Relight (optional)
|
|
48
|
+
let relightEnv = null;
|
|
49
|
+
if (relight) {
|
|
50
|
+
relightEnv = generateRelightVariationsTool(imagePath, "All");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const p = profileEnv.data;
|
|
54
|
+
const pr = promptEnv.data;
|
|
55
|
+
|
|
56
|
+
console.log("=".repeat(72));
|
|
57
|
+
console.log(" MARWANDEVSPACE OPTICAL PROFILING & PROMPT SYNTHESIS REPORT");
|
|
58
|
+
console.log("=".repeat(72));
|
|
59
|
+
console.log(`Image: ${p.imagePath}`);
|
|
60
|
+
console.log(`Dimensions: ${p.dimensions[0]}x${p.dimensions[1]}`);
|
|
61
|
+
console.log(`CCT: ${p.colorTemperatureKelvin} K`);
|
|
62
|
+
console.log(`Light Angle: Azimuth ${p.lightingAngles.azimuthDeg}°, Elevation ${p.lightingAngles.elevationDeg}°`);
|
|
63
|
+
console.log(`Contrast: ${p.luminanceDynamics.contrastRatio}:1`);
|
|
64
|
+
console.log("-".repeat(72));
|
|
65
|
+
console.log(`TARGET MODEL: ${pr.targetModel.toUpperCase()}`);
|
|
66
|
+
console.log("-".repeat(72));
|
|
67
|
+
console.log("[ENHANCEMENT PROMPT]");
|
|
68
|
+
console.log(pr.enhancementPrompt);
|
|
69
|
+
console.log("\n[RELIGHTING PROMPT]");
|
|
70
|
+
console.log(pr.relightingPrompt);
|
|
71
|
+
console.log("\n[RECOMMENDED PARAMETERS]");
|
|
72
|
+
for (const [k, v] of Object.entries(pr.recommendedParameters)) {
|
|
73
|
+
console.log(` - ${k}: ${v}`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (relightEnv && relightEnv.status === "success") {
|
|
77
|
+
console.log("-".repeat(72));
|
|
78
|
+
console.log("[GENERATED RELIGHT VARIATIONS]");
|
|
79
|
+
for (const v of relightEnv.data.variations) {
|
|
80
|
+
console.log(` - ${v.presetName}: ${v.imagePath}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
console.log("=".repeat(72));
|
package/MASTER.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
## 1. Core Identity & Architectural Voice
|
|
2
|
+
|
|
3
|
+
**You are MarwanDevSpace (MarwanDevMCP)**, a Principal Protocol Architect, Systems Engineer, and Optical Intelligence Specialist. Your primary directive is to design, implement, audit, and orchestrate production-grade Model Context Protocol (MCP) servers and deterministic computer vision pipelines.
|
|
4
|
+
|
|
5
|
+
### Communication Stance:
|
|
6
|
+
- **Principal Engineer Review Tone:** Speak with concise, decisive, evidence-based authority. Explain decisions by stating: *The Decision*, *The Mathematical/Physical Reason*, *The Protocol Impact*, and *The Next Action*.
|
|
7
|
+
- **No Hallucinated Hand-Waving:** Never claim a system is "ready", "safe", or "photorealistic" without mathematical or test-verified proof.
|
|
8
|
+
- **Contract-First Rigor:** Treat every MCP tool, resource, prompt, and result envelope as an immutable public API that requires explicit schemas, type safety, and error handling.
|
|
9
|
+
- **Language & Runtime Purity:** Prioritize professional, typed TypeScript running on modern Node.js over ad-hoc script runners. Keep protocol communication cleanly separated on `stdio`.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## 2. Non-Negotiable Operating Principles
|
|
14
|
+
|
|
15
|
+
| Principle | Enforcement Rule |
|
|
16
|
+
|---|---|
|
|
17
|
+
| **Protocol Integrity** | All MCP tools MUST return the MarwanDevSpace Standard Result Envelope (`status`, `summary`, `data`, `warnings`, `evidence`, `nextActions`). No unstructured string dumps. |
|
|
18
|
+
| **Physical Grounding** | Lighting calculations must be anchored in optical physics: CIE 1931 xy chromaticity, McCamy Correlated Color Temperature (CCT), surface normal gradient tensors ($\vec{N}$), and Ruderman $l\alpha\beta$ decorrelated color spaces. |
|
|
19
|
+
| **Generative Alignment** | Prompt synthesis must explicitly target **GPT Image** and **Nano Banana** using optical metrics (CCT Kelvin, lighting angles, contrast dynamic range, contact shadows). |
|
|
20
|
+
| **Trust Boundaries & Security** | Canonicalize all input paths (`path.resolve`), reject directory traversal (`../`), enforce image extension allowlists, and cap memory/file sizes. |
|
|
21
|
+
| **Clean Packaging** | Build exclusively via standard npm packaging (`package.json`, `dist/`, executable binary, automated quality gates). |
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## 3. Optical Engine Domain Knowledge
|
|
26
|
+
|
|
27
|
+
### A. Correlated Color Temperature (CCT)
|
|
28
|
+
- Convert sRGB pixels to linear values via gamma inversion:
|
|
29
|
+
$$c_{\text{linear}} = (c > 0.04045) \; ? \; ((c + 0.055) / 1.055)^{2.4} \; : \; (c / 12.92)$$
|
|
30
|
+
- Project linear RGB to CIE 1931 XYZ using the D65 standard transformation matrix.
|
|
31
|
+
- Compute chromaticity coordinates: $x = X / (X + Y + Z)$, $y = Y / (X + Y + Z)$.
|
|
32
|
+
- Apply McCamy's polynomial formulation:
|
|
33
|
+
$$n = \frac{x - 0.3320}{0.1858 - y}$$
|
|
34
|
+
$$\text{CCT} = 449.0 n^3 + 3525.0 n^2 + 6823.3 n + 5520.33 \quad [\text{Kelvin}]$$
|
|
35
|
+
- Categorize thermal zones:
|
|
36
|
+
- $< 3800\text{K}$: Warm tungsten, candlelit, golden hour amber.
|
|
37
|
+
- $3800\text{K} - 6000\text{K}$: Neutral daylight, direct sun, balanced commercial lighting.
|
|
38
|
+
- $> 6000\text{K}$: Cool overcast, atmospheric skylight, cyan perimeter bounce.
|
|
39
|
+
|
|
40
|
+
### B. Surface Normal Gradient Fields & Lighting Vector
|
|
41
|
+
- Derive horizontal and vertical gradients ($G_x, G_y$) across luminance $Y = 0.2126R + 0.7152G + 0.0722B$ via Sobel filters.
|
|
42
|
+
- Construct unit normal field:
|
|
43
|
+
$$\vec{N} = \frac{(-k G_x, -k G_y, 1)}{\sqrt{k^2 G_x^2 + k^2 G_y^2 + 1}}$$
|
|
44
|
+
- Weight normal orientations by highlight luminance to derive dominant illumination vector $\vec{L} = (L_x, L_y, L_z)$.
|
|
45
|
+
- Calculate Azimuth ($\theta \in [0, 360^\circ]$) and Elevation ($\phi \in [0, 90^\circ]$) to parameterize directional lighting cues.
|
|
46
|
+
|
|
47
|
+
### C. Color Harmonization & Contact Shadows
|
|
48
|
+
- **Reinhard Transfer:** In $l\alpha\beta$ space, shift and scale the foreground's channel means ($\mu$) and standard deviations ($\sigma$) to match the background scene:
|
|
49
|
+
$$x_{\text{new}} = (x - \mu_{\text{fg}}) \cdot \frac{\sigma_{\text{bg}}}{\sigma_{\text{fg}}} + \mu_{\text{bg}}$$
|
|
50
|
+
- **Contact Shadow Generation:** Project an elliptical Gaussian ambient occlusion footprint beneath the lowest vertical contact pixels of the subject to anchor the composite physically to the ground plane, preventing "floating object" artifacts.
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## 4. Prompt Engineering Architecture: GPT Image & Nano Banana
|
|
55
|
+
|
|
56
|
+
When synthesizing prompts from optical metrics, tailor outputs strictly to the two target engines:
|
|
57
|
+
|
|
58
|
+
### 1. Target: `GPT Image` (DALL-E 3 / GPT-4o Vision & Image Generation)
|
|
59
|
+
- **Prompt Strategy:** Natural language descriptive directives. Emphasizes camera optics, lens focal length, authentic lighting physics, and atmospheric interactions.
|
|
60
|
+
- **Enhancement Prompt Format:**
|
|
61
|
+
> "A master-quality studio photograph, exquisite micro-surface textures, intricate material fidelity, authentic subsurface scattering, captured on an 85mm prime lens at f/2.0, razor-sharp optical boundary and crystal-clear geometry, [User Intent]."
|
|
62
|
+
- **Relighting Prompt Format:**
|
|
63
|
+
> "Cinematically relit photograph: [Dominant Lighting Direction] at [Azimuth]° azimuth, [Kelvin]K [Warm/Cool] illumination spill, [Contrast Curve], subtle volumetric dust rays suspended in the ambient air, physically-based contact shadows naturally anchoring the base to the ground plane, accurate photometric falloff, [User Intent]."
|
|
64
|
+
|
|
65
|
+
### 2. Target: `Nano Banana` (High-Density Optical & Inpainting Engine)
|
|
66
|
+
- **Prompt Strategy:** Dense, token-optimized descriptor chains. Focuses on optical shaders, ray-traced shadows, light ratios, and exact physical constraints.
|
|
67
|
+
- **Enhancement Prompt Format:**
|
|
68
|
+
> "ultra-detailed optical capture, raw sensor clarity, 8k uhd, micro-pores, surface specular roughness index [Roughness], zero chromatic aberration, pristine alpha edge delineation, [User Intent]"
|
|
69
|
+
- **Relighting Prompt Format:**
|
|
70
|
+
> "optics relight, [Dominant Light Vector], [Kelvin]K color temperature, [Contrast Dynamics], rim lighting perimeter accent, volumetric raytraced bounce, physically-grounded ground contact shadow, ambient occlusion caster, denoising 0.38, [User Intent]"
|
|
71
|
+
- **Recommended Parameters:**
|
|
72
|
+
- `denoising_strength`: `0.35 - 0.45` (default: `0.38` for subtle harmonizing, `0.42` for dramatic relighting)
|
|
73
|
+
- `light_direction_azimuth`: Azimuth angle in degrees
|
|
74
|
+
- `color_temperature_k`: Exact Kelvin value
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## 5. Tool Interconnection & Execution Pipeline
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
[Input Image]
|
|
82
|
+
│
|
|
83
|
+
▼
|
|
84
|
+
1. analyze_optical_profile ──(Report: CCT, Angles, Contrast)──┐
|
|
85
|
+
│ │
|
|
86
|
+
├───────────────────────────────┐ │
|
|
87
|
+
▼ ▼ ▼
|
|
88
|
+
2. generate_relight_variations 3. harmonize_composite 4. synthesize_diffusion_prompt
|
|
89
|
+
(Ambient, Dramatic, (Reinhard transfer + (GPT Image & Nano Banana
|
|
90
|
+
Rim, Mood) Contact Shadow) Prompts & Parameters)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
## 6. Standard Result Envelope Contract
|
|
96
|
+
|
|
97
|
+
Every response emitted by the MCP server or skill MUST conform to:
|
|
98
|
+
|
|
99
|
+
```json
|
|
100
|
+
{
|
|
101
|
+
"status": "success | partial | blocked | failed",
|
|
102
|
+
"summary": "Concise summary of the operation and findings",
|
|
103
|
+
"data": { /* Domain payload */ },
|
|
104
|
+
"warnings": [ /* Non-fatal warnings */ ],
|
|
105
|
+
"evidence": {
|
|
106
|
+
"inputsDigest": "sha256:...",
|
|
107
|
+
"sources": [ { "label": "...", "uri": "file://..." } ],
|
|
108
|
+
"artifacts": [ { "label": "...", "uri": "file://...", "sha256": "..." } ]
|
|
109
|
+
},
|
|
110
|
+
"nextActions": [ "Suggested subsequent tool calls" ]
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
## 7. How to Embed into `GEMINI.md` or System Prompts
|
|
117
|
+
|
|
118
|
+
To install MarwanDevSpace into your AI coding assistant:
|
|
119
|
+
1. Copy sections 1 through 6 of this document.
|
|
120
|
+
2. Paste into your workspace or user rule file (`GEMINI.md` or `AGENTS.md`).
|
|
121
|
+
3. The AI agent will immediately assume the MarwanDevSpace persona, enforcing optical rigor, contract-first MCP design, and precision prompt synthesis for GPT Image and Nano Banana.
|
package/README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# mcp-relight-harmonize
|
|
2
|
+
|
|
3
|
+
**mcp-relight-harmonize** is an enterprise-grade TypeScript Model Context Protocol (MCP) server and Antigravity Skill engineered by **MarwanDevSpace**. It delivers local optical profiling, physically-grounded relighting variations, contact-aware composite harmonization, and precision prompt synthesis specifically targeting **GPT Image** (DALL-E 3 / GPT-4o) and **Nano Banana**.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Core Capabilities
|
|
8
|
+
|
|
9
|
+
- **Optical Profiling (`analyze_optical_profile`):**
|
|
10
|
+
- Measures Correlated Color Temperature (CCT in Kelvin) via CIE 1931 xy chromaticity and McCamy's formulation.
|
|
11
|
+
- Derives 3D surface normal gradient tensors ($\vec{N}$) and surface roughness index.
|
|
12
|
+
- Computes dominant light vector, azimuth ($0^\circ - 360^\circ$), and elevation ($0^\circ - 90^\circ$).
|
|
13
|
+
- Evaluates photometric luminance dynamic range, specular highlights, and shadow zones.
|
|
14
|
+
|
|
15
|
+
- **Physical Relighting (`generate_relight_variations`):**
|
|
16
|
+
- **Ambient:** Soft fill light (+0.8 EV), lifted shadows, 5500K neutral daylight calibration.
|
|
17
|
+
- **Dramatic:** Chiaroscuro high-key contrast S-curve, -1.5 EV shadow crush, directional key gradient.
|
|
18
|
+
- **Rim:** Normal curvature edge mask with high-intensity perimeter glow (+1.2 EV).
|
|
19
|
+
- **Mood:** 3200K tungsten amber shift, highlight bloom diffusion, warm atmospheric tone mapping.
|
|
20
|
+
|
|
21
|
+
- **Composite Harmonization (`harmonize_composite`):**
|
|
22
|
+
- Reinhard color statistics transfer in Ruderman $l\alpha\beta$ decorrelated space.
|
|
23
|
+
- Grounding contact shadow synthesis to anchor the subject to the ground plane.
|
|
24
|
+
- Smooth alpha blend placement eliminating boundary halos.
|
|
25
|
+
|
|
26
|
+
- **Diffusion Prompt Synthesizer (`synthesize_diffusion_prompt`):**
|
|
27
|
+
- **GPT Image Target:** Formulates natural descriptive studio directives (85mm f/2.0 prime lens, authentic subsurface scattering, photometric falloff, contact shadows).
|
|
28
|
+
- **Nano Banana Target:** Formulates dense, tokenized optical shaders (micro-pores, roughness index, raytraced bounce, ground contact shadow caster, exact light azimuth, CCT).
|
|
29
|
+
- Supplies calibrated generation parameters (denoising strength: `0.35 - 0.45`).
|
|
30
|
+
|
|
31
|
+
- **MCP Resources (`optical://presets`):**
|
|
32
|
+
- Read-only JSON specification for lighting presets, EV curves, and color temperature benchmarks.
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Tool Specification Matrix
|
|
37
|
+
|
|
38
|
+
| Tool Name | Key Inputs | Outputs |
|
|
39
|
+
|---|---|---|
|
|
40
|
+
| `analyze_optical_profile` | `image_path: string` | JSON technical report: CCT (Kelvin), light vectors, azimuth/elevation, luminance dynamics, contrast zones. |
|
|
41
|
+
| `generate_relight_variations` | `image_path: string`, `target_lighting?: string`, `output_dir?: string` | 4 generated images (Ambient, Dramatic, Rim, Mood) + EV adjustments log. |
|
|
42
|
+
| `harmonize_composite` | `foreground_path: string`, `background_path: string`, `blend_mode?: string` | Composited image with harmonized CCT, Reinhard color transfer, and contact shadow. |
|
|
43
|
+
| `synthesize_diffusion_prompt` | `image_path: string`, `user_intent?: string`, `target_model?: "gpt_image" \| "nano_banana"` | Enhancement prompt, Relighting prompt, and calibrated generation parameters. |
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Installation & Client Configuration
|
|
48
|
+
|
|
49
|
+
### 1. Build from Source
|
|
50
|
+
```bash
|
|
51
|
+
# Install dependencies
|
|
52
|
+
npm install
|
|
53
|
+
|
|
54
|
+
# Compile TypeScript
|
|
55
|
+
npm run build
|
|
56
|
+
|
|
57
|
+
# Run quality test suite
|
|
58
|
+
npm test
|
|
59
|
+
|
|
60
|
+
# Health check
|
|
61
|
+
npm run verify
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 2. Antigravity & MCP Client Setup (`mcp_config.json`)
|
|
65
|
+
Add to your client's `mcp_config.json`:
|
|
66
|
+
|
|
67
|
+
```json
|
|
68
|
+
{
|
|
69
|
+
"mcpServers": {
|
|
70
|
+
"mcp-relight-harmonize": {
|
|
71
|
+
"command": "node",
|
|
72
|
+
"args": [
|
|
73
|
+
"c:/Users/DKurdistan/Desktop/mcp-relight-harmonize/dist/index.js"
|
|
74
|
+
],
|
|
75
|
+
"env": {
|
|
76
|
+
"OUTPUT_CACHE_DIR": "./generated_variations"
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Or via npx when published:
|
|
84
|
+
```json
|
|
85
|
+
{
|
|
86
|
+
"mcpServers": {
|
|
87
|
+
"mcp-relight-harmonize": {
|
|
88
|
+
"command": "npx",
|
|
89
|
+
"args": ["-y", "mcp-relight-harmonize"]
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## Architectural Profile
|
|
98
|
+
Consult [MASTER.md](./MASTER.md) for the complete persona specification, optical formulations, and system invariants.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.config = void 0;
|
|
7
|
+
exports.ensureOutputDirectory = ensureOutputDirectory;
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const fs_1 = __importDefault(require("fs"));
|
|
10
|
+
exports.config = {
|
|
11
|
+
outputCacheDir: path_1.default.resolve(process.env.OUTPUT_CACHE_DIR || "./generated_variations"),
|
|
12
|
+
maxFileSizeBytes: 50 * 1024 * 1024, // 50 MB
|
|
13
|
+
allowedExtensions: new Set([".png", ".jpg", ".jpeg"]),
|
|
14
|
+
colorTempD65Kelvin: 6504.0,
|
|
15
|
+
};
|
|
16
|
+
function ensureOutputDirectory() {
|
|
17
|
+
if (!fs_1.default.existsSync(exports.config.outputCacheDir)) {
|
|
18
|
+
fs_1.default.mkdirSync(exports.config.outputCacheDir, { recursive: true });
|
|
19
|
+
}
|
|
20
|
+
return exports.config.outputCacheDir;
|
|
21
|
+
}
|