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 
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  {
103  compressed = compressed || (wave_packet_descr[i]->getCompressionType() > 0);
104  }
105  }
106 
107  // create file name and open file
108 
109  if (start_of_waveform_data_packet_record == 0)
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 
164  I64 position = start_of_waveform_data_packet_record + 60;
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 }
BOOL IS_LITTLE_ENDIAN()
Definition: mydefs.hpp:144
I32 decompress(I32 iPred, U32 context=0)
int BOOL
Definition: mydefs.hpp:57
#define FALSE
Definition: mydefs.hpp:133
virtual I64 tell() const =0
BOOL init(ByteStreamIn *instream)
IntegerCompressor * ic8
float F32
Definition: mydefs.hpp:51
unsigned int U32
Definition: mydefs.hpp:39
F64 get_y() const
F32 getXt() const
virtual BOOL seek(const I64 position)=0
unsigned short U16
Definition: mydefs.hpp:40
long long I64
Definition: mydefs.hpp:48
F32 getYt() const
LASwavepacket wavepacket
unsigned char U8
Definition: mydefs.hpp:41
F32 getLocation() const
F64 get_x() const
U8 getIndex() const
BOOL open(const char *file_name, I64 start_of_waveform_data_packet_record, const LASvlr_wave_packet_descr *const *wave_packet_descr)
IntegerCompressor * ic16
F32 getZt() const
virtual void getBytes(U8 *bytes, const U32 num_bytes)=0
U64 getOffset() const
int I32
Definition: mydefs.hpp:35
const LASvlr_wave_packet_descr *const * wave_packet_descr
F64 get_z() const
virtual void get32bitsLE(U8 *bytes)=0
#define TRUE
Definition: mydefs.hpp:137
ArithmeticDecoder * dec
BOOL read_waveform(const LASpoint *point)
virtual void get16bitsLE(U8 *bytes)=0


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 Mon Feb 28 2022 22:46:07