75781bde73c20df01d983bf66dbf713edb2cbf87
[yaffs-website] / lsolesen / pel / src / PelEntry.php
1 <?php
2
3 /**
4  * PEL: PHP Exif Library.
5  * A library with support for reading and
6  * writing all Exif headers in JPEG and TIFF images using PHP.
7  *
8  * Copyright (C) 2004, 2005, 2006 Martin Geisler.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program in the file COPYING; if not, write to the
22  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23  * Boston, MA 02110-1301 USA
24  */
25 namespace lsolesen\pel;
26
27 /**
28  * Classes for dealing with Exif entries.
29  *
30  * This file defines two exception classes and the abstract class
31  * {@link PelEntry} which provides the basic methods that all Exif
32  * entries will have. All Exif entries will be represented by
33  * descendants of the {@link PelEntry} class --- the class itself is
34  * abstract and so it cannot be instantiated.
35  *
36  * @author Martin Geisler <mgeisler@users.sourceforge.net>
37  * @license http://www.gnu.org/licenses/gpl.html GNU General Public
38  *          License (GPL)
39  * @package PEL
40  */
41
42 /**
43  * Common ancestor class of all {@link PelIfd} entries.
44  *
45  * As this class is abstract you cannot instantiate objects from it.
46  * It only serves as a common ancestor to define the methods common to
47  * all entries. The most important methods are {@link getValue()} and
48  * {@link setValue()}, both of which is abstract in this class. The
49  * descendants will give concrete implementations for them.
50  *
51  * If you have some data coming from an image (some raw bytes), then
52  * the static method {@link newFromData()} is helpful --- it will look
53  * at the data and give you a proper decendent of {@link PelEntry}
54  * back.
55  *
56  * If you instead want to have an entry for some data which take the
57  * form of an integer, a string, a byte, or some other PHP type, then
58  * don't use this class. You should instead create an object of the
59  * right subclass ({@link PelEntryShort} for short integers, {@link
60  * PelEntryAscii} for strings, and so on) directly.
61  *
62  * @author Martin Geisler <mgeisler@users.sourceforge.net>
63  * @package PEL
64  */
65 abstract class PelEntry
66 {
67
68     /**
69      * Type of IFD containing this tag.
70      *
71      * This must be one of the constants defined in {@link PelIfd}:
72      * {@link PelIfd::IFD0} for the main image IFD, {@link PelIfd::IFD1}
73      * for the thumbnail image IFD, {@link PelIfd::EXIF} for the Exif
74      * sub-IFD, {@link PelIfd::GPS} for the GPS sub-IFD, or {@link
75      * PelIfd::INTEROPERABILITY} for the interoperability sub-IFD.
76      *
77      * @var int
78      */
79     protected $ifd_type;
80
81     /**
82      * The bytes representing this entry.
83      *
84      * Subclasses must either override {@link getBytes()} or, if
85      * possible, maintain this property so that it always contains a
86      * true representation of the entry.
87      *
88      * @var string
89      */
90     protected $bytes = '';
91
92     /**
93      * The {@link PelTag} of this entry.
94      *
95      * @var PelTag
96      */
97     protected $tag;
98
99     /**
100      * The {@link PelFormat} of this entry.
101      *
102      * @var PelFormat
103      */
104     protected $format;
105
106     /**
107      * The number of components of this entry.
108      *
109      * @var int
110      */
111     protected $components;
112
113     /**
114      * Return the tag of this entry.
115      *
116      * @return PelTag the tag of this entry.
117      */
118     public function getTag()
119     {
120         return $this->tag;
121     }
122
123     /**
124      * Return the type of IFD which holds this entry.
125      *
126      * @return int one of the constants defined in {@link PelIfd}:
127      *         {@link PelIfd::IFD0} for the main image IFD, {@link PelIfd::IFD1}
128      *         for the thumbnail image IFD, {@link PelIfd::EXIF} for the Exif
129      *         sub-IFD, {@link PelIfd::GPS} for the GPS sub-IFD, or {@link
130      *         PelIfd::INTEROPERABILITY} for the interoperability sub-IFD.
131      */
132     public function getIfdType()
133     {
134         return $this->ifd_type;
135     }
136
137     /**
138      * Update the IFD type.
139      *
140      * @param
141      *            int must be one of the constants defined in {@link
142      *            PelIfd}: {@link PelIfd::IFD0} for the main image IFD, {@link
143      *            PelIfd::IFD1} for the thumbnail image IFD, {@link PelIfd::EXIF}
144      *            for the Exif sub-IFD, {@link PelIfd::GPS} for the GPS sub-IFD, or
145      *            {@link PelIfd::INTEROPERABILITY} for the interoperability
146      *            sub-IFD.
147      */
148     public function setIfdType($type)
149     {
150         $this->ifd_type = $type;
151     }
152
153     /**
154      * Return the format of this entry.
155      *
156      * @return PelFormat the format of this entry.
157      */
158     public function getFormat()
159     {
160         return $this->format;
161     }
162
163     /**
164      * Return the number of components of this entry.
165      *
166      * @return int the number of components of this entry.
167      */
168     public function getComponents()
169     {
170         return $this->components;
171     }
172
173     /**
174      * Turn this entry into bytes.
175      *
176      * @param
177      *            PelByteOrder the desired byte order, which must be either
178      *            {@link Convert::LITTLE_ENDIAN} or {@link Convert::BIG_ENDIAN}.
179      *
180      * @return string bytes representing this entry.
181      */
182     public function getBytes($o)
183     {
184         return $this->bytes;
185     }
186
187     /**
188      * Get the value of this entry as text.
189      *
190      * The value will be returned in a format suitable for presentation,
191      * e.g., rationals will be returned as 'x/y', ASCII strings will be
192      * returned as themselves etc.
193      *
194      * @param
195      *            boolean some values can be returned in a long or more
196      *            brief form, and this parameter controls that.
197      *
198      * @return string the value as text.
199      */
200     abstract public function getText($brief = false);
201
202     /**
203      * Get the value of this entry.
204      *
205      * The value returned will generally be the same as the one supplied
206      * to the constructor or with {@link setValue()}. For a formatted
207      * version of the value, one should use {@link getText()} instead.
208      *
209      * @return mixed the unformatted value.
210      */
211     abstract public function getValue();
212
213     /**
214      * Set the value of this entry.
215      *
216      * The value should be in the same format as for the constructor.
217      *
218      * @param
219      *            mixed the new value.
220      *
221      * @abstract
222      *
223      */
224     public function setValue($value)
225     {
226         /*
227          * This (fake) abstract method is here to make it possible for the
228          * documentation to refer to PelEntry::setValue().
229          * It cannot declared abstract in the proper PHP way, for then PHP
230          * wont allow subclasses to define it with two arguments (which is
231          * what PelEntryCopyright does).
232          */
233         throw new PelException('setValue() is abstract.');
234     }
235
236     /**
237      * Turn this entry into a string.
238      *
239      * @return string a string representation of this entry. This is
240      *         mostly for debugging.
241      */
242     public function __toString()
243     {
244         $str = Pel::fmt("  Tag: 0x%04X (%s)\n", $this->tag, PelTag::getName($this->ifd_type, $this->tag));
245         $str .= Pel::fmt("    Format    : %d (%s)\n", $this->format, PelFormat::getName($this->format));
246         $str .= Pel::fmt("    Components: %d\n", $this->components);
247         if ($this->getTag() != PelTag::MAKER_NOTE && $this->getTag() != PelTag::PRINT_IM) {
248             $str .= Pel::fmt("    Value     : %s\n", print_r($this->getValue(), true));
249         }
250         $str .= Pel::fmt("    Text      : %s\n", $this->getText());
251         return $str;
252     }
253 }