Skip to content

Commit de39ee8

Browse files
committed
PDFBOX-6192: add factory method to create an instance of PDIndexed based on a request from Stefan Ziegler
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1934296 13f79535-47bb-0310-9956-ffa450edef68
1 parent 74c3e5a commit de39ee8

2 files changed

Lines changed: 130 additions & 0 deletions

File tree

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexed.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
import java.awt.image.Raster;
2525
import java.awt.image.WritableRaster;
2626
import java.io.IOException;
27+
import java.util.Arrays;
2728

2829
import org.apache.pdfbox.cos.COSArray;
2930
import org.apache.pdfbox.cos.COSBase;
31+
import org.apache.pdfbox.cos.COSInteger;
3032
import org.apache.pdfbox.cos.COSName;
3133
import org.apache.pdfbox.cos.COSNumber;
3234
import org.apache.pdfbox.cos.COSStream;
@@ -53,6 +55,13 @@ public final class PDIndexed extends PDSpecialColorSpace
5355
private int actualMaxIndex;
5456
private int[][] rgbColorTable;
5557

58+
/**
59+
* Private constructor.
60+
*/
61+
private PDIndexed()
62+
{
63+
// needed for the create factory method
64+
}
5665
/**
5766
* Creates a new indexed color space from the given PDF array.
5867
*
@@ -81,6 +90,42 @@ public PDIndexed(COSArray indexedArray, PDResources resources) throws IOExceptio
8190
initRgbColorTable();
8291
}
8392

93+
/**
94+
* Factory method to create a new indexed color space.
95+
*
96+
* @param base base colorspace, mandantory has to be != null
97+
* @param hival maximum valid index value for lookup data
98+
* @param lookupData array for lookup data, mandantory has to be != null
99+
*
100+
* @return an instance of PDIndedex initialized with the given values
101+
*
102+
* @throws IOException if the colorspace could not be created
103+
*/
104+
public static PDIndexed create(PDColorSpace base, int hival, byte[] lookupData)
105+
throws IOException
106+
{
107+
if (base == null && lookupData == null)
108+
{
109+
throw new IllegalArgumentException("base value is null");
110+
}
111+
if (base == null && lookupData == null)
112+
{
113+
throw new IllegalArgumentException("lookupData value is null");
114+
}
115+
PDIndexed pdIndexed = new PDIndexed();
116+
pdIndexed.array = new COSArray();
117+
pdIndexed.array.add(COSName.INDEXED);
118+
pdIndexed.baseColorSpace = base;
119+
pdIndexed.array.add(1, base.getCOSObject());
120+
pdIndexed.array.add(2, COSInteger.get(hival));
121+
pdIndexed.lookupData = Arrays.copyOf(lookupData, lookupData.length);
122+
COSString cosLookupData = new COSString(lookupData, true);
123+
pdIndexed.array.add(3, cosLookupData);
124+
pdIndexed.readColorTable();
125+
pdIndexed.initRgbColorTable();
126+
return pdIndexed;
127+
}
128+
84129
@Override
85130
public String getName()
86131
{
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2015 The Apache Software Foundation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.pdfbox.pdmodel.graphics.color;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
20+
import static org.junit.jupiter.api.Assertions.fail;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
25+
import org.apache.pdfbox.cos.COSArray;
26+
import org.apache.pdfbox.cos.COSName;
27+
import org.apache.pdfbox.cos.COSNumber;
28+
import org.apache.pdfbox.cos.COSString;
29+
import org.apache.pdfbox.pdfwriter.compress.CompressParameters;
30+
import org.apache.pdfbox.pdmodel.PDDocument;
31+
import org.apache.pdfbox.pdmodel.PDPage;
32+
import org.apache.pdfbox.pdmodel.PDResources;
33+
import org.junit.jupiter.api.Test;
34+
35+
class PDIndexedTest
36+
{
37+
38+
/**
39+
* Test of factory method for PDFBOX-6192.
40+
*/
41+
@Test
42+
void testFactory()
43+
{
44+
final PDColorSpace baseColorspace = PDDeviceRGB.INSTANCE;
45+
// define 6 color values
46+
final int hival = 5;
47+
// create s string containing 6 RGB values. Spaces are added for a better readability
48+
final String stringLookupData = "AA1166 112233 000000 FEDC01 4561FE DC34DA" //
49+
.replace(" ", "");
50+
// expected written string for COSArray
51+
final String outputString = "/Indexed /DeviceRGB 5 <" + stringLookupData + ">";
52+
53+
try
54+
{
55+
byte[] lookupData = COSString.parseHex(stringLookupData).getBytes();
56+
PDIndexed pdIndexed = PDIndexed.create(baseColorspace, hival, lookupData);
57+
COSArray indexedCOSArray = ((COSArray) pdIndexed.getCOSObject());
58+
assertEquals(hival, ((COSNumber) indexedCOSArray.getObject(2)).intValue(),
59+
"unexpected value for hival");
60+
assertEquals(COSName.INDEXED.getName(), pdIndexed.getName(),
61+
"unexpected value for name");
62+
assertEquals(baseColorspace, pdIndexed.getBaseColorSpace(),
63+
"unexpected value for base colorspace");
64+
String lookupDataString = ((COSString) indexedCOSArray.getObject(3)).toHexString();
65+
assertEquals(stringLookupData, lookupDataString, "unexpected value for lookup data");
66+
67+
PDDocument document = new PDDocument();
68+
PDPage page = new PDPage();
69+
PDResources resources = new PDResources();
70+
resources.add(pdIndexed);
71+
page.setResources(resources);
72+
document.addPage(page);
73+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
74+
document.save(baos, CompressParameters.NO_COMPRESSION);
75+
document.close();
76+
String pdfAsString = baos.toString();
77+
assertTrue(pdfAsString.contains(outputString), "output doesn't match expected string");
78+
}
79+
catch (IOException e)
80+
{
81+
fail("Unexpected exception");
82+
}
83+
}
84+
85+
}

0 commit comments

Comments
 (0)