source: extensions/AMetaData/JpegMetaData/Readers/CanonReader.class.php @ 5038

Last change on this file since 5038 was 5038, checked in by grum, 14 years ago

Manage multiple lenses for one Id for Pentax camera
Add some minors features

  • Property svn:executable set to *
File size: 22.8 KB
Line 
1<?php
2/*
3 * --:: JPEG MetaDatas ::-------------------------------------------------------
4 *
5 *  Author    : Grum
6 *   email    : grum at piwigo.org
7 *   website  : http://photos.grum.fr
8 *
9 *   << May the Little SpaceFrog be with you ! >>
10 *
11 *
12 * +-----------------------------------------------------------------------+
13 * | JpegMetaData - a PHP based Jpeg Metadata manager                      |
14 * +-----------------------------------------------------------------------+
15 * | Copyright(C) 2010  Grum - http://www.grum.fr                          |
16 * +-----------------------------------------------------------------------+
17 * | This program is free software; you can redistribute it and/or modify  |
18 * | it under the terms of the GNU General Public License as published by  |
19 * | the Free Software Foundation                                          |
20 * |                                                                       |
21 * | This program is distributed in the hope that it will be useful, but   |
22 * | WITHOUT ANY WARRANTY; without even the implied warranty of            |
23 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
24 * | General Public License for more details.                              |
25 * |                                                                       |
26 * | You should have received a copy of the GNU General Public License     |
27 * | along with this program; if not, write to the Free Software           |
28 * | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
29 * | USA.                                                                  |
30 * +-----------------------------------------------------------------------+
31 *
32 *
33 * -----------------------------------------------------------------------------
34 *
35 * The CanonReader class is the dedicated to read the specific Canon tags
36 *
37 * ====> See MakerNotesReader.class.php to know more about the structure <======
38 *
39 * -----------------------------------------------------------------------------
40 *
41 * .. Notes ..
42 *
43 *
44 * The CanonReader class is derived from the MakerNotesReader class.
45 *
46 * =====> See MakerNotesReader.class.php to know more about common methods <====
47 *
48 * -----------------------------------------------------------------------------
49 */
50
51
52
53  require_once(JPEG_METADATA_DIR."TagDefinitions/CanonTags.class.php");
54  require_once(JPEG_METADATA_DIR."Readers/MakerNotesReader.class.php");
55
56  class CanonReader extends MakerNotesReader
57  {
58    /**
59     * The constructor needs, like the ancestor, the datas to be parsed
60     *
61     * Some datas are offset on extra data, and this offset can be (some time)
62     * absolute inside the IFD, or relative. So, the offset of the IFD structure
63     * is needed
64     *
65     * The byte order can be different from the TIFF byte order !
66     *
67     * The constructor need the maker signature (see the MakerNotesSignatures
68     * class for a list of known signatures)
69     *
70     * @param String $data
71     * @param ULong $offset : offset of IFD block in the jpeg file
72     * @param String $byteOrder
73     * @param String $makerSignature :
74     */
75    function __construct($data, $offset, $byteOrder, $makerSignature)
76    {
77      /*
78       * Canon don't have signatures in his maker note, starting directly with
79       * the number of entries
80       */
81      $this->maker = MAKER_CANON;
82      $this->header = "";
83      $this->headerSize = 0;
84
85      parent::__construct($data, $offset, $byteOrder);
86    }
87
88    function __destruct()
89    {
90      parent::__destruct();
91    }
92
93
94    /**
95     * initialize the definition for Pentax exif tags
96     */
97    protected function initializeTagDef()
98    {
99      $this->tagDef = new CanonTags();
100    }
101
102    /**
103     * skip the IFD header
104     */
105    protected function skipHeader($headerSize=0)
106    {
107      parent::skipHeader($headerSize);
108    }
109
110    /**
111     * this function do the interpretation of specials tags
112     *
113     * the function return the interpreted value for the tag
114     *
115     * @param $tagId             : the id of the tag
116     * @param $values            : 'raw' value to be interpreted
117     * @param UByte $type        : if needed (for IFD structure) the type of data
118     * @param ULong $valueOffset : if needed, the offset of data in the jpeg file
119     * @return String or Array or DateTime or Integer or Float...
120     */
121    protected function processSpecialTag($tagId, $values, $type, $valuesOffset=0)
122    {
123      switch($tagId)
124      {
125        case 0x0001: // "CanonImageType"
126          $this->processSubTag0x0001($values);
127          $returned=$values;
128          break;
129        case 0x0004: // "CanonShotInfo"
130          $this->processSubTag0x0004($values);
131          $returned=$values;
132          break;
133        case 0x0006: // "CanonImageType"
134        case 0x0007: // "CanonFirmwareVersion"
135        case 0x0009: // "OwnerName"
136        case 0x0095: // "LensModel"
137        case 0x0096: // "InternalSerialNumber"
138          /*
139           * null terminated strings
140           */
141          $returned=ConvertData::toStrings($values);
142          break;
143        case 0x000c: // "SerialNumber"
144          $returned=$values;
145          break;
146        case 0x000d: // "CanonCameraInfo"
147          $returned=$this->processSubTag0x000d($values);
148          break;
149        case 0x0010: // "CanonModelID"
150          $tag=$this->tagDef->getTagById(0x0010);
151          $returned=$tag['tagValues.special'][sprintf("0x%08x", $values)];
152          unset($tag);
153          break;
154        case 0x0015: // "SerialNumberFormat"
155          $tag=$this->tagDef->getTagById(0x0015);
156          $returned=$tag['tagValues.special'][sprintf("0x%08x", $values)];
157          unset($tag);
158          break;
159        default:
160          $returned="Not yet implemented;".ConvertData::toHexDump($tagId, ByteType::USHORT)." => ".ConvertData::toHexDump($values, $type);
161          break;
162      }
163      return($returned);
164    }
165
166    /**
167     * this function process the subtag of the 0x0001 "CanonCameraSettings" tag
168     *
169     * @param Boolean $add : if set to false, the function return the tag value
170     *                       otherwise the function adds the tag
171     */
172    protected function processSubTag0x0001($values, $add=true)
173    {
174      foreach($values as $key => $val)
175      {
176        $tagDef=$this->tagDef->getTagById("0x0001.$key");
177
178        if(is_array($tagDef))
179        {
180          // make a fake IFDEntry
181          $entry=new IfdEntryReader("\xFF\xFF\x00\x00\x00\x00\x00\x00\xFF\x01\x00".chr($key), $this->byteOrder, "", 0, null);
182
183          $entry->getTag()->setId("0x0001.$key");
184          $entry->getTag()->setName($tagDef['tagName']);
185          $entry->getTag()->setValue($val);
186          $entry->getTag()->setKnown(true);
187          $entry->getTag()->setImplemented($tagDef['implemented']);
188          $entry->getTag()->setTranslatable($tagDef['translatable']);
189
190          if(array_key_exists('tagValues', $tagDef))
191          {
192            if(array_key_exists($val, $tagDef['tagValues']))
193            {
194              $returned=$tagDef['tagValues'][$val];
195            }
196            else
197            {
198              $returned="unknown (".$val.")";
199            }
200          }
201          else
202          {
203            switch($key)
204            {
205              case 2: // SelfTimer
206                if($val==0)
207                {
208                  $returned=Array("Off");
209                }
210                else
211                {
212                  $returned=Array((($val & 0xfff) / 10).' s');
213                  if($val & 0x4000)
214                    $returned[]="Custom";
215                }
216                break;
217              case 22: // LensType
218                /* in most case, with one Id we have one lens
219                 * in some case, with one Id we can have more than one lens
220                 * in this case, we made a $focal var like :
221                 *             "90 mm"
222                 *             "28-300mm"
223                 * and try to find a lens with this properties
224                 */
225                if(array_key_exists($val, $tagDef['tagValues.special']))
226                {
227                  $lens=$tagDef['tagValues.special'][$val];
228
229                  if(is_array($lens))
230                  {
231                    $focalUnit=(array_key_exists(25, $values))?$values[25]:1;
232                    $FocalShort=(array_key_exists(24, $values))?$values[24]/$focalUnit:0;
233                    $FocalLong=(array_key_exists(23, $values))?$values[23]/$focalUnit:0;
234                    $focal=(($FocalShort==$FocalLong or $FocalLong==0)?$FocalShort:$FocalShort."-".$FocalLong)."mm";
235
236                    foreach($lens as $name)
237                    {
238                      if(preg_match("/.*".$focal.".*/i", $name))
239                        $returned=$name;
240                    }
241                  }
242                  else
243                  {
244                    $returned=$lens;
245                  }
246                }
247                else
248                {
249                  $returned=$tagDef['tagValues.special'][0xffff];
250                }
251
252                break;
253              case 23: // LongFocal
254              case 24: // ShortFocal
255                /* note : the values seems to be divided by the FocalUnit
256                 * (subTag #25)
257                 */
258                if(array_key_exists(25, $values))
259                {
260                  $focalUnit=$values[25];
261                }
262                else
263                {
264                  $focalUnit=1;
265                }
266
267                $returned=ConvertData::toFocalLength($val/$focalUnit);
268                break;
269              case 25: // FocalUnit
270                $returned=$val."/mm";
271                break;
272              case 26: // MaxAperture
273              case 27: // MinAperture
274                $returned=ConvertData::toFNumber(round(exp($this->canonEv($val)*log(2)/2),1));
275                break;
276              case 29: // FlashBits
277                $returned=Array();
278                foreach($tagDef['tagValues.special'] as $key => $name)
279                {
280                  if(($key & $val) == $key)
281                  {
282                    $returned[]=$name;
283                  }
284                }
285                break;
286              default:
287                $returned="not yet implemented";
288                break;
289            }
290          }
291
292          if($add)
293          {
294            $entry->getTag()->setLabel($returned);
295            $this->entries[]=$entry;
296          }
297          else
298          {
299            // only return the value for the asked tag
300            unset($entry);
301            unset($tagDef);
302            return($returned);
303          }
304
305          unset($entry);
306        }
307        unset($tagDef);
308      }
309    }
310
311    /**
312     * this function process the subtag of the 0x0004 "CanonShotInfo" tag
313     *
314     * @param Boolean $add : if set to false, the function return the tag value
315     *                       otherwise the function adds the tag
316     */
317    protected function processSubTag0x0004($values, $add=true)
318    {
319      foreach($values as $key => $val)
320      {
321        $tagDef=$this->tagDef->getTagById("0x0004.$key");
322
323        if(is_array($tagDef))
324        {
325          // make a fake IFDEntry
326          $entry=new IfdEntryReader("\xFF\xFF\x00\x00\x00\x00\x00\x00\xFF\x04\x00".chr($key), $this->byteOrder, "", 0, null);
327
328          $entry->getTag()->setId("0x0004.$key");
329          $entry->getTag()->setName($tagDef['tagName']);
330          $entry->getTag()->setValue($val);
331          $entry->getTag()->setKnown(true);
332          $entry->getTag()->setImplemented($tagDef['implemented']);
333          $entry->getTag()->setTranslatable($tagDef['translatable']);
334
335          if(array_key_exists('tagValues', $tagDef))
336          {
337            if(array_key_exists($val, $tagDef['tagValues']))
338            {
339              $returned=$tagDef['tagValues'][$val];
340            }
341            else
342            {
343              $returned="unknown (".$val.")";
344            }
345          }
346          else
347          {
348            switch($key)
349            {
350              case 1: // AutoISO
351                $returned=round(exp($val/32*log(2))*100,0);
352                break;
353              case 2: // BaseISO
354                $returned=exp($this->canonEv($val) * log(2)) * 100 / 32;
355                break;
356              case 4: // TargetAperture
357              case 21: // FNumber
358                $returned=ConvertData::toFNumber(round(exp($this->canonEv($val)*log(2)/2), 1));
359                break;
360              case 5: // TargetExposureTime
361                $returned=ConvertData::toExposureTime(exp(-$this->CanonEv($val)*log(2)));
362                break;
363              case 6: // ExposureCompensation
364              case 15: // FlashExposureComp
365              case 17: // AEBBracketValue
366                $returned=ConvertData::$this->CanonEv($val);
367                break;
368              case 9: // SlowShutter
369                $returned=$val;
370                break;
371              case 13: // FlashGuideNumber
372                $returned=$val/32;
373                break;
374              case 23: // MeasuredEV2
375                $returned=$val/8-6;
376                break;
377              case 24: // BulbDuration
378              case 29: // SelfTimer2
379                $returned=$val/10;
380                break;
381              default:
382                $returned="not yet implemented";
383                break;
384            }
385          }
386
387          if($add)
388          {
389            $entry->getTag()->setLabel($returned);
390            $this->entries[]=$entry;
391          }
392          else
393          {
394            // only return the value for the asked tag
395            unset($entry);
396            unset($tagDef);
397            return($returned);
398          }
399
400          unset($entry);
401        }
402        unset($tagDef);
403      }
404    }
405
406    /**
407     * this function process the subtag of the 0x000d "CanonCameraInfo" tag
408     *
409     * @param Boolean $add : if set to false, the function return the tag value
410     *                       otherwise the function adds the tag
411     */
412    protected function processSubTag0x000d($values, $add=true)
413    {
414      $name=GlobalTags::getExifMaker();
415
416      if(preg_match("/\b1DS?$/", $name))
417      {
418
419      }/*
420      elseif(preg_match("/\b1Ds? Mark II$/", $name))
421      {
422
423      }
424      elseif(preg_match("/\b1Ds? Mark II N$/", $name))
425      {
426
427      }
428      elseif(preg_match("/\b1Ds? Mark III$/", $name))
429      {
430
431      }
432      elseif(preg_match("/EOS 5D$/", $name))
433      {
434
435      }
436      elseif(preg_match("/EOS 5D Mark II$/", $name))
437      {
438
439      }
440      elseif(preg_match("/EOS 7D$/", $name))
441      {
442
443      }*/
444      elseif(preg_match("/.*\b1D Mark IV/i", $name))
445      {
446        $returned=$this->processSubTag0x000d_1DMarkIV($values, $add);
447      }
448      elseif(preg_match("/.*EOS 40D/i", $name))
449      {
450        $returned=$this->processSubTag0x000d_40D($values, $add);
451      }/*
452      elseif(preg_match("/EOS 50D$/", $name))
453      {
454
455      }
456      elseif(preg_match("/\b(450D|REBEL XSi|Kiss X2)\b/", $name))
457      {
458
459      }
460      elseif(preg_match("/\b(500D|REBEL T1i|Kiss X3)\b/", $name))
461      {
462
463      }
464      elseif(preg_match("/\b(1000D|REBEL XS|Kiss F)\b/", $name))
465      {
466
467      }
468      elseif(preg_match("/\b1DS?/", $name))
469      {
470
471      }*/
472      else
473      {
474        /*
475         * note : powershot are not implemented, in exiftool condition to
476         * determine the model are not very understandable
477         */
478        $returned="$name is not yet implemented => ".ConvertData::toHexDump($values, ByteType::ASCII);
479      }
480      return($returned);
481    }
482
483    /**
484     * this function process the subtag of the 0x000d "CanonCameraInfo" tag
485     * for the EOS 40D camera
486     */
487    protected function processSubTag0x000d_40D($values)
488    {
489      $tagDef=$this->tagDef->getTagById(0x000d);
490      $list=$tagDef['tagValues.special']['40D'];
491      $data=new Data($values);
492
493      foreach($list as $tagIndex)
494      {
495        $subTagDef=$this->tagDef->getTagById("0x000d.40D.$tagIndex");
496
497        if(is_array($subTagDef))
498        {
499          $val=$this->readDataFromSubTag($data, $subTagDef);
500
501          // make a fake IFDEntry
502          $entry=new IfdEntryReader("\xFF\xFF\x00\x00\x00\x00\x00\x00\xFF\x0d\x00\x00", $this->byteOrder, "", 0, null);
503
504          $entry->getTag()->setId("0x000d.40D.$tagIndex");
505          $entry->getTag()->setName($subTagDef['tagName']);
506          $entry->getTag()->setValue($val);
507          $entry->getTag()->setKnown(true);
508          $entry->getTag()->setImplemented($subTagDef['implemented']);
509          $entry->getTag()->setTranslatable($subTagDef['translatable']);
510
511          if(array_key_exists('tagValues', $subTagDef))
512          {
513            if(array_key_exists($val, $subTagDef['tagValues']))
514            {
515              $returned=$subTagDef['tagValues'][$val];
516            }
517            else
518            {
519              $returned="unknown (".$val.")";
520            }
521          }
522          else
523          {
524            switch($tagIndex)
525            {
526              case 24: // CameraTemperature
527                $returned=($val-128)."°C";
528                break;
529              case 29: // FocalLength
530                $returned=ConvertData::toFocalLength($val);
531                break;
532              case 111: // WhiteBalance
533                // same method than the ShotInfo.WhiteBalance tag
534                $returned=$this->processSubTag0x0004(Array(7 => $val), false);
535                break;
536              case 214: // LensType
537                // same method than the CanonCameraSettings.LensType tag
538                $FocalShort=$this->readDataFromSubTag($data, $this->tagDef->getTagById("0x000d.40D.216"));
539                $FocalLong=$this->readDataFromSubTag($data, $this->tagDef->getTagById("0x000d.40D.218"));
540
541                $returned=$this->processSubTag0x0001(Array(22 => $val, 23 => $FocalLong, 24 => $FocalShort), false);
542                break;
543              case 216: // ShortFocal
544              case 218: // LongFocal
545                $returned=ConvertData::toFocalLength($val);
546                break;
547              case 255: // FirmwareVersion
548              case 2347: // LensModel
549                $returned=ConvertData::toStrings($val);
550                break;
551              default:
552                $returned="not yet implemented ($tagIndex)";
553                break;
554            }
555          }
556
557          $entry->getTag()->setLabel($returned);
558          $this->entries[]=$entry;
559
560          unset($entry);
561        }
562        unset($subTagDef);
563      }
564      unset($tagDef);
565      unset($list);
566      unset($data);
567
568      return("[EOS 40D]");
569    }
570
571    /**
572     * this function process the subtag of the 0x000d "CanonCameraInfo" tag
573     * for the EOS 1D Mark IV camera
574     *
575     */
576    protected function processSubTag0x000d_1DMarkIV($values)
577    {
578      $tagDef=$this->tagDef->getTagById(0x000d);
579      $list=$tagDef['tagValues.special']['1DMarkIV'];
580      $data=new Data($values);
581
582      foreach($list as $tagIndex)
583      {
584        $subTagDef=$this->tagDef->getTagById("0x000d.1DMarkIV.$tagIndex");
585
586        if(is_array($subTagDef))
587        {
588          $val=$this->readDataFromSubTag($data, $subTagDef);
589
590          // make a fake IFDEntry
591          $entry=new IfdEntryReader("\xFF\xFF\x00\x00\x00\x00\x00\x00\xFF\x0d\x00\x00", $this->byteOrder, "", 0, null);
592
593          $entry->getTag()->setId("0x000d.1DMarkIV.$tagIndex");
594          $entry->getTag()->setName($subTagDef['tagName']);
595          $entry->getTag()->setValue($val);
596          $entry->getTag()->setKnown(true);
597          $entry->getTag()->setImplemented($subTagDef['implemented']);
598          $entry->getTag()->setTranslatable($subTagDef['translatable']);
599
600          if(array_key_exists('tagValues', $subTagDef))
601          {
602            if(array_key_exists($val, $subTagDef['tagValues']))
603            {
604              $returned=$subTagDef['tagValues'][$val];
605            }
606            else
607            {
608              $returned="unknown (".$val.")";
609            }
610          }
611          else
612          {
613            switch($tagIndex)
614            {
615              case 25: // CameraTemperature
616                $returned=($val-128)."°C";
617                break;
618              case 30: // FocalLength
619                $returned=ConvertData::toFocalLength($val);
620                break;
621              default:
622                $returned="not yet implemented ($tagIndex)";
623                break;
624            }
625          }
626
627          $entry->getTag()->setLabel($returned);
628          $this->entries[]=$entry;
629
630          unset($entry);
631        }
632        unset($subTagDef);
633      }
634      unset($tagDef);
635      unset($list);
636      unset($data);
637
638      return("[EOS 1D Mark IV]");
639    }
640
641
642
643    /**
644     * read datas from a $data object, according to the tag definition
645     *
646     * @param Data $data : a Data object
647     * @param Array $tagDef : the tag definition
648     */
649    protected function readDataFromSubTag($data, $tagDef)
650    {
651      if(!array_key_exists('pos', $tagDef))
652        return("Invalid tagDef (no 'pos')");
653
654      $pos=$tagDef['pos'];
655
656      if(array_key_exists('byteOrder', $tagDef))
657      {
658        $data->setByteOrder($tagDef['byteOrder']);
659      }
660      else
661      {
662        $data->setByteOrder(BYTE_ORDER_LITTLE_ENDIAN);
663      }
664
665      $returned="";
666      switch($tagDef['type'])
667      {
668        case ByteType::UNDEFINED:
669        case ByteType::UNKNOWN :
670        case ByteType::ASCII:
671          $nbCar=(array_key_exists('length', $tagDef))?$tagDef['length']:1;
672          $returned=$data->readASCII($nbCar, $pos);
673          break;
674        case ByteType::UBYTE:
675          $returned=$data->readUByte($pos);
676          break;
677        case ByteType::USHORT:
678          $returned=$data->readUShort($pos);
679          break;
680        case ByteType::ULONG:
681          $returned=$data->readULong($pos);
682          break;
683        case ByteType::URATIONAL:
684          $returned=$data->readURational($pos);
685          break;
686        case ByteType::SBYTE:
687          $returned=$data->readSByte($pos);
688          break;
689        case ByteType::SSHORT:
690          $returned=$data->readSShort($pos);
691          break;
692        case ByteType::SLONG:
693          $returned=$data->readSLong($pos);
694          break;
695        case ByteType::SRATIONAL:
696          $returned=$data->readSRational($pos);
697          break;
698        case ByteType::FLOAT:
699          $returned="";
700          break;
701        case ByteType::DOUBLE:
702          $returned="";
703          break;
704      }
705      return($returned);
706    }
707
708
709    /**
710     * Convert Canon hex-based EV (modulo 0x20) to real number
711     *     0x00 -> 0
712     *     0x0c -> 0.33333
713     *     0x10 -> 0.5
714     *     0x14 -> 0.66666
715     *     0x20 -> 1   ...  etc
716     *
717     * function from exiftool
718     *
719     * @param Integer $value : the canon EV value
720     * @return Float : the converted value
721     */
722    protected function canonEv($val)
723    {
724      //$val=95;
725
726      if($val<0)
727      {
728        $val=-$val;
729        $sign=-1;
730      }
731      else
732      {
733        $sign=1;
734      }
735      $frac = (float) ($val & 0x1f); //$frac = $val % 0x20;
736      $val -= $frac;  // remove fraction
737      // Convert 1/3 and 2/3 codes
738      if ($frac == 0x0c)
739      {
740        $frac = 0x20 / 3;
741      }
742      elseif($frac==0x14)
743      {
744        $frac = 0x40 / 3;
745      }
746      return($sign * ($val + $frac) / 0x20);
747    }
748  }
749
750?>
Note: See TracBrowser for help on using the repository browser.