You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2023/04/28 15:35:23 UTC
svn commit: r1909475 - in /pdfbox/branches/2.0/pdfbox/src: main/java/org/apache/pdfbox/filter/RunLengthDecodeFilter.java test/java/org/apache/pdfbox/filter/TestFilters.java
Author: tilman
Date: Fri Apr 28 15:35:23 2023
New Revision: 1909475
URL: http://svn.apache.org/viewvc?rev=1909475&view=rev
Log:
PDFBOX-4932: add encoder to test the decoder
Modified:
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/filter/RunLengthDecodeFilter.java
pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/filter/TestFilters.java
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/filter/RunLengthDecodeFilter.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/filter/RunLengthDecodeFilter.java?rev=1909475&r1=1909474&r2=1909475&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/filter/RunLengthDecodeFilter.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/filter/RunLengthDecodeFilter.java Fri Apr 28 15:35:23 2023
@@ -28,6 +28,7 @@ import org.apache.pdfbox.cos.COSDictiona
* reproducing the original text or binary data
*
* @author Ben Litchfield
+ * @author Tilman Hausherr
*/
final class RunLengthDecodeFilter extends Filter
{
@@ -79,6 +80,113 @@ final class RunLengthDecodeFilter extend
protected void encode(InputStream input, OutputStream encoded, COSDictionary parameters)
throws IOException
{
- LOG.warn("RunLengthDecodeFilter.encode is not implemented yet, skipping this stream.");
+ // Not used in PDFBox except for testing the decoder.
+ int lastVal = -1;
+ int byt;
+ int count = 0;
+ boolean equality = false;
+
+ // buffer for "unequal" runs, size between 2 and 128
+ byte[] buf = new byte[128];
+
+ while ((byt = input.read()) != -1)
+ {
+ if (lastVal == -1)
+ {
+ // first time
+ lastVal = byt;
+ count = 1;
+ }
+ else
+ {
+ if (count == 128)
+ {
+ if (equality)
+ {
+ // max length of equals
+ encoded.write(129); // = 257 - 128
+ encoded.write(lastVal);
+ }
+ else
+ {
+ // max length of unequals
+ encoded.write(127);
+ encoded.write(buf, 0, 128);
+ }
+ equality = false;
+ lastVal = byt;
+ count = 1;
+ }
+ else if (count == 1)
+ {
+ if (byt == lastVal)
+ {
+ equality = true;
+ }
+ else
+ {
+ buf[0] = (byte) lastVal;
+ buf[1] = (byte) byt;
+ lastVal = byt;
+ }
+ count = 2;
+ }
+ else
+ {
+ // 1 < count < 128
+ if (byt == lastVal)
+ {
+ if (equality)
+ {
+ ++count;
+ }
+ else
+ {
+ // write all we got except the last
+ encoded.write(count - 2);
+ encoded.write(buf, 0, count - 1);
+ count = 2;
+ equality = true;
+ }
+ }
+ else
+ {
+ if (equality)
+ {
+ // equality ends here
+ encoded.write(257 - count);
+ encoded.write(lastVal);
+ equality = false;
+ count = 1;
+ }
+ else
+ {
+ buf[count] = (byte) byt;
+ ++count;
+ }
+ lastVal = byt;
+ }
+ }
+ }
+ }
+ if (count > 0)
+ {
+ if (count == 1)
+ {
+ encoded.write(0);
+ encoded.write(lastVal);
+ }
+ else if (equality)
+ {
+ encoded.write(257 - count);
+ encoded.write(lastVal);
+ }
+ else
+ {
+ encoded.write(count - 1);
+ encoded.write(buf, 0, count);
+ }
+ }
+ encoded.write(RUN_LENGTH_EOD);
}
}
Modified: pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/filter/TestFilters.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/filter/TestFilters.java?rev=1909475&r1=1909474&r2=1909475&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/filter/TestFilters.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/filter/TestFilters.java Fri Apr 28 15:35:23 2023
@@ -97,8 +97,7 @@ public class TestFilters extends TestCas
if( filter instanceof DCTFilter ||
filter instanceof CCITTFaxFilter ||
filter instanceof JPXFilter ||
- filter instanceof JBIG2Filter ||
- filter instanceof RunLengthDecodeFilter )
+ filter instanceof JBIG2Filter)
{
continue;
}
@@ -143,6 +142,35 @@ public class TestFilters extends TestCas
checkEncodeDecode(lzwFilter, byteArray);
}
+ /**
+ * Test simple and corner cases (128 identical, 128 identical at the end) of RLE implementation.
+ * 128 non identical bytes likely to be caught in random testing.
+ *
+ * @throws IOException
+ */
+ public void testRLE() throws IOException
+ {
+ Filter rleFilter = FilterFactory.INSTANCE.getFilter(COSName.RUN_LENGTH_DECODE);
+ byte[] input0 = new byte[0];
+ checkEncodeDecode(rleFilter, input0);
+ byte[] input1 = new byte[] { 1, 2, 3, 4, 5, (byte) 128, (byte) 140, (byte) 180, (byte) 0xFF};
+ checkEncodeDecode(rleFilter, input1);
+ byte[] input2 = new byte[10];
+ checkEncodeDecode(rleFilter, input2);
+ byte[] input3 = new byte[128];
+ checkEncodeDecode(rleFilter, input3);
+ byte[] input4 = new byte[129];
+ checkEncodeDecode(rleFilter, input4);
+ byte[] input5 = new byte[128 + 128];
+ checkEncodeDecode(rleFilter, input5);
+ byte[] input6 = new byte[1];
+ checkEncodeDecode(rleFilter, input6);
+ byte[] input7 = new byte[] {1, 2};
+ checkEncodeDecode(rleFilter, input7);
+ byte[] input8 = new byte[2];
+ checkEncodeDecode(rleFilter, input8);
+ }
+
private void checkEncodeDecode(Filter filter, byte[] original) throws IOException
{
ByteArrayOutputStream encoded = new ByteArrayOutputStream();