File indexing completed on 2024-05-12 05:58:15

0001 <?php
0002 /////////////////////////////////////////////////////////////////
0003 /// getID3() by James Heinrich <info@getid3.org>               //
0004 //  available at http://getid3.sourceforge.net                 //
0005 //            or http://www.getid3.org                         //
0006 //          also https://github.com/JamesHeinrich/getID3       //
0007 /////////////////////////////////////////////////////////////////
0008 // See readme.txt for more details                             //
0009 /////////////////////////////////////////////////////////////////
0010 //                                                             //
0011 // module.graphic.pcd.php                                      //
0012 // module for analyzing PhotoCD (PCD) Image files              //
0013 // dependencies: NONE                                          //
0014 //                                                            ///
0015 /////////////////////////////////////////////////////////////////
0016 
0017 
0018 class getid3_pcd extends getid3_handler
0019 {
0020   public $ExtractData = 0;
0021 
0022   public function Analyze() {
0023     $info = &$this->getid3->info;
0024 
0025     $info['fileformat']          = 'pcd';
0026     $info['video']['dataformat'] = 'pcd';
0027     $info['video']['lossless']   = false;
0028 
0029 
0030     $this->fseek($info['avdataoffset'] + 72);
0031 
0032     $PCDflags = $this->fread(1);
0033     $PCDisVertical = ((ord($PCDflags) & 0x01) ? true : false);
0034 
0035 
0036     if ($PCDisVertical) {
0037       $info['video']['resolution_x'] = 3072;
0038       $info['video']['resolution_y'] = 2048;
0039     } else {
0040       $info['video']['resolution_x'] = 2048;
0041       $info['video']['resolution_y'] = 3072;
0042     }
0043 
0044 
0045     if ($this->ExtractData > 3) {
0046 
0047       $info['error'][] = 'Cannot extract PSD image data for detail levels above BASE (level-3) because encrypted with Kodak-proprietary compression/encryption.';
0048 
0049     } elseif ($this->ExtractData > 0) {
0050 
0051       $PCD_levels[1] = array( 192,  128, 0x02000); // BASE/16
0052       $PCD_levels[2] = array( 384,  256, 0x0B800); // BASE/4
0053       $PCD_levels[3] = array( 768,  512, 0x30000); // BASE
0054       //$PCD_levels[4] = array(1536, 1024,    ??); // BASE*4  - encrypted with Kodak-proprietary compression/encryption
0055       //$PCD_levels[5] = array(3072, 2048,    ??); // BASE*16 - encrypted with Kodak-proprietary compression/encryption
0056       //$PCD_levels[6] = array(6144, 4096,    ??); // BASE*64 - encrypted with Kodak-proprietary compression/encryption; PhotoCD-Pro only
0057 
0058       list($PCD_width, $PCD_height, $PCD_dataOffset) = $PCD_levels[3];
0059 
0060       $this->fseek($info['avdataoffset'] + $PCD_dataOffset);
0061 
0062       for ($y = 0; $y < $PCD_height; $y += 2) {
0063         // The image-data of these subtypes start at the respective offsets of 02000h, 0b800h and 30000h.
0064         // To decode the YcbYr to the more usual RGB-code, three lines of data have to be read, each
0065         // consisting of ‘w’ bytes, where ‘w’ is the width of the image-subtype. The first ‘w’ bytes and
0066         // the first half of the third ‘w’ bytes contain data for the first RGB-line, the second ‘w’ bytes
0067         // and the second half of the third ‘w’ bytes contain data for a second RGB-line.
0068 
0069         $PCD_data_Y1 = $this->fread($PCD_width);
0070         $PCD_data_Y2 = $this->fread($PCD_width);
0071         $PCD_data_Cb = $this->fread(intval(round($PCD_width / 2)));
0072         $PCD_data_Cr = $this->fread(intval(round($PCD_width / 2)));
0073 
0074         for ($x = 0; $x < $PCD_width; $x++) {
0075           if ($PCDisVertical) {
0076             $info['pcd']['data'][$PCD_width - $x][$y]     = $this->YCbCr2RGB(ord($PCD_data_Y1{$x}), ord($PCD_data_Cb{floor($x / 2)}), ord($PCD_data_Cr{floor($x / 2)}));
0077             $info['pcd']['data'][$PCD_width - $x][$y + 1] = $this->YCbCr2RGB(ord($PCD_data_Y2{$x}), ord($PCD_data_Cb{floor($x / 2)}), ord($PCD_data_Cr{floor($x / 2)}));
0078           } else {
0079             $info['pcd']['data'][$y][$x]                  = $this->YCbCr2RGB(ord($PCD_data_Y1{$x}), ord($PCD_data_Cb{floor($x / 2)}), ord($PCD_data_Cr{floor($x / 2)}));
0080             $info['pcd']['data'][$y + 1][$x]              = $this->YCbCr2RGB(ord($PCD_data_Y2{$x}), ord($PCD_data_Cb{floor($x / 2)}), ord($PCD_data_Cr{floor($x / 2)}));
0081           }
0082         }
0083       }
0084 
0085       // Example for plotting extracted data
0086       //getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.ac3.php', __FILE__, true);
0087       //if ($PCDisVertical) {
0088       //  $BMPinfo['resolution_x'] = $PCD_height;
0089       //  $BMPinfo['resolution_y'] = $PCD_width;
0090       //} else {
0091       //  $BMPinfo['resolution_x'] = $PCD_width;
0092       //  $BMPinfo['resolution_y'] = $PCD_height;
0093       //}
0094       //$BMPinfo['bmp']['data'] = $info['pcd']['data'];
0095       //getid3_bmp::PlotBMP($BMPinfo);
0096       //exit;
0097 
0098     }
0099 
0100   }
0101 
0102   public function YCbCr2RGB($Y, $Cb, $Cr) {
0103     static $YCbCr_constants = array();
0104     if (empty($YCbCr_constants)) {
0105       $YCbCr_constants['red']['Y']    =  0.0054980 * 256;
0106       $YCbCr_constants['red']['Cb']   =  0.0000000 * 256;
0107       $YCbCr_constants['red']['Cr']   =  0.0051681 * 256;
0108       $YCbCr_constants['green']['Y']  =  0.0054980 * 256;
0109       $YCbCr_constants['green']['Cb'] = -0.0015446 * 256;
0110       $YCbCr_constants['green']['Cr'] = -0.0026325 * 256;
0111       $YCbCr_constants['blue']['Y']   =  0.0054980 * 256;
0112       $YCbCr_constants['blue']['Cb']  =  0.0079533 * 256;
0113       $YCbCr_constants['blue']['Cr']  =  0.0000000 * 256;
0114     }
0115 
0116     $RGBcolor = array('red'=>0, 'green'=>0, 'blue'=>0);
0117     foreach ($RGBcolor as $rgbname => $dummy) {
0118       $RGBcolor[$rgbname] = max(0,
0119                     min(255,
0120                       intval(
0121                         round(
0122                           ($YCbCr_constants[$rgbname]['Y'] * $Y) +
0123                           ($YCbCr_constants[$rgbname]['Cb'] * ($Cb - 156)) +
0124                           ($YCbCr_constants[$rgbname]['Cr'] * ($Cr - 137))
0125                         )
0126                       )
0127                     )
0128                   );
0129     }
0130     return (($RGBcolor['red'] * 65536) + ($RGBcolor['green'] * 256) + $RGBcolor['blue']);
0131   }
0132 
0133 }