Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
clusty committed Dec 20, 2023
1 parent bc3f4f1 commit c692b48
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 67 deletions.
103 changes: 45 additions & 58 deletions src/lib/OpenEXR/ImfZstdCompressor.cpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,19 @@
//
// Created by vladal on 27/10/23.
//

#include <cstring>
#include "ImfZstdCompressor.h"

//#include <zstd.h>
#include "blosc2.h"
#include "IlmThreadPool.h"
#include "ImfChannelList.h"
#include "ImfMisc.h"
namespace
{
/*
int
ZSTD_compress_impl (const char* inPtr, int inSize, Imf::ZstdCompressor::raw_ptr& outPtr)
{
auto const cBuffSize = ZSTD_compressBound(inSize);
outPtr = Imf::ZstdCompressor::raw_ptr((char*)malloc (cBuffSize), &free);
auto const cSize = ZSTD_compress(outPtr.get(), cBuffSize, inPtr, inSize, 15);
return cSize;
}
int
ZSTD_uncompress_impl (const char* inPtr, int inSize, Imf::ZstdCompressor::raw_ptr& outPtr)
{
auto rSize = ZSTD_getFrameContentSize(inPtr, inSize);
outPtr = Imf::ZstdCompressor::raw_ptr((char*)malloc (rSize), &free);
auto const dSize = ZSTD_decompress(outPtr.get(), rSize, inPtr, inSize);
return dSize;
}
*/
class BloscInit
{
public:
static void Init () { getInstance (); }
BloscInit (const BloscInit&) = delete;
BloscInit& operator= (const BloscInit&) = delete;

private:
BloscInit () { blosc2_init (); }
~BloscInit () { blosc2_destroy (); }
Expand All @@ -46,11 +23,16 @@ class BloscInit
return instance;
}
};
}
} // namespace

OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
ZstdCompressor::ZstdCompressor ( const Header& hdr, size_t maxScanlineSize, size_t numScanLines): Compressor(hdr),
_maxScanlineSize (maxScanlineSize),_numScanLines(numScanLines),_outBuffer (nullptr, &free), _schunk (nullptr,&blosc2_schunk_free)
ZstdCompressor::ZstdCompressor (
const Header& hdr, size_t maxScanlineSize, size_t numScanLines)
: Compressor (hdr)
, _maxScanlineSize (maxScanlineSize)
, _numScanLines (numScanLines)
, _outBuffer (nullptr, &free)
, _schunk (nullptr, &blosc2_schunk_free)
{}

int
Expand All @@ -62,61 +44,66 @@ int
ZstdCompressor::compress (
const char* inPtr, int inSize, int minY, const char*& outPtr)
{
auto ret = BLOSC_compress_impl(inPtr, inSize, outPtr);
auto ret = BLOSC_compress_impl (inPtr, inSize, outPtr);
return ret;
}
int
ZstdCompressor::uncompress (
const char* inPtr, int inSize, int minY, const char*& outPtr)
{
auto ret = BLOSC_uncompress_impl(inPtr, inSize, outPtr);
auto ret = BLOSC_uncompress_impl (inPtr, inSize, outPtr);
return ret;
}


