[SPIP] ~maj v3.2.9-->v3.2.11
[lhc/web/www.git] / www / plugins-dist / medias / lib / getid3 / module.graphic.gif.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.gif.php //
12 // module for analyzing GIF Image files //
13 // dependencies: NONE //
14 // ///
15 /////////////////////////////////////////////////////////////////
16
17 /**
18 * @link https://www.w3.org/Graphics/GIF/spec-gif89a.txt
19 * @link http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
20 */
21
22 if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
23 exit;
24 }
25
26 class getid3_gif extends getid3_handler
27 {
28 /**
29 * @return bool
30 */
31 public function Analyze() {
32 $info = &$this->getid3->info;
33
34 $info['fileformat'] = 'gif';
35 $info['video']['dataformat'] = 'gif';
36 $info['video']['lossless'] = true;
37 $info['video']['pixel_aspect_ratio'] = (float) 1;
38
39 $this->fseek($info['avdataoffset']);
40 $GIFheader = $this->fread(13);
41 $offset = 0;
42
43 $info['gif']['header']['raw']['identifier'] = substr($GIFheader, $offset, 3);
44 $offset += 3;
45
46 $magic = 'GIF';
47 if ($info['gif']['header']['raw']['identifier'] != $magic) {
48 $this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($info['gif']['header']['raw']['identifier']).'"');
49 unset($info['fileformat']);
50 unset($info['gif']);
51 return false;
52 }
53
54 //if (!$this->getid3->option_extra_info) {
55 // $this->warning('GIF Extensions and Global Color Table not returned due to !getid3->option_extra_info');
56 //}
57
58 $info['gif']['header']['raw']['version'] = substr($GIFheader, $offset, 3);
59 $offset += 3;
60 $info['gif']['header']['raw']['width'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 2));
61 $offset += 2;
62 $info['gif']['header']['raw']['height'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 2));
63 $offset += 2;
64 $info['gif']['header']['raw']['flags'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 1));
65 $offset += 1;
66 $info['gif']['header']['raw']['bg_color_index'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 1));
67 $offset += 1;
68 $info['gif']['header']['raw']['aspect_ratio'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 1));
69 $offset += 1;
70
71 $info['video']['resolution_x'] = $info['gif']['header']['raw']['width'];
72 $info['video']['resolution_y'] = $info['gif']['header']['raw']['height'];
73 $info['gif']['version'] = $info['gif']['header']['raw']['version'];
74 $info['gif']['header']['flags']['global_color_table'] = (bool) ($info['gif']['header']['raw']['flags'] & 0x80);
75 if ($info['gif']['header']['raw']['flags'] & 0x80) {
76 // Number of bits per primary color available to the original image, minus 1
77 $info['gif']['header']['bits_per_pixel'] = 3 * ((($info['gif']['header']['raw']['flags'] & 0x70) >> 4) + 1);
78 } else {
79 $info['gif']['header']['bits_per_pixel'] = 0;
80 }
81 $info['gif']['header']['flags']['global_color_sorted'] = (bool) ($info['gif']['header']['raw']['flags'] & 0x40);
82 if ($info['gif']['header']['flags']['global_color_table']) {
83 // the number of bytes contained in the Global Color Table. To determine that
84 // actual size of the color table, raise 2 to [the value of the field + 1]
85 $info['gif']['header']['global_color_size'] = pow(2, ($info['gif']['header']['raw']['flags'] & 0x07) + 1);
86 $info['video']['bits_per_sample'] = ($info['gif']['header']['raw']['flags'] & 0x07) + 1;
87 } else {
88 $info['gif']['header']['global_color_size'] = 0;
89 }
90 if ($info['gif']['header']['raw']['aspect_ratio'] != 0) {
91 // Aspect Ratio = (Pixel Aspect Ratio + 15) / 64
92 $info['gif']['header']['aspect_ratio'] = ($info['gif']['header']['raw']['aspect_ratio'] + 15) / 64;
93 }
94
95 if ($info['gif']['header']['flags']['global_color_table']) {
96 $GIFcolorTable = $this->fread(3 * $info['gif']['header']['global_color_size']);
97 if ($this->getid3->option_extra_info) {
98 $offset = 0;
99 for ($i = 0; $i < $info['gif']['header']['global_color_size']; $i++) {
100 $red = getid3_lib::LittleEndian2Int(substr($GIFcolorTable, $offset++, 1));
101 $green = getid3_lib::LittleEndian2Int(substr($GIFcolorTable, $offset++, 1));
102 $blue = getid3_lib::LittleEndian2Int(substr($GIFcolorTable, $offset++, 1));
103 $info['gif']['global_color_table'][$i] = (($red << 16) | ($green << 8) | ($blue));
104 $info['gif']['global_color_table_rgb'][$i] = sprintf('%02X%02X%02X', $red, $green, $blue);
105 }
106 }
107 }
108
109 // Image Descriptor
110 $info['gif']['animation']['animated'] = false;
111 while (!feof($this->getid3->fp)) {
112 $NextBlockTest = $this->fread(1);
113 switch ($NextBlockTest) {
114
115 /*
116 case ',': // ',' - Image separator character
117 $ImageDescriptorData = $NextBlockTest.$this->fread(9);
118 $ImageDescriptor = array();
119 $ImageDescriptor['image_left'] = getid3_lib::LittleEndian2Int(substr($ImageDescriptorData, 1, 2));
120 $ImageDescriptor['image_top'] = getid3_lib::LittleEndian2Int(substr($ImageDescriptorData, 3, 2));
121 $ImageDescriptor['image_width'] = getid3_lib::LittleEndian2Int(substr($ImageDescriptorData, 5, 2));
122 $ImageDescriptor['image_height'] = getid3_lib::LittleEndian2Int(substr($ImageDescriptorData, 7, 2));
123 $ImageDescriptor['flags_raw'] = getid3_lib::LittleEndian2Int(substr($ImageDescriptorData, 9, 1));
124 $ImageDescriptor['flags']['use_local_color_map'] = (bool) ($ImageDescriptor['flags_raw'] & 0x80);
125 $ImageDescriptor['flags']['image_interlaced'] = (bool) ($ImageDescriptor['flags_raw'] & 0x40);
126 $info['gif']['image_descriptor'][] = $ImageDescriptor;
127
128 if ($ImageDescriptor['flags']['use_local_color_map']) {
129
130 $this->warning('This version of getID3() cannot parse local color maps for GIFs');
131 return true;
132
133 }
134 $RasterData = array();
135 $RasterData['code_size'] = getid3_lib::LittleEndian2Int($this->fread(1));
136 $RasterData['block_byte_count'] = getid3_lib::LittleEndian2Int($this->fread(1));
137 $info['gif']['raster_data'][count($info['gif']['image_descriptor']) - 1] = $RasterData;
138
139 $CurrentCodeSize = $RasterData['code_size'] + 1;
140 for ($i = 0; $i < pow(2, $RasterData['code_size']); $i++) {
141 $DefaultDataLookupTable[$i] = chr($i);
142 }
143 $DefaultDataLookupTable[pow(2, $RasterData['code_size']) + 0] = ''; // Clear Code
144 $DefaultDataLookupTable[pow(2, $RasterData['code_size']) + 1] = ''; // End Of Image Code
145
146 $NextValue = $this->GetLSBits($CurrentCodeSize);
147 echo 'Clear Code: '.$NextValue.'<BR>';
148
149 $NextValue = $this->GetLSBits($CurrentCodeSize);
150 echo 'First Color: '.$NextValue.'<BR>';
151
152 $Prefix = $NextValue;
153 $i = 0;
154 while ($i++ < 20) {
155 $NextValue = $this->GetLSBits($CurrentCodeSize);
156 echo $NextValue.'<br>';
157 }
158 echo 'escaping<br>';
159 return true;
160 break;
161 */
162
163 case '!':
164 // GIF Extension Block
165 $ExtensionBlockData = $NextBlockTest.$this->fread(2);
166 $ExtensionBlock = array();
167 $ExtensionBlock['function_code'] = getid3_lib::LittleEndian2Int(substr($ExtensionBlockData, 1, 1));
168 $ExtensionBlock['byte_length'] = getid3_lib::LittleEndian2Int(substr($ExtensionBlockData, 2, 1));
169 $ExtensionBlock['data'] = (($ExtensionBlock['byte_length'] > 0) ? $this->fread($ExtensionBlock['byte_length']) : null);
170
171 if (substr($ExtensionBlock['data'], 0, 11) == 'NETSCAPE2.0') { // Netscape Application Block (NAB)
172 $ExtensionBlock['data'] .= $this->fread(4);
173 if (substr($ExtensionBlock['data'], 11, 2) == "\x03\x01") {
174 $info['gif']['animation']['animated'] = true;
175 $info['gif']['animation']['loop_count'] = getid3_lib::LittleEndian2Int(substr($ExtensionBlock['data'], 13, 2));
176 } else {
177 $this->warning('Expecting 03 01 at offset '.($this->ftell() - 4).', found "'.getid3_lib::PrintHexBytes(substr($ExtensionBlock['data'], 11, 2)).'"');
178 }
179 }
180
181 if ($this->getid3->option_extra_info) {
182 $info['gif']['extension_blocks'][] = $ExtensionBlock;
183 }
184 break;
185
186 case ';':
187 $info['gif']['terminator_offset'] = $this->ftell() - 1;
188 // GIF Terminator
189 break;
190
191 default:
192 break;
193
194
195 }
196 }
197
198 return true;
199 }
200
201 /**
202 * @param int $bits
203 *
204 * @return float|int
205 */
206 public function GetLSBits($bits) {
207 static $bitbuffer = '';
208 while (strlen($bitbuffer) < $bits) {
209 $bitbuffer = str_pad(decbin(ord($this->fread(1))), 8, '0', STR_PAD_LEFT).$bitbuffer;
210 }
211 $value = bindec(substr($bitbuffer, 0 - $bits));
212 $bitbuffer = substr($bitbuffer, 0, 0 - $bits);
213
214 return $value;
215 }
216
217 }