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 2014/04/29 08:15:24 UTC

svn commit: r1590878 - in /pdfbox/branches/1.8/pdfbox/src: main/java/org/apache/pdfbox/pdmodel/graphics/color/PDLab.java test/java/org/apache/pdfbox/pdmodel/graphics/color/PDLabTest.java

Author: tilman
Date: Tue Apr 29 06:15:23 2014
New Revision: 1590878

URL: http://svn.apache.org/r1590878
Log:
PDFBOX-2047: A read operation must not alter the pdf; created a test

Added:
    pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/color/PDLabTest.java   (with props)
Modified:
    pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDLab.java

Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDLab.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDLab.java?rev=1590878&r1=1590877&r2=1590878&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDLab.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDLab.java Tue Apr 29 06:15:23 2014
@@ -99,7 +99,7 @@ public class PDLab extends PDColorSpace
      */
     protected ColorSpace createColorSpace() throws IOException
     {
-        return new ColorSpaceLab(getWhitepoint(), getBlackPoint(), getARange(), getBRange());
+        return new ColorSpaceLab(getWhitePoint(), getBlackPoint(), getARange(), getBRange());
     }
 
     /**
@@ -135,24 +135,22 @@ public class PDLab extends PDColorSpace
     }
 
     /**
-     * This will return the whitepoint tristimulus.  As this is a required field
-     * this will never return null.  A default of 1,1,1 will be returned if the
-     * pdf does not have any values yet.
-     *
-     * @return The whitepoint tristimulus.
+     * This will return the whitepoint tristimulus.
+     * As this is a required field this will never return null.
+     * A default of 1,1,1 will be returned if the pdf does not have any values yet.
+     * @return the whitepoint tristimulus
      */
-    public PDTristimulus getWhitepoint()
+    public PDTristimulus getWhitePoint()
     {
-        COSArray wp = (COSArray)dictionary.getDictionaryObject( COSName.WHITE_POINT );
-        if( wp == null )
+        COSArray wp = (COSArray)dictionary.getDictionaryObject(COSName.WHITE_POINT);
+        if(wp == null)
         {
             wp = new COSArray();
-            wp.add( new COSFloat( 1.0f ) );
-            wp.add( new COSFloat( 1.0f ) );
-            wp.add( new COSFloat( 1.0f ) );
-            dictionary.setItem( COSName.WHITE_POINT, wp );
+            wp.add(new COSFloat(1.0f));
+            wp.add(new COSFloat(1.0f));
+            wp.add(new COSFloat(1.0f));
         }
-        return new PDTristimulus( wp );
+        return new PDTristimulus(wp);
     }
 
     /**
@@ -161,7 +159,7 @@ public class PDLab extends PDColorSpace
      *
      * @param wp The whitepoint tristimulus.
      */
