{"record":{"id":"a445ef243bc7b03f","repo":"koral--/android-gif-drawable","slug":"x-must-be-width","errorCode":null,"errorMessage":"x must be < width","messagePattern":"x must be < width","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"android-gif-drawable/src/main/java/pl/droidsonroids/gif/GifDrawable.java","lineNumber":756,"sourceCode":"\t */\n\tpublic void getPixels(@NonNull int[] pixels) {\n\t\tmBuffer.getPixels(pixels, 0, mNativeInfoHandle.getWidth(), 0, 0, mNativeInfoHandle.getWidth(), mNativeInfoHandle.getHeight());\n\t}\n\n\t/**\n\t * Returns the {@link Color} at the specified location. Throws an exception\n\t * if x or y are out of bounds (negative or &gt;= to the width or height\n\t * respectively). The returned color is a non-premultiplied ARGB value.\n\t *\n\t * @param x The x coordinate (0...width-1) of the pixel to return\n\t * @param y The y coordinate (0...height-1) of the pixel to return\n\t * @return The argb {@link Color} at the specified coordinate\n\t * @throws IllegalArgumentException if x, y exceed the drawable's bounds\n\t * @throws IllegalStateException    if drawable is recycled\n\t */\n\tpublic int getPixel(@IntRange(from = 0) int x, @IntRange(from = 0) int y) {\n\t\tif (x >= mNativeInfoHandle.getWidth()) { //need to check explicitly because reused bitmap may be larger\n\t\t\tthrow new IllegalArgumentException(\"x must be < width\");\n\t\t}\n\t\tif (y >= mNativeInfoHandle.getHeight()) {\n\t\t\tthrow new IllegalArgumentException(\"y must be < height\");\n\t\t}\n\t\treturn mBuffer.getPixel(x, y);\n\t}\n\n\t@Override\n\tprotected void onBoundsChange(@NonNull Rect bounds) {\n\t\tmDstRect.set(bounds);\n\t\tif (mTransform != null) {\n\t\t\tmTransform.onBoundsChange(bounds);\n\t\t}\n\t}\n\n\t/**\n\t * Reads and renders new frame if needed then draws last rendered frame.\n\t *","sourceCodeStart":738,"sourceCodeEnd":774,"githubUrl":"https://github.com/koral--/android-gif-drawable/blob/26ff795f78b7fe8bbaf375a947b2bda95fc58e3d/android-gif-drawable/src/main/java/pl/droidsonroids/gif/GifDrawable.java#L738-L774","documentation":"getPixel returns the ARGB color of a single pixel of the current frame. Because the internal buffer bitmap may be larger than the GIF (a reused bitmap), the library explicitly checks x and y against the GIF's intrinsic width/height and throws IllegalArgumentException if they exceed bounds.","triggerScenarios":"Calling getPixel(x, y) with x >= gif width or y >= gif height, e.g. sampling coordinates from a touch event scaled to a larger view, or iterating a parent bitmap's dimensions.","commonSituations":"Touch-to-color pickers mapping view coordinates directly to bitmap coordinates without scaling/offset; iterating rows/cols using the container bitmap size instead of getWidth()/getHeight(); density scaling mismatches.","solutions":["Clamp coordinates: x = Math.min(x, gifDrawable.getWidth() - 1), same for height","Scale touch/view coordinates to drawable bitmap coordinates before sampling","Catch IllegalArgumentException around getPixel and return a transparent/sentinel color"],"exampleFix":"// before\nint color = gifDrawable.getPixel(touchX, touchY);\n// after\nint x = Math.min(touchX, gifDrawable.getIntrinsicWidth() - 1);\nint y = Math.min(touchY, gifDrawable.getIntrinsicHeight() - 1);\nint color = gifDrawable.getPixel(x, y);","handlingStrategy":"validation","validationCode":"if (x >= gifDrawable.getIntrinsicWidth() || y >= gifDrawable.getIntrinsicHeight()) { return Color.TRANSPARENT; }","typeGuard":"boolean inBounds(GifDrawable g, int x, int y) { return x >= 0 && y >= 0 && x < g.getIntrinsicWidth() && y < g.getIntrinsicHeight(); }","tryCatchPattern":"try { color = gifDrawable.getPixel(x, y); } catch (IllegalArgumentException e) { color = Color.TRANSPARENT; }","preventionTips":["Validate coordinates against getIntrinsicWidth()/getIntrinsicHeight(), not the buffer bitmap size","Scale touch coordinates from view space to drawable space before sampling","Use getPixel only with coordinates derived from the GIF's own dimensions"],"tags":["android","gif","bounds","pixel"],"backgroundTag":"index-out-of-range","analyzedSha":"26ff795f78b7fe8bbaf375a947b2bda95fc58e3d","analyzedAt":"2026-09-10T14:37:11.595Z","contentChangedAt":"2026-09-10T14:37:11.595Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}