Beatmup
color_matrix.cpp
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 #include "color_matrix.h"
20 #include "../bitmap/bitmap_access.h"
21 #include "../bitmap/processing.h"
22 #include "../color/color_spaces.h"
23 #include "../color/constants.h"
24 
25 using namespace Beatmup;
26 
28  allowIntApprox(true), bias(Color::ZERO_F)
29 {}
30 
31 
32 namespace Kernels {
33  /**
34  Application of a Color::Matrix on CPU
35  */
36  template <class in_t, class out_t> class ApplyColorMatrix {
37  public:
38  static void process(AbstractBitmap& input, AbstractBitmap& output, int x, int y, const pixfloat4 &biasF, const Color::Matrix& matrixF, bool useIntApprox, msize nPix) {
39  in_t in(input, x, y);
40  out_t out(output, x, y);
41 
42  if (useIntApprox) {
43  pixint4 matrixI[4], biasI;
44  biasI = biasF;
45  for (int i = 0; i < 4; i++)
46  matrixI[i] = matrixF[i];
47  for (msize n = 0; n < nPix; n++) {
48  out.assign(
49  (in() * matrixI[CHANNELS_4.R]).sum() + biasI.val[CHANNELS_4.R],
50  (in() * matrixI[CHANNELS_4.G]).sum() + biasI.val[CHANNELS_4.G],
51  (in() * matrixI[CHANNELS_4.B]).sum() + biasI.val[CHANNELS_4.B],
52  (in() * matrixI[CHANNELS_4.A]).sum() + biasI.val[CHANNELS_4.A]
53  );
54  in++;
55  out++;
56  }
57  }
58  else
59  for (msize n = 0; n < nPix; n++) {
60  out.assign(
61  (in() * matrixF[CHANNELS_4.R]).sum() + biasF[CHANNELS_4.R],
62  (in() * matrixF[CHANNELS_4.G]).sum() + biasF[CHANNELS_4.G],
63  (in() * matrixF[CHANNELS_4.B]).sum() + biasF[CHANNELS_4.B],
64  (in() * matrixF[CHANNELS_4.A]).sum() + biasF[CHANNELS_4.A]
65  );
66  in++;
67  out++;
68  }
69  }
70  };
71 }
72 
73 
74 void Filters::ColorMatrix::apply(int x, int y, msize nPix, TaskThread& thread) {
75  BitmapProcessing::pipeline<Kernels::ApplyColorMatrix>(
76  *inputBitmap, *outputBitmap, x, y,
77  bias, matrix, allowIntApprox && inputBitmap->isInteger() && outputBitmap->isInteger(),
78  nPix
79  );
80 }
81 
82 
84  if (allowIntApprox)
85  return
86  "uniform lowp mat4 transform;" \
87  "uniform lowp vec4 bias;";
88  else
89  return
90  "uniform highp mat4 transform;" \
91  "uniform highp vec4 bias;";
92 }
93 
94 
96  return "gl_FragColor = " + PixelwiseFilter::GLSL_RGBA_INPUT + " * transform + bias;";
97 }
98 
99 
100 void Filters::ColorMatrix::setup(bool useGpu) {
101  if (useGpu) {
102  shader->setFloatMatrix4("transform", matrix);
103  shader->setFloat("bias", bias.r, bias.g, bias.b, bias.a);
104  }
105 }
106 
107 
109  allowIntApprox = allow;
110 }
111 
112 
113 void Filters::ColorMatrix::setCoefficients(int outChannel, float bias, float r, float g, float b, float a) {
114  OutOfRange::check(outChannel, 0, 3, "Invalid output channel index: %d");
115  pixfloat4 _(this->bias);
116  _[outChannel] = bias;
117  this->bias = _;
118  matrix[outChannel].r = r;
119  matrix[outChannel].g = g;
120  matrix[outChannel].b = b;
121  matrix[outChannel].a = a;
122 }
123 
124 
125 void Filters::ColorMatrix::setHSVCorrection(float hueDegrees, float saturationFactor, float valueFactor) {
126  matrix = Color::Matrix(hueDegrees, saturationFactor, valueFactor);
128 }
129 
130 
131 void Filters::ColorMatrix::setColorInversion(color3f preservedHue, float saturationFactor, float valueFactor) {
132  matrix = Color::Matrix(preservedHue, saturationFactor, valueFactor);
134 }
135 
136 
138  matrix.r().r *= factor;
139  matrix.r().g *= factor;
140  matrix.r().b *= factor;
141  matrix.g().r *= factor;
142  matrix.g().g *= factor;
143  matrix.g().b *= factor;
144  matrix.b().r *= factor;
145  matrix.b().g *= factor;
146  matrix.b().b *= factor;
147 }
148 
149 
150 void Filters::ColorMatrix::setBrightness(float brightness) {
151  bias.r = brightness;
152  bias.g = brightness;
153  bias.b = brightness;
154 }
A very basic class for any image.
RGBA color mapping.
Definition: matrix.h:29
void setBrightness(float brightness)
Sets a brightness adjustment by a given factor (non-cumulative with respect to the current transforma...
void setColorInversion(color3f preservedHue, float saturationFactor=1.0f, float valueFactor=1.0f)
Resets the current transformation to a fancy color inversion mode with a fixed hue point.
void allowIntegerApproximations(bool allow)
void setHSVCorrection(float hueShiftDegrees, float saturationFactor=1.0f, float valueFactor=1.0f)
Resets the current transformation to a matrix performing standard HSV correction.
void setup(bool useGpu)
A callback run every time before the filter is applied to the image.
std::string getGlslSourceCode() const
Provides GLSL source code of the filter.
void setCoefficients(int outChannel, float bias, float r=.0f, float g=.0f, float b=.0f, float a=.0f)
Sets color matrix coefficients for a specific output color channel.
std::string getGlslDeclarations() const
Provides GLSL declarations used in the GLSL code.
void apply(int x, int y, msize nPix, TaskThread &thread)
Applies filtering to given pixel data.
void applyContrast(float factor)
Applies a contrast adjustment by a given factor on top of the current transformation.
static const std::string GLSL_RGBA_INPUT
static void check(const datatype value, const datatype min, const datatype max, const char *message)
Definition: exception.h:86
Thread executing tasks.
Definition: parallelism.h:154
Application of a Color::Matrix on CPU.
static void process(AbstractBitmap &input, AbstractBitmap &output, int x, int y, const pixfloat4 &biasF, const Color::Matrix &matrixF, bool useIntApprox, msize nPix)
static const color4f ZERO_F
Definition: constants.h:37
uint32_t msize
memory size
Definition: basic_types.h:30
static const struct Beatmup::@1 CHANNELS_4
Operations kernels.
Definition: basic_types.h:85
4-channel floating point arithmetic
4-channel integer arithmetic
jobject jlong jint jint jint jint g
jobject jlong jint jint jint jint jint b
jobject jlong jint jint y
jobject jlong jint jint jint r
jobject jlong jint jint jint jint jint jint a
jobject jlong jint x
JNIEnv jlong jint out
jlong jboolean allow
JNIEnv jlong jint jfloat bias
int n
layer getMapping().setCenterPosition(Beatmup jlong jfloat factor