-
Notifications
You must be signed in to change notification settings - Fork 27
/
LASzip.cs
756 lines (720 loc) · 18.6 KB
/
LASzip.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
//===============================================================================
//
// FILE: laszip.cs
//
// CONTENTS:
//
// Contains LASzip (chunk) structs as well as the IDs of the currently
// supported entropy coding scheme
//
// PROGRAMMERS:
//
// [email protected] - http://rapidlasso.com
//
// COPYRIGHT:
//
// (c) 2005-2012, martin isenburg, rapidlasso - tools to catch reality
// (c) of the C# port 2014 by Shinta <[email protected]>
//
// This is free software; you can redistribute and/or modify it under the
// terms of the GNU Lesser General Licence as published by the Free Software
// Foundation. See the COPYING file for more information.
//
// This software is distributed WITHOUT ANY WARRANTY and without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// CHANGE HISTORY: omitted for easier Copy&Paste (pls see the original)
//
//===============================================================================
using System;
using System.Diagnostics;
namespace laszip.net
{
class LASzip
{
public const int VERSION_MAJOR=2;
public const int VERSION_MINOR=2;
public const int VERSION_REVISION=0;
public const int VERSION_BUILD_DATE=140907;
public const int COMPRESSOR_NONE=0;
public const int COMPRESSOR_POINTWISE=1;
public const int COMPRESSOR_POINTWISE_CHUNKED=2;
public const int COMPRESSOR_TOTAL_NUMBER_OF=3;
public const int COMPRESSOR_CHUNKED=COMPRESSOR_POINTWISE_CHUNKED;
public const int COMPRESSOR_NOT_CHUNKED=COMPRESSOR_POINTWISE;
public const int COMPRESSOR_DEFAULT=COMPRESSOR_CHUNKED;
public const int CODER_ARITHMETIC=0;
const int CODER_TOTAL_NUMBER_OF=1;
const int CHUNK_SIZE_DEFAULT=50000;
// supported version control
public bool check_compressor(ushort compressor)
{
if(compressor<COMPRESSOR_TOTAL_NUMBER_OF) return true;
return return_error(string.Format("compressor {0} not supported", compressor));
}
public bool check_coder(ushort coder)
{
if(coder<CODER_TOTAL_NUMBER_OF) return true;
return return_error(string.Format("coder {0} not supported", coder));
}
public bool check_item(LASitem item)
{
switch(item.type)
{
case LASitem.Type.POINT10:
if(item.size!=20) return return_error("POINT10 has size != 20");
if(item.version>2) return return_error("POINT10 has version > 2");
break;
case LASitem.Type.GPSTIME11:
if(item.size!=8) return return_error("GPSTIME11 has size != 8");
if(item.version>2) return return_error("GPSTIME11 has version > 2");
break;
case LASitem.Type.RGB12:
if(item.size!=6) return return_error("RGB12 has size != 6");
if(item.version>2) return return_error("RGB12 has version > 2");
break;
case LASitem.Type.WAVEPACKET13:
if(item.size!=29) return return_error("WAVEPACKET13 has size != 29");
if(item.version>1) return return_error("WAVEPACKET13 has version > 1");
break;
case LASitem.Type.BYTE:
if(item.size<1) return return_error("BYTE has size < 1");
if(item.version>2) return return_error("BYTE has version > 2");
break;
case LASitem.Type.POINT14:
if(item.size!=30) return return_error("POINT14 has size != 30");
if(item.version>0) return return_error("POINT14 has version > 0");
break;
case LASitem.Type.RGBNIR14:
if(item.size!=8) return return_error("RGBNIR14 has size != 8");
if(item.version>0) return return_error("RGBNIR14 has version > 0");
break;
default:
if(true)
return return_error(string.Format("item unknown ({0},{1},{2})", item.type, item.size, item.version));
}
return true;
}
public bool check_items(ushort num_items, LASitem[] items)
{
if(num_items==0) return return_error("number of items cannot be zero");
if(items==null) return return_error("items pointer cannot be NULL");
for(int i=0; i<num_items; i++)
{
if(!check_item(items[i])) return false;
}
return true;
}
public bool check()
{
if(!check_compressor(compressor)) return false;
if(!check_coder(coder)) return false;
if(!check_items(num_items, items)) return false;
return true;
}
// go back and forth between item array and point type & size
public bool setup(out ushort num_items, out LASitem[] items, byte point_type, ushort point_size, ushort compressor=COMPRESSOR_NONE)
{
num_items=0;
items=null;
bool have_point14=false;
bool have_gps_time=false;
bool have_rgb=false;
bool have_nir=false;
bool have_wavepacket=false;
int extra_bytes_number=0;
// switch over the point types we know
switch(point_type)
{
case 0:
extra_bytes_number=(int)point_size-20;
break;
case 1:
have_gps_time=true;
extra_bytes_number=(int)point_size-28;
break;
case 2:
have_rgb=true;
extra_bytes_number=(int)point_size-26;
break;
case 3:
have_gps_time=true;
have_rgb=true;
extra_bytes_number=(int)point_size-34;
break;
case 4:
have_gps_time=true;
have_wavepacket=true;
extra_bytes_number=(int)point_size-57;
break;
case 5:
have_gps_time=true;
have_rgb=true;
have_wavepacket=true;
extra_bytes_number=(int)point_size-63;
break;
case 6:
have_point14=true;
extra_bytes_number=(int)point_size-30;
break;
case 7:
have_point14=true;
have_rgb=true;
extra_bytes_number=(int)point_size-36;
break;
case 8:
have_point14=true;
have_rgb=true;
have_nir=true;
extra_bytes_number=(int)point_size-38;
break;
case 9:
have_point14=true;
have_wavepacket=true;
extra_bytes_number=(int)point_size-59;
break;
case 10:
have_point14=true;
have_rgb=true;
have_nir=true;
have_wavepacket=true;
extra_bytes_number=(int)point_size-67;
break;
default:
if(true)
return return_error(string.Format("point type {0} unknown", point_type));
}
if(extra_bytes_number<0)
{
Console.Error.WriteLine("WARNING: point size {0} too small by {1} bytes for point type {2}. assuming point_size of {3}", point_size, -extra_bytes_number, point_type, point_size-extra_bytes_number);
extra_bytes_number=0;
}
// create item description
num_items=(ushort)(1+(have_gps_time?1:0)+(have_rgb?1:0)+(have_wavepacket?1:0)+(extra_bytes_number!=0?1:0));
items=new LASitem[num_items];
ushort i=1;
if(have_point14)
{
items[0]=new LASitem();
items[0].type=LASitem.Type.POINT14;
items[0].size=30;
items[0].version=0;
}
else
{
items[0]=new LASitem();
items[0].type=LASitem.Type.POINT10;
items[0].size=20;
items[0].version=0;
}
if(have_gps_time)
{
items[i]=new LASitem();
items[i].type=LASitem.Type.GPSTIME11;
items[i].size=8;
items[i].version=0;
i++;
}
if(have_rgb)
{
items[i]=new LASitem();
if(have_nir)
{
items[i].type=LASitem.Type.RGBNIR14;
items[i].size=8;
items[i].version=0;
}
else
{
items[i].type=LASitem.Type.RGB12;
items[i].size=6;
items[i].version=0;
}
i++;
}
if(have_wavepacket)
{
items[i]=new LASitem();
items[i].type=LASitem.Type.WAVEPACKET13;
items[i].size=29;
items[i].version=0;
i++;
}
if(extra_bytes_number!=0)
{
items[i]=new LASitem();
items[i].type=LASitem.Type.BYTE;
items[i].size=(ushort)extra_bytes_number;
items[i].version=0;
i++;
}
if(compressor!=0) request_version(2);
Debug.Assert(i==num_items);
return true;
}
public bool is_standard(ushort num_items, LASitem[] items, out byte point_type, out ushort record_length)
{
// this is always true
point_type=127;
record_length=0;
if(items==null) return return_error("LASitem array is zero");
for(int i=0; i<num_items; i++) record_length+=items[i].size;
// the minimal number of items is 1
if(num_items<1) return return_error("less than one LASitem entries");
// the maximal number of items is 5
if(num_items>5) return return_error("more than five LASitem entries");
if(items[0].is_type(LASitem.Type.POINT10))
{
// consider all the POINT10 combinations
if(num_items==1)
{
point_type=0;
Debug.Assert(record_length==20);
return true;
}
else
{
if(items[1].is_type(LASitem.Type.GPSTIME11))
{
if(num_items==2)
{
point_type=1;
Debug.Assert(record_length==28);
return true;
}
else
{
if(items[2].is_type(LASitem.Type.RGB12))
{
if(num_items==3)
{
point_type=3;
Debug.Assert(record_length==34);
return true;
}
else
{
if(items[3].is_type(LASitem.Type.WAVEPACKET13))
{
if(num_items==4)
{
point_type=5;
Debug.Assert(record_length==63);
return true;
}
else
{
if(items[4].is_type(LASitem.Type.BYTE))
{
if(num_items==5)
{
point_type=5;
Debug.Assert(record_length==(63+items[4].size));
return true;
}
}
}
}
else if(items[3].is_type(LASitem.Type.BYTE))
{
if(num_items==4)
{
point_type=3;
Debug.Assert(record_length==(34+items[3].size));
return true;
}
}
}
}
else if(items[2].is_type(LASitem.Type.WAVEPACKET13))
{
if(num_items==3)
{
point_type=4;
Debug.Assert(record_length==57);
return true;
}
else
{
if(items[3].is_type(LASitem.Type.BYTE))
{
if(num_items==4)
{
point_type=4;
Debug.Assert(record_length==(57+items[3].size));
return true;
}
}
}
}
else if(items[2].is_type(LASitem.Type.BYTE))
{
if(num_items==3)
{
point_type=1;
Debug.Assert(record_length==(28+items[2].size));
return true;
}
}
}
}
else if(items[1].is_type(LASitem.Type.RGB12))
{
if(num_items==2)
{
point_type=2;
Debug.Assert(record_length==26);
return true;
}
else
{
if(items[2].is_type(LASitem.Type.BYTE))
{
if(num_items==3)
{
point_type=2;
Debug.Assert(record_length==(26+items[2].size));
return true;
}
}
}
}
else if(items[1].is_type(LASitem.Type.BYTE))
{
if(num_items==2)
{
point_type=0;
Debug.Assert(record_length==(20+items[1].size));
return true;
}
}
}
}
else if(items[0].is_type(LASitem.Type.POINT14))
{
// consider all the POINT14 combinations
if(num_items==1)
{
point_type=6;
Debug.Assert(record_length==30);
return true;
}
else
{
if(items[1].is_type(LASitem.Type.RGB12))
{
if(num_items==2)
{
point_type=7;
Debug.Assert(record_length==36);
return true;
}
else
{
if(items[2].is_type(LASitem.Type.BYTE))
{
if(num_items==3)
{
point_type=7;
Debug.Assert(record_length==(36+items[2].size));
return true;
}
}
}
}
else if(items[1].is_type(LASitem.Type.RGBNIR14))
{
if(num_items==2)
{
point_type=8;
Debug.Assert(record_length==38);
return true;
}
else
{
if(items[2].is_type(LASitem.Type.WAVEPACKET13))
{
if(num_items==3)
{
point_type=10;
Debug.Assert(record_length==67);
return true;
}
else
{
if(items[3].is_type(LASitem.Type.BYTE))
{
if(num_items==4)
{
point_type=10;
Debug.Assert(record_length==(67+items[3].size));
return true;
}
}
}
}
else if(items[2].is_type(LASitem.Type.BYTE))
{
if(num_items==3)
{
point_type=8;
Debug.Assert(record_length==(38+items[2].size));
return true;
}
}
}
}
else if(items[1].is_type(LASitem.Type.WAVEPACKET13))
{
if(num_items==2)
{
point_type=9;
Debug.Assert(record_length==59);
return true;
}
else
{
if(items[2].is_type(LASitem.Type.BYTE))
{
if(num_items==3)
{
point_type=9;
Debug.Assert(record_length==(59+items[2].size));
return true;
}
}
}
}
else if(items[1].is_type(LASitem.Type.BYTE))
{
if(num_items==2)
{
point_type=6;
Debug.Assert(record_length==(30+items[1].size));
return true;
}
}
}
}
else
{
return_error("first LASitem is neither POINT10 nor POINT14");
}
return return_error("LASitem array does not match LAS specification 1.4");
}
public bool is_standard(out byte point_type, out ushort record_length)
{
return is_standard(num_items, items, out point_type, out record_length);
}
// pack to and unpack from VLR
public byte[] bytes;
public unsafe bool unpack(byte[] bytes, int num)
{
// check input
if(num<34) return return_error("too few bytes to unpack");
if(((num-34)%6)!=0) return return_error("wrong number bytes to unpack");
if(((num-34)/6)==0) return return_error("zero items to unpack");
num_items=(ushort)((num-34)/6);
// create item list
items=new LASitem[num_items];
// do the unpacking
ushort i;
fixed(byte* pBytes=bytes)
{
byte* b=pBytes;
compressor=*((ushort*)b);
b+=2;
coder=*((ushort*)b);
b+=2;
version_major=*b;
b+=1;
version_minor=*b;
b+=1;
version_revision=*((ushort*)b);
b+=2;
options=*((uint*)b);
b+=4;
chunk_size=*((uint*)b);
b+=4;
number_of_special_evlrs=*((long*)b);
b+=8;
offset_to_special_evlrs=*((long*)b);
b+=8;
num_items=*((ushort*)b);
b+=2;
for(i=0; i<num_items; i++)
{
items[i].type=(LASitem.Type)(int)*((ushort*)b);
b+=2;
items[i].size=*((ushort*)b);
b+=2;
items[i].version=*((ushort*)b);
b+=2;
}
Debug.Assert((pBytes+num)==b);
// check if we support the contents
for(i=0; i<num_items; i++)
{
if(!check_item(items[i])) return false;
}
return true;
}
}
public unsafe bool pack(out byte[] bytes, ref int num)
{
bytes=null;
num=0;
// check if we support the contents
if(!check()) return false;
// prepare output
num=34+6*num_items;
this.bytes=bytes=new byte[num];
// pack
ushort i;
fixed(byte* pBytes=bytes)
{
byte* b=pBytes;
*((ushort*)b)=compressor;
b+=2;
*((ushort*)b)=coder;
b+=2;
*b=version_major;
b+=1;
*b=version_minor;
b+=1;
*((ushort*)b)=version_revision;
b+=2;
*((uint*)b)=options;
b+=4;
*((uint*)b)=chunk_size;
b+=4;
*((long*)b)=number_of_special_evlrs;
b+=8;
*((long*)b)=offset_to_special_evlrs;
b+=8;
*((ushort*)b)=num_items;
b+=2;
for(i=0; i<num_items; i++)
{
*((ushort*)b)=(ushort)items[i].type;
b+=2;
*((ushort*)b)=items[i].size;
b+=2;
*((ushort*)b)=items[i].version;
b+=2;
}
Debug.Assert((pBytes+num)==b);
return true;
}
}
// setup
public bool setup(byte point_type, ushort point_size, ushort compressor=COMPRESSOR_DEFAULT)
{
if(!check_compressor(compressor)) return false;
num_items=0;
items=null;
if(!setup(out num_items, out items, point_type, point_size, compressor)) return false;
this.compressor=compressor;
if(this.compressor==COMPRESSOR_POINTWISE_CHUNKED)
{
if(chunk_size==0) chunk_size=CHUNK_SIZE_DEFAULT;
}
return true;
}
public bool setup(ushort num_items, LASitem[] items, ushort compressor)
{
// check input
if(!check_compressor(compressor)) return false;
if(!check_items(num_items, items)) return false;
// setup compressor
this.compressor=compressor;
if(this.compressor==COMPRESSOR_POINTWISE_CHUNKED)
{
if(chunk_size==0) chunk_size=CHUNK_SIZE_DEFAULT;
}
// prepare items
this.num_items=0;
this.items=null;
this.num_items=num_items;
this.items=new LASitem[num_items];
// setup items
for(int i=0; i<num_items; i++)
{
this.items[i]=items[i];
}
return true;
}
public bool set_chunk_size(uint chunk_size) // for compressor only
{
if(num_items==0) return return_error("call setup() before setting chunk size");
if(compressor==COMPRESSOR_POINTWISE_CHUNKED)
{
this.chunk_size=chunk_size;
return true;
}
return false;
}
public bool request_version(ushort requested_version) // for compressor only
{
if(num_items==0) return return_error("call setup() before requesting version");
if(compressor==COMPRESSOR_NONE)
{
if(requested_version>0) return return_error("without compression version is always 0");
}
else
{
if(requested_version<1) return return_error("with compression version is at least 1");
if(requested_version>2) return return_error("version larger than 2 not supported");
}
for(int i=0; i<num_items; i++)
{
switch(items[i].type)
{
case LASitem.Type.POINT10:
case LASitem.Type.GPSTIME11:
case LASitem.Type.RGB12:
case LASitem.Type.BYTE: items[i].version=requested_version; break;
case LASitem.Type.WAVEPACKET13: items[i].version=1; break; // no version 2
default: return return_error("itrm type not supported");
}
}
return true;
}
// in case a function returns false this string describes the problem
public string get_error() { return error_string; }
// stored in LASzip VLR data section
public ushort compressor;
public ushort coder;
public byte version_major;
public byte version_minor;
public ushort version_revision;
public uint options;
public uint chunk_size;
public long number_of_special_evlrs; // must be -1 if unused
public long offset_to_special_evlrs; // must be -1 if unused
public ushort num_items;
public LASitem[] items;
public LASzip()
{
compressor=COMPRESSOR_DEFAULT;
coder=CODER_ARITHMETIC;
version_major=VERSION_MAJOR;
version_minor=VERSION_MINOR;
version_revision=VERSION_REVISION;
options=0;
num_items=0;
chunk_size=CHUNK_SIZE_DEFAULT;
number_of_special_evlrs=-1;
offset_to_special_evlrs=-1;
error_string=null;
items=null;
bytes=null;
}
bool return_error(string error)
{
error_string=string.Format("{0} (LASzip v{1}.{2}r{3})", error, VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION);
return false;
}
string error_string;
}
}