android-gpuimage icon indicating copy to clipboard operation
android-gpuimage copied to clipboard

GPUImageToneCurveFilter bug fix for updateToneCurveTexture()

Open BrianARice opened this issue 12 years ago • 1 comments

I am totally unskilled at adding fixes to these projects... but if someone else wants to add this fix... here it is:

For the following command (that loads the curve data up into the texture memory of the GPU):

GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, 256 /*width*/, 1 /*height*/, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, ByteBuffer.wrap(toneCurveByteArray));

In the iOS version it uses GLES20.GL_RGBA for the third parameter and it uses GLES20.GL_BGRA for the 7th parameter… effectively swapping the R and B values during the texture load… However, it appears that Android does not support having these be different so the “same” values were used as shown above… but also then the code should swap the R and B values manually but this was not done. My full fix for this function is below. (all I really had to do was change the indexes for where the R and B values were being saved).

private void updateToneCurveTexture() {
    runOnDraw(new Runnable() {
        @Override
        public void run() {
            GLES20.glActiveTexture(GLES20.GL_TEXTURE3);
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mToneCurveTexture[0]);

            if ((mRedCurve.size() >= 256) && (mGreenCurve.size() >= 256) && (mBlueCurve.size() >= 256) && (mRgbCompositeCurve.size() >= 256)) {
                byte[] toneCurveByteArray = new byte[256 * 4];
                for (int currentCurveIndex = 0; currentCurveIndex < 256; currentCurveIndex++) {
                    // BGRA for upload to texture
/*  glTexImage2D doesn't support GL_BGRA for the 7th parameter like iOS has it... so we'll switch it here - BAR                     
                    toneCurveByteArray[currentCurveIndex * 4] = (byte) ((int) Math.min(Math.max(currentCurveIndex + mBlueCurve.get(currentCurveIndex) + mRgbCompositeCurve.get(currentCurveIndex), 0), 255) & 0xff);
                    toneCurveByteArray[currentCurveIndex * 4 + 1] = (byte) ((int) Math.min(Math.max(currentCurveIndex + mGreenCurve.get(currentCurveIndex) + mRgbCompositeCurve.get(currentCurveIndex), 0), 255) & 0xff);
                    toneCurveByteArray[currentCurveIndex * 4 + 2] = (byte) ((int) Math.min(Math.max(currentCurveIndex + mRedCurve.get(currentCurveIndex) + mRgbCompositeCurve.get(currentCurveIndex), 0), 255) & 0xff);
                    toneCurveByteArray[currentCurveIndex * 4 + 3] = (byte) (255 & 0xff);
*/                    
                    toneCurveByteArray[currentCurveIndex * 4 + 2] = (byte) ((int) Math.min(Math.max(currentCurveIndex + mBlueCurve.get(currentCurveIndex) + mRgbCompositeCurve.get(currentCurveIndex), 0), 255) & 0xff);
                    toneCurveByteArray[currentCurveIndex * 4 + 1] = (byte) ((int) Math.min(Math.max(currentCurveIndex + mGreenCurve.get(currentCurveIndex) + mRgbCompositeCurve.get(currentCurveIndex), 0), 255) & 0xff);
                    toneCurveByteArray[currentCurveIndex * 4 + 0] = (byte) ((int) Math.min(Math.max(currentCurveIndex + mRedCurve.get(currentCurveIndex) + mRgbCompositeCurve.get(currentCurveIndex), 0), 255) & 0xff);
                    toneCurveByteArray[currentCurveIndex * 4 + 3] = (byte) (255 & 0xff);
                }

                GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, 256 /*width*/, 1 /*height*/, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, ByteBuffer.wrap(toneCurveByteArray));
            }
//        Buffer pixels!
//        GLES20.glTexImage2D(int target,
//            int level,
//            int internalformat,
//            int width,
//            int height,
//            int border,
//            int format,
//            int type,
//            java.nio.Buffer pixels);
        }
    });
}

BrianARice avatar Feb 25 '14 19:02 BrianARice

YOU SAVED MY LIFE!!!

zhaoyouqi avatar Aug 20 '14 08:08 zhaoyouqi