libCZI
Reading and Writing CZI documents made easy
Loading...
Searching...
No Matches
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
13namespace 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 IntPoint
79 {
80 int x;
81 int y;
82 };
83
85 enum class CZIFrameOfReference : std::uint8_t
86 {
87 Invalid,
88 Default,
91 };
92
99
106
108 struct DblRect
109 {
110 double x;
111 double y;
112 double w;
113 double h;
114
116 void Invalidate() { this->w = this->h = -1; }
117 };
118
120 struct IntSize
121 {
122 std::uint32_t w;
123 std::uint32_t h;
124 };
125
128 {
129 std::uint8_t r;
130 std::uint8_t g;
131 std::uint8_t b;
132 };
133
136 {
137 float r;
138 float g;
139 float b;
140 };
141
143 enum class PixelType : std::uint8_t
144 {
145 Invalid = 0xff,
146 Gray8 = 0,
147 Gray16 = 1,
148 Gray32Float = 2,
149 Bgr24 = 3,
150 Bgr48 = 4,
151 Bgr96Float = 8,
152 Bgra32 = 9,
153 Gray64ComplexFloat = 10,
154 Bgr192ComplexFloat = 11,
155 Gray32 = 12,
156 Gray64Float = 13,
157 };
158
160 enum class CompressionMode : std::uint8_t
161 {
162 Invalid = 0xff,
163 UnCompressed = 0,
164 Jpg = 1,
165 JpgXr = 4,
166 Zstd0 = 5,
167 Zstd1 = 6
168 };
169
173 enum class SubBlockPyramidType : std::uint8_t
174 {
175 Invalid = 0xff,
176 None = 0,
177 SingleSubBlock= 1,
178 MultiSubBlock = 2
179 };
180
183 {
184 void* ptrData;
186 std::uint32_t stride;
187 std::uint64_t size;
188 };
189
197 {
198 public:
199
203 virtual PixelType GetPixelType() const = 0;
204
208 virtual IntSize GetSize() const = 0;
209
218 virtual BitmapLockInfo Lock() = 0;
219
224 virtual void Unlock() = 0;
225
226 virtual ~IBitmapData() {}
227
231 std::uint32_t GetWidth() const { return this->GetSize().w; }
232
236 std::uint32_t GetHeight() const { return this->GetSize().h; }
237 };
238
283 template <typename tBitmap>
285 {
286 private:
287 tBitmap bmData;
288 public:
289 ScopedBitmapLocker() = delete;
290
293 explicit ScopedBitmapLocker(tBitmap bmData) : bmData(bmData)
294 {
295 auto lockInfo = bmData->Lock();
296 this->ptrData = lockInfo.ptrData;
297 this->ptrDataRoi = lockInfo.ptrDataRoi;
298 this->stride = lockInfo.stride;
299 this->size = lockInfo.size;
300 }
301
304 ScopedBitmapLocker(const ScopedBitmapLocker<tBitmap>& other) : bmData(other.bmData)
305 {
306 auto lockInfo = other.bmData->Lock();
307 this->ptrData = lockInfo.ptrData;
308 this->ptrDataRoi = lockInfo.ptrDataRoi;
309 this->stride = lockInfo.stride;
310 this->size = lockInfo.size;
311 }
312
314 ScopedBitmapLocker(ScopedBitmapLocker<tBitmap>&& other) noexcept : bmData(tBitmap())
315 {
316 *this = std::move(other);
317 }
318
321 {
322 if (this != &other)
323 {
324 if (this->bmData)
325 {
326 this->bmData->Unlock();
327 }
328
329 this->bmData = std::move(other.bmData);
330 this->ptrData = other.ptrData;
331 this->ptrDataRoi = other.ptrDataRoi;
332 this->stride = other.stride;
333 this->size = other.size;
334 other.ptrData = other.ptrDataRoi = nullptr;
335 other.bmData = tBitmap();
336 }
337
338 return *this;
339 }
340
342 {
343 if (this->bmData)
344 {
345 this->bmData->Unlock();
346 }
347 }
348 };
349
352
355
356 //-------------------------------------------------------------------------
357
362 inline std::ostream& operator<<(std::ostream& os, const IntRect& rect)
363 {
364 os << "(" << rect.x << "," << rect.y << "," << rect.w << "," << rect.h << ")";
365 return os;
366 }
367
372 inline std::ostream& operator<<(std::ostream& os, const IntSize& size)
373 {
374 os << "(" << size.w << "," << size.h << ")";
375 return os;
376 }
377}
Definition libCZI_Pixels.h:197
std::uint32_t GetHeight() const
Definition libCZI_Pixels.h:236
virtual void Unlock()=0
virtual PixelType GetPixelType() const =0
std::uint32_t GetWidth() const
Definition libCZI_Pixels.h:231
virtual IntSize GetSize() const =0
virtual BitmapLockInfo Lock()=0
Definition libCZI_Pixels.h:285
ScopedBitmapLocker< tBitmap > & operator=(ScopedBitmapLocker< tBitmap > &&other) noexcept
move assignment
Definition libCZI_Pixels.h:320
ScopedBitmapLocker(const ScopedBitmapLocker< tBitmap > &other)
Definition libCZI_Pixels.h:304
ScopedBitmapLocker(tBitmap bmData)
Definition libCZI_Pixels.h:293
ScopedBitmapLocker(ScopedBitmapLocker< tBitmap > &&other) noexcept
move constructor
Definition libCZI_Pixels.h:314
External interfaces, classes, functions and structs are found in the namespace "libCZI".
Definition libCZI.h:31
std::ostream & operator<<(std::ostream &os, const IntRect &rect)
Definition libCZI_Pixels.h:362
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:354
@ None
No camera processing was used.
CompressionMode
An enum specifying the compression method.
Definition libCZI_Pixels.h:161
@ 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:174
@ 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...
@ Default
An enum constant representing the default option (which is JXRLib)
CZIFrameOfReference
Values that represent different frame of reference.
Definition libCZI_Pixels.h:86
@ Invalid
Invalid frame of reference.
@ PixelCoordinateSystem
The pixel coordinate system.
@ RawSubBlockCoordinateSystem
The raw sub-block coordinate system.
ScopedBitmapLocker< IBitmapData * > ScopedBitmapLockerP
Defines an alias representing the scoped bitmap locker for use with libCZI::IBitmapData.
Definition libCZI_Pixels.h:351
PixelType
An enum representing a pixel-type.
Definition libCZI_Pixels.h:144
@ Gray64ComplexFloat
Currently not supported in libCZI.
@ Gray16
Grayscale 16-bit unsigned.
@ Bgra32
Currently not supported in libCZI.
@ 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).
Information about a locked bitmap - allowing direct access to the image data in memory.
Definition libCZI_Pixels.h:183
std::uint64_t size
The size of the bitmap data (pointed to by ptrDataRoi) in bytes.
Definition libCZI_Pixels.h:187
void * ptrData
Not currently used, to be ignored.
Definition libCZI_Pixels.h:184
std::uint32_t stride
The stride of the bitmap data (pointed to by ptrDataRoi).
Definition libCZI_Pixels.h:186
void * ptrDataRoi
The pointer to the first (top-left) pixel of the bitmap.
Definition libCZI_Pixels.h:185
A rectangle (with double coordinates).
Definition libCZI_Pixels.h:109
double h
The height of the rectangle.
Definition libCZI_Pixels.h:113
double w
The width of the rectangle.
Definition libCZI_Pixels.h:112
double y
The y-coordinate of the upper-left point of the rectangle.
Definition libCZI_Pixels.h:111
void Invalidate()
Invalidates this object.
Definition libCZI_Pixels.h:116
double x
The x-coordinate of the upper-left point of the rectangle.
Definition libCZI_Pixels.h:110
This structure combines a point with a specification of the frame of reference.
Definition libCZI_Pixels.h:102
libCZI::CZIFrameOfReference frame_of_reference
The frame of reference.
Definition libCZI_Pixels.h:103
IntPoint point
The point.
Definition libCZI_Pixels.h:104
A point (with integer coordinates).
Definition libCZI_Pixels.h:79
This structure combines a rectangle with a specification of the frame of reference.
Definition libCZI_Pixels.h:95
libCZI::CZIFrameOfReference frame_of_reference
The frame of reference.
Definition libCZI_Pixels.h:96
libCZI::IntRect rectangle
The rectangle.
Definition libCZI_Pixels.h:97
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:121
std::uint32_t h
The height.
Definition libCZI_Pixels.h:123
std::uint32_t w
The width.
Definition libCZI_Pixels.h:122
A structure representing an R-G-B-color triple (as bytes).
Definition libCZI_Pixels.h:128
std::uint8_t b
The blue component.
Definition libCZI_Pixels.h:131
std::uint8_t g
The green component.
Definition libCZI_Pixels.h:130
std::uint8_t r
The red component.
Definition libCZI_Pixels.h:129
A structure representing an R-G-B-color triple (as floats).
Definition libCZI_Pixels.h:136
float r
The red component.
Definition libCZI_Pixels.h:137
float b
The blue component.
Definition libCZI_Pixels.h:139
float g
The green component.
Definition libCZI_Pixels.h:138