HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImfXdr.h
Go to the documentation of this file.
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright (c) Contributors to the OpenEXR Project.
4 //
5 
6 #ifndef INCLUDED_IMF_XDR_H
7 #define INCLUDED_IMF_XDR_H
8 
9 //----------------------------------------------------------------------------
10 //
11 // Xdr -- routines to convert data between the machine's native
12 // format and a machine-independent external data representation:
13 //
14 // write<R> (T &o, S v); converts a value, v, of type S
15 // into a machine-independent
16 // representation and stores the
17 // result in an output buffer, o.
18 //
19 // read<R> (T &i, S &v); reads the machine-independent
20 // representation of a value of type
21 // S from input buffer i, converts
22 // the value into the machine's native
23 // representation, and stores the result
24 // in v.
25 //
26 // size<S>(); returns the size, in bytes, of the
27 // machine-independent representation
28 // of an object of type S.
29 //
30 // The write() and read() routines are templates; data can be written
31 // to and read from any output or input buffer type T for which a helper
32 // class, R, exits. Class R must define a method to store a char array
33 // in a T, and a method to read a char array from a T:
34 //
35 // struct R
36 // {
37 // static void
38 // writeChars (T &o, const char c[/*n*/], int n)
39 // {
40 // ... // Write c[0], c[1] ... c[n-1] to output buffer o.
41 // }
42 //
43 // static void
44 // readChars (T &i, char c[/*n*/], int n)
45 // {
46 // ... // Read n characters from input buffer i
47 // // and copy them to c[0], c[1] ... c[n-1].
48 // }
49 // };
50 //
51 // Example - writing to and reading from iostreams:
52 //
53 // struct CharStreamIO
54 // {
55 // static void
56 // writeChars (ostream &os, const char c[], int n)
57 // {
58 // os.write (c, n);
59 // }
60 //
61 // static void
62 // readChars (istream &is, char c[], int n)
63 // {
64 // is.read (c, n);
65 // }
66 // };
67 //
68 // ...
69 //
70 // Xdr::write<CharStreamIO> (os, 3);
71 // Xdr::write<CharStreamIO> (os, 5.0);
72 //
73 //----------------------------------------------------------------------------
74 
75 #include "ImfNamespace.h"
76 
77 #include "IexMathExc.h"
78 #include <cstdint>
79 #include <half.h>
80 #include <limits.h>
81 
83 
84 namespace Xdr
85 {
86 
87 //-------------------------------
88 // Write data to an output stream
89 //-------------------------------
90 
91 template <class S, class T> void write (T& out, bool v);
92 
93 template <class S, class T> void write (T& out, char v);
94 
95 template <class S, class T> void write (T& out, signed char v);
96 
97 template <class S, class T> void write (T& out, unsigned char v);
98 
99 template <class S, class T> void write (T& out, signed short v);
100 
101 template <class S, class T> void write (T& out, unsigned short v);
102 
103 template <class S, class T> void write (T& out, signed int v);
104 
105 template <class S, class T> void write (T& out, unsigned int v);
106 
107 template <class S, class T> void write (T& out, int64_t v);
108 
109 template <class S, class T> void write (T& out, uint64_t v);
110 
111 template <class S, class T> void write (T& out, float v);
112 
113 template <class S, class T> void write (T& out, double v);
114 
115 template <class S, class T> void write (T& out, half v);
116 
117 template <class S, class T>
118 void write (T& out, const char v[/*n*/], int n); // fixed-size char array
119 
120 template <class S, class T>
121 void write (T& out, const char v[]); // zero-terminated string
122 
123 //-----------------------------------------
124 // Append padding bytes to an output stream
125 //-----------------------------------------
126 
127 template <class S, class T> void pad (T& out, int n); // write n padding bytes
128 
129 //-------------------------------
130 // Read data from an input stream
131 //-------------------------------
132 
133 template <class S, class T> void read (T& in, bool& v);
134 
135 template <class S, class T> void read (T& in, char& v);
136 
137 template <class S, class T> void read (T& in, signed char& v);
138 
139 template <class S, class T> void read (T& in, unsigned char& v);
140 
141 template <class S, class T> void read (T& in, signed short& v);
142 
143 template <class S, class T> void read (T& in, unsigned short& v);
144 
145 template <class S, class T> void read (T& in, signed int& v);
146 
147 template <class S, class T> void read (T& in, unsigned int& v);
148 
149 template <class S, class T> void read (T& in, int64_t& v);
150 
151 template <class S, class T> void read (T& in, uint64_t& v);
152 
153 template <class S, class T> void read (T& in, float& v);
154 
155 template <class S, class T> void read (T& in, double& v);
156 
157 template <class S, class T> void read (T& in, half& v);
158 
159 template <class S, class T>
160 void read (T& in, char v[/*n*/], int n); // fixed-size char array
161 
162 template <class S, class T>
163 void read (T& in, int n, char v[/*n*/]); // zero-terminated string
164 
165 //-------------------------------------------
166 // Skip over padding bytes in an input stream
167 //-------------------------------------------
168 
169 template <class S, class T> void skip (T& in, int n); // skip n padding bytes
170 
171 //--------------------------------------
172 // Size of the machine-independent
173 // representation of an object of type S
174 //--------------------------------------
175 
176 template <class S> int size ();
177 
178 //---------------
179 // Implementation
180 //---------------
181 
182 template <class S, class T>
183 inline void
184 writeSignedChars (T& out, const signed char c[], int n)
185 {
186  S::writeChars (out, (const char*) c, n);
187 }
188 
189 template <class S, class T>
190 inline void
191 writeUnsignedChars (T& out, const unsigned char c[], int n)
192 {
193  S::writeChars (out, (const char*) c, n);
194 }
195 
196 template <class S, class T>
197 inline void
198 readSignedChars (T& in, signed char c[], int n)
199 {
200  S::readChars (in, (char*) c, n);
201 }
202 
203 template <class S, class T>
204 inline void
205 readUnsignedChars (T& in, unsigned char c[], int n)
206 {
207  S::readChars (in, (char*) c, n);
208 }
209 
210 template <class S, class T>
211 inline void
212 write (T& out, bool v)
213 {
214  char c = !!v;
215  S::writeChars (out, &c, 1);
216 }
217 
218 template <class S, class T>
219 inline void
220 write (T& out, char v)
221 {
222  S::writeChars (out, &v, 1);
223 }
224 
225 template <class S, class T>
226 inline void
227 write (T& out, signed char v)
228 {
229  writeSignedChars<S> (out, &v, 1);
230 }
231 
232 template <class S, class T>
233 inline void
234 write (T& out, unsigned char v)
235 {
236  writeUnsignedChars<S> (out, &v, 1);
237 }
238 
239 template <class S, class T>
240 void
241 write (T& out, signed short v)
242 {
243  signed char b[2];
244 
245  b[0] = (signed char) (v);
246  b[1] = (signed char) (v >> 8);
247 
248  writeSignedChars<S> (out, b, 2);
249 }
250 
251 template <class S, class T>
252 void
253 write (T& out, unsigned short v)
254 {
255  unsigned char b[2];
256 
257  b[0] = (unsigned char) (v);
258  b[1] = (unsigned char) (v >> 8);
259 
260  writeUnsignedChars<S> (out, b, 2);
261 }
262 
263 template <class S, class T>
264 void
265 write (T& out, signed int v)
266 {
267  signed char b[4];
268 
269  b[0] = (signed char) (v);
270  b[1] = (signed char) (v >> 8);
271  b[2] = (signed char) (v >> 16);
272  b[3] = (signed char) (v >> 24);
273 
274  writeSignedChars<S> (out, b, 4);
275 }
276 
277 template <class S, class T>
278 void
279 write (T& out, unsigned int v)
280 {
281  unsigned char b[4];
282 
283  b[0] = (unsigned char) (v);
284  b[1] = (unsigned char) (v >> 8);
285  b[2] = (unsigned char) (v >> 16);
286  b[3] = (unsigned char) (v >> 24);
287 
288  writeUnsignedChars<S> (out, b, 4);
289 }
290 
291 template <class S, class T>
292 void
293 write (T& out, int64_t v)
294 {
295  signed char b[8];
296 
297  b[0] = (signed char) (v);
298  b[1] = (signed char) (v >> 8);
299  b[2] = (signed char) (v >> 16);
300  b[3] = (signed char) (v >> 24);
301  b[4] = (signed char) (v >> 32);
302  b[5] = (signed char) (v >> 40);
303  b[6] = (signed char) (v >> 48);
304  b[7] = (signed char) (v >> 56);
305 
306  writeSignedChars<S> (out, b, 8);
307 }
308 
309 template <class S, class T>
310 void
311 write (T& out, uint64_t v)
312 {
313  unsigned char b[8];
314 
315  b[0] = (unsigned char) (v);
316  b[1] = (unsigned char) (v >> 8);
317  b[2] = (unsigned char) (v >> 16);
318  b[3] = (unsigned char) (v >> 24);
319  b[4] = (unsigned char) (v >> 32);
320  b[5] = (unsigned char) (v >> 40);
321  b[6] = (unsigned char) (v >> 48);
322  b[7] = (unsigned char) (v >> 56);
323 
324  writeUnsignedChars<S> (out, b, 8);
325 }
326 
327 template <class S, class T>
328 void
329 write (T& out, float v)
330 {
331  union
332  {
333  unsigned int i;
334  float f;
335  } u;
336  u.f = v;
337 
338  unsigned char b[4];
339 
340  b[0] = (unsigned char) (u.i);
341  b[1] = (unsigned char) (u.i >> 8);
342  b[2] = (unsigned char) (u.i >> 16);
343  b[3] = (unsigned char) (u.i >> 24);
344 
345  writeUnsignedChars<S> (out, b, 4);
346 }
347 
348 template <class S, class T>
349 void
350 write (T& out, double v)
351 {
352  union
353  {
354  uint64_t i;
355  double d;
356  } u;
357  u.d = v;
358 
359  unsigned char b[8];
360 
361  b[0] = (unsigned char) (u.i);
362  b[1] = (unsigned char) (u.i >> 8);
363  b[2] = (unsigned char) (u.i >> 16);
364  b[3] = (unsigned char) (u.i >> 24);
365  b[4] = (unsigned char) (u.i >> 32);
366  b[5] = (unsigned char) (u.i >> 40);
367  b[6] = (unsigned char) (u.i >> 48);
368  b[7] = (unsigned char) (u.i >> 56);
369 
370  writeUnsignedChars<S> (out, b, 8);
371 }
372 
373 template <class S, class T>
374 inline void
375 write (T& out, half v)
376 {
377  unsigned char b[2];
378 
379  b[0] = (unsigned char) (v.bits ());
380  b[1] = (unsigned char) (v.bits () >> 8);
381 
382  writeUnsignedChars<S> (out, b, 2);
383 }
384 
385 template <class S, class T>
386 inline void
387 write (T& out, const char v[], int n) // fixed-size char array
388 {
389  S::writeChars (out, v, n);
390 }
391 
392 template <class S, class T>
393 void
394 write (T& out, const char v[]) // zero-terminated string
395 {
396  while (*v)
397  {
398  S::writeChars (out, v, 1);
399  ++v;
400  }
401 
402  S::writeChars (out, v, 1);
403 }
404 
405 template <class S, class T>
406 void
407 pad (T& out, int n) // add n padding bytes
408 {
409  for (int i = 0; i < n; i++)
410  {
411  const char c = 0;
412  S::writeChars (out, &c, 1);
413  }
414 }
415 
416 template <class S, class T>
417 inline void
418 read (T& in, bool& v)
419 {
420  char c;
421 
422  S::readChars (in, &c, 1);
423  v = !!c;
424 }
425 
426 template <class S, class T>
427 inline void
428 read (T& in, char& v)
429 {
430  S::readChars (in, &v, 1);
431 }
432 
433 template <class S, class T>
434 inline void
435 read (T& in, signed char& v)
436 {
437  readSignedChars<S> (in, &v, 1);
438 }
439 
440 template <class S, class T>
441 inline void
442 read (T& in, unsigned char& v)
443 {
444  readUnsignedChars<S> (in, &v, 1);
445 }
446 
447 template <class S, class T>
448 void
449 read (T& in, signed short& v)
450 {
451  signed char b[2];
452 
453  readSignedChars<S> (in, b, 2);
454 
455  v = (static_cast<unsigned char> (b[0]) & 0x00ff) |
456  (static_cast<unsigned char> (b[1]) << 8);
457 }
458 
459 template <class S, class T>
460 void
461 read (T& in, unsigned short& v)
462 {
463  unsigned char b[2];
464 
465  readUnsignedChars<S> (in, b, 2);
466 
467  v = (b[0] & 0x00ff) | (b[1] << 8);
468 }
469 
470 template <class S, class T>
471 void
472 read (T& in, signed int& v)
473 {
474  signed char b[4];
475 
476  readSignedChars<S> (in, b, 4);
477 
478  v = (static_cast<unsigned char> (b[0]) & 0x000000ff) |
479  ((static_cast<unsigned char> (b[1]) << 8) & 0x0000ff00) |
480  ((static_cast<unsigned char> (b[2]) << 16) & 0x00ff0000) |
481  (static_cast<unsigned char> (b[3]) << 24);
482 }
483 
484 template <class S, class T>
485 void
486 read (T& in, unsigned int& v)
487 {
488  unsigned char b[4];
489 
490  readUnsignedChars<S> (in, b, 4);
491 
492  v = (b[0] & 0x000000ff) | ((b[1] << 8) & 0x0000ff00) |
493  ((b[2] << 16) & 0x00ff0000) | (b[3] << 24);
494 }
495 
496 template <class S, class T>
497 void
498 read (T& in, int64_t& v)
499 {
500  signed char b[8];
501 
502  readSignedChars<S> (in, b, 8);
503 
504  v = (static_cast<int64_t> (b[0]) & 0x00000000000000ff) |
505  ((static_cast<int64_t> (b[1]) << 8) & 0x000000000000ff00) |
506  ((static_cast<int64_t> (b[2]) << 16) & 0x0000000000ff0000) |
507  ((static_cast<int64_t> (b[3]) << 24) & 0x00000000ff000000) |
508  ((static_cast<int64_t> (b[4]) << 32) & 0x000000ff00000000) |
509  ((static_cast<int64_t> (b[5]) << 40) & 0x0000ff0000000000) |
510  ((static_cast<int64_t> (b[6]) << 48) & 0x00ff000000000000) |
511  (static_cast<int64_t> (b[7]) << 56);
512 }
513 
514 template <class S, class T>
515 void
516 read (T& in, uint64_t& v)
517 {
518  unsigned char b[8];
519 
520  readUnsignedChars<S> (in, b, 8);
521 
522  v = ((uint64_t) b[0] & 0x00000000000000ffLL) |
523  (((uint64_t) b[1] << 8) & 0x000000000000ff00LL) |
524  (((uint64_t) b[2] << 16) & 0x0000000000ff0000LL) |
525  (((uint64_t) b[3] << 24) & 0x00000000ff000000LL) |
526  (((uint64_t) b[4] << 32) & 0x000000ff00000000LL) |
527  (((uint64_t) b[5] << 40) & 0x0000ff0000000000LL) |
528  (((uint64_t) b[6] << 48) & 0x00ff000000000000LL) |
529  ((uint64_t) b[7] << 56);
530 }
531 
532 template <class S, class T>
533 void
534 read (T& in, float& v)
535 {
536  unsigned char b[4];
537 
538  readUnsignedChars<S> (in, b, 4);
539 
540  union
541  {
542  unsigned int i;
543  float f;
544  } u;
545 
546  u.i = (b[0] & 0x000000ff) | ((b[1] << 8) & 0x0000ff00) |
547  ((b[2] << 16) & 0x00ff0000) | (b[3] << 24);
548 
549  v = u.f;
550 }
551 
552 template <class S, class T>
553 void
554 read (T& in, double& v)
555 {
556  unsigned char b[8];
557 
558  readUnsignedChars<S> (in, b, 8);
559 
560  union
561  {
562  uint64_t i;
563  double d;
564  } u;
565 
566  u.i = ((uint64_t) b[0] & 0x00000000000000ffULL) |
567  (((uint64_t) b[1] << 8) & 0x000000000000ff00ULL) |
568  (((uint64_t) b[2] << 16) & 0x0000000000ff0000ULL) |
569  (((uint64_t) b[3] << 24) & 0x00000000ff000000ULL) |
570  (((uint64_t) b[4] << 32) & 0x000000ff00000000ULL) |
571  (((uint64_t) b[5] << 40) & 0x0000ff0000000000ULL) |
572  (((uint64_t) b[6] << 48) & 0x00ff000000000000ULL) |
573  ((uint64_t) b[7] << 56);
574 
575  v = u.d;
576 }
577 
578 template <class S, class T>
579 inline void
580 read (T& in, half& v)
581 {
582  unsigned char b[2];
583 
584  readUnsignedChars<S> (in, b, 2);
585 
586  v.setBits ((b[0] & 0x00ff) | (b[1] << 8));
587 }
588 
589 template <class S, class T>
590 inline void
591 read (T& in, char v[], int n) // fixed-size char array
592 {
593  S::readChars (in, v, n);
594 }
595 
596 template <class S, class T>
597 void
598 read (T& in, int n, char v[]) // zero-terminated string
599 {
600  while (n >= 0)
601  {
602  S::readChars (in, v, 1);
603 
604  if (*v == 0) break;
605 
606  --n;
607  ++v;
608  }
609 }
610 
611 template <class S, class T>
612 void
613 skip (T& in, int n) // skip n padding bytes
614 {
615  char c[1024];
616 
617  while (n >= (int) sizeof (c))
618  {
619  if (!S::readChars (in, c, sizeof (c))) return;
620 
621  n -= sizeof (c);
622  }
623 
624  if (n >= 1) S::readChars (in, c, n);
625 }
626 
627 template <>
628 inline int
630 {
631  return 1;
632 }
633 template <>
634 inline int
636 {
637  return 1;
638 }
639 template <>
640 inline int
642 {
643  return 1;
644 }
645 template <>
646 inline int
648 {
649  return 1;
650 }
651 template <>
652 inline int
654 {
655  return 2;
656 }
657 template <>
658 inline int
660 {
661  return 2;
662 }
663 template <>
664 inline int
666 {
667  return 4;
668 }
669 template <>
670 inline int
672 {
673  return 4;
674 }
675 template <>
676 inline int
678 {
679  return 8;
680 }
681 template <>
682 inline int
684 {
685  return 8;
686 }
687 template <>
688 inline int
690 {
691  return 8;
692 }
693 template <>
694 inline int
696 {
697  return 4;
698 }
699 template <>
700 inline int
702 {
703  return 8;
704 }
705 template <>
706 inline int
708 {
709  return 2;
710 }
711 
712 } // namespace Xdr
714 
715 #if defined(OPENEXR_IMF_INTERNAL_NAMESPACE_AUTO_EXPOSE)
716 namespace Imf
717 {
718 using namespace OPENEXR_IMF_INTERNAL_NAMESPACE;
719 }
720 #endif
721 
722 #endif
#define OPENEXR_IMF_INTERNAL_NAMESPACE
Definition: OpenEXRConfig.h:46
int size()
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
Definition: ImfNamespace.h:83
void writeUnsignedChars(T &out, const unsigned char c[], int n)
Definition: ImfXdr.h:191
imath_half_bits_t half
if we're in a C-only context, alias the half bits type to half
Definition: half.h:266
void skip(T &in, int n)
Definition: ImfXdr.h:613
const GLdouble * v
Definition: glcorearb.h:837
void readUnsignedChars(T &in, unsigned char c[], int n)
Definition: ImfXdr.h:205
void read(T &in, bool &v)
Definition: ImfXdr.h:418
void pad(T &out, int n)
Definition: ImfXdr.h:407
GLdouble n
Definition: glcorearb.h:2008
GLfloat f
Definition: glcorearb.h:1926
void writeSignedChars(T &out, const signed char c[], int n)
Definition: ImfXdr.h:184
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLsizeiptr size
Definition: glcorearb.h:664
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
Definition: ImfNamespace.h:80
void readSignedChars(T &in, signed char c[], int n)
Definition: ImfXdr.h:198
void write(T &out, bool v)
Definition: ImfXdr.h:212