ab7d62aea6d0682ce54dd7605cda9882f40d296a
[lhc/web/www.git] / www / plugins-dist / medias / lib / getid3 / module.graphic.gif.php
1 <?php
2 /////////////////////////////////////////////////////////////////
3 /// getID3() by James Heinrich <info@getid3.org> //
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
6 // also https://github.com/JamesHeinrich/getID3 //
7 /////////////////////////////////////////////////////////////////
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 class getid3_gif extends getid3_handler
19 {
20
21 public function Analyze() {
22 $info = &$this->getid3->info;
23
24 $info['fileformat'] = 'gif';
25 $info['video']['dataformat'] = 'gif';
26 $info['video']['lossless'] = true;
27 $info['video']['pixel_aspect_ratio'] = (float) 1;
28
29 $this->fseek($info['avdataoffset']);
30 $GIFheader = $this->fread(13);
31 $offset = 0;
32
33 $info['gif']['header']['raw']['identifier'] = substr($GIFheader, $offset, 3);
34 $offset += 3;
35
36 $magic = 'GIF';
37 if ($info['gif']['header']['raw']['identifier'] != $magic) {
38 $this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($info['gif']['header']['raw']['identifier']).'"');
39 unset($info['fileformat']);
40 unset($info['gif']);
41 return false;
42 }
43
44 $info['gif']['header']['raw']['version'] = substr($GIFheader, $offset, 3);
45 $offset += 3;
46 $info['gif']['header']['raw']['width'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 2));
47 $offset += 2;
48 $info['gif']['header']['raw']['height'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 2));
49 $offset += 2;
50 $info['gif']['header']['raw']['flags'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 1));
51 $offset += 1;
52 $info['gif']['header']['raw']['bg_color_index'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 1));
53 $offset += 1;
54 $info['gif']['header']['raw']['aspect_ratio'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 1));
55 $offset += 1;
56
57 $info['video']['resolution_x'] = $info['gif']['header']['raw']['width'];
58 $info['video']['resolution_y'] = $info['gif']['header']['raw']['height'];
59 $info['gif']['version'] = $info['gif']['header']['raw']['version'];
60 $info['gif']['header']['flags']['global_color_table'] = (bool) ($info['gif']['header']['raw']['flags'] & 0x80);
61 if ($info['gif']['header']['raw']['flags'] & 0x80) {
62 // Number of bits per primary color available to the original image, minus 1
63 $info['gif']['header']['bits_per_pixel'] = 3 * ((($info['gif']['header']['raw']['flags'] & 0x70) >> 4) + 1);
64 } else {
65 $info['gif']['header']['bits_per_pixel'] = 0;
66 }
67 $info['gif']['header']['flags']['global_color_sorted'] = (bool) ($info['gif']['header']['raw']['flags'] & 0x40);
68 if ($info['gif']['header']['flags']['global_color_table']) {
69 // the number of bytes contained in the Global Color Table. To determine that
70 // actual size of the color table, raise 2 to [the value of the field + 1]
71 $info['gif']['header']['global_color_size'] = pow(2, ($info['gif']['header']['raw']['flags'] & 0x07) + 1);
72 $info['video']['bits_per_sample'] = ($info['gif']['header']['raw']['flags'] & 0x07) + 1;
73 } else {
74 $info['gif']['header']['global_color_size'] = 0;
75 }
76 if ($info['gif']['header']['raw']['aspect_ratio'] != 0) {
77 // Aspect Ratio = (Pixel Aspect Ratio + 15) / 64
78 $info['gif']['header']['aspect_ratio'] = ($info['gif']['header']['raw']['aspect_ratio'] + 15) / 64;
79 }
80
81 // if ($info['gif']['header']['flags']['global_color_table']) {
82 // $GIFcolorTable = $this->fread(3 * $info['gif']['header']['global_color_size']);
83 // $offset = 0;
84 // for ($i = 0; $i < $info['gif']['header']['global_color_size']; $i++) {
85 // $red = getid3_lib::LittleEndian2Int(substr($GIFcolorTable, $offset++, 1));
86 // $green = getid3_lib::LittleEndian2Int(substr($GIFcolorTable, $offset++, 1));
87 // $blue = getid3_lib::LittleEndian2Int(substr($GIFcolorTable, $offset++, 1));
88 // $info['gif']['global_color_table'][$i] = (($red << 16) | ($green << 8) | ($blue));
89 // }
90 // }
91 //
92 // // Image Descriptor
93 // while (!feof($this->getid3->fp)) {
94 // $NextBlockTest = $this->fread(1);
95 // switch ($NextBlockTest) {
96 //
97 // case ',': // ',' - Image separator character
98 //
99 // $ImageDescriptorData = $NextBlockTest.$this->fread(9);
100 // $ImageDescriptor = array();
101 // $ImageDescriptor['image_left'] = getid3_lib::LittleEndian2Int(substr($ImageDescriptorData, 1, 2));
102 // $ImageDescriptor['image_top'] = getid3_lib::LittleEndian2Int(substr($ImageDescriptorData, 3, 2));
103 // $ImageDescriptor['image_width'] = getid3_lib::LittleEndian2Int(substr($ImageDescriptorData, 5, 2));
104 // $ImageDescriptor['image_height'] = getid3_lib::LittleEndian2Int(substr($ImageDescriptorData, 7, 2));
105 // $ImageDescriptor['flags_raw'] = getid3_lib::LittleEndian2Int(substr($ImageDescriptorData, 9, 1));
106 // $ImageDescriptor['flags']['use_local_color_map'] = (bool) ($ImageDescriptor['flags_raw'] & 0x80);
107 // $ImageDescriptor['flags']['image_interlaced'] = (bool) ($ImageDescriptor['flags_raw'] & 0x40);
108 // $info['gif']['image_descriptor'][] = $ImageDescriptor;
109 //
110 // if ($ImageDescriptor['flags']['use_local_color_map']) {
111 //
112 // $this->warning('This version of getID3() cannot parse local color maps for GIFs');
113 // return true;
114 //
115 // }
116 //echo 'Start of raster data: '.$this->ftell().'<BR>';
117 // $RasterData = array();
118 // $RasterData['code_size'] = getid3_lib::LittleEndian2Int($this->fread(1));
119 // $RasterData['block_byte_count'] = getid3_lib::LittleEndian2Int($this->fread(1));
120 // $info['gif']['raster_data'][count($info['gif']['image_descriptor']) - 1] = $RasterData;
121 //
122 // $CurrentCodeSize = $RasterData['code_size'] + 1;
123 // for ($i = 0; $i < pow(2, $RasterData['code_size']); $i++) {
124 // $DefaultDataLookupTable[$i] = chr($i);
125 // }
126 // $DefaultDataLookupTable[pow(2, $RasterData['code_size']) + 0] = ''; // Clear Code
127 // $DefaultDataLookupTable[pow(2, $RasterData['code_size']) + 1] = ''; // End Of Image Code
128 //
129 //
130 // $NextValue = $this->GetLSBits($CurrentCodeSize);
131 // echo 'Clear Code: '.$NextValue.'<BR>';
132 //
133 // $NextValue = $this->GetLSBits($CurrentCodeSize);
134 // echo 'First Color: '.$NextValue.'<BR>';
135 //
136 // $Prefix = $NextValue;
137 //$i = 0;
138 // while ($i++ < 20) {
139 // $NextValue = $this->GetLSBits($CurrentCodeSize);
140 // echo $NextValue.'<BR>';
141 // }
142 //return true;
143 // break;
144 //
145 // case '!':
146 // // GIF Extension Block
147 // $ExtensionBlockData = $NextBlockTest.$this->fread(2);
148 // $ExtensionBlock = array();
149 // $ExtensionBlock['function_code'] = getid3_lib::LittleEndian2Int(substr($ExtensionBlockData, 1, 1));
150 // $ExtensionBlock['byte_length'] = getid3_lib::LittleEndian2Int(substr($ExtensionBlockData, 2, 1));
151 // $ExtensionBlock['data'] = $this->fread($ExtensionBlock['byte_length']);
152 // $info['gif']['extension_blocks'][] = $ExtensionBlock;
153 // break;
154 //
155 // case ';':
156 // $info['gif']['terminator_offset'] = $this->ftell() - 1;
157 // // GIF Terminator
158 // break;
159 //
160 // default:
161 // break;
162 //
163 //
164 // }
165 // }
166
167 return true;
168 }
169
170
171 public function GetLSBits($bits) {
172 static $bitbuffer = '';
173 while (strlen($bitbuffer) < $bits) {
174 $bitbuffer = str_pad(decbin(ord($this->fread(1))), 8, '0', STR_PAD_LEFT).$bitbuffer;
175 }
176 $value = bindec(substr($bitbuffer, 0 - $bits));
177 $bitbuffer = substr($bitbuffer, 0, 0 - $bits);
178
179 return $value;
180 }
181
182 }