-    public void setWhitepoint( PDTristimulus wp )
+    public void setWhitePoint( PDTristimulus wp )
     {
         COSBase wpArray = wp.getCOSObject();
         if( wpArray != null )
@@ -171,24 +169,22 @@ public class PDLab extends PDColorSpace
     }
 
     /**
-     * This will return the BlackPoint tristimulus.  This is an optional field but
-     * has defaults so this will never return null.
+     * This will return the BlackPoint tristimulus.
+     * This is an optional field but has defaults so this will never return null.
      * A default of 0,0,0 will be returned if the pdf does not have any values yet.
-     *
-     * @return The blackpoint tristimulus.
+     * @return the blackpoint tristimulus
      */
     public PDTristimulus getBlackPoint()
     {
-        COSArray bp = (COSArray)dictionary.getDictionaryObject( COSName.BLACK_POINT );
-        if( bp == null )
+        COSArray bp = (COSArray)dictionary.getDictionaryObject(COSName.BLACK_POINT);
+        if(bp == null)
         {
             bp = new COSArray();
-            bp.add( new COSFloat( 0.0f ) );
-            bp.add( new COSFloat( 0.0f ) );
-            bp.add( new COSFloat( 0.0f ) );
-            dictionary.setItem( COSName.BLACK_POINT, bp );
+            bp.add(new COSFloat(0.0f));
+            bp.add(new COSFloat(0.0f));
+            bp.add(new COSFloat(0.0f));
         }
-        return new PDTristimulus( bp );
+        return new PDTristimulus(bp);
     }
 
     /**
@@ -199,7 +195,6 @@ public class PDLab extends PDColorSpace
      */
     public void setBlackPoint( PDTristimulus bp )
     {
-
         COSBase bpArray = null;
         if( bp != null )
         {
@@ -208,86 +203,100 @@ public class PDLab extends PDColorSpace
         dictionary.setItem( COSName.BLACK_POINT, bpArray );
     }
 
-    private COSArray getRangeArray()
+    /**
+     * creates a range array with default values (-100..100 -100..100).
+     * @return the new range array.
+     */
+    private COSArray getDefaultRangeArray()
     {
-        COSArray range = (COSArray)dictionary.getDictionaryObject( COSName.RANGE );
-        if( range == null )
-        {
-            range = new COSArray();
-            dictionary.setItem( COSName.RANGE, array );
-            range.add( new COSFloat( -100 ) );
-            range.add( new COSFloat( 100 ) );
-            range.add( new COSFloat( -100 ) );
-            range.add( new COSFloat( 100 ) );
-        }
+        COSArray range = new COSArray();
+        range.add(new COSFloat(-100));
+        range.add(new COSFloat(100));
+        range.add(new COSFloat(-100));
+        range.add(new COSFloat(100));
         return range;
     }
 
     /**
-     * This will get the valid range for the a component.  If none is found
-     * then the default will be returned, which is -100 to 100.
-     *
-     * @return The a range.
+     * This will get the valid range for the "a" component.
+     * If none is found then the default will be returned, which is -100..100.
+     * @return the "a" range.
      */
     public PDRange getARange()
     {
-        COSArray range = getRangeArray();
-        return new PDRange( range, 0 );
+        COSArray rangeArray = (COSArray) dictionary.getDictionaryObject(COSName.RANGE);
+        if (rangeArray == null)
+        {
+            rangeArray = getDefaultRangeArray();
+        }
+        return new PDRange(rangeArray, 0);
     }
 
     /**
-     * This will set the a range for this color space.
-     *
-     * @param range The new range for the a component.
+     * This will set the a range for the "a" component.
+     * @param range the new range for the "a" component, 
+     * or null if defaults (-100..100) are to be set.
      */
-    public void setARange( PDRange range )
+    public void setARange(PDRange range)
     {
-        COSArray rangeArray = null;
+        COSArray rangeArray = (COSArray) dictionary.getDictionaryObject(COSName.RANGE);
+        if (rangeArray == null)
+        {
+            rangeArray = getDefaultRangeArray();
+        }
         //if null then reset to defaults
-        if( range == null )
+        if(range == null)
         {
-            rangeArray = getRangeArray();
-            rangeArray.set( 0, new COSFloat( -100 ) );
-            rangeArray.set( 1, new COSFloat( 100 ) );
+            rangeArray.set(0, new COSFloat(-100));
+            rangeArray.set(1, new COSFloat(100));
         }
         else
         {
-            rangeArray = range.getCOSArray();
+            rangeArray.set(0, new COSFloat(range.getMin()));
+            rangeArray.set(1, new COSFloat(range.getMax()));
         }
-        dictionary.setItem( COSName.RANGE, rangeArray );
+        dictionary.setItem(COSName.RANGE, rangeArray);
     }
 
     /**
-     * This will get the valid range for the b component.  If none is found
-     * then the default will be returned, which is -100 to 100.
-     *
-     * @return The b range.
+     * This will get the valid range for the "b" component.
+     * If none is found  then the default will be returned, which is -100..100.
+     * @return the "b" range.
      */
     public PDRange getBRange()
     {
-        COSArray range = getRangeArray();
-        return new PDRange( range, 1 );
+        COSArray rangeArray = (COSArray) dictionary.getDictionaryObject(COSName.RANGE);
+        if (rangeArray == null)
+        {
+            rangeArray = getDefaultRangeArray();
+        }
+        return new PDRange(rangeArray, 1);
     }
 
     /**
-     * This will set the b range for this color space.
-     *
-     * @param range The new range for the b component.
+     * This will set the "b" range for this color space.
+     * @param range the new range for the "b" component,
+     * or null if defaults (-100..100) are to be set.
      */
-    public void setBRange( PDRange range )
+    public void setBRange(PDRange range)
     {
-        COSArray rangeArray = null;
+        COSArray rangeArray = (COSArray) dictionary.getDictionaryObject(COSName.RANGE);
+        if (rangeArray == null)
+        {
+            rangeArray = getDefaultRangeArray();
+        }
         //if null then reset to defaults
-        if( range == null )
+        if(range == null)
         {
-            rangeArray = getRangeArray();
-            rangeArray.set( 2, new COSFloat( -100 ) );
-            rangeArray.set( 3, new COSFloat( 100 ) );
+            rangeArray.set(2, new COSFloat(-100));
+            rangeArray.set(3, new COSFloat(100));
         }
         else
         {
-            rangeArray = range.getCOSArray();
+            rangeArray.set(2, new COSFloat(range.getMin()));
+            rangeArray.set(3, new COSFloat(range.getMax()));
         }
-        dictionary.setItem( COSName.RANGE, rangeArray );
+        dictionary.setItem(COSName.RANGE, rangeArray);
     }
+
 }

Added: pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/color/PDLabTest.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/color/PDLabTest.java?rev=1590878&view=auto
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/color/PDLabTest.java (added)
+++ pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/color/PDLabTest.java Tue Apr 29 06:15:23 2014
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2014 The Apache Software Foundation.
+ *
+ * Licensed 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.pdmodel.graphics.color;
+
+import java.io.IOException;
+import junit.framework.TestCase;
+import org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.pdmodel.common.PDRange;
+
+/**
+ *
+ * @author Tilman Hausherr
+ */
+public class PDLabTest extends TestCase
+{
+
+    /**
+     * This test checks that getting default values do not alter the object.
+     */
+    public void testLAB() throws IOException
+    {
+        PDLab pdLab = new PDLab();
+        COSArray cosArray = (COSArray) pdLab.getCOSObject();
+        COSDictionary dict = (COSDictionary) cosArray.getObject(1);
+        
+        // test with default values
+        assertEquals("Lab", pdLab.getName());
+        assertEquals(3, pdLab.getNumberOfComponents());
+        assertEquals(0f, pdLab.getBlackPoint().getX());
+        assertEquals(0f, pdLab.getBlackPoint().getY());
+        assertEquals(0f, pdLab.getBlackPoint().getZ());
+        assertEquals(1f, pdLab.getWhitePoint().getX());
+        assertEquals(1f, pdLab.getWhitePoint().getY());
+        assertEquals(1f, pdLab.getWhitePoint().getZ());
+        assertEquals(-100f, pdLab.getARange().getMin());
+        assertEquals(100f, pdLab.getARange().getMax());
+        assertEquals(-100f, pdLab.getBRange().getMin());
+        assertEquals(100f, pdLab.getBRange().getMax());
+        assertEquals("read operations should not change the size of /Lab objects", 0, dict.size());
+        dict.toString(); // rev 1571125 did a stack overflow here
+
+        // test setting specific values
+        PDRange pdRange = new PDRange();
+        pdRange.setMin(-1);
+        pdRange.setMax(2);
+        pdLab.setARange(pdRange);
+        pdRange = new PDRange();
+        pdRange.setMin(3);
+        pdRange.setMax(4);
+        pdLab.setBRange(pdRange);
+        assertEquals(-1f, pdLab.getARange().getMin());
+        assertEquals(2f, pdLab.getARange().getMax());
+        assertEquals(3f, pdLab.getBRange().getMin());
+        assertEquals(4f, pdLab.getBRange().getMax());
+        PDTristimulus pdTristimulus = new PDTristimulus();
+        pdTristimulus.setX(5);
+        pdTristimulus.setY(6);
+        pdTristimulus.setZ(7);
+        pdLab.setWhitePoint(pdTristimulus);
+        pdTristimulus = new PDTristimulus();
+        pdTristimulus.setX(8);
+        pdTristimulus.setY(9);
+        pdTristimulus.setZ(10);
+        pdLab.setBlackPoint(pdTristimulus);
+        assertEquals(5f, pdLab.getWhitePoint().getX());
+        assertEquals(6f, pdLab.getWhitePoint().getY());
+        assertEquals(7f, pdLab.getWhitePoint().getZ());
+        assertEquals(8f, pdLab.getBlackPoint().getX());
+        assertEquals(9f, pdLab.getBlackPoint().getY());
+        assertEquals(10f, pdLab.getBlackPoint().getZ());
+    }
+
+}

Propchange: pdfbox/branches/1.8/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/color/PDLabTest.java
------------------------------------------------------------------------------
    svn:eol-style = native