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 java.awt.color.ColorSpace;
033: import java.awt.color.ICC_ColorSpace;
034: import java.io.IOException;
035: import java.io.OutputStream;
036:
037: import org.pdfbox.cos.COSArray;
038: import org.pdfbox.cos.COSBase;
039: import org.pdfbox.cos.COSFloat;
040: import org.pdfbox.cos.COSName;
041: import org.pdfbox.pdmodel.PDDocument;
042: import org.pdfbox.pdmodel.common.PDStream;
043:
044: /**
045: * This class represents a color space in a pdf document.
046: *
047: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
048: * @version $Revision: 1.11 $
049: */
050: public final class PDColorSpaceFactory {
051: /**
052: * Private constructor for utility classes.
053: */
054: private PDColorSpaceFactory() {
055: //utility class should not be implemented
056: }
057:
058: /**
059: * This will create the correct color space given the name.
060: *
061: * @param colorSpace The color space object.
062: *
063: * @return The color space.
064: *
065: * @throws IOException If the color space name is unknown.
066: */
067: public static PDColorSpace createColorSpace(COSBase colorSpace)
068: throws IOException {
069: PDColorSpace retval = null;
070: if (colorSpace instanceof COSName) {
071: retval = createColorSpace(((COSName) colorSpace).getName());
072: } else if (colorSpace instanceof COSArray) {
073: COSArray array = (COSArray) colorSpace;
074: COSName type = (COSName) array.getObject(0);
075: if (type.getName().equals(PDCalGray.NAME)) {
076: retval = new PDCalGray(array);
077: } else if (type.getName().equals(PDCalRGB.NAME)) {
078: retval = new PDCalRGB(array);
079: } else if (type.getName().equals(PDDeviceN.NAME)) {
080: retval = new PDDeviceN(array);
081: } else if (type.getName().equals(PDIndexed.NAME)
082: || type.getName()
083: .equals(PDIndexed.ABBREVIATED_NAME)) {
084: retval = new PDIndexed(array);
085: } else if (type.getName().equals(PDLab.NAME)) {
086: retval = new PDLab(array);
087: } else if (type.getName().equals(PDSeparation.NAME)) {
088: retval = new PDSeparation(array);
089: } else if (type.getName().equals(PDICCBased.NAME)) {
090: retval = new PDICCBased(array);
091: } else if (type.getName().equals(PDPattern.NAME)) {
092: retval = new PDPattern(array);
093: } else {
094: throw new IOException("Unknown colorspace array type:"
095: + type);
096: }
097: } else {
098: throw new IOException("Unknown colorspace type:"
099: + colorSpace);
100: }
101: return retval;
102: }
103:
104: /**
105: * This will create the correct color space given the name.
106: *
107: * @param colorSpaceName The name of the colorspace.
108: *
109: * @return The color space.
110: *
111: * @throws IOException If the color space name is unknown.
112: */
113: public static PDColorSpace createColorSpace(String colorSpaceName)
114: throws IOException {
115: PDColorSpace cs = null;
116: if (colorSpaceName.equals(PDDeviceCMYK.NAME)
117: || colorSpaceName.equals(PDDeviceCMYK.ABBREVIATED_NAME)) {
118: cs = PDDeviceCMYK.INSTANCE;
119: } else if (colorSpaceName.equals(PDDeviceRGB.NAME)
120: || colorSpaceName.equals(PDDeviceRGB.ABBREVIATED_NAME)) {
121: cs = PDDeviceRGB.INSTANCE;
122: } else if (colorSpaceName.equals(PDDeviceGray.NAME)
123: || colorSpaceName.equals(PDDeviceGray.ABBREVIATED_NAME)) {
124: cs = new PDDeviceGray();
125: } else if (colorSpaceName.equals(PDLab.NAME)) {
126: cs = new PDLab();
127: } else if (colorSpaceName.equals(PDPattern.NAME)) {
128: cs = new PDPattern();
129: } else {
130: throw new IOException("Error: Unknown colorspace '"
131: + colorSpaceName + "'");
132: }
133: return cs;
134: }
135:
136: /**
137: * This will create the correct color space from a java colorspace.
138: *
139: * @param doc The doc to potentiall write information to.
140: * @param cs The awt colorspace.
141: *
142: * @return The color space.
143: *
144: * @throws IOException If the color space name is unknown.
145: */
146: public static PDColorSpace createColorSpace(PDDocument doc,
147: ColorSpace cs) throws IOException {
148: PDColorSpace retval = null;
149: if (cs.isCS_sRGB()) {
150: retval = PDDeviceRGB.INSTANCE;
151: } else if (cs instanceof ICC_ColorSpace) {
152: ICC_ColorSpace ics = (ICC_ColorSpace) cs;
153: PDICCBased pdCS = new PDICCBased(doc);
154: retval = pdCS;
155: COSArray ranges = new COSArray();
156: for (int i = 0; i < cs.getNumComponents(); i++) {
157: ranges.add(new COSFloat(ics.getMinValue(i)));
158: ranges.add(new COSFloat(ics.getMaxValue(i)));
159: }
160: PDStream iccData = pdCS.getPDStream();
161: OutputStream output = null;
162: try {
163: output = iccData.createOutputStream();
164: output.write(ics.getProfile().getData());
165: } finally {
166: if (output != null) {
167: output.close();
168: }
169: }
170: pdCS.setNumberOfComponents(cs.getNumComponents());
171: } else {
172: throw new IOException("Not yet implemented:" + cs);
173: }
174: return retval;
175: }
176: }
|