source-class-Com.Tecnick.Pdf.Image.Import.Png

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 
<?php
/**
 * Png.php
 *
 * @since       2011-05-23
 * @category    Library
 * @package     PdfImage
 * @author      Nicola Asuni <info@tecnick.com>
 * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 * @link        https://github.com/tecnickcom/tc-lib-pdf-image
 *
 * This file is part of tc-lib-pdf-image software library.
 */

namespace Com\Tecnick\Pdf\Image\Import;

use \Com\Tecnick\File\File;
use \Com\Tecnick\File\Byte;
use \Com\Tecnick\Pdf\Image\Exception as ImageException;

/**
 * Com\Tecnick\Pdf\Image\Import\Png
 *
 * @since       2011-05-23
 * @category    Library
 * @package     PdfImage
 * @author      Nicola Asuni <info@tecnick.com>
 * @copyright   2011-2016 Nicola Asuni - Tecnick.com LTD
 * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 * @link        https://github.com/tecnickcom/tc-lib-pdf-image
 */
class Png
{
    /**
     * Extract data from a PNG image
     *
     * @param string $data Image raw data
     *
     * @return array Image raw data array
     */
    public function getData($data)
    {
        $data['filter'] = 'FlateDecode';
        $byte = new Byte($data['raw']);

        $offset = 0;
        // check signature
        if (substr($data['raw'], $offset, 8) != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) {
            // @codeCoverageIgnoreStart
            throw new ImageException('Not a PNG image');
            // @codeCoverageIgnoreEnd
        }
        $offset += 8;
        $offset += 4;

        $data = $this->getIhdrChunk($data, $offset);

        // check compression, filter and interlacing settings
        if (($byte->getByte($offset++) != 0)
            || ($byte->getByte($offset++) != 0)
            || ($byte->getByte($offset++) != 0)
        ) {
            if (!empty($data['recoded'])) {
                // this image has been already re-encoded
                // @codeCoverageIgnoreStart
                throw new ImageException('Unsupported feature');
                // @codeCoverageIgnoreEnd
            }
            // re-encode PNG
            $data['recode'] = true;
            return $data;
        }

        if (strpos($data['colspace'], '+Alpha') !== false) {
            // alpha channel: split images (plain + alpha)
            $data['splitalpha'] = true;
            return $data;
        }

        $data['parms'] = '/DecodeParms <<'
            .' /Predictor 15'
            .' /Colors '.$data['channels']
            .' /BitsPerComponent '.$data['bits']
            .' /Columns '.$data['width']
            .' >>';

        $offset += 4;
        return $this->getChunks($data, $offset);
    }

    /**
     * Extract the IHDR chunk data
     *
     * The header chunk (IHDR) contains basic information about the image data and must appear as the first chunk,
     * and there must only be one header chunk in a PNG data stream.
     * @param string $data   Image raw data
     * @param int    $offset Current byte offset
     *
     * @return array Image raw data array
     */
    protected function getIhdrChunk($data, &$offset)
    {
        $byte = new Byte($data['raw']);
        if (substr($data['raw'], $offset, 4) != 'IHDR') {
            // @codeCoverageIgnoreStart
            throw new ImageException('Invalid PNG image');
            // @codeCoverageIgnoreEnd
        }
        $offset += 4;
        $data['width'] = $byte->getULong($offset);
        $offset += 4;
        $data['height'] = $byte->getULong($offset);
        $offset += 4;
        $data['bits'] = $byte->getByte($offset);
        $offset += 1;
        $chc = $byte->getByte($offset); // channels code
        $offset += 1;
        $data['channels'] = (($chc == 2) ? 3 : 1);
        $chcmap = array(
            0 => 'DeviceGray',
            2 => 'DeviceRGB',
            3 => 'Indexed',
            4 => 'DeviceGray+Alpha',
            6 => 'DeviceRGB+Alpha',
        );
        if (isset($chcmap[$chc])) {
            $data['colspace'] = $chcmap[$chc];
        } else {
            // @codeCoverageIgnoreStart
            throw new ImageException('Unknown color mode');
            // @codeCoverageIgnoreEnd
        }
        return $data;
    }

