Beatmup
Beatmup::Metric Class Reference

Measures the difference between two bitmaps. More...

#include <metric.h>

Inheritance diagram for Beatmup::Metric:
Beatmup::AbstractTask Beatmup::BitmapContentLock Beatmup::Object

Public Types

enum class  Norm { L1 , L2 }
 Norm (distance) to measure between two images. More...
 
- Public Types inherited from Beatmup::AbstractTask
enum class  TaskDeviceRequirement { CPU_ONLY , GPU_OR_CPU , GPU_ONLY }
 Specifies which device (CPU and/or GPU) is used to run the task. More...
 

Public Member Functions

 Metric ()
 
void setBitmaps (AbstractBitmap *bitmap1, AbstractBitmap *bitmap2)
 Sets input images. More...
 
void setBitmaps (AbstractBitmap *bitmap1, const IntRectangle &roi1, AbstractBitmap *bitmap2, const IntRectangle &roi2)
 Sets input images and rectangular regions delimiting the measurement areas. More...
 
void setNorm (Norm norm)
 Specifies the norm to use in the measurement. More...
 
double getResult () const
 
- Public Member Functions inherited from Beatmup::AbstractTask
virtual bool processOnGPU (GraphicPipeline &gpu, TaskThread &thread)
 Executes the task on GPU. More...
 
virtual TaskDeviceRequirement getUsedDevices () const
 Communicates devices (CPU and/or GPU) the task is run on. More...
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Static Public Member Functions

static float psnr (AbstractBitmap &bitmap1, AbstractBitmap &bitmap2)
 
- Static Public Member Functions inherited from Beatmup::AbstractTask
static ThreadIndex validThreadCount (int number)
 Valid thread count from a given integer value. More...
 

Private Member Functions

ThreadIndex getMaxThreads () const
 Gives the upper limint on the number of threads the task may be performed by. More...
 
void beforeProcessing (ThreadIndex threadCount, ProcessingTarget target, GraphicPipeline *gpu)
 Instruction called before the task is executed. More...
 
void afterProcessing (ThreadIndex threadCount, GraphicPipeline *gpu, bool aborted)
 Instruction called after the task is executed. More...
 
bool process (TaskThread &thread)
 Executes the task on CPU within a given thread. More...
 
- Private Member Functions inherited from Beatmup::BitmapContentLock
 BitmapContentLock ()
 
 ~BitmapContentLock ()
 
void readLock (GraphicPipeline *gpu, AbstractBitmap *bitmap, ProcessingTarget target)
 Locks content of a bitmap for reading using a specific processing target device. More...
 
void writeLock (GraphicPipeline *gpu, AbstractBitmap *bitmap, ProcessingTarget target)
 Locks content of a bitmap for writing using a specific processing target device. More...
 
void unlock (AbstractBitmap *bitmap)
 Drops a lock to the bitmap. More...
 
void unlockAll ()
 Unlocks all the locked bitmaps unconditionally. More...
 
template<const ProcessingTarget target>
void lock (GraphicPipeline *gpu, AbstractBitmap *input, AbstractBitmap *output)
 
void lock (GraphicPipeline *gpu, ProcessingTarget target, AbstractBitmap *input, AbstractBitmap *output)
 
template<const ProcessingTarget target>
void lock (GraphicPipeline *gpu, std::initializer_list< AbstractBitmap * > read, std::initializer_list< AbstractBitmap * > write)
 
template<typename ... Args>
void unlock (AbstractBitmap *first, Args ... others)
 

Private Attributes

AbstractBitmapbitmap [2]
 
IntRectangle roi [2]
 
Norm norm
 
std::vector< double > results
 
double result
 

Detailed Description

Measures the difference between two bitmaps.

Definition at line 29 of file metric.h.

Member Enumeration Documentation

◆ Norm

enum Beatmup::Metric::Norm
strong

Norm (distance) to measure between two images.

Enumerator
L1 
L2 

Definition at line 34 of file metric.h.

