libCZI
Reading CZI documents made easy
libCZI_Pixels.h
1 //******************************************************************************
2 //
3 // libCZI is a reader for the CZI fileformat written in C++
4 // Copyright (C) 2017 Zeiss Microscopy GmbH
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 //
19 // To obtain a commercial version please contact Zeiss Microscopy GmbH.
20 //
21 //******************************************************************************
22 
23 #pragma once
24 
25 #include <cstdint>
26 #include <memory>
27 #include <algorithm>
28 #include <ostream>
29 
30 namespace libCZI
31 {
33  struct IntRect
34  {
35  int x;
36  int y;
37  int w;
38  int h;
39 
41  void Invalidate() { this->w = this->h = -1; }
42 
44  bool IsValid() const { return this->w >= 0 && this->h >= 0; }
45 
49  bool IntersectsWith(const IntRect& r) const
50  {
51  const IntRect is = this->Intersect(r);
52  if (is.w <= 0 || is.h <= 0)
53  {
54  return false;
55  }
56 
57  return true;
58  }
59 
65  IntRect Intersect(const IntRect& r) const
66  {
67  return IntRect::Intersect(*this, r);
68  }
69 
76  static IntRect Intersect(const IntRect& a, const IntRect& b)
77  {
78  int x1 = (std::max)(a.x, b.x);
79  int x2 = (std::min)(a.x + a.w, b.x + b.w);
80  int y1 = (std::max)(a.y, b.y);
81  int y2 = (std::min)(a.y + a.h, b.y + b.h);
82  if (x2 >= x1&& y2 >= y1)
83  {
84  return IntRect{ x1, y1, x2 - x1, y2 - y1 };
85  }
86 
87  return IntRect{ 0,0,0,0 };
88  }
89  };
90 
92  struct DblRect
93  {
94  double x;
95  double y;
96  double w;
97  double h;
98 
100  void Invalidate() { this->w = this->h = -1; }
101  };
102 
104  struct IntSize
105  {
106  std::uint32_t w;
107  std::uint32_t h;
108  };
109 
111  struct Rgb8Color
112  {
113  std::uint8_t r;
114  std::uint8_t g;
115  std::uint8_t b;
116  };
117 
120  {
121  float r;
122  float g;
123  float b;
124  };
125 
127  enum class PixelType : std::uint8_t
128  {
129  Invalid = 0xff,
130  Gray8 = 0,
131  Gray16 = 1,
132  Gray32Float = 2,
133  Bgr24 = 3,
134  Bgr48 = 4,
135  Bgr96Float = 8,
136  Bgra32 = 9,
137  Gray64ComplexFloat = 10,
138  Bgr192ComplexFloat = 11,
139  Gray32 = 12,
140  Gray64Float = 13,
141  };
142 
144  enum class CompressionMode : std::uint8_t
145  {
146  Invalid = 0xff,
147  UnCompressed = 0,
148  Jpg = 1,
149  JpgXr = 4
150  };
151 
154  {
155  void* ptrData;
156  void* ptrDataRoi;
157  std::uint32_t stride;
158  std::uint64_t size;
159  };
160 
168  {
169  public:
170 
174  virtual PixelType GetPixelType() const = 0;
175 
179  virtual IntSize GetSize() const = 0;
180 
189  virtual BitmapLockInfo Lock() = 0;
190 
195  virtual void Unlock() = 0;
196 
197  virtual ~IBitmapData() {};
198 
202  std::uint32_t GetWidth() const { return this->GetSize().w; }
203 
207  std::uint32_t GetHeight() const { return this->GetSize().h; }
208  };
209 
254  template <typename tBitmap>
256  {
257  private:
258  tBitmap bmData;
259  public:
260  ScopedBitmapLocker() = delete;
261 
264  explicit ScopedBitmapLocker(tBitmap bmData) : bmData(bmData)
265  {
266  auto lockInfo = bmData->Lock();
267  this->ptrData = lockInfo.ptrData;
268  this->ptrDataRoi = lockInfo.ptrDataRoi;
269  this->stride = lockInfo.stride;
270  this->size = lockInfo.size;
271  }
272 
275  ScopedBitmapLocker(const ScopedBitmapLocker<tBitmap>& other) : bmData(other.bmData)
276  {
277  auto lockInfo = other.bmData->Lock();
278  this->ptrData = lockInfo.ptrData;
279  this->ptrDataRoi = lockInfo.ptrDataRoi;
280  this->stride = lockInfo.stride;
281  this->size = lockInfo.size;
282  }
283 
285  ScopedBitmapLocker(ScopedBitmapLocker<tBitmap>&& other) noexcept : bmData(tBitmap())
286  {
287  *this = std::move(other);
288  }
289 
292  {
293  if (this != &other)
294  {
295  if (this->bmData)
296  {
297  this->bmData->Unlock();
298  }
299 
300  this->bmData = std::move(other.bmData);
301  this->ptrData = other.ptrData;
302  this->ptrDataRoi = other.ptrDataRoi;
303  this->stride = other.stride;
304  this->size = other.size;
305  other.ptrData = other.ptrDataRoi = nullptr;
306  other.bmData = tBitmap();
307  }
308 
309  return *this;
310  }
311 
313  {
314  if (this->bmData)
315  {
316  this->bmData->Unlock();
317  }
318  }
319  };
320 
323 
326 
327  //-------------------------------------------------------------------------
328 
333  inline std::ostream& operator<<(std::ostream& os, const IntRect& rect)
334  {
335  os << "(" << rect.x << "," << rect.y << "," << rect.w << "," << rect.h << ")";
336  return os;
337  }
338 
343  inline std::ostream& operator<<(std::ostream& os, const IntSize& size)
344  {
345  os << "(" << size.w << "," << size.h << ")";
346  return os;
347  }
348 }
PixelType
An enum representing a pixel-type.
Definition: libCZI_Pixels.h:127
A rectangle (with double coordinates).
Definition: libCZI_Pixels.h:92
Definition: libCZI_Pixels.h:167
CompressionMode
An enum specifying the compression method.
Definition: libCZI_Pixels.h:144
BGR-color 4 byte float triples (memory order B, G, R).
void * ptrData
Not currently used, to be ignored.
Definition: libCZI_Pixels.h:155
Invalid pixel type.
Definition: libCZI_Pixels.h:255
Currently not supported in libCZI.
Information about a locked bitmap - allowing direct access to the image data in memory.
Definition: libCZI_Pixels.h:153
std::uint32_t GetWidth() const
Definition: libCZI_Pixels.h:202
The data is JPG-XR-compressed.
std::ostream & operator<<(std::ostream &os, const IntRect &rect)
Definition: libCZI_Pixels.h:333
int w
The width of the rectangle.
Definition: libCZI_Pixels.h:37
BGR-color 16-bytes triples (memory order B, G, R).
std::uint32_t h
The height.
Definition: libCZI_Pixels.h:107
int h
The height of the rectangle.
Definition: libCZI_Pixels.h:38
double y
The y-coordinate of the upper-left point of the rectangle.
Definition: libCZI_Pixels.h:95
Currently not supported in libCZI.
std::uint64_t size
The size of the bitmap data (pointed to by ptrDataRoi) in bytes.
Definition: libCZI_Pixels.h:158
std::uint8_t b
The blue component.
Definition: libCZI_Pixels.h:115
ScopedBitmapLocker(tBitmap bmData)
Definition: libCZI_Pixels.h:264
bool IntersectsWith(const IntRect &r) const
Definition: libCZI_Pixels.h:49
A structure representing an R-G-B-color triple (as bytes).
Definition: libCZI_Pixels.h:111
The data is uncompressed.
std::uint32_t stride
The stride of the bitmap data (pointed to by ptrDataRoi).
Definition: libCZI_Pixels.h:157
float r
The red component.
Definition: libCZI_Pixels.h:121
Currently not supported in libCZI.
ScopedBitmapLocker< tBitmap > & operator=(ScopedBitmapLocker< tBitmap > &&other) noexcept
move assignment
Definition: libCZI_Pixels.h:291
static IntRect Intersect(const IntRect &a, const IntRect &b)
Definition: libCZI_Pixels.h:76
double h
The height of the rectangle.
Definition: libCZI_Pixels.h:97
BGR-color 8-bytes triples (memory order B, G, R).
Grayscale 16-bit unsinged.
Grayscale 8-bit unsinged.
double x
The x-coordinate of the upper-left point of the rectangle.
Definition: libCZI_Pixels.h:94
std::uint8_t r
The red component.
Definition: libCZI_Pixels.h:113
ScopedBitmapLocker(ScopedBitmapLocker< tBitmap > &&other) noexcept
move constructor
Definition: libCZI_Pixels.h:285
float g
The green component.
Definition: libCZI_Pixels.h:122
ScopedBitmapLocker(const ScopedBitmapLocker< tBitmap > &other)
Definition: libCZI_Pixels.h:275
void Invalidate()
Invalidates this object.
Definition: libCZI_Pixels.h:41
std::uint32_t GetHeight() const
Definition: libCZI_Pixels.h:207
A rectangle (with integer coordinates).
Definition: libCZI_Pixels.h:33
A structure representing an R-G-B-color triple (as floats).
Definition: libCZI_Pixels.h:119
std::uint8_t g
The green component.
Definition: libCZI_Pixels.h:114
External interfaces, classes, functions and structs are found in the namespace "libCZI".
Definition: libCZI.h:45
int y
The y-coordinate of the upper-left point of the rectangle.
Definition: libCZI_Pixels.h:36
double w
The width of the rectangle.
Definition: libCZI_Pixels.h:96
Grayscale 4 byte float.
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:325
float b
The blue component.
Definition: libCZI_Pixels.h:123
Currently not supported in libCZI.
The data is JPG-compressed.
A structure representing a size (width and height) in integers.
Definition: libCZI_Pixels.h:104
bool IsValid() const
Returns a boolean indicating whether this rectangle contains valid information.
Definition: libCZI_Pixels.h:44
ScopedBitmapLocker< IBitmapData * > ScopedBitmapLockerP
Defines an alias representing the scoped bitmap locker for use with libCZI::IBitmapData.
Definition: libCZI_Pixels.h:322
void Invalidate()
Invalidates this object.
Definition: libCZI_Pixels.h:100
int x
The x-coordinate of the upper-left point of the rectangle.
Definition: libCZI_Pixels.h:35
Currently not supported in libCZI.
void * ptrDataRoi
The pointer to the first (top-left) pixel of the bitmap.
Definition: libCZI_Pixels.h:156
IntRect Intersect(const IntRect &r) const
Definition: libCZI_Pixels.h:65
std::uint32_t w
The width.
Definition: libCZI_Pixels.h:106