{"record":{"id":"f677a69ba883716a","repo":"kitao/pyxel","slug":"image-dimensions-are-too-large","errorCode":null,"errorMessage":"image dimensions are too large","messagePattern":"image dimensions are too large","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/pyxel-core/src/image.rs","lineNumber":50,"sourceCode":"pub struct Image {\n    pub(crate) canvas: Canvas<Color>,\n    pub(crate) palette: [Color; MAX_COLORS as usize],\n    pub(crate) palette_is_identity: bool,\n}\n\nimpl ToIndex for Color {\n    fn to_index(&self) -> usize {\n        *self as usize\n    }\n}\n\ndefine_rc_type!(RcImage, Image);\n\nimpl Image {\n    // Constructors\n\n    pub fn new(width: u32, height: u32) -> RcImage {\n        Self::try_new(width, height).expect(\"image dimensions are too large\")\n    }\n\n    pub fn try_new(width: u32, height: u32) -> Result<RcImage, String> {\n        let canvas = Canvas::try_new(width, height)\n            .ok_or_else(|| \"image dimensions are too large\".to_string())?;\n        Ok(new_rc_type!(Self {\n            canvas,\n            palette: array::from_fn(|i| i as Color),\n            palette_is_identity: true,\n        }))\n    }\n\n    pub fn from_image(filename: &str, include_colors: Option<bool>) -> Result<RcImage, String> {\n        let include_colors = include_colors.unwrap_or(false);\n        let file_image = image::open(Path::new(&filename))\n            .map_err(|_| format!(\"Failed to open file '{filename}'\"))?\n            .to_rgb8();\n","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/kitao/pyxel/blob/50f9bd77780c993aca62b5b221766bde5791081d/crates/pyxel-core/src/image.rs#L32-L68","documentation":"`Image::new` delegates to `Canvas::try_new` and panics when the pixel buffer cannot be created because width*height overflows or is otherwise too large. The error string 'image dimensions are too large' surfaces this guard to the image API.","triggerScenarios":"Calling Image::new(w, h) (or constructors that build an Image internally, e.g. Pyxel's screen/image resources) with dimensions whose product exceeds the canvas limit, causing try_new to return Err.","commonSituations":"Loading resource files with corrupted/oversized image headers; passing user-controlled sizes into image creation; porting assets sized for other tools that exceed Pyxel's canvas limits.","solutions":["Use Image::try_new and handle the Err(String) instead of panicking via new().","Clamp/validate width and height before construction.","Verify the source resource file is not corrupted or oversized."],"exampleFix":"// before\nlet img = Image::new(w, h);\n// after\nlet img = Image::try_new(w, h).map_err(|e| format!(\"cannot create image: {e}\"))?;","handlingStrategy":"validation","validationCode":"fn valid_image_dims(w: u32, h: u32) -> bool {\n    w.checked_mul(h).is_some()\n}","typeGuard":null,"tryCatchPattern":"// use the fallible API instead of catching the panic\nmatch Image::try_new(w, h) {\n    Ok(img) => img,\n    Err(msg) => { /* log/limit size, fallback */ Image::new(default_w, default_h) }\n}","preventionTips":["Prefer Image::try_new over Image::new for runtime-derived sizes.","Validate resource file headers before constructing images.","Keep dimension limits consistent with Pyxel's canvas maximums in config."],"tags":["rust","panic","image","canvas"],"backgroundTag":"dimensions-too-large","analyzedSha":"50f9bd77780c993aca62b5b221766bde5791081d","analyzedAt":"2026-09-03T10:27:43.285Z","contentChangedAt":"2026-09-03T10:27:43.285Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}