* Added license information
[lhc/web/wiklou.git] / includes / Exif.php
1 <?php
2 if (defined('MEDIAWIKI')) {
3 /**
4 * @package MediaWiki
5 * @subpackage Metadata
6 *
7 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
8 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
9 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 *
26 * @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
27 * @bug 1555, 1947
28 */
29
30 /**
31 * @package MediaWiki
32 * @subpackage Metadata
33 */
34 class Exif {
35 /**#@+
36 * @var array
37 */
38
39 /**
40 * Exif tags grouped by category, the tagname itself is the key and the type
41 * is the value, in the case of more than one possible value type they are
42 * seperated by commas.
43 *
44 * @access private
45 */
46 var $mExif;
47
48 /**
49 * A one dimentional array of all Exif tags
50 */
51 var $mFlatExif;
52
53 /**
54 * A one dimentional array of all Exif tags that we'd want to save in
55 * the database or present on a page.
56 */
57 var $mValidExif;
58
59 /**#@-*/
60
61 /**
62 * Constructor
63 */
64 function Exif() {
65 /**#@+
66 * Exif tag type definition
67 */
68 define('BYTE', 1); # An 8-bit unsigned integer.
69 define('ASCII', 2); # An 8-bit byte containing one 7-bit ASCII code. The final byte is terminated with NULL.
70 define('SHORT', 3); # A 16-bit (2-byte) unsigned integer.
71 define('LONG', 4); # A 32-bit (4-byte) unsigned integer.
72 define('RATIONAL', 5); # Two LONGs. The first LONG is the numerator and the second LONG expresses the denominator
73 define('UNDEFINED', 7); # An 8-bit byte that can take any value depending on the field definition
74 define('SLONG', 9); # A 32-bit (4-byte) signed integer (2's complement notation),
75 define('SRATIONAL', 10); # Two SLONGs. The first SLONG is the numerator and the second SLONG is the denominator.
76 /**#@-*/
77
78 /**
79 * Page numbers here refer to pages in the EXIF 2.2 standard
80 *
81 * @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
82 */
83 $this->mExif = array(
84 # TIFF Rev. 6.0 Attribute Information (p22)
85 'tiff' => array(
86 # Tags relating to image structure
87 'structure' => array(
88 'ImageWidth' => SHORT.','.LONG, # Image width
89 'ImageLength' => SHORT.','.LONG, # Image height
90 'BitsPerSample' => SHORT, # Number of bits per component
91 # "When a primary image is JPEG compressed, this designation is not"
92 # "necessary and is omitted." (p23)
93 'Compression' => SHORT, # Compression scheme #p23
94 'PhotometricInterpretation' => SHORT, # Pixel composition #p23
95 'Orientation' => SHORT, # Orientation of image #p24
96 'SamplesPerPixel' => SHORT, # Number of components
97 'PlanarConfiguration' => SHORT, # Image data arrangement #p24
98 'YCbCrSubSampling' => SHORT, # Subsampling ratio of Y to C #p24
99 'YCbCrPositioning' => SHORT, # Y and C positioning #p24-25
100 'XResolution' => RATIONAL, # Image resolution in width direction
101 'YResolution' => RATIONAL, # Image resolution in height direction
102 'ResolutionUnit' => SHORT, # Unit of X and Y resolution #(p26)
103 ),
104
105 # Tags relating to recording offset
106 'offset' => array(
107 'StripOffsets' => SHORT.','.LONG, # Image data location
108 'RowsPerStrip' => SHORT.','.LONG, # Number of rows per strip
109 'StripByteCounts' => SHORT.','.LONG, # Bytes per compressed strip
110 'JPEGInterchangeFormat' => SHORT.','.LONG, # Offset to JPEG SOI
111 'JPEGInterchangeFormatLength' => SHORT.','.LONG, # Bytes of JPEG data
112 ),
113
114 # Tags relating to image data characteristics
115 'characteristics' => array(
116 'TransferFunction' => SHORT, # Transfer function
117 'WhitePoint' => RATIONAL, # White point chromaticity
118 'PrimaryChromaticities' => RATIONAL, # Chromaticities of primarities
119 'YCbCrCoefficients' => RATIONAL, # Color space transformation matrix coefficients #p27
120 'ReferenceBlackWhite' => RATIONAL # Pair of black and white reference values
121 ),
122
123 # Other tags
124 'other' => array(
125 'DateTime' => ASCII, # File change date and time
126 'ImageDescription' => ASCII, # Image title
127 'Make' => ASCII, # Image input equipment manufacturer
128 'Model' => ASCII, # Image input equipment model
129 'Software' => ASCII, # Software used
130 'Artist' => ASCII, # Person who created the image
131 'Copyright' => ASCII, # Copyright holder
132 ),
133 ),
134
135 # Exif IFD Attribute Information (p30-31)
136 'exif' => array(
137 # Tags relating to version
138 'version' => array(
139 # TODO: NOTE: Nonexistence of this field is taken to mean nonconformance
140 # to the EXIF 2.1 AND 2.2 standards
141 'ExifVersion' => UNDEFINED, # Exif version
142 'FlashpixVersion' => UNDEFINED, # Supported Flashpix version #p32
143 ),
144
145 # Tags relating to Image Data Characteristics
146 'characteristics' => array(
147 'ColorSpace' => SHORT, # Color space information #p32
148 ),
149
150 # Tags relating to image configuration
151 'configuration' => array(
152 'ComponentsConfiguration' => UNDEFINED, # Meaning of each component #p33
153 'CompressedBitsPerPixel' => RATIONAL, # Image compression mode
154 'PixelYDimension' => SHORT.','.LONG, # Valid image width
155 'PixelXDimension' => SHORT.','.LONG, # Valind image height
156 ),
157
158 # Tags relating to related user information
159 'user' => array(
160 'MakerNote' => UNDEFINED, # Manufacturer notes
161 'UserComment' => UNDEFINED, # User comments #p34
162 ),
163
164 # Tags relating to related file information
165 'related' => array(
166 'RelatedSoundFile' => ASCII, # Related audio file
167 ),
168
169 # Tags relating to date and time
170 'dateandtime' => array(
171 'DateTimeOriginal' => ASCII, # Date and time of original data generation #p36
172 'DateTimeDigitized' => ASCII, # Date and time of original data generation
173 'SubSecTime' => ASCII, # DateTime subseconds
174 'SubSecTimeOriginal' => ASCII, # DateTimeOriginal subseconds
175 'SubSecTimeDigitized' => ASCII, # DateTimeDigitized subseconds
176 ),
177
178 # Tags relating to picture-taking conditions (p31)
179 'conditions' => array(
180 'ExposureTime' => RATIONAL, # Exposure time
181 'FNumber' => RATIONAL, # F Number
182 'ExposureProgram' => SHORT, # Exposure Program #p38
183 'SpectralSensitivity' => ASCII, # Spectral sensitivity
184 'ISOSpeedRatings' => SHORT, # ISO speed rating
185 'OECF' => UNDEFINED, # Optoelectronic conversion factor
186 'ShutterSpeedValue' => SRATIONAL, # Shutter speed
187 'ApertureValue' => RATIONAL, # Aperture
188 'BrightnessValue' => SRATIONAL, # Brightness
189 'ExposureBiasValue' => SRATIONAL, # Exposure bias
190 'MaxApertureValue' => RATIONAL, # Maximum land aperture
191 'SubjectDistance' => RATIONAL, # Subject distance
192 'MeteringMode' => SHORT, # Metering mode #p40
193 'LightSource' => SHORT, # Light source #p40-41
194 'Flash' => SHORT, # Flash #p41-42
195 'FocalLength' => RATIONAL, # Lens focal length
196 'SubjectArea' => SHORT, # Subject area
197 'FlashEnergy' => RATIONAL, # Flash energy
198 'SpatialFrequencyResponse' => UNDEFINED, # Spatial frequency response
199 'FocalPlaneXResolution' => RATIONAL, # Focal plane X resolution
200 'FocalPlaneYResolution' => RATIONAL, # Focal plane Y resolution
201 'FocalPlaneResolutionUnit' => SHORT, # Focal plane resolution unit
202 'SubjectLocation' => SHORT, # Subject location
203 'ExposureIndex' => RATIONAL, # Exposure index
204 'SensingMethod' => SHORT, # Sensing method #p46
205 'FileSource' => UNDEFINED, # File source #p47
206 'SceneType' => UNDEFINED, # Scene type #p47
207 'CFAPattern' => UNDEFINED, # CFA pattern
208 'CustomRendered' => SHORT, # Custom image processing #p48
209 'ExposureMode' => SHORT, # Exposure mode #p48
210 'WhiteBalance' => SHORT, # White Balance #p49
211 'DigitalZoomRatio' => RATIONAL, # Digital zoom ration
212 'FocalLengthIn35mmFilm' => SHORT, # Focal length in 35 mm film
213 'SceneCaptureType' => SHORT, # Scene capture type #p49
214 'GainControl' => RATIONAL, # Scene control #p49-50
215 'Contrast' => SHORT, # Contrast #p50
216 'Saturation' => SHORT, # Saturation #p50
217 'Sharpness' => SHORT, # Sharpness #p50
218 'DeviceSettingDescription' => UNDEFINED, # Desice settings description
219 'SubjectDistanceRange' => SHORT, # Subject distance range #p51
220 ),
221
222 'other' => array(
223 'ImageUniqueID' => ASCII, # Unique image ID
224 ),
225 ),
226
227 # GPS Attribute Information (p52)
228 'gps' => array(
229 'GPSVersionID' => BYTE, # GPS tag version
230 'GPSLatitudeRef' => ASCII, # North or South Latitude #p52-53
231 'GPSLatitude' => RATIONAL, # Latitude
232 'GPSLongitudeRef' => ASCII, # East or West Longitude #p53
233 'GPSLongitude' => RATIONAL, # Longitude
234 'GPSAltitudeRef' => BYTE, # Altitude reference
235 'GPSAltitude' => RATIONAL, # Altitude
236 'GPSTimeStamp' => RATIONAL, # GPS time (atomic clock)
237 'GPSSatellites' => ASCII, # Satellites used for measurement
238 'GPSStatus' => ASCII, # Receiver status #p54
239 'GPSMeasureMode' => ASCII, # Measurement mode #p54-55
240 'GPSDOP' => RATIONAL, # Measurement precision
241 'GPSSpeedRef' => ASCII, # Speed unit #p55
242 'GPSSpeed' => RATIONAL, # Speed of GPS receiver
243 'GPSTrackRef' => ASCII, # Reference for direction of movement #p55
244 'GPSTrack' => RATIONAL, # Direction of movement
245 'GPSImgDirectionRef' => ASCII, # Reference for direction of image #p56
246 'GPSImgDirection' => RATIONAL, # Direction of image
247 'GPSMapDatum' => ASCII, # Geodetic survey data used
248 'GPSDestLatitudeRef' => ASCII, # Reference for latitude of destination #p56
249 'GPSDestLatitude' => RATIONAL, # Latitude destination
250 'GPSDestLongitudeRef' => ASCII, # Reference for longitude of destination #p57
251 'GPSDestLongitude' => RATIONAL, # Longitude of destination
252 'GPSDestBearingRef' => ASCII, # Reference for bearing of destination #p57
253 'GPSDestBearing' => RATIONAL, # Bearing of destination
254 'GPSDestDistanceRef' => ASCII, # Reference for distance to destination #p57-58
255 'GPSDestDistance' => RATIONAL, # Distance to destination
256 'GPSProcessingMethod' => UNDEFINED, # Name of GPS processing method
257 'GPSAreaInformation' => UNDEFINED, # Name of GPS area
258 'GPSDateStamp' => ASCII, # GPS date
259 'GPSDifferential' => SHORT, # GPS differential correction
260 ),
261 );
262
263 $this->makeFlatExifTags();
264 $this->makeValidExifTags();
265 }
266
267 /**
268 * Get the raw list of exiftags
269 *
270 * @access private
271 * @return array
272 */
273 function getExif() {
274 return $this->mExif;
275 }
276
277 /**
278 * Generate a flat list of the exif tags
279 */
280 function makeFlatExifTags() {
281 $exif = $this->getExif();
282 array_walk($exif, array(&$this, 'callback')); // note the reference
283 }
284
285 /**
286 * A callback function used by makeFlatExifTags()
287 */
288 function callback($val, $key) {
289 if (gettype($val) === 'array')
290 array_walk($val, array(&$this, 'callback'));
291 else
292 $this->mFlatExif[$key] = $val;
293 }
294
295 /**
296 * Produce a list of all Exif tags appropriate for user output
297 *
298 * Produce a list of all tags that we want to show in output, in order not to
299 * output binary gibberish such as raw thumbnails we strip all tags
300 * with the datatype of UNDEFINED.
301 *
302 * @todo We might actually want to display some of the UNDEFINED
303 * stuff, but we strip it for now.
304 */
305 function makeValidExifTags() {
306 foreach($this->mFlatExif as $key => $val) {
307 if (strpos( $val, (string)UNDEFINED ) !== false)
308 continue;
309 $this->mValidExif[] = $key;
310 }
311 }
312
313 /**
314 * The version of the output format
315 *
316 * Before the actual metadata information is saved in the database we
317 * strip some of it since we don't want to save things like thumbnails
318 * which usually accompany Exif data. This value gets saved in the
319 * database along with the actual Exif data, and if the version in the
320 * database doesn't equal the value returned by this function the Exif
321 * data is regenerated.
322 *
323 * @return int
324 */
325 function version() {
326 return 1; // We don't need no bloddy constants!
327 }
328
329 /**#@+
330 * Validates if a tag value is of the type it should be according to the Exif spec
331 *
332 * @param mixed $in The input value to check
333 * @return bool
334 */
335 function isByte( $in ) {
336 return is_numeric($in) && $in >= 0 && $in <= 255;
337 }
338
339 function isASCII( $in ) {
340 return true; // TODO: FIXME
341 }
342
343 function isShort( $in ) {
344 return is_numeric($in) && $in >= 0 && $in <= 65536;
345 }
346
347 function isLong( $in ) {
348 return is_numeric($in) && $in >= 0 && $in <= 4294967296;
349
350 }
351
352 function isRational( $in ) {
353 $in = explode( '/', $in, 2 );
354 return $this->isLong($in[0]) && $this->isLong($in[1]);
355 }
356
357 function isUndefined( $in ) {
358 return true;
359 }
360
361 function isSlong( $in ) {
362 return $this->isLong( abs( $in ) );
363 }
364
365 function isSrational( $in ) {
366 $in = explode( '/', $in, 2 );
367 return $this->isSlong($in[0]) && $this->isSlong($in[1]);
368 }
369 /**#@-*/
370
371 /**
372 * Validates if a tag has a legal value according to the Exif spec, presumes
373 * that the given tag is valid ( has been checked in advance with
374 * $this->mValidExif )
375 *
376 * @param string $tag The tag to check
377 * @param mixed $val The value of the tag
378 * @return bool
379 */
380 function validate( $tag, $val ) {
381 // Fucks up if not typecast
382 switch((string)$this->mFlatExif[$tag]) {
383 case (string)BYTE:
384 return $this->isByte( $val );
385 case (string)ASCII:
386 return $this->isASCII( $val );
387 case (string)SHORT:
388 return $this->isShort( $val );
389 case (string)LONG:
390 return $this->isLong( $val );
391 case (string)RATIONAL:
392 return $this->isRational( $val );
393 case (string)UNDEFINED:
394 return $this->isUndefined( $val );
395 case (string)SLONG:
396 return $this->isSlong( $val );
397 case (string)SRATIONAL:
398 return $this->isSrational( $val );
399 case (string)SHORT.','.LONG:
400 return $this->isShort( $val ) || $this->isLong( $val );
401 default:
402 wfDebug( "Exif: The tag \"$tag\" had an invalid value: \"$val\"\n" );
403 return false;
404 }
405 }
406
407 /**
408 * Numbers given by Exif user agents are often magical, that is they
409 * should be replaced by a detailed explanation depending on their
410 * value which most of the time are plain integers. This function
411 * formats Exif values into human readable form.
412 *
413 * @param string $tag The tag to be formatted
414 * @param mixed $val The value of the tag
415 * @return string
416 */
417 function format( $tag, $val ) {
418 global $wgLang;
419
420 switch ($tag) {
421 case 'Compression':
422 switch ($val) {
423 case 1: case 6:
424 return $this->msg( $tag, $val );
425 }
426
427 case 'PhotometricInterpretation':
428 switch ($val) {
429 case 2: case 6:
430 return $this->msg( $tag, $val );
431 }
432
433 case 'Orientation':
434 switch ($val) {
435 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
436 return $this->msg( $tag, $val );
437 }
438
439 // TODO: If this field does not exist use 1
440 case 'PlanarConfiguration':
441 switch ($val) {
442 case 1: case 2:
443 return $this->msg( $tag, $val );
444 }
445
446 // TODO: YCbCrSubSampling
447 // TODO: YCbCrPositioning
448 // TODO: If this field does not exists use 2
449 case 'ResolutionUnit': #p26
450 switch ($val) {
451 case 2: case 3:
452 return $this->msg( $tag, $val );
453 }
454
455 // TODO: YCbCrCoefficients #p27 (see annex E)
456 case 'ExifVersion': case 'FlashpixVersion':
457 return "$val"/100;
458
459 case 'ColorSpace':
460 switch ($val) {
461 case 1: case 'FFFF.H':
462 return $this->msg( $tag, $val );
463 }
464
465 case 'ComponentsConfiguration':
466 switch ($val) {
467 case 0: case 1: case 2: case 3: case 4: case 5: case 6:
468 return $this->msg( $tag, $val );
469 }
470
471 case 'DateTimeOriginal':
472 case 'DateTimeDigitized':
473 return $wgLang->timeanddate( wfTimestamp(TS_MW, $val) );
474 case 'ExposureProgram':
475 switch ($val) {
476 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
477 return $this->msg( $tag, $val );
478 }
479 case 'MeteringMode':
480 switch ($val) {
481 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 255:
482 return $this->msg( $tag, $val );
483 }
484 case 'LightSource':
485 switch ($val) {
486 case 0: case 1: case 2: case 3: case 4: case 9: case 10: case 11:
487 case 12: case 13: case 14: case 15: case 17: case 18: case 19: case 20:
488 case 21: case 22: case 23: case 24: case 255:
489 return $this->msg( $tag, $val );
490 }
491 // TODO: Flash
492 case 'SensingMethod':
493 switch ($val) {
494 case 1: case 2: case 3: case 4: case 5: case 7: case 8:
495 return $this->msg( $tag, $val );
496 }
497 case 'FileSource':
498 switch ($val) {
499 case 3:
500 return $this->msg( $tag, $val );
501 }
502 case 'SceneType':
503 switch ($val) {
504 case 1:
505 return $this->msg( $tag, $val );
506 }
507 case 'CustomRendered':
508 switch ($val) {
509 case 0: case 1:
510 return $this->msg( $tag, $val );
511 }
512 case 'ExposureMode':
513 switch ($val) {
514 case 0: case 1: case 2:
515 return $this->msg( $tag, $val );
516 }
517 case 'WhiteBalance':
518 switch ($val) {
519 case 0: case 1:
520 return $this->msg( $tag, $val );
521 }
522 case 'SceneCaptureType':
523 switch ($val) {
524 case 0: case 1: case 2: case 3:
525 return $this->msg( $tag, $val );
526 }
527 case 'GainControl':
528 switch ($val) {
529 case 0: case 1: case 2: case 3: case 4:
530 return $this->msg( $tag, $val );
531 }
532 case 'Contrast':
533 switch ($val) {
534 case 0: case 1: case 2:
535 return $this->msg( $tag, $val );
536 }
537 case 'Saturation':
538 switch ($val) {
539 case 0: case 1: case 2:
540 return $this->msg( $tag, $val );
541 }
542 case 'Sharpness':
543 switch ($val) {
544 case 0: case 1: case 2:
545 return $this->msg( $tag, $val );
546 }
547 case 'SubjectDistanceRange':
548 switch ($val) {
549 case 0: case 1: case 2: case 3:
550 return $this->msg( $tag, $val );
551 }
552 case 'GPSLatitudeRef':
553 switch ($val) {
554 case 'N': case 'S':
555 return $this->msg( $tag, $val );
556 }
557 case 'GPSLongitudeRef':
558 switch ($val) {
559 case 'E': case 'W':
560 return $this->msg( $tag, $val );
561 }
562 case 'GPSStatus':
563 switch ($val) {
564 case 'A': case 'V':
565 return $this->msg( $tag, $val );
566 }
567 case 'GPSMeasureMode':
568 switch ($val) {
569 case 2: case 3:
570 return $this->msg( $tag, $val );
571 }
572 case 'GPSSpeedRef':
573 switch ($val) {
574 case 'K': case 'M': case 'N':
575 return $this->msg( $tag, $val );
576 }
577 case 'GPSTrackRef':
578 switch ($val) {
579 case 'T': case 'M':
580 return $this->msg( $tag, $val );
581 }
582 case 'GPSImgDirectionRef':
583 switch ($val) {
584 case 'T': case 'M':
585 return $this->msg( $tag, $val );
586 }
587 case 'GPSDestLatitudeRef':
588 switch ($val) {
589 case 'N': case 'S':
590 return $this->msg( $tag, $val );
591 }
592 case 'GPSDestLongitudeRef':
593 switch ($val) {
594 case 'E': case 'W':
595 return $this->msg( $tag, $val );
596 }
597 case 'GPSDestBearingRef':
598 switch ($val) {
599 case 'T': case 'M':
600 return $this->msg( $tag, $val );
601 }
602 case 'GPSDateStamp':
603 return $wgLang->date( substr($val, 0, 4) . substr($val, 5, 2) . substr($val, 8, 2) . 000000 );
604
605 // This is not in the Exif standard, just a special
606 // case for our purposes which enables wikis to wikify
607 // the make and model to write articles about them.
608 case 'Make': case 'Model':
609 return wfMsg( strtolower( "exif-$tag-value" ), $val );
610 default:
611 return $val;
612 }
613 }
614
615 /**
616 * Conviniance function for format()
617 *
618 * @param string $tag The tag name to pass on
619 * @param string $val The value of the tag
620 * @return string A wfMsg of "exif-$tag-$val" in lower case
621 */
622 function msg( $tag, $val ) {
623 return wfMsg( strtolower("exif-$tag-$val") );
624 }
625 }
626 } // MEDIAWIKI