Blame view

sources/3rdparty/getid3/module.graphic.tiff.php 8.45 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  <?php
  /////////////////////////////////////////////////////////////////
  /// getID3() by James Heinrich <info@getid3.org>               //
  //  available at http://getid3.sourceforge.net                 //
  //            or http://www.getid3.org                         //
  /////////////////////////////////////////////////////////////////
  // See readme.txt for more details                             //
  /////////////////////////////////////////////////////////////////
  //                                                             //
  // module.archive.tiff.php                                     //
  // module for analyzing TIFF files                             //
  // dependencies: NONE                                          //
  //                                                            ///
  /////////////////////////////////////////////////////////////////
  
  
  class getid3_tiff extends getid3_handler
  {
31b7f2792   Kload   Upgrade to ownclo...
19
  	public function Analyze() {
03e52840d   Kload   Init
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  		$info = &$this->getid3->info;
  
  		fseek($this->getid3->fp, $info['avdataoffset'], SEEK_SET);
  		$TIFFheader = fread($this->getid3->fp, 4);
  
  		switch (substr($TIFFheader, 0, 2)) {
  			case 'II':
  				$info['tiff']['byte_order'] = 'Intel';
  				break;
  			case 'MM':
  				$info['tiff']['byte_order'] = 'Motorola';
  				break;
  			default:
  				$info['error'][] = 'Invalid TIFF byte order identifier ('.substr($TIFFheader, 0, 2).') at offset '.$info['avdataoffset'];
  				return false;
  				break;
  		}
  
  		$info['fileformat']          = 'tiff';
  		$info['video']['dataformat'] = 'tiff';
  		$info['video']['lossless']   = true;
  		$info['tiff']['ifd']         = array();
  		$CurrentIFD                          = array();
  
  		$FieldTypeByteLength = array(1=>1, 2=>1, 3=>2, 4=>4, 5=>8);
  
  		$nextIFDoffset = $this->TIFFendian2Int(fread($this->getid3->fp, 4), $info['tiff']['byte_order']);
  
  		while ($nextIFDoffset > 0) {
  
  			$CurrentIFD['offset'] = $nextIFDoffset;
  
  			fseek($this->getid3->fp, $info['avdataoffset'] + $nextIFDoffset, SEEK_SET);
  			$CurrentIFD['fieldcount'] = $this->TIFFendian2Int(fread($this->getid3->fp, 2), $info['tiff']['byte_order']);
  
  			for ($i = 0; $i < $CurrentIFD['fieldcount']; $i++) {
  				$CurrentIFD['fields'][$i]['raw']['tag']    = $this->TIFFendian2Int(fread($this->getid3->fp, 2), $info['tiff']['byte_order']);
  				$CurrentIFD['fields'][$i]['raw']['type']   = $this->TIFFendian2Int(fread($this->getid3->fp, 2), $info['tiff']['byte_order']);
  				$CurrentIFD['fields'][$i]['raw']['length'] = $this->TIFFendian2Int(fread($this->getid3->fp, 4), $info['tiff']['byte_order']);
  				$CurrentIFD['fields'][$i]['raw']['offset'] =                       fread($this->getid3->fp, 4);
  
  				switch ($CurrentIFD['fields'][$i]['raw']['type']) {
  					case 1: // BYTE  An 8-bit unsigned integer.
  						if ($CurrentIFD['fields'][$i]['raw']['length'] <= 4) {
  							$CurrentIFD['fields'][$i]['value']  = $this->TIFFendian2Int(substr($CurrentIFD['fields'][$i]['raw']['offset'], 0, 1), $info['tiff']['byte_order']);
  						} else {
  							$CurrentIFD['fields'][$i]['offset'] = $this->TIFFendian2Int($CurrentIFD['fields'][$i]['raw']['offset'], $info['tiff']['byte_order']);
  						}
  						break;
  
  					case 2: // ASCII 8-bit bytes  that store ASCII codes; the last byte must be null.
  						if ($CurrentIFD['fields'][$i]['raw']['length'] <= 4) {
  							$CurrentIFD['fields'][$i]['value']  = substr($CurrentIFD['fields'][$i]['raw']['offset'], 3);
  						} else {
  							$CurrentIFD['fields'][$i]['offset'] = $this->TIFFendian2Int($CurrentIFD['fields'][$i]['raw']['offset'], $info['tiff']['byte_order']);
  						}
  						break;
  
  					case 3: // SHORT A 16-bit (2-byte) unsigned integer.
  						if ($CurrentIFD['fields'][$i]['raw']['length'] <= 2) {
  							$CurrentIFD['fields'][$i]['value']  = $this->TIFFendian2Int(substr($CurrentIFD['fields'][$i]['raw']['offset'], 0, 2), $info['tiff']['byte_order']);
  						} else {
  							$CurrentIFD['fields'][$i]['offset'] = $this->TIFFendian2Int($CurrentIFD['fields'][$i]['raw']['offset'], $info['tiff']['byte_order']);
  						}
  						break;
  
  					case 4: // LONG  A 32-bit (4-byte) unsigned integer.
  						if ($CurrentIFD['fields'][$i]['raw']['length'] <= 1) {
  							$CurrentIFD['fields'][$i]['value']  = $this->TIFFendian2Int($CurrentIFD['fields'][$i]['raw']['offset'], $info['tiff']['byte_order']);
  						} else {
  							$CurrentIFD['fields'][$i]['offset'] = $this->TIFFendian2Int($CurrentIFD['fields'][$i]['raw']['offset'], $info['tiff']['byte_order']);
  						}
  						break;
  
  					case 5: // RATIONAL   Two LONG_s:  the first represents the numerator of a fraction, the second the denominator.
  						break;
  				}
  			}
  
  			$info['tiff']['ifd'][] = $CurrentIFD;
  			$CurrentIFD = array();
  			$nextIFDoffset = $this->TIFFendian2Int(fread($this->getid3->fp, 4), $info['tiff']['byte_order']);
  
  		}
  
  		foreach ($info['tiff']['ifd'] as $IFDid => $IFDarray) {
  			foreach ($IFDarray['fields'] as $key => $fieldarray) {
  				switch ($fieldarray['raw']['tag']) {
  					case 256: // ImageWidth
  					case 257: // ImageLength
  					case 258: // BitsPerSample
  					case 259: // Compression
  						if (!isset($fieldarray['value'])) {
  							fseek($this->getid3->fp, $fieldarray['offset'], SEEK_SET);
  							$info['tiff']['ifd'][$IFDid]['fields'][$key]['raw']['data'] = fread($this->getid3->fp, $fieldarray['raw']['length'] * $FieldTypeByteLength[$fieldarray['raw']['type']]);
  
  						}
  						break;
  
  					case 270: // ImageDescription
  					case 271: // Make
  					case 272: // Model
  					case 305: // Software
  					case 306: // DateTime
  					case 315: // Artist
  					case 316: // HostComputer
  						if (isset($fieldarray['value'])) {
  							$info['tiff']['ifd'][$IFDid]['fields'][$key]['raw']['data'] = $fieldarray['value'];
  						} else {
  							fseek($this->getid3->fp, $fieldarray['offset'], SEEK_SET);
  							$info['tiff']['ifd'][$IFDid]['fields'][$key]['raw']['data'] = fread($this->getid3->fp, $fieldarray['raw']['length'] * $FieldTypeByteLength[$fieldarray['raw']['type']]);
  
  						}
  						break;
  				}
  				switch ($fieldarray['raw']['tag']) {
  					case 256: // ImageWidth
  						$info['video']['resolution_x'] = $fieldarray['value'];
  						break;
  
  					case 257: // ImageLength
  						$info['video']['resolution_y'] = $fieldarray['value'];
  						break;
  
  					case 258: // BitsPerSample
  						if (isset($fieldarray['value'])) {
  							$info['video']['bits_per_sample'] = $fieldarray['value'];
  						} else {
  							$info['video']['bits_per_sample'] = 0;
  							for ($i = 0; $i < $fieldarray['raw']['length']; $i++) {
  								$info['video']['bits_per_sample'] += $this->TIFFendian2Int(substr($info['tiff']['ifd'][$IFDid]['fields'][$key]['raw']['data'], $i * $FieldTypeByteLength[$fieldarray['raw']['type']], $FieldTypeByteLength[$fieldarray['raw']['type']]), $info['tiff']['byte_order']);
  							}
  						}
  						break;
  
  					case 259: // Compression
  						$info['video']['codec'] = $this->TIFFcompressionMethod($fieldarray['value']);
  						break;
  
  					case 270: // ImageDescription
  					case 271: // Make
  					case 272: // Model
  					case 305: // Software
  					case 306: // DateTime
  					case 315: // Artist
  					case 316: // HostComputer
  						$TIFFcommentName = $this->TIFFcommentName($fieldarray['raw']['tag']);
  						if (isset($info['tiff']['comments'][$TIFFcommentName])) {
  							$info['tiff']['comments'][$TIFFcommentName][] =       $info['tiff']['ifd'][$IFDid]['fields'][$key]['raw']['data'];
  						} else {
  							$info['tiff']['comments'][$TIFFcommentName]   = array($info['tiff']['ifd'][$IFDid]['fields'][$key]['raw']['data']);
  						}
  						break;
  
  					default:
  						break;
  				}
  			}
  		}
  
  		return true;
  	}
31b7f2792   Kload   Upgrade to ownclo...
182
  	public function TIFFendian2Int($bytestring, $byteorder) {
03e52840d   Kload   Init
183
184
185
186
187
188
189
  		if ($byteorder == 'Intel') {
  			return getid3_lib::LittleEndian2Int($bytestring);
  		} elseif ($byteorder == 'Motorola') {
  			return getid3_lib::BigEndian2Int($bytestring);
  		}
  		return false;
  	}
31b7f2792   Kload   Upgrade to ownclo...
190
  	public function TIFFcompressionMethod($id) {
03e52840d   Kload   Init
191
192
193
194
195
196
197
198
199
200
201
202
  		static $TIFFcompressionMethod = array();
  		if (empty($TIFFcompressionMethod)) {
  			$TIFFcompressionMethod = array(
  				1     => 'Uncompressed',
  				2     => 'Huffman',
  				3     => 'Fax - CCITT 3',
  				5     => 'LZW',
  				32773 => 'PackBits',
  			);
  		}
  		return (isset($TIFFcompressionMethod[$id]) ? $TIFFcompressionMethod[$id] : 'unknown/invalid ('.$id.')');
  	}
31b7f2792   Kload   Upgrade to ownclo...
203
  	public function TIFFcommentName($id) {
03e52840d   Kload   Init
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  		static $TIFFcommentName = array();
  		if (empty($TIFFcommentName)) {
  			$TIFFcommentName = array(
  				270 => 'imagedescription',
  				271 => 'make',
  				272 => 'model',
  				305 => 'software',
  				306 => 'datetime',
  				315 => 'artist',
  				316 => 'hostcomputer',
  			);
  		}
  		return (isset($TIFFcommentName[$id]) ? $TIFFcommentName[$id] : 'unknown/invalid ('.$id.')');
  	}
  
  }