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
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkLZ4DataCompressor.h"
#include "vtkObjectFactory.h"
#include "vtk_lz4.h"
VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkLZ4DataCompressor);
//------------------------------------------------------------------------------
vtkLZ4DataCompressor::vtkLZ4DataCompressor()
{
this->AccelerationLevel = 1;
}
//------------------------------------------------------------------------------
vtkLZ4DataCompressor::~vtkLZ4DataCompressor() = default;
//------------------------------------------------------------------------------
void vtkLZ4DataCompressor::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "AccelerationLevel: " << this->AccelerationLevel << endl;
}
//------------------------------------------------------------------------------
size_t vtkLZ4DataCompressor::CompressBuffer(unsigned char const* uncompressedData,
size_t uncompressedSize, unsigned char* compressedData, size_t compressionSpace)
{
const char* ud = reinterpret_cast<const char*>(uncompressedData);
char* cd = reinterpret_cast<char*>(compressedData);
// Call LZ4's compress function.
int cs = LZ4_compress_fast(ud, cd, static_cast<int>(uncompressedSize),
static_cast<int>(compressionSpace), this->AccelerationLevel);
if (cs == 0)
{
vtkErrorMacro("LZ4 error while compressing data.");
}
return static_cast<size_t>(cs);
}
//------------------------------------------------------------------------------
size_t vtkLZ4DataCompressor::UncompressBuffer(unsigned char const* compressedData,
size_t compressedSize, unsigned char* uncompressedData, size_t uncompressedSize)
{
char* ud = reinterpret_cast<char*>(uncompressedData);
const char* cd = reinterpret_cast<const char*>(compressedData);
int us = LZ4_decompress_safe(
cd, ud, static_cast<int>(compressedSize), static_cast<int>(uncompressedSize));
if (us < 0)
{
vtkErrorMacro("Zlib error while uncompressing data.");
return 0;
}
// Make sure the output size matched that expected.
if (us != static_cast<int>(uncompressedSize))
{
vtkErrorMacro("Decompression produced incorrect size.\n"
"Expected "
<< uncompressedSize << " and got " << us);
return 0;
}
return static_cast<size_t>(us);
}
//------------------------------------------------------------------------------
int vtkLZ4DataCompressor::GetCompressionLevel()
{
vtkDebugMacro(<< this->GetClassName() << " (" << this << "): returning CompressionLevel "
<< (10 - this->AccelerationLevel));
return 10 - this->AccelerationLevel;
}
//------------------------------------------------------------------------------
void vtkLZ4DataCompressor::SetCompressionLevel(int compressionLevel)
{
int min = 1;
int max = 9;
vtkDebugMacro(<< this->GetClassName() << " (" << this << "): setting CompressionLevel to "
<< compressionLevel);
// In order to make an intuitive interface for vtkDataCompressor objects
// we accept compressionLevel values 1..9. 1 is fastest, 9 is slowest
// 1 is worst compression, 9 is best compression. LZ4 acceleration works inversely, with no upper
// bound. Note: LZ4 Acceleration set/get exists in header file, with no upper bound.
if (this->AccelerationLevel !=
(10 - (compressionLevel < min ? min : (compressionLevel > max ? max : compressionLevel))))
{
this->AccelerationLevel =
10 - (compressionLevel < min ? min : (compressionLevel > max ? max : compressionLevel));
this->Modified();
}
}
//------------------------------------------------------------------------------
size_t vtkLZ4DataCompressor::GetMaximumCompressionSpace(size_t size)
{
return LZ4_COMPRESSBOUND(size);
}
VTK_ABI_NAMESPACE_END
|