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

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

[Plugin:AMetaData] prepare the directory for a future plugin

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