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 java.awt.color.ColorSpace; 20 import java.awt.color.ICC_ColorSpace; 21 import java.io.IOException; 22 import java.io.OutputStream; 23 import java.util.Map; 24 25 import org.apache.pdfbox.cos.COSArray; 26 import org.apache.pdfbox.cos.COSBase; 27 import org.apache.pdfbox.cos.COSFloat; 28 import org.apache.pdfbox.cos.COSName; 29 import org.apache.pdfbox.pdmodel.PDDocument; 30 import org.apache.pdfbox.pdmodel.common.PDStream; 31 32 /** 33 * This class represents a color space in a pdf document. 34 * 35 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 36 * @version $Revision: 1.11 $ 37 */ 38 public final class PDColorSpaceFactory 39 { 40 /** 41 * Private constructor for utility classes. 42 */ 43 private PDColorSpaceFactory() 44 { 45 //utility class should not be implemented 46 } 47 48 /** 49 * This will create the correct color space given the name. 50 * 51 * @param colorSpace The color space object. 52 * 53 * @return The color space. 54 * 55 * @throws IOException If the color space name is unknown. 56 */ 57 public static PDColorSpace createColorSpace( COSBase colorSpace ) throws IOException 58 { 59 return createColorSpace( colorSpace, null ); 60 } 61 62 /** 63 * This will create the correct color space given the name. 64 * 65 * @param colorSpace The color space object. 66 * @param colorSpaces The ColorSpace dictionary from the current resources, if any. 67 * 68 * @return The color space. 69 * 70 * @throws IOException If the color space name is unknown. 71 */ 72 public static PDColorSpace createColorSpace( COSBase colorSpace, Map colorSpaces ) throws IOException 73 { 74 PDColorSpace retval = null; 75 if( colorSpace instanceof COSName ) 76 { 77 retval = createColorSpace( ((COSName)colorSpace).getName(), colorSpaces ); 78 } 79 else if( colorSpace instanceof COSArray ) 80 { 81 COSArray array = (COSArray)colorSpace; 82 COSName type = (COSName)array.getObject( 0 ); 83 if( type.getName().equals( PDCalGray.NAME ) ) 84 { 85 retval = new PDCalGray( array ); 86 } 87 else if( type.getName().equals( PDDeviceRGB.NAME ) ) 88 { 89 retval = PDDeviceRGB.INSTANCE; 90 } 91 else if( type.getName().equals( PDDeviceCMYK.NAME ) ) 92 { 93 retval = PDDeviceCMYK.INSTANCE; 94 } 95 else if( type.getName().equals( PDCalRGB.NAME ) ) 96 { 97 retval = new PDCalRGB( array ); 98 } 99 else if( type.getName().equals( PDDeviceN.NAME ) ) 100 { 101 retval = new PDDeviceN( array ); 102 } 103 else if( type.getName().equals( PDIndexed.NAME ) || 104 type.getName().equals( PDIndexed.ABBREVIATED_NAME )) 105 { 106 retval = new PDIndexed( array ); 107 } 108 else if( type.getName().equals( PDLab.NAME ) ) 109 { 110 retval = new PDLab( array ); 111 } 112 else if( type.getName().equals( PDSeparation.NAME ) ) 113 { 114 retval = new PDSeparation( array ); 115 } 116 else if( type.getName().equals( PDICCBased.NAME ) ) 117 { 118 retval = new PDICCBased( array ); 119 } 120 else if( type.getName().equals( PDPattern.NAME ) ) 121 { 122 retval = new PDPattern( array ); 123 } 124 else 125 { 126 throw new IOException( "Unknown colorspace array type:" + type ); 127 } 128 } 129 else 130 { 131 throw new IOException( "Unknown colorspace type:" + colorSpace ); 132 } 133 return retval; 134 } 135 136 /** 137 * This will create the correct color space given the name. 138 * 139 * @param colorSpaceName The name of the colorspace. 140 * 141 * @return The color space. 142 * 143 * @throws IOException If the color space name is unknown. 144 */ 145 public static PDColorSpace createColorSpace( String colorSpaceName ) throws IOException 146 { 147 return createColorSpace(colorSpaceName, null); 148 } 149 150 /** 151 * This will create the correct color space given the name. 152 * 153 * @param colorSpaceName The name of the colorspace. 154 * @param colorSpaces The ColorSpace dictionary from the current resources, if any. 155 * 156 * @return The color space. 157 * 158 * @throws IOException If the color space name is unknown. 159 */ 160 public static PDColorSpace createColorSpace( String colorSpaceName, Map colorSpaces ) throws IOException 161 { 162 PDColorSpace cs = null; 163 if( colorSpaceName.equals( PDDeviceCMYK.NAME ) || 164 colorSpaceName.equals( PDDeviceCMYK.ABBREVIATED_NAME ) ) 165 { 166 cs = PDDeviceCMYK.INSTANCE; 167 } 168 else if( colorSpaceName.equals( PDDeviceRGB.NAME ) || 169 colorSpaceName.equals( PDDeviceRGB.ABBREVIATED_NAME ) ) 170 { 171 cs = PDDeviceRGB.INSTANCE; 172 } 173 else if( colorSpaceName.equals( PDDeviceGray.NAME ) || 174 colorSpaceName.equals( PDDeviceGray.ABBREVIATED_NAME )) 175 { 176 cs = new PDDeviceGray(); 177 } 178 else if( colorSpaces != null && colorSpaces.get( colorSpaceName ) != null ) 179 { 180 cs = (PDColorSpace)colorSpaces.get( colorSpaceName ); 181 } 182 else if( colorSpaceName.equals( PDLab.NAME ) ) 183 { 184 cs = new PDLab(); 185 } 186 else if( colorSpaceName.equals( PDPattern.NAME ) ) 187 { 188 cs = new PDPattern(); 189 } 190 else 191 { 192 throw new IOException( "Error: Unknown colorspace '" + colorSpaceName + "'" ); 193 } 194 return cs; 195 } 196 197 /** 198 * This will create the correct color space from a java colorspace. 199 * 200 * @param doc The doc to potentiall write information to. 201 * @param cs The awt colorspace. 202 * 203 * @return The color space. 204 * 205 * @throws IOException If the color space name is unknown. 206 */ 207 public static PDColorSpace createColorSpace( PDDocument doc, ColorSpace cs ) throws IOException 208 { 209 PDColorSpace retval = null; 210 if( cs.isCS_sRGB() ) 211 { 212 retval = PDDeviceRGB.INSTANCE; 213 } 214 else if( cs instanceof ICC_ColorSpace ) 215 { 216 ICC_ColorSpace ics = (ICC_ColorSpace)cs; 217 PDICCBased pdCS = new PDICCBased( doc ); 218 retval = pdCS; 219 COSArray ranges = new COSArray(); 220 for( int i=0; i<cs.getNumComponents(); i++ ) 221 { 222 ranges.add( new COSFloat( ics.getMinValue( i ) ) ); 223 ranges.add( new COSFloat( ics.getMaxValue( i ) ) ); 224 } 225 PDStream iccData = pdCS.getPDStream(); 226 OutputStream output = null; 227 try 228 { 229 output = iccData.createOutputStream(); 230 output.write( ics.getProfile().getData() ); 231 } 232 finally 233 { 234 if( output != null ) 235 { 236 output.close(); 237 } 238 } 239 pdCS.setNumberOfComponents( cs.getNumComponents() ); 240 } 241 else 242 { 243 throw new IOException( "Not yet implemented:" + cs ); 244 } 245 return retval; 246 } 247 }