rlm@3: <?php
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | PHP version 5                                                        |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen                 |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | This source file is subject to version 2 of the GPL license,         |
rlm@3: // | that is bundled with this package in the file license.txt and is     |
rlm@3: // | available through the world-wide-web at the following url:           |
rlm@3: // | http://www.gnu.org/copyleft/gpl.html                                 |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org    |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | Authors: James Heinrich <infoØgetid3*org>                            |
rlm@3: // |          Allan Hansen <ahØartemis*dk>                                |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | module.graphic.png.php                                               |
rlm@3: // | Module for analyzing PNG graphic files.                              |
rlm@3: // | dependencies: zlib support in PHP (optional)                         |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: //
rlm@3: // $Id: module.graphic.png.php,v 1.4 2006/11/02 10:48:02 ah Exp $
rlm@3: 
rlm@3:         
rlm@3:         
rlm@3: class getid3_png extends getid3_handler
rlm@3: {
rlm@3: 
rlm@3:     public function Analyze() {
rlm@3: 
rlm@3:         $getid3 = $this->getid3;
rlm@3: 
rlm@3:         $getid3->info['png'] = array ();
rlm@3:         $info_png = &$getid3->info['png'];
rlm@3: 
rlm@3:         $getid3->info['fileformat']          = 'png';
rlm@3:         $getid3->info['video']['dataformat'] = 'png';
rlm@3:         $getid3->info['video']['lossless']   = false;
rlm@3: 
rlm@3:         fseek($getid3->fp, $getid3->info['avdataoffset'], SEEK_SET);
rlm@3:         $png_filedata = fread($getid3->fp, getid3::FREAD_BUFFER_SIZE);
rlm@3: 
rlm@3:         // Magic bytes  "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"
rlm@3: 
rlm@3:         $offset = 8;
rlm@3: 
rlm@3:         while (((ftell($getid3->fp) - (strlen($png_filedata) - $offset)) < $getid3->info['filesize'])) {
rlm@3:             
rlm@3:             $chunk['data_length'] = getid3_lib::BigEndian2Int(substr($png_filedata, $offset, 4));
rlm@3:             $offset += 4;
rlm@3:             while (((strlen($png_filedata) - $offset) < ($chunk['data_length'] + 4)) && (ftell($getid3->fp) < $getid3->info['filesize'])) {
rlm@3:                 $png_filedata .= fread($getid3->fp, getid3::FREAD_BUFFER_SIZE);
rlm@3:             }
rlm@3:             
rlm@3:             $chunk['type_text'] = substr($png_filedata, $offset, 4);
rlm@3:             $chunk['type_raw']  = getid3_lib::BigEndian2Int($chunk['type_text']);
rlm@3:             $offset += 4;
rlm@3:             
rlm@3:             $chunk['data'] = substr($png_filedata, $offset, $chunk['data_length']);
rlm@3:             $offset += $chunk['data_length'];
rlm@3:             
rlm@3:             $chunk['crc'] = getid3_lib::BigEndian2Int(substr($png_filedata, $offset, 4));
rlm@3:             $offset += 4;
rlm@3: 
rlm@3:             $chunk['flags']['ancilliary']   = (bool)($chunk['type_raw'] & 0x20000000);
rlm@3:             $chunk['flags']['private']      = (bool)($chunk['type_raw'] & 0x00200000);
rlm@3:             $chunk['flags']['reserved']     = (bool)($chunk['type_raw'] & 0x00002000);
rlm@3:             $chunk['flags']['safe_to_copy'] = (bool)($chunk['type_raw'] & 0x00000020);
rlm@3: 
rlm@3:             // shortcut
rlm@3:             $info_png[$chunk['type_text']] = array ();
rlm@3:             $info_png_chunk_type_text = &$info_png[$chunk['type_text']];
rlm@3: 
rlm@3:             switch ($chunk['type_text']) {
rlm@3: 
rlm@3:                 case 'IHDR': // Image Header
rlm@3:                     $info_png_chunk_type_text['header'] = $chunk;
rlm@3:                     $info_png_chunk_type_text['width']  = getid3_lib::BigEndian2Int(substr($chunk['data'],  0, 4));
rlm@3:                     $info_png_chunk_type_text['height'] = getid3_lib::BigEndian2Int(substr($chunk['data'],  4, 4));
rlm@3:                     
rlm@3:                     getid3_lib::ReadSequence('BigEndian2Int', $info_png_chunk_type_text['raw'], $chunk['data'], 8, 
rlm@3:                         array (
rlm@3:                             'bit_depth'          => 1,
rlm@3:                             'color_type'         => 1,
rlm@3:                             'compression_method' => 1,
rlm@3:                             'filter_method'      => 1,
rlm@3:                             'interlace_method'   => 1
rlm@3:                         )
rlm@3:                     );
rlm@3: 
rlm@3:                     $info_png_chunk_type_text['compression_method_text']  = getid3_png::PNGcompressionMethodLookup($info_png_chunk_type_text['raw']['compression_method']);
rlm@3:                     $info_png_chunk_type_text['color_type']['palette']    = (bool)($info_png_chunk_type_text['raw']['color_type'] & 0x01);
rlm@3:                     $info_png_chunk_type_text['color_type']['true_color'] = (bool)($info_png_chunk_type_text['raw']['color_type'] & 0x02);
rlm@3:                     $info_png_chunk_type_text['color_type']['alpha']      = (bool)($info_png_chunk_type_text['raw']['color_type'] & 0x04);
rlm@3: 
rlm@3:                     $getid3->info['video']['resolution_x'] = $info_png_chunk_type_text['width'];
rlm@3:                     $getid3->info['video']['resolution_y'] = $info_png_chunk_type_text['height'];
rlm@3: 
rlm@3:                     $getid3->info['video']['bits_per_sample'] = getid3_png::IHDRcalculateBitsPerSample($info_png_chunk_type_text['raw']['color_type'], $info_png_chunk_type_text['raw']['bit_depth']);
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'PLTE': // Palette
rlm@3:                     $info_png_chunk_type_text['header'] = $chunk;
rlm@3:                     $palette_offset = 0;
rlm@3:                     for ($i = 0; $i <= 255; $i++) {
rlm@3:                         $red   = @getid3_lib::BigEndian2Int($chunk['data']{$palette_offset++});
rlm@3:                         $green = @getid3_lib::BigEndian2Int($chunk['data']{$palette_offset++});
rlm@3:                         $blue  = @getid3_lib::BigEndian2Int($chunk['data']{$palette_offset++});
rlm@3:                         $info_png_chunk_type_text[$i] = (($red << 16) | ($green << 8) | ($blue));
rlm@3:                     }
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'tRNS': // Transparency
rlm@3:                     $info_png_chunk_type_text['header'] = $chunk;
rlm@3:                     switch ($info_png['IHDR']['raw']['color_type']) {
rlm@3:                         case 0:
rlm@3:                             $info_png_chunk_type_text['transparent_color_gray']  = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 2));
rlm@3:                             break;
rlm@3: 
rlm@3:                         case 2:
rlm@3:                             $info_png_chunk_type_text['transparent_color_red']   = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 2));
rlm@3:                             $info_png_chunk_type_text['transparent_color_green'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 2, 2));
rlm@3:                             $info_png_chunk_type_text['transparent_color_blue']  = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 2));
rlm@3:                             break;
rlm@3: 
rlm@3:                         case 3:
rlm@3:                             for ($i = 0; $i < strlen($chunk['data']); $i++) {
rlm@3:                                 $info_png_chunk_type_text['palette_opacity'][$i] = getid3_lib::BigEndian2Int($chunk['data'][$i]);
rlm@3:                             }
rlm@3:                             break;
rlm@3: 
rlm@3:                         case 4:
rlm@3:                         case 6:
rlm@3:                             throw new getid3_exception('Invalid color_type in tRNS chunk: '.$info_png['IHDR']['raw']['color_type']);
rlm@3: 
rlm@3:                         default:
rlm@3:                             $getid3->warning('Unhandled color_type in tRNS chunk: '.$info_png['IHDR']['raw']['color_type']);
rlm@3:                             break;
rlm@3:                     }
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'gAMA': // Image Gamma
rlm@3:                     $info_png_chunk_type_text['header'] = $chunk;
rlm@3:                     $info_png_chunk_type_text['gamma']  = getid3_lib::BigEndian2Int($chunk['data']) / 100000;
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'cHRM': // Primary Chromaticities
rlm@3:                     $info_png_chunk_type_text['header']  = $chunk;
rlm@3:                     $info_png_chunk_type_text['white_x'] = getid3_lib::BigEndian2Int(substr($chunk['data'],  0, 4)) / 100000;
rlm@3:                     $info_png_chunk_type_text['white_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'],  4, 4)) / 100000;
rlm@3:                     $info_png_chunk_type_text['red_y']   = getid3_lib::BigEndian2Int(substr($chunk['data'],  8, 4)) / 100000;
rlm@3:                     $info_png_chunk_type_text['red_y']   = getid3_lib::BigEndian2Int(substr($chunk['data'], 12, 4)) / 100000;
rlm@3:                     $info_png_chunk_type_text['green_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 16, 4)) / 100000;
rlm@3:                     $info_png_chunk_type_text['green_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 20, 4)) / 100000;
rlm@3:                     $info_png_chunk_type_text['blue_y']  = getid3_lib::BigEndian2Int(substr($chunk['data'], 24, 4)) / 100000;
rlm@3:                     $info_png_chunk_type_text['blue_y']  = getid3_lib::BigEndian2Int(substr($chunk['data'], 28, 4)) / 100000;
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'sRGB': // Standard RGB Color Space
rlm@3:                     $info_png_chunk_type_text['header']                 = $chunk;
rlm@3:                     $info_png_chunk_type_text['reindering_intent']      = getid3_lib::BigEndian2Int($chunk['data']);
rlm@3:                     $info_png_chunk_type_text['reindering_intent_text'] = getid3_png::PNGsRGBintentLookup($info_png_chunk_type_text['reindering_intent']);
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'iCCP': // Embedded ICC Profile
rlm@3:                     $info_png_chunk_type_text['header']                  = $chunk;
rlm@3:                     list($profilename, $compressiondata)                 = explode("\x00", $chunk['data'], 2);
rlm@3:                     $info_png_chunk_type_text['profile_name']            = $profilename;
rlm@3:                     $info_png_chunk_type_text['compression_method']      = getid3_lib::BigEndian2Int($compressiondata[0]);
rlm@3:                     $info_png_chunk_type_text['compression_profile']     = substr($compressiondata, 1);
rlm@3:                     $info_png_chunk_type_text['compression_method_text'] = getid3_png::PNGcompressionMethodLookup($info_png_chunk_type_text['compression_method']);
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'tEXt': // Textual Data
rlm@3:                     $info_png_chunk_type_text['header']  = $chunk;
rlm@3:                     list($keyword, $text)                                = explode("\x00", $chunk['data'], 2);
rlm@3:                     $info_png_chunk_type_text['keyword'] = $keyword;
rlm@3:                     $info_png_chunk_type_text['text']    = $text;
rlm@3: 
rlm@3:                     $info_png['comments'][$info_png_chunk_type_text['keyword']][] = $info_png_chunk_type_text['text'];
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'zTXt': // Compressed Textual Data
rlm@3:                     $info_png_chunk_type_text['header']                  = $chunk;
rlm@3:                     list($keyword, $otherdata)                           = explode("\x00", $chunk['data'], 2);
rlm@3:                     $info_png_chunk_type_text['keyword']                 = $keyword;
rlm@3:                     $info_png_chunk_type_text['compression_method']      = getid3_lib::BigEndian2Int(substr($otherdata, 0, 1));
rlm@3:                     $info_png_chunk_type_text['compressed_text']         = substr($otherdata, 1);
rlm@3:                     $info_png_chunk_type_text['compression_method_text'] = getid3_png::PNGcompressionMethodLookup($info_png_chunk_type_text['compression_method']);
rlm@3:                     
rlm@3:                     if ($info_png_chunk_type_text['compression_method'] != 0) {
rlm@3:                         // unknown compression method
rlm@3:                         break;
rlm@3:                     }
rlm@3:                     
rlm@3:                     if (function_exists('gzuncompress')) {
rlm@3:                         $info_png_chunk_type_text['text']  = gzuncompress($info_png_chunk_type_text['compressed_text']);
rlm@3:                     }
rlm@3:                     else {
rlm@3:                         if (!@$this->zlib_warning) {
rlm@3:                             $getid3->warning('PHP does not have --with-zlib support - cannot gzuncompress()');
rlm@3:                         }
rlm@3:                         $this->zlib_warning = true;
rlm@3:                     }
rlm@3:                     
rlm@3: 
rlm@3:                     if (isset($info_png_chunk_type_text['text'])) {
rlm@3:                         $info_png['comments'][$info_png_chunk_type_text['keyword']][] = $info_png_chunk_type_text['text'];
rlm@3:                     }
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'iTXt': // International Textual Data
rlm@3:                     $info_png_chunk_type_text['header']                  = $chunk;
rlm@3:                     list($keyword, $otherdata)                           = explode("\x00", $chunk['data'], 2);
rlm@3:                     $info_png_chunk_type_text['keyword']                 = $keyword;
rlm@3:                     $info_png_chunk_type_text['compression']             = (bool)getid3_lib::BigEndian2Int(substr($otherdata, 0, 1));
rlm@3:                     $info_png_chunk_type_text['compression_method']      = getid3_lib::BigEndian2Int($otherdata[1]);
rlm@3:                     $info_png_chunk_type_text['compression_method_text'] = getid3_png::PNGcompressionMethodLookup($info_png_chunk_type_text['compression_method']);
rlm@3:                     list($languagetag, $translatedkeyword, $text)        = explode("\x00", substr($otherdata, 2), 3);
rlm@3:                     $info_png_chunk_type_text['language_tag']            = $languagetag;
rlm@3:                     $info_png_chunk_type_text['translated_keyword']      = $translatedkeyword;
rlm@3: 
rlm@3:                     if ($info_png_chunk_type_text['compression']) {
rlm@3: 
rlm@3:                         switch ($info_png_chunk_type_text['compression_method']) {
rlm@3:                             case 0:
rlm@3:                                 if (function_exists('gzuncompress')) {
rlm@3:                                     $info_png_chunk_type_text['text'] = gzuncompress($text);
rlm@3:                                 }
rlm@3:                                 else {
rlm@3:                                     if (!@$this->zlib_warning) {
rlm@3:                                         $getid3->warning('PHP does not have --with-zlib support - cannot gzuncompress()');
rlm@3:                                     }
rlm@3:                                     $this->zlib_warning = true;
rlm@3:                                 }
rlm@3:                                 break;
rlm@3: 
rlm@3:                             default:
rlm@3:                                 // unknown compression method
rlm@3:                                 break;
rlm@3:                         }
rlm@3: 
rlm@3:                     } else {
rlm@3: 
rlm@3:                         $info_png_chunk_type_text['text']                = $text;
rlm@3: 
rlm@3:                     }
rlm@3: 
rlm@3:                     if (isset($info_png_chunk_type_text['text'])) {
rlm@3:                         $info_png['comments'][$info_png_chunk_type_text['keyword']][] = $info_png_chunk_type_text['text'];
rlm@3:                     }
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'bKGD': // Background Color
rlm@3:                     $info_png_chunk_type_text['header']                   = $chunk;
rlm@3:                     switch ($info_png['IHDR']['raw']['color_type']) {
rlm@3:                         case 0:
rlm@3:                         case 4:
rlm@3:                             $info_png_chunk_type_text['background_gray']  = getid3_lib::BigEndian2Int($chunk['data']);
rlm@3:                             break;
rlm@3: 
rlm@3:                         case 2:
rlm@3:                         case 6:
rlm@3:                             $info_png_chunk_type_text['background_red']   = getid3_lib::BigEndian2Int(substr($chunk['data'], 0 * $info_png['IHDR']['raw']['bit_depth'], $info_png['IHDR']['raw']['bit_depth']));
rlm@3:                             $info_png_chunk_type_text['background_green'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 1 * $info_png['IHDR']['raw']['bit_depth'], $info_png['IHDR']['raw']['bit_depth']));
rlm@3:                             $info_png_chunk_type_text['background_blue']  = getid3_lib::BigEndian2Int(substr($chunk['data'], 2 * $info_png['IHDR']['raw']['bit_depth'], $info_png['IHDR']['raw']['bit_depth']));
rlm@3:                             break;
rlm@3: 
rlm@3:                         case 3:
rlm@3:                             $info_png_chunk_type_text['background_index'] = getid3_lib::BigEndian2Int($chunk['data']);
rlm@3:                             break;
rlm@3: 
rlm@3:                         default:
rlm@3:                             break;
rlm@3:                     }
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'pHYs': // Physical Pixel Dimensions
rlm@3:                     $info_png_chunk_type_text['header']                 = $chunk;
rlm@3:                     $info_png_chunk_type_text['pixels_per_unit_x']      = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 4));
rlm@3:                     $info_png_chunk_type_text['pixels_per_unit_y']      = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 4));
rlm@3:                     $info_png_chunk_type_text['unit_specifier']         = getid3_lib::BigEndian2Int(substr($chunk['data'], 8, 1));
rlm@3:                     $info_png_chunk_type_text['unit']                   = getid3_png::PNGpHYsUnitLookup($info_png_chunk_type_text['unit_specifier']);
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'sBIT': // Significant Bits
rlm@3:                     $info_png_chunk_type_text['header'] = $chunk;
rlm@3:                     switch ($info_png['IHDR']['raw']['color_type']) {
rlm@3:                         case 0:
rlm@3:                             $info_png_chunk_type_text['significant_bits_gray']  = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
rlm@3:                             break;
rlm@3: 
rlm@3:                         case 2:
rlm@3:                         case 3:
rlm@3:                             $info_png_chunk_type_text['significant_bits_red']   = getid3_lib::BigEndian2Int($chunk['data'][0]);
rlm@3:                             $info_png_chunk_type_text['significant_bits_green'] = getid3_lib::BigEndian2Int($chunk['data'][1]);
rlm@3:                             $info_png_chunk_type_text['significant_bits_blue']  = getid3_lib::BigEndian2Int($chunk['data'][2]);
rlm@3:                             break;                                                                                                                     
rlm@3:                                                                                                                                                        
rlm@3:                         case 4:                                                                                                                        
rlm@3:                             $info_png_chunk_type_text['significant_bits_gray']  = getid3_lib::BigEndian2Int($chunk['data'][0]);
rlm@3:                             $info_png_chunk_type_text['significant_bits_alpha'] = getid3_lib::BigEndian2Int($chunk['data'][1]);
rlm@3:                             break;                                                                                                                     
rlm@3:                                                                                                                                                        
rlm@3:                         case 6:                                                                                                                        
rlm@3:                             $info_png_chunk_type_text['significant_bits_red']   = getid3_lib::BigEndian2Int($chunk['data'][0]);
rlm@3:                             $info_png_chunk_type_text['significant_bits_green'] = getid3_lib::BigEndian2Int($chunk['data'][1]);
rlm@3:                             $info_png_chunk_type_text['significant_bits_blue']  = getid3_lib::BigEndian2Int($chunk['data'][2]);
rlm@3:                             $info_png_chunk_type_text['significant_bits_alpha'] = getid3_lib::BigEndian2Int($chunk['data'][3]);
rlm@3:                             break;
rlm@3: 
rlm@3:                         default:
rlm@3:                             break;
rlm@3:                     }
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'sPLT': // Suggested Palette
rlm@3:                     $info_png_chunk_type_text['header'] = $chunk;
rlm@3:                     
rlm@3:                     list($palettename, $otherdata) = explode("\x00", $chunk['data'], 2);
rlm@3:                     $info_png_chunk_type_text['palette_name'] = $palettename;
rlm@3:                     
rlm@3:                     $info_png_chunk_type_text['sample_depth_bits']  = getid3_lib::BigEndian2Int($otherdata[0]);
rlm@3:                     $info_png_chunk_type_text['sample_depth_bytes'] = $info_png_chunk_type_text['sample_depth_bits'] / 8;
rlm@3:                     
rlm@3:                     $s_plt_offset = 1;
rlm@3:                     $paletteCounter = 0;
rlm@3:                     while ($s_plt_offset < strlen($otherdata)) {
rlm@3:                         
rlm@3:                         $info_png_chunk_type_text['red'][$paletteCounter] = getid3_lib::BigEndian2Int(substr($otherdata, $s_plt_offset, $info_png_chunk_type_text['sample_depth_bytes']));
rlm@3:                         $s_plt_offset += $info_png_chunk_type_text['sample_depth_bytes'];
rlm@3:                         
rlm@3:                         $info_png_chunk_type_text['green'][$paletteCounter] = getid3_lib::BigEndian2Int(substr($otherdata, $s_plt_offset, $info_png_chunk_type_text['sample_depth_bytes']));
rlm@3:                         $s_plt_offset += $info_png_chunk_type_text['sample_depth_bytes'];
rlm@3:                         
rlm@3:                         $info_png_chunk_type_text['blue'][$paletteCounter] = getid3_lib::BigEndian2Int(substr($otherdata, $s_plt_offset, $info_png_chunk_type_text['sample_depth_bytes']));
rlm@3:                         $s_plt_offset += $info_png_chunk_type_text['sample_depth_bytes'];
rlm@3:                         
rlm@3:                         $info_png_chunk_type_text['alpha'][$paletteCounter] = getid3_lib::BigEndian2Int(substr($otherdata, $s_plt_offset, $info_png_chunk_type_text['sample_depth_bytes']));
rlm@3:                         $s_plt_offset += $info_png_chunk_type_text['sample_depth_bytes'];
rlm@3:                         
rlm@3:                         $info_png_chunk_type_text['frequency'][$paletteCounter] = getid3_lib::BigEndian2Int(substr($otherdata, $s_plt_offset, 2));
rlm@3:                         $s_plt_offset += 2;
rlm@3:                         
rlm@3:                         $paletteCounter++;
rlm@3:                     }
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'hIST': // Palette Histogram
rlm@3:                     $info_png_chunk_type_text['header'] = $chunk;
rlm@3:                     $h_ist_counter = 0;
rlm@3:                     while ($h_ist_counter < strlen($chunk['data'])) {
rlm@3:                         $info_png_chunk_type_text[$h_ist_counter] = getid3_lib::BigEndian2Int(substr($chunk['data'], $h_ist_counter / 2, 2));
rlm@3:                         $h_ist_counter += 2;
rlm@3:                     }
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'tIME': // Image Last-Modification Time
rlm@3:                     $info_png_chunk_type_text['header'] = $chunk;
rlm@3:                     $info_png_chunk_type_text['year']   = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 2));
rlm@3:                     $info_png_chunk_type_text['month']  = getid3_lib::BigEndian2Int($chunk['data']{2});
rlm@3:                     $info_png_chunk_type_text['day']    = getid3_lib::BigEndian2Int($chunk['data']{3});
rlm@3:                     $info_png_chunk_type_text['hour']   = getid3_lib::BigEndian2Int($chunk['data']{4});
rlm@3:                     $info_png_chunk_type_text['minute'] = getid3_lib::BigEndian2Int($chunk['data']{5});
rlm@3:                     $info_png_chunk_type_text['second'] = getid3_lib::BigEndian2Int($chunk['data']{6});
rlm@3:                     $info_png_chunk_type_text['unix']   = gmmktime($info_png_chunk_type_text['hour'], $info_png_chunk_type_text['minute'], $info_png_chunk_type_text['second'], $info_png_chunk_type_text['month'], $info_png_chunk_type_text['day'], $info_png_chunk_type_text['year']);
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'oFFs': // Image Offset
rlm@3:                     $info_png_chunk_type_text['header']         = $chunk;
rlm@3:                     $info_png_chunk_type_text['position_x']     = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 4), false, true);
rlm@3:                     $info_png_chunk_type_text['position_y']     = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 4), false, true);
rlm@3:                     $info_png_chunk_type_text['unit_specifier'] = getid3_lib::BigEndian2Int($chunk['data'][8]);
rlm@3:                     $info_png_chunk_type_text['unit']           = getid3_png::PNGoFFsUnitLookup($info_png_chunk_type_text['unit_specifier']);
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'pCAL': // Calibration Of Pixel Values
rlm@3:                     $info_png_chunk_type_text['header']             = $chunk;
rlm@3:                     list($calibrationname, $otherdata)              = explode("\x00", $chunk['data'], 2);
rlm@3:                     $info_png_chunk_type_text['calibration_name']   = $calibrationname;
rlm@3:                     $info_png_chunk_type_text['original_zero']      = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 4), false, true);
rlm@3:                     $info_png_chunk_type_text['original_max']       = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 4), false, true);
rlm@3:                     $info_png_chunk_type_text['equation_type']      = getid3_lib::BigEndian2Int($chunk['data'][8]);
rlm@3:                     $info_png_chunk_type_text['equation_type_text'] = getid3_png::PNGpCALequationTypeLookup($info_png_chunk_type_text['equation_type']);
rlm@3:                     $info_png_chunk_type_text['parameter_count']    = getid3_lib::BigEndian2Int($chunk['data'][9]);
rlm@3:                     $info_png_chunk_type_text['parameters']         = explode("\x00", substr($chunk['data'], 10));
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'sCAL': // Physical Scale Of Image Subject
rlm@3:                     $info_png_chunk_type_text['header']         = $chunk;
rlm@3:                     $info_png_chunk_type_text['unit_specifier'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
rlm@3:                     $info_png_chunk_type_text['unit']           = getid3_png::PNGsCALUnitLookup($info_png_chunk_type_text['unit_specifier']);
rlm@3:                     list($info_png_chunk_type_text['pixel_width'], $info_png_chunk_type_text['pixel_height']) = explode("\x00", substr($chunk['data'], 1));
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'gIFg': // GIF Graphic Control Extension
rlm@3:                     $gIFg_counter = 0;
rlm@3:                     if (isset($info_png_chunk_type_text) && is_array($info_png_chunk_type_text)) {
rlm@3:                         $gIFg_counter = count($info_png_chunk_type_text);
rlm@3:                     }
rlm@3:                     $info_png_chunk_type_text[$gIFg_counter]['header']          = $chunk;
rlm@3:                     $info_png_chunk_type_text[$gIFg_counter]['disposal_method'] = getid3_lib::BigEndian2Int($chunk['data'][0]);
rlm@3:                     $info_png_chunk_type_text[$gIFg_counter]['user_input_flag'] = getid3_lib::BigEndian2Int($chunk['data'][1]);
rlm@3:                     $info_png_chunk_type_text[$gIFg_counter]['delay_time']      = getid3_lib::BigEndian2Int($chunk['data'][2]);
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'gIFx': // GIF Application Extension
rlm@3:                     $gIFx_counter = 0;
rlm@3:                     if (isset($info_png_chunk_type_text) && is_array($info_png_chunk_type_text)) {
rlm@3:                         $gIFx_counter = count($info_png_chunk_type_text);
rlm@3:                     }
rlm@3:                     $info_png_chunk_type_text[$gIFx_counter]['header']                 = $chunk;
rlm@3:                     $info_png_chunk_type_text[$gIFx_counter]['application_identifier'] = substr($chunk['data'],  0, 8);
rlm@3:                     $info_png_chunk_type_text[$gIFx_counter]['authentication_code']    = substr($chunk['data'],  8, 3);
rlm@3:                     $info_png_chunk_type_text[$gIFx_counter]['application_data']       = substr($chunk['data'], 11);
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'IDAT': // Image Data
rlm@3:                     $idat_information_field_index = 0;
rlm@3:                     if (isset($info_png['IDAT']) && is_array($info_png['IDAT'])) {
rlm@3:                         $idat_information_field_index = count($info_png['IDAT']);
rlm@3:                     }
rlm@3:                     unset($chunk['data']);
rlm@3:                     $info_png_chunk_type_text[$idat_information_field_index]['header'] = $chunk;
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 case 'IEND': // Image Trailer
rlm@3:                     $info_png_chunk_type_text['header'] = $chunk;
rlm@3:                     break;
rlm@3: 
rlm@3: 
rlm@3:                 default:
rlm@3:                     $info_png_chunk_type_text['header'] = $chunk;
rlm@3:                     $getid3->warning('Unhandled chunk type: '.$chunk['type_text']);
rlm@3:                     break;
rlm@3:             }
rlm@3:         }
rlm@3: 
rlm@3:         return true;
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function PNGsRGBintentLookup($sRGB) {
rlm@3:         
rlm@3:         static $lookup = array (
rlm@3:             0 => 'Perceptual',
rlm@3:             1 => 'Relative colorimetric',
rlm@3:             2 => 'Saturation',
rlm@3:             3 => 'Absolute colorimetric'
rlm@3:         );
rlm@3:         return (isset($lookup[$sRGB]) ? $lookup[$sRGB] : 'invalid');
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function PNGcompressionMethodLookup($compression_method) {
rlm@3:     
rlm@3:         return ($compression_method == 0 ?  'deflate/inflate' : 'invalid');
rlm@3:     }
rlm@3: 
rlm@3:     
rlm@3:     
rlm@3:     public static function PNGpHYsUnitLookup($unit_id) {
rlm@3:     
rlm@3:         static $lookup = array (
rlm@3:             0 => 'unknown',
rlm@3:             1 => 'meter'
rlm@3:         );
rlm@3:         return (isset($lookup[$unit_id]) ? $lookup[$unit_id] : 'invalid');
rlm@3:     }
rlm@3: 
rlm@3:     
rlm@3:     
rlm@3:     public static function PNGoFFsUnitLookup($unit_id) {
rlm@3:     
rlm@3:         static $lookup = array (
rlm@3:             0 => 'pixel',
rlm@3:             1 => 'micrometer'
rlm@3:         );
rlm@3:         return (isset($lookup[$unit_id]) ? $lookup[$unit_id] : 'invalid');
rlm@3:     }
rlm@3: 
rlm@3:     
rlm@3:     
rlm@3:     public static function PNGpCALequationTypeLookup($equation_type) {
rlm@3:         
rlm@3:         static $lookup = array (
rlm@3:             0 => 'Linear mapping',
rlm@3:             1 => 'Base-e exponential mapping',
rlm@3:             2 => 'Arbitrary-base exponential mapping',
rlm@3:             3 => 'Hyperbolic mapping'
rlm@3:         );
rlm@3:         return (isset($lookup[$equation_type]) ? $lookup[$equation_type] : 'invalid');
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function PNGsCALUnitLookup($unit_id) {
rlm@3: 
rlm@3:         static $lookup = array (
rlm@3:             0 => 'meter',
rlm@3:             1 => 'radian'
rlm@3:         );
rlm@3:         return (isset($lookup[$unit_id]) ? $lookup[$unit_id] : 'invalid');
rlm@3:     }
rlm@3: 
rlm@3:     
rlm@3:     
rlm@3:     public static function IHDRcalculateBitsPerSample($color_type, $bit_depth) {
rlm@3:     
rlm@3:         switch ($color_type) {
rlm@3:             case 0: // Each pixel is a grayscale sample.
rlm@3:                 return $bit_depth;
rlm@3: 
rlm@3:             case 2: // Each pixel is an R,G,B triple
rlm@3:                 return 3 * $bit_depth;
rlm@3: 
rlm@3:             case 3: // Each pixel is a palette index; a PLTE chunk must appear.
rlm@3:                 return $bit_depth;
rlm@3: 
rlm@3:             case 4: // Each pixel is a grayscale sample, followed by an alpha sample.
rlm@3:                 return 2 * $bit_depth;
rlm@3: 
rlm@3:             case 6: // Each pixel is an R,G,B triple, followed by an alpha sample.
rlm@3:                 return 4 * $bit_depth;
rlm@3:         }
rlm@3:         return false;
rlm@3:     }
rlm@3: 
rlm@3: }
rlm@3: 
rlm@3: 
rlm@3: ?>