{
    "sourceFile": "Tickets/ISSUE-010-color-channel-regression.md",
    "activeCommit": 0,
    "commits": [
        {
            "activePatchIndex": 2,
            "patches": [
                {
                    "date": 1745335634854,
                    "content": "Index: \n===================================================================\n--- \n+++ \n"
                },
                {
                    "date": 1745335788240,
                    "content": "Index: \n===================================================================\n--- \n+++ \n@@ -41,5 +41,36 @@\n *   [ ] Process an asset requiring map merging (e.g., ORM from Roughness, Metallic, AO). Verify the output merged map has correct RGB(A) channel order.\r\n *   [ ] Verify image statistics in `metadata.json` remain correct (reflecting RGB values).\r\n *   [ ] Verify grayscale maps are processed and saved correctly.\r\n *   [ ] Verify normal maps are processed and saved correctly (as per ISSUE-004 fix).\r\n-*   [ ] (Optional) Verify RGBA maps are processed and saved correctly.\n\\ No newline at end of file\n+*   [ ] (Optional) Verify RGBA maps are processed and saved correctly.\r\n+---\r\n+\r\n+## Implementation Plan (Generated 2025-04-22)\r\n+\r\n+**Goal:** Correct the BGR/RGB channel order regression for saved color maps (introduced by the `ISSUE-004` fix) while maintaining the correct handling for statistics, grayscale maps, and EXR files.\r\n+\r\n+**Core Idea:** Convert the image data back from RGB to BGR *just before* saving with `cv2.imwrite`, but only for 3-channel images and formats where this conversion is necessary (e.g., PNG, JPG, TIF, but *not* EXR).\r\n+\r\n+**Plan Steps:**\r\n+\r\n+1.  **Modify `_process_maps` Saving Logic (`asset_processor.py`):**\r\n+    *   Before the primary `cv2.imwrite` call (around line 1182) and the fallback call (around line 1206), add logic to check if the image is 3-channel and the output format is *not* 'exr'. If true, convert the image from RGB to BGR using `cv2.cvtColor` and add a debug log message.\r\n+2.  **Modify `_merge_maps` Saving Logic (`asset_processor.py`):**\r\n+    *   Apply the same conditional RGB to BGR conversion logic before the primary `cv2.imwrite` call (around line 1505) and the fallback call (around line 1531), including a debug log message.\r\n+3.  **Verification Strategy:**\r\n+    *   Test with various 3-channel color maps (Albedo, Emission, etc.) and merged maps (NRMRGH, etc.) saved as PNG/JPG/TIF.\r\n+    *   Verify correct RGB order in outputs.\r\n+    *   Verify grayscale, EXR, and statistics calculation remain correct.\r\n+\r\n+**Mermaid Diagram:**\r\n+\r\n+```mermaid\r\n+graph TD\r\n+    subgraph \"_process_maps / _merge_maps Saving Step\"\r\n+        A[Prepare Final Image Data (in RGB)] --> B{Is it 3-Channel?};\r\n+        B -- Yes --> C{Output Format != 'exr'?};\r\n+        B -- No --> E[Save Image using cv2.imwrite];\r\n+        C -- Yes --> D(Convert Image RGB -> BGR);\r\n+        C -- No --> E;\r\n+        D --> E;\r\n+    end\n\\ No newline at end of file\n"
                },
                {
                    "date": 1745339075067,
                    "content": "Index: \n===================================================================\n--- \n+++ \n@@ -1,10 +1,10 @@\n ---\r\n ID: ISSUE-010\r\n Type: Issue\r\n-Status: Backlog\r\n+Status: Resolved\r\n Priority: High\r\n-Labels: [bug, core, image-processing, regression]\r\n+Labels: [bug, core, image-processing, regression, resolved]\r\n Created: 2025-04-22\r\n Updated: 2025-04-22\r\n Related: #ISSUE-004, asset_processor.py\r\n ---\r\n@@ -36,16 +36,23 @@\n - Review `_merge_maps`: How are channels combined? Does the saving logic here also need adjustment?\r\n - Determine if the BGR->RGB conversion should be conditional or if a final RGB->BGR conversion is needed before saving specific map types.\r\n \r\n ## Acceptance Criteria (Optional)\r\n-*   [ ] Process an asset containing standard RGB maps (e.g., Color/Albedo). Verify the output map has correct RGB channel order.\r\n-*   [ ] Process an asset requiring map merging (e.g., ORM from Roughness, Metallic, AO). Verify the output merged map has correct RGB(A) channel order.\r\n-*   [ ] Verify image statistics in `metadata.json` remain correct (reflecting RGB values).\r\n-*   [ ] Verify grayscale maps are processed and saved correctly.\r\n-*   [ ] Verify normal maps are processed and saved correctly (as per ISSUE-004 fix).\r\n+*   [x] Process an asset containing standard RGB maps (e.g., Color/Albedo). Verify the output map has correct RGB channel order.\r\n+*   [x] Process an asset requiring map merging (e.g., ORM from Roughness, Metallic, AO). Verify the output merged map has correct RGB(A) channel order.\r\n+*   [x] Verify image statistics in `metadata.json` remain correct (reflecting RGB values).\r\n+*   [x] Verify grayscale maps are processed and saved correctly.\r\n+*   [x] Verify normal maps are processed and saved correctly (as per ISSUE-004 fix).\r\n *   [ ] (Optional) Verify RGBA maps are processed and saved correctly.\r\n ---\r\n \r\n+## Resolution\r\n+The issue was resolved by implementing conditional RGB to BGR conversion immediately before saving images using `cv2.imwrite` within the `_process_maps` and `_merge_maps` methods in `asset_processor.py`.\r\n+\r\n+The logic checks if the image is 3-channel and if the target output format is *not* EXR. If both conditions are true, the image data is converted from the internal RGB representation back to BGR, which is the expected channel order for `cv2.imwrite` when saving formats like PNG, JPG, and TIF.\r\n+\r\n+This approach ensures that color maps are saved with the correct channel order in standard formats while leaving EXR files (which handle RGB correctly) and grayscale/single-channel images unaffected. It also preserves the internal RGB representation used for accurate image statistics calculation and channel merging, thus not reintroducing the issues fixed by `ISSUE-004`.\r\n+\r\n ## Implementation Plan (Generated 2025-04-22)\r\n \r\n **Goal:** Correct the BGR/RGB channel order regression for saved color maps (introduced by the `ISSUE-004` fix) while maintaining the correct handling for statistics, grayscale maps, and EXR files.\r\n \r\n"
                }
            ],
            "date": 1745335634854,
            "name": "Commit-0",
            "content": "---\r\nID: ISSUE-010\r\nType: Issue\r\nStatus: Backlog\r\nPriority: High\r\nLabels: [bug, core, image-processing, regression]\r\nCreated: 2025-04-22\r\nUpdated: 2025-04-22\r\nRelated: #ISSUE-004, asset_processor.py\r\n---\r\n\r\n# [ISSUE-010]: Color Channel Swapping Regression for RGB/Merged Maps after ISSUE-004 Fix\r\n\r\n## Description\r\nThe resolution implemented for `ISSUE-004` successfully corrected the BGR/RGB channel handling for image statistics calculation and potentially normal map packing. However, this fix appears to have introduced a regression where standard RGB color maps and maps generated through merging operations are now being saved with swapped Blue and Red channels (BGR order) instead of the expected RGB order.\r\n\r\n## Current Behavior\r\n- Standard RGB texture maps (e.g., Diffuse, Albedo) loaded and processed are saved with BGR channel order.\r\n- Texture maps created by merging channels (e.g., ORM, NRMRGH) are saved with BGR channel order for their color components.\r\n- Image statistics (`metadata.json`) are calculated correctly based on RGB data.\r\n- Grayscale maps are handled correctly.\r\n- RGBA maps are assumed to be handled correctly (needs verification).\r\n\r\n## Desired Behavior / Goals\r\n- All processed color texture maps (both original RGB and merged maps) should be saved with the standard RGB channel order.\r\n- Image statistics should continue to be calculated correctly based on RGB data.\r\n- Grayscale and RGBA maps should retain their correct handling.\r\n- The fix should not reintroduce the problems solved by `ISSUE-004`.\r\n\r\n## Implementation Notes (Optional)\r\nThe issue likely stems from the universal application of `cv2.cvtColor(img_loaded, cv2.COLOR_BGR2RGB)` in `_process_maps` introduced in the `ISSUE-004` fix. While this ensures consistent RGB data for internal operations like statistics and merging (which now expects RGB), the final saving step (`cv2.imwrite`) might implicitly expect BGR data for color images, or the conversion needs to be selectively undone before saving certain map types.\r\n\r\nInvestigation needed in `asset_processor.py`:\r\n- Review `_process_maps`: Where is the BGR->RGB conversion happening? Is it applied to all 3-channel images?\r\n- Review `_process_maps` saving logic: How are different map types saved? Does `cv2.imwrite` expect RGB or BGR?\r\n- Review `_merge_maps`: How are channels combined? Does the saving logic here also need adjustment?\r\n- Determine if the BGR->RGB conversion should be conditional or if a final RGB->BGR conversion is needed before saving specific map types.\r\n\r\n## Acceptance Criteria (Optional)\r\n*   [ ] Process an asset containing standard RGB maps (e.g., Color/Albedo). Verify the output map has correct RGB channel order.\r\n*   [ ] Process an asset requiring map merging (e.g., ORM from Roughness, Metallic, AO). Verify the output merged map has correct RGB(A) channel order.\r\n*   [ ] Verify image statistics in `metadata.json` remain correct (reflecting RGB values).\r\n*   [ ] Verify grayscale maps are processed and saved correctly.\r\n*   [ ] Verify normal maps are processed and saved correctly (as per ISSUE-004 fix).\r\n*   [ ] (Optional) Verify RGBA maps are processed and saved correctly."
        }
    ]
}