libCZI
Reading and Writing CZI documents made easy
libCZI_Pixels.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 <cstdint>
8 #include <memory>
9 #include <algorithm>
10 #include <ostream>
11 #include <utility>
12 
13 namespace libCZI
14 {
16  struct IntRect
17  {
18  int x;
19  int y;
20  int w;
21  int h;
22 
24  void Invalidate() { this->w = this->h = -1; }
25 
27  bool IsValid() const { return this->w >= 0 && this->h >= 0; }
28 
30  bool IsNonEmpty() const { return this->w > 0 && this->h > 0; }
31 
35  bool IntersectsWith(const IntRect& r) const
36  {
37  const IntRect is = this->Intersect(r);
38  if (is.w <= 0 || is.h <= 0)
39  {
40  return false;
41  }
42 
43  return true;
44  }
45 
51  IntRect Intersect(const IntRect& r) const
52  {
53  return IntRect::Intersect(*this, r);
54  }
55 
62  static IntRect Intersect(const IntRect& a, const IntRect& b)
63  {
64  const int x1 = (std::max)(a.x, b.x);
65  const int x2 = (std::min)(a.x + a.w, b.x + b.w);
66  const int y1 = (std::max)(a.y, b.y);
67  const int y2 = (std::min)(a.y + a.h, b.y + b.h);
68  if (x2 >= x1 && y2 >= y1)
69  {
70  return IntRect{ x1, y1, x2 - x1, y2 - y1 };
71  }
72 
73  return IntRect{ 0,0,0,0 };
74  }
75  };
76 
78  struct DblRect
79  {
80  double x;
81  double y;
82  double w;
83  double h;
84 
86  void Invalidate() { this->w = this->h = -1; }
87  };
88 
90  struct IntSize
91  {
92  std::uint32_t w;
93  std::uint32_t h;
94  };
95 
97  struct Rgb8Color
98  {
99  std::uint8_t r;
100  std::uint8_t g;
101  std::uint8_t b;
102  };
103 
106  {
107  float r;
108  float g;
109  float b;
110  };
111 
113  enum class PixelType : std::uint8_t
114  {
115  Invalid = 0xff,
116  Gray8 = 0,
117  Gray16 = 1,
118  Gray32Float = 2,
119  Bgr24 = 3,
120  Bgr48 = 4,
121  Bgr96Float = 8,
122  Bgra32 = 9,
123  Gray64ComplexFloat = 10,
124  Bgr192ComplexFloat = 11,
125  Gray32 = 12,
126  Gray64Float = 13,
127  };
128 
130  enum class CompressionMode : std::uint8_t
131  {
132  Invalid = 0xff,
133  UnCompressed = 0,
134  Jpg = 1,
135  JpgXr = 4,
136  Zstd0 = 5,
137  Zstd1 = 6
138  };
139 
143  enum class SubBlockPyramidType : std::uint8_t
144  {
145  Invalid = 0xff,
146  None = 0,
147  SingleSubBlock= 1,
148  MultiSubBlock = 2
149  };
150 
153  {
154  void* ptrData;
155  void* ptrDataRoi;
156  std::uint32_t stride;
157  std::uint64_t size;
158  };
159 
167  {
168  public:
169 
173  virtual PixelType GetPixelType() const = 0;
174 
178  virtual IntSize GetSize() const = 0;
179 
188  virtual BitmapLockInfo Lock() = 0;
189 
194  virtual void Unlock() = 0;
195 
196  virtual ~IBitmapData() {}
197 
201  std::uint32_t GetWidth() const { return this->GetSize().w; }
202 
206  std::uint32_t GetHeight() const { return this->GetSize().h; }
207  };
208 
253  template <typename tBitmap>
255  {
256  private:
257  tBitmap bmData;
258  public:
259  ScopedBitmapLocker() = delete;
260 
263  explicit ScopedBitmapLocker(tBitmap bmData) : bmData(bmData)
264  {
265  auto lockInfo = bmData->Lock();
266  this->ptrData = lockInfo.ptrData;
267  this->ptrDataRoi = lockInfo.ptrDataRoi;
268  this->stride = lockInfo.stride;
269  this->size = lockInfo.size;
270  }
271 
274  ScopedBitmapLocker(const ScopedBitmapLocker<tBitmap>& other) : bmData(other.bmData)
275  {
276  auto lockInfo = other.bmData->Lock();
277  this->ptrData = lockInfo.ptrData;
278  this->ptrDataRoi = lockInfo.ptrDataRoi;
279  this->stride = lockInfo.stride;
280  this->size = lockInfo.size;
281  }
282 
284  ScopedBitmapLocker(ScopedBitmapLocker<tBitmap>&& other) noexcept : bmData(tBitmap())
285  {
286  *this = std::move(other);
287  }
288 
291  {
292  if (this != &other)
293  {
294  if (this->bmData)
295  {
296  this->bmData->Unlock();
297  }
298 
299  this->bmData = std::move(other.bmData);
300  this->ptrData = other.ptrData;
301  this->ptrDataRoi = other.ptrDataRoi;
302  this->stride = other.stride;
303  this->size = other.size;
304  other.ptrData = other.ptrDataRoi = nullptr;
305  other.bmData = tBitmap();
306  }
307 
308  return *this;
309  }
310 
312  {
313  if (this->bmData)
314  {
315  this->bmData->Unlock();
316  }
317  }
318  };
319 
322 
325 
326  //-------------------------------------------------------------------------
327 
332  inline std::ostream& operator<<(std::ostream& os, const IntRect& rect)
333  {
334  os << "(" << rect.x << "," << rect.y << "," << rect.w << "," << rect.h << ")";
335  return os;
336  }
337 
342  inline std::ostream& operator<<(std::ostream& os, const IntSize& size)
343  {
344  os << "(" << size.w << "," << size.h << ")";
345  return os;
346  }
347 }
Definition: libCZI_Pixels.h:167
std::uint32_t GetHeight() const
Definition: libCZI_Pixels.h:206
virtual void Unlock()=0
virtual PixelType GetPixelType() const =0
std::uint32_t GetWidth() const
Definition: libCZI_Pixels.h:201
virtual IntSize GetSize() const =0
virtual BitmapLockInfo Lock()=0
Definition: libCZI_Pixels.h:255
ScopedBitmapLocker(const ScopedBitmapLocker< tBitmap > &other)
Definition: libCZI_Pixels.h:274
ScopedBitmapLocker(tBitmap bmData)
Definition: libCZI_Pixels.h:263
ScopedBitmapLocker< tBitmap > & operator=(ScopedBitmapLocker< tBitmap > &&other) noexcept
move assignment
Definition: libCZI_Pixels.h:290
ScopedBitmapLocker(ScopedBitmapLocker< tBitmap > &&other) noexcept
move constructor
Definition: libCZI_Pixels.h:284
External interfaces, classes, functions and structs are found in the namespace "libCZI".
Definition: libCZI.h:31
ScopedBitmapLocker< std::shared_ptr< IBitmapData > > ScopedBitmapLockerSP
Defines an alias representing the scoped bitmap locker for use with a shared_ptr of type libCZI::IBit...
Definition: libCZI_Pixels.h:324
CompressionMode
An enum specifying the compression method.
Definition: libCZI_Pixels.h:131
@ Invalid
Invalid compression type.
@ JpgXr
The data is JPG-XR-compressed.
@ Jpg
The data is JPG-compressed.
@ Zstd1
The data contains a header, followed by a zstd-compressed block.
@ Zstd0
The data is compressed with zstd.
@ UnCompressed
The data is uncompressed.
SubBlockPyramidType
Definition: libCZI_Pixels.h:144
@ MultiSubBlock
The subblock is a pyramid subblock, and it covers multiple subblocks of the lower layer.
@ SingleSubBlock
The subblock is a pyramid subblock, and it covers a single subblock of the lower layer (or: it is a m...
@ Invalid
Invalid pyramid type.
@ None
No pyramid (indicating that the subblock is not a pyramid subblock, but a layer-0 subblock).
ScopedBitmapLocker< IBitmapData * > ScopedBitmapLockerP
Defines an alias representing the scoped bitmap locker for use with libCZI::IBitmapData.
Definition: libCZI_Pixels.h:321
PixelType
An enum representing a pixel-type.
Definition: libCZI_Pixels.h:114
@ Gray64ComplexFloat
Currently not supported in libCZI.
@ Gray16
Grayscale 16-bit unsigned.
@ Bgra32
Currently not supported in libCZI.
@ Invalid
Invalid pixel type.
@ Gray64Float
Currently not supported in libCZI.
@ Gray32Float
Grayscale 4 byte float.
@ Bgr48
BGR-color 16-bytes triples (memory order B, G, R).
@ Gray32
Currently not supported in libCZI.
@ Bgr192ComplexFloat
Currently not supported in libCZI.
@ Bgr24
BGR-color 8-bytes triples (memory order B, G, R).
@ Gray8
Grayscale 8-bit unsigned.
@ Bgr96Float
BGR-color 4 byte float triples (memory order B, G, R).
std::ostream & operator<<(std::ostream &os, const IntRect &rect)
Definition: libCZI_Pixels.h:332
Information about a locked bitmap - allowing direct access to the image data in memory.
Definition: libCZI_Pixels.h:153
std::uint64_t size
The size of the bitmap data (pointed to by ptrDataRoi) in bytes.
Definition: libCZI_Pixels.h:157
void * ptrData
Not currently used, to be ignored.
Definition: libCZI_Pixels.h:154
std::uint32_t stride
The stride of the bitmap data (pointed to by ptrDataRoi).
Definition: libCZI_Pixels.h:156
void * ptrDataRoi
The pointer to the first (top-left) pixel of the bitmap.
Definition: libCZI_Pixels.h:155
A rectangle (with double coordinates).
Definition: libCZI_Pixels.h:79
double h
The height of the rectangle.
Definition: libCZI_Pixels.h:83
double w
The width of the rectangle.
Definition: libCZI_Pixels.h:82
double y
The y-coordinate of the upper-left point of the rectangle.
Definition: libCZI_Pixels.h:81
void Invalidate()
Invalidates this object.
Definition: libCZI_Pixels.h:86
double x
The x-coordinate of the upper-left point of the rectangle.
Definition: libCZI_Pixels.h:80
A rectangle (with integer coordinates).
Definition: libCZI_Pixels.h:17
int h
The height of the rectangle.
Definition: libCZI_Pixels.h:21
bool IsNonEmpty() const
Returns a boolean indicating whether this rectangle is valid and non-empty.
Definition: libCZI_Pixels.h:30
int y
The y-coordinate of the upper-left point of the rectangle.
Definition: libCZI_Pixels.h:19
IntRect Intersect(const IntRect &r) const
Definition: libCZI_Pixels.h:51
int x
The x-coordinate of the upper-left point of the rectangle.
Definition: libCZI_Pixels.h:18
bool IntersectsWith(const IntRect &r) const
Definition: libCZI_Pixels.h:35
static IntRect Intersect(const IntRect &a, const IntRect &b)
Definition: libCZI_Pixels.h:62
int w
The width of the rectangle.
Definition: libCZI_Pixels.h:20
bool IsValid() const
Returns a boolean indicating whether this rectangle contains valid information.
Definition: libCZI_Pixels.h:27
void Invalidate()
Invalidates this object.
Definition: libCZI_Pixels.h:24
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 bytes).
Definition: libCZI_Pixels.h:98
std::uint8_t b
The blue component.
Definition: libCZI_Pixels.h:101
std::uint8_t g
The green component.
Definition: libCZI_Pixels.h:100
std::uint8_t r
The red component.
Definition: libCZI_Pixels.h:99
A structure representing an R-G-B-color triple (as floats).
Definition: libCZI_Pixels.h:106
float r
The red component.
Definition: libCZI_Pixels.h:107
float b
The blue component.
Definition: libCZI_Pixels.h:109
float g
The green component.
Definition: libCZI_Pixels.h:108