libCZI
Reading and Writing CZI documents made easy
libCZI_Utilities.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 <vector>
8 #include <tuple>
9 #include <limits>
10 #include <utility>
11 #include <memory>
12 #include <string>
13 #include <cstdint>
14 #include "libCZI_Metadata.h"
15 #include "libCZI_Pixels.h"
16 
17 namespace libCZI
18 {
24  struct LIBCZI_API GUID
25  {
26  std::uint32_t Data1;
27  std::uint16_t Data2;
28  std::uint16_t Data3;
29  std::uint8_t Data4[8];
30 
35  bool operator==(const GUID& other) const
36  {
37  return this->Data1 == other.Data1 &&
38  this->Data2 == other.Data2 &&
39  this->Data3 == other.Data3 &&
40  this->Data4[0] == other.Data4[0] &&
41  this->Data4[1] == other.Data4[1] &&
42  this->Data4[2] == other.Data4[2] &&
43  this->Data4[3] == other.Data4[3] &&
44  this->Data4[4] == other.Data4[4] &&
45  this->Data4[5] == other.Data4[5] &&
46  this->Data4[6] == other.Data4[6] &&
47  this->Data4[7] == other.Data4[7];
48  }
49 
54  bool operator!=(const GUID& other) const
55  {
56  return !(*this == other);
57  }
58 
63  int compare(const GUID& other) const
64  {
65  if (this->Data1 != other.Data1)
66  {
67  return this->Data1 < other.Data1 ? -1 : 1;
68  }
69 
70  if (this->Data2 != other.Data2)
71  {
72  return this->Data2 < other.Data2 ? -1 : 1;
73  }
74 
75  if (this->Data3 != other.Data3)
76  {
77  return this->Data3 < other.Data3 ? -1 : 1;
78  }
79 
80  for (int i = 0; i < 8; ++i)
81  {
82  if (this->Data4[i] != other.Data4[i])
83  {
84  return this->Data4[i] < other.Data4[i] ? -1 : 1;
85  }
86  }
87 
88  return 0;
89  }
90  };
91 
92  class ISubBlockRepository;
93  class IDisplaySettings;
94  class ICompressParameters;
95 
97  class LIBCZI_API Utils
98  {
99  public:
107 
116 
122  static int CalcMd5SumHash(libCZI::IBitmapData* bm, std::uint8_t* ptrHash, int hashSize);
123 
130  static int CalcMd5SumHash(const void* ptrData, size_t sizeData, std::uint8_t* ptrHash, int hashSize);
131 
140  static std::vector<std::uint8_t> Create8BitLookUpTableFromSplines(int tableElementCnt, float blackPoint, float whitePoint, const std::vector<libCZI::IDisplaySettings::SplineData>& splineData);
141 
151  static std::vector<std::uint8_t> Create8BitLookUpTableFromGamma(int tableElementCnt, float blackPoint, float whitePoint, float gamma);
152 
162  static std::vector<std::uint16_t> Create16BitLookUpTableFromGamma(int tableElementCnt, float blackPoint, float whitePoint, float gamma);
163 
168  static std::vector<libCZI::IDisplaySettings::SplineData> CalcSplineDataFromPoints(int pointCnt, std::function< std::tuple<double, double>(int idx)> getPoint);
169 
175  static std::shared_ptr<libCZI::IBitmapData > NearestNeighborResize(libCZI::IBitmapData* bmSrc, int dstWidth, int dstHeight);
176 
184  static std::shared_ptr<libCZI::IBitmapData > NearestNeighborResize(libCZI::IBitmapData* bmSrc, int dstWidth, int dstHeight, const DblRect& roiSrc, const DblRect& roiDest);
185 
192  static float CalcZoom(const libCZI::IntRect& logicalRect, const libCZI::IntSize& physicalSize)
193  {
194  if (physicalSize.w > physicalSize.h)
195  {
196  return static_cast<float>(physicalSize.w) / logicalRect.w;
197  }
198  else
199  {
200  return static_cast<float>(physicalSize.h) / logicalRect.h;
201  }
202  }
203 
210  static float CalcZoom(const libCZI::IntSize& logicalSize, const libCZI::IntSize& physicalSize)
211  {
212  if (physicalSize.w > physicalSize.h)
213  {
214  return static_cast<float>(physicalSize.w) / logicalSize.w;
215  }
216  else
217  {
218  return static_cast<float>(physicalSize.h) / logicalSize.h;
219  }
220  }
221 
227  static const char* PixelTypeToInformalString(libCZI::PixelType pixeltype);
228 
232  static std::uint8_t GetBytesPerPixel(libCZI::PixelType pixelType);
233 
241  static const char* CompressionModeToInformalString(libCZI::CompressionMode compressionMode);
242 
246  static std::string DimCoordinateToString(const libCZI::IDimCoordinate* coord);
247 
252  static bool StringToDimCoordinate(const char* sz, libCZI::CDimCoordinate* coord);
253 
257  static std::string DimBoundsToString(const libCZI::IDimBounds* bounds);
258 
266  static std::shared_ptr<libCZI::IIndexSet> IndexSetFromString(const std::wstring& s);
267 
277 
288  static int Compare(const IDimCoordinate* a, const IDimCoordinate* b);
289 
296  static bool HasSameDimensions(const IDimCoordinate* a, const IDimCoordinate* b);
297 
315  static std::shared_ptr<ICziMetadataBuilder> CreateSubBlockMetadata(const std::function<bool(int, std::tuple<std::string, std::string>&)>& tagsEnum = nullptr);
316 
325  static bool EnumAllCoordinates(const libCZI::CDimBounds& bounds, const std::function<bool(std::uint64_t, const libCZI::CDimCoordinate& coord)>& func);
326 
330  static void FillBitmap(libCZI::IBitmapData* bm, const libCZI::RgbFloatColor& floatColor);
331 
338 
343 
347  static bool IsValidMindex(int mIndex)
348  {
349  return mIndex != (std::numeric_limits<int>::max)() && mIndex != (std::numeric_limits<int>::min)();
350  }
351 
354  static const char* const KEY_COMPRESS_EXPLICIT_LEVEL /*= "ExplicitLevel"*/;
355 
360  static const char* const KEY_COMPRESS_PRE_PROCESS /*= "PreProcess"*/;
361 
370  static const char* const VALUE_COMPRESS_HILO_BYTE_UNPACK /*= "HiLoByteUnpack"*/;
371 
374  using CompressionOption = std::pair<libCZI::CompressionMode, std::shared_ptr<libCZI::ICompressParameters>>;
375 
383  static CompressionOption ParseCompressionOptions(const std::string& options);
384  };
385 }
Implementation of a class representing an interval (and implementing the libCZI::IDimBounds-interface...
Definition: libCZI_DimCoordinate.h:288
Implementation of a class representing a coordinate (and implementing the IDimCoordinate-interface).
Definition: libCZI_DimCoordinate.h:149
Definition: libCZI_Pixels.h:167
Interface used to represent an interval (for several dimensions).
Definition: libCZI_DimCoordinate.h:80
Interface used to represent a coordinate (in the space of the dimensions identified by DimensionIndex...
Definition: libCZI_DimCoordinate.h:37
Interface for sub-block repository. This interface is used to access the sub-blocks in a CZI-file.
Definition: libCZI.h:532
A bunch of utility functions.
Definition: libCZI_Utilities.h:98
static std::vector< std::uint16_t > Create16BitLookUpTableFromGamma(int tableElementCnt, float blackPoint, float whitePoint, float gamma)
static bool HasSameDimensions(const IDimCoordinate *a, const IDimCoordinate *b)
static int CalcMd5SumHash(libCZI::IBitmapData *bm, std::uint8_t *ptrHash, int hashSize)
static std::vector< libCZI::IDisplaySettings::SplineData > CalcSplineDataFromPoints(int pointCnt, std::function< std::tuple< double, double >(int idx)> getPoint)
static int Compare(const IDimCoordinate *a, const IDimCoordinate *b)
static std::int32_t CompressionModeToCompressionIdentifier(libCZI::CompressionMode mode)
static bool IsValidMindex(int mIndex)
Definition: libCZI_Utilities.h:347
static libCZI::CompressionMode CompressionModeFromRawCompressionIdentifier(std::int32_t m)
static std::vector< std::uint8_t > Create8BitLookUpTableFromGamma(int tableElementCnt, float blackPoint, float whitePoint, float gamma)
static bool EnumAllCoordinates(const libCZI::CDimBounds &bounds, const std::function< bool(std::uint64_t, const libCZI::CDimCoordinate &coord)> &func)
static bool StringToDimCoordinate(const char *sz, libCZI::CDimCoordinate *coord)
static std::vector< std::uint8_t > Create8BitLookUpTableFromSplines(int tableElementCnt, float blackPoint, float whitePoint, const std::vector< libCZI::IDisplaySettings::SplineData > &splineData)
static CompressionOption ParseCompressionOptions(const std::string &options)
std::pair< libCZI::CompressionMode, std::shared_ptr< libCZI::ICompressParameters > > CompressionOption
Definition: libCZI_Utilities.h:374
static std::uint8_t GetBytesPerPixel(libCZI::PixelType pixelType)
static std::string DimBoundsToString(const libCZI::IDimBounds *bounds)
static std::shared_ptr< libCZI::IIndexSet > IndexSetFromString(const std::wstring &s)
static std::shared_ptr< libCZI::IBitmapData > NearestNeighborResize(libCZI::IBitmapData *bmSrc, int dstWidth, int dstHeight, const DblRect &roiSrc, const DblRect &roiDest)
static const char * CompressionModeToInformalString(libCZI::CompressionMode compressionMode)
static void FillBitmap(libCZI::IBitmapData *bm, const libCZI::RgbFloatColor &floatColor)
static const char * PixelTypeToInformalString(libCZI::PixelType pixeltype)
static float CalcZoom(const libCZI::IntSize &logicalSize, const libCZI::IntSize &physicalSize)
Definition: libCZI_Utilities.h:210
static libCZI::DimensionIndex CharToDimension(char c)
static libCZI::PixelType TryDeterminePixelTypeForChannel(libCZI::ISubBlockRepository *repository, int channelIdx)
static int CalcMd5SumHash(const void *ptrData, size_t sizeData, std::uint8_t *ptrHash, int hashSize)
static char DimensionToChar(libCZI::DimensionIndex dim)
static std::string DimCoordinateToString(const libCZI::IDimCoordinate *coord)
static float CalcZoom(const libCZI::IntRect &logicalRect, const libCZI::IntSize &physicalSize)
Definition: libCZI_Utilities.h:192
static std::shared_ptr< libCZI::IBitmapData > NearestNeighborResize(libCZI::IBitmapData *bmSrc, int dstWidth, int dstHeight)
static std::shared_ptr< ICziMetadataBuilder > CreateSubBlockMetadata(const std::function< bool(int, std::tuple< std::string, std::string > &)> &tagsEnum=nullptr)
External interfaces, classes, functions and structs are found in the namespace "libCZI".
Definition: libCZI.h:31
DimensionIndex
Values that represent dimension indexes.
Definition: libCZI_DimCoordinate.h:17
CompressionMode
An enum specifying the compression method.
Definition: libCZI_Pixels.h:131
PixelType
An enum representing a pixel-type.
Definition: libCZI_Pixels.h:114
A rectangle (with double coordinates).
Definition: libCZI_Pixels.h:79
Represents a globally unique identifier (GUID) consisting of four unsigned 32-bit integers.
Definition: libCZI_Utilities.h:25
bool operator!=(const GUID &other) const
Provide an inequality comparison operator between GUID objects.
Definition: libCZI_Utilities.h:54
std::uint32_t Data1
The first component of the GUID.
Definition: libCZI_Utilities.h:26
std::uint16_t Data2
The second component of the GUID.
Definition: libCZI_Utilities.h:27
int compare(const GUID &other) const
Provide a method to lexically compare two GUID objects.
Definition: libCZI_Utilities.h:63
std::uint8_t Data4[8]
The fourth component of the GUID, represented as an array of 8 bytes.
Definition: libCZI_Utilities.h:29
bool operator==(const GUID &other) const
Provide an equality comparison operator between GUID objects.
Definition: libCZI_Utilities.h:35
std::uint16_t Data3
The third component of the GUID.
Definition: libCZI_Utilities.h:28
A rectangle (with integer coordinates).
Definition: libCZI_Pixels.h:17
int h
The height of the rectangle.
Definition: libCZI_Pixels.h:21
int w
The width of the rectangle.
Definition: libCZI_Pixels.h:20
A structure representing a size (width and height) in integers.
Definition: libCZI_Pixels.h:91
std::uint32_t h
The height.
Definition: libCZI_Pixels.h:93
std::uint32_t w
The width.
Definition: libCZI_Pixels.h:92
A structure representing an R-G-B-color triple (as floats).
Definition: libCZI_Pixels.h:106