Skip to content

Instantly share code, notes, and snippets.

@lukesleeman
Created August 5, 2013 01:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukesleeman/6152843 to your computer and use it in GitHub Desktop.
Save lukesleeman/6152843 to your computer and use it in GitHub Desktop.
Terrible hacks to GPUImage to get a bitmap of the rendered image on samsung galaxy Y
GPUImageRenderer.SAVE_TO_BITMAP = true;
GPUImageRenderer.SAVED_CALLBACK = new Runnable() {
@Override
public void run() {
Utility.debugDumpBitmap(ResizeActivity.this, GPUImageRenderer.SAVED_BITMAP);
}
};
glSurfaceView.requestRender();
@Override
public void onDrawFrame(final GL10 gl) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
runAll(mRunOnDraw);
mFilter.onDraw(mGLTextureId, mGLCubeBuffer, mGLTextureBuffer);
runAll(mRunOnDrawEnd);
if (mSurfaceTexture != null) {
mSurfaceTexture.updateTexImage();
}
if(SAVE_TO_BITMAP){
SAVED_BITMAP = savePixels(gl);
SAVED_CALLBACK.run();
}
}
public static boolean SAVE_TO_BITMAP = false;
public static Bitmap SAVED_BITMAP = null;
public static Runnable SAVED_CALLBACK = null;
public Bitmap savePixels(GL10 gl) {
int b[] = new int[mOutputWidth * mOutputHeight];
IntBuffer ib = IntBuffer.wrap(b);
ib.position(0);
gl.glReadPixels(0, 0, mOutputWidth, mOutputHeight, GL10.GL_RGBA,
GL10.GL_UNSIGNED_BYTE, ib);
// The bytes within the ints are in the wrong order for android, but
// convert into a
// bitmap anyway. They're also bottom-to-top rather than top-to-bottom.
// We'll fix
// this up soon using some fast API calls.
Bitmap glbitmap = Bitmap.createBitmap(b, mOutputWidth, mOutputHeight,
Bitmap.Config.ARGB_8888);
ib = null; // we're done with ib
b = null; // we're done with b, so allow the memory to be freed
// To swap the color channels, we'll use a
// ColorMatrix/ColorMatrixFilter. From the Android docs:
//
// This is a 5x4 matrix: [ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o,
// p, q, r, s, t ]
// When applied to a color [r, g, b, a] the resulting color is computed
// as (after clamping):
//
// R' = a*R + b*G + c*B + d*A + e;
// G' = f*R + g*G + h*B + i*A + j;
// B' = k*R + l*G + m*B + n*A + o;
// A' = p*R + q*G + r*B + s*A + t;
//
// We want to swap R and B, so the coefficients will be:
// R' = B => 0,0,1,0,0
// G' = G => 0,1,0,0,0
// B' = R => 1,0,0,0,0
// A' = A => 0,0,0,1,0
final float[] cmVals = { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 1, 0 };
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(cmVals))); // our
// R<->B
// swapping
// paint
Bitmap bitmap = Bitmap.createBitmap(mOutputWidth, mOutputHeight,
Bitmap.Config.ARGB_8888); // the bitmap we're going to draw onto
Canvas canvas = new Canvas(bitmap); // we draw to the bitmap through a
// canvas
// scaling: x = x, y = -y, i.e. vertically flip
canvas.scale(1f, -1f);
canvas.drawBitmap(glbitmap, 0,-glbitmap.getHeight(), paint); // draw the opengl bitmap onto
// the canvas, using the
// color swapping paint
return bitmap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment