You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2022/01/04 09:33:08 UTC

[pdfbox-jbig2] branch master updated: PDFBOX-5220: reduce the exposure of the byte array of a Bitmap

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

lehmi 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 00bce1e  PDFBOX-5220: reduce the exposure of the byte array of a Bitmap
00bce1e is described below

commit 00bce1e7f1987b064d400f2fcc46fd2dad1184ae
Author: Andreas Lehmkühler <an...@lehmi.de>
AuthorDate: Tue Jan 4 10:32:51 2022 +0100

    PDFBOX-5220: reduce the exposure of the byte array of a Bitmap
---
 src/main/java/org/apache/pdfbox/jbig2/Bitmap.java  | 42 +++++++++++++++
 .../java/org/apache/pdfbox/jbig2/JBIG2Page.java    |  2 +-
 .../org/apache/pdfbox/jbig2/image/Bitmaps.java     |  2 +-
 .../pdfbox/jbig2/segments/HalftoneRegion.java      |  2 +-
 .../pdfbox/jbig2/segments/SymbolDictionary.java    |  2 +-
 .../apache/pdfbox/jbig2/segments/TextRegion.java   |  2 +-
 .../apache/pdfbox/jbig2/ChecksumCalculator.java    | 62 ----------------------
 .../org/apache/pdfbox/jbig2/GithubIssuesTest.java  |  9 +++-
 .../jbig2/decoder/mmr/MMRDecompressorTest.java     | 10 ++--
 .../apache/pdfbox/jbig2/image/BitmapsBlitTest.java | 12 ++---
 10 files changed, 65 insertions(+), 80 deletions(-)

diff --git a/src/main/java/org/apache/pdfbox/jbig2/Bitmap.java b/src/main/java/org/apache/pdfbox/jbig2/Bitmap.java
index 96c2250..b186e9f 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/Bitmap.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/Bitmap.java
@@ -18,6 +18,7 @@
 package org.apache.pdfbox.jbig2;
 
 import java.awt.Rectangle;
+import java.util.Arrays;
 
 /**
  * This class represents a bi-level image that is organized like a bitmap.
@@ -104,6 +105,8 @@ public class Bitmap
      * Simply returns the byte array of this bitmap.
      * 
      * @return The byte array of this bitmap.
+     * 
+     * @deprecated don't expose the underlying byte array, will be removed in a future release.
      */
     public byte[] getByteArray()
     {
@@ -202,8 +205,47 @@ public class Bitmap
         return new Rectangle(0, 0, width, height);
     }
 
+    /**
+     * Returns the length of the underlying byte array.
+     * 
+     * @return byte array length
+     * 
+     * @deprecated renamed, will be removed in a future release. Use {@link Bitmap#getLength()} instead.
+     */
     public int getMemorySize()
     {
+        return getLength();
+    }
+
+    /**
+     * Returns the length of the underlying byte array.
+     * 
+     * @return byte array length
+     */
+    public int getLength()
+    {
         return bitmap.length;
     }
+
+    /**
+     * Fill the underlying bitmap with the given byte value.
+     * 
+     * @param fillByte the value to be stored in all elements of the bitmap
+     */
+    public void fillBitmap(byte fillByte)
+    {
+        Arrays.fill(bitmap, fillByte);
+    }
+    
+    @Override
+    public boolean equals(Object obj)
+    {
+        // most likely used for tests
+        if (!(obj instanceof Bitmap))
+        {
+            return false;
+        }
+        Bitmap other = (Bitmap)obj;
+        return Arrays.equals(bitmap, other.bitmap);
+    }
 }
diff --git a/src/main/java/org/apache/pdfbox/jbig2/JBIG2Page.java b/src/main/java/org/apache/pdfbox/jbig2/JBIG2Page.java
index 41fcbec..6c9cc75 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/JBIG2Page.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/JBIG2Page.java
@@ -162,7 +162,7 @@ class JBIG2Page
         // If default pixel value is not 0, byte will be filled with 0xff
         if (pageInformation.getDefaultPixelValue() != 0)
         {
-            Arrays.fill(pageBitmap.getByteArray(), (byte) 0xff);
+            pageBitmap.fillBitmap((byte) 0xff);
         }
 
         for (SegmentHeader s : segments.values())
diff --git a/src/main/java/org/apache/pdfbox/jbig2/image/Bitmaps.java b/src/main/java/org/apache/pdfbox/jbig2/image/Bitmaps.java
index 255ea22..c32aeaa 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/image/Bitmaps.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/image/Bitmaps.java
@@ -365,7 +365,7 @@ public class Bitmaps
         for (int x = firstSourceByteOfLine; x < lastSourceByteOfLine; x++)
         {
 
-            if (sourceOffset + 1 < src.getByteArray().length)
+            if (sourceOffset + 1 < src.getLength())
             {
                 final boolean isLastByte = x + 1 == lastSourceByteOfLine;
                 byte value = (byte) (src.getByte(sourceOffset++) << sourceUpShift
diff --git a/src/main/java/org/apache/pdfbox/jbig2/segments/HalftoneRegion.java b/src/main/java/org/apache/pdfbox/jbig2/segments/HalftoneRegion.java
index ddac8d9..7dc6c0a 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/segments/HalftoneRegion.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/segments/HalftoneRegion.java
@@ -165,7 +165,7 @@ public class HalftoneRegion implements Region
 
             if (hDefaultPixel == 1)
             {
-                Arrays.fill(halftoneRegionBitmap.getByteArray(), (byte) 0xff);
+                halftoneRegionBitmap.fillBitmap((byte) 0xff);
             }
 
             /* 2) */
diff --git a/src/main/java/org/apache/pdfbox/jbig2/segments/SymbolDictionary.java b/src/main/java/org/apache/pdfbox/jbig2/segments/SymbolDictionary.java
index bf6c343..5837b7c 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/segments/SymbolDictionary.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/segments/SymbolDictionary.java
@@ -807,7 +807,7 @@ public class SymbolDictionary implements Dictionary
         {
             final Bitmap heightClassCollectiveBitmap = new Bitmap(totalWidth, heightClassHeight);
 
-            for (int i = 0; i < heightClassCollectiveBitmap.getByteArray().length; i++)
+            for (int i = 0; i < heightClassCollectiveBitmap.getLength(); i++)
             {
                 heightClassCollectiveBitmap.setByte(i, subInputStream.readByte());
             }
diff --git a/src/main/java/org/apache/pdfbox/jbig2/segments/TextRegion.java b/src/main/java/org/apache/pdfbox/jbig2/segments/TextRegion.java
index 4aaaf49..8e5e38b 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/segments/TextRegion.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/segments/TextRegion.java
@@ -387,7 +387,7 @@ public class TextRegion implements Region
         /* 1) */
         if (defaultPixel != 0)
         {
-            Arrays.fill(regionBitmap.getByteArray(), (byte) 0xff);
+            regionBitmap.fillBitmap((byte) 0xff);
         }
     }
 
diff --git a/src/test/java/org/apache/pdfbox/jbig2/ChecksumCalculator.java b/src/test/java/org/apache/pdfbox/jbig2/ChecksumCalculator.java
deleted file mode 100644
index 8cacc8f..0000000
--- a/src/test/java/org/apache/pdfbox/jbig2/ChecksumCalculator.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.pdfbox.jbig2;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
-import java.util.Arrays;
-
-import javax.imageio.stream.ImageInputStream;
-
-import org.apache.pdfbox.jbig2.err.JBIG2Exception;
-import org.apache.pdfbox.jbig2.io.DefaultInputStreamFactory;
-import org.junit.Ignore;
-import org.junit.Test;
-
-@Ignore
-public class ChecksumCalculator
-{
-
-    @Ignore
-    @Test
-    public void computeChecksum() throws NoSuchAlgorithmException, IOException, JBIG2Exception
-    {
-        String filepath = "/images/sampledata_page3.jb2";
-        int pageNumber = 1;
-
-        InputStream is = getClass().getResourceAsStream(filepath);
-        DefaultInputStreamFactory disf = new DefaultInputStreamFactory();
-        ImageInputStream iis = disf.getInputStream(is);
-        JBIG2Document doc = new JBIG2Document(iis);
-        Bitmap bitmap = doc.getPage(pageNumber).getBitmap();
-
-        byte[] md5 = md5(bitmap);
-        for (byte b : md5)
-        {
-            System.out.print(b);
-        }
-        System.out.println(Arrays.toString(md5));
-    }
-
-    public static byte[] md5(Bitmap b) throws NoSuchAlgorithmException
-    {
-        return MessageDigest.getInstance("MD5").digest(b.getByteArray());
-    }
-}
diff --git a/src/test/java/org/apache/pdfbox/jbig2/GithubIssuesTest.java b/src/test/java/org/apache/pdfbox/jbig2/GithubIssuesTest.java
index e4ceb00..c695b86 100644
--- a/src/test/java/org/apache/pdfbox/jbig2/GithubIssuesTest.java
+++ b/src/test/java/org/apache/pdfbox/jbig2/GithubIssuesTest.java
@@ -17,10 +17,11 @@
 
 package org.apache.pdfbox.jbig2;
 
-import static org.apache.pdfbox.jbig2.ChecksumCalculator.md5;
 import static org.apache.pdfbox.jbig2.JBIG2DocumentFacade.doc;
 
 import java.io.InputStream;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 
 import javax.imageio.ImageIO;
 import javax.imageio.stream.ImageInputStream;
@@ -69,4 +70,10 @@ public class GithubIssuesTest
         }
 
     }
+    
+    private static byte[] md5(Bitmap b) throws NoSuchAlgorithmException
+    {
+        return MessageDigest.getInstance("MD5").digest(b.getByteArray());
+    }
+
 }
diff --git a/src/test/java/org/apache/pdfbox/jbig2/decoder/mmr/MMRDecompressorTest.java b/src/test/java/org/apache/pdfbox/jbig2/decoder/mmr/MMRDecompressorTest.java
index e64ee22..2c4fd3c 100644
--- a/src/test/java/org/apache/pdfbox/jbig2/decoder/mmr/MMRDecompressorTest.java
+++ b/src/test/java/org/apache/pdfbox/jbig2/decoder/mmr/MMRDecompressorTest.java
@@ -17,7 +17,7 @@
 
 package org.apache.pdfbox.jbig2.decoder.mmr;
 
-import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assume.assumeTrue;
 
 import java.io.File;
@@ -42,6 +42,11 @@ public class MMRDecompressorTest
         final byte[] expected = new byte[] { 0, 0, 2, 34, 38, 102, -17, -1, 2, 102, 102, //
                 -18, -18, -17, -1, -1, 0, 2, 102, 102, 127, //
                 -1, -1, -1, 0, 0, 0, 4, 68, 102, 102, 127 };
+        final Bitmap expectedBitmap = new Bitmap(16 * 4, 4);
+        for(int i = 0; i < expected.length; i++ )
+        {
+            expectedBitmap.setByte(i, expected[i]);
+        }
 
         final File inputFile = new File("target/images/sampledata.jb2");
         // skip test if input stream isn't available
@@ -58,8 +63,7 @@ public class MMRDecompressorTest
         final MMRDecompressor mmrd = new MMRDecompressor(16 * 4, 4, sis);
 
         final Bitmap b = mmrd.uncompress();
-        final byte[] actual = b.getByteArray();
 
-        assertArrayEquals(expected, actual);
+        assertTrue(expectedBitmap.equals(b));
     }
 }
diff --git a/src/test/java/org/apache/pdfbox/jbig2/image/BitmapsBlitTest.java b/src/test/java/org/apache/pdfbox/jbig2/image/BitmapsBlitTest.java
index 67958bc..ba1126d 100644
--- a/src/test/java/org/apache/pdfbox/jbig2/image/BitmapsBlitTest.java
+++ b/src/test/java/org/apache/pdfbox/jbig2/image/BitmapsBlitTest.java
@@ -17,7 +17,7 @@
 
 package org.apache.pdfbox.jbig2.image;
 
-import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assume.assumeTrue;
 
 import java.awt.Rectangle;
@@ -57,10 +57,7 @@ public class BitmapsBlitTest
         final Bitmap dst = new Bitmap(src.getWidth(), src.getHeight());
         Bitmaps.blit(src, dst, 0, 0, CombinationOperator.REPLACE);
 
-        final byte[] srcData = src.getByteArray();
-        final byte[] dstData = dst.getByteArray();
-
-        assertArrayEquals(srcData, dstData);
+        assertTrue(src.equals(dst));
     }
 
     @Test
@@ -86,10 +83,7 @@ public class BitmapsBlitTest
 
         final Bitmap dstRegionBitmap = Bitmaps.extract(roi, dst);
 
-        final byte[] srcData = src.getByteArray();
-        final byte[] dstRegionData = dstRegionBitmap.getByteArray();
-
-        assertArrayEquals(srcData, dstRegionData);
+        assertTrue(src.equals(dstRegionBitmap));
     }
 
 }