Skip to content

Instantly share code, notes, and snippets.

@ssube
Created July 21, 2014 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssube/c5d7769f3087aae10421 to your computer and use it in GitHub Desktop.
Save ssube/c5d7769f3087aae10421 to your computer and use it in GitHub Desktop.
package com.stackoverflow.questions;
public class BlurShader implements Shader {
@Override
public int apply(final int[] d, final int s, final int x, final int y) {
final int p = s * y + x;
final int a = d[p] * 2
+ d[p - 1]
+ d[p + 1]
+ d[p - s]
+ d[p + s];
return (1000 * (a + 500)) / 6000;
}
}
package com.stackoverflow.questions;
import java.util.Random;
public class Image {
public static Image random(final int newWidth, final int newHeight) {
final Random random = new Random();
return new Image(newWidth, newHeight).process((final int[] data, final int s, final int x, final int y) -> random.nextInt());
}
private final int[] data;
private final int width;
private final int height;
private final int stride;
public Image(final int width, final int height) {
this.width = width;
this.height = height;
this.stride = width + 2;
final int area = this.stride * (this.height + 2);
this.data = new int[area];
}
/**
* This method expects control of the array after being passed in. Do not reuse.
*/
public Image(final int width, final int height, final int[] data) {
this.width = width;
this.height = height;
this.stride = width + 2;
final int area = this.stride * (this.height + 2);
if (data.length != area) {
throw new IllegalArgumentException("Invalid data buffer.");
}
this.data = data;
}
public Image process(final Shader f) {
final int w = this.width, h = this.height, s = this.stride;
final int[] d = this.data;
final int[] n = new int[d.length];
for (int y = 1; y < h; ++y) {
for (int x = 1; x < w; ++x) {
final int p = y * s + x;
n[p] = f.apply(d, s, x, y);
}
}
return new Image(w, h, n);
}
public Image processInline() {
final int w = this.width, h = this.height, s = this.stride;
final int[] d = this.data;
final int[] n = new int[d.length];
for (int y = 1; y < h; ++y) {
for (int x = 1; x < w; ++x) {
final int p = y * s + x;
final int a = d[p] * 2
+ d[p - 1]
+ d[p + 1]
+ d[p - s]
+ d[p + s];
n[p] = (1000 * (a + 500)) / 6000;
}
}
return new Image(w, h, n);
}
}
package com.stackoverflow.questions;
public interface Shader {
int apply(int[] data, int s, int x, int y);
}
package com.stackoverflow.questions;
import org.openjdk.jmh.annotations.*;
@State(Scope.Benchmark)
public class ShaderBench {
public static final int width = 4000;
public static final int height = 2000;
public Shader proc;
public Shader lambda;
public Image image;
@Setup
public void setupBenchmark() {
this.image = Image.random(ShaderBench.width, ShaderBench.height);
this.proc = new BlurShader();
this.lambda = (final int[] d, final int s, final int x, final int y) -> {
final int p = s * y + x;
final int a = d[p] * 2
+ d[p - 1]
+ d[p + 1]
+ d[p - s]
+ d[p + s];
return (1000 * (a + 500)) / 6000;
};
}
@Benchmark
public Image testProcessProc() {
return this.image.process(this.proc);
}
@Benchmark
public Image testProcessInline() {
return this.image.processInline();
}
@Benchmark
public Image testProcessLambda() {
return this.image.process(this.lambda);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment