{"record":{"id":"402dd71cfd60b425","repo":"SubtitleEdit/subtitleedit","slug":"image-dimensions-width-height-not-set-or-invali","errorCode":null,"errorMessage":"Image dimensions [width, height] not set or invalid","messagePattern":"Image dimensions \\[width, height\\] not set or invalid","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/ui/Logic/Ocr/GoogleLens/BoundingBox.cs","lineNumber":24,"sourceCode":"{\n    private readonly int[] _imageDimensions;\n\n    public double CenterPerX { get; }\n    public double CenterPerY { get; }\n    public double PerWidth { get; }\n    public double PerHeight { get; }\n    public PixelCoordinates PixelCoords { get; }\n\n    public BoundingBox(double[] box, int[] imageDimensions)\n    {\n        if (box == null || box.Length != 4)\n        {\n            throw new ArgumentException(\"Bounding box array [centerPerX, centerPerY, perWidth, perHeight] not set or invalid\");\n        }\n        \n        if (imageDimensions == null || imageDimensions.Length != 2)\n        {\n            throw new ArgumentException(\"Image dimensions [width, height] not set or invalid\");\n        }\n\n        _imageDimensions = imageDimensions;\n        CenterPerX = box[0];\n        CenterPerY = box[1];\n        PerWidth = box[2];\n        PerHeight = box[3];\n        PixelCoords = ToPixelCoords();\n    }\n\n    private PixelCoordinates ToPixelCoords()\n    {\n        var imgWidth = _imageDimensions[0];\n        var imgHeight = _imageDimensions[1];\n\n        var width = PerWidth * imgWidth;\n        var height = PerHeight * imgHeight;\n","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Logic/Ocr/GoogleLens/BoundingBox.cs#L6-L42","documentation":"Thrown by the BoundingBox constructor when imageDimensions is null or does not have exactly 2 elements (width, height). These pixel dimensions are required by ToPixelCoords to convert normalized Lens coordinates into absolute pixels; without a valid pair the conversion cannot run, so the constructor refuses to build a half-initialized object.","triggerScenarios":"Calling new BoundingBox(box, imageDimensions) with imageDimensions null or of a length other than 2 — e.g. passing the original bitmap's single dimension, a 3-element array, or a null from an uninitialized caller.","commonSituations":"The caller forgot to populate image dimensions; the upstream ScanByData/ScanByBitmap did not thread the original dimensions through; a refactor changed the dimension carrier type.","solutions":["Always pass a 2-element int[] [width, height] derived from the source bitmap or image header.","Null-check imageDimensions at the call site before constructing.","Validate Length == 2 in the Lens parsing layer before creating boxes.","Centralize dimension extraction (Helper.ImageDimensionsFromData) and reuse the result."],"exampleFix":"// before\nvar box = new BoundingBox(region, dimsArray);\n\n// after\nif (dimsArray == null || dimsArray.Length != 2) throw new ArgumentException(\"dims\");\nvar box = new BoundingBox(region, dimsArray);","handlingStrategy":"validation","validationCode":"if (imageDimensions == null || imageDimensions.Length != 2)\n    throw new ArgumentException(\"imageDimensions must be [width, height]\");\nvar bb = new BoundingBox(box, imageDimensions);","typeGuard":"static bool IsValidDims(int[] dims) => dims != null && dims.Length == 2 && dims[0] > 0 && dims[1] > 0;","tryCatchPattern":"try { var bb = new BoundingBox(box, dims); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Image dimensions\")) { /* skip or recompute dims */ }","preventionTips":["Always derive imageDimensions from the source bitmap/header and thread it through.","Validate Length == 2 at the call site.","Avoid passing partial dimension arrays.","Use Helper.ImageDimensionsFromData to populate dims consistently."],"tags":["google-lens","ocr","bounding-box","validation","argument"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}