aa9caa3cca5b95c08e140ce2f9add30c749d9e1d
[yaffs-website] / lsolesen / pel / src / PelFormat.php
1 <?php
2 /*
3  * PEL: PHP Exif Library.
4  * A library with support for reading and
5  * writing all Exif headers in JPEG and TIFF images using PHP.
6  *
7  * Copyright (C) 2004, 2005 Martin Geisler.
8  * Copyright (C) 2017 Johannes Weberhofer.
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  * Namespace for functions operating on Exif formats.
29  *
30  * This class defines the constants that are to be used whenever one
31  * has to refer to the format of an Exif tag. They will be
32  * collectively denoted by the pseudo-type PelFormat throughout the
33  * documentation.
34  *
35  * All the methods defined here are static, and they all operate on a
36  * single argument which should be one of the class constants.
37  *
38  * @author Martin Geisler <mgeisler@users.sourceforge.net>
39  * @author Johannes Weberhofer <jweberhofer@weberhofer.at>
40  * @license http://www.gnu.org/licenses/gpl.html GNU General Public
41  *          License (GPL)
42  * @package
43  *
44  */
45 class PelFormat
46 {
47
48     /**
49      * Unsigned byte.
50      *
51      * Each component will be an unsigned 8-bit integer with a value
52      * between 0 and 255.
53      *
54      * Modelled with the {@link PelEntryByte} class.
55      */
56     const BYTE = 1;
57
58     /**
59      * ASCII string.
60      *
61      * Each component will be an ASCII character.
62      *
63      * Modelled with the {@link PelEntryAscii} class.
64      */
65     const ASCII = 2;
66
67     /**
68      * Unsigned short.
69      *
70      * Each component will be an unsigned 16-bit integer with a value
71      * between 0 and 65535.
72      *
73      * Modelled with the {@link PelEntryShort} class.
74      */
75     const SHORT = 3;
76
77     /**
78      * Unsigned long.
79      *
80      * Each component will be an unsigned 32-bit integer with a value
81      * between 0 and 4294967295.
82      *
83      * Modelled with the {@link PelEntryLong} class.
84      */
85     const LONG = 4;
86
87     /**
88      * Unsigned rational number.
89      *
90      * Each component will consist of two unsigned 32-bit integers
91      * denoting the enumerator and denominator. Each integer will have
92      * a value between 0 and 4294967295.
93      *
94      * Modelled with the {@link PelEntryRational} class.
95      */
96     const RATIONAL = 5;
97
98     /**
99      * Signed byte.
100      *
101      * Each component will be a signed 8-bit integer with a value
102      * between -128 and 127.
103      *
104      * Modelled with the {@link PelEntrySByte} class.
105      */
106     const SBYTE = 6;
107
108     /**
109      * Undefined byte.
110      *
111      * Each component will be a byte with no associated interpretation.
112      *
113      * Modelled with the {@link PelEntryUndefined} class.
114      */
115     const UNDEFINED = 7;
116
117     /**
118      * Signed short.
119      *
120      * Each component will be a signed 16-bit integer with a value
121      * between -32768 and 32767.
122      *
123      * Modelled with the {@link PelEntrySShort} class.
124      */
125     const SSHORT = 8;
126
127     /**
128      * Signed long.
129      *
130      * Each component will be a signed 32-bit integer with a value
131      * between -2147483648 and 2147483647.
132      *
133      * Modelled with the {@link PelEntrySLong} class.
134      */
135     const SLONG = 9;
136
137     /**
138      * Signed rational number.
139      *
140      * Each component will consist of two signed 32-bit integers
141      * denoting the enumerator and denominator. Each integer will have
142      * a value between -2147483648 and 2147483647.
143      *
144      * Modelled with the {@link PelEntrySRational} class.
145      */
146     const SRATIONAL = 10;
147
148     /**
149      * Floating point number.
150      *
151      * Entries with this format are not currently implemented.
152      */
153     const FLOAT = 11;
154
155     /**
156      * Double precision floating point number.
157      *
158      * Entries with this format are not currently implemented.
159      */
160     const DOUBLE = 12;
161
162     /**
163      * Values for format's short names
164      */
165     protected static $formatName = array(
166         self::ASCII => 'Ascii',
167         self::BYTE => 'Byte',
168         self::SHORT => 'Short',
169         self::LONG => 'Long',
170         self::RATIONAL => 'Rational',
171         self::SBYTE => 'SByte',
172         self::SSHORT => 'SShort',
173         self::SLONG => 'SLong',
174         self::SRATIONAL => 'SRational',
175         self::FLOAT => 'Float',
176         self::DOUBLE => 'Double',
177         self::UNDEFINED => 'Undefined'
178     );
179
180     protected static $formatLength = array(
181         self::ASCII => 1,
182         self::BYTE => 1,
183         self::SHORT => 2,
184         self::LONG => 4,
185         self::RATIONAL => 8,
186         self::SBYTE => 1,
187         self::SSHORT => 2,
188         self::SLONG => 4,
189         self::SRATIONAL => 8,
190         self::FLOAT => 4,
191         self::DOUBLE => 8,
192         self::UNDEFINED => 1
193     );
194
195     /**
196      * Returns the name of a format like 'Ascii' for the {@link ASCII} format
197      *
198      * @param integer $type
199      *            as defined in {@link PelFormat}
200      * @return string
201      */
202     public static function getName($type)
203     {
204         if (array_key_exists($type, self::$formatName)) {
205             return self::$formatName[$type];
206         } else {
207             return Pel::fmt('Unknown format: 0x%X', $type);
208         }
209     }
210
211     /**
212      * Return the size of components in a given format in bytes needed to store one component with the
213      * given format.
214      *
215      * @param integer $type
216      *            as defined in {@link PelFormat}
217      * @return integer|string
218      */
219     public static function getSize($type)
220     {
221         if (array_key_exists($type, self::$formatLength)) {
222             return self::$formatLength[$type];
223         } else {
224             return Pel::fmt('Unknown format: 0x%X', $type);
225         }
226     }
227 }