source: extensions/AMetaData/JpegMetaData/Readers/TiffReader.class.php @ 4698

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

[Plugin:AMetaData] Finished to comment the JpegMetaData classes and rename some methods

  • Property svn:executable set to *
File size: 5.7 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 TiffReader class is the dedicated to read a TIFF structure
36 *
37 * A Tiff structure is formatted as this :
38 *  - byte order            : 2 bytes, "II" or "MM", indicates the byte order
39 *  - header tag            : 2 bytes, UShort equals 0x002a
40 *  - first IFD offset      : 4 bytes, ULong
41 *  - IFDs                  :
42 *
43 * => See IfdReader.class.php & IfdEntryReader.class.php to know more on IFD <==
44 *
45 * -----------------------------------------------------------------------------
46 *
47 * .. Notes ..
48 *
49 *
50 * The TiffReader class is derived from the SegmentReader class.
51 *
52 * ======> See SegmentReader.class.php to know more about common methods <======
53 *
54 *
55 * This class provides theses public functions :
56 *  - getNbIFDs
57 *  - getIFDs
58 *  - getIFD
59 *
60 * -----------------------------------------------------------------------------
61 */
62
63
64  require_once(JPEG_METADATA_DIR."Common/ConvertData.class.php");
65  require_once(JPEG_METADATA_DIR."Common/Data.class.php");
66  require_once(JPEG_METADATA_DIR."Readers/SegmentReader.class.php");
67  require_once(JPEG_METADATA_DIR."Readers/IfdReader.class.php");
68
69  class TiffReader extends SegmentReader
70  {
71    private $IFDs = Array();
72    private $offsetData = 0;
73    private $byteOrder = BYTE_ORDER_LITTLE_ENDIAN;
74    private $firstIFDOffset = 0;
75
76    /**
77     * The constructor need the Tiff block datas (given as a Data object) and
78     * offset of the TIFF block inside the jpeg file
79     *
80     * @param Data $data :
81     * @param ULong $offsetData (optional) :
82     */
83    function __construct(Data $data, $offsetData=0)
84    {
85      parent::__construct($data);
86
87      $this->offsetData = $offsetData;
88      $header=$this->data->readASCII(2);
89
90      /*
91       * TIFF Header begins wih "II" or "MM" (indicate the byte order)
92       * next value is an USHORT, must equals 0x2a
93       *
94       * all data have to be read with the byte order defined in header
95       */
96      if($header=="II" or $header="MM")
97      {
98        $this->byteOrder=$header;
99        $this->data->setByteOrder($this->byteOrder);
100
101        $header=$this->data->readUShort();
102        if($header==0x002a)
103        {
104          $this->isValid=true;
105          $this->firstIFDOffset=$this->data->readULong();
106          $this->readData();
107        }
108      }
109    }
110
111    function __destruct()
112    {
113      unset($this->IFDs);
114    }
115
116    /**
117     * return the number of IFDs found in the Tiff block
118     *
119     * @return Integer
120     */
121    public function getNbIFDs()
122    {
123      return(count($this->IFDs));
124    }
125
126    /**
127     * return an array of IFD found in the the Tiff block
128     *
129     * @return IFD[]
130     */
131    public function getIFDs()
132    {
133      return($this->IFDs);
134    }
135
136    /**
137     * returns a specific IFD
138     *
139     * @param Integer $num : index of the needed IFD
140     * @return IFD
141     */
142    public function getIFD($num)
143    {
144      if($num>=0 and $num<count($this->IFDs))
145        return($this->IFDs[$num]);
146      else
147        return(null);
148    }
149
150    public function toString()
151    {
152      $returned="TIFF block offset: ".sprintf("%08x", $this->offsetData).
153                " ; byteOrder: ".$this->byteOrder.
154                " ; isValid: ".($this->isValid?"Y":"N").
155                " ; isLoaded: ".($this->isValid?"Y":"N").
156                " ; IFDs: ".count($this->IFDs).
157                " ; first IFD Offset: ".sprintf("0x%04x", $this->firstIFDOffset);
158      return($returned);
159    }
160
161    /**
162     * The readData function read IFDs from the Tiff block, and add them into
163     * the IFDs array
164     */
165    private function readData()
166    {
167      $nextIFD = $this->firstIFDOffset;
168      /*
169       * while the next IFD offset is not zero, read the IFD at the designed
170       * offset
171       *
172       * the next IFD offset is given at the end of the last IFD read.
173       */
174      while($nextIFD!=0)
175      {
176        $this->data->seek($nextIFD);
177        $IFD = new IfdReader($this->data->readASCII(), $nextIFD, $this->byteOrder);
178        $this->IFDs[]=$IFD;
179        $nextIFD = $IFD->getNextIFDOffset();
180      }
181    }
182  }
183
184
185?>
Note: See TracBrowser for help on using the repository browser.