{
    "sourceFile": "Tickets/ISSUE-012-plan.md",
    "activeCommit": 0,
    "commits": [
        {
            "activePatchIndex": 0,
            "patches": [
                {
                    "date": 1745348522990,
                    "content": "Index: \n===================================================================\n--- \n+++ \n"
                }
            ],
            "date": 1745348522990,
            "name": "Commit-0",
            "content": "# Plan for ISSUE-012: MASK map processing fails to extract alpha channel from RGBA images\r\n\r\n## Issue Description\r\nWhen processing texture sets that include MASK maps provided as RGBA images, the asset processor is expected to extract the alpha channel to represent the mask. However, the current implementation appears to be converting the RGBA image to grayscale instead of isolating the alpha channel, resulting in incorrect MASK maps. This issue affects all RGBA images classified as MASK, including plain 'MASK' and MASK variants (e.g., MASK-1).\r\n\r\n## Analysis\r\nBased on the code in `asset_processor.py`, specifically the `_load_and_transform_source` method, the issue likely stems from the order of operations. The current logic converts 4-channel BGRA images to 3-channel RGB *before* checking specifically for MASK types and attempting to extract the alpha channel. This means the alpha channel is lost before the code gets a chance to extract it. The condition `if map_type.upper() == 'MASK':` is too strict and does not cover MASK variants.\r\n\r\n## Detailed Plan\r\n\r\n1.  **Analyze `_load_and_transform_source`:** Re-examine the `_load_and_transform_source` method in `asset_processor.py` to confirm the exact sequence of image loading, color space conversions, and MASK-specific handling. (Completed during initial analysis).\r\n2.  **Modify MASK Handling Condition:** Change the current condition `if map_type.upper() == 'MASK':` to use the `_get_base_map_type` helper function, so it correctly identifies all MASK variants (e.g., 'MASK', 'MASK-1', 'MASK-2') and applies the special handling logic to them. The condition will become `if _get_base_map_type(map_type) == 'MASK':`.\r\n3.  **Reorder and Refine Logic:**\r\n    *   The image will still be loaded with `cv2.IMREAD_UNCHANGED` for MASK types to ensure the alpha channel is initially present.\r\n    *   Immediately after loading, and *before* any general color space conversions (like BGR->RGB), check if the base map type is 'MASK' and if the loaded image is 4-channel (RGBA/BGRA).\r\n    *   If both conditions are true, extract the alpha channel (`img_loaded[:, :, 3]`) and use this single-channel data for subsequent processing steps (`img_prepared`).\r\n    *   If the base map type is 'MASK' but the loaded image is 3-channel (RGB/BGR), convert it to grayscale (`cv2.cvtColor(img_loaded, cv2.COLOR_BGR2GRAY)`) and use this for `img_prepared`.\r\n    *   If the base map type is 'MASK' and the loaded image is already 1-channel (grayscale), keep it as is.\r\n    *   If the base map type is *not* 'MASK', the existing BGR->RGB conversion logic for 3/4 channel images will be applied as before.\r\n4.  **Review Subsequent Steps:** Verify that the rest of the `_load_and_transform_source` method (Gloss->Rough inversion, resizing, dtype conversion) correctly handles the single-channel image data that will now be produced for RGBA MASK inputs.\r\n5.  **Testing:** Use the acceptance criteria outlined in `ISSUE-012` to ensure the fix works correctly.\r\n\r\n## Proposed Code Logic Flow\r\n\r\n```mermaid\r\ngraph TD\r\n    A[Load Image (IMREAD_UNCHANGED for MASK)] --> B{Loading Successful?};\r\n    B -- No --> C[Handle Load Error];\r\n    B -- Yes --> D[Get Original Dtype & Shape];\r\n    D --> E{Base Map Type is MASK?};\r\n    E -- Yes --> F{Loaded Image is 4-Channel?};\r\n    F -- Yes --> G[Extract Alpha Channel];\r\n    F -- No --> H{Loaded Image is 3-Channel?};\r\n    H -- Yes --> I[Convert BGR/RGB to Grayscale];\r\n    H -- No --> J[Keep as is (Assume Grayscale)];\r\n    G --> K[Set img_prepared to Mask Data];\r\n    I --> K;\r\n    J --> K;\r\n    E -- No --> L{Loaded Image is 3 or 4-Channel?};\r\n    L -- Yes --> M[Convert BGR/BGRA to RGB];\r\n    L -- No --> N[Keep as is];\r\n    M --> O[Set img_prepared to RGB Data];\r\n    N --> O;\r\n    K --> P[Proceed with other transformations (Gloss, Resize, etc.)];\r\n    O --> P;\r\n    P --> Q[Cache Result & Return];\r\n    C --> Q;\r\n```\r\n\r\n## Acceptance Criteria (from ISSUE-012)\r\n*   [ ] Process an asset with an RGBA image designated as a MASK map according to the preset.\r\n*   [ ] Verify that the output MASK map is a single-channel grayscale image.\r\n*   [ ] Confirm that the grayscale values in the output MASK map correspond to the alpha values of the original RGBA input image."
        }
    ]
}