34  {
35  L1,
36  L2
37  };

Constructor & Destructor Documentation

◆ Metric()

Metric::Metric ( )

Definition at line 68 of file metric.cpp.

68  : bitmap{ nullptr, nullptr }, norm(Norm::L2), result(0)
69 {}
double result
Definition: metric.h:44
AbstractBitmap * bitmap[2]
Definition: metric.h:40

Member Function Documentation

◆ getMaxThreads()

ThreadIndex Beatmup::Metric::getMaxThreads ( ) const
inlineprivatevirtual

Gives the upper limint on the number of threads the task may be performed by.

The actual number of threads running a specific task may be less or equal to the returned value, depending on the number of workers in ThreadPool running the task.

Reimplemented from Beatmup::AbstractTask.

Definition at line 46 of file metric.h.

46 { return MAX_THREAD_INDEX; }
static const ThreadIndex MAX_THREAD_INDEX
maximum possible thread index value
Definition: parallelism.h:71

◆ beforeProcessing()

void Metric::beforeProcessing ( ThreadIndex  threadCount,
ProcessingTarget  target,
GraphicPipeline gpu 
)
privatevirtual

Instruction called before the task is executed.

Parameters
threadCountNumber of threads used to perform the task
targetDevice used to perform the task
gpuA graphic pipeline instance; may be null.

Reimplemented from Beatmup::AbstractTask.

Definition at line 90 of file metric.cpp.

90  {
91  NullTaskInput::check(bitmap[0], "bitmap 1");
92  NullTaskInput::check(bitmap[1], "bitmap 2");
93  RuntimeError::check(bitmap[0]->getPixelFormat() == bitmap[1]->getPixelFormat(), "Pixel format mismatch");
94  RuntimeError::check(roi[0] && roi[1], "Regions of interest are of different size");
97  results.resize(threadCount);
98 }
void readLock(GraphicPipeline *gpu, AbstractBitmap *bitmap, ProcessingTarget target)
Locks content of a bitmap for reading using a specific processing target device.
IntRectangle roi[2]
Definition: metric.h:41
std::vector< double > results
Definition: metric.h:43
static void check(const void *pointer, const char *which)
Definition: exception.h:115
static void check(const bool condition, const std::string &message)
Definition: exception.h:64
return bitmap getPixelFormat()

◆ afterProcessing()

void Metric::afterProcessing ( ThreadIndex  threadCount,
GraphicPipeline gpu,
bool  aborted 
)
privatevirtual

Instruction called after the task is executed.

Parameters
threadCountNumber of threads used to perform the task
gpuGPU to be used to execute the task; may be null.
abortedtrue if the task was aborted

Reimplemented from Beatmup::AbstractTask.

Definition at line 101 of file metric.cpp.

101  {
102  unlock(bitmap[0], bitmap[1]);
103 
104  double sum = 0;
105  for (const auto& _ : results)
106  sum += _;
107 
108  switch (norm) {
109  case Norm::L1:
110  result = sum;
111  break;
112 
113  case Norm::L2:
114  result = std::sqrt(sum);
115  break;
116  }
117 }
void unlock(AbstractBitmap *bitmap)
Drops a lock to the bitmap.

◆ process()

bool Metric::process ( TaskThread thread)
privatevirtual

Executes the task on CPU within a given thread.

Generally called by multiple threads.

Parameters
threadassociated task execution context
Returns
true if the execution is finished correctly, false otherwise

Implements Beatmup::AbstractTask.

Definition at line 120 of file metric.cpp.

120  {
121  const IntRectangle
122  myRoi1 = roi[0].split(thread.currentThread(), thread.numThreads()),
123  myRoi2 = roi[1].split(thread.currentThread(), thread.numThreads());
124 
125  switch (norm) {
126  case Norm::L1:
127  BitmapProcessing::write<Kernels::ComputeL1Metric>(*bitmap[0], *bitmap[1], myRoi1, myRoi2, results[thread.currentThread()]);
128  break;
129 
130  case Norm::L2:
131  BitmapProcessing::write<Kernels::ComputeSquaredL2Metric>(*bitmap[0], *bitmap[1], myRoi1, myRoi2, results[thread.currentThread()]);
132  break;
133  }
134 
135  return true;
136 }
CustomRectangle< numeric > split(const int part, const int totalParts)
Definition: geometry.h:294
virtual ThreadIndex numThreads() const =0
ThreadIndex currentThread() const
Definition: parallelism.h:165

◆ setBitmaps() [1/2]

void Metric::setBitmaps ( AbstractBitmap bitmap1,
AbstractBitmap bitmap2 
)

Sets input images.

Definition at line 72 of file metric.cpp.

72  {
73  this->bitmap[0] = bitmap1;
74  this->bitmap[1] = bitmap2;
75  if (this->bitmap[0])
76  roi[0] = this->bitmap[0]->getSize().halfOpenedRectangle();
77  if (this->bitmap[1])
78  roi[1] = this->bitmap[1]->getSize().halfOpenedRectangle();
79 }
const ImageResolution getSize() const
Returns the bitmap resolution within ImageResolution object.
IntRectangle halfOpenedRectangle() const

◆ setBitmaps() [2/2]

void Metric::setBitmaps ( AbstractBitmap bitmap1,
const IntRectangle roi1,
AbstractBitmap bitmap2,
const IntRectangle roi2 
)

Sets input images and rectangular regions delimiting the measurement areas.

Definition at line 82 of file metric.cpp.

82  {
83  this->bitmap[0] = bitmap1;
84  this->bitmap[1] = bitmap2;
85  this->roi[0] = roi1;
86  this->roi[1] = roi2;
87 }

◆ setNorm()

void Beatmup::Metric::setNorm ( Norm  norm)
inline

Specifies the norm to use in the measurement.

Definition at line 67 of file metric.h.

67 { this->norm = norm; }

◆ getResult()

double Beatmup::Metric::getResult ( ) const
inline
Returns
the measurement result (after the task is executed)

Definition at line 72 of file metric.h.

72 { return result; }

◆ psnr()

float Metric::psnr ( AbstractBitmap bitmap1,
AbstractBitmap bitmap2 
)
static
Returns
peak signal-to-noise ratio in dB for two given images

Definition at line 139 of file metric.cpp.

139  {
140  Metric metric;
141  metric.setBitmaps(&bitmap1, &bitmap2);
142  metric.setNorm(Norm::L2);
143  bitmap1.getContext().performTask(metric);
144  const int n = bitmap1.getSize().numPixels() * bitmap1.getNumberOfChannels();
145  return 20 * std::log10(std::sqrt((double)n) / metric.getResult());
146 }
const unsigned char getNumberOfChannels() const
Returns number of bytes per pixel stored in each bitmap.
Context & getContext() const
float performTask(AbstractTask &task, const PoolIndex pool=DEFAULT_POOL)
Performs a given task.
Definition: context.cpp:240
Measures the difference between two bitmaps.
Definition: metric.h:29
void setNorm(Norm norm)
Specifies the norm to use in the measurement.
Definition: metric.h:67
void setBitmaps(AbstractBitmap *bitmap1, AbstractBitmap *bitmap2)
Sets input images.
Definition: metric.cpp:72
double getResult() const
Definition: metric.h:72
int n

Member Data Documentation

◆ bitmap

AbstractBitmap* Beatmup::Metric::bitmap[2]
private

Definition at line 40 of file metric.h.

◆ roi

IntRectangle Beatmup::Metric::roi[2]
private

Definition at line 41 of file metric.h.

◆ norm

Norm Beatmup::Metric::norm
private

Definition at line 42 of file metric.h.

◆ results

std::vector<double> Beatmup::Metric::results
private

Definition at line 43 of file metric.h.

◆ result

double Beatmup::Metric::result
private

Definition at line 44 of file metric.h.


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