From 1a5cfd8f5867abfcab836ccc8836e56a778fadd8 Mon Sep 17 00:00:00 2001 From: Constantin Piber <59023762+cpiber@users.noreply.github.com> Date: Tue, 5 May 2026 16:03:56 +0200 Subject: [PATCH] Implement `setPixel` & `setPixels` (#1971) * Implement `setPixel` & `setPixels` Closes #1970 * Update changelog --- .../main/java/android/graphics/Bitmap.java | 51 +++++++++++++++++++ CHANGELOG.md | 2 +- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/AndroidCompat/src/main/java/android/graphics/Bitmap.java b/AndroidCompat/src/main/java/android/graphics/Bitmap.java index f4b7872f..29532267 100644 --- a/AndroidCompat/src/main/java/android/graphics/Bitmap.java +++ b/AndroidCompat/src/main/java/android/graphics/Bitmap.java @@ -345,6 +345,57 @@ public final class Bitmap { return image.getRGB(x, y); } + /** + *
Write the specified {@link Color} into the bitmap (assuming it is + * mutable) at the x,y coordinate. The color must be a + * non-premultiplied ARGB value in the {@link ColorSpace.Named#SRGB sRGB} + * color space.
+ * + * @param x The x coordinate of the pixel to replace (0...width-1) + * @param y The y coordinate of the pixel to replace (0...height-1) + * @param color The ARGB color to write into the bitmap + * + * @throws IllegalStateException if the bitmap is not mutable + * @throws IllegalArgumentException if x, y are outside of the bitmap's + * bounds. + */ + public void setPixel(int x, int y, @ColorInt int color) { + checkPixelAccess(x, y); + image.setRGB(x, y, color); + } + + /** + *Replace pixels in the bitmap with the colors in the array. Each element + * in the array is a packed int representing a non-premultiplied ARGB + * {@link Color} in the {@link ColorSpace.Named#SRGB sRGB} color space.
+ * + * @param pixels The colors to write to the bitmap + * @param offset The index of the first color to read from pixels[] + * @param stride The number of colors in pixels[] to skip between rows. + * Normally this value will be the same as the width of + * the bitmap, but it can be larger (or negative). + * @param x The x coordinate of the first pixel to write to in + * the bitmap. + * @param y The y coordinate of the first pixel to write to in + * the bitmap. + * @param width The number of colors to copy from pixels[] per row + * @param height The number of rows to write to the bitmap + * + * @throws IllegalStateException if the bitmap is not mutable + * @throws IllegalArgumentException if x, y, width, height are outside of + * the bitmap's bounds. + * @throws ArrayIndexOutOfBoundsException if the pixels array is too small + * to receive the specified number of pixels. + */ + public void setPixels(@NonNull @ColorInt int[] pixels, int offset, int stride, + int x, int y, int width, int height) { + if (width == 0 || height == 0) { + return; // nothing to do + } + checkPixelsAccess(x, y, width, height, offset, stride, pixels); + image.setRGB(x, y, width, height, pixels, offset, stride); + } + public void eraseColor(int c) { java.awt.Color color = Color.valueOf(c).toJavaColor(); Graphics2D graphics = image.createGraphics(); diff --git a/CHANGELOG.md b/CHANGELOG.md index c5205ae1..42e41a35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). - (**Extension/Android**) Add Main dispatcher implementation - (**Extension/Android**) Add LruCache implementation - (**Extension/Android**) Support basic BitmapFactory options -- (**Extension/Android**) Support Bitmap pixel-based access +- (**Extension/Android**) Support Bitmap pixel-based access and modification - (**Extension/Android**) Add Rect.set functionality - (**OPDS**) Add reading progress synchronization for KOReader - (**WebView**) Support copy & paste