|
4 | 4 |
|
5 | 5 | use Override; |
6 | 6 | use PrinsFrank\PdfParser\Document\Dictionary\DictionaryKey\DictionaryKey; |
| 7 | +use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Array\ArrayValue; |
7 | 8 | use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Integer\IntegerValue; |
8 | 9 | use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Name\CIEColorSpaceNameValue; |
9 | 10 | use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Name\DeviceColorSpaceNameValue; |
10 | 11 | use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Name\FilterNameValue; |
11 | 12 | use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Name\SpecialColorSpaceNameValue; |
12 | 13 | use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Name\SubtypeNameValue; |
| 14 | +use PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Reference\ReferenceValue; |
| 15 | +use PrinsFrank\PdfParser\Document\Image\ColorSpace\ColorSpace; |
| 16 | +use PrinsFrank\PdfParser\Document\Image\ColorSpace\LUT; |
13 | 17 | use PrinsFrank\PdfParser\Document\Image\ImageType; |
14 | 18 | use PrinsFrank\PdfParser\Document\Image\RasterizedImage; |
15 | 19 | use PrinsFrank\PdfParser\Exception\ParseFailureException; |
@@ -71,13 +75,35 @@ private function getBitsPerComponent(): ?int { |
71 | 75 | ->getValueForKey(DictionaryKey::BITS_PER_COMPONENT, IntegerValue::class)?->value; |
72 | 76 | } |
73 | 77 |
|
74 | | - private function getColorSpace(): DeviceColorSpaceNameValue|CIEColorSpaceNameValue|SpecialColorSpaceNameValue|null { |
| 78 | + private function getColorSpace(): ?ColorSpace { |
75 | 79 | if (($type = $this->getDictionary()->getTypeForKey(DictionaryKey::COLOR_SPACE)) === null) { |
76 | 80 | return null; |
77 | 81 | } |
78 | 82 |
|
79 | 83 | if ($type === DeviceColorSpaceNameValue::class || $type === CIEColorSpaceNameValue::class || $type === SpecialColorSpaceNameValue::class) { |
80 | | - return $this->getDictionary()->getValueForKey(DictionaryKey::COLOR_SPACE, $type); |
| 84 | + return new ColorSpace($this->getDictionary()->getValueForKey(DictionaryKey::COLOR_SPACE, $type) ?? throw new ParseFailureException(), null); |
| 85 | + } |
| 86 | + |
| 87 | + if ($type === ReferenceValue::class) { |
| 88 | + $colorSpaceObject = $this->getDictionary()->getObjectForReference($this->document, DictionaryKey::COLOR_SPACE) |
| 89 | + ?? throw new ParseFailureException('Unable to retrieve colorspace object'); |
| 90 | + |
| 91 | + $colorSpaceInfo = ArrayValue::fromValue($colorSpaceObject->getContent()); |
| 92 | + if (!$colorSpaceInfo instanceof ArrayValue || !array_key_exists(0, $colorSpaceInfo->value) || !is_string($colorSpaceInfo->value[0])) { |
| 93 | + throw new ParseFailureException('Expected an array for colorspace info'); |
| 94 | + } |
| 95 | + |
| 96 | + $colorSpaceName = substr($colorSpaceInfo->value[0], 1); |
| 97 | + $colorSpace = CIEColorSpaceNameValue::tryFrom($colorSpaceName) ?? DeviceColorSpaceNameValue::tryFrom($colorSpaceName) ?? SpecialColorSpaceNameValue::tryFrom($colorSpaceName) ?? throw new ParseFailureException(sprintf('Unsupported colorspace "%s"', $colorSpaceName)); |
| 98 | + if (count($colorSpaceInfo->value) !== 4 || $colorSpaceInfo->value[3] !== 'R') { |
| 99 | + throw new ParseFailureException(sprintf('Expected reference value for colorspace info, got "%s"', $colorSpaceObject->getContent())); |
| 100 | + } |
| 101 | + |
| 102 | + if (!is_int($objectNumber = $colorSpaceInfo->value[1])) { |
| 103 | + throw new ParseFailureException(sprintf('Expected an integer for object number, got "%s"', json_encode($objectNumber))); |
| 104 | + } |
| 105 | + |
| 106 | + return new ColorSpace($colorSpace, new LUT($this->document->getObject($objectNumber) ?? throw new ParseFailureException(sprintf('Unable to locate object %d', $colorSpaceInfo->value[1])))); |
81 | 107 | } |
82 | 108 |
|
83 | 109 | throw new ParseFailureException(sprintf('Unsupported colorspace format %s', $type)); |
|
0 commit comments