source: extensions/AMetaData/JpegMetaData/Readers/HeightBIMReader.class.php @ 6299

Last change on this file since 6299 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.9 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 HeightBIMReader class is the class dedicated to read 8BIM block from an
36 * APP13 segment
37 *
38 * An 8BIM block is defined by
39 *  4 bytes (ASCII)  : block header equals "8BIM"
40 *  2 bytes (UShort) : block type "\x04\x04" for IPTC (other type are not
41 *                     managed)
42 *  1 byte           : size of a pascal string
43 *  n bytes          : name of the 8BIM block - a pascal string (equals \x00 if
44 *                     size length equals 0)
45 *  4 bytes          : block size
46 *
47 * -----------------------------------------------------------------------------
48 *
49 * .. Notes ..
50 *
51 * This class provides theses public functions :
52 *  - getNbEntries
53 *  - getEntries
54 *  - isValid
55 *  - getSize
56 *  - getBlockSize
57 *  - getName
58 *
59 * -----------------------------------------------------------------------------
60 *
61 * 8BIM definition class (IPTC block)
62 *
63 * -----------------------------------------------------------------------------
64 */
65
66  require_once(JPEG_METADATA_DIR."Common/Tag.class.php");
67
68  class HeightBIMReader
69  {
70    const HEADER = "8BIM";
71    const TYPE_IPTC = 0x0404;
72
73    private $blockSize = 0;
74    private $dataSize = 0;
75    private $readed = 0;
76    private $name = "";
77
78    private $isValid = false;
79
80    private $entries = array();
81
82    private $data = null;
83
84    /**
85     * the constructor only needs datas to parse
86     *
87     * @param String $data
88     */
89    function __construct($data)
90    {
91      $this->data = new Data($data, BYTE_ORDER_BIG_ENDIAN);
92
93      $tmp=$this->data->readASCII(4);
94
95      if($tmp==self::HEADER)
96      {
97        /*
98         * header is ok, so try to read the 8BIM datas
99         */
100        $this->type = $this->data->readUShort();
101        $pLength=$this->data->readUByte();
102
103
104        if($pLength==0)
105        {
106          // the block doesn't have name
107          $this->data->readUByte();
108          $this->blockSize=12;
109        }
110        else
111        {
112          // the block have a name
113          $this->name=$this->data->readASCII($pLength);
114          $this->blockSize=11+$pLength;
115        }
116
117        $this->dataSize = $this->data->readULong();
118        $this->blockSize+=$this->dataSize+1;
119        if($this->dataSize>0 and $this->type==self::TYPE_IPTC)
120        {
121          $this->readEntries();
122          $this->isValid=true;
123        }
124      }
125    }
126
127    function __destruct()
128    {
129      unset($this->entries);
130      unset($this->data);
131    }
132
133    /**
134     * returns the number of tags found in the 8BIM block
135     *
136     * @return Integer
137     */
138    public function getNbTags()
139    {
140      return(count($this->entries));
141    }
142
143    /**
144     * returns an array of found tags in the 8BIM block
145     *
146     * @return Tag[]
147     */
148    public function getTags()
149    {
150      return($this->entries);
151    }
152
153    /**
154     * returns true if the 8BIM block is a valid IPTC data block
155     *
156     * @return Boolean
157     */
158    public function isValid()
159    {
160      return($this->isValid);
161    }
162
163    /**
164     * returns the size of datas in the 8BIM block
165     *
166     * @return Integer
167     */
168    public function getSize()
169    {
170      return($this->dataSize);
171    }
172
173    /**
174     * returns the size of the 8BIM block
175     *
176     * @return Integer
177     */
178    public function getBlockSize()
179    {
180      return($this->blockSize);
181    }
182
183    /**
184     * returns the name of the 8BIM block
185     *
186     * @return String
187     */
188    public function getName()
189    {
190      return($this->name);
191    }
192
193    /**
194     * read IPTC datas from the 8BIM block, instanciate the tags and add it to
195     * entries
196     *
197     * the tag value is not interpreted
198     */
199    private function readEntries()
200    {
201      while(($this->readed<$this->dataSize) and ($this->data->readUByte()==0x1C))
202      {
203        $tagId=$this->data->readUShort();
204        $tagLength=$this->data->readUShort();
205
206        if($tagLength>0)
207        {
208          $tagValue=$this->data->readASCII($tagLength);
209        }
210        else
211        {
212          $tagValue="";
213        }
214
215        $this->readed+=$tagLength+5; // +5 => 3bytes for tag Id, 2bytes for tag length
216
217        $tag=new Tag($tagId, $tagValue);
218        $this->entries[]=$tag;
219        unset($tag);
220      }
221    }
222
223  }
224
225
226?>
Note: See TracBrowser for help on using the repository browser.