|
| 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