Source for file PelConvert.php

Documentation is available at PelConvert.php

  1. <?php
  2.  
  3. /* PEL: PHP EXIF Library. A library with support for reading and
  4. * writing all EXIF headers in JPEG and TIFF images using PHP.
  5. *
  6. * Copyright (C) 2004 Martin Geisler <gimpster@users.sourceforge.net>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program in the file COPYING; if not, write to the
  20. * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  21. * Boston, MA 02111-1307 USA
  22. */
  23.  
  24. /* PelConvert.php,v 1.12 2004/07/21 15:43:09 gimpster Exp */
  25.  
  26.  
  27. /**
  28. * Routines for converting back and forth between bytes and integers.
  29. *
  30. * @author Martin Geisler <gimpster@users.sourceforge.net>
  31. * @version 1.12
  32. * @date 2004/07/21 15:43:09
  33. * @license http://www.gnu.org/licenses/gpl.html GNU General Public
  34. * License (GPL)
  35. * @package PEL
  36. */
  37.  
  38. /**
  39. * Conversion functions to and from bytes and integers.
  40. *
  41. * The functions found in this class are used to convert bytes into
  42. * integers of several sizes ({@link bytesToShort}, {@link }
  43. * bytesToLong}, and {@link bytesToRational}) and convert integers of
  44. * several sizes into bytes ({@link shortToBytes} and {@link }
  45. * longToBytes}).
  46. *
  47. * All the methods are static and they all rely on an argument that
  48. * specifies the byte order to be used, this must be one of the class
  49. * constants {@link LITTLE_ENDIAN} or {@link BIG_ENDIAN}. These
  50. * constants will be referred to as the pseudo type PelByteOrder
  51. * throughout the documentation.
  52. *
  53. * @author Martin Geisler <gimpster@users.sourceforge.net>
  54. * @package PEL
  55. */
  56. class PelConvert {
  57.  
  58. /**
  59. * Little-endian byte order.
  60. *
  61. * Data stored in little-endian byte order store the least
  62. * significant byte first, so the number 0x12345678 becomes 0x78
  63. * 0x56 0x34 0x12 when stored with little-endian byte order.
  64. */
  65. const LITTLE_ENDIAN = true;
  66.  
  67. /**
  68. * Big-endian byte order.
  69. *
  70. * Data stored in big-endian byte order store the most significant
  71. * byte first, so the number 0x12345678 becomes 0x12 0x34 0x56 0x78
  72. * when stored with big-endian byte order.
  73. */
  74. const BIG_ENDIAN = false;
  75.  
  76.  
  77. /**
  78. * Convert a short into two bytes.
  79. *
  80. * @param int the short that will be converted. The lower two bytes
  81. * will be extracted regardless of the actual size passed.
  82. *
  83. * @param PelByteOrder one of {@link LITTLE_ENDIAN} and {@link }
  84. * BIG_ENDIAN}.
  85. *
  86. * @return string the bytes representing the short.
  87. */
  88. static function shortToBytes($value, $endian) {
  89. if ($endian == self::LITTLE_ENDIAN)
  90. return chr($value) . chr($value >> 8);
  91. else
  92. return chr($value >> 8) . chr($value);
  93. }
  94.  
  95. /**
  96. * Convert a long into two bytes.
  97. *
  98. * @param int the long that will be converted. The lower four bytes
  99. * will be extracted regardless of the actual size passed.
  100. *
  101. * @param PelByteOrder one of {@link LITTLE_ENDIAN} and {@link }
  102. * BIG_ENDIAN}.
  103. *
  104. * @return string the bytes representing the long.
  105. */
  106. static function longToBytes($value, $endian) {
  107. if ($endian == self::LITTLE_ENDIAN)
  108. return (chr($value) .
  109. chr($value >> 8) .
  110. chr($value >> 16) .
  111. chr($value >> 24));
  112. else
  113. return (chr($value >> 24) .
  114. chr($value >> 16) .
  115. chr($value >> 8) .
  116. chr($value));
  117. }
  118.  
  119.  
  120. /**
  121. * Extract an unsigned byte from a string of bytes.
  122. *
  123. * @param string the bytes.
  124. *
  125. * @param int the offset. The byte found at the offset will be
  126. * returned as an integer. The must be at least one byte available
  127. * at offset.
  128. *
  129. * @return int the unsigned byte found at offset, e.g., an integer
  130. * in the range 0 to 255.
  131. */
  132. static function bytesToByte(&$bytes, $offset) {
  133. return ord($bytes{$offset});
  134. }
  135.  
  136.  
  137. /**
  138. * Extract a signed byte from bytes.
  139. *
  140. * @param string the bytes.
  141. *
  142. * @param int the offset. The byte found at the offset will be
  143. * returned as an integer. The must be at least one byte available
  144. * at offset.
  145. *
  146. * @return int the signed byte found at offset, e.g., an integer in
  147. * the range -128 to 127.
  148. */
  149. static function bytesToSByte(&$bytes, $offset) {
  150. $n = self::bytesToByte($bytes, $offset);
  151. if ($n > 127)
  152. return $n - 256;
  153. else
  154. return $n;
  155. }
  156.  
  157.  
  158. /**
  159. * Extract an unsigned short from bytes.
  160. *
  161. * @param string the bytes.
  162. *
  163. * @param int the offset. The short found at the offset will be
  164. * returned as an integer. There must be at least two bytes
  165. * available beginning at the offset given.
  166. *
  167. * @return int the unsigned short found at offset, e.g., an integer
  168. * in the range 0 to 65535.
  169. *
  170. * @param PelByteOrder one of {@link LITTLE_ENDIAN} and {@link }
  171. * BIG_ENDIAN}.
  172. */
  173. static function bytesToShort(&$bytes, $offset, $endian) {
  174. if ($endian == self::LITTLE_ENDIAN)
  175. return (ord($bytes{$offset+1}) << 8 |
  176. ord($bytes{$offset}));
  177. else
  178. return (ord($bytes{$offset}) << 8 |
  179. ord($bytes{$offset+1}));
  180. }
  181.  
  182.  
  183. /**
  184. * Extract a signed short from bytes.
  185. *
  186. * @param string the bytes.
  187. *
  188. * @param int the offset. The short found at offset will be returned
  189. * as an integer. There must be at least two bytes available
  190. * beginning at the offset given.
  191. *
  192. * @return int the signed byte found at offset, e.g., an integer in
  193. * the range -32768 to 32767.
  194. *
  195. * @param PelByteOrder one of {@link LITTLE_ENDIAN} and {@link }
  196. * BIG_ENDIAN}.
  197. */
  198. static function bytesToSShort(&$bytes, $offset, $endian) {
  199. $n = self::bytesToShort($bytes, $offset, $endian);
  200. if ($n > 32767)
  201. return $n - 65536;
  202. else
  203. return $n;
  204. }
  205.  
  206.  
  207. /**
  208. * Extract an unsigned long from bytes.
  209. *
  210. * @param string the bytes.
  211. *
  212. * @param int the offset. The long found at offset will be returned
  213. * as an integer. There must be at least four bytes available
  214. * beginning at the offset given.
  215. *
  216. * @return int the unsigned long found at offset, e.g., an integer
  217. * in the range 0 to 4294967295.
  218. *
  219. * @param PelByteOrder one of {@link LITTLE_ENDIAN} and {@link }
  220. * BIG_ENDIAN}.
  221. */
  222. static function bytesToLong(&$bytes, $offset, $endian) {
  223. if ($endian == self::LITTLE_ENDIAN)
  224. return (ord($bytes{$offset+3}) * 16777216 +
  225. ord($bytes{$offset+2}) * 65536 +
  226. ord($bytes{$offset+1}) * 256 +
  227. ord($bytes{$offset}));
  228. else
  229. return (ord($bytes{$offset}) * 16777216 +
  230. ord($bytes{$offset+1}) * 65536 +
  231. ord($bytes{$offset+2}) * 256 +
  232. ord($bytes{$offset+3}));
  233. }
  234.  
  235.  
  236. /**
  237. * Extract a signed long from bytes.
  238. *
  239. * @param string the bytes.
  240. *
  241. * @param int the offset. The long found at offset will be returned
  242. * as an integer. There must be at least four bytes available
  243. * beginning at the offset given.
  244. *
  245. * @return int the signed long found at offset, e.g., an integer in
  246. * the range -2147483648 to 2147483647.
  247. *
  248. * @param PelByteOrder one of {@link LITTLE_ENDIAN} and {@link }
  249. * BIG_ENDIAN}.
  250. */
  251. static function bytesToSLong(&$bytes, $offset, $endian) {
  252. $n = self::bytesToLong($bytes, $offset, $endian);
  253. if ($n > 2147483647)
  254. return $n - 4294967296;
  255. else
  256. return $n;
  257. }
  258.  
  259.  
  260. /**
  261. * Extract an unsigned rational from bytes.
  262. *
  263. * @param string the bytes.
  264. *
  265. * @param int the offset. The rational found at offset will be
  266. * returned as an array. There must be at least eight bytes
  267. * available beginning at the offset given.
  268. *
  269. * @return array the unsigned rational found at offset, e.g., an
  270. * array with two integers in the range 0 to 4294967295.
  271. *
  272. * @param PelByteOrder one of {@link LITTLE_ENDIAN} and {@link }
  273. * BIG_ENDIAN}.
  274. */
  275. static function bytesToRational(&$bytes, $offset, $endian) {
  276. return array(self::bytesToLong($bytes, $offset, $endian),
  277. self::bytesToLong($bytes, $offset+4, $endian));
  278. }
  279.  
  280.  
  281. /**
  282. * Extract a signed rational from bytes.
  283. *
  284. * @param string the bytes.
  285. *
  286. * @param int the offset. The rational found at offset will be
  287. * returned as an array. There must be at least eight bytes
  288. * available beginning at the offset given.
  289. *
  290. * @return array the signed rational found at offset, e.g., an array
  291. * with two integers in the range -2147483648 to 2147483647.
  292. *
  293. * @param PelByteOrder one of {@link LITTLE_ENDIAN} and {@link }
  294. * BIG_ENDIAN}.
  295. */
  296. static function bytesToSRational(&$bytes, $offset, $endian) {
  297. return array(self::bytesToSLong($bytes, $offset, $endian),
  298. self::bytesToSLong($bytes, $offset+4, $endian));
  299. }
  300.  
  301.  
  302. /**
  303. * Format bytes for dumping.
  304. *
  305. * This method is for debug output, it will format a string as a
  306. * hexadecimal dump suitable for display on a terminal. The output
  307. * is printed directly to standard out.
  308. *
  309. * @param string the bytes that will be dumped.
  310. *
  311. * @param int the maximum number of bytes to dump. If this is left
  312. * out (or left to the default of 0), then the entire string will be
  313. * dumped.
  314. */
  315. static function bytesToDump($bytes, $max = 0) {
  316. $s = strlen($bytes);
  317.  
  318. if ($max > 0)
  319. $s = min($max, $s);
  320.  
  321. $line = 24;
  322.  
  323. for ($i = 0; $i < $s; $i++) {
  324. printf('%02X ', ord($bytes{$i}));
  325. if (($i+1) % $line == 0)
  326. print("\n");
  327. }
  328. print("\n");
  329. }
  330.  
  331. }
  332.  
  333. ?>

SourceForge.net Logo Documentation generated on Wed, 21 Jul 2004 19:12:55 +0200 by phpDocumentor 1.3.0RC3