libCZI
Reading and Writing CZI documents made easy
Loading...
Searching...
No Matches
libCZI::ScopedBitmapLocker< tBitmap > Class Template Reference

#include <libCZI_Pixels.h>

Inheritance diagram for libCZI::ScopedBitmapLocker< tBitmap >:
libCZI::BitmapLockInfo

Public Member Functions

 ScopedBitmapLocker (tBitmap bmData)
 
 ScopedBitmapLocker (const ScopedBitmapLocker< tBitmap > &other)
 
 ScopedBitmapLocker (ScopedBitmapLocker< tBitmap > &&other) noexcept
 move constructor
 
ScopedBitmapLocker< tBitmap > & operator= (ScopedBitmapLocker< tBitmap > &&other) noexcept
 move assignment
 

Additional Inherited Members

- Public Attributes inherited from libCZI::BitmapLockInfo
void * ptrData
 Not currently used, to be ignored.
 
void * ptrDataRoi
 The pointer to the first (top-left) pixel of the bitmap.
 
std::uint32_t stride
 The stride of the bitmap data (pointed to by ptrDataRoi).
 
std::uint64_t size
 The size of the bitmap data (pointed to by ptrDataRoi) in bytes.
 

Detailed Description

template<typename tBitmap>
class libCZI::ScopedBitmapLocker< tBitmap >

A helper class used to scope the lock state of a bitmap.

It is intended to be used like this:

libCZI::IBitmapData* bm = ... // assume that we have a pointer to a bitmap
// this example assumes that the pixel type is Gray8 and nothing else...
if (bm->GetPixelType() != libCZI::PixelType::Gray8) throw std::exception();
{
// access the bitmap's pixels directly within this scope
libCZI::ScopedBitmapLocker<libCZI::IBitmapData*> lckBm{ bm }; // <- will call bm->Lock here
for (std::uint32_t y = 0; y < bm->GetHeight(); ++y)
{
const std::uint8_t* ptrLine = ((const std::uint8_t*)lckBm.ptrDataRoi) + y * lckBm.stride;
for (std::uint32_t x = 0; x < bm->GetWidth(); ++x)
{
auto pixelVal = ptrLine[x];
// do something with the pixel...
}
}
// when lckBm goes out of scope, bm->Unlock will be called
}
Definition libCZI_Pixels.h:197
std::uint32_t GetHeight() const
Definition libCZI_Pixels.h:236
virtual PixelType GetPixelType() const =0
std::uint32_t GetWidth() const
Definition libCZI_Pixels.h:231
Definition libCZI_Pixels.h:285
@ Gray8
Grayscale 8-bit unsigned.

For convenience two typedef are provided: ScopedBitmapLockerP and ScopedBitmapLockerSP for use with the types IBitmapData* and std::shared_ptr<IBitmapData>.

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
ScopedBitmapLocker< IBitmapData * > ScopedBitmapLockerP
Defines an alias representing the scoped bitmap locker for use with libCZI::IBitmapData.
Definition libCZI_Pixels.h:351

So in above sample we could have used

This utility is intended to help adhering to the RAII-pattern, since it makes writing exception-safe code easier - in case of an exception (within the scope of the ScopedBitmapLocker object) the bitmap's Unlock method will be called (which is cumbersome to achieve otherwise).

Constructor & Destructor Documentation

◆ ScopedBitmapLocker() [1/2]

template<typename tBitmap >
libCZI::ScopedBitmapLocker< tBitmap >::ScopedBitmapLocker ( tBitmap  bmData)
inlineexplicit

Constructor taking the object for which we provide the scope-guard.

Parameters
bmDataThe object for which we are to provide the scope-guard.

◆ ScopedBitmapLocker() [2/2]

template<typename tBitmap >
libCZI::ScopedBitmapLocker< tBitmap >::ScopedBitmapLocker ( const ScopedBitmapLocker< tBitmap > &  other)
inline

Copy-Constructor .

Parameters
otherThe other object.

The documentation for this class was generated from the following file: