Beatmup
Beatmup::BitmapResampler Class Reference

Resamples an image to a given resolution. More...

#include <resampler.h>

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

Public Types

enum class  Mode {
  NEAREST_NEIGHBOR , BOX , LINEAR , CUBIC ,
  CONVNET
}
 Resampling mode (algorithm) specification. 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

 BitmapResampler (Context &context)
 Creates a resampler. More...
 
 ~BitmapResampler ()
 
void setInput (AbstractBitmap *input)
 Sets the image to process. More...
 
void setOutput (AbstractBitmap *output)
 Sets the output image. More...
 
AbstractBitmapgetInput ()
 
AbstractBitmapgetOutput ()
 
void setMode (Mode mode)
 Sets the resampling algorithm to use. More...
 
Mode getMode () const
 Returns currently selected resampling algorithm. More...
 
void setCubicParameter (float alpha)
 Sets cubic interpolation parameter ("alpha"). More...
 
float getCubicParameter () const
 Returns cubic interpolation parameter ("alpha"). More...
 
void setUsingEs31IfAvailable (bool useEs31)
 Defines OpenGL ES backend selection policy (2.0 vs 3.1) when applicable. More...
 
void setInputRect (const IntRectangle &rect)
 Specifies a rectangular working area in the input bitmap. More...
 
void setOutputRect (const IntRectangle &rect)
 Specifies a rectangular working area in the output bitmap. More...
 
IntRectangle getInputRect () const
 
IntRectangle getOutputRect () const
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Static Public Attributes

static const float DEFAULT_CUBIC_PARAMETER = -0.5f
 

Protected Member Functions

virtual TaskDeviceRequirement getUsedDevices () const
 Communicates devices (CPU and/or GPU) the task is run on. More...
 
virtual bool process (TaskThread &thread)
 Executes the task on CPU within a given thread. More...
 
virtual bool processOnGPU (GraphicPipeline &gpu, TaskThread &thread)
 Executes the task on GPU. More...
 
virtual void beforeProcessing (ThreadIndex, ProcessingTarget target, GraphicPipeline *)
 Instruction called before the task is executed. More...
 
virtual void afterProcessing (ThreadIndex, GraphicPipeline *, bool)
 Instruction called after the task is executed. More...
 
virtual ThreadIndex getMaxThreads () const
 Gives the upper limint on the number of threads the task may be performed by. More...
 

Private Attributes

Contextcontext
 a context managing intermediate bitmaps More...
 
AbstractBitmapinput
 
AbstractBitmapoutput
 input and output bitmaps More...
 
IntRectangle srcRect
 
IntRectangle destRect
 
Mode mode
 
float cubicParameter
 
X2UpsamplingNetworkconvnet
 convnet instance More...
 
bool isUsingEs31IfAvailable
 if true, uses OpenGL ES 3.1 backend when available instead ES 2.0 More...
 

Additional Inherited Members

- 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 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)
 

Detailed Description

Resamples an image to a given resolution.

Implements different resampling approaches, including standard ones (bilinear, bicubic, etc.) and a neural network-based 2x upsampling approach dubbed as "x2".

Definition at line 33 of file resampler.h.

Member Enumeration Documentation

◆ Mode

Resampling mode (algorithm) specification.

Enumerator
NEAREST_NEIGHBOR 

zero-order: usual nearest neighbor

BOX 

"0.5-order": anti-aliasing box filter; identical to nearest neighbor when upsampling

LINEAR 

first order: bilinear interpolation

CUBIC 

third order: bicubic interpolation

CONVNET 

upsampling x2 using a convolutional neural network

Definition at line 38 of file resampler.h.

38  {
39  NEAREST_NEIGHBOR, //!< zero-order: usual nearest neighbor
40  BOX, //!< "0.5-order": anti-aliasing box filter; identical to nearest neighbor when upsampling
41  LINEAR, //!< first order: bilinear interpolation
42  CUBIC, //!< third order: bicubic interpolation
43  CONVNET //!< upsampling x2 using a convolutional neural network
44  };

Constructor & Destructor Documentation

◆ BitmapResampler()

BitmapResampler::BitmapResampler ( Context context)

Creates a resampler.

Parameters
contextA context instance used to manage resources required to run some of resampling algorithms.

Definition at line 32 of file resampler.cpp.

32  :
36 {}
AbstractBitmap * input
Definition: resampler.h:47
AbstractBitmap * output
input and output bitmaps
Definition: resampler.h:47
@ CUBIC
third order: bicubic interpolation
bool isUsingEs31IfAvailable
if true, uses OpenGL ES 3.1 backend when available instead ES 2.0
Definition: resampler.h:52
X2UpsamplingNetwork * convnet
convnet instance
Definition: resampler.h:51
static const float DEFAULT_CUBIC_PARAMETER
Definition: resampler.h:63
Context & context
a context managing intermediate bitmaps
Definition: resampler.h:46

◆ ~BitmapResampler()

BitmapResampler::~BitmapResampler ( )

Definition at line 39 of file resampler.cpp.

39  {
40  if (convnet)
41  delete convnet;
42 }

Member Function Documentation

◆ getUsedDevices()

AbstractTask::TaskDeviceRequirement BitmapResampler::getUsedDevices ( ) const
protectedvirtual

Communicates devices (CPU and/or GPU) the task is run on.

Reimplemented from Beatmup::AbstractTask.

Definition at line 85 of file resampler.cpp.

85  {
87 }
@ CPU_ONLY
this task does not use GPU
@ GPU_ONLY
this task requires GPU, otherwise it cannot run
@ CONVNET
upsampling x2 using a convolutional neural network

◆ process()

bool BitmapResampler::process ( TaskThread thread)
protectedvirtual

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 137 of file resampler.cpp.

137  {
138  switch (mode) {
140  BitmapProcessing::pipeline<Kernels::NearestNeighborResampling>(
141  *input, *output,
142  srcRect, destRect, thread
143  );
144  break;
145 
146  case Mode::BOX:
147  BitmapProcessing::pipeline<Kernels::BoxResampling>(
148  *input, *output,
149  srcRect, destRect, thread
150  );
151  break;
152 
153  case Mode::LINEAR:
154  BitmapProcessing::pipeline<Kernels::BilinearResampling>(
155  *input, *output,
156  srcRect, destRect, thread
157  );
158  break;
159 
160  case Mode::CUBIC:
161  BitmapProcessing::pipeline<Kernels::BicubicResampling>(
162  *input, *output,
164  );
165  break;
166 
167  case Mode::CONVNET:
168  break;
169 
170  default:
171  Insanity::insanity("Resampling mode not implemented");
172  }
173  return true;
174 }
IntRectangle srcRect
Definition: resampler.h:48
IntRectangle destRect
Definition: resampler.h:48
@ NEAREST_NEIGHBOR
zero-order: usual nearest neighbor
@ LINEAR
first order: bilinear interpolation
@ BOX
"0.5-order": anti-aliasing box filter; identical to nearest neighbor when upsampling
static void insanity(const char *message)
Definition: exception.h:136

◆ processOnGPU()

bool BitmapResampler::processOnGPU ( GraphicPipeline gpu,
TaskThread thread 
)
protectedvirtual

Executes the task on GPU.

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

Reimplemented from Beatmup::AbstractTask.

Definition at line 177 of file resampler.cpp.

177  {
178  switch (mode) {
179  case Mode::CONVNET:
180  convnet->process(gpu, *input, *output);
181  break;
182 
183  default:
184  Insanity::insanity("Resampling mode not implemented");
185  }
186  return true;
187 }
virtual void process(GraphicPipeline &gpu, GL::TextureHandler &input, AbstractBitmap &output)=0

◆ beforeProcessing()

void BitmapResampler::beforeProcessing ( ThreadIndex  threadCount,
ProcessingTarget  target,
GraphicPipeline gpu 
)
protectedvirtual

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 resampler.cpp.

90  {
91  NullTaskInput::check(input, "input bitmap");
92  NullTaskInput::check(output, "output bitmap");
93  RuntimeError::check(input != output, "input and output is the same bitmap");
96  "input and output rectangular areas not matching the corresponding bitmaps are not supported when using GPU"
97  );
102 
103  if (mode == Mode::CONVNET) {
104  RuntimeError::check(gpu != nullptr, "convnet resampling requires GPU");
106  "input and/or output bitmaps contexts do not match the BitmapRecycler context");
108  destRect.width() == 2 * srcRect.width() && destRect.height() == 2 * srcRect.height(),
109  "convnet resampling is only applicable for 2x upsampling"
110  );
111 
112 #ifndef BEATMUP_OPENGLVERSION_GLES20
113  // check if the ES backend needed to be upgraded to 3.1
115  delete convnet;
116  convnet = nullptr;
117  }
118 #endif
119 
120  // init convnet instance if not yet
121 #ifndef BEATMUP_OPENGLVERSION_GLES20
124 #endif
125  if (!convnet)
127  }
128  lock(gpu, target, input, output);
129 }
Context & getContext() const
const ImageResolution getSize() const
Returns the bitmap resolution within ImageResolution object.
void lock(GraphicPipeline *gpu, AbstractBitmap *input, AbstractBitmap *output)
Definition: content_lock.h:84
GL::RecycleBin * getGpuRecycleBin() const
Definition: context.cpp:340
void limit(const CustomRectangle &frame)
Truncates a rectangle to a limiting frame.
Definition: geometry.h:221
numeric height() const
Definition: geometry.h:178
numeric width() const
Definition: geometry.h:174
void normalize()
Flips corners coordinates guaranteeing that it has a non negative area, i.e.
Definition: geometry.h:192
x2 image upsampler using a convolutional neural network.
Definition: cnn.h:29
x2 image upsampler using a convolutional neural network.
Definition: cnn.h:31
virtual const int getHeight() const =0
Height of the texture in pixels.
virtual const int getWidth() const =0
Width of the texture in pixels.
IntRectangle halfOpenedRectangle() const
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
virtual bool usesEs31Backend() const =0
CustomRectangle< int > IntRectangle
Definition: geometry.h:630

◆ afterProcessing()

void BitmapResampler::afterProcessing ( ThreadIndex  threadCount,
GraphicPipeline gpu,
bool  aborted 
)
protectedvirtual

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 132 of file resampler.cpp.

132  {
133  unlock(input, output);
134 }
void unlock(AbstractBitmap *bitmap)
Drops a lock to the bitmap.

◆ getMaxThreads()

ThreadIndex BitmapResampler::getMaxThreads ( ) const
protectedvirtual

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.

< minimum number of pixels per worker

Reimplemented from Beatmup::AbstractTask.

Definition at line 79 of file resampler.cpp.

79  {
80  static const int MIN_PIXELS_PER_THREAD = 1000; //!< minimum number of pixels per worker
81  return AbstractTask::validThreadCount(std::min(destRect.height() + 1, srcRect.getArea() / MIN_PIXELS_PER_THREAD));
82 }
static ThreadIndex validThreadCount(int number)
Valid thread count from a given integer value.
Definition: parallelism.cpp:45
numeric getArea() const
Computes the rectangle area.
Definition: geometry.h:185
CustomPoint< numeric > min(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:724

◆ setInput()

void BitmapResampler::setInput ( AbstractBitmap input)

Sets the image to process.

Parameters
[in]inputThe input image

Definition at line 45 of file resampler.cpp.

45  {
46  this->input = input;
47  if (input)
49 }

◆ setOutput()

void BitmapResampler::setOutput ( AbstractBitmap output)

Sets the output image.

The output image contains the resampled version of the input one once the task finishes.

Parameters
[in]outputThe output image

Definition at line 51 of file resampler.cpp.

51  {
52  this->output = output;
53  if (output)
55 }

◆ getInput()

AbstractBitmap* Beatmup::BitmapResampler::getInput ( )
inline

Definition at line 86 of file resampler.h.

86 { return input; }

◆ getOutput()

AbstractBitmap* Beatmup::BitmapResampler::getOutput ( )
inline

Definition at line 87 of file resampler.h.

87 { return output; }

◆ setMode()

void BitmapResampler::setMode ( Mode  mode)

Sets the resampling algorithm to use.

Parameters
[in]modeThe algorithm to use

Definition at line 59 of file resampler.cpp.

59  {
60  this->mode = mode;
61 }

◆ getMode()

Mode Beatmup::BitmapResampler::getMode ( ) const
inline

Returns currently selected resampling algorithm.

Definition at line 98 of file resampler.h.

98 { return mode; }

◆ setCubicParameter()

void BitmapResampler::setCubicParameter ( float  alpha)

Sets cubic interpolation parameter ("alpha").

Has no impact if the resampling mode is different from cubic.

Parameters
[in]alphaThe alpha parameter value

Definition at line 64 of file resampler.cpp.

64  {
65  cubicParameter = alpha;
66 }

◆ getCubicParameter()

float Beatmup::BitmapResampler::getCubicParameter ( ) const
inline

Returns cubic interpolation parameter ("alpha").

Definition at line 110 of file resampler.h.

110 { return cubicParameter; }

◆ setUsingEs31IfAvailable()

void Beatmup::BitmapResampler::setUsingEs31IfAvailable ( bool  useEs31)
inline

Defines OpenGL ES backend selection policy (2.0 vs 3.1) when applicable.

Parameters
[in]useEs31If true, ES 3.1 backend will be used when available, otherwise ES 2.0 is used.

Definition at line 116 of file resampler.h.

116 { isUsingEs31IfAvailable = useEs31; }

◆ setInputRect()

void BitmapResampler::setInputRect ( const IntRectangle rect)

Specifies a rectangular working area in the input bitmap.

Pixels outside of this area are not used.

Definition at line 69 of file resampler.cpp.

69  {
70  srcRect = rect;
71 }
Beatmup::IntRectangle rect(x1, y1, x2, y2)

◆ setOutputRect()

void BitmapResampler::setOutputRect ( const IntRectangle rect)

Specifies a rectangular working area in the output bitmap.

Pixels outside of this area are not affected.

Definition at line 74 of file resampler.cpp.

74  {
75  destRect = rect;
76 }

◆ getInputRect()

IntRectangle Beatmup::BitmapResampler::getInputRect ( ) const
inline

Definition at line 130 of file resampler.h.

130 { return srcRect; }

◆ getOutputRect()

IntRectangle Beatmup::BitmapResampler::getOutputRect ( ) const
inline

Definition at line 131 of file resampler.h.

131 { return destRect; }

Member Data Documentation

◆ context

Context& Beatmup::BitmapResampler::context
private

a context managing intermediate bitmaps

Definition at line 46 of file resampler.h.

◆ input

AbstractBitmap* Beatmup::BitmapResampler::input
private

Definition at line 47 of file resampler.h.

◆ output

AbstractBitmap * Beatmup::BitmapResampler::output
private

input and output bitmaps

Definition at line 47 of file resampler.h.

◆ srcRect

IntRectangle Beatmup::BitmapResampler::srcRect
private

Definition at line 48 of file resampler.h.

◆ destRect

IntRectangle Beatmup::BitmapResampler::destRect
private

Definition at line 48 of file resampler.h.

◆ mode

Mode Beatmup::BitmapResampler::mode
private

Definition at line 49 of file resampler.h.

◆ cubicParameter

float Beatmup::BitmapResampler::cubicParameter
private

Definition at line 50 of file resampler.h.

◆ convnet

X2UpsamplingNetwork* Beatmup::BitmapResampler::convnet
private

convnet instance

Definition at line 51 of file resampler.h.

◆ isUsingEs31IfAvailable

bool Beatmup::BitmapResampler::isUsingEs31IfAvailable
private

if true, uses OpenGL ES 3.1 backend when available instead ES 2.0

Definition at line 52 of file resampler.h.

◆ DEFAULT_CUBIC_PARAMETER

const float BitmapResampler::DEFAULT_CUBIC_PARAMETER = -0.5f
static

Definition at line 63 of file resampler.h.


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