-
Notifications
You must be signed in to change notification settings - Fork 26
/
LASwriteItemCompressed_RGB12_v2.cs
144 lines (128 loc) · 4.32 KB
/
LASwriteItemCompressed_RGB12_v2.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
//===============================================================================
//
// FILE: laswriteitemcompressed_rgb12_v2.cs
//
// CONTENTS:
//
// Implementation of LASwriteItemCompressed for RGB12 items (version 2).
//
// PROGRAMMERS:
//
// martin.isenburg@rapidlasso.com - http://rapidlasso.com
//
// COPYRIGHT:
//
// (c) 2005-2012, martin isenburg, rapidlasso - tools to catch reality
// (c) of the C# port 2014 by Shinta <shintadono@googlemail.com>
//
// 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 LASwriteItemCompressed_RGB12_v2 : LASwriteItemCompressed
{
public LASwriteItemCompressed_RGB12_v2(ArithmeticEncoder enc)
{
// set encoder
Debug.Assert(enc!=null);
this.enc=enc;
// create models and integer compressors
m_byte_used=enc.createSymbolModel(128);
m_rgb_diff_0=enc.createSymbolModel(256);
m_rgb_diff_1=enc.createSymbolModel(256);
m_rgb_diff_2=enc.createSymbolModel(256);
m_rgb_diff_3=enc.createSymbolModel(256);
m_rgb_diff_4=enc.createSymbolModel(256);
m_rgb_diff_5=enc.createSymbolModel(256);
}
public override bool init(laszip_point item)
{
// init state
// init models and integer compressors
enc.initSymbolModel(m_byte_used);
enc.initSymbolModel(m_rgb_diff_0);
enc.initSymbolModel(m_rgb_diff_1);
enc.initSymbolModel(m_rgb_diff_2);
enc.initSymbolModel(m_rgb_diff_3);
enc.initSymbolModel(m_rgb_diff_4);
enc.initSymbolModel(m_rgb_diff_5);
// init last item
Buffer.BlockCopy(item.rgb, 0, last_item, 0, 6);
return true;
}
public override bool write(laszip_point item)
{
int diff_l=0;
int diff_h=0;
uint sym=0;
bool rl=(last_item[0]&0x00FF)!=(item.rgb[0]&0x00FF); if(rl) sym|=1;
bool rh=(last_item[0]&0xFF00)!=(item.rgb[0]&0xFF00); if(rh) sym|=2;
bool gl=(last_item[1]&0x00FF)!=(item.rgb[1]&0x00FF); if(gl) sym|=4;
bool gh=(last_item[1]&0xFF00)!=(item.rgb[1]&0xFF00); if(gh) sym|=8;
bool bl=(last_item[2]&0x00FF)!=(item.rgb[2]&0x00FF); if(bl) sym|=16;
bool bh=(last_item[2]&0xFF00)!=(item.rgb[2]&0xFF00); if(bh) sym|=32;
bool allColors=((item.rgb[0]&0x00FF)!=(item.rgb[1]&0x00FF))||((item.rgb[0]&0x00FF)!=(item.rgb[2]&0x00FF))||
((item.rgb[0]&0xFF00)!=(item.rgb[1]&0xFF00))||((item.rgb[0]&0xFF00)!=(item.rgb[2]&0xFF00));
if(allColors) sym|=64;
enc.encodeSymbol(m_byte_used, sym);
if(rl)
{
diff_l=((int)(item.rgb[0]&255))-(last_item[0]&255);
enc.encodeSymbol(m_rgb_diff_0, (byte)MyDefs.U8_FOLD(diff_l));
}
if(rh)
{
diff_h=((int)(item.rgb[0]>>8))-(last_item[0]>>8);
enc.encodeSymbol(m_rgb_diff_1, (byte)MyDefs.U8_FOLD(diff_h));
}
if(allColors)
{
if(gl)
{
int corr=((int)(item.rgb[1]&255))-MyDefs.U8_CLAMP(diff_l+(last_item[1]&255));
enc.encodeSymbol(m_rgb_diff_2, (byte)MyDefs.U8_FOLD(corr));
}
if(bl)
{
diff_l=(diff_l+(item.rgb[1]&255)-(last_item[1]&255))/2;
int corr=((int)(item.rgb[2]&255))-MyDefs.U8_CLAMP(diff_l+(last_item[2]&255));
enc.encodeSymbol(m_rgb_diff_4, (byte)MyDefs.U8_FOLD(corr));
}
if(gh)
{
int corr=((int)(item.rgb[1]>>8))-MyDefs.U8_CLAMP(diff_h+(last_item[1]>>8));
enc.encodeSymbol(m_rgb_diff_3, (byte)MyDefs.U8_FOLD(corr));
}
if(bh)
{
diff_h=(diff_h+(item.rgb[1]>>8)-(last_item[1]>>8))/2;
int corr=((int)(item.rgb[2]>>8))-MyDefs.U8_CLAMP(diff_h+(last_item[2]>>8));
enc.encodeSymbol(m_rgb_diff_5, (byte)MyDefs.U8_FOLD(corr));
}
}
last_item[0]=item.rgb[0];
last_item[1]=item.rgb[1];
last_item[2]=item.rgb[2];
return true;
}
ArithmeticEncoder enc;
ushort[] last_item=new ushort[3];
ArithmeticModel m_byte_used;
ArithmeticModel m_rgb_diff_0;
ArithmeticModel m_rgb_diff_1;
ArithmeticModel m_rgb_diff_2;
ArithmeticModel m_rgb_diff_3;
ArithmeticModel m_rgb_diff_4;
ArithmeticModel m_rgb_diff_5;
}
}