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.COSInteger;
035: import org.pdfbox.cos.COSName;
036: import org.pdfbox.cos.COSNumber;
037: import org.pdfbox.cos.COSStream;
038: import org.pdfbox.cos.COSString;
039:
040: import java.awt.color.ColorSpace;
041: import java.awt.image.ColorModel;
042: import java.awt.image.IndexColorModel;
043:
044: import java.io.InputStream;
045: import java.io.ByteArrayOutputStream;
046: import java.io.IOException;
047:
048: /**
049: * This class represents an Indexed color space.
050: *
051: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
052: * @version $Revision: 1.4 $
053: */
054: public class PDIndexed extends PDColorSpace {
055:
056: /**
057: * The name of this color space.
058: */
059: public static final String NAME = "Indexed";
060:
061: /**
062: * The abbreviated name of this color space.
063: */
064: public static final String ABBREVIATED_NAME = "I";
065:
066: private COSArray array;
067:
068: /**
069: * Constructor, default DeviceRGB, hival 255.
070: */
071: public PDIndexed() {
072: array = new COSArray();
073: array.add(COSName.getPDFName(NAME));
074: array.add(COSName.getPDFName(PDDeviceRGB.NAME));
075: array.add(new COSInteger(255));
076: array.add(org.pdfbox.cos.COSNull.NULL);
077: }
078:
079: /**
080: * Constructor.
081: *
082: * @param indexedArray The array containing the indexed parameters
083: */
084: public PDIndexed(COSArray indexedArray) {
085: array = indexedArray;
086: }
087:
088: /**
089: * This will return the number of color components. This will return the
090: * number of color components in the base color.
091: *
092: * @return The number of components in this color space.
093: *
094: * @throws IOException If there is an error getting the number of color components.
095: */
096: public int getNumberOfComponents() throws IOException {
097: return getBaseColorSpace().getNumberOfComponents();
098: }
099:
100: /**
101: * This will return the name of the color space.
102: *
103: * @return The name of the color space.
104: */
105: public String getName() {
106: return NAME;
107: }
108:
109: /**
110: * Create a Java colorspace for this colorspace.
111: *
112: * @return A color space that can be used for Java AWT operations.
113: *
114: * @throws IOException If there is an error creating the color space.
115: */
116: public ColorSpace createColorSpace() throws IOException {
117: throw new IOException("Not implemented");
118: }
119:
120: /**
121: * Create a Java color model for this colorspace.
122: *
123: * @param bpc The number of bits per component.
124: *
125: * @return A color model that can be used for Java AWT operations.
126: *
127: * @throws IOException If there is an error creating the color model.
128: */
129: public ColorModel createColorModel(int bpc) throws IOException {
130: int size = getHighValue();
131: byte[] index = getLookupData();
132: //for (int i=0;i<index.length;i++) System.out.print(index[i]+" ");
133:
134: ColorModel cm = new IndexColorModel(bpc, size + 1, index, 0,
135: false);
136: return cm;
137: }
138:
139: /**
140: * This will get the color space that acts as the index for this color space.
141: *
142: * @return The base color space.
143: *
144: * @throws IOException If there is error creating the base color space.
145: */
146: public PDColorSpace getBaseColorSpace() throws IOException {
147: PDColorSpace retval = null;
148: COSBase base = array.getObject(1);
149: if (base instanceof COSName) {
150: retval = PDColorSpaceFactory.createColorSpace(base);
151: } else {
152: throw new IOException("Error:unknown base colorspace");
153: }
154:
155: return retval;
156: }
157:
158: /**
159: * This will set the base color space.
160: *
161: * @param base The base color space to use as the index.
162: */
163: public void setBaseColorSpace(PDColorSpace base) {
164: array.set(1, base.getCOSObject());
165: }
166:
167: /**
168: * Get the highest value for the lookup.
169: *
170: * @return The hival entry.
171: */
172: public int getHighValue() {
173: return ((COSNumber) array.getObject(2)).intValue();
174: }
175:
176: /**
177: * This will set the highest value that is allowed. This cannot be higher
178: * than 255.
179: *
180: * @param high The highest value for the lookup table.
181: */
182: public void setHighValue(int high) {
183: array.set(2, new COSInteger(high));
184: }
185:
186: /**
187: * This will perform a lookup into the color lookup table.
188: *
189: * @param componentNumber The component number, probably 1,2,3,3.
190: * @param lookupIndex The zero-based index into the table, should not exceed the high value.
191: *
192: * @return The value that was from the lookup table.
193: *
194: * @throws IOException If there is an error looking up the color.
195: */
196: public int lookupColor(int componentNumber, int lookupIndex)
197: throws IOException {
198: PDColorSpace baseColor = getBaseColorSpace();
199: byte[] data = getLookupData();
200: int numberOfComponents = baseColor.getNumberOfComponents();
201: return (data[componentNumber * numberOfComponents + lookupIndex] + 256) % 256;
202: }
203:
204: private byte[] getLookupData() throws IOException {
205: COSBase lookupTable = array.getObject(3);
206: byte[] data = null;
207: if (lookupTable instanceof COSString) {
208: data = ((COSString) lookupTable).getBytes();
209: } else if (lookupTable instanceof COSStream) {
210: //Data will be small so just load the whole thing into memory for
211: //easier processing
212: COSStream lookupStream = (COSStream) lookupTable;
213: InputStream input = lookupStream.getUnfilteredStream();
214: ByteArrayOutputStream output = new ByteArrayOutputStream(
215: 1024);
216: byte[] buffer = new byte[1024];
217: int amountRead;
218: while ((amountRead = input.read(buffer, 0, buffer.length)) != -1) {
219: output.write(buffer, 0, amountRead);
220: }
221: data = output.toByteArray();
222: } else if (lookupTable == null) {
223: data = new byte[0];
224: } else {
225: throw new IOException(
226: "Error: Unknown type for lookup table "
227: + lookupTable);
228: }
229: return data;
230: }
231:
232: /**
233: * This will set a color in the color lookup table.
234: *
235: * @param componentNumber The component number, probably 1,2,3,3.
236: * @param lookupIndex The zero-based index into the table, should not exceed the high value.
237: * @param color The color that will go into the table.
238: *
239: * @throws IOException If there is an error looking up the color.
240: */
241: public void setLookupColor(int componentNumber, int lookupIndex,
242: int color) throws IOException {
243: PDColorSpace baseColor = getBaseColorSpace();
244: int numberOfComponents = baseColor.getNumberOfComponents();
245: byte[] data = getLookupData();
246: data[componentNumber * numberOfComponents + lookupIndex] = (byte) color;
247: COSString string = new COSString(data);
248: array.set(3, string);
249: }
250: }
|