Bitmap: Allow pixel-based access (#1855)

This commit is contained in:
Constantin Piber
2026-01-11 22:48:07 +01:00
committed by GitHub
parent b979db9acb
commit c4d8bba5ca
3 changed files with 70 additions and 3 deletions

View File

@@ -315,6 +315,23 @@ public final class Bitmap {
}
}
/**
* Shared code to check for illegal arguments passed to getPixel()
* or setPixel()
*
* @param x x coordinate of the pixel
* @param y y coordinate of the pixel
*/
private void checkPixelAccess(int x, int y) {
checkXYSign(x, y);
if (x >= getWidth()) {
throw new IllegalArgumentException("x must be < bitmap.width()");
}
if (y >= getHeight()) {
throw new IllegalArgumentException("y must be < bitmap.height()");
}
}
public void getPixels(@ColorInt int[] pixels, int offset, int stride,
int x, int y, int width, int height) {
checkPixelsAccess(x, y, width, height, offset, stride, pixels);
@@ -322,6 +339,12 @@ public final class Bitmap {
image.getRGB(x, y, width, height, pixels, offset, stride);
}
@ColorInt
public int getPixel(int x, int y) {
checkPixelAccess(x, y);
return image.getRGB(x, y);
}
public void eraseColor(int c) {
java.awt.Color color = Color.valueOf(c).toJavaColor();
Graphics2D graphics = image.createGraphics();