/usr/include/pdal/Compression.hpp is in libpdal-dev 1.4.0-1+b1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /******************************************************************************
* Copyright (c) 2014, Howard Butler (howard@hobu.co)
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
****************************************************************************/
#pragma once
#include <pdal/pdal_internal.hpp>
#ifdef PDAL_HAVE_LAZPERF
#include <laz-perf/common/common.hpp>
#include <laz-perf/compressor.hpp>
#include <laz-perf/decompressor.hpp>
#include <laz-perf/encoder.hpp>
#include <laz-perf/decoder.hpp>
#include <laz-perf/formats.hpp>
#include <laz-perf/io.hpp>
#include <laz-perf/las.hpp>
#endif
#include <pdal/Dimension.hpp>
#include <pdal/DimType.hpp>
#include <pdal/util/OStream.hpp>
#include <map>
#include <vector>
namespace pdal
{
namespace
{
template<typename LasZipEngine>
size_t addFields(LasZipEngine& engine, const DimTypeList& dims)
{
using namespace Dimension;
size_t pointSize = 0;
for (auto di = dims.begin(); di != dims.end(); ++di)
{
switch (di->m_type)
{
case Type::Signed64:
engine->template add_field<int32_t>();
engine->template add_field<int32_t>();
break;
case Type::Signed32:
case Type::Float:
engine->template add_field<int32_t>();
break;
case Type::Signed16:
engine->template add_field<int16_t>();
break;
case Type::Signed8:
engine->template add_field<int8_t>();
break;
case Type::Unsigned64:
case Type::Double:
engine->template add_field<uint32_t>();
engine->template add_field<uint32_t>();
break;
case Type::Unsigned32:
engine->template add_field<uint32_t>();
break;
case Type::Unsigned16:
engine->template add_field<uint16_t>();
break;
case Type::Unsigned8:
engine->template add_field<uint8_t>();
break;
default:
return 0;
}
pointSize += Dimension::size(di->m_type);
}
return pointSize;
}
} // anonymous namespace
enum class CompressionType
{
None = 0,
Ght = 1,
Dimensional = 2,
Lazperf = 3,
Unknown = 256
};
// This is a utility input/output buffer for the compressor/decompressor.
template <typename CTYPE = unsigned char>
class TypedLazPerfBuf
{
typedef std::vector<CTYPE> LazPerfRawBuf;
private:
LazPerfRawBuf& m_buf;
size_t m_idx;
public:
TypedLazPerfBuf(LazPerfRawBuf& buf) : m_buf(buf), m_idx(0)
{}
void putBytes(const unsigned char *b, size_t len)
{
m_buf.insert(m_buf.end(), (const CTYPE *)b, (const CTYPE *)(b + len));
}
void putByte(const unsigned char b)
{ m_buf.push_back((CTYPE)b); }
unsigned char getByte()
{ return (unsigned char)m_buf[m_idx++]; }
void getBytes(unsigned char *b, int len)
{
memcpy(b, m_buf.data() + m_idx, len);
m_idx += len;
}
};
typedef TypedLazPerfBuf<char> SignedLazPerfBuf;
typedef TypedLazPerfBuf<unsigned char> LazPerfBuf;
#ifdef PDAL_HAVE_LAZPERF
template<typename OutputStream>
class LazPerfCompressor
{
public:
LazPerfCompressor(OutputStream& output, const DimTypeList& dims) :
m_encoder(output),
m_compressor(laszip::formats::make_dynamic_compressor(m_encoder)),
m_pointSize(0),
m_done(false)
{ m_pointSize = addFields(m_compressor, dims); }
~LazPerfCompressor()
{
if (!m_done)
std::cerr << "LazPerfCompressor destroyed without a call "
"to done()";
}
size_t pointSize() const
{ return m_pointSize; }
point_count_t compress(const char *inbuf, size_t bufsize)
{
point_count_t numRead = 0;
const char *end = inbuf + bufsize;
while (inbuf + m_pointSize <= end)
{
m_compressor->compress(inbuf);
inbuf += m_pointSize;
numRead++;
}
return numRead;
}
void done()
{
if (!m_done)
m_encoder.done();
m_done = true;
}
private:
typedef laszip::encoders::arithmetic<OutputStream> Encoder;
Encoder m_encoder;
typedef typename laszip::formats::dynamic_field_compressor<Encoder>::ptr
Compressor;
Compressor m_compressor;
size_t m_pointSize;
bool m_done;
};
class LazPerfVlrCompressor
{
typedef laszip::io::__ofstream_wrapper<std::ostream> OutputStream;
typedef laszip::encoders::arithmetic<OutputStream> Encoder;
typedef laszip::formats::dynamic_compressor Compressor;
typedef laszip::factory::record_schema Schema;
public:
LazPerfVlrCompressor(std::ostream& stream, const Schema& schema,
uint32_t chunksize) :
m_stream(stream), m_outputStream(stream), m_schema(schema),
m_chunksize(chunksize), m_chunkPointsWritten(0), m_chunkInfoPos(0),
m_chunkOffset(0)
{}
~LazPerfVlrCompressor()
{
if (m_encoder)
std::cerr << "LazPerfVlrCompressor destroyed without a call "
"to done()";
}
void compress(const char *inbuf)
{
// First time through.
if (!m_encoder || !m_compressor)
{
// Get the position
m_chunkInfoPos = m_stream.tellp();
// Seek over the chunk info offset value
m_stream.seekp(sizeof(uint64_t), std::ios::cur);
m_chunkOffset = m_stream.tellp();
resetCompressor();
}
else if (m_chunkPointsWritten == m_chunksize)
{
resetCompressor();
newChunk();
}
m_compressor->compress(inbuf);
m_chunkPointsWritten++;
}
void done()
{
// Close and clear the point encoder.
m_encoder->done();
m_encoder.reset();
newChunk();
// Save our current position. Go to the location where we need
// to write the chunk table offset at the beginning of the point data.
std::streampos chunkTablePos = m_stream.tellp();
m_stream.seekp(m_chunkInfoPos);
OLeStream out(&m_stream);
out << (uint64_t)chunkTablePos;
// Move to the start of the chunk table.
m_stream.seekp(chunkTablePos);
// Write the chunk table header.
out << (uint32_t)0; // Version (?)
out << (uint32_t)m_chunkTable.size();
// Encode and write the chunk table.
OutputStream outputStream(m_stream);
Encoder encoder(outputStream);
laszip::compressors::integer compressor(32, 2);
compressor.init();
uint32_t predictor = 0;
for (uint32_t offset : m_chunkTable)
{
offset = htole32(offset);
compressor.compress(encoder, predictor, offset, 1);
predictor = offset;
}
encoder.done();
}
private:
void resetCompressor()
{
if (m_encoder)
m_encoder->done();
m_encoder.reset(new Encoder(m_outputStream));
m_compressor = laszip::factory::build_compressor(*m_encoder, m_schema);
}
void newChunk()
{
std::streampos offset = m_stream.tellp();
m_chunkTable.push_back((uint32_t)(offset - m_chunkOffset));
m_chunkOffset = offset;
m_chunkPointsWritten = 0;
}
std::ostream& m_stream;
OutputStream m_outputStream;
std::unique_ptr<Encoder> m_encoder;
Compressor::ptr m_compressor;
Schema m_schema;
uint32_t m_chunksize;
uint32_t m_chunkPointsWritten;
std::streampos m_chunkInfoPos;
std::streampos m_chunkOffset;
std::vector<uint32_t> m_chunkTable;
};
template<typename InputStream>
class LazPerfDecompressor
{
public:
LazPerfDecompressor(InputStream& input, const DimTypeList& dims) :
m_decoder(input),
m_decompressor(laszip::formats::make_dynamic_decompressor(m_decoder))
{ m_pointSize = addFields(m_decompressor, dims); }
size_t pointSize() const
{ return m_pointSize; }
point_count_t decompress(char *outbuf, size_t bufsize)
{
point_count_t numWritten = 0;
char *end = outbuf + bufsize;
while (outbuf + m_pointSize <= end)
{
m_decompressor->decompress(outbuf);
outbuf += m_pointSize;
numWritten++;
}
return numWritten;
}
private:
typedef laszip::decoders::arithmetic<InputStream> Decoder;
Decoder m_decoder;
typedef typename laszip::formats::dynamic_field_decompressor<Decoder>::ptr
Decompressor;
Decompressor m_decompressor;
size_t m_pointSize;
};
class LazPerfVlrDecompressor
{
public:
LazPerfVlrDecompressor(std::istream& stream, const char *vlrData,
std::streamoff pointOffset) :
m_stream(stream), m_inputStream(stream), m_chunksize(0),
m_chunkPointsRead(0)
{
laszip::io::laz_vlr zipvlr(vlrData);
m_chunksize = zipvlr.chunk_size;
m_schema = laszip::io::laz_vlr::to_schema(zipvlr);
m_stream.seekg(pointOffset + sizeof(int64_t));
}
size_t pointSize() const
{ return (size_t)m_schema.size_in_bytes(); }
void decompress(char *outbuf)
{
if (m_chunkPointsRead == m_chunksize || !m_decoder || !m_decompressor)
{
resetDecompressor();
m_chunkPointsRead = 0;
}
m_decompressor->decompress(outbuf);
m_chunkPointsRead++;
}
private:
void resetDecompressor()
{
m_decoder.reset(new Decoder(m_inputStream));
m_decompressor =
laszip::factory::build_decompressor(*m_decoder, m_schema);
}
typedef laszip::io::__ifstream_wrapper<std::istream> InputStream;
typedef laszip::decoders::arithmetic<InputStream> Decoder;
typedef laszip::formats::dynamic_decompressor Decompressor;
typedef laszip::factory::record_schema Schema;
std::istream& m_stream;
InputStream m_inputStream;
std::unique_ptr<Decoder> m_decoder;
Decompressor::ptr m_decompressor;
Schema m_schema;
uint32_t m_chunksize;
uint32_t m_chunkPointsRead;
};
#else
typedef char LazPerfVlrCompressor;
typedef char LazPerfVlrDecompressor;
#endif // PDAL_HAVE_LAZPERF
} // namespace pdal
|