You are on page 1of 2

package com.example.mohitpant.beautyeffectapp.

BeautyEffect;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
//import android.renderscript.Allocation;
//import android.renderscript.Element;
//import android.renderscript.RenderScript;
//import android.renderscript.ScriptIntrinsic3DLUT;
//import android.renderscript.Type;
import android.support.v8.renderscript.*;

import android.util.Log;
@SuppressLint("NewApi")
public class RenderScriptLutColorFilter {

private final int RED_DIM = 64;


private final int GREEN_DIM = 64;
private final int BLUE_DIM = 64;
private static RenderScript rs;
Context context;

public RenderScriptLutColorFilter(Context context) {


// TODO Auto-generated constructor stub
this.context = context;
rs = RenderScript.create(context);
}

public Bitmap renderImage(Bitmap sourceImage, Bitmap lutImage){


Bitmap resultImage = Bitmap.createBitmap(sourceImage.getWidth(),
sourceImage.getHeight(), sourceImage.getConfig());

ScriptIntrinsic3DLUT scriptLut = getRsScript(lutImage);

Allocation mAllocIn = Allocation.createFromBitmap(rs, sourceImage);


Allocation mAllocOut = Allocation.createFromBitmap(rs, resultImage);

scriptLut.forEach(mAllocIn, mAllocOut);
mAllocOut.copyTo(resultImage);

// if (intensity != 1) {
// outputBitmap = renderIntensity(bitmap, outputBitmap,
intensity);
// }

return resultImage;

private synchronized Allocation getLutCube(RenderScript rs, Bitmap lutImage)


{
Bitmap lutBitmap = lutImage;

final Type.Builder tb = new Type.Builder(rs, Element.U8_4(rs));


tb.setX(RED_DIM);
tb.setY(GREEN_DIM);
tb.setZ(BLUE_DIM);
Allocation mAllocCube = Allocation.createTyped(rs, tb.create());

final int w = lutBitmap.getWidth();


final int h = lutBitmap.getHeight();

int[] lut = new int[w * h];


int[] pixels = new int[w * h];

lutBitmap.getPixels(pixels, 0, w, 0, 0, w, h);

for (int r = 0; r < RED_DIM; r++)


for (int g = 0; g < GREEN_DIM; g++)
for (int b = 0; b < BLUE_DIM; b++) {
try {
final int rgb = pixels[ (r / 8) * w * GREEN_DIM
+ (r % 8) * RED_DIM + g * w + b ];

lut[ r * BLUE_DIM * GREEN_DIM + g * BLUE_DIM +


b ] = 0xff000000 | ((rgb >> 16) & 0xFF) | ((rgb >> 8 ) & 0xFF) << 8 | ((rgb )
& 0xFF) << 16;
}catch (Exception e){
Log.e("Exception", ""+e);
}
}

try {
mAllocCube.copyFromUnchecked(lut);
} catch (Exception e) {

return mAllocCube;
}

private ScriptIntrinsic3DLUT getRsScript(Bitmap lutImage) {


ScriptIntrinsic3DLUT script;
script = ScriptIntrinsic3DLUT.create(rs, Element.RGBA_8888(rs));
script.setLUT(getLutCube(rs, lutImage));

return script;
}

You might also like