{"record":{"id":"241406c15fe1e2d2","repo":"tonhowtf/omniget","slug":"m-scara-com-tamanho-inconsistente","errorCode":null,"errorMessage":"máscara com tamanho inconsistente","messagePattern":"máscara com tamanho inconsistente","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src-tauri/omniget-core/src/core/tools/img_bg.rs","lineNumber":217,"sourceCode":"    let slice = &raw[..n];\n    let mut mi = f32::INFINITY;\n    let mut ma = f32::NEG_INFINITY;\n    for v in slice {\n        if v.is_finite() {\n            mi = mi.min(*v);\n            ma = ma.max(*v);\n        }\n    }\n    let span = ma - mi;\n    let buf: Vec<u8> = if !span.is_finite() || span <= f32::EPSILON {\n        vec![0u8; n]\n    } else {\n        slice\n            .iter()\n            .map(|v| (((v - mi) / span) * 255.0).round().clamp(0.0, 255.0) as u8)\n            .collect()\n    };\n    GrayImage::from_raw(w, h, buf).ok_or_else(|| anyhow!(\"máscara com tamanho inconsistente\"))\n}\n\n/// Limpeza opcional da máscara: corte de alfa fraco e/ou borda dura.\npub fn clean_mask(mask: &GrayImage, alpha_threshold: u8, hard_edges: bool) -> GrayImage {\n    let mut out = mask.clone();\n    for px in out.pixels_mut() {\n        let mut v = px.0[0];\n        if alpha_threshold > 0 && v < alpha_threshold {\n            v = 0;\n        }\n        if hard_edges {\n            v = if v >= 128 { 255 } else { 0 };\n        }\n        px.0[0] = v;\n    }\n    out\n}\n","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/tonhowtf/omniget/blob/8600b91f4246848bac346874daa9e61c1fc5677a/src-tauri/omniget-core/src/core/tools/img_bg.rs#L199-L235","documentation":"`mask_from_raw` finishes by constructing the GrayImage from the raw byte buffer via `GrayImage::from_raw(w, h, buf)`; if the buffer length doesn't match w*h (or allocation check fails), it returns 'máscara com tamanho inconsistente'. This is a defensive internal invariant check after normalization.","triggerScenarios":"Calling `mask_from_raw` when the computed per-pixel buffer length deviates from w*h — practically only if slicing/normalization logic changed or w/h are zero/inconsistent with buf construction.","commonSituations":"A modified code path pushes fewer/more bytes into buf; zero-sized dimensions (w=0 or h=0) yielding an empty buffer mismatch; concurrent mutation of the buffer.","solutions":["Verify w and h are nonzero and match the raw.len() used to compute the buffer (raw.len() >= w*h was already checked upstream).","If you modified mask_from_raw, ensure buf has exactly w*h bytes before from_raw.","Report as a bug if hit with the stock implementation — it indicates an internal invariant violation."],"exampleFix":"// before\nlet buf: Vec<u8> = ...; // possibly wrong length after custom edits\nGrayImage::from_raw(w, h, buf).ok_or_else(|| anyhow!(\"máscara com tamanho inconsistente\"))\n// after\ndebug_assert_eq!(buf.len(), (w as usize) * (h as usize), \"mask buffer size\");\nGrayImage::from_raw(w, h, buf).ok_or_else(|| anyhow!(\"máscara com tamanho inconsistente\"))","handlingStrategy":"type-guard","validationCode":"let buf_len = buf.len();\nassert_eq!(buf_len, (w as usize) * (h as usize));","typeGuard":"fn dims_consistent(w: u32, h: u32, buf_len: usize) -> bool {\n    w > 0 && h > 0 && buf_len == (w as usize) * (h as usize)\n}","tryCatchPattern":"match mask_from_raw(&raw, w, h) {\n    Ok(mask) => use(mask),\n    Err(e) if e.to_string().contains(\"tamanho inconsistente\") => {\n        eprintln!(\"bug interno: buffer da máscara não casa com w×h\");\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Don't modify mask_from_raw's buffer construction without updating length checks","Reject zero width/height inputs before calling","Add debug_assert on buffer length in tests"],"tags":["internal-invariant","image","mask","rust"],"backgroundTag":"internal-invariant-violation","analyzedSha":"8600b91f4246848bac346874daa9e61c1fc5677a","analyzedAt":"2026-09-12T14:29:19.317Z","contentChangedAt":"2026-09-12T14:29:19.317Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}