ImfXdr.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. #ifndef INCLUDED_IMF_XDR_H
  35. #define INCLUDED_IMF_XDR_H
  36. //----------------------------------------------------------------------------
  37. //
  38. // Xdr -- routines to convert data between the machine's native
  39. // format and a machine-independent external data representation:
  40. //
  41. // write<R> (T &o, S v); converts a value, v, of type S
  42. // into a machine-independent
  43. // representation and stores the
  44. // result in an output buffer, o.
  45. //
  46. // read<R> (T &i, S &v); reads the machine-independent
  47. // representation of a value of type
  48. // S from input buffer i, converts
  49. // the value into the machine's native
  50. // representation, and stores the result
  51. // in v.
  52. //
  53. // size<S>(); returns the size, in bytes, of the
  54. // machine-independent representation
  55. // of an object of type S.
  56. //
  57. // The write() and read() routines are templates; data can be written
  58. // to and read from any output or input buffer type T for which a helper
  59. // class, R, exits. Class R must define a method to store a char array
  60. // in a T, and a method to read a char array from a T:
  61. //
  62. // struct R
  63. // {
  64. // static void
  65. // writeChars (T &o, const char c[/*n*/], int n)
  66. // {
  67. // ... // Write c[0], c[1] ... c[n-1] to output buffer o.
  68. // }
  69. //
  70. // static void
  71. // readChars (T &i, char c[/*n*/], int n)
  72. // {
  73. // ... // Read n characters from input buffer i
  74. // // and copy them to c[0], c[1] ... c[n-1].
  75. // }
  76. // };
  77. //
  78. // Example - writing to and reading from iostreams:
  79. //
  80. // struct CharStreamIO
  81. // {
  82. // static void
  83. // writeChars (ostream &os, const char c[], int n)
  84. // {
  85. // os.write (c, n);
  86. // }
  87. //
  88. // static void
  89. // readChars (istream &is, char c[], int n)
  90. // {
  91. // is.read (c, n);
  92. // }
  93. // };
  94. //
  95. // ...
  96. //
  97. // Xdr::write<CharStreamIO> (os, 3);
  98. // Xdr::write<CharStreamIO> (os, 5.0);
  99. //
  100. //----------------------------------------------------------------------------
  101. #include "ImfInt64.h"
  102. #include "IexMathExc.h"
  103. #include "half.h"
  104. #include <limits.h>
  105. #include "ImfNamespace.h"
  106. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  107. namespace Xdr {
  108. //-------------------------------
  109. // Write data to an output stream
  110. //-------------------------------
  111. template <class S, class T>
  112. void
  113. write (T &out, bool v);
  114. template <class S, class T>
  115. void
  116. write (T &out, char v);
  117. template <class S, class T>
  118. void
  119. write (T &out, signed char v);
  120. template <class S, class T>
  121. void
  122. write (T &out, unsigned char v);
  123. template <class S, class T>
  124. void
  125. write (T &out, signed short v);
  126. template <class S, class T>
  127. void
  128. write (T &out, unsigned short v);
  129. template <class S, class T>
  130. void
  131. write (T &out, signed int v);
  132. template <class S, class T>
  133. void
  134. write (T &out, unsigned int v);
  135. template <class S, class T>
  136. void
  137. write (T &out, signed long v);
  138. template <class S, class T>
  139. void
  140. write (T &out, unsigned long v);
  141. #if ULONG_MAX != 18446744073709551615LU
  142. template <class S, class T>
  143. void
  144. write (T &out, Int64 v);
  145. #endif
  146. template <class S, class T>
  147. void
  148. write (T &out, float v);
  149. template <class S, class T>
  150. void
  151. write (T &out, double v);
  152. template <class S, class T>
  153. void
  154. write (T &out, half v);
  155. template <class S, class T>
  156. void
  157. write (T &out, const char v[/*n*/], int n); // fixed-size char array
  158. template <class S, class T>
  159. void
  160. write (T &out, const char v[]); // zero-terminated string
  161. //-----------------------------------------
  162. // Append padding bytes to an output stream
  163. //-----------------------------------------
  164. template <class S, class T>
  165. void
  166. pad (T &out, int n); // write n padding bytes
  167. //-------------------------------
  168. // Read data from an input stream
  169. //-------------------------------
  170. template <class S, class T>
  171. void
  172. read (T &in, bool &v);
  173. template <class S, class T>
  174. void
  175. read (T &in, char &v);
  176. template <class S, class T>
  177. void
  178. read (T &in, signed char &v);
  179. template <class S, class T>
  180. void
  181. read (T &in, unsigned char &v);
  182. template <class S, class T>
  183. void
  184. read (T &in, signed short &v);
  185. template <class S, class T>
  186. void
  187. read (T &in, unsigned short &v);
  188. template <class S, class T>
  189. void
  190. read (T &in, signed int &v);
  191. template <class S, class T>
  192. void
  193. read (T &in, unsigned int &v);
  194. template <class S, class T>
  195. void
  196. read (T &in, signed long &v);
  197. template <class S, class T>
  198. void
  199. read (T &in, unsigned long &v);
  200. #if ULONG_MAX != 18446744073709551615LU
  201. template <class S, class T>
  202. void
  203. read (T &in, Int64 &v);
  204. #endif
  205. template <class S, class T>
  206. void
  207. read (T &in, float &v);
  208. template <class S, class T>
  209. void
  210. read (T &in, double &v);
  211. template <class S, class T>
  212. void
  213. read (T &in, half &v);
  214. template <class S, class T>
  215. void
  216. read (T &in, char v[/*n*/], int n); // fixed-size char array
  217. template <class S, class T>
  218. void
  219. read (T &in, int n, char v[/*n*/]); // zero-terminated string
  220. //-------------------------------------------
  221. // Skip over padding bytes in an input stream
  222. //-------------------------------------------
  223. template <class S, class T>
  224. void
  225. skip (T &in, int n); // skip n padding bytes
  226. //--------------------------------------
  227. // Size of the machine-independent
  228. // representation of an object of type S
  229. //--------------------------------------
  230. template <class S>
  231. int
  232. size ();
  233. //---------------
  234. // Implementation
  235. //---------------
  236. template <class S, class T>
  237. inline void
  238. writeSignedChars (T &out, const signed char c[], int n)
  239. {
  240. S::writeChars (out, (const char *) c, n);
  241. }
  242. template <class S, class T>
  243. inline void
  244. writeUnsignedChars (T &out, const unsigned char c[], int n)
  245. {
  246. S::writeChars (out, (const char *) c, n);
  247. }
  248. template <class S, class T>
  249. inline void
  250. readSignedChars (T &in, signed char c[], int n)
  251. {
  252. S::readChars (in, (char *) c, n);
  253. }
  254. template <class S, class T>
  255. inline void
  256. readUnsignedChars (T &in, unsigned char c[], int n)
  257. {
  258. S::readChars (in, (char *) c, n);
  259. }
  260. template <class S, class T>
  261. inline void
  262. write (T &out, bool v)
  263. {
  264. char c = !!v;
  265. S::writeChars (out, &c, 1);
  266. }
  267. template <class S, class T>
  268. inline void
  269. write (T &out, char v)
  270. {
  271. S::writeChars (out, &v, 1);
  272. }
  273. template <class S, class T>
  274. inline void
  275. write (T &out, signed char v)
  276. {
  277. writeSignedChars<S> (out, &v, 1);
  278. }
  279. template <class S, class T>
  280. inline void
  281. write (T &out, unsigned char v)
  282. {
  283. writeUnsignedChars<S> (out, &v, 1);
  284. }
  285. template <class S, class T>
  286. void
  287. write (T &out, signed short v)
  288. {
  289. signed char b[2];
  290. b[0] = (signed char) (v);
  291. b[1] = (signed char) (v >> 8);
  292. writeSignedChars<S> (out, b, 2);
  293. }
  294. template <class S, class T>
  295. void
  296. write (T &out, unsigned short v)
  297. {
  298. unsigned char b[2];
  299. b[0] = (unsigned char) (v);
  300. b[1] = (unsigned char) (v >> 8);
  301. writeUnsignedChars<S> (out, b, 2);
  302. }
  303. template <class S, class T>
  304. void
  305. write (T &out, signed int v)
  306. {
  307. signed char b[4];
  308. b[0] = (signed char) (v);
  309. b[1] = (signed char) (v >> 8);
  310. b[2] = (signed char) (v >> 16);
  311. b[3] = (signed char) (v >> 24);
  312. writeSignedChars<S> (out, b, 4);
  313. }
  314. template <class S, class T>
  315. void
  316. write (T &out, unsigned int v)
  317. {
  318. unsigned char b[4];
  319. b[0] = (unsigned char) (v);
  320. b[1] = (unsigned char) (v >> 8);
  321. b[2] = (unsigned char) (v >> 16);
  322. b[3] = (unsigned char) (v >> 24);
  323. writeUnsignedChars<S> (out, b, 4);
  324. }
  325. template <class S, class T>
  326. void
  327. write (T &out, signed long v)
  328. {
  329. signed char b[8];
  330. b[0] = (signed char) (v);
  331. b[1] = (signed char) (v >> 8);
  332. b[2] = (signed char) (v >> 16);
  333. b[3] = (signed char) (v >> 24);
  334. #if LONG_MAX == 2147483647
  335. if (v >= 0)
  336. {
  337. b[4] = 0;
  338. b[5] = 0;
  339. b[6] = 0;
  340. b[7] = 0;
  341. }
  342. else
  343. {
  344. b[4] = ~0;
  345. b[5] = ~0;
  346. b[6] = ~0;
  347. b[7] = ~0;
  348. }
  349. #elif LONG_MAX == 9223372036854775807L
  350. b[4] = (signed char) (v >> 32);
  351. b[5] = (signed char) (v >> 40);
  352. b[6] = (signed char) (v >> 48);
  353. b[7] = (signed char) (v >> 56);
  354. #else
  355. #error write<T> (T &out, signed long v) not implemented
  356. #endif
  357. writeSignedChars<S> (out, b, 8);
  358. }
  359. template <class S, class T>
  360. void
  361. write (T &out, unsigned long v)
  362. {
  363. unsigned char b[8];
  364. b[0] = (unsigned char) (v);
  365. b[1] = (unsigned char) (v >> 8);
  366. b[2] = (unsigned char) (v >> 16);
  367. b[3] = (unsigned char) (v >> 24);
  368. #if ULONG_MAX == 4294967295U
  369. b[4] = 0;
  370. b[5] = 0;
  371. b[6] = 0;
  372. b[7] = 0;
  373. #elif ULONG_MAX == 18446744073709551615LU
  374. b[4] = (unsigned char) (v >> 32);
  375. b[5] = (unsigned char) (v >> 40);
  376. b[6] = (unsigned char) (v >> 48);
  377. b[7] = (unsigned char) (v >> 56);
  378. #else
  379. #error write<T> (T &out, unsigned long v) not implemented
  380. #endif
  381. writeUnsignedChars<S> (out, b, 8);
  382. }
  383. #if ULONG_MAX != 18446744073709551615LU
  384. template <class S, class T>
  385. void
  386. write (T &out, Int64 v)
  387. {
  388. unsigned char b[8];
  389. b[0] = (unsigned char) (v);
  390. b[1] = (unsigned char) (v >> 8);
  391. b[2] = (unsigned char) (v >> 16);
  392. b[3] = (unsigned char) (v >> 24);
  393. b[4] = (unsigned char) (v >> 32);
  394. b[5] = (unsigned char) (v >> 40);
  395. b[6] = (unsigned char) (v >> 48);
  396. b[7] = (unsigned char) (v >> 56);
  397. writeUnsignedChars<S> (out, b, 8);
  398. }
  399. #endif
  400. template <class S, class T>
  401. void
  402. write (T &out, float v)
  403. {
  404. union {unsigned int i; float f;} u;
  405. u.f = v;
  406. unsigned char b[4];
  407. b[0] = (unsigned char) (u.i);
  408. b[1] = (unsigned char) (u.i >> 8);
  409. b[2] = (unsigned char) (u.i >> 16);
  410. b[3] = (unsigned char) (u.i >> 24);
  411. writeUnsignedChars<S> (out, b, 4);
  412. }
  413. template <class S, class T>
  414. void
  415. write (T &out, double v)
  416. {
  417. union {Int64 i; double d;} u;
  418. u.d = v;
  419. unsigned char b[8];
  420. b[0] = (unsigned char) (u.i);
  421. b[1] = (unsigned char) (u.i >> 8);
  422. b[2] = (unsigned char) (u.i >> 16);
  423. b[3] = (unsigned char) (u.i >> 24);
  424. b[4] = (unsigned char) (u.i >> 32);
  425. b[5] = (unsigned char) (u.i >> 40);
  426. b[6] = (unsigned char) (u.i >> 48);
  427. b[7] = (unsigned char) (u.i >> 56);
  428. writeUnsignedChars<S> (out, b, 8);
  429. }
  430. template <class S, class T>
  431. inline void
  432. write (T &out, half v)
  433. {
  434. unsigned char b[2];
  435. b[0] = (unsigned char) (v.bits());
  436. b[1] = (unsigned char) (v.bits() >> 8);
  437. writeUnsignedChars<S> (out, b, 2);
  438. }
  439. template <class S, class T>
  440. inline void
  441. write (T &out, const char v[], int n) // fixed-size char array
  442. {
  443. S::writeChars (out, v, n);
  444. }
  445. template <class S, class T>
  446. void
  447. write (T &out, const char v[]) // zero-terminated string
  448. {
  449. while (*v)
  450. {
  451. S::writeChars (out, v, 1);
  452. ++v;
  453. }
  454. S::writeChars (out, v, 1);
  455. }
  456. template <class S, class T>
  457. void
  458. pad (T &out, int n) // add n padding bytes
  459. {
  460. for (int i = 0; i < n; i++)
  461. {
  462. const char c = 0;
  463. S::writeChars (out, &c, 1);
  464. }
  465. }
  466. template <class S, class T>
  467. inline void
  468. read (T &in, bool &v)
  469. {
  470. char c;
  471. S::readChars (in, &c, 1);
  472. v = !!c;
  473. }
  474. template <class S, class T>
  475. inline void
  476. read (T &in, char &v)
  477. {
  478. S::readChars (in, &v, 1);
  479. }
  480. template <class S, class T>
  481. inline void
  482. read (T &in, signed char &v)
  483. {
  484. readSignedChars<S> (in, &v, 1);
  485. }
  486. template <class S, class T>
  487. inline void
  488. read (T &in, unsigned char &v)
  489. {
  490. readUnsignedChars<S> (in, &v, 1);
  491. }
  492. template <class S, class T>
  493. void
  494. read (T &in, signed short &v)
  495. {
  496. signed char b[2];
  497. readSignedChars<S> (in, b, 2);
  498. v = (b[0] & 0x00ff) |
  499. (b[1] << 8);
  500. }
  501. template <class S, class T>
  502. void
  503. read (T &in, unsigned short &v)
  504. {
  505. unsigned char b[2];
  506. readUnsignedChars<S> (in, b, 2);
  507. v = (b[0] & 0x00ff) |
  508. (b[1] << 8);
  509. }
  510. template <class S, class T>
  511. void
  512. read (T &in, signed int &v)
  513. {
  514. signed char b[4];
  515. readSignedChars<S> (in, b, 4);
  516. v = (b[0] & 0x000000ff) |
  517. ((b[1] << 8) & 0x0000ff00) |
  518. ((b[2] << 16) & 0x00ff0000) |
  519. (b[3] << 24);
  520. }
  521. template <class S, class T>
  522. void
  523. read (T &in, unsigned int &v)
  524. {
  525. unsigned char b[4];
  526. readUnsignedChars<S> (in, b, 4);
  527. v = (b[0] & 0x000000ff) |
  528. ((b[1] << 8) & 0x0000ff00) |
  529. ((b[2] << 16) & 0x00ff0000) |
  530. (b[3] << 24);
  531. }
  532. template <class S, class T>
  533. void
  534. read (T &in, signed long &v)
  535. {
  536. signed char b[8];
  537. readSignedChars<S> (in, b, 8);
  538. #if LONG_MAX == 2147483647
  539. v = (b[0] & 0x000000ff) |
  540. ((b[1] << 8) & 0x0000ff00) |
  541. ((b[2] << 16) & 0x00ff0000) |
  542. (b[3] << 24);
  543. if (( b[4] || b[5] || b[6] || b[7]) &&
  544. (~b[4] || ~b[5] || ~b[6] || ~b[7]))
  545. {
  546. throw IEX_NAMESPACE::OverflowExc ("Long int overflow - read a large "
  547. "64-bit integer in a 32-bit process.");
  548. }
  549. #elif LONG_MAX == 9223372036854775807L
  550. v = ((long) b[0] & 0x00000000000000ff) |
  551. (((long) b[1] << 8) & 0x000000000000ff00) |
  552. (((long) b[2] << 16) & 0x0000000000ff0000) |
  553. (((long) b[3] << 24) & 0x00000000ff000000) |
  554. (((long) b[4] << 32) & 0x000000ff00000000) |
  555. (((long) b[5] << 40) & 0x0000ff0000000000) |
  556. (((long) b[6] << 48) & 0x00ff000000000000) |
  557. ((long) b[7] << 56);
  558. #else
  559. #error read<T> (T &in, signed long &v) not implemented
  560. #endif
  561. }
  562. template <class S, class T>
  563. void
  564. read (T &in, unsigned long &v)
  565. {
  566. unsigned char b[8];
  567. readUnsignedChars<S> (in, b, 8);
  568. #if ULONG_MAX == 4294967295U
  569. v = (b[0] & 0x000000ff) |
  570. ((b[1] << 8) & 0x0000ff00) |
  571. ((b[2] << 16) & 0x00ff0000) |
  572. (b[3] << 24);
  573. if (b[4] || b[5] || b[6] || b[7])
  574. {
  575. throw IEX_NAMESPACE::OverflowExc ("Long int overflow - read a large "
  576. "64-bit integer in a 32-bit process.");
  577. }
  578. #elif ULONG_MAX == 18446744073709551615LU
  579. v = ((unsigned long) b[0] & 0x00000000000000ff) |
  580. (((unsigned long) b[1] << 8) & 0x000000000000ff00) |
  581. (((unsigned long) b[2] << 16) & 0x0000000000ff0000) |
  582. (((unsigned long) b[3] << 24) & 0x00000000ff000000) |
  583. (((unsigned long) b[4] << 32) & 0x000000ff00000000) |
  584. (((unsigned long) b[5] << 40) & 0x0000ff0000000000) |
  585. (((unsigned long) b[6] << 48) & 0x00ff000000000000) |
  586. ((unsigned long) b[7] << 56);
  587. #else
  588. #error read<T> (T &in, unsigned long &v) not implemented
  589. #endif
  590. }
  591. #if ULONG_MAX != 18446744073709551615LU
  592. template <class S, class T>
  593. void
  594. read (T &in, Int64 &v)
  595. {
  596. unsigned char b[8];
  597. readUnsignedChars<S> (in, b, 8);
  598. v = ((Int64) b[0] & 0x00000000000000ffLL) |
  599. (((Int64) b[1] << 8) & 0x000000000000ff00LL) |
  600. (((Int64) b[2] << 16) & 0x0000000000ff0000LL) |
  601. (((Int64) b[3] << 24) & 0x00000000ff000000LL) |
  602. (((Int64) b[4] << 32) & 0x000000ff00000000LL) |
  603. (((Int64) b[5] << 40) & 0x0000ff0000000000LL) |
  604. (((Int64) b[6] << 48) & 0x00ff000000000000LL) |
  605. ((Int64) b[7] << 56);
  606. }
  607. #endif
  608. template <class S, class T>
  609. void
  610. read (T &in, float &v)
  611. {
  612. unsigned char b[4];
  613. readUnsignedChars<S> (in, b, 4);
  614. union {unsigned int i; float f;} u;
  615. u.i = (b[0] & 0x000000ff) |
  616. ((b[1] << 8) & 0x0000ff00) |
  617. ((b[2] << 16) & 0x00ff0000) |
  618. (b[3] << 24);
  619. v = u.f;
  620. }
  621. template <class S, class T>
  622. void
  623. read (T &in, double &v)
  624. {
  625. unsigned char b[8];
  626. readUnsignedChars<S> (in, b, 8);
  627. union {Int64 i; double d;} u;
  628. u.i = ((Int64) b[0] & 0x00000000000000ffULL) |
  629. (((Int64) b[1] << 8) & 0x000000000000ff00ULL) |
  630. (((Int64) b[2] << 16) & 0x0000000000ff0000ULL) |
  631. (((Int64) b[3] << 24) & 0x00000000ff000000ULL) |
  632. (((Int64) b[4] << 32) & 0x000000ff00000000ULL) |
  633. (((Int64) b[5] << 40) & 0x0000ff0000000000ULL) |
  634. (((Int64) b[6] << 48) & 0x00ff000000000000ULL) |
  635. ((Int64) b[7] << 56);
  636. v = u.d;
  637. }
  638. template <class S, class T>
  639. inline void
  640. read (T &in, half &v)
  641. {
  642. unsigned char b[2];
  643. readUnsignedChars<S> (in, b, 2);
  644. v.setBits ((b[0] & 0x00ff) | (b[1] << 8));
  645. }
  646. template <class S, class T>
  647. inline void
  648. read (T &in, char v[], int n) // fixed-size char array
  649. {
  650. S::readChars (in, v, n);
  651. }
  652. template <class S, class T>
  653. void
  654. read (T &in, int n, char v[]) // zero-terminated string
  655. {
  656. while (n >= 0)
  657. {
  658. S::readChars (in, v, 1);
  659. if (*v == 0)
  660. break;
  661. --n;
  662. ++v;
  663. }
  664. }
  665. template <class S, class T>
  666. void
  667. skip (T &in, int n) // skip n padding bytes
  668. {
  669. char c[1024];
  670. while (n >= (int) sizeof (c))
  671. {
  672. if (!S::readChars (in, c, sizeof (c)))
  673. return;
  674. n -= sizeof (c);
  675. }
  676. if (n >= 1)
  677. S::readChars (in, c, n);
  678. }
  679. template <> inline int size <bool> () {return 1;}
  680. template <> inline int size <char> () {return 1;}
  681. template <> inline int size <signed char> () {return 1;}
  682. template <> inline int size <unsigned char> () {return 1;}
  683. template <> inline int size <signed short> () {return 2;}
  684. template <> inline int size <unsigned short> () {return 2;}
  685. template <> inline int size <signed int> () {return 4;}
  686. template <> inline int size <unsigned int> () {return 4;}
  687. template <> inline int size <signed long> () {return 8;}
  688. template <> inline int size <unsigned long> () {return 8;}
  689. template <> inline int size <unsigned long long> () {return 8;}
  690. template <> inline int size <float> () {return 4;}
  691. template <> inline int size <double> () {return 8;}
  692. template <> inline int size <half> () {return 2;}
  693. } // namespace Xdr
  694. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  695. #if defined (OPENEXR_IMF_INTERNAL_NAMESPACE_AUTO_EXPOSE)
  696. namespace Imf{using namespace OPENEXR_IMF_INTERNAL_NAMESPACE;}
  697. #endif
  698. #endif