int
ZstdCompressor::BLOSC_compress_impl (const char* inPtr, int inSize, const char*& out)
ZstdCompressor::BLOSC_compress_impl (
const char* inPtr, int inSize, const char*& out)
{
BloscInit::Init();
BloscInit::Init ();
blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS;

cparams.typesize = pixelTypeSize(PixelType::FLOAT); // Expect Float values
cparams.clevel = header().zstdCompressionLevel(); // 9 is about a 20% increase in compression compared to 5. Decompression speed is unchanged.
cparams.typesize = pixelTypeSize (PixelType::FLOAT); // Expect Float values
cparams.clevel =
header ()
.zstdCompressionLevel (); // 9 is about a 20% increase in compression compared to 5. Decompression speed is unchanged.
cparams.nthreads = 1;
cparams.compcode = BLOSC_ZSTD; // Codec
cparams.splitmode = BLOSC_NEVER_SPLIT; // Split => multithreading, not split better compression

cparams.splitmode =
BLOSC_NEVER_SPLIT; // Split => multithreading, not split better compression

blosc2_storage storage= BLOSC2_STORAGE_DEFAULTS;
storage.cparams=&cparams;
storage.contiguous = true;
blosc2_storage storage = BLOSC2_STORAGE_DEFAULTS;
storage.cparams = &cparams;
storage.contiguous = true;

_schunk = schunk_ptr (blosc2_schunk_new(&storage), &blosc2_schunk_free);
_schunk = schunk_ptr (blosc2_schunk_new (&storage), &blosc2_schunk_free);

auto in = const_cast<char*>(inPtr);
blosc2_schunk_append_buffer(_schunk.get(), in, inSize);
auto in = const_cast<char*> (inPtr);
blosc2_schunk_append_buffer (_schunk.get (), in, inSize);

uint8_t *buffer;
bool shouldFree = true;
auto size = blosc2_schunk_to_buffer(_schunk.get(), &buffer, &shouldFree);
out = (char*)buffer;
if (shouldFree)
{
_outBuffer = raw_ptr((char*)buffer, &free);
}
uint8_t* buffer;
bool shouldFree = true;
auto size = blosc2_schunk_to_buffer (_schunk.get (), &buffer, &shouldFree);
out = (char*) buffer;
if (shouldFree) { _outBuffer = raw_ptr ((char*) buffer, &free); }
return size;
}

int
ZstdCompressor::BLOSC_uncompress_impl (const char* inPtr, int inSize, const char*& out)
ZstdCompressor::BLOSC_uncompress_impl (
const char* inPtr, int inSize, const char*& out)
{
auto in = const_cast<char*>(inPtr);
_schunk = schunk_ptr (blosc2_schunk_from_buffer(reinterpret_cast<uint8_t*>(in), inSize, true), &blosc2_schunk_free);

auto buffSize = _maxScanlineSize * numScanLines();
_outBuffer = Imf::ZstdCompressor::raw_ptr((char*)malloc (buffSize), &free);
auto size = blosc2_schunk_decompress_chunk(_schunk.get(), 0, _outBuffer.get(), buffSize);
out = _outBuffer.get();
auto in = const_cast<char*> (inPtr);
_schunk = schunk_ptr (
blosc2_schunk_from_buffer (
reinterpret_cast<uint8_t*> (in), inSize, true),
&blosc2_schunk_free);

auto buffSize = _maxScanlineSize * numScanLines ();
_outBuffer =
Imf::ZstdCompressor::raw_ptr ((char*) malloc (buffSize), &free);
auto size = blosc2_schunk_decompress_chunk (
_schunk.get (), 0, _outBuffer.get (), buffSize);
out = _outBuffer.get ();
return size;
}

Expand Down
20 changes: 11 additions & 9 deletions src/lib/OpenEXR/ImfZstdCompressor.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
class ZstdCompressor : public Compressor
{
public:
ZstdCompressor (const Header& hdr, size_t maxScanLines, size_t numScanLines);
ZstdCompressor (
const Header& hdr, size_t maxScanLines, size_t numScanLines);

private:
using schunk_ptr = std::unique_ptr<blosc2_schunk, decltype(&blosc2_schunk_free)> ;
using raw_ptr = std::unique_ptr<char, decltype(&free)> ;
raw_ptr _outBuffer;
using schunk_ptr =
std::unique_ptr<blosc2_schunk, decltype (&blosc2_schunk_free)>;
using raw_ptr = std::unique_ptr<char, decltype (&free)>;
raw_ptr _outBuffer;
schunk_ptr _schunk;
size_t _maxScanlineSize;
size_t _numScanLines;
int numScanLines () const override; // max
int compress (
const char* inPtr, int inSize, int minY, const char*& outPtr) override;
size_t _maxScanlineSize;
size_t _numScanLines;
int numScanLines () const override; // max
int compress (
const char* inPtr, int inSize, int minY, const char*& outPtr) override;
int uncompress (
const char* inPtr, int inSize, int minY, const char*& outPtr) override;
int BLOSC_compress_impl (const char* inPtr, int inSize, const char*& out);
Expand Down

0 comments on commit c692b48

Please sign in to comment.