FOP-2719: PDF to PS NPE when encode param not set

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop-pdf-images/trunk@1801064 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/java/org/apache/fop/render/pdf/pdfbox/PSPDFGraphics2D.java b/src/java/org/apache/fop/render/pdf/pdfbox/PSPDFGraphics2D.java
index 0b3584c..ad7c847 100644
--- a/src/java/org/apache/fop/render/pdf/pdfbox/PSPDFGraphics2D.java
+++ b/src/java/org/apache/fop/render/pdf/pdfbox/PSPDFGraphics2D.java
@@ -173,7 +173,7 @@
         }
     }
 
-    private static Function getFunction(PDFunction f) throws IOException {
+    protected static Function getFunction(PDFunction f) throws IOException {
         if (f instanceof PDFunctionType3) {
             PDFunctionType3 sourceFT3 = (PDFunctionType3) f;
             float[] bounds = sourceFT3.getBounds().toFloatArray();
@@ -193,11 +193,11 @@
             COSDictionary s = f.getCOSObject();
             assert s instanceof COSStream;
             COSStream stream = (COSStream) s;
-            COSArray encode = (COSArray) s.getDictionaryObject(COSName.ENCODE);
             COSArray domain = (COSArray) s.getDictionaryObject(COSName.DOMAIN);
             COSArray range = (COSArray) s.getDictionaryObject(COSName.RANGE);
             int bits = ((COSInteger)s.getDictionaryObject(COSName.BITS_PER_SAMPLE)).intValue();
             COSArray size = (COSArray) s.getDictionaryObject(COSName.SIZE);
+            COSArray encode = getEncode(s);
             byte[] x = IOUtils.toByteArray(stream.getUnfilteredStream());
             for (byte y : x) {
                 if (y != 0) {
@@ -215,6 +215,20 @@
         throw new IOException("Unsupported " + f.toString());
     }
 
+    private static COSArray getEncode(COSDictionary s) {
+        COSArray encode = (COSArray) s.getDictionaryObject(COSName.ENCODE);
+        if (encode == null) {
+            encode = new COSArray();
+            COSArray size = (COSArray) s.getDictionaryObject(COSName.SIZE);
+            int sizeValuesSize = size.size();
+            for (int i = 0; i < sizeValuesSize; i++) {
+                encode.add(COSInteger.ZERO);
+                encode.add(COSInteger.get(size.getInt(i) - 1));
+            }
+        }
+        return encode;
+    }
+
     private static List<Float> toList(float[] array) {
         List<Float> list = new ArrayList<Float>(array.length);
         for (float f : array) {
diff --git a/test/java/org/apache/fop/render/pdf/PSPDFGraphics2DTestCase.java b/test/java/org/apache/fop/render/pdf/PSPDFGraphics2DTestCase.java
new file mode 100644
index 0000000..b3eadca
--- /dev/null
+++ b/test/java/org/apache/fop/render/pdf/PSPDFGraphics2DTestCase.java
@@ -0,0 +1,73 @@
+/*
+ * 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.
+ */
+
+/* $Id$ */
+package org.apache.fop.render.pdf;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSInteger;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSStream;
+import org.apache.pdfbox.pdmodel.common.function.PDFunction;
+import org.apache.pdfbox.pdmodel.common.function.PDFunctionType0;
+
+import org.apache.fop.render.gradient.Function;
+import org.apache.fop.render.pdf.pdfbox.PSPDFGraphics2D;
+
+public class PSPDFGraphics2DTestCase {
+
+    @Test
+    public void testShading() throws IOException {
+        COSStream stream = new COSStream();
+        OutputStream streamData = stream.createOutputStream();
+        streamData.write("test".getBytes("UTF-8"));
+        streamData.close();
+        stream.setItem(COSName.BITS_PER_SAMPLE, COSInteger.get(8));
+        stream.setItem(COSName.FUNCTION_TYPE, COSInteger.ZERO);
+        COSArray range = new COSArray();
+        range.add(COSInteger.ZERO);
+        range.add(COSInteger.ONE);
+        range.add(COSInteger.ZERO);
+        range.add(COSInteger.ONE);
+        range.add(COSInteger.ZERO);
+        range.add(COSInteger.ONE);
+        stream.setItem(COSName.RANGE, range);
+        stream.setItem(COSName.DOMAIN, range);
+        COSArray size = new COSArray();
+        size.add(COSInteger.ONE);
+        stream.setItem(COSName.SIZE, size);
+
+        Function f = new MyPSPDFGraphics2D().getAFunction(new PDFunctionType0(stream));
+        Assert.assertEquals(f.getBitsPerSample(), 8);
+    }
+
+    static class MyPSPDFGraphics2D extends PSPDFGraphics2D {
+        MyPSPDFGraphics2D() {
+            super(false);
+        }
+
+        Function getAFunction(PDFunction function) throws IOException {
+            return getFunction(function);
+        }
+    }
+}