Beatmup
image_sampler.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2020, 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 "operation.h"
21 
22 namespace Beatmup {
23  namespace NNets {
24 
25  /**
26  Image preprocessing operation.
27  Samples an image of a fixed size from an arbitrary size texture. Has three key missions.
28  * If enabled, performs a center crop keeping the output aspect ratio (otherwise the input is stretched to fit the output).
29  * If enabled, uses linear interpolation when possible to reduce aliasing (otherwise nearest neighbor sampling is used).
30  * Brings support of OES textures. This allows for example to read data directly from camera in Android.
31  */
33  private:
34  const IntPoint size;
37  bool linearInterpolation; //!< if `true`, the input image is linearly interpolated when possible
38  bool centerCrop; /**< if `true`, a center crop is performed to sample the output image from the input;
39  otherwise the input is stretched to match the output shape */
40 
41  int rotation; //!< clockwise rotation to apply to the input image; 1 unit = 90 degrees
42 
43  void prepare(GraphicPipeline& gpu, ChunkCollection& data, GL::ProgramBank& bank);
44  void execute(TaskThread& thread, GraphicPipeline& gpu);
45  inline int getInputPadding(int index = 0) const { return 0; }
46  inline void getSampledChannels(int index, int& min, int& max) const { min = max = 0; }
47 
48  public:
49  /**
50  Creates an instance of image preprocessing operation.
51  \param[in] name Layer name
52  \param[in] size Output image size in pixels
53  \param[in] centerCrop If `true`, the center crop is enabled
54  \param[in] linearInterp If `true`, the linear interpolation is enabled
55  */
57  const std::string& name,
58  const IntPoint& size,
59  bool centerCrop = true,
60  bool linearInterp = true
61  );
62 
63  /**
64  Enables / disables center crop.
65  */
66  inline void setCenterCrop(bool enable) { this->centerCrop = enable; }
67  inline bool getCenterCrop() const { return this->centerCrop; }
68 
69  /**
70  Enables / disables linear interpolation.
71  */
72  inline void setLinearInterpolation(bool enable) { this->linearInterpolation = enable; }
73  inline bool getLinearInterpolation() const { return this->linearInterpolation; }
74 
75  /**
76  Specifies a rotation to apply to the input image.
77  \param quarterTurns Number of times a clockwise rotation by 90 degree is applied to the input image.
78  */
79  inline void setRotation(int quarterTurns) { this->rotation = quarterTurns; }
80 
81  /**
82  Returns rotation applied to the input image.
83  \return number of times a clockwise rotation by 90 degree is applied to the input image.
84  */
85  inline int getRotation() const { return this->rotation; }
86 
87  inline int getInputCount() const { return 1; }
88  inline int getOutputCount() const { return 1; }
89 
90  inline bool acceptsTextureInput(int index = 0) const { return index == 0; }
91  inline bool acceptsTextureOutput(int index = 0) const { return index == 0; }
92 
93  inline Size getOutputSize(int outputIndex = 0) const { return Size(size.x, size.y, 3); }
94 
95  void getOutput(GL::TextureHandler*& texture, int index = 0);
96 
97  void setInput(GL::TextureHandler& texture, int inputIndex = 0);
98  void setOutput(GL::TextureHandler& texture, int outputIndex = 0);
99 
100  std::map<std::string, std::string> serialize() const;
101 
102  void disconnect();
103 
104  unsigned long countTexelFetches() const;
105 
106  /**
107  Sets up deserialization of the operation.
108  */
109  static bool initDeserializer();
110  };
111 
112  /**
113  \internal
114  Being declared here, this variaable ensures ImageSampler::initDeserializer() is called with inclusion of this header file.
115  */
117  }
118 }
A key-value pair set storing pieces of arbitrary data (chunks) under string keys.
Definition: chunkfile.h:36
Stores linked GLSL programs and their associated fragment shader codes.
Definition: program_bank.h:31
GLSL program to render images Makes use of default vertex attributes to pass the texture coordinates ...
Definition: program.h:240
Internal low-level GPU control API.
Definition: pipeline.h:33
Abstract neural net operation (layer).
Definition: operation.h:46
Image preprocessing operation.
Definition: image_sampler.h:32
void prepare(GraphicPipeline &gpu, ChunkCollection &data, GL::ProgramBank &bank)
Compiles GLSL shaders.
Size getOutputSize(int outputIndex=0) const
Returns full size of a specific operation output.
Definition: image_sampler.h:93
void execute(TaskThread &thread, GraphicPipeline &gpu)
Executes the operation.
bool linearInterpolation
if true, the input image is linearly interpolated when possible
Definition: image_sampler.h:37
void getOutput(GL::TextureHandler *&texture, int index=0)
Returns a GL::TextureHandler bound to a specific operation output.
bool acceptsTextureOutput(int index=0) const
Returns true if the operation can take a GL::TextureHandler at a specific output.
Definition: image_sampler.h:91
std::map< std::string, std::string > serialize() const
Returns a serialized representation of th operation;.
void setOutput(GL::TextureHandler &texture, int outputIndex=0)
int getRotation() const
Returns rotation applied to the input image.
Definition: image_sampler.h:85
static bool initDeserializer()
Sets up deserialization of the operation.
bool acceptsTextureInput(int index=0) const
Returns true if the operation can take a GL::TextureHandler at a specific input.
Definition: image_sampler.h:90
void setCenterCrop(bool enable)
Enables / disables center crop.
Definition: image_sampler.h:66
GL::TextureHandler * output
Definition: image_sampler.h:35
void setLinearInterpolation(bool enable)
Enables / disables linear interpolation.
Definition: image_sampler.h:72
int getOutputCount() const
Returns number of operation outputs.
Definition: image_sampler.h:88
int getInputPadding(int index=0) const
Retrieves minimum required size of zero padding for a given input.
Definition: image_sampler.h:45
bool centerCrop
if true, a center crop is performed to sample the output image from the input; otherwise the input is...
Definition: image_sampler.h:38
GL::TextureHandler * input
Definition: image_sampler.h:35
void disconnect()
Assigns empty inputs and outputs.
void setRotation(int quarterTurns)
Specifies a rotation to apply to the input image.
Definition: image_sampler.h:79
ImageSampler(const std::string &name, const IntPoint &size, bool centerCrop=true, bool linearInterp=true)
Creates an instance of image preprocessing operation.
GL::RenderingProgram * program
Definition: image_sampler.h:36
int rotation
clockwise rotation to apply to the input image; 1 unit = 90 degrees
Definition: image_sampler.h:41
void getSampledChannels(int index, int &min, int &max) const
Retrieves range of input features channels sampled at the same time for a specific input.
Definition: image_sampler.h:46
bool getLinearInterpolation() const
Definition: image_sampler.h:73
int getInputCount() const
Returns number of operation inputs.
Definition: image_sampler.h:87
void setInput(GL::TextureHandler &texture, int inputIndex=0)
unsigned long countTexelFetches() const
Counts (approximate) number of texels fetches.
Operation 3D input/output size.
Definition: storage.h:37
Thread executing tasks.
Definition: parallelism.h:154
static const bool IMAGESAMPLER_OP_DESERIALIZABLE
CustomPoint< numeric > min(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:724
CustomPoint< numeric > max(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:728
jlong jint index