libCZI
Reading and Writing CZI documents made easy
libCZI_compress.h
1 // SPDX-FileCopyrightText: 2017-2022 Carl Zeiss Microscopy GmbH
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4 
5 #pragma once
6 
7 #include <memory>
8 #include <cstdint>
9 #include <functional>
10 #include <map>
11 #include <type_traits>
12 #include "libCZI.h"
13 
14 namespace libCZI
15 {
20  {
24 
28 
32  JXRLIB_QUALITY = 3,
33  };
34 
36  struct LIBCZI_API CompressParameter
37  {
39  enum class Type
40  {
41  Invalid,
42  Int32,
43  Uint32,
44  Boolean
45  };
46 
49  {
50  }
51 
55  explicit CompressParameter(std::int32_t v)
56  {
57  this->SetInt32(v);
58  }
59 
63  explicit CompressParameter(std::uint32_t v)
64  {
65  this->SetUInt32(v);
66  }
67 
71  explicit CompressParameter(bool v)
72  {
73  this->SetBoolean(v);
74  }
75 
79  void SetInt32(std::int32_t v)
80  {
81  this->type = Type::Int32;
82  this->int32Value = v;
83  }
84 
88  void SetUInt32(std::uint32_t v)
89  {
90  this->type = Type::Uint32;
91  this->uint32Value = v;
92  }
93 
97  void SetBoolean(bool v)
98  {
99  this->type = Type::Boolean;
100  this->boolValue = v;
101  }
102 
106  Type GetType() const { return this->type; }
107 
111  std::int32_t GetInt32() const
112  {
113  this->ThrowIfTypeIsUnequalTo(Type::Int32);
114  return this->int32Value;
115  }
116 
120  std::uint32_t GetUInt32() const
121  {
122  this->ThrowIfTypeIsUnequalTo(Type::Uint32);
123  return this->uint32Value;
124  }
125 
129  bool GetBoolean() const
130  {
131  this->ThrowIfTypeIsUnequalTo(Type::Boolean);
132  return this->boolValue;
133  }
134  private:
135  Type type;
136 
137  union
138  {
139  std::int32_t int32Value;
140  std::uint32_t uint32Value;
141  bool boolValue;
142  };
143 
144  void ThrowIfTypeIsUnequalTo(Type typeToCheck) const
145  {
146  if (this->type != typeToCheck)
147  {
148  throw std::runtime_error("Unexpected type encountered.");
149  }
150  }
151  };
152 
155  class LIBCZI_API ICompressParameters
156  {
157  public:
164  virtual bool TryGetProperty(int key, CompressParameter* parameter) const = 0;
165 
166  virtual ~ICompressParameters() = default;
167 
176  {
177  return this->TryGetProperty(static_cast<typename std::underlying_type<libCZI::CompressionParameterKey>::type>(key), parameter);
178  }
179  };
180 
182  class LIBCZI_API IMemoryBlock
183  {
184  public:
190  virtual void* GetPtr() = 0;
191 
195  virtual size_t GetSizeOfData() const = 0;
196 
197  virtual ~IMemoryBlock() = default;
198  };
199 
207  class LIBCZI_API ZstdCompress
208  {
209  public:
221  static size_t CalculateMaxCompressedSizeZStd0(std::uint32_t sourceWidth, std::uint32_t sourceHeight, libCZI::PixelType sourcePixeltype);
222 
255  static bool CompressZStd0(
256  std::uint32_t sourceWidth,
257  std::uint32_t sourceHeight,
258  std::uint32_t sourceStride,
259  libCZI::PixelType sourcePixeltype,
260  const void* source,
261  const std::function<void* (size_t)>& allocateTempBuffer,
262  const std::function<void(void*)>& freeTempBuffer,
263  void* destination,
264  size_t& sizeDestination,
265  const ICompressParameters* parameters);
266 
290  static std::shared_ptr<IMemoryBlock> CompressZStd0Alloc(
291  std::uint32_t sourceWidth,
292  std::uint32_t sourceHeight,
293  std::uint32_t sourceStride,
294  libCZI::PixelType sourcePixeltype,
295  const void* source,
296  const std::function<void* (size_t)>& allocateTempBuffer,
297  const std::function<void(void*)>& freeTempBuffer,
298  const ICompressParameters* parameters);
299 
326  static bool CompressZStd0(
327  std::uint32_t sourceWidth,
328  std::uint32_t sourceHeight,
329  std::uint32_t sourceStride,
330  libCZI::PixelType sourcePixeltype,
331  const void* source,
332  void* destination,
333  size_t& sizeDestination,
334  const ICompressParameters* parameters);
335 
359  static std::shared_ptr<IMemoryBlock> CompressZStd0Alloc(
360  std::uint32_t sourceWidth,
361  std::uint32_t sourceHeight,
362  std::uint32_t sourceStride,
363  libCZI::PixelType sourcePixeltype,
364  const void* source,
365  const ICompressParameters* parameters);
366 
378  static size_t CalculateMaxCompressedSizeZStd1(std::uint32_t sourceWidth, std::uint32_t sourceHeight, libCZI::PixelType sourcePixeltype);
379 
411  static bool CompressZStd1(
412  std::uint32_t sourceWidth,
413  std::uint32_t sourceHeight,
414  std::uint32_t sourceStride,
415  libCZI::PixelType sourcePixeltype,
416  const void* source,
417  const std::function<void* (size_t)>& allocateTempBuffer,
418  const std::function<void(void*)>& freeTempBuffer,
419  void* destination,
420  size_t& sizeDestination,
421  const ICompressParameters* parameters);
422 
451  static std::shared_ptr<IMemoryBlock> CompressZStd1Alloc(
452  std::uint32_t sourceWidth,
453  std::uint32_t sourceHeight,
454  std::uint32_t sourceStride,
455  libCZI::PixelType sourcePixeltype,
456  const void* source,
457  const std::function<void* (size_t)>& allocateTempBuffer,
458  const std::function<void(void*)>& freeTempBuffer,
459  const ICompressParameters* parameters);
460 
486  static bool CompressZStd1(
487  std::uint32_t sourceWidth,
488  std::uint32_t sourceHeight,
489  std::uint32_t sourceStride,
490  libCZI::PixelType sourcePixeltype,
491  const void* source,
492  void* destination,
493  size_t& sizeDestination,
494  const ICompressParameters* parameters);
495 
517  static std::shared_ptr<IMemoryBlock> CompressZStd1Alloc(
518  std::uint32_t sourceWidth,
519  std::uint32_t sourceHeight,
520  std::uint32_t sourceStride,
521  libCZI::PixelType sourcePixeltype,
522  const void* source,
523  const ICompressParameters* parameters);
524  };
525 
530  class LIBCZI_API JxrLibCompress
531  {
532  public:
545  static std::shared_ptr<IMemoryBlock> Compress(
546  libCZI::PixelType pixel_type,
547  std::uint32_t width,
548  std::uint32_t height,
549  std::uint32_t stride,
550  const void* ptrData,
551  const ICompressParameters* parameters);
552  };
553 
558  {
559  public:
560  std::map<int, CompressParameter> map;
561 
568  bool TryGetProperty(int key, CompressParameter* parameter) const override
569  {
570  const auto it = this->map.find(key);
571  if (it != this->map.cend())
572  {
573  if (parameter != nullptr)
574  {
575  *parameter = it->second;
576  }
577 
578  return true;
579  }
580 
581  return false;
582  }
583  };
584 }
Definition: libCZI_compress.h:558
bool TryGetProperty(int key, CompressParameter *parameter) const override
Definition: libCZI_compress.h:568
std::map< int, CompressParameter > map
The key-value map containing "compression parameters".
Definition: libCZI_compress.h:560
Definition: libCZI_compress.h:156
bool TryGetProperty(libCZI::CompressionParameterKey key, CompressParameter *parameter) const
Definition: libCZI_compress.h:175
virtual bool TryGetProperty(int key, CompressParameter *parameter) const =0
Interface representing a "block of memory". It is used to hold the result of a compression-operation.
Definition: libCZI_compress.h:183
virtual void * GetPtr()=0
virtual size_t GetSizeOfData() const =0
Definition: libCZI_compress.h:531
static std::shared_ptr< IMemoryBlock > Compress(libCZI::PixelType pixel_type, std::uint32_t width, std::uint32_t height, std::uint32_t stride, const void *ptrData, const ICompressParameters *parameters)
Definition: libCZI_compress.h:208
static bool CompressZStd1(std::uint32_t sourceWidth, std::uint32_t sourceHeight, std::uint32_t sourceStride, libCZI::PixelType sourcePixeltype, const void *source, const std::function< void *(size_t)> &allocateTempBuffer, const std::function< void(void *)> &freeTempBuffer, void *destination, size_t &sizeDestination, const ICompressParameters *parameters)
static bool CompressZStd1(std::uint32_t sourceWidth, std::uint32_t sourceHeight, std::uint32_t sourceStride, libCZI::PixelType sourcePixeltype, const void *source, void *destination, size_t &sizeDestination, const ICompressParameters *parameters)
static size_t CalculateMaxCompressedSizeZStd1(std::uint32_t sourceWidth, std::uint32_t sourceHeight, libCZI::PixelType sourcePixeltype)
static bool CompressZStd0(std::uint32_t sourceWidth, std::uint32_t sourceHeight, std::uint32_t sourceStride, libCZI::PixelType sourcePixeltype, const void *source, void *destination, size_t &sizeDestination, const ICompressParameters *parameters)
static std::shared_ptr< IMemoryBlock > CompressZStd1Alloc(std::uint32_t sourceWidth, std::uint32_t sourceHeight, std::uint32_t sourceStride, libCZI::PixelType sourcePixeltype, const void *source, const ICompressParameters *parameters)
static std::shared_ptr< IMemoryBlock > CompressZStd1Alloc(std::uint32_t sourceWidth, std::uint32_t sourceHeight, std::uint32_t sourceStride, libCZI::PixelType sourcePixeltype, const void *source, const std::function< void *(size_t)> &allocateTempBuffer, const std::function< void(void *)> &freeTempBuffer, const ICompressParameters *parameters)
static std::shared_ptr< IMemoryBlock > CompressZStd0Alloc(std::uint32_t sourceWidth, std::uint32_t sourceHeight, std::uint32_t sourceStride, libCZI::PixelType sourcePixeltype, const void *source, const ICompressParameters *parameters)
static size_t CalculateMaxCompressedSizeZStd0(std::uint32_t sourceWidth, std::uint32_t sourceHeight, libCZI::PixelType sourcePixeltype)
static std::shared_ptr< IMemoryBlock > CompressZStd0Alloc(std::uint32_t sourceWidth, std::uint32_t sourceHeight, std::uint32_t sourceStride, libCZI::PixelType sourcePixeltype, const void *source, const std::function< void *(size_t)> &allocateTempBuffer, const std::function< void(void *)> &freeTempBuffer, const ICompressParameters *parameters)
static bool CompressZStd0(std::uint32_t sourceWidth, std::uint32_t sourceHeight, std::uint32_t sourceStride, libCZI::PixelType sourcePixeltype, const void *source, const std::function< void *(size_t)> &allocateTempBuffer, const std::function< void(void *)> &freeTempBuffer, void *destination, size_t &sizeDestination, const ICompressParameters *parameters)
External interfaces, classes, functions and structs are found in the namespace "libCZI".
Definition: libCZI.h:31
CompressionParameterKey
Definition: libCZI_compress.h:20
PixelType
An enum representing a pixel-type.
Definition: libCZI_Pixels.h:114
@ Invalid
Invalid pixel type.
Simple variant type used for the compression-parameters-property-bag.
Definition: libCZI_compress.h:37
CompressParameter(std::int32_t v)
Definition: libCZI_compress.h:55
CompressParameter()
Default constructor - setting the variant to 'invalid'.
Definition: libCZI_compress.h:48
void SetUInt32(std::uint32_t v)
Definition: libCZI_compress.h:88
Type GetType() const
Definition: libCZI_compress.h:106
std::int32_t GetInt32() const
Definition: libCZI_compress.h:111
std::uint32_t GetUInt32() const
Definition: libCZI_compress.h:120
void SetInt32(std::int32_t v)
Definition: libCZI_compress.h:79
CompressParameter(bool v)
Definition: libCZI_compress.h:71
Type
Values that represent the type represented by this variant.
Definition: libCZI_compress.h:40
CompressParameter(std::uint32_t v)
Definition: libCZI_compress.h:63
bool GetBoolean() const
Definition: libCZI_compress.h:129
void SetBoolean(bool v)
Definition: libCZI_compress.h:97