tonhowtf/omniget · error

máscara com tamanho inconsistente

Error message

máscara com tamanho inconsistente

What it means

`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.

Solutions

  1. Verify w and h are nonzero and match the raw.len() used to compute the buffer (raw.len() >= w*h was already checked upstream).
  2. If you modified mask_from_raw, ensure buf has exactly w*h bytes before from_raw.
  3. Report as a bug if hit with the stock implementation — it indicates an internal invariant violation.

Example fix

// before
let buf: Vec<u8> = ...; // possibly wrong length after custom edits
GrayImage::from_raw(w, h, buf).ok_or_else(|| anyhow!("máscara com tamanho inconsistente"))
// after
debug_assert_eq!(buf.len(), (w as usize) * (h as usize), "mask buffer size");
GrayImage::from_raw(w, h, buf).ok_or_else(|| anyhow!("máscara com tamanho inconsistente"))
Defensive patterns

Strategy: type-guard

Validate before calling

let buf_len = buf.len();
assert_eq!(buf_len, (w as usize) * (h as usize));

Type guard

fn dims_consistent(w: u32, h: u32, buf_len: usize) -> bool {
    w > 0 && h > 0 && buf_len == (w as usize) * (h as usize)
}

Try / catch

match mask_from_raw(&raw, w, h) {
    Ok(mask) => use(mask),
    Err(e) if e.to_string().contains("tamanho inconsistente") => {
        eprintln!("bug interno: buffer da máscara não casa com w×h");
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of tonhowtf/omniget@8600b91f42 (2026-09-12). Data as JSON: /api/errors/241406c15fe1e2d2. Report an issue: GitHub.

Appendix: source

Thrown at src-tauri/omniget-core/src/core/tools/img_bg.rs:217

    let slice = &raw[..n];
    let mut mi = f32::INFINITY;
    let mut ma = f32::NEG_INFINITY;
    for v in slice {
        if v.is_finite() {
            mi = mi.min(*v);
            ma = ma.max(*v);
        }
    }
    let span = ma - mi;
    let buf: Vec<u8> = if !span.is_finite() || span <= f32::EPSILON {
        vec![0u8; n]
    } else {
        slice
            .iter()
            .map(|v| (((v - mi) / span) * 255.0).round().clamp(0.0, 255.0) as u8)
            .collect()
    };
    GrayImage::from_raw(w, h, buf).ok_or_else(|| anyhow!("máscara com tamanho inconsistente"))
}

/// Limpeza opcional da máscara: corte de alfa fraco e/ou borda dura.
pub fn clean_mask(mask: &GrayImage, alpha_threshold: u8, hard_edges: bool) -> GrayImage {
    let mut out = mask.clone();
    for px in out.pixels_mut() {
        let mut v = px.0[0];
        if alpha_threshold > 0 && v < alpha_threshold {
            v = 0;
        }
        if hard_edges {
            v = if v >= 128 { 255 } else { 0 };
        }
        px.0[0] = v;
    }
    out
}

View on GitHub (pinned to 8600b91f42)