Beatmup
resampler.cpp
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 #include "resampler.h"
20 #include "resampling_kernels.h"
21 #include "processing.h"
23 #ifndef BEATMUP_OPENGLVERSION_GLES20
25 #endif
26 
27 using namespace Beatmup;
28 
30 
31 
33  context(context),
34  input(nullptr), output(nullptr), mode(Mode::CUBIC), cubicParameter(DEFAULT_CUBIC_PARAMETER), convnet(nullptr),
35  isUsingEs31IfAvailable(false)
36 {}
37 
38 
40  if (convnet)
41  delete convnet;
42 }
43 
44 
46  this->input = input;
47  if (input)
49 }
50 
52  this->output = output;
53  if (output)
55 }
56 
57 
58 
60  this->mode = mode;
61 }
62 
63 
65  cubicParameter = alpha;
66 }
67 
68 
70  srcRect = rect;
71 }
72 
73 
75  destRect = rect;
76 }
77 
78 
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 }
83 
84 
87 }
88 
89 
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 }
130 
131 
132 void BitmapResampler::afterProcessing(ThreadIndex threadCount, GraphicPipeline* gpu, bool aborted) {
133  unlock(input, output);
134 }
135 
136 
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 }
175 
176 
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 }
A very basic class for any image.
Context & getContext() const
const ImageResolution getSize() const
Returns the bitmap resolution within ImageResolution object.
static ThreadIndex validThreadCount(int number)
Valid thread count from a given integer value.
Definition: parallelism.cpp:45
TaskDeviceRequirement
Specifies which device (CPU and/or GPU) is used to run the task.
Definition: parallelism.h:95
@ CPU_ONLY
this task does not use GPU
@ GPU_ONLY
this task requires GPU, otherwise it cannot run
void unlock(AbstractBitmap *bitmap)
Drops a lock to the bitmap.
void lock(GraphicPipeline *gpu, AbstractBitmap *input, AbstractBitmap *output)
Definition: content_lock.h:84
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
IntRectangle destRect
Definition: resampler.h:48
BitmapResampler(Context &context)
Creates a resampler.
Definition: resampler.cpp:32
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
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
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
X2UpsamplingNetwork * convnet
convnet instance
Definition: resampler.h:51
void setMode(Mode mode)
Sets the resampling algorithm to use.
Definition: resampler.cpp:59
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
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 getArea() const
Computes the rectangle area.
Definition: geometry.h:185
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.
Internal low-level GPU control API.
Definition: pipeline.h:33
IntRectangle halfOpenedRectangle() const
static void insanity(const char *message)
Definition: exception.h:136
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
Thread executing tasks.
Definition: parallelism.h:154
virtual bool usesEs31Backend() const =0
virtual void process(GraphicPipeline &gpu, GL::TextureHandler &input, AbstractBitmap &output)=0
unsigned char ThreadIndex
number of threads / thread index
Definition: parallelism.h:68
CustomRectangle< int > IntRectangle
Definition: geometry.h:630
ProcessingTarget
Definition: basic_types.h:55
CustomPoint< numeric > min(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:724
JNIEnv jlong jint mode
Beatmup::IntRectangle rect(x1, y1, x2, y2)