This is an automated email from the ASF dual-hosted git repository.

tilman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pdfbox-jbig2.git


The following commit(s) were added to refs/heads/master by this push:
     new 97935ae  PDFBOX-6156: fix setPixel
97935ae is described below

commit 97935ae1a0c9d11b068671035ace4862a9bf0711
Author: Tilman Hausherr <[email protected]>
AuthorDate: Thu Jan 29 16:40:57 2026 +0100

    PDFBOX-6156: fix setPixel
---
 src/main/java/org/apache/pdfbox/jbig2/Bitmap.java    | 20 ++++++++++++++++++--
 .../java/org/apache/pdfbox/jbig2/BitmapTest.java     |  4 ++++
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/apache/pdfbox/jbig2/Bitmap.java 
b/src/main/java/org/apache/pdfbox/jbig2/Bitmap.java
index 7784285..0968b65 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/Bitmap.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/Bitmap.java
@@ -75,6 +75,16 @@ public class Bitmap
         return (byte) ((this.getByte(byteIndex) >> toShift) & 0x01);
     }
 
+    /**
+     * Sets the value of a pixel specified by the given coordinates.
+     * <p>
+     * By default, the value is {@code 0} for a white pixel and {@code 1} for 
a black pixel. The
+     * value is taken from the rightmost bit in the byte.
+     *
+     * @param x - The x coordinate of the pixel.
+     * @param y - The y coordinate of the pixel.
+     * @param pixelValue The value of a pixel.
+     */
     public void setPixel(int x, int y, byte pixelValue)
     {
         final int byteIndex = getByteIndex(x, y);
@@ -83,8 +93,14 @@ public class Bitmap
         final int shift = 7 - bitOffset;
 
         final byte src = bitmap[byteIndex];
-        final byte result = (byte) (src | (pixelValue << shift));
-        bitmap[byteIndex] = result;
+        if ((pixelValue & 1) == 1)
+        {
+            bitmap[byteIndex] = (byte) (src | (1 << shift));
+        }
+        else
+        {
+            bitmap[byteIndex] = (byte) (src & ~(1 << shift));
+        }
     }
 
     /**
diff --git a/src/test/java/org/apache/pdfbox/jbig2/BitmapTest.java 
b/src/test/java/org/apache/pdfbox/jbig2/BitmapTest.java
index 1728f65..b505ddd 100644
--- a/src/test/java/org/apache/pdfbox/jbig2/BitmapTest.java
+++ b/src/test/java/org/apache/pdfbox/jbig2/BitmapTest.java
@@ -34,6 +34,10 @@ public class BitmapTest
         bitmap.setPixel(3, 19, (byte) 1);
 
         assertEquals(1, bitmap.getPixel(3, 19));
+        
+        bitmap.setPixel(3, 19, (byte) 0);
+
+        assertEquals(0, bitmap.getPixel(3, 19));
     }
 
     @Test

Reply via email to