{"record":{"id":"652b9ee4c5f87b06","repo":"tonhowtf/omniget","slug":"n-o-gravei-o-png-image-stitch","errorCode":null,"errorMessage":"não gravei o PNG: {}","messagePattern":"não gravei o PNG: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src-tauri/omniget-core/src/core/tools/image_stitch.rs","lineNumber":347,"sourceCode":"}\n\n/// Encolhe proporcionalmente quando passa da largura pedida.\nfn cap_width(img: RgbaImage, max_width: u32) -> RgbaImage {\n    if max_width == 0 || img.width() <= max_width {\n        return img;\n    }\n    let h = ((img.height() as f64 * max_width as f64 / img.width() as f64).round() as u32).max(1);\n    imageops::resize(&img, max_width, h, imageops::FilterType::Lanczos3)\n}\n\nfn encode(img: &RgbaImage, format: &str, quality: u8) -> anyhow::Result<Vec<u8>> {\n    if format.eq_ignore_ascii_case(\"jpeg\") || format.eq_ignore_ascii_case(\"jpg\") {\n        super::image_compress::encode_jpeg(&DynamicImage::ImageRgba8(img.clone()), quality)\n    } else {\n        let mut buf = Cursor::new(Vec::new());\n        DynamicImage::ImageRgba8(img.clone())\n            .write_to(&mut buf, image::ImageFormat::Png)\n            .map_err(|e| anyhow!(\"não gravei o PNG: {}\", e))?;\n        Ok(buf.into_inner())\n    }\n}\n\npub fn run(opts: &StitchOptions, progress: &ProgressFn) -> anyhow::Result<StitchResult> {\n    if opts.inputs.is_empty() {\n        return Err(anyhow!(\"escolha pelo menos um print\"));\n    }\n    let total = opts.inputs.len() as u64;\n    let mut frames: Vec<RgbaImage> = Vec::with_capacity(opts.inputs.len());\n    for (i, path) in opts.inputs.iter().enumerate() {\n        super::report(\n            progress,\n            \"img-stitch\",\n            \"progress\",\n            i as u64,\n            Some(total),\n            Some(path.clone()),","sourceCodeStart":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/tonhowtf/omniget/blob/8600b91f4246848bac346874daa9e61c1fc5677a/src-tauri/omniget-core/src/core/tools/image_stitch.rs#L329-L365","documentation":"This error is raised in `encode` in image_stitch.rs when the stitched RGBA image cannot be serialized to PNG bytes via `DynamicImage::write_to`. The `image` crate returns an error on buffer/format failures, and the code wraps it with anyhow to add context ('não gravei o PNG'). It indicates PNG encoding of the final stitched image failed, not that stitching failed.","triggerScenarios":"Calling `encode` with a format that is not jpeg/jpg, and the underlying `write_to(&mut buf, image::ImageFormat::Png)` call fails — e.g. image dimensions exceed the PNG or encoder limits, or an internal image-crate encoding error occurs.","commonSituations":"Stitching extremely tall screenshots whose pixel count exceeds PNG encoder limits; running in an environment where the image crate was compiled without PNG support; a corrupted in-memory frame producing an unencodable image.","solutions":["Check the wrapped image::ImageError in the message for the root cause (e.g. 'Image too large' limits) and reduce input size or max_width/max dimensions.","Ensure the `image` crate build includes the `png` feature and that the dimension limits are acceptable for your stitched output.","If the failure is dimension-related, cap the canvas via cap_width or split the stitch into segments before encoding."],"exampleFix":"// before\nDynamicImage::ImageRgba8(img.clone())\n    .write_to(&mut buf, image::ImageFormat::Png)\n    .map_err(|e| anyhow!(\"não gravei o PNG: {}\", e))?;\n// after\nlet capped = cap_width(DynamicImage::ImageRgba8(img.clone()), max_width); // guard huge dimensions\nlet mut buf = Cursor::new(Vec::new());\ncapped.write_to(&mut buf, image::ImageFormat::Png)\n    .map_err(|e| anyhow!(\"não gravei o PNG: {}\", e))?;","handlingStrategy":"try-catch","validationCode":"// check image pixel count before encoding\nlet pixels = (img.width() as u64) * (img.height() as u64);\nif pixels > 100_000_000 { return Err(\"imagem grande demais para codificar\"); }","typeGuard":"fn is_encodable_size(img: &DynamicImage) -> bool {\n    let (w, h) = (img.width() as u64, img.height() as u64);\n    w > 0 && h > 0 && w * h <= 100_000_000\n}","tryCatchPattern":"match encode(&opts, progress) {\n    Ok(bytes) => write_out(&bytes),\n    Err(e) if e.to_string().contains(\"não gravei o PNG\") => {\n        eprintln!(\"falha ao codificar PNG: {e:#}\");\n        // retry with reduced max_width\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Cap stitched canvas dimensions with cap_width before encoding","Keep the `png` feature enabled in the image crate build","Test with very tall screenshot sets to catch dimension-limit regressions"],"tags":["image-encoding","png","rust","anyhow"],"backgroundTag":"file-write-failed","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"}