laswaveform13reader.cpp
Go to the documentation of this file.
1 /*
2 ===============================================================================
3 
4  FILE: laswaveform13reader.cpp
5 
6  CONTENTS:
7 
8  see corresponding header file
9 
10  PROGRAMMERS:
11 
12  martin.isenburg@gmail.com
13 
14  COPYRIGHT:
15 
16  (c) 2007-2011, Martin Isenburg, LASSO - tools to catch reality
17 
18  This is free software; you can redistribute and/or modify it under the
19  terms of the GNU Lesser General Licence as published by the Free Software
20  Foundation. See the COPYING file for more information.
21 
22  This software is distributed WITHOUT ANY WARRANTY and without even the
23  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24 
25  CHANGE HISTORY:
26 
27  see corresponding header file
28 
29 ===============================================================================
30 */
31 #include "laswaveform13reader.hpp"
32 
33 #include "bytestreamin_file.hpp"
34 #include "arithmeticdecoder.hpp"
35 #include "integercompressor.hpp"
36 
38 {
39  nbits = 0;
40  nsamples = 0;
41  temporal = 0;
42  location = 0;
43  XYZt[0] = XYZt[1] = XYZt[2] = 0;
44  XYZreturn[0] = XYZreturn[1] = XYZreturn[2] = 0;
45 
46  s_count = 0;
47  XYZsample[0] = XYZsample[1] = XYZsample[2] = 0;
48  sample = 0;
49 
50  sampleMin = 0;
51  sampleMax = 0;
52 
53  samples = 0;
54 
55  size = 0;
56  compressed = FALSE;
58  file = 0;
59  stream = 0;
60  last_position = 0;
62  dec = 0;
63  ic8 = 0;
64  ic16 = 0;
65 }
66 
68 {
69  if (samples) delete [] samples;
70  if (dec) delete dec;
71  if (ic8) delete ic8;
72  if (ic16) delete ic16;
73 }
74 
76 {
77  return compressed;
78 }
79 
80 BOOL LASwaveform13reader::open(const char* file_name, I64 start_of_waveform_data_packet_record, const LASvlr_wave_packet_descr * const * wave_packet_descr)
81 {
82  if (file_name == 0)
83  {
84  fprintf(stderr,"ERROR: file name pointer is zero\n");
85  return FALSE;
86  }
87 
88  if (wave_packet_descr == 0)
89  {
90  fprintf(stderr,"ERROR: wave packet descriptor pointer is zero\n");
91  return FALSE;
92  }
93 
94  // check if compressed or not
95 
96  I32 i;
97  compressed = FALSE;
98 
99  for (i = 0; i < 256; i++)
100  {
101  if (wave_packet_descr[i])
102  {
104  }
105  }
106 
107  // create file name and open file
108 
110  {
111  if (!compressed && (strstr(".wdp", file_name) || strstr(".WDP", file_name)))
112  {
113  file = fopen(file_name, "rb");
114  }
115  else if (compressed && (strstr(".wdz", file_name) || strstr(".WDZ", file_name)))
116  {
117  file = fopen(file_name, "rb");
118  }
119  else
120  {
121  char* file_name_temp = strdup(file_name);
122  int len = strlen(file_name_temp);
123  if ((file_name_temp[len-3] == 'L') || (file_name_temp[len-3] == 'W'))
124  {
125  file_name_temp[len-3] = 'W';
126  file_name_temp[len-2] = 'D';
127  file_name_temp[len-1] = (compressed ? 'Z' : 'P');
128  }
129  else
130  {
131  file_name_temp[len-3] = 'w';
132  file_name_temp[len-2] = 'd';
133  file_name_temp[len-1] = (compressed ? 'z' : 'p');
134  }
135  file = fopen(file_name_temp, "rb");
136  free(file_name_temp);
137  }
138  }
139  else
140  {
141  file = fopen(file_name, "rb");
142  }
143 
144  if (file == 0)
145  {
146  fprintf(stderr, "ERROR: cannot open waveform file '%s'\n", file_name);
147  return FALSE;
148  }
149 
150  if (IS_LITTLE_ENDIAN())
151  {
153  }
154  else
155  {
157  }
158 
159  this->start_of_waveform_data_packet_record = start_of_waveform_data_packet_record;
160  this->wave_packet_descr = wave_packet_descr;
161 
162  // attempt waveform descriptor cross-check
163 
165  stream->seek(position);
166 
167  char magic[25];
168  try { stream->getBytes((U8*)magic, 24); } catch(...)
169  {
170  fprintf(stderr,"ERROR: reading waveform descriptor cross-check\n");
171  return FALSE;
172  }
173 
174  if (strncmp(magic, "LAStools waveform ", 18) == 0)
175  {
176  // do waveform descriptor cross-check
177 
178  U16 i, number;
179  try { stream->get16bitsLE((U8*)&number); } catch(...)
180  {
181  fprintf(stderr,"ERROR: reading number of waveform descriptors\n");
182  return FALSE;
183  }
184  for (i = 0; i < number; i++)
185  {
186  U16 index;
187  try { stream->get16bitsLE((U8*)&index); } catch(...)
188  {
189  fprintf(stderr,"ERROR: reading index of waveform descriptor %d\n", i);
190  return FALSE;
191  }
192  if (index > 255)
193  {
194  fprintf(stderr,"ERROR: cross-check - index %d of waveform descriptor %d out-of-range\n", index, i);
195  return FALSE;
196  }
197  if (wave_packet_descr[index] == 0)
198  {
199  fprintf(stderr,"WARNING: cross-check - waveform descriptor %d with index %d unknown\n", i, index);
200  I32 dummy;
201  try { stream->get32bitsLE((U8*)&dummy); } catch(...)
202  {
203  fprintf(stderr,"ERROR: cross-check - reading rest of waveform descriptor %d\n", i);
204  return FALSE;
205  }
206  continue;
207  }
208  U8 compression;
209  try { stream->getBytes(&compression, 1); } catch(...)
210  {
211  fprintf(stderr,"ERROR: reading compression of waveform descriptor %d\n", i);
212  return FALSE;
213  }
214  if (compression != wave_packet_descr[index]->getCompressionType())
215  {
216  fprintf(stderr,"ERROR: cross-check - compression %d %d of waveform descriptor %d with index %d is different\n", compression, wave_packet_descr[index]->getCompressionType(), i, index);
217  return FALSE;
218  }
219  U8 nbits;
220  try { stream->getBytes(&nbits, 1); } catch(...)
221  {
222  fprintf(stderr,"ERROR: reading nbits of waveform descriptor %d\n", i);
223  return FALSE;
224  }
225  if (nbits != wave_packet_descr[index]->getBitsPerSample())
226  {
227  fprintf(stderr,"ERROR: cross-check - nbits %d %d of waveform descriptor %d with index %d is different\n", nbits, wave_packet_descr[index]->getBitsPerSample(), i, index);
228  return FALSE;
229  }
230  U16 nsamples;
231  try { stream->get16bitsLE((U8*)&nsamples); } catch(...)
232  {
233  fprintf(stderr,"ERROR: reading nsamples of waveform descriptor %d\n", i);
234  return FALSE;
235  }
236  if (nsamples != wave_packet_descr[index]->getNumberOfSamples())
237  {
238  fprintf(stderr,"ERROR: cross-check - nsamples %d %d of waveform descriptor %d with index %d is different\n", nsamples, wave_packet_descr[index]->getNumberOfSamples(), i, index);
239  return FALSE;
240  }
241  }
242  }
243 
245 
246  // create decompressor
247 
248  if (compressed)
249  {
250  if (dec == 0) dec = new ArithmeticDecoder();
251  if (ic8 == 0) ic8 = new IntegerCompressor(dec, 8);
252  if (ic16 == 0) ic16 = new IntegerCompressor(dec, 16);
253  }
254  return TRUE;
255 }
256 
258 {
259  U32 index = point->wavepacket.getIndex();
260  if (index == 0)
261  {
262  return FALSE;
263  }
264 
266  if ((nbits != 8) && (nbits != 16))
267  {
268  fprintf(stderr, "ERROR: waveform with %d bits per samples not supported yet\n", nbits);
269  return FALSE;
270  }
271 
273  if (nsamples == 0)
274  {
275  fprintf(stderr, "ERROR: waveform has no samples\n");
276  return FALSE;
277  }
278 
280  location = point->wavepacket.getLocation();
281 
282  XYZt[0] = point->wavepacket.getXt();
283  XYZt[1] = point->wavepacket.getYt();
284  XYZt[2] = point->wavepacket.getZt();
285 
286  XYZreturn[0] = point->get_x();
287  XYZreturn[1] = point->get_y();
288  XYZreturn[2] = point->get_z();
289 
290  // alloc data
291 
292  if (size < ((nbits/8) * nsamples))
293  {
294  if (samples) delete [] samples;
295  samples = new U8[((nbits/8) * nsamples)];
296  }
297 
298  size = ((nbits/8) * nsamples);
299 
300  // read waveform
301 
303  stream->seek(position);
304 
305  if (wave_packet_descr[index]->getCompressionType() == 0)
306  {
307  try { stream->getBytes(samples, size); } catch(...)
308  {
309  fprintf(stderr, "ERROR: cannot read %u bytes for waveform with %u samples of %u bits\n", size, nsamples, nbits);
310  return FALSE;
311  }
312  }
313  else
314  {
315  if (nbits == 8)
316  {
317  stream->getBytes(samples, 1);
318  dec->init(stream);
320  for (s_count = 1; s_count < nsamples; s_count++)
321  {
323  }
324  }
325  else
326  {
327  stream->getBytes(samples, 2);
328  dec->init(stream);
330  for (s_count = 1; s_count < nsamples; s_count++)
331  {
333  }
334  }
335  dec->done();
336  }
337 
338  s_count = 0;
339  return TRUE;
340 }
341 
343 {
344  if (nbits == 8)
345  {
346  sampleMin = samples[0];
347  sampleMax = samples[0];
348  for (s_count = 1; s_count < nsamples; s_count++)
349  {
352  }
353  }
354  else
355  {
356  sampleMin = ((U16*)samples)[0];
357  sampleMax = ((U16*)samples)[0];
358  for (s_count = 1; s_count < nsamples; s_count++)
359  {
361  else if (((U16*)samples)[s_count] > sampleMax) sampleMax = ((U16*)samples)[s_count];
362  }
363  }
364  s_count = 0;
365  return (s_count < nsamples);
366 }
367 
369 {
370  if (s_count < nsamples)
371  {
372  if (nbits == 8)
373  {
375  }
376  else
377  {
378  sample = ((U16*)samples)[s_count];
379  }
380  s_count++;
381  return TRUE;
382  }
383  return FALSE;
384 }
385 
387 {
388  if (nbits == 8)
389  {
390  sampleMin = samples[0];
391  sampleMax = samples[0];
392  for (s_count = 1; s_count < nsamples; s_count++)
393  {
396  }
397  }
398  else
399  {
400  sampleMin = ((U16*)samples)[0];
401  sampleMax = ((U16*)samples)[0];
402  for (s_count = 1; s_count < nsamples; s_count++)
403  {
405  else if (((U16*)samples)[s_count] > sampleMax) sampleMax = ((U16*)samples)[s_count];
406  }
407  }
408  s_count = 0;
409  return (s_count < nsamples);
410 }
411 
413 {
414  if (s_count < nsamples)
415  {
416  F32 dist = location - s_count*temporal;
417  XYZsample[0] = XYZreturn[0] + dist*XYZt[0];
418  XYZsample[1] = XYZreturn[1] + dist*XYZt[1];
419  XYZsample[2] = XYZreturn[2] + dist*XYZt[2];
420  if (nbits == 8)
421  {
423  }
424  else
425  {
426  sample = ((U16*)samples)[s_count];
427  }
428  s_count++;
429  return TRUE;
430  }
431  return FALSE;
432 }
433 
435 {
436  if (stream)
437  {
438  delete stream;
439  stream = 0;
440  }
441  if (file)
442  {
443  fclose(file);
444  file = 0;
445  }
446 }
ByteStreamIn::tell
virtual I64 tell() const =0
LASwaveform13reader::stream
ByteStreamIn * stream
Definition: laswaveform13reader.hpp:83
LASwaveform13reader::XYZreturn
F64 XYZreturn[3]
Definition: laswaveform13reader.hpp:50
LASwaveform13reader::~LASwaveform13reader
~LASwaveform13reader()
Definition: laswaveform13reader.cpp:67
LASwaveform13reader::nbits
U32 nbits
Definition: laswaveform13reader.hpp:45
LASpoint
Definition: lasdefinitions.hpp:472
LASwaveform13reader::temporal
U32 temporal
Definition: laswaveform13reader.hpp:47
LASwaveform13reader::open
BOOL open(const char *file_name, I64 start_of_waveform_data_packet_record, const LASvlr_wave_packet_descr *const *wave_packet_descr)
Definition: laswaveform13reader.cpp:80
LASwavepacket::getZt
F32 getZt() const
Definition: lasdefinitions.hpp:76
ArithmeticDecoder::init
BOOL init(ByteStreamIn *instream)
Definition: arithmeticdecoder.cpp:82
I64
long long I64
Definition: mydefs.hpp:48
LASvlr_wave_packet_descr
Definition: lasdefinitions.hpp:917
I32
int I32
Definition: mydefs.hpp:35
LASwaveform13reader::last_position
I64 last_position
Definition: laswaveform13reader.hpp:85
LASpoint::wavepacket
LASwavepacket wavepacket
Definition: lasdefinitions.hpp:499
TRUE
#define TRUE
Definition: mydefs.hpp:137
LASwaveform13reader::get_samples
BOOL get_samples()
Definition: laswaveform13reader.cpp:342
LASwaveform13reader::compressed
BOOL compressed
Definition: laswaveform13reader.hpp:79
LASwaveform13reader::get_samples_xyz
BOOL get_samples_xyz()
Definition: laswaveform13reader.cpp:386
ArithmeticDecoder::done
void done()
Definition: arithmeticdecoder.cpp:94
LASwaveform13reader::sampleMax
U32 sampleMax
Definition: laswaveform13reader.hpp:58
LASwaveform13reader::size
U32 size
Definition: laswaveform13reader.hpp:80
LASwaveform13reader::LASwaveform13reader
LASwaveform13reader()
Definition: laswaveform13reader.cpp:37
LASwaveform13reader::s_count
U32 s_count
Definition: laswaveform13reader.hpp:54
LASwavepacket::getYt
F32 getYt() const
Definition: lasdefinitions.hpp:75
ArithmeticDecoder
Definition: arithmeticdecoder.hpp:39
LASwaveform13reader::read_waveform
BOOL read_waveform(const LASpoint *point)
Definition: laswaveform13reader.cpp:257
LASvlr_wave_packet_descr::getTemporalSpacing
U32 getTemporalSpacing() const
Definition: lasdefinitions.hpp:925
ByteStreamIn::seek
virtual BOOL seek(const I64 position)=0
ByteStreamIn::get16bitsLE
virtual void get16bitsLE(U8 *bytes)=0
LASwaveform13reader::location
F32 location
Definition: laswaveform13reader.hpp:48
LASwaveform13reader::XYZt
F32 XYZt[3]
Definition: laswaveform13reader.hpp:49
ByteStreamIn::get32bitsLE
virtual void get32bitsLE(U8 *bytes)=0
LASwavepacket::getXt
F32 getXt() const
Definition: lasdefinitions.hpp:74
LASpoint::get_y
F64 get_y() const
Definition: lasdefinitions.hpp:812
LASwaveform13reader::ic16
IntegerCompressor * ic16
Definition: laswaveform13reader.hpp:88
LASwaveform13reader::samples
U8 * samples
Definition: laswaveform13reader.hpp:60
U16
unsigned short U16
Definition: mydefs.hpp:40
LASwaveform13reader::XYZsample
F64 XYZsample[3]
Definition: laswaveform13reader.hpp:52
LASwaveform13reader::file
FILE * file
Definition: laswaveform13reader.hpp:82
LASwaveform13reader::sampleMin
U32 sampleMin
Definition: laswaveform13reader.hpp:57
laswaveform13reader.hpp
LASwaveform13reader::ic8
IntegerCompressor * ic8
Definition: laswaveform13reader.hpp:87
LASvlr_wave_packet_descr::getCompressionType
U8 getCompressionType() const
Definition: lasdefinitions.hpp:923
U8
unsigned char U8
Definition: mydefs.hpp:41
BOOL
int BOOL
Definition: mydefs.hpp:57
FALSE
#define FALSE
Definition: mydefs.hpp:133
F32
float F32
Definition: mydefs.hpp:51
ByteStreamInFileLE
Definition: bytestreamin_file.hpp:65
integercompressor.hpp
bytestreamin_file.hpp
LASwavepacket::getLocation
F32 getLocation() const
Definition: lasdefinitions.hpp:73
LASwaveform13reader::start_of_waveform_data_packet_record
I64 start_of_waveform_data_packet_record
Definition: laswaveform13reader.hpp:84
LASwaveform13reader::nsamples
U32 nsamples
Definition: laswaveform13reader.hpp:46
LASwaveform13reader::sample
U32 sample
Definition: laswaveform13reader.hpp:55
IS_LITTLE_ENDIAN
BOOL IS_LITTLE_ENDIAN()
Definition: mydefs.hpp:144
U32
unsigned int U32
Definition: mydefs.hpp:39
LASwavepacket::getIndex
U8 getIndex() const
Definition: lasdefinitions.hpp:70
LASvlr_wave_packet_descr::getNumberOfSamples
U32 getNumberOfSamples() const
Definition: lasdefinitions.hpp:924
arithmeticdecoder.hpp
LASpoint::get_z
F64 get_z() const
Definition: lasdefinitions.hpp:813
LASwaveform13reader::is_compressed
BOOL is_compressed() const
Definition: laswaveform13reader.cpp:75
LASpoint::get_x
F64 get_x() const
Definition: lasdefinitions.hpp:811
IntegerCompressor::decompress
I32 decompress(I32 iPred, U32 context=0)
Definition: integercompressor.cpp:319
LASwaveform13reader::has_samples_xyz
BOOL has_samples_xyz()
Definition: laswaveform13reader.cpp:412
LASwaveform13reader::has_samples
BOOL has_samples()
Definition: laswaveform13reader.cpp:368
IntegerCompressor::initDecompressor
void initDecompressor()
Definition: integercompressor.cpp:274
ByteStreamIn::getBytes
virtual void getBytes(U8 *bytes, const U32 num_bytes)=0
LASvlr_wave_packet_descr::getBitsPerSample
U8 getBitsPerSample() const
Definition: lasdefinitions.hpp:922
IntegerCompressor
Definition: integercompressor.hpp:53
LASwaveform13reader::wave_packet_descr
const LASvlr_wave_packet_descr *const * wave_packet_descr
Definition: laswaveform13reader.hpp:81
LASwaveform13reader::dec
ArithmeticDecoder * dec
Definition: laswaveform13reader.hpp:86
LASwaveform13reader::close
void close()
Definition: laswaveform13reader.cpp:434
ByteStreamInFileBE
Definition: bytestreamin_file.hpp:85
LASwavepacket::getOffset
U64 getOffset() const
Definition: lasdefinitions.hpp:71


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:23