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

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

Optimize some memory leak and some bugged lines of code

  • Property svn:executable set to *
File size: 5.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 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      parent::__destruct();
114      unset($this->IFDs);
115    }
116
117    /**
118     * return the number of IFDs found in the Tiff block
119     *
120     * @return Integer
121     */
122    public function getNbIFDs()
123    {
124      return(count($this->IFDs));
125    }
126
127    /**
128     * return an array of IFD found in the the Tiff block
129     *
130     * @return IFD[]
131     */
132    public function getIFDs()
133    {
134      return($this->IFDs);
135    }
136
137    /**
138     * returns a specific IFD
139     *
140     * @param Integer $num : index of the needed IFD
141     * @return IFD
142     */
143    public function getIFD($num)
144    {
145      if($num>=0 and $num<count($this->IFDs))
146        return($this->IFDs[$num]);
147      else
148        return(null);
149    }
150
151    public function toString()
152    {
153      $returned="TIFF block offset: ".sprintf("%08x", $this->offsetData).
154                " ; byteOrder: ".$this->byteOrder.
155                " ; isValid: ".($this->isValid?"Y":"N").
156                " ; isLoaded: ".($this->isValid?"Y":"N").
157                " ; IFDs: ".count($this->IFDs).
158                " ; first IFD Offset: ".sprintf("0x%04x", $this->firstIFDOffset);
159      return($returned);
160    }
161
162    /**
163     * The readData function read IFDs from the Tiff block, and add them into
164     * the IFDs array
165     */
166    private function readData()
167    {
168      $nextIFD = $this->firstIFDOffset;
169      /*
170       * while the next IFD offset is not zero, read the IFD at the designed
171       * offset
172       *
173       * the next IFD offset is given at the end of the last IFD read.
174       */
175      while($nextIFD!=0)
176      {
177        $this->data->seek($nextIFD);
178        $IFD = new IfdReader($this->data->readASCII(), $nextIFD, $this->byteOrder);
179        $this->IFDs[]=$IFD;
180        $nextIFD = $IFD->getNextIFDOffset();
181        unset($IFD);
182      }
183    }
184  }
185
186
187?>
Note: See TracBrowser for help on using the repository browser.