File: lazperf_adapter.cpp

package info (click to toggle)
pgpointcloud 1.2.5-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,892 kB
  • sloc: sql: 40,767; ansic: 11,045; xml: 935; makefile: 297; cpp: 282; perl: 248; python: 178; sh: 92
file content (253 lines) | stat: -rw-r--r-- 6,047 bytes parent folder | download | duplicates (3)
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
/***********************************************************************
 * lazperf_adapter.cpp
 *
 *  LazPerf compression/decompression
 *
 *  Copyright (c) 2016 Paul Blottiere, Oslandia
 *
 ***********************************************************************/

#include "lazperf_adapter.hpp"

#ifdef HAVE_LAZPERF

/**********************************************************************
 * C API
 */
size_t lazperf_compress_from_uncompressed(const PCPATCH_UNCOMPRESSED *pa,
                                          uint8_t **compressed)
{
  size_t size = 1;

  LazPerfBuf buf;
  LazPerfCompressor engine(pa->schema, buf);

  if (engine.compress(pa->data, pa->datasize) == pa->npoints)
  {
    size = buf.buf.size();
    *compressed = (uint8_t *)malloc(size);
    *compressed = (uint8_t *)memcpy(*compressed, buf.data(), size);
  }

  // log
  // lazperf_dump(pa);
  // lazperf_dump(*compressed, size);

  return size;
}

size_t lazperf_uncompress_from_compressed(const PCPATCH_LAZPERF *pa,
                                          uint8_t **decompressed)
{
  size_t size = -1;
  size_t datasize = pa->schema->size * pa->npoints;

  LazPerfBuf buf;
  buf.putBytes(pa->lazperf, pa->lazperfsize);
  LazPerfDecompressor engine(pa->schema, buf);

  *decompressed = (uint8_t *)malloc(datasize);

  if (engine.decompress(*decompressed, datasize) == pa->npoints)
    size = buf.buf.size();

  // log
  // lazperf_dump(pa);
  // lazperf_dump(*decompressed, datasize);

  return size;
}

/**********************************************************************
 * INTERNAL CPP
 */
// utility functions
void lazperf_dump(uint8_t *data, const size_t size)
{
  std::cout << "DUMP DATA: " << std::endl;
  std::cout << "	- datasize: " << size << std::endl;
  std::cout << "	- data: ";
  for (int i = 0; i < size; ++i)
    printf("%02x ", data[i]);
  std::cout << std::endl;
}

void lazperf_dump(const PCPATCH_UNCOMPRESSED *p)
{
  std::cout << std::endl;
  std::cout << "DUMP UNCOMPRESSED PATCH: " << std::endl;
  std::cout << "	- type: " << p->type << std::endl;
  std::cout << "	- schema->size " << p->schema->size << std::endl;
  std::cout << "	- readonly: " << p->readonly << std::endl;
  std::cout << "	- npoints: " << p->npoints << std::endl;
  std::cout << "	- maxpoints: " << p->maxpoints << std::endl;
  std::cout << "	- datasize: " << p->datasize << std::endl;
  std::cout << "	- data: ";
  for (int i = 0; i < p->datasize; ++i)
    printf("%02x ", p->data[i]);
  std::cout << std::endl;
}

void lazperf_dump(const PCPATCH_LAZPERF *p)
{
  std::cout << std::endl;
  std::cout << "DUMP LAZPERF PATCH: " << std::endl;
  std::cout << "	- type: " << p->type << std::endl;
  std::cout << "	- schema->size " << p->schema->size << std::endl;
  std::cout << "	- readonly: " << p->readonly << std::endl;
  std::cout << "	- npoints: " << p->npoints << std::endl;
  std::cout << "	- lazperfsize: " << p->lazperfsize << std::endl;
  std::cout << "	- lazperf: ";
  for (int i = 0; i < p->lazperfsize; ++i)
    printf("%02x ", p->lazperf[i]);
  std::cout << std::endl;
}

// LazPerf class
template <typename LazPerfEngine, typename LazPerfCoder>
LazPerf<LazPerfEngine, LazPerfCoder>::LazPerf(const PCSCHEMA *pcschema,
                                              LazPerfBuf &buf)
    : _pcschema(pcschema), _coder(buf), _pointsize(0)
{
}

template <typename LazPerfEngine, typename LazPerfCoder>
LazPerf<LazPerfEngine, LazPerfCoder>::~LazPerf()
{
}

template <typename LazPerfEngine, typename LazPerfCoder>
void LazPerf<LazPerfEngine, LazPerfCoder>::initSchema()
{
  for (int i = 0; i < _pcschema->ndims; i++)
    addField(_pcschema->dims[i]);
}

template <typename LazPerfEngine, typename LazPerfCoder>
bool LazPerf<LazPerfEngine, LazPerfCoder>::addField(const PCDIMENSION *dim)
{
  bool rc = true;

  switch (dim->interpretation)
  {
  case PC_INT8:
  {
    _engine->template add_field<I8>();
    break;
  }
  case PC_UINT8:
  {
    _engine->template add_field<U8>();
    break;
  }
  case PC_INT16:
  {
    _engine->template add_field<I16>();
    break;
  }
  case PC_UINT16:
  {
    _engine->template add_field<U16>();
    break;
  }
  case PC_INT32:
  {
    _engine->template add_field<I32>();
    break;
  }
  case PC_UINT32:
  {
    _engine->template add_field<U32>();
    break;
  }
  case PC_INT64:
  {
    _engine->template add_field<I32>();
    _engine->template add_field<I32>();
    break;
  }
  case PC_UINT64:
  {
    _engine->template add_field<U32>();
    _engine->template add_field<U32>();
    break;
  }
  case PC_DOUBLE:
  {
    _engine->template add_field<U32>();
    _engine->template add_field<U32>();
    break;
  }
  case PC_FLOAT:
  {
    _engine->template add_field<I32>();
    break;
  }
  case PC_UNKNOWN:
  default:
    rc = false;
  }

  if (rc)
    _pointsize += dim->size;

  return rc;
}

// LazPerf Compressor
LazPerfCompressor::LazPerfCompressor(const PCSCHEMA *pcschema,
                                     LazPerfBuf &output)
    : LazPerf(pcschema, output)
{
  _engine = laszip::formats::make_dynamic_compressor(_coder);
  initSchema();
}

LazPerfCompressor::~LazPerfCompressor() {}

size_t LazPerfCompressor::compress(const uint8_t *input, const size_t inputsize)
{
  size_t size = 0;

  const uint8_t *end = input + inputsize;

  while (input + _pointsize <= end)
  {
    _engine->compress((const char *)input);
    input += _pointsize;
    size++;
  }

  _coder.done();

  return size;
}

// LazPerf Decompressor
LazPerfDecompressor::LazPerfDecompressor(const PCSCHEMA *pcschema,
                                         LazPerfBuf &input)
    : LazPerf(pcschema, input)
{
  _engine = laszip::formats::make_dynamic_decompressor(_coder);
  initSchema();
}

LazPerfDecompressor::~LazPerfDecompressor() {}

size_t LazPerfDecompressor::decompress(uint8_t *output, const size_t outputsize)
{
  size_t size = 0;

  const uint8_t *end = output + outputsize;

  while (output + _pointsize <= end)
  {
    _engine->decompress((char *)output);
    output += _pointsize;
    size++;
  }

  return size;
}

#endif // HAVE_LAZPERF