# Plan for ISSUE-012: MASK map processing fails to extract alpha channel from RGBA images ## Issue Description When 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). ## Analysis Based 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. ## Detailed Plan 1. **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). 2. **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':`. 3. **Reorder and Refine Logic:** * The image will still be loaded with `cv2.IMREAD_UNCHANGED` for MASK types to ensure the alpha channel is initially present. * 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). * If both conditions are true, extract the alpha channel (`img_loaded[:, :, 3]`) and use this single-channel data for subsequent processing steps (`img_prepared`). * 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`. * If the base map type is 'MASK' and the loaded image is already 1-channel (grayscale), keep it as is. * If the base map type is *not* 'MASK', the existing BGR->RGB conversion logic for 3/4 channel images will be applied as before. 4. **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. 5. **Testing:** Use the acceptance criteria outlined in `ISSUE-012` to ensure the fix works correctly. ## Proposed Code Logic Flow ```mermaid graph TD A[Load Image (IMREAD_UNCHANGED for MASK)] --> B{Loading Successful?}; B -- No --> C[Handle Load Error]; B -- Yes --> D[Get Original Dtype & Shape]; D --> E{Base Map Type is MASK?}; E -- Yes --> F{Loaded Image is 4-Channel?}; F -- Yes --> G[Extract Alpha Channel]; F -- No --> H{Loaded Image is 3-Channel?}; H -- Yes --> I[Convert BGR/RGB to Grayscale]; H -- No --> J[Keep as is (Assume Grayscale)]; G --> K[Set img_prepared to Mask Data]; I --> K; J --> K; E -- No --> L{Loaded Image is 3 or 4-Channel?}; L -- Yes --> M[Convert BGR/BGRA to RGB]; L -- No --> N[Keep as is]; M --> O[Set img_prepared to RGB Data]; N --> O; K --> P[Proceed with other transformations (Gloss, Resize, etc.)]; O --> P; P --> Q[Cache Result & Return]; C --> Q; ``` ## Acceptance Criteria (from ISSUE-012) * [ ] Process an asset with an RGBA image designated as a MASK map according to the preset. * [ ] Verify that the output MASK map is a single-channel grayscale image. * [ ] Confirm that the grayscale values in the output MASK map correspond to the alpha values of the original RGBA input image.