1 #include "PyLazperf.hpp"
2 #include <stdexcept>
3
4
5 namespace pylazperf
6 {
7
8 template<typename LasZipEngine>
addDimension(LasZipEngine & engine,pylazperf::Type t)9 size_t addDimension(LasZipEngine& engine, pylazperf::Type t)
10 {
11
12 switch (t)
13 {
14 case Type::Signed64:
15 engine->template add_field<int32_t>();
16 engine->template add_field<int32_t>();
17 break;
18 case Type::Signed32:
19 case Type::Float:
20 engine->template add_field<int32_t>();
21 break;
22 case Type::Signed16:
23 engine->template add_field<int16_t>();
24 break;
25 case Type::Signed8:
26 engine->template add_field<int8_t>();
27 break;
28 case Type::Unsigned64:
29 case Type::Double:
30 engine->template add_field<uint32_t>();
31 engine->template add_field<uint32_t>();
32 break;
33 case Type::Unsigned32:
34 engine->template add_field<uint32_t>();
35 break;
36 case Type::Unsigned16:
37 engine->template add_field<uint16_t>();
38 break;
39 case Type::Unsigned8:
40 engine->template add_field<uint8_t>();
41 break;
42 default:
43 break;
44 }
45 return pylazperf::size(t);
46 }
47
LAZEngine()48 LAZEngine::LAZEngine()
49 : m_pointSize(0)
50 {
51 }
52
53
Decompressor(const unsigned char * data,size_t dataLen)54 Decompressor::Decompressor(const unsigned char *data, size_t dataLen)
55 : LAZEngine()
56 , m_stream(data, dataLen)
57 , m_decoder(m_stream)
58 , m_decompressor(laszip::formats::make_dynamic_decompressor(m_decoder))
59 {
60 }
61
add_dimension(pylazperf::Type t)62 void Decompressor::add_dimension(pylazperf::Type t)
63 {
64 m_pointSize = m_pointSize + addDimension(m_decompressor, t);
65 }
66
decompress(char * output,size_t buffer_size)67 size_t Decompressor::decompress(char* output, size_t buffer_size)
68 {
69 size_t count (0);
70
71 char* out = output;
72 char* end = out + buffer_size;
73
74 while (out + m_pointSize <= end)
75 {
76 // Determine when we're done?
77 m_decompressor->decompress(out);
78 out += m_pointSize;
79 count++;
80 }
81 return count;
82
83 }
84
85
Compressor(std::vector<uint8_t> & uncompressed)86 Compressor::Compressor(std::vector<uint8_t>& uncompressed)
87 : LAZEngine()
88 , m_stream(uncompressed)
89 , m_encoder(m_stream)
90 , m_compressor(laszip::formats::make_dynamic_compressor(m_encoder))
91 , m_done(false)
92 {
93 }
94
done()95 void Compressor::done()
96 {
97 if (!m_done)
98 m_encoder.done();
99 m_done = true;
100 }
101
compress(const char * inbuf,size_t bufsize)102 size_t Compressor::compress(const char *inbuf, size_t bufsize)
103 {
104 size_t numRead = 0;
105
106 if (m_done)
107 throw std::runtime_error("Encoder has finished, unable to compress!");
108
109 const char *end = inbuf + bufsize;
110 while (inbuf + m_pointSize <= end)
111 {
112 m_compressor->compress(inbuf);
113 inbuf += m_pointSize;
114 numRead++;
115 }
116 return numRead;
117 }
118
add_dimension(pylazperf::Type t)119 void Compressor::add_dimension(pylazperf::Type t)
120 {
121 m_pointSize = m_pointSize + addDimension(m_compressor, t);
122 }
123
data() const124 const std::vector<uint8_t>* Compressor::data() const
125 {
126 return &m_stream.m_buf;
127 }
128
copy_data_to(uint8_t * destination) const129 void Compressor::copy_data_to(uint8_t *destination) const
130 {
131 std::copy(m_stream.m_buf.begin(), m_stream.m_buf.end(), destination);
132 }
133
134 } //Namespace
135
136
137