Skip to content

Commit c38f48e

Browse files
committed
rebase (pick): add support for WaveForm & Histogram
1 parent 8213925 commit c38f48e

File tree

9 files changed

+289
-0
lines changed

9 files changed

+289
-0
lines changed

cgeDemo/src/main/java/org/wysaid/cgeDemo/MainActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class MainActivity extends AppCompatActivity {
2828

2929
public static final String EFFECT_CONFIGS[] = {
3030
"",
31+
"@style waveform 0.5 0.5 200. 150. 0.0 0.0 0.0 0.9",
32+
"@style hist 0.5 0.5 200. 150. 0.0 0.0 0.0 ",
3133
"@curve RGB(0,255)(255,0) @style cm mapping0.jpg 80 80 8 3", // ASCII art (字符画效果)
3234
"@beautify face 1 480 640", //Beautify
3335
"@adjust lut edgy_amber.png",

library/src/main/jni/Android.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ LOCAL_SRC_FILES := \
7373
$(CGE_SOURCE)/filters/cgeHalftoneFilter.cpp \
7474
$(CGE_SOURCE)/filters/cgeEdgeFilter.cpp \
7575
$(CGE_SOURCE)/filters/cgeEmbossFilter.cpp \
76+
\
77+
$(CGE_SOURCE)/filters/cgeHistogramFilter.cpp \
78+
$(CGE_SOURCE)/filters/cgeWaveformFilter.cpp \
79+
\
7680
$(CGE_SOURCE)/filters/cgeCrosshatchFilter.cpp \
7781
$(CGE_SOURCE)/filters/cgeLiquifyFilter.cpp \
7882
$(CGE_SOURCE)/filters/cgeRandomBlurFilter.cpp \

library/src/main/jni/cge/filters/cgeAdvancedEffects.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,13 @@ CGEBeautifyFilter* createBeautifyFilter()
137137
COMMON_FUNC(CGEBeautifyFilter);
138138
}
139139

140+
CGEHistogramFilter* createHistogramFilter()
141+
{
142+
COMMON_FUNC(CGEHistogramFilter);
143+
}
144+
145+
CGEWaveformFilter* createWaveformFilter()
146+
{
147+
COMMON_FUNC(CGEWaveformFilter);
148+
}
140149
} // namespace CGE

library/src/main/jni/cge/filters/cgeAdvancedEffects.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
#include "cgeEmbossFilter.h"
1616
#include "cgeHalftoneFilter.h"
1717
#include "cgeHazeFilter.h"
18+
#include "cgeHistogramFilter.h"
1819
#include "cgeLerpblurFilter.h"
1920
#include "cgeLiquifyFilter.h"
2021
#include "cgeMosaicBlurFilter.h"
2122
#include "cgePolarPixellateFilter.h"
2223
#include "cgePolkaDotFilter.h"
2324
#include "cgeRandomBlurFilter.h"
2425
#include "cgeSketchFilter.h"
26+
#include "cgeWaveformFilter.h"
2527

2628
namespace CGE
2729
{
@@ -48,6 +50,9 @@ CGELerpblurFilter* createLerpblurFilter();
4850
CGESketchFilter* createSketchFilter();
4951

5052
CGEBeautifyFilter* createBeautifyFilter();
53+
54+
CGEHistogramFilter* createHistogramFilter();
55+
CGEWaveformFilter* createWaveformFilter();
5156
} // namespace CGE
5257

5358
#endif

library/src/main/jni/cge/filters/cgeDataParsingEngine.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,44 @@ CGEImageFilterInterface* CGEDataParsingEngine::advancedStyleParser(const char* p
10931093
{
10941094
ADJUSTHELP_COMMON_FUNC2(pstr, CGECrosshatchFilter, setCrosshatchSpacing, setLineWidth);
10951095
}
1096+
else if (strcmp(buffer, "hist") == 0)
1097+
{
1098+
float x, y, value1, value2, r, g, b;
1099+
if (sscanf(pstr, "%f%*c%f%*c%f%*c%f%*c%f%*c%f%*c%f", &x, &y, &value1, &value2, &r, &g, &b) != 7)
1100+
{
1101+
LOG_ERROR_PARAM(pstr);
1102+
return nullptr;
1103+
}
1104+
1105+
CGEHistogramFilter* filter = createHistogramFilter();
1106+
if (filter != nullptr)
1107+
{
1108+
proc = filter;
1109+
filter->setCenter(x, y);
1110+
filter->setXValue(value1);
1111+
filter->setYValue(value2);
1112+
filter->setColor(r, g, b);
1113+
}
1114+
}
1115+
1116+
else if (strcmp(buffer, "waveform") == 0)
1117+
{
1118+
float x, y, value1, value2, r, g, b, a = 1.0f;
1119+
if (sscanf(pstr, "%f%*c%f%*c%f%*c%f%*c%f%*c%f%*c%f%*c%f", &x, &y, &value1, &value2, &r, &g, &b, &a) < 7)
1120+
{
1121+
LOG_ERROR_PARAM(pstr);
1122+
return nullptr;
1123+
}
1124+
1125+
CGEWaveformFilter* filter = createWaveformFilter();
1126+
if (filter != nullptr)
1127+
{
1128+
proc = filter;
1129+
filter->setFormPosition(x, y);
1130+
filter->setFormSize(value1, value2);
1131+
filter->setColor(r, g, b, a);
1132+
}
1133+
}
10961134
else if (strcmp(buffer, "edge") == 0)
10971135
{
10981136
ADJUSTHELP_COMMON_FUNC2(pstr, CGEEdgeSobelFilter, setIntensity, setStride);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "cgeHistogramFilter.h"
2+
3+
static CGEConstString s_fshHistogram = CGE_SHADER_STRING_PRECISION_M(
4+
varying vec2 textureCoordinate;
5+
6+
uniform sampler2D inputImageTexture;
7+
8+
uniform vec2 center; //坐标系原点
9+
10+
uniform float xValue; //宽度(x轴长度)
11+
12+
uniform float yValue; //高度(y轴长度)
13+
14+
uniform vec3 bgColor; //背景颜色
15+
16+
void main() {
17+
/**
18+
在以center为原点,宽xValue,高yValue,背景色为bgColor的矩形框中绘制亮度直方图。如何在这个库的基础上实现?
19+
*/
20+
gl_FragColor = texture2D(inputImageTexture, textureCoordinate);
21+
});
22+
23+
namespace CGE
24+
{
25+
CGEConstString CGEHistogramFilter::paramCenter = "center";
26+
CGEConstString CGEHistogramFilter::paramXValue = "xValue";
27+
CGEConstString CGEHistogramFilter::paramYValue = "yValue";
28+
CGEConstString CGEHistogramFilter::paramColor = "bgColor";
29+
30+
bool CGEHistogramFilter::init()
31+
{
32+
if (initShadersFromString(g_vshDefaultWithoutTexCoord, s_fshHistogram))
33+
{
34+
setCenter(0.5f, 0.5f);
35+
setXValue(0.4f);
36+
setYValue(0.3f);
37+
setColor(0.0f, 0.0f, 0.0f);
38+
return true;
39+
}
40+
return false;
41+
}
42+
43+
void CGEHistogramFilter::setCenter(float x, float y)
44+
{
45+
m_program.bind();
46+
m_program.sendUniformf(paramCenter, x, y);
47+
}
48+
49+
void CGEHistogramFilter::setXValue(float value)
50+
{
51+
m_program.bind();
52+
m_program.sendUniformf(paramXValue, value);
53+
}
54+
55+
void CGEHistogramFilter::setYValue(float value)
56+
{
57+
m_program.bind();
58+
m_program.sendUniformf(paramYValue, value);
59+
}
60+
61+
void CGEHistogramFilter::setColor(float r, float b, float g)
62+
{
63+
m_program.bind();
64+
m_program.sendUniformf(paramColor, r, b, g);
65+
}
66+
} // namespace CGE
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef _HISTOGRAMFILTER_H_
2+
#define _HISTOGRAMFILTER_H_
3+
4+
#include "cgeImageFilter.h"
5+
6+
namespace CGE
7+
{
8+
class CGEHistogramFilter : public CGEImageFilterInterface
9+
{
10+
public:
11+
/**
12+
* 坐标系原点, 默认 (0.5, 0.5)
13+
*/
14+
void setCenter(float x, float y);
15+
/**
16+
* 宽度
17+
*/
18+
void setXValue(float value);
19+
/**
20+
* 高度
21+
*/
22+
void setYValue(float value);
23+
/**
24+
* 背景颜色
25+
*/
26+
void setColor(float r, float b, float g);
27+
28+
bool init();
29+
30+
protected:
31+
static CGEConstString paramCenter;
32+
static CGEConstString paramXValue;
33+
static CGEConstString paramYValue;
34+
static CGEConstString paramColor;
35+
};
36+
} // namespace CGE
37+
38+
#endif
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include "cgeWaveformFilter.h"
2+
3+
#include <EGL/egl.h>
4+
5+
static CGEConstString s_vshWaveform = "#version 310 es\n" CGE_SHADER_STRING_PRECISION_H(
6+
layout(location = 0) in vec2 position;
7+
layout(location = 0) out vec2 textureCoordinate;
8+
void main() {
9+
gl_Position = vec4(position, 0.0, 1.0);
10+
textureCoordinate = (position.xy + 1.0) / 2.0;
11+
});
12+
13+
static CGEConstString s_fshWaveform = "#version 310 es\n" CGE_SHADER_STRING(
14+
precision highp float;
15+
precision highp int;
16+
layout(location = 0) in vec2 textureCoordinate;
17+
layout(binding = 0) uniform sampler2D inputImageTexture;
18+
layout(rgba8, binding = 1) uniform writeonly image2D outputImageTexture;
19+
layout(location = 0) out vec4 fragColor;
20+
21+
void main() {
22+
fragColor = texture(inputImageTexture, textureCoordinate);
23+
24+
fragColor.r = 1.0;
25+
});
26+
27+
namespace CGE
28+
{
29+
CGEWaveformFilter::~CGEWaveformFilter()
30+
{
31+
}
32+
33+
bool CGEWaveformFilter::init()
34+
{
35+
if (initShadersFromString(s_vshWaveform, s_fshWaveform))
36+
{
37+
setFormPosition(0.5f, 0.5f);
38+
setFormSize(0.4f, 0.3f);
39+
setColor(0.0f, 0.0f, 0.0f, 0.5f);
40+
m_drawer.reset(TextureDrawer::create());
41+
return true;
42+
}
43+
44+
CGE_LOG_ERROR(R"(CGEWaveformFilter::init failed. This filter needs GLES3.1 and later!
45+
Only GLES 3.1+ support image store.
46+
You need to imp a fallback version which reading pixels every frame like `cgeColorMappingFilter`
47+
)");
48+
CGE_LOG_ERROR("Failed Vertex Shader: %s\n", s_vshWaveform);
49+
CGE_LOG_ERROR("Failed Fragment Shader: %s\n", s_fshWaveform);
50+
return false;
51+
}
52+
53+
void CGEWaveformFilter::render2Texture(CGEImageHandlerInterface* handler, GLuint srcTexture, GLuint vertexBufferID)
54+
{
55+
handler->setAsTarget();
56+
m_program.bind();
57+
58+
/// 渲染不写入, 使用 imageStore 写入.
59+
// glColorMask(false, false, false, false);
60+
61+
glEnableVertexAttribArray(0);
62+
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
63+
glActiveTexture(GL_TEXTURE0);
64+
// glBindImageTexture(0, srcTexture, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
65+
glBindTexture(GL_TEXTURE_2D, srcTexture);
66+
// glActiveTexture(GL_TEXTURE1);
67+
// glBindImageTexture(1, handler->getTargetTextureID(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
68+
// glBindTexture(GL_TEXTURE_2D, handler->getTargetTextureID());
69+
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
70+
}
71+
72+
void CGEWaveformFilter::setFormPosition(float left, float top)
73+
{
74+
m_position = { left, top };
75+
}
76+
77+
void CGEWaveformFilter::setFormSize(float width, float height)
78+
{
79+
m_size = { width, height };
80+
}
81+
82+
void CGEWaveformFilter::setColor(float r, float g, float b, float a)
83+
{
84+
m_color = { r, g, b, a };
85+
}
86+
} // namespace CGE
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef _WAVEFORMFILTER_H_
2+
#define _WAVEFORMFILTER_H_
3+
4+
#include "cgeImageFilter.h"
5+
#include "cgeTextureUtils.h"
6+
#include "cgeVec.h"
7+
8+
namespace CGE
9+
{
10+
class CGEWaveformFilter : public CGEImageFilterInterface
11+
{
12+
public:
13+
~CGEWaveformFilter() override;
14+
15+
/**
16+
* @brief 左上角的点
17+
*/
18+
void setFormPosition(float left, float top);
19+
/**
20+
* @brief 相对大小
21+
*/
22+
void setFormSize(float width, float height);
23+
24+
/**
25+
* @brief 背景色
26+
*/
27+
void setColor(float r, float g, float b, float a);
28+
29+
bool init() override;
30+
31+
void render2Texture(CGEImageHandlerInterface* handler, GLuint srcTexture, GLuint vertexBufferID) override;
32+
33+
protected:
34+
std::unique_ptr<TextureDrawer> m_drawer;
35+
Vec2f m_position;
36+
Vec2f m_size;
37+
Vec4f m_color;
38+
};
39+
} // namespace CGE
40+
41+
#endif

0 commit comments

Comments
 (0)