vil_stream_read.cxx
Go to the documentation of this file.
1 //:
2 // \file
3 // \brief read numbers from vil_stream
4 //
5 // Functions to read integers and floats from a vil_stream.
6 // The endianness refers to the format in the stream, not the
7 // native format of the compiler or execution environment.
8 //
9 // \author fsm
10 //
11 // \verbatim
12 // Modifications
13 // Peter Vanroose, July 2000: corrected serious bug: VXL_LITTLE_ENDIAN not needed
14 // (implementation was wrong for VXL_BIG_ENDIAN machines)
15 // Ian Scott, May 2003: rearrange explicit io, to allow for easier expansion.
16 // Peter Vanroose - 23 Oct.2003 - Added support for 64-bit int pixels
17 // \endverbatim
18 
19 #include "vil_stream_read.h"
20 #include <cassert>
21 #ifdef _MSC_VER
22 # include <vcl_msvc_warnings.h>
23 #endif
24 
25 #include <vil/vil_stream.h>
26 #include <vxl_config.h>
27 
29 {
30  vxl_uint_8 bytes[2];
31  if (s->read(bytes, sizeof bytes) != sizeof bytes) return 0;
32  return vxl_uint_16(bytes[1]) | vxl_uint_16(bytes[0]<<8);
33 }
34 
36 {
37  vxl_uint_8 bytes[2];
38  if (s->read(bytes, sizeof bytes) != sizeof bytes) return 0;
39  return vxl_uint_16(bytes[0]) | vxl_uint_16(bytes[1]<<8);
40 }
41 
42 #if VXL_HAS_INT_64
43 
44 vxl_uint_64 vil_stream_read_big_endian_uint_64(vil_stream *s)
45 {
46  vxl_byte bytes[8];
47  if (s->read(bytes, sizeof bytes) != sizeof bytes) return 0;
48  return (vxl_uint_64(bytes[0])<<56) | (vxl_uint_64(bytes[1])<<48) | (vxl_uint_64(bytes[2])<<40) | (vxl_uint_64(bytes[3])<<32)
49  | (vxl_uint_64(bytes[0])<<24) | (vxl_uint_64(bytes[1])<<16) | (vxl_uint_64(bytes[2])<< 8) | vxl_uint_64(bytes[3]);
50 }
51 
52 vxl_uint_64 vil_stream_read_little_endian_uint_64(vil_stream *s)
53 {
54  vxl_byte bytes[4];
55  if (s->read(bytes, sizeof bytes) != sizeof bytes) return 0;
56  return (vxl_uint_64(bytes[3])<<56) | (vxl_uint_64(bytes[2])<<48) | (vxl_uint_64(bytes[1])<<40) | (vxl_uint_64(bytes[0])<<32)
57  | (vxl_uint_64(bytes[3])<<24) | (vxl_uint_64(bytes[2])<<16) | (vxl_uint_64(bytes[1])<< 8) | vxl_uint_64(bytes[0]);
58 }
59 
60 #endif // VXL_HAS_INT_64
61 
63 {
64  vxl_byte bytes[4];
65  if (s->read(bytes, sizeof bytes) != sizeof bytes) return 0;
66  return (vxl_uint_32(bytes[0])<<24) | (vxl_uint_32(bytes[1])<<16) | (vxl_uint_32(bytes[2])<<8) | (vxl_uint_32(bytes[3]));
67 }
68 
70 {
71  vxl_byte bytes[4];
72  if (s->read(bytes, sizeof bytes) != sizeof bytes) return 0;
73  return (vxl_uint_32(bytes[3])<<24) | (vxl_uint_32(bytes[2])<<16) | (vxl_uint_32(bytes[1])<<8) | (vxl_uint_32(bytes[0]));
74 }
75 
77 {
78  vxl_byte bytes[4];
79  if (s->read(bytes, sizeof bytes) != sizeof bytes) return 0;
80  return (vxl_int_32(bytes[0])<<24) | (vxl_int_32(bytes[1])<<16) | (vxl_int_32(bytes[2])<<8) | (vxl_int_32(bytes[3]));
81 }
82 
84 {
85  vxl_byte bytes[4];
86  if (s->read(bytes, sizeof bytes) != sizeof bytes) return 0;
87  return (vxl_int_32(bytes[3])<<24) | (vxl_int_32(bytes[2])<<16) | (vxl_int_32(bytes[1])<<8) | (vxl_int_32(bytes[0]));
88 }
89 
90 // The following function should be moved to relevant places in vil soon
91 // This static function is only needed if it will be used below
92 #if VXL_LITTLE_ENDIAN
93 static void swap16(char *a, unsigned n)
94 {
95  char c;
96  for (unsigned i = 0; i < n * 2; i += 2)
97  {
98  c = a[i]; a[i] = a[i+1]; a[i+1] = c;
99  }
100 }
101 #endif
102 
103 #if VXL_LITTLE_ENDIAN
104 // The following function should be moved to relevant places in vil soon
105 // This static function is only needed if it will be used below
106 static void swap32(char *a, unsigned n)
107 {
108  char c;
109  for (unsigned i = 0; i < n * 4; i += 4)
110  {
111  c = a[i];
112  a[i] = a[i+3];
113  a[i+3] = c;
114  c = a[i+1];
115  a[i+1] = a[i+2];
116  a[i+2] = c;
117  }
118 }
119 #endif
120 
121 // The following function should be moved to relevant places in vil soon
123 {
124  float f;
125  is->read((char*)&f,4);
126 #if VXL_LITTLE_ENDIAN
127  swap32((char*)&f,1);
128 #endif
129  return f;
130 }
131 
132 // The following function should be moved to relevant places in vil soon
133 // Reads in n shorts, assumed to be two bytes, into data[i]
135  vxl_uint_16* data, unsigned n)
136 {
137  assert(sizeof(short)==2);
138  is->read((char*)data,n*2);
139 #if VXL_LITTLE_ENDIAN
140  swap16((char*)data,n);
141 #endif
142 }
Stream interface for VIL image loaders.
vxl_uint_32 vil_stream_read_big_endian_uint_32(vil_stream *s)
vxl_int_32 vil_stream_read_big_endian_int_32(vil_stream *s)
read numbers from vil_stream
float vil_stream_read_big_endian_float(vil_stream *is)
Reads in a 4-byte big-endian float.
virtual vil_streampos read(void *buf, vil_streampos n)=0
Read n bytes into buf. Returns number of bytes read.
void swap32(void *ptr)
vxl_int_32 vil_stream_read_little_endian_int_32(vil_stream *s)
Stream interface for VIL image loaders.
Definition: vil_stream.h:21
void vil_stream_read_big_endian_int_16(vil_stream *is, vxl_uint_16 *data, unsigned n)
Reads in n 16 bit unsigned ints.
vxl_uint_32 vil_stream_read_little_endian_uint_32(vil_stream *s)
vxl_uint_16 vil_stream_read_little_endian_uint_16(vil_stream *s)
vxl_uint_16 vil_stream_read_big_endian_uint_16(vil_stream *s)