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