001: /**
002: * Copyright (c) 2004, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.graphics.color;
031:
032: import org.pdfbox.cos.COSArray;
033: import org.pdfbox.cos.COSBase;
034: import org.pdfbox.cos.COSDictionary;
035: import org.pdfbox.cos.COSFloat;
036: import org.pdfbox.cos.COSName;
037: import org.pdfbox.cos.COSNumber;
038:
039: import java.awt.color.ColorSpace;
040: import java.awt.image.ColorModel;
041:
042: import java.io.IOException;
043:
044: /**
045: * This class represents a Cal Gray color space.
046: *
047: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
048: * @version $Revision: 1.5 $
049: */
050: public class PDCalGray extends PDColorSpace {
051: /**
052: * The name of this color space.
053: */
054: public static final String NAME = "CalGray";
055:
056: private COSArray array;
057: private COSDictionary dictionary;
058:
059: /**
060: * Constructor.
061: */
062: public PDCalGray() {
063: array = new COSArray();
064: dictionary = new COSDictionary();
065: array.add(COSName.getPDFName(NAME));
066: array.add(dictionary);
067: }
068:
069: /**
070: * Constructor with array.
071: *
072: * @param gray The underlying color space.
073: */
074: public PDCalGray(COSArray gray) {
075: array = gray;
076: dictionary = (COSDictionary) array.getObject(1);
077: }
078:
079: /**
080: * This will get the number of components that this color space is made up of.
081: *
082: * @return The number of components in this color space.
083: *
084: * @throws IOException If there is an error getting the number of color components.
085: */
086: public int getNumberOfComponents() throws IOException {
087: return 1;
088: }
089:
090: /**
091: * This will return the name of the color space.
092: *
093: * @return The name of the color space.
094: */
095: public String getName() {
096: return NAME;
097: }
098:
099: /**
100: * Create a Java colorspace for this colorspace.
101: *
102: * @return A color space that can be used for Java AWT operations.
103: *
104: * @throws IOException If there is an error creating the color space.
105: */
106: public ColorSpace createColorSpace() throws IOException {
107: throw new IOException("Not implemented");
108: }
109:
110: /**
111: * Create a Java color model for this colorspace.
112: *
113: * @param bpc The number of bits per component.
114: *
115: * @return A color model that can be used for Java AWT operations.
116: *
117: * @throws IOException If there is an error creating the color model.
118: */
119: public ColorModel createColorModel(int bpc) throws IOException {
120: throw new IOException("Not implemented");
121: }
122:
123: /**
124: * Convert this standard java object to a COS object.
125: *
126: * @return The cos object that matches this Java object.
127: */
128: public COSBase getCOSObject() {
129: return array;
130: }
131:
132: /**
133: * This will get the gamma value. If none is present then the default of 1
134: * will be returned.
135: *
136: * @return The gamma value.
137: */
138: public float getGamma() {
139: float retval = 1.0f;
140: COSNumber gamma = (COSNumber) dictionary
141: .getDictionaryObject(COSName.getPDFName("Gamma"));
142: if (gamma != null) {
143: retval = gamma.floatValue();
144: }
145: return retval;
146: }
147:
148: /**
149: * Set the gamma value.
150: *
151: * @param value The new gamma value.
152: */
153: public void setGamma(float value) {
154: dictionary.setItem(COSName.getPDFName("Gamma"), new COSFloat(
155: value));
156: }
157:
158: /**
159: * This will return the whitepoint tristimulus. As this is a required field
160: * this will never return null. A default of 1,1,1 will be returned if the
161: * pdf does not have any values yet.
162: *
163: * @return The whitepoint tristimulus.
164: */
165: public PDTristimulus getWhitepoint() {
166: COSArray wp = (COSArray) dictionary.getDictionaryObject(COSName
167: .getPDFName("WhitePoint"));
168: if (wp == null) {
169: wp = new COSArray();
170: wp.add(new COSFloat(1.0f));
171: wp.add(new COSFloat(1.0f));
172: wp.add(new COSFloat(1.0f));
173: dictionary.setItem(COSName.getPDFName("WhitePoint"), wp);
174: }
175: return new PDTristimulus(wp);
176: }
177:
178: /**
179: * This will set the whitepoint tristimulus. As this is a required field
180: * this null should not be passed into this function.
181: *
182: * @param wp The whitepoint tristimulus.
183: */
184: public void setWhitepoint(PDTristimulus wp) {
185: COSBase wpArray = wp.getCOSObject();
186: if (wpArray != null) {
187: dictionary.setItem(COSName.getPDFName("WhitePoint"),
188: wpArray);
189: }
190: }
191:
192: /**
193: * This will return the BlackPoint tristimulus. This is an optional field but
194: * has defaults so this will never return null.
195: * A default of 0,0,0 will be returned if the pdf does not have any values yet.
196: *
197: * @return The blackpoint tristimulus.
198: */
199: public PDTristimulus getBlackPoint() {
200: COSArray bp = (COSArray) dictionary.getDictionaryObject(COSName
201: .getPDFName("BlackPoint"));
202: if (bp == null) {
203: bp = new COSArray();
204: bp.add(new COSFloat(0.0f));
205: bp.add(new COSFloat(0.0f));
206: bp.add(new COSFloat(0.0f));
207: dictionary.setItem(COSName.getPDFName("BlackPoint"), bp);
208: }
209: return new PDTristimulus(bp);
210: }
211:
212: /**
213: * This will set the BlackPoint tristimulus. As this is a required field
214: * this null should not be passed into this function.
215: *
216: * @param bp The BlackPoint tristimulus.
217: */
218: public void setBlackPoint(PDTristimulus bp) {
219: COSBase bpArray = null;
220: if (bp != null) {
221: bpArray = bp.getCOSObject();
222: }
223: dictionary.setItem(COSName.getPDFName("BlackPoint"), bpArray);
224: }
225: }
|