Source for file PelEntryRational.php

Documentation is available at PelEntryRational.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. /* PelEntryRational.php,v 1.8 2004/06/28 20:33:00 gimpster Exp */
  25.  
  26.  
  27. /**
  28. * Classes used to manipulate rational numbers.
  29. *
  30. * @author Martin Geisler <gimpster@users.sourceforge.net>
  31. * @version 1.8
  32. * @date 2004/06/28 20:33:00
  33. * @license http://www.gnu.org/licenses/gpl.html GNU General Public
  34. * License (GPL)
  35. * @package PEL
  36. */
  37.  
  38. /**#@+ Required class definitions. */
  39. ('PelEntryLong.php');
  40. /**#@-*/ * Class for holding unsigned rational numbers.
  41. *
  42. * This class can hold rational numbers, consisting of a enumerator
  43. * and denominator both of which are of type unsigned long. Each
  44. * rational is represented by an array with just two entries: the
  45. * enumerator and the denominator, in that order.
  46. *
  47. * The class can hold either just a single rational or an array of
  48. * rationals. The class will be used to manipulate any of the EXIF
  49. * tags which can have format {@link PelFormat::RATIONAL} like in
  50. * this example:
  51. *
  52. * <code>
  53. * $resolution = $ifd->getEntry(PelTag::X_RESOLUTION);
  54. * $resolution->setValue(array(1, 300));
  55. * </code>
  56. *
  57. * Here the x-resolution is adjusted to 1/300, which will be 300 DPI,
  58. * unless the {@link PelTag::RESOLUTION_UNIT resolution unit} is
  59. * set to something different than 2 which means inches.
  60. *
  61. * @author Martin Geisler <gimpster@users.sourceforge.net>
  62. * @package PEL
  63. */
  64. class PelEntryRational extends PelEntryLong {
  65.  
  66. /**
  67. * Make a new entry that can hold an unsigned rational.
  68. *
  69. * @param PelTag the tag which this entry represents. This
  70. * should be one of the constants defined in {@link PelTag},
  71. * e.g., {@link PelTag::X_RESOLUTION}, or any other tag which can
  72. * have format {@link PelFormat::RATIONAL}.
  73. *
  74. * @param array $value... the rational(s) that this entry will
  75. * represent. The arguments passed must obey the same rules as the
  76. * argument to {@link setValue}, namely that each argument should be
  77. * an array with two entries, both of which must be within range of
  78. * an unsigned long (32 bit), that is between 0 and 4294967295
  79. * (inclusive). If not, then a {@link PelOverflowException} will be
  80. * thrown.
  81. */
  82. function __construct($tag /* ... */) {
  83. $this->tag = $tag;
  84. $this->format = PelFormat::RATIONAL;
  85. $this->dimension = 2;
  86. $this->min = 0;
  87. $this->max = 4294967295;
  88.  
  89. $value = func_get_args();
  90. array_shift($value);
  91. $this->setValueArray($value);
  92. }
  93.  
  94.  
  95. /**
  96. * Format a rational number.
  97. *
  98. * The rational will be returned as a string with a slash '/'
  99. * between the enumerator and denominator.
  100. *
  101. * @param array the rational which will be formatted.
  102. *
  103. * @param boolean not used.
  104. *
  105. * @return string the rational formatted as a string suitable for
  106. * display.
  107. */
  108. function formatNumber($number, $brief = false) {
  109. return $number[0] . '/' . $number[1];
  110. }
  111.  
  112.  
  113. /**
  114. * Get the value of an entry as text.
  115. *
  116. * The value will be returned in a format suitable for presentation,
  117. * e.g., rationals will be returned as 'x/y', ASCII strings will be
  118. * returned as themselves etc.
  119. *
  120. * @param boolean some values can be returned in a long or more
  121. * brief form, and this parameter controls that.
  122. *
  123. * @return string the value as text.
  124. */
  125. function getText($brief = false) {
  126. if (isset($this->value[0]))
  127. $v = $this->value[0];
  128. switch ($this->tag) {
  129. case PelTag::FNUMBER:
  130. //CC (e->components, 1, v);
  131. return Pel::fmt('f/%.01f', $v[0]/$v[1]);
  132.  
  133. case PelTag::APERTURE_VALUE:
  134. //CC (e->components, 1, v);
  135. //if (!v_rat.denominator) return (NULL);
  136. return Pel::fmt('f/%.01f', pow(2, $v[0]/$v[1]/2));
  137.  
  138. case PelTag::FOCAL_LENGTH:
  139. //CC (e->components, 1, v);
  140. //if (!v_rat.denominator) return (NULL);
  141. return Pel::fmt('%.1f mm', $v[0]/$v[1]);
  142. case PelTag::SUBJECT_DISTANCE:
  143. //CC (e->components, 1, v);
  144. //if (!v_rat.denominator) return (NULL);
  145. return Pel::fmt('%.1f m', $v[0]/$v[1]);
  146.  
  147. case PelTag::EXPOSURE_TIME:
  148. //CC (e->components, 1, v);
  149. //if (!v_rat.denominator) return (NULL);
  150. if ($v[0]/$v[1] < 1)
  151. return Pel::fmt('1/%d sec.', $v[1]/$v[0]);
  152. else
  153. return Pel::fmt('%d sec.', $v[0]/$v[1]);
  154. default:
  155. return parent::getText($brief);
  156. }
  157. }
  158. }
  159.  
  160.  
  161. /**
  162. * Class for holding signed rational numbers.
  163. *
  164. * This class can hold rational numbers, consisting of a enumerator
  165. * and denominator both of which are of type unsigned long. Each
  166. * rational is represented by an array with just two entries: the
  167. * enumerator and the denominator, in that order.
  168. *
  169. * The class can hold either just a single rational or an array of
  170. * rationals. The class will be used to manipulate any of the EXIF
  171. * tags which can have format {@link PelFormat::SRATIONAL}.
  172. *
  173. * @author Martin Geisler <gimpster@users.sourceforge.net>
  174. * @package PEL
  175. */
  176. class PelEntrySRational extends PelEntrySLong {
  177.  
  178. /**
  179. * Make a new entry that can hold a signed rational.
  180. *
  181. * @param PelTag the tag which this entry represents. This
  182. * should be one of the constants defined in {@link PelTag},
  183. * e.g., {@link PelTag::SHUTTER_SPEED_VALUE}, or any other tag
  184. * which can have format {@link PelFormat::SRATIONAL}.
  185. *
  186. * @param array $value... the rational(s) that this entry will
  187. * represent. The arguments passed must obey the same rules as the
  188. * argument to {@link setValue}, namely that each argument should be
  189. * an array with two entries, both of which must be within range of
  190. * a signed long (32 bit), that is between -2147483648 and
  191. * 2147483647 (inclusive). If not, then a {@link }
  192. * PelOverflowException} will be thrown.
  193. */
  194. function __construct($tag) {
  195. $this->tag = $tag;
  196. $this->format = PelFormat::SRATIONAL;
  197. $this->dimension = 2;
  198. $this->min = -2147483648;
  199. $this->max = 2147483647;
  200.  
  201. $value = func_get_args();
  202. array_shift($value);
  203. $this->setValueArray($value);
  204. }
  205.  
  206.  
  207. /**
  208. * Format a rational number.
  209. *
  210. * The rational will be returned as a string with a slash '/'
  211. * between the enumerator and denominator. Care is taken to display
  212. * '-1/2' instead of the mathematically equivalent '1/-2'.
  213. *
  214. * @param array the rational which will be formatted.
  215. *
  216. * @param boolean not used.
  217. *
  218. * @return string the rational formatted as a string suitable for
  219. * display.
  220. */
  221. function formatNumber($number, $brief = false) {
  222. if ($number[1] < 0)
  223. /* Turn output like 1/-2 into -1/2. */
  224. return (-$number[0]) . '/' . (-$number[1]);
  225. else
  226. return $number[0] . '/' . $number[1];
  227. }
  228.  
  229.  
  230. /**
  231. * Get the value of an entry as text.
  232. *
  233. * The value will be returned in a format suitable for presentation,
  234. * e.g., rationals will be returned as 'x/y', ASCII strings will be
  235. * returned as themselves etc.
  236. *
  237. * @param boolean some values can be returned in a long or more
  238. * brief form, and this parameter controls that.
  239. *
  240. * @return string the value as text.
  241. */
  242. function getText($brief = false) {
  243. if (isset($this->value[0]))
  244. $v = $this->value[0];
  245.  
  246. switch ($this->tag) {
  247. case PelTag::SHUTTER_SPEED_VALUE:
  248. //CC (e->components, 1, v);
  249. //if (!v_srat.denominator) return (NULL);
  250. return Pel::fmt('%.0f/%.0f sec. (APEX: %d)',
  251. $v[0], $v[1], pow(sqrt(2), $v[0]/$v[1]));
  252.  
  253. case PelTag::BRIGHTNESS_VALUE:
  254. //CC (e->components, 1, v);
  255. //
  256. // TODO: figure out the APEX thing, or remove this so that it's
  257. // handled by the default clause at the bottom.
  258. return sprintf('%d/%d', $v[0], $v[1]);
  259. //FIXME: How do I calculate the APEX value?
  260.  
  261. case PelTag::EXPOSURE_BIAS_VALUE:
  262. //CC (e->components, 1, v);
  263. //if (!v_srat.denominator) return (NULL);
  264. return sprintf('%s%.01f', $v[0]*$v[1] > 0 ? '+' : '', $v[0]/$v[1]);
  265.  
  266. default:
  267. return parent::getText($brief);
  268. }
  269. }
  270.  
  271. }
  272.  
  273. ?>

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