You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
11 KiB

11 years ago
9 years ago
11 years ago
10 years ago
11 years ago
9 years ago
11 years ago
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Pdf
  17. * @subpackage FileParser
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** @see Zend_Pdf_FileParser_Image */
  23. // require_once 'Zend/Pdf/FileParser/Image.php';
  24. /**
  25. * Abstract base class for Image file parsers.
  26. *
  27. * @package Zend_Pdf
  28. * @subpackage FileParser
  29. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Pdf_FileParser_Image_Png extends Zend_Pdf_FileParser_Image
  33. {
  34. protected $_isPNG;
  35. protected $_width;
  36. protected $_height;
  37. protected $_bits;
  38. protected $_color;
  39. protected $_compression;
  40. protected $_preFilter;
  41. protected $_interlacing;
  42. protected $_imageData;
  43. protected $_paletteData;
  44. protected $_transparencyData;
  45. /**** Public Interface ****/
  46. public function getWidth() {
  47. if(!$this->_isParsed) {
  48. $this->parse();
  49. }
  50. return $this->_width;
  51. }
  52. public function getHeight() {
  53. if(!$this->_isParsed) {
  54. $this->parse();
  55. }
  56. return $this->_width;
  57. }
  58. public function getBitDepth() {
  59. if(!$this->_isParsed) {
  60. $this->parse();
  61. }
  62. return $this->_bits;
  63. }
  64. public function getColorSpace() {
  65. if(!$this->_isParsed) {
  66. $this->parse();
  67. }
  68. return $this->_color;
  69. }
  70. public function getCompressionStrategy() {
  71. if(!$this->_isParsed) {
  72. $this->parse();
  73. }
  74. return $this->_compression;
  75. }
  76. public function getPaethFilter() {
  77. if(!$this->_isParsed) {
  78. $this->parse();
  79. }
  80. return $this->_preFilter;
  81. }
  82. public function getInterlacingMode() {
  83. if(!$this->_isParsed) {
  84. $this->parse();
  85. }
  86. return $this->_interlacing;
  87. }
  88. public function getRawImageData() {
  89. if(!$this->_isParsed) {
  90. $this->parse();
  91. }
  92. return $this->_imageData;
  93. }
  94. public function getRawPaletteData() {
  95. if(!$this->_isParsed) {
  96. $this->parse();
  97. }
  98. return $this->_paletteData;
  99. }
  100. public function getRawTransparencyData() {
  101. if(!$this->_isParsed) {
  102. $this->parse();
  103. }
  104. return $this->_transparencyData;
  105. }
  106. /* Semi-Concrete Class Implementation */
  107. /**
  108. * Verifies that the image file is in the expected format.
  109. *
  110. * @throws Zend_Pdf_Exception
  111. */
  112. public function screen()
  113. {
  114. if ($this->_isScreened) {
  115. return;
  116. }
  117. return $this->_checkSignature();
  118. }
  119. /**
  120. * Reads and parses the image data from the file on disk.
  121. *
  122. * @throws Zend_Pdf_Exception
  123. */
  124. public function parse()
  125. {
  126. if ($this->_isParsed) {
  127. return;
  128. }
  129. /* Screen the font file first, if it hasn't been done yet.
  130. */
  131. $this->screen();
  132. $this->_parseIHDRChunk();
  133. $this->_parseChunks();
  134. }
  135. protected function _parseSignature() {
  136. $this->moveToOffset(1); //Skip the first byte (%)
  137. if('PNG' != $this->readBytes(3)) {
  138. $this->_isPNG = false;
  139. } else {
  140. $this->_isPNG = true;
  141. }
  142. }
  143. protected function _checkSignature() {
  144. if(!isset($this->_isPNG)) {
  145. $this->_parseSignature();
  146. }
  147. return $this->_isPNG;
  148. }
  149. protected function _parseChunks() {
  150. $this->moveToOffset(33); //Variable chunks start at the end of IHDR
  151. //Start processing chunks. If there are no more bytes to read parsing is complete.
  152. $size = $this->getSize();
  153. while($size - $this->getOffset() >= 8) {
  154. $chunkLength = $this->readUInt(4);
  155. if($chunkLength < 0 || ($chunkLength + $this->getOffset() + 4) > $size) {
  156. // require_once 'Zend/Pdf/Exception.php';
  157. throw new Zend_Pdf_Exception("PNG Corrupt: Invalid Chunk Size In File.");
  158. }
  159. $chunkType = $this->readBytes(4);
  160. $offset = $this->getOffset();
  161. //If we know how to process the chunk, do it here, else ignore the chunk and move on to the next
  162. switch($chunkType) {
  163. case 'IDAT': // This chunk may appear more than once. It contains the actual image data.
  164. $this->_parseIDATChunk($offset, $chunkLength);
  165. break;
  166. case 'PLTE': // This chunk contains the image palette.
  167. $this->_parsePLTEChunk($offset, $chunkLength);
  168. break;
  169. case 'tRNS': // This chunk contains non-alpha channel transparency data
  170. $this->_parseTRNSChunk($offset, $chunkLength);
  171. break;
  172. case 'IEND':
  173. break 2; //End the loop too
  174. //@TODO Implement the rest of the PNG chunks. (There are many not implemented here)
  175. }
  176. if($offset + $chunkLength + 4 < $size) {
  177. $this->moveToOffset($offset + $chunkLength + 4); //Skip past the data finalizer. (Don't rely on the parse to leave the offsets correct)
  178. }
  179. }
  180. if(empty($this->_imageData)) {
  181. // require_once 'Zend/Pdf/Exception.php';
  182. throw new Zend_Pdf_Exception ( "This PNG is corrupt. All png must contain IDAT chunks." );
  183. }
  184. }
  185. protected function _parseIHDRChunk() {
  186. $this->moveToOffset(12); //IHDR must always start at offset 12 and run for 17 bytes
  187. if(!$this->readBytes(4) == 'IHDR') {
  188. // require_once 'Zend/Pdf/Exception.php';
  189. throw new Zend_Pdf_Exception( "This PNG is corrupt. The first chunk in a PNG file must be IHDR." );
  190. }
  191. $this->_width = $this->readUInt(4);
  192. $this->_height = $this->readUInt(4);
  193. $this->_bits = $this->readInt(1);
  194. $this->_color = $this->readInt(1);
  195. $this->_compression = $this->readInt(1);
  196. $this->_preFilter = $this->readInt(1);
  197. $this->_interlacing = $this->readInt(1);
  198. if($this->_interlacing != Zend_Pdf_Image::PNG_INTERLACING_DISABLED) {
  199. // require_once 'Zend/Pdf/Exception.php';
  200. throw new Zend_Pdf_Exception( "Only non-interlaced images are currently supported." );
  201. }
  202. }
  203. protected function _parseIDATChunk($chunkOffset, $chunkLength) {
  204. $this->moveToOffset($chunkOffset);
  205. if(!isset($this->_imageData)) {
  206. $this->_imageData = $this->readBytes($chunkLength);
  207. } else {
  208. $this->_imageData .= $this->readBytes($chunkLength);
  209. }
  210. }
  211. protected function _parsePLTEChunk($chunkOffset, $chunkLength) {
  212. $this->moveToOffset($chunkOffset);
  213. $this->_paletteData = $this->readBytes($chunkLength);
  214. }
  215. protected function _parseTRNSChunk($chunkOffset, $chunkLength) {
  216. $this->moveToOffset($chunkOffset);
  217. //Processing of tRNS data varies dependending on the color depth
  218. switch($this->_color) {
  219. case Zend_Pdf_Image::PNG_CHANNEL_GRAY:
  220. $baseColor = $this->readInt(1);
  221. $this->_transparencyData = array($baseColor, $baseColor);
  222. break;
  223. case Zend_Pdf_Image::PNG_CHANNEL_RGB:
  224. //@TODO Fix this hack.
  225. //This parser cheats and only uses the lsb's (and only works with < 16 bit depth images)
  226. /*
  227. From the standard:
  228. For color type 2 (truecolor), the tRNS chunk contains a single RGB color value, stored in the format:
  229. Red: 2 bytes, range 0 .. (2^bitdepth)-1
  230. Green: 2 bytes, range 0 .. (2^bitdepth)-1
  231. Blue: 2 bytes, range 0 .. (2^bitdepth)-1
  232. (If the image bit depth is less than 16, the least significant bits are used and the others are 0.)
  233. Pixels of the specified color value are to be treated as transparent (equivalent to alpha value 0);
  234. all other pixels are to be treated as fully opaque (alpha value 2bitdepth-1).
  235. */
  236. $red = $this->readInt(1);
  237. $this->skipBytes(1);
  238. $green = $this->readInt(1);
  239. $this->skipBytes(1);
  240. $blue = $this->readInt(1);
  241. $this->_transparencyData = array($red, $red, $green, $green, $blue, $blue);
  242. break;
  243. case Zend_Pdf_Image::PNG_CHANNEL_INDEXED:
  244. //@TODO Fix this hack.
  245. //This parser cheats too. It only masks the first color in the palette.
  246. /*
  247. From the standard:
  248. For color type 3 (indexed color), the tRNS chunk contains a series of one-byte alpha values, corresponding to entries in the PLTE chunk:
  249. Alpha for palette index 0: 1 byte
  250. Alpha for palette index 1: 1 byte
  251. ...etc...
  252. Each entry indicates that pixels of the corresponding palette index must be treated as having the specified alpha value.
  253. Alpha values have the same interpretation as in an 8-bit full alpha channel: 0 is fully transparent, 255 is fully opaque,
  254. regardless of image bit depth. The tRNS chunk must not contain more alpha values than there are palette entries,
  255. but tRNS can contain fewer values than there are palette entries. In this case, the alpha value for all remaining palette
  256. entries is assumed to be 255. In the common case in which only palette index 0 need be made transparent, only a one-byte
  257. tRNS chunk is needed.
  258. */
  259. $tmpData = $this->readBytes($chunkLength);
  260. if(($trnsIdx = strpos($tmpData, "\0")) !== false) {
  261. $this->_transparencyData = array($trnsIdx, $trnsIdx);
  262. }
  263. break;
  264. case Zend_Pdf_Image::PNG_CHANNEL_GRAY_ALPHA:
  265. //Fall through to the next case
  266. case Zend_Pdf_Image::PNG_CHANNEL_RGB_ALPHA:
  267. // require_once 'Zend/Pdf/Exception.php';
  268. throw new Zend_Pdf_Exception( "tRNS chunk illegal for Alpha Channel Images" );
  269. break;
  270. }
  271. }
  272. }