|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * gomoob/php-metadata-extractor |
| 5 | +* |
| 6 | +* @copyright Copyright (c) 2016, GOMOOB SARL (http://gomoob.com) |
| 7 | +* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE.md file) |
| 8 | +*/ |
| 9 | +namespace Gomoob\MetadataExtractor\Imaging\Png; |
| 10 | + |
| 11 | +use Gomoob\Java\Lang\IllegalArgumentException; |
| 12 | + |
| 13 | +/** |
| 14 | + * @author Baptiste GAILLARD ([email protected]) |
| 15 | + */ |
| 16 | +class PngChunkType |
| 17 | +{ |
| 18 | + private $identifiersAllowingMultiples = ['IDAT', 'sPLT', 'iTXt', 'tEXt', 'zTXt']; |
| 19 | + |
| 20 | + // |
| 21 | + // Standard critical chunks |
| 22 | + // |
| 23 | + /** |
| 24 | + * Denotes a critical {@link PngChunk} that contains basic information about the PNG image. |
| 25 | + * This must be the first chunk in the data sequence, and may only occur once. |
| 26 | + * |
| 27 | + * The format is: |
| 28 | + * * <b>pixel width</b> 4 bytes, unsigned and greater than zero |
| 29 | + * * <b>pixel height</b> 4 bytes, unsigned and greater than zero> |
| 30 | + * * <b>bit depth</b> 1 byte, number of bits per sample or per palette index (not per pixel) |
| 31 | + * * <b>color type</b> 1 byte, maps to {@link PngColorType} enum |
| 32 | + * * <b>compression method</b> 1 byte, currently only a value of zero (deflate/inflate) is in the standard |
| 33 | + * * <b>filter method</b> 1 byte, currently only a value of zero (adaptive filtering with five basic filter types) |
| 34 | + * is in the standard</li> |
| 35 | + * * <b>interlace method</b> 1 byte, indicates the transmission order of image data, currently only 0 (no interlace) |
| 36 | + * and 1 (Adam7 interlace) are in the standard</li> |
| 37 | + */ |
| 38 | + public static $IHDR = null; |
| 39 | + |
| 40 | + /** |
| 41 | + * Denotes a critical {@link PngChunk} that contains palette entries. |
| 42 | + * This chunk should only appear for a {@link PngColorType} of <code>IndexedColor</code>, |
| 43 | + * and may only occur once in the PNG data sequence. |
| 44 | + * <p> |
| 45 | + * The chunk contains between one and 256 entries, each of three bytes: |
| 46 | + * <ul> |
| 47 | + * <li><b>red</b> 1 byte</li> |
| 48 | + * <li><b>green</b> 1 byte</li> |
| 49 | + * <li><b>blue</b> 1 byte</li> |
| 50 | + * </ul> |
| 51 | + * The number of entries is determined by the chunk length. A chunk length indivisible by three is an error. |
| 52 | + */ |
| 53 | + public static $PLTE = null; |
| 54 | + public static $IDAT = null; |
| 55 | + public static $IEND = null; |
| 56 | + |
| 57 | + // |
| 58 | + // Standard ancillary chunks |
| 59 | + // |
| 60 | + public static $cHRM = null; |
| 61 | + public static $gAMA = null; |
| 62 | + public static $iCCP = null; |
| 63 | + public static $sBIT = null; |
| 64 | + public static $sRGB = null; |
| 65 | + public static $bKGD = null; |
| 66 | + public static $hIST = null; |
| 67 | + public static $tRNS = null; |
| 68 | + public static $pHYs = null; |
| 69 | + public static $sPLT = null; |
| 70 | + public static $tIME = null; |
| 71 | + public static $iTXt = null; |
| 72 | + |
| 73 | + /** |
| 74 | + * Denotes an ancillary {@link PngChunk} that contains textual data, having first a keyword and then a value. |
| 75 | + * If multiple text data keywords are needed, then multiple chunks are included in the PNG data stream. |
| 76 | + * <p> |
| 77 | + * The format is: |
| 78 | + * <ul> |
| 79 | + * <li><b>keyword</b> 1-79 bytes</li> |
| 80 | + * <li><b>null separator</b> 1 byte (\0)</li> |
| 81 | + * <li><b>text string</b> 0 or more bytes</li> |
| 82 | + * </ul> |
| 83 | + * Text is interpreted according to the Latin-1 character set [ISO-8859-1]. |
| 84 | + * Newlines should be represented by a single linefeed character (0x9). |
| 85 | + */ |
| 86 | + public static $tEXt = null; |
| 87 | + public static $zTXt = null; |
| 88 | + |
| 89 | + private $bytes; |
| 90 | + private $multipleAllowed; |
| 91 | + |
| 92 | + public function __construct($identifierOrBytes, $multipleAllowed = null) |
| 93 | + { |
| 94 | + if (is_string($identifierOrBytes)) { |
| 95 | + $this->multipleAllowed = $multipleAllowed; |
| 96 | + try { |
| 97 | + $splitted = str_split($identifierOrBytes); |
| 98 | + $bytes = []; |
| 99 | + |
| 100 | + foreach ($splitted as $s) { |
| 101 | + $bytes[] = ord($s[0]); |
| 102 | + } |
| 103 | + $this->validateBytes($bytes); |
| 104 | + $this->bytes = $bytes; |
| 105 | + } catch (UnsupportedEncodingException $e) { |
| 106 | + throw new IllegalArgumentException("Unable to convert string code to bytes."); |
| 107 | + } |
| 108 | + } elseif (is_array($identifierOrBytes)) { |
| 109 | + $this->validateBytes($bytes); |
| 110 | + $this->bytes = $bytes; |
| 111 | + $this->multipleAllowed = in_array($this->getIdentifier(), $this->identifiersAllowingMultiples); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private static function validateBytes($bytes) |
| 116 | + { |
| 117 | + if (count($bytes) != 4) { |
| 118 | + throw new IllegalArgumentException("PNG chunk type identifier must be four bytes in length"); |
| 119 | + } |
| 120 | + |
| 121 | + foreach ($bytes as $b) { |
| 122 | + if (!static::isValidByte($b)) { |
| 123 | + throw new IllegalArgumentException("PNG chunk type identifier may only contain alphabet characters"); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public function isCritical() |
| 129 | + { |
| 130 | + return $this->isUpperCase($this->bytes[0]); |
| 131 | + } |
| 132 | + |
| 133 | + public function isAncillary() |
| 134 | + { |
| 135 | + return !$this->isCritical(); |
| 136 | + } |
| 137 | + |
| 138 | + public function isPrivate() |
| 139 | + { |
| 140 | + return $this->isUpperCase($this->bytes[1]); |
| 141 | + } |
| 142 | + |
| 143 | + public function isSafeToCopy() |
| 144 | + { |
| 145 | + return $this->isLowerCase($this->bytes[3]); |
| 146 | + } |
| 147 | + |
| 148 | + public function areMultipleAllowed() |
| 149 | + { |
| 150 | + return $this->multipleAllowed; |
| 151 | + } |
| 152 | + |
| 153 | + private static function isLowerCase($b) |
| 154 | + { |
| 155 | + return ($b & (1 << 5)) != 0; |
| 156 | + } |
| 157 | + |
| 158 | + private static function isUpperCase($b) |
| 159 | + { |
| 160 | + return ($b & (1 << 5)) == 0; |
| 161 | + } |
| 162 | + |
| 163 | + private static function isValidByte($b) |
| 164 | + { |
| 165 | + return ($b >= 65 && $b <= 90) || ($b >= 97 && $b <= 122); |
| 166 | + } |
| 167 | + |
| 168 | + public function getIdentifier() |
| 169 | + { |
| 170 | + try { |
| 171 | + $identifier = ''; |
| 172 | + foreach ($this->bytes as $b) { |
| 173 | + $identifier .= chr($b); |
| 174 | + } |
| 175 | + |
| 176 | + return $identifier; |
| 177 | + } catch (UnsupportedEncodingException $e) { |
| 178 | + // The constructor should ensure that we're always able to encode the bytes in ASCII. |
| 179 | + // noinspection ConstantConditions |
| 180 | + assert(false); |
| 181 | + return "Invalid object instance"; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + public function toString() |
| 186 | + { |
| 187 | + return $this->getIdentifier(); |
| 188 | + } |
| 189 | + |
| 190 | + public function equals($o) |
| 191 | + { |
| 192 | + if ($this === $o) { |
| 193 | + return true; |
| 194 | + } |
| 195 | + |
| 196 | + if ($o === null || $this->getClass() !== $o->getClass()) { |
| 197 | + return false; |
| 198 | + } |
| 199 | + |
| 200 | + $that = $o; |
| 201 | + |
| 202 | + return Arrays.equals($this->bytes, $that->bytes); |
| 203 | + } |
| 204 | + |
| 205 | + public function hashCode() |
| 206 | + { |
| 207 | + return Arrays.hashCode($this->bytes); |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +/* FIXME: Will be fixed when static initializers will be added in PHP |
| 212 | + * |
| 213 | + * ---------------------------------------------------------------------- |
| 214 | + * FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE |
| 215 | + * ---------------------------------------------------------------------- |
| 216 | + * 1 | WARNING | A file should declare new symbols (classes, functions, |
| 217 | + * | | constants, etc.) and cause no other side effects, or |
| 218 | + * | | it should execute logic with side effects, but should |
| 219 | + * | | not do both. The first symbol is defined on line 16 |
| 220 | + * | | and the first side effect is on line 212. |
| 221 | + * ---------------------------------------------------------------------- |
| 222 | +PngChunkType::$IHDR = new PngChunkType('IHDR'); |
| 223 | +PngChunkType::$PLTE = new PngChunkType("PLTE"); |
| 224 | +PngChunkType::$IDAT = new PngChunkType("IDAT", true); |
| 225 | +PngChunkType::$IEND = new PngChunkType("IEND"); |
| 226 | +PngChunkType::$cHRM = new PngChunkType("cHRM"); |
| 227 | +PngChunkType::$gAMA = new PngChunkType("gAMA"); |
| 228 | +PngChunkType::$iCCP = new PngChunkType("iCCP"); |
| 229 | +PngChunkType::$sBIT = new PngChunkType("sBIT"); |
| 230 | +PngChunkType::$sRGB = new PngChunkType("sRGB"); |
| 231 | +PngChunkType::$bKGD = new PngChunkType("bKGD"); |
| 232 | +PngChunkType::$hIST = new PngChunkType("hIST"); |
| 233 | +PngChunkType::$tRNS = new PngChunkType("tRNS"); |
| 234 | +PngChunkType::$pHYs = new PngChunkType("pHYs"); |
| 235 | +PngChunkType::$sPLT = new PngChunkType("sPLT", true); |
| 236 | +PngChunkType::$tIME = new PngChunkType("tIME"); |
| 237 | +PngChunkType::$iTXt = new PngChunkType("iTXt", true); |
| 238 | +PngChunkType::$tEXt = new PngChunkType("tEXt", true); |
| 239 | +PngChunkType::$zTXt = new PngChunkType("zTXt", true); |
| 240 | +*/ |
0 commit comments