Home » pdfbox-1.1.0-src » org.apache.pdfbox.pdmodel.graphics.color » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.pdfbox.pdmodel.graphics.color;
   18   
   19   import org.apache.pdfbox.cos.COSArray;
   20   import org.apache.pdfbox.cos.COSBase;
   21   import org.apache.pdfbox.cos.COSDictionary;
   22   import org.apache.pdfbox.cos.COSFloat;
   23   import org.apache.pdfbox.cos.COSName;
   24   
   25   import org.apache.pdfbox.pdmodel.common.PDRange;
   26   
   27   import java.awt.color.ColorSpace;
   28   import java.awt.image.ColorModel;
   29   
   30   import java.io.IOException;
   31   
   32   /**
   33    * This class represents a Lab color space.
   34    *
   35    * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
   36    * @version $Revision: 1.4 $
   37    */
   38   public class PDLab extends PDColorSpace
   39   {
   40       /**
   41        * The name of this color space.
   42        */
   43       public static final String NAME = "Lab";
   44   
   45       private COSArray array;
   46       private COSDictionary dictionary;
   47   
   48       /**
   49        * Constructor.
   50        */
   51       public PDLab()
   52       {
   53           array = new COSArray();
   54           dictionary = new COSDictionary();
   55           array.add( COSName.getPDFName( NAME ) );
   56           array.add( dictionary );
   57       }
   58   
   59       /**
   60        * Constructor with array.
   61        *
   62        * @param lab The underlying color space.
   63        */
   64       public PDLab( COSArray lab )
   65       {
   66           array = lab;
   67           dictionary = (COSDictionary)array.getObject( 1 );
   68       }
   69   
   70       /**
   71        * This will return the name of the color space.
   72        *
   73        * @return The name of the color space.
   74        */
   75       public String getName()
   76       {
   77           return NAME;
   78       }
   79   
   80       /**
   81        * Convert this standard java object to a COS object.
   82        *
   83        * @return The cos object that matches this Java object.
   84        */
   85       public COSBase getCOSObject()
   86       {
   87           return array;
   88       }
   89   
   90       /**
   91        * Create a Java colorspace for this colorspace.
   92        *
   93        * @return A color space that can be used for Java AWT operations.
   94        *
   95        * @throws IOException If there is an error creating the color space.
   96        */
   97       protected ColorSpace createColorSpace() throws IOException
   98       {
   99           throw new IOException( "Not implemented" );
  100       }
  101   
  102       /**
  103        * Create a Java color model for this colorspace.
  104        *
  105        * @param bpc The number of bits per component.
  106        *
  107        * @return A color model that can be used for Java AWT operations.
  108        *
  109        * @throws IOException If there is an error creating the color model.
  110        */
  111       public ColorModel createColorModel( int bpc ) throws IOException
  112       {
  113           throw new IOException( "Not implemented" );
  114       }
  115   
  116       /**
  117        * This will get the number of components that this color space is made up of.
  118        *
  119        * @return The number of components in this color space.
  120        *
  121        * @throws IOException If there is an error getting the number of color components.
  122        */
  123       public int getNumberOfComponents() throws IOException
  124       {
  125           //BJL
  126           //hmm is this correct, I am not 100% sure.
  127           return 3;
  128       }
  129   
  130       /**
  131        * This will return the whitepoint tristimulus.  As this is a required field
  132        * this will never return null.  A default of 1,1,1 will be returned if the
  133        * pdf does not have any values yet.
  134        *
  135        * @return The whitepoint tristimulus.
  136        */
  137       public PDTristimulus getWhitepoint()
  138       {
  139           COSArray wp = (COSArray)dictionary.getDictionaryObject( COSName.getPDFName( "WhitePoint" ) );
  140           if( wp == null )
  141           {
  142               wp = new COSArray();
  143               wp.add( new COSFloat( 1.0f ) );
  144               wp.add( new COSFloat( 1.0f ) );
  145               wp.add( new COSFloat( 1.0f ) );
  146               dictionary.setItem( COSName.getPDFName( "WhitePoint" ), wp );
  147           }
  148           return new PDTristimulus( wp );
  149       }
  150   
  151       /**
  152        * This will set the whitepoint tristimulus.  As this is a required field
  153        * this null should not be passed into this function.
  154        *
  155        * @param wp The whitepoint tristimulus.
  156        */
  157       public void setWhitepoint( PDTristimulus wp )
  158       {
  159           COSBase wpArray = wp.getCOSObject();
  160           if( wpArray != null )
  161           {
  162               dictionary.setItem( COSName.getPDFName( "WhitePoint" ), wpArray );
  163           }
  164       }
  165   
  166       /**
  167        * This will return the BlackPoint tristimulus.  This is an optional field but
  168        * has defaults so this will never return null.
  169        * A default of 0,0,0 will be returned if the pdf does not have any values yet.
  170        *
  171        * @return The blackpoint tristimulus.
  172        */
  173       public PDTristimulus getBlackPoint()
  174       {
  175           COSArray bp = (COSArray)dictionary.getDictionaryObject( COSName.getPDFName( "BlackPoint" ) );
  176           if( bp == null )
  177           {
  178               bp = new COSArray();
  179               bp.add( new COSFloat( 0.0f ) );
  180               bp.add( new COSFloat( 0.0f ) );
  181               bp.add( new COSFloat( 0.0f ) );
  182               dictionary.setItem( COSName.getPDFName( "BlackPoint" ), bp );
  183           }
  184           return new PDTristimulus( bp );
  185       }
  186   
  187       /**
  188        * This will set the BlackPoint tristimulus.  As this is a required field
  189        * this null should not be passed into this function.
  190        *
  191        * @param bp The BlackPoint tristimulus.
  192        */
  193       public void setBlackPoint( PDTristimulus bp )
  194       {
  195   
  196           COSBase bpArray = null;
  197           if( bp != null )
  198           {
  199               bpArray = bp.getCOSObject();
  200           }
  201           dictionary.setItem( COSName.getPDFName( "BlackPoint" ), bpArray );
  202       }
  203   
  204       private COSArray getRangeArray()
  205       {
  206           COSArray range = (COSArray)dictionary.getDictionaryObject( COSName.getPDFName( "Range" ) );
  207           if( range == null )
  208           {
  209               range = new COSArray();
  210               dictionary.setItem( COSName.getPDFName( "Range" ), array );
  211               range.add( new COSFloat( -100 ) );
  212               range.add( new COSFloat( 100 ) );
  213               range.add( new COSFloat( -100 ) );
  214               range.add( new COSFloat( 100 ) );
  215           }
  216           return range;
  217       }
  218   
  219       /**
  220        * This will get the valid range for the a component.  If none is found
  221        * then the default will be returned, which is -100 to 100.
  222        *
  223        * @return The a range.
  224        */
  225       public PDRange getARange()
  226       {
  227           COSArray range = getRangeArray();
  228           return new PDRange( range, 0 );
  229       }
  230   
  231       /**
  232        * This will set the a range for this color space.
  233        *
  234        * @param range The new range for the a component.
  235        */
  236       public void setARange( PDRange range )
  237       {
  238           COSArray rangeArray = null;
  239           //if null then reset to defaults
  240           if( range == null )
  241           {
  242               rangeArray = getRangeArray();
  243               rangeArray.set( 0, new COSFloat( -100 ) );
  244               rangeArray.set( 1, new COSFloat( 100 ) );
  245           }
  246           else
  247           {
  248               rangeArray = range.getCOSArray();
  249           }
  250           dictionary.setItem( COSName.getPDFName( "Range" ), rangeArray );
  251       }
  252   
  253       /**
  254        * This will get the valid range for the b component.  If none is found
  255        * then the default will be returned, which is -100 to 100.
  256        *
  257        * @return The b range.
  258        */
  259       public PDRange getBRange()
  260       {
  261           COSArray range = getRangeArray();
  262           return new PDRange( range, 1 );
  263       }
  264   
  265       /**
  266        * This will set the b range for this color space.
  267        *
  268        * @param range The new range for the b component.
  269        */
  270       public void setBRange( PDRange range )
  271       {
  272           COSArray rangeArray = null;
  273           //if null then reset to defaults
  274           if( range == null )
  275           {
  276               rangeArray = getRangeArray();
  277               rangeArray.set( 2, new COSFloat( -100 ) );
  278               rangeArray.set( 3, new COSFloat( 100 ) );
  279           }
  280           else
  281           {
  282               rangeArray = range.getCOSArray();
  283           }
  284           dictionary.setItem( COSName.getPDFName( "Range" ), rangeArray );
  285       }
  286   }

Home » pdfbox-1.1.0-src » org.apache.pdfbox.pdmodel.graphics.color » [javadoc | source]