Beatmup
resampler.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include "abstract_bitmap.h"
21 #include "../parallelism.h"
22 #include "../geometry.h"
23 
24 namespace Beatmup {
25 
26  class X2UpsamplingNetwork;
27 
28  /**
29  Resamples an image to a given resolution.
30  Implements different resampling approaches, including standard ones (bilinear, bicubic, etc.) and a neural network-based 2x upsampling
31  approach dubbed as "x2".
32  */
34  public:
35  /**
36  Resampling mode (algorithm) specification.
37  */
38  enum class Mode {
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  };
45  private:
46  Context& context; //!< a context managing intermediate bitmaps
47  AbstractBitmap *input, *output; //!< input and output bitmaps
51  X2UpsamplingNetwork* convnet; //!< convnet instance
52  bool isUsingEs31IfAvailable; //!< if `true`, uses OpenGL ES 3.1 backend when available instead ES 2.0
53 
54  protected:
55  virtual TaskDeviceRequirement getUsedDevices() const;
56  virtual bool process(TaskThread& thread);
57  virtual bool processOnGPU(GraphicPipeline& gpu, TaskThread& thread);
59  virtual void afterProcessing(ThreadIndex, GraphicPipeline*, bool);
60  virtual ThreadIndex getMaxThreads() const;
61 
62  public:
63  static const float DEFAULT_CUBIC_PARAMETER;
64 
65  /**
66  Creates a resampler.
67  \param context A context instance used to manage resources required to run some of resampling algorithms.
68  */
70 
72 
73  /**
74  Sets the image to process.
75  \param[in] input The input image
76  */
78 
79  /**
80  Sets the output image.
81  The output image contains the resampled version of the input one once the task finishes.
82  \param[in] output The output image
83  */
85 
86  inline AbstractBitmap* getInput() { return input; }
87  inline AbstractBitmap* getOutput() { return output; }
88 
89  /**
90  Sets the resampling algorithm to use.
91  \param[in] mode The algorithm to use
92  */
93  void setMode(Mode mode);
94 
95  /**
96  Returns currently selected resampling algorithm.
97  */
98  Mode getMode() const { return mode; }
99 
100  /**
101  Sets cubic interpolation parameter ("alpha").
102  Has no impact if the resampling mode is different from cubic.
103  \param[in] alpha The alpha parameter value
104  */
105  void setCubicParameter(float alpha);
106 
107  /**
108  Returns cubic interpolation parameter ("alpha").
109  */
110  float getCubicParameter() const { return cubicParameter; }
111 
112  /**
113  Defines OpenGL ES backend selection policy (2.0 vs 3.1) when applicable.
114  \param[in] useEs31 If `true`, ES 3.1 backend will be used when available, otherwise ES 2.0 is used.
115  */
116  inline void setUsingEs31IfAvailable(bool useEs31) { isUsingEs31IfAvailable = useEs31; }
117 
118  /**
119  Specifies a rectangular working area in the input bitmap.
120  Pixels outside of this area are not used.
121  */
122  void setInputRect(const IntRectangle& rect);
123 
124  /**
125  Specifies a rectangular working area in the output bitmap.
126  Pixels outside of this area are not affected.
127  */
128  void setOutputRect(const IntRectangle& rect);
129 
130  IntRectangle getInputRect() const { return srcRect; }
131  IntRectangle getOutputRect() const { return destRect; }
132  };
133 }
A very basic class for any image.
Task: an operation that can be executed by multiple threads in parallel.
Definition: parallelism.h:90
TaskDeviceRequirement
Specifies which device (CPU and/or GPU) is used to run the task.
Definition: parallelism.h:95
Makes sure the bitmap content is accessible within an image processing task.
Definition: content_lock.h:34
Resamples an image to a given resolution.
Definition: resampler.h:33
IntRectangle srcRect
Definition: resampler.h:48
void setOutput(AbstractBitmap *output)
Sets the output image.
Definition: resampler.cpp:51
virtual void beforeProcessing(ThreadIndex, ProcessingTarget target, GraphicPipeline *)
Instruction called before the task is executed.
Definition: resampler.cpp:90
void setInput(AbstractBitmap *input)
Sets the image to process.
Definition: resampler.cpp:45
AbstractBitmap * input
Definition: resampler.h:47
AbstractBitmap * output
input and output bitmaps
Definition: resampler.h:47
virtual ThreadIndex getMaxThreads() const
Gives the upper limint on the number of threads the task may be performed by.
Definition: resampler.cpp:79
virtual TaskDeviceRequirement getUsedDevices() const
Communicates devices (CPU and/or GPU) the task is run on.
Definition: resampler.cpp:85
float getCubicParameter() const
Returns cubic interpolation parameter ("alpha").
Definition: resampler.h:110
IntRectangle destRect
Definition: resampler.h:48
BitmapResampler(Context &context)
Creates a resampler.
Definition: resampler.cpp:32
void setUsingEs31IfAvailable(bool useEs31)
Defines OpenGL ES backend selection policy (2.0 vs 3.1) when applicable.
Definition: resampler.h:116
void setCubicParameter(float alpha)
Sets cubic interpolation parameter ("alpha").
Definition: resampler.cpp:64
Mode
Resampling mode (algorithm) specification.
Definition: resampler.h:38
@ CONVNET
upsampling x2 using a convolutional neural network
@ NEAREST_NEIGHBOR
zero-order: usual nearest neighbor
@ LINEAR
first order: bilinear interpolation
@ CUBIC
third order: bicubic interpolation
@ BOX
"0.5-order": anti-aliasing box filter; identical to nearest neighbor when upsampling
bool isUsingEs31IfAvailable
if true, uses OpenGL ES 3.1 backend when available instead ES 2.0
Definition: resampler.h:52
IntRectangle getInputRect() const
Definition: resampler.h:130
virtual void afterProcessing(ThreadIndex, GraphicPipeline *, bool)
Instruction called after the task is executed.
Definition: resampler.cpp:132
virtual bool process(TaskThread &thread)
Executes the task on CPU within a given thread.
Definition: resampler.cpp:137
void setInputRect(const IntRectangle &rect)
Specifies a rectangular working area in the input bitmap.
Definition: resampler.cpp:69
Mode getMode() const
Returns currently selected resampling algorithm.
Definition: resampler.h:98
virtual bool processOnGPU(GraphicPipeline &gpu, TaskThread &thread)
Executes the task on GPU.
Definition: resampler.cpp:177
void setOutputRect(const IntRectangle &rect)
Specifies a rectangular working area in the output bitmap.
Definition: resampler.cpp:74
IntRectangle getOutputRect() const
Definition: resampler.h:131
AbstractBitmap * getInput()
Definition: resampler.h:86
X2UpsamplingNetwork * convnet
convnet instance
Definition: resampler.h:51
void setMode(Mode mode)
Sets the resampling algorithm to use.
Definition: resampler.cpp:59
AbstractBitmap * getOutput()
Definition: resampler.h:87
static const float DEFAULT_CUBIC_PARAMETER
Definition: resampler.h:63
Context & context
a context managing intermediate bitmaps
Definition: resampler.h:46
Basic class: task and memory management, any kind of static data.
Definition: context.h:59
Internal low-level GPU control API.
Definition: pipeline.h:33
Thread executing tasks.
Definition: parallelism.h:154
Interface of x2 image upsampling using a convolutional neural network on GPU.
Definition: cnn_interface.h:29
unsigned char ThreadIndex
number of threads / thread index
Definition: parallelism.h:68
ProcessingTarget
Definition: basic_types.h:55
Beatmup::IntRectangle rect(x1, y1, x2, y2)