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 
29 namespace libCZI
30 {
32  struct IntRect
33  {
34  int x;
35  int y;
36  int w;
37  int h;
38 
40  void Invalidate() { this->w = this->h = -1; }
41 
43  bool IsValid() const { return this->w >= 0 && this->h >= 0; }
44 
48  bool IntersectsWith(const IntRect& r) const
49  {
50  IntRect is = this->Intersect(r);
51  if (is.w <= 0 || is.h <= 0)
52  {
53  return false;
54  }
55 
56  return true;
57  }
58 
64  IntRect Intersect(const IntRect& r) const
65  {
66  return IntRect::Intersect(*this, r);
67  }
68 
75  static IntRect Intersect(const IntRect& a, const IntRect& b)
76  {
77  int x1 = (std::max)(a.x, b.x);
78  int x2 = (std::min)(a.x + a.w, b.x + b.w);
79  int y1 = (std::max)(a.y, b.y);
80  int y2 = (std::min)(a.y + a.h, b.y + b.h);
81  if (x2 >= x1&& y2 >= y1)
82  {
83  return IntRect{ x1, y1, x2 - x1, y2 - y1 };
84  }
85 
86  return IntRect{ 0,0,0,0 };
87  }
88  };
89 
91  struct DblRect
92  {
93  double x;
94  double y;
95  double w;
96  double h;
97 
99  void Invalidate() { this->w = this->h = -1; }
100  };
101 
103  struct IntSize
104  {
105  std::uint32_t w;
106  std::uint32_t h;
107  };
108 
110  struct Rgb8Color
111  {
112  std::uint8_t r;
113  std::uint8_t g;
114  std::uint8_t b;
115  };
116 
119  {
120  float r;
121  float g;
122  float b;
123  };
124 
126  enum class PixelType : std::uint8_t
127  {
128  Invalid = 0xff,
129  Gray8 = 0,
130  Gray16 = 1,
131  Gray32Float = 2,
132  Bgr24 = 3,
133  Bgr48 = 4,
134  Bgr96Float = 8,
135  Bgra32 = 9,
136  Gray64ComplexFloat = 10,
137  Bgr192ComplexFloat = 11,
138  Gray32 = 12,
139  Gray64Float = 13,
140  };
141 
143  enum class CompressionMode : std::uint8_t
144  {
145  Invalid = 0xff,
146  UnCompressed = 0,
147  Jpg = 1,
148  JpgXr = 4
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 
199  std::uint32_t GetWidth() const { return this->GetSize().w; }
200 
204  std::uint32_t GetHeight() const { return this->GetSize().h; }
205  };
206 
251  template <typename tBitmap>
253  {
254  private:
255  tBitmap bmData;
256  public:
257  ScopedBitmapLocker() = delete;
258 
261  explicit ScopedBitmapLocker(tBitmap bmData) : bmData(bmData)
262  {
263  auto lockInfo = bmData->Lock();
264  this->ptrData = lockInfo.ptrData;
265  this->ptrDataRoi = lockInfo.ptrDataRoi;
266  this->stride = lockInfo.stride;
267  this->size = lockInfo.size;
268  }
269 
272  ScopedBitmapLocker(const ScopedBitmapLocker<tBitmap>& other) : bmData(other.bmData)
273  {
274  auto lockInfo = other.bmData->Lock();
275  this->ptrData = lockInfo.ptrData;
276  this->ptrDataRoi = lockInfo.ptrDataRoi;
277  this->stride = lockInfo.stride;
278  this->size = lockInfo.size;
279  }
280 
282  ScopedBitmapLocker(ScopedBitmapLocker<tBitmap>&& other) noexcept : bmData(tBitmap())
283  {
284  *this = std::move(other);
285  }
286 
289  {
290  if (this != &other)
291  {
292  if (this->bmData)
293  {
294  this->bmData->Unlock();
295  }
296 
297  this->bmData = std::move(other.bmData);
298  this->ptrData = other.ptrData;
299  this->ptrDataRoi = other.ptrDataRoi;
300  this->stride = other.stride;
301  this->size = other.size;
302  other.ptrData = other.ptrDataRoi = nullptr;
303  other.bmData = tBitmap();
304  }
305 
306  return *this;
307  }
308 
310  {
311  if (this->bmData)
312  {
313  this->bmData->Unlock();
314  }
315  }
316  };
317 
320 
323 
324  //-------------------------------------------------------------------------
325 
330  inline std::ostream& operator<<(std::ostream& os, const IntRect& rect)
331  {
332  os << "(" << rect.x << "," << rect.y << "," << rect.w << "," << rect.h << ")";
333  return os;
334  }
335 
340  inline std::ostream& operator<<(std::ostream& os, const IntSize& size)
341  {
342  os << "(" << size.w << "," << size.h << ")";
343  return os;
344  }
345 }
PixelType
An enum representing a pixel-type.
Definition: libCZI_Pixels.h:126
A rectangle (with double coordinates).
Definition: libCZI_Pixels.h:91
Definition: libCZI_Pixels.h:166
CompressionMode
An enum specifying the compression method.
Definition: libCZI_Pixels.h:143
BGR-color 4 byte float triples (memory order B, G, R).
void * ptrData
Not currently used, to be ignored.
Definition: libCZI_Pixels.h:154
Invalid pixel type.
Definition: libCZI_Pixels.h:252
Currently not supported in libCZI.
virtual PixelType GetPixelType() const =0
Information about a locked bitmap - allowing direct access to the image data in memory.
Definition: libCZI_Pixels.h:152
std::uint32_t GetWidth() const
Definition: libCZI_Pixels.h:199
The data is JPG-XR-compressed.
std::ostream & operator<<(std::ostream &os, const IntRect &rect)
Definition: libCZI_Pixels.h:330
int w
The width of the rectangle.
Definition: libCZI_Pixels.h:36
BGR-color 16-bytes triples (memory order B, G, R).
std::uint32_t h
The height.
Definition: libCZI_Pixels.h:106
int h
The height of the rectangle.
Definition: libCZI_Pixels.h:37
double y
The y-coordinate of the upper-left point of the rectangle.
Definition: libCZI_Pixels.h:94
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:157
std::uint8_t b
The blue component.
Definition: libCZI_Pixels.h:114
ScopedBitmapLocker(tBitmap bmData)
Definition: libCZI_Pixels.h:261
bool IntersectsWith(const IntRect &r) const
Definition: libCZI_Pixels.h:48
A structure representing an R-G-B-color triple (as bytes).
Definition: libCZI_Pixels.h:110
The data is uncompressed.
std::uint32_t stride
The stride of the bitmap data (pointed to by ptrDataRoi).
Definition: libCZI_Pixels.h:156
float r
The red component.
Definition: libCZI_Pixels.h:120
Currently not supported in libCZI.
ScopedBitmapLocker< tBitmap > & operator=(ScopedBitmapLocker< tBitmap > &&other) noexcept
move assignment
Definition: libCZI_Pixels.h:288
static IntRect Intersect(const IntRect &a, const IntRect &b)
Definition: libCZI_Pixels.h:75
double h
The height of the rectangle.
Definition: libCZI_Pixels.h:96
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:93
std::uint8_t r
The red component.
Definition: libCZI_Pixels.h:112
ScopedBitmapLocker(ScopedBitmapLocker< tBitmap > &&other) noexcept
move constructor
Definition: libCZI_Pixels.h:282
virtual BitmapLockInfo Lock()=0
float g
The green component.
Definition: libCZI_Pixels.h:121
ScopedBitmapLocker(const ScopedBitmapLocker< tBitmap > &other)
Definition: libCZI_Pixels.h:272
void Invalidate()
Invalidates this object.
Definition: libCZI_Pixels.h:40
std::uint32_t GetHeight() const
Definition: libCZI_Pixels.h:204
A rectangle (with integer coordinates).
Definition: libCZI_Pixels.h:32
A structure representing an R-G-B-color triple (as floats).
Definition: libCZI_Pixels.h:118
virtual IntSize GetSize() const =0
std::uint8_t g
The green component.
Definition: libCZI_Pixels.h:113
External interfaces, classes, functions and structs are found in the namespace "libCZI".
Definition: libCZI.h:44
virtual void Unlock()=0
int y
The y-coordinate of the upper-left point of the rectangle.
Definition: libCZI_Pixels.h:35
double w
The width of the rectangle.
Definition: libCZI_Pixels.h:95
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:322
float b
The blue component.
Definition: libCZI_Pixels.h:122
Currently not supported in libCZI.
The data is JPG-compressed.
A structure representing a size (width and height) in integers.
Definition: libCZI_Pixels.h:103
bool IsValid() const
Returns a boolean indicating whether this rectangle contains valid information.
Definition: libCZI_Pixels.h:43
ScopedBitmapLocker< IBitmapData * > ScopedBitmapLockerP
Defines an alias representing the scoped bitmap locker for use with libCZI::IBitmapData.
Definition: libCZI_Pixels.h:319
void Invalidate()
Invalidates this object.
Definition: libCZI_Pixels.h:99
int x
The x-coordinate of the upper-left point of the rectangle.
Definition: libCZI_Pixels.h:34
Currently not supported in libCZI.
void * ptrDataRoi
The pointer to the first (top-left) pixel of the bitmap.
Definition: libCZI_Pixels.h:155
IntRect Intersect(const IntRect &r) const
Definition: libCZI_Pixels.h:64
std::uint32_t w
The width.
Definition: libCZI_Pixels.h:105