    /**
     * Extract chunks data from a PNG image
     *
     * @param string $data   Image raw data
     * @param int    $offset Current byte offset
     *
     * @return array Image raw data array
     */
    protected function getChunks($data, $offset)
    {
        $byte = new Byte($data['raw']);
        while (($len = $byte->getULong($offset)) >= 0) {
            $offset += 4;
            $type = substr($data['raw'], $offset, 4);
            $offset += 4;
            if ($type == 'PLTE') {
                $data = $this->getPlteChunk($data, $offset, $len);
            } elseif ($type == 'tRNS') {
                $data = $this->getTrnsChunk($data, $offset, $len);
            } elseif ($type == 'IDAT') {
                $data = $this->getIdatChunk($data, $offset, $len);
            } elseif ($type == 'iCCP') {
                $data = $this->getIccpChunk($byte, $data, $offset, $len);
            } elseif ($type == 'IEND') {
                // The image trailer chunk (IEND) must be the final chunk
                // and marks the end of the PNG file or data stream.
                break;
            } else {
                $offset += $len;
                $offset += 4;
            }
        }
        if (($data['colspace'] == 'Indexed') && (empty($data['pal']))) {
            // @codeCoverageIgnoreStart
            throw new ImageException('The color palette is missing');
            // @codeCoverageIgnoreEnd
        }
        return $data;
    }

    /**
     * Extract the PLTE chunk data
     *
     * The palette chunk (PLTE) stores the colormap data associated with the image data.
     * This chunk is presentonly if the image data uses a color palette and must appear before the image data chunk.
     *
     * @param string $data   Image raw data
     * @param int    $offset Current byte offset
     * @param int    $len    NUmber of bytes in this chunk
     *
     * @return array Image raw data array
     */
    protected function getPlteChunk($data, &$offset, $len)
    {
        $data['pal'] = substr($data['raw'], $offset, $len);
        $offset += $len;
        $offset += 4;
        return $data;
    }

    /**
     * Extract the tRNS chunk data
     *
     * @param string $data   Image raw data
     * @param int    $offset Current byte offset
     * @param int    $len    NUmber of bytes in this chunk
     *
     * @return array Image raw data array
     */
    protected function getTrnsChunk($data, &$offset, $len)
    {
        // read transparency info
        $trns = substr($data['raw'], $offset, $len);
        $offset += $len;
        if ($data['colspace'] == 'DeviceGray') {
            // DeviceGray
            $data['trns'][] = ord($trns[1]);
        } elseif ($data['colspace'] == 'DeviceRGB') {
            // DeviceRGB
            $data['trns'][] = ord($trns[1]);
            $data['trns'][] = ord($trns[3]);
            $data['trns'][] = ord($trns[5]);
        } else {
            // Indexed
            $data['trns'] = array_map('ord', str_split($trns));
        }
        $offset += 4;
        return $data;
    }

    /**
     * Extract the IDAT chunk data
     *
     * The image data chunk (IDAT) stores the actual image data,
     * and multiple image data chunks may occur in a data stream and must be stored in contiguous order.
     *
     * @param string $data   Image raw data
     * @param int    $offset Current byte offset
     * @param int    $len    NUmber of bytes in this chunk
     *
     * @return array Image raw data array
     */
    protected function getIdatChunk($data, &$offset, $len)
    {
        $data['data'] .= substr($data['raw'], $offset, $len);
        $offset += $len;
        $offset += 4;
        return $data;
    }

    /**
     * Extract the iCCP chunk data
     *
     * @param Byte   $byte   Byte class object
     * @param string $data   Image raw data
     * @param int    $offset Current byte offset
     * @param int    $len    NUmber of bytes in this chunk
     *
     * @return array Image raw data array
     */
    protected function getIccpChunk($byte, $data, &$offset, $len)
    {
        // skip profile name
        $pos = 0;
        while (($byte->getByte($offset++) != 0) && ($pos < 80)) {
            ++$pos;
        }
        // get compression method
        if ($byte->getByte($offset++) != 0) {
            // @codeCoverageIgnoreStart
            throw new ImageException('Unknown filter method');
            // @codeCoverageIgnoreEnd
        }
        // read ICC Color Profile
        $len -= ($pos + 2);
        $data['icc'] = gzuncompress(substr($data['raw'], $offset, $len));
        $offset += $len;
        $offset += 4;
        return $data;
    }
}
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy