Skip to content

Commit 1c82345

Browse files
Ruei-sung LinAndroid (Google) Code Review
authored andcommitted
Merge "Fix b/5974247" into jb-dev
2 parents 04e83d3 + f4b4031 commit 1c82345

File tree

3 files changed

+15
-110
lines changed

3 files changed

+15
-110
lines changed

media/mca/filterpacks/java/android/filterpacks/imageproc/BlackWhiteFilter.java

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,19 @@ public class BlackWhiteFilter extends Filter {
4747
private int mHeight = 0;
4848
private int mTarget = FrameFormat.TARGET_UNSPECIFIED;
4949

50-
private Frame mNoiseFrame = null;
51-
private Random mRandom;
52-
5350
private final String mBlackWhiteShader =
5451
"precision mediump float;\n" +
5552
"uniform sampler2D tex_sampler_0;\n" +
56-
"uniform sampler2D tex_sampler_1;\n" +
5753
"uniform float black;\n" +
5854
"uniform float scale;\n" +
5955
"uniform float stepsize;\n" +
6056
"varying vec2 v_texcoord;\n" +
57+
"float rand(vec2 loc) {\n" +
58+
" return fract(sin(dot(loc, vec2(12.9898, 78.233))) * 43758.5453);\n" +
59+
"}\n" +
6160
"void main() {\n" +
6261
" vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" +
63-
" float dither = texture2D(tex_sampler_1, v_texcoord).r;\n" +
62+
" float dither = rand(v_texcoord);\n" +
6463
" vec3 xform = clamp((color.rgb - black) * scale, 0.0, 1.0);\n" +
6564
" vec3 temp = clamp((color.rgb + stepsize - black) * scale, 0.0, 1.0);\n" +
6665
" vec3 new_color = clamp(xform + (temp - xform) * (dither - 0.5), 0.0, 1.0);\n" +
@@ -69,8 +68,6 @@ public class BlackWhiteFilter extends Filter {
6968

7069
public BlackWhiteFilter(String name) {
7170
super(name);
72-
73-
mRandom = new Random();
7471
}
7572

7673
@Override
@@ -84,14 +81,6 @@ public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
8481
return inputFormat;
8582
}
8683

87-
@Override
88-
public void tearDown(FilterContext context) {
89-
if (mNoiseFrame != null) {
90-
mNoiseFrame.release();
91-
mNoiseFrame = null;
92-
}
93-
}
94-
9584
public void initProgram(FilterContext context, int target) {
9685
switch (target) {
9786
case FrameFormat.TARGET_GPU:
@@ -139,33 +128,13 @@ public void process(FilterContext context) {
139128
if (inputFormat.getWidth() != mWidth || inputFormat.getHeight() != mHeight) {
140129
mWidth = inputFormat.getWidth();
141130
mHeight = inputFormat.getHeight();
142-
143-
if (mNoiseFrame != null) {
144-
mNoiseFrame.release();
145-
}
146-
147-
int[] buffer = new int[mWidth * mHeight];
148-
for (int i = 0; i < mWidth * mHeight; ++i) {
149-
buffer[i] = mRandom.nextInt(255);
150-
}
151-
FrameFormat format = ImageFormat.create(mWidth, mHeight,
152-
ImageFormat.COLORSPACE_RGBA,
153-
FrameFormat.TARGET_GPU);
154-
mNoiseFrame = context.getFrameManager().newFrame(format);
155-
mNoiseFrame.setInts(buffer);
156-
}
157-
158-
if (mNoiseFrame != null && (mNoiseFrame.getFormat().getWidth() != mWidth ||
159-
mNoiseFrame.getFormat().getHeight() != mHeight)) {
160-
throw new RuntimeException("Random map and imput image size mismatch!");
161131
}
162132

163133
// Create output frame
164134
Frame output = context.getFrameManager().newFrame(inputFormat);
165135

166136
// Process
167-
Frame[] inputs = {input, mNoiseFrame};
168-
mProgram.process(inputs, output);
137+
mProgram.process(input, output);
169138

170139
// Push output
171140
pushOutput("image", output);

media/mca/filterpacks/java/android/filterpacks/imageproc/DocumentaryFilter.java

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,20 @@ public class DocumentaryFilter extends Filter {
4141
private int mHeight = 0;
4242
private int mTarget = FrameFormat.TARGET_UNSPECIFIED;
4343

44-
private Frame mNoiseFrame;
45-
private Random mRandom;
46-
4744
private final String mDocumentaryShader =
4845
"precision mediump float;\n" +
4946
"uniform sampler2D tex_sampler_0;\n" +
50-
"uniform sampler2D tex_sampler_1;\n" +
5147
"uniform float stepsize;\n" +
5248
"uniform float inv_max_dist;\n" +
5349
"uniform vec2 center;\n" +
5450
"varying vec2 v_texcoord;\n" +
51+
"float rand(vec2 loc) {\n" +
52+
" return fract(sin(dot(loc, vec2(12.9898, 78.233))) * 43758.5453);\n" +
53+
"}\n" +
5554
"void main() {\n" +
5655
// black white
5756
" vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" +
58-
" float dither = texture2D(tex_sampler_1, v_texcoord).r;\n" +
57+
" float dither = rand(v_texcoord);\n" +
5958
" vec3 xform = clamp(2.0 * color.rgb, 0.0, 1.0);\n" +
6059
" vec3 temp = clamp(2.0 * (color.rgb + stepsize), 0.0, 1.0);\n" +
6160
" vec3 new_color = clamp(xform + (temp - xform) * (dither - 0.5), 0.0, 1.0);\n" +
@@ -70,8 +69,6 @@ public class DocumentaryFilter extends Filter {
7069

7170
public DocumentaryFilter(String name) {
7271
super(name);
73-
74-
mRandom = new Random();
7572
}
7673

7774
@Override
@@ -85,14 +82,6 @@ public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
8582
return inputFormat;
8683
}
8784

88-
@Override
89-
public void tearDown(FilterContext context) {
90-
if (mNoiseFrame != null) {
91-
mNoiseFrame.release();
92-
mNoiseFrame = null;
93-
}
94-
}
95-
9685
public void initProgram(FilterContext context, int target) {
9786
switch (target) {
9887
case FrameFormat.TARGET_GPU:
@@ -123,34 +112,14 @@ public void process(FilterContext context) {
123112
if (inputFormat.getWidth() != mWidth || inputFormat.getHeight() != mHeight) {
124113
mWidth = inputFormat.getWidth();
125114
mHeight = inputFormat.getHeight();
126-
127-
int[] buffer = new int[mWidth * mHeight];
128-
for (int i = 0; i < mWidth * mHeight; ++i) {
129-
buffer[i] = mRandom.nextInt(255);
130-
}
131-
FrameFormat format = ImageFormat.create(mWidth, mHeight,
132-
ImageFormat.COLORSPACE_RGBA,
133-
FrameFormat.TARGET_GPU);
134-
if (mNoiseFrame != null) {
135-
mNoiseFrame.release();
136-
}
137-
mNoiseFrame = context.getFrameManager().newFrame(format);
138-
mNoiseFrame.setInts(buffer);
139-
140115
initParameters();
141116
}
142117

143-
if (mNoiseFrame != null && (mNoiseFrame.getFormat().getWidth() != mWidth ||
144-
mNoiseFrame.getFormat().getHeight() != mHeight)) {
145-
throw new RuntimeException("Random map and imput image size mismatch!");
146-
}
147-
148118
// Create output frame
149119
Frame output = context.getFrameManager().newFrame(inputFormat);
150120

151121
// Process
152-
Frame[] inputs = {input, mNoiseFrame};
153-
mProgram.process(inputs, output);
122+
mProgram.process(input, output);
154123

155124
// Push output
156125
pushOutput("image", output);

media/mca/filterpacks/java/android/filterpacks/imageproc/LomoishFilter.java

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import android.filterfw.core.ShaderProgram;
2929
import android.filterfw.format.ImageFormat;
3030

31-
import java.util.Random;
32-
3331
public class LomoishFilter extends Filter {
3432

3533
@GenerateFieldPort(name = "tile_size", hasDefault = true)
@@ -41,19 +39,18 @@ public class LomoishFilter extends Filter {
4139
private int mHeight = 0;
4240
private int mTarget = FrameFormat.TARGET_UNSPECIFIED;
4341

44-
private Frame mNoiseFrame;
45-
private Random mRandom;
46-
4742
private final String mLomoishShader =
4843
"precision mediump float;\n" +
4944
"uniform sampler2D tex_sampler_0;\n" +
50-
"uniform sampler2D tex_sampler_1;\n" +
5145
"uniform float stepsizeX;\n" +
5246
"uniform float stepsizeY;\n" +
5347
"uniform float stepsize;\n" +
5448
"uniform vec2 center;\n" +
5549
"uniform float inv_max_dist;\n" +
5650
"varying vec2 v_texcoord;\n" +
51+
"float rand(vec2 loc) {\n" +
52+
" return fract(sin(dot(loc, vec2(12.9898, 78.233))) * 43758.5453);\n" +
53+
"}\n" +
5754
"void main() {\n" +
5855
// sharpen
5956
" vec3 nbr_color = vec3(0.0, 0.0, 0.0);\n" +
@@ -99,7 +96,7 @@ public class LomoishFilter extends Filter {
9996
" }\n" +
10097
" c_color.b = s_color.b * 0.5 + 0.25;\n" +
10198
// blackwhite
102-
" float dither = texture2D(tex_sampler_1, v_texcoord).r;\n" +
99+
" float dither = rand(v_texcoord);\n" +
103100
" vec3 xform = clamp((c_color.rgb - 0.15) * 1.53846, 0.0, 1.0);\n" +
104101
" vec3 temp = clamp((color.rgb + stepsize - 0.15) * 1.53846, 0.0, 1.0);\n" +
105102
" vec3 bw_color = clamp(xform + (temp - xform) * (dither - 0.5), 0.0, 1.0);\n" +
@@ -111,8 +108,6 @@ public class LomoishFilter extends Filter {
111108

112109
public LomoishFilter(String name) {
113110
super(name);
114-
115-
mRandom = new Random();
116111
}
117112

118113
@Override
@@ -126,14 +121,6 @@ public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
126121
return inputFormat;
127122
}
128123

129-
@Override
130-
public void tearDown(FilterContext context) {
131-
if (mNoiseFrame != null) {
132-
mNoiseFrame.release();
133-
mNoiseFrame = null;
134-
}
135-
}
136-
137124
public void initProgram(FilterContext context, int target) {
138125
switch (target) {
139126
case FrameFormat.TARGET_GPU:
@@ -180,34 +167,14 @@ public void process(FilterContext context) {
180167
if (inputFormat.getWidth() != mWidth || inputFormat.getHeight() != mHeight) {
181168
mWidth = inputFormat.getWidth();
182169
mHeight = inputFormat.getHeight();
183-
184-
int[] buffer = new int[mWidth * mHeight];
185-
for (int i = 0; i < mWidth * mHeight; ++i) {
186-
buffer[i] = mRandom.nextInt(255);
187-
}
188-
FrameFormat format = ImageFormat.create(mWidth, mHeight,
189-
ImageFormat.COLORSPACE_RGBA,
190-
FrameFormat.TARGET_GPU);
191-
if (mNoiseFrame != null) {
192-
mNoiseFrame.release();
193-
}
194-
mNoiseFrame = context.getFrameManager().newFrame(format);
195-
mNoiseFrame.setInts(buffer);
196-
197170
initParameters();
198171
}
199172

200-
if (mNoiseFrame != null && (mNoiseFrame.getFormat().getWidth() != mWidth ||
201-
mNoiseFrame.getFormat().getHeight() != mHeight)) {
202-
throw new RuntimeException("Random map and imput image size mismatch!");
203-
}
204-
205173
// Create output frame
206174
Frame output = context.getFrameManager().newFrame(inputFormat);
207175

208176
// Process
209-
Frame[] inputs = {input, mNoiseFrame};
210-
mProgram.process(inputs, output);
177+
mProgram.process(input, output);
211178

212179
// Push output
213180
pushOutput("image", output);

0 commit comments

Comments
 (0)