1<?php
2
3/////////////////////////////////////////////////////////////////
4/// getID3() by James Heinrich <info@getid3.org>               //
5//  available at https://github.com/JamesHeinrich/getID3       //
6//            or https://www.getid3.org                        //
7//            or http://getid3.sourceforge.net                 //
8//  see readme.txt for more details                            //
9/////////////////////////////////////////////////////////////////
10//                                                             //
11// module.graphic.pcd.php                                      //
12// module for analyzing PhotoCD (PCD) Image files              //
13// dependencies: NONE                                          //
14//                                                            ///
15/////////////////////////////////////////////////////////////////
16
17if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
18	exit;
19}
20class getid3_pcd extends getid3_handler
21{
22	public $ExtractData = 0;
23
24	/**
25	 * @return bool
26	 */
27	public function Analyze() {
28		$info = &$this->getid3->info;
29
30		$info['fileformat']          = 'pcd';
31		$info['video']['dataformat'] = 'pcd';
32		$info['video']['lossless']   = false;
33
34
35		$this->fseek($info['avdataoffset'] + 72);
36
37		$PCDflags = $this->fread(1);
38		$PCDisVertical = ((ord($PCDflags) & 0x01) ? true : false);
39
40
41		if ($PCDisVertical) {
42			$info['video']['resolution_x'] = 3072;
43			$info['video']['resolution_y'] = 2048;
44		} else {
45			$info['video']['resolution_x'] = 2048;
46			$info['video']['resolution_y'] = 3072;
47		}
48
49
50		if ($this->ExtractData > 3) {
51
52			$this->error('Cannot extract PSD image data for detail levels above BASE (level-3) because encrypted with Kodak-proprietary compression/encryption.');
53			return false;
54
55		} elseif ($this->ExtractData > 0) {
56
57			$PCD_levels[1] = array( 192,  128, 0x02000); // BASE/16
58			$PCD_levels[2] = array( 384,  256, 0x0B800); // BASE/4
59			$PCD_levels[3] = array( 768,  512, 0x30000); // BASE
60			//$PCD_levels[4] = array(1536, 1024,    ??); // BASE*4  - encrypted with Kodak-proprietary compression/encryption
61			//$PCD_levels[5] = array(3072, 2048,    ??); // BASE*16 - encrypted with Kodak-proprietary compression/encryption
62			//$PCD_levels[6] = array(6144, 4096,    ??); // BASE*64 - encrypted with Kodak-proprietary compression/encryption; PhotoCD-Pro only
63
64			list($PCD_width, $PCD_height, $PCD_dataOffset) = $PCD_levels[3];
65
66			$this->fseek($info['avdataoffset'] + $PCD_dataOffset);
67
68			for ($y = 0; $y < $PCD_height; $y += 2) {
69				// The image-data of these subtypes start at the respective offsets of 02000h, 0b800h and 30000h.
70				// To decode the YcbYr to the more usual RGB-code, three lines of data have to be read, each
71				// consisting of ‘w’ bytes, where ‘w’ is the width of the image-subtype. The first ‘w’ bytes and
72				// the first half of the third ‘w’ bytes contain data for the first RGB-line, the second ‘w’ bytes
73				// and the second half of the third ‘w’ bytes contain data for a second RGB-line.
74
75				$PCD_data_Y1 = $this->fread($PCD_width);
76				$PCD_data_Y2 = $this->fread($PCD_width);
77				$PCD_data_Cb = $this->fread(intval(round($PCD_width / 2)));
78				$PCD_data_Cr = $this->fread(intval(round($PCD_width / 2)));
79
80				for ($x = 0; $x < $PCD_width; $x++) {
81					if ($PCDisVertical) {
82						$info['pcd']['data'][$PCD_width - $x][$y]     = $this->YCbCr2RGB(ord($PCD_data_Y1[$x]), ord($PCD_data_Cb[(int) floor($x / 2)]), ord($PCD_data_Cr[(int) floor($x / 2)]));
83						$info['pcd']['data'][$PCD_width - $x][$y + 1] = $this->YCbCr2RGB(ord($PCD_data_Y2[$x]), ord($PCD_data_Cb[(int) floor($x / 2)]), ord($PCD_data_Cr[(int) floor($x / 2)]));
84					} else {
85						$info['pcd']['data'][$y][$x]                  = $this->YCbCr2RGB(ord($PCD_data_Y1[$x]), ord($PCD_data_Cb[(int) floor($x / 2)]), ord($PCD_data_Cr[(int) floor($x / 2)]));
86						$info['pcd']['data'][$y + 1][$x]              = $this->YCbCr2RGB(ord($PCD_data_Y2[$x]), ord($PCD_data_Cb[(int) floor($x / 2)]), ord($PCD_data_Cr[(int) floor($x / 2)]));
87					}
88				}
89			}
90
91			// Example for plotting extracted data
92			//getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.ac3.php', __FILE__, true);
93			//if ($PCDisVertical) {
94			//	$BMPinfo['resolution_x'] = $PCD_height;
95			//	$BMPinfo['resolution_y'] = $PCD_width;
96			//} else {
97			//	$BMPinfo['resolution_x'] = $PCD_width;
98			//	$BMPinfo['resolution_y'] = $PCD_height;
99			//}
100			//$BMPinfo['bmp']['data'] = $info['pcd']['data'];
101			//getid3_bmp::PlotBMP($BMPinfo);
102			//exit;
103
104		}
105
106		return false;
107	}
108
109	/**
110	 * @param int $Y
111	 * @param int $Cb
112	 * @param int $Cr
113	 *
114	 * @return int
115	 */
116	public function YCbCr2RGB($Y, $Cb, $Cr) {
117		static $YCbCr_constants = array();
118		if (empty($YCbCr_constants)) {
119			$YCbCr_constants['red']['Y']    =  0.0054980 * 256;
120			$YCbCr_constants['red']['Cb']   =  0.0000000 * 256;
121			$YCbCr_constants['red']['Cr']   =  0.0051681 * 256;
122			$YCbCr_constants['green']['Y']  =  0.0054980 * 256;
123			$YCbCr_constants['green']['Cb'] = -0.0015446 * 256;
124			$YCbCr_constants['green']['Cr'] = -0.0026325 * 256;
125			$YCbCr_constants['blue']['Y']   =  0.0054980 * 256;
126			$YCbCr_constants['blue']['Cb']  =  0.0079533 * 256;
127			$YCbCr_constants['blue']['Cr']  =  0.0000000 * 256;
128		}
129
130		$RGBcolor = array('red'=>0, 'green'=>0, 'blue'=>0);
131		foreach ($RGBcolor as $rgbname => $dummy) {
132			$RGBcolor[$rgbname] = max(0,
133										min(255,
134											intval(
135												round(
136													($YCbCr_constants[$rgbname]['Y'] * $Y) +
137													($YCbCr_constants[$rgbname]['Cb'] * ($Cb - 156)) +
138													($YCbCr_constants[$rgbname]['Cr'] * ($Cr - 137))
139												)
140											)
141										)
142									);
143		}
144		return (($RGBcolor['red'] * 65536) + ($RGBcolor['green'] * 256) + $RGBcolor['blue']);
145	}
146
147}
148