use of android.renderscript.RenderScript in project vlc-android by GeoffreyMetais.
the class UiTools method blurBitmap.
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap blurBitmap(Bitmap bitmap, float radius) {
if (bitmap == null || bitmap.getConfig() == null)
return null;
// Let's create an empty bitmap with the same size of the bitmap we want to blur
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
// Instantiate a new Renderscript
RenderScript rs = RenderScript.create(VLCApplication.getAppContext());
// Create an Intrinsic Blur Script using the Renderscript
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
// Create the Allocations (in/out) with the Renderscript and the in/out bitmaps
Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
// Set the radius of the blur
blurScript.setRadius(radius);
// Perform the Renderscript
blurScript.setInput(allIn);
blurScript.forEach(allOut);
// Copy the final bitmap created by the out Allocation to the outBitmap
allOut.copyTo(outBitmap);
// After finishing everything, we destroy the Renderscript.
rs.destroy();
return outBitmap;
}
use of android.renderscript.RenderScript in project android_packages_apps_Snap by LineageOS.
the class CaptureUI method getMonoDummySurface.
public Surface getMonoDummySurface() {
if (mMonoDummyAllocation == null) {
RenderScript rs = RenderScript.create(mActivity);
Type.Builder yuvTypeBuilder = new Type.Builder(rs, Element.YUV(rs));
yuvTypeBuilder.setX(mPreviewWidth);
yuvTypeBuilder.setY(mPreviewHeight);
yuvTypeBuilder.setYuvFormat(ImageFormat.YUV_420_888);
mMonoDummyAllocation = Allocation.createTyped(rs, yuvTypeBuilder.create(), Allocation.USAGE_IO_INPUT | Allocation.USAGE_SCRIPT);
ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.RGBA_8888(rs));
yuvToRgbIntrinsic.setInput(mMonoDummyAllocation);
if (mSettingsManager.getValue(SettingsManager.KEY_MONO_PREVIEW).equalsIgnoreCase("on")) {
Type.Builder rgbTypeBuilder = new Type.Builder(rs, Element.RGBA_8888(rs));
rgbTypeBuilder.setX(mPreviewWidth);
rgbTypeBuilder.setY(mPreviewHeight);
mMonoDummyOutputAllocation = Allocation.createTyped(rs, rgbTypeBuilder.create(), Allocation.USAGE_SCRIPT | Allocation.USAGE_IO_OUTPUT);
mMonoDummyOutputAllocation.setSurface(mSurfaceHolderMono.getSurface());
mActivity.runOnUiThread(new Runnable() {
public void run() {
mSurfaceHolderMono.setFixedSize(mPreviewWidth, mPreviewHeight);
mSurfaceViewMono.setVisibility(View.VISIBLE);
}
});
}
mMonoDummyAllocation.setOnBufferAvailableListener(new MonoDummyListener(yuvToRgbIntrinsic));
mIsMonoDummyAllocationEverUsed = false;
}
return mMonoDummyAllocation.getSurface();
}
use of android.renderscript.RenderScript in project OpenCamera by ageback.
the class HDRProcessor method autoAlignment.
/**
* @param bitmaps Only required if use_mtb is true, otherwise may be null.
* @param base_bitmap Index of bitmap in bitmaps that should be kept fixed; the other bitmaps
* will be aligned relative to this.
* @param assume_sorted If assume_sorted if false, and use_mtb is true, this function will also
* sort the allocations and bitmaps from darkest to brightest.
* @param use_mtb Whether to align based on the median threshold bitmaps or not.
* @param floating_point If true, the first allocation is in floating point (F32_3) format.
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private BrightnessDetails autoAlignment(int[] offsets_x, int[] offsets_y, Allocation[] allocations, int width, int height, List<Bitmap> bitmaps, int base_bitmap, boolean assume_sorted, SortCallback sort_cb, boolean use_mtb, boolean floating_point, long time_s) {
if (MyDebug.LOG)
Log.d(TAG, "autoAlignment");
// initialise
for (int i = 0; i < offsets_x.length; i++) {
offsets_x[i] = 0;
offsets_y[i] = 0;
}
Allocation[] mtb_allocations = new Allocation[allocations.length];
if (MyDebug.LOG)
Log.d(TAG, "### time after creating mtb_allocations: " + (System.currentTimeMillis() - time_s));
// Testing shows that in practice we get good results by only aligning the centre quarter of the images. This gives better
// performance, and uses less memory.
int mtb_width = width / 2;
int mtb_height = height / 2;
int mtb_x = mtb_width / 2;
int mtb_y = mtb_height / 2;
/*int mtb_width = width;
int mtb_height = height;
int mtb_x = 0;
int mtb_y = 0;*/
// create RenderScript
ScriptC_create_mtb createMTBScript = new ScriptC_create_mtb(rs);
LuminanceInfo[] luminanceInfos = null;
if (use_mtb) {
luminanceInfos = new LuminanceInfo[allocations.length];
for (int i = 0; i < allocations.length; i++) {
luminanceInfos[i] = computeMedianLuminance(bitmaps.get(i), mtb_x, mtb_y, mtb_width, mtb_height);
if (MyDebug.LOG)
Log.d(TAG, i + ": median_value: " + luminanceInfos[i].median_value);
}
if (MyDebug.LOG)
Log.d(TAG, "time after computeMedianLuminance: " + (System.currentTimeMillis() - time_s));
}
if (!assume_sorted && use_mtb) {
if (MyDebug.LOG)
Log.d(TAG, "sort bitmaps");
class BitmapInfo {
final LuminanceInfo luminanceInfo;
final Bitmap bitmap;
final Allocation allocation;
final int index;
BitmapInfo(LuminanceInfo luminanceInfo, Bitmap bitmap, Allocation allocation, int index) {
this.luminanceInfo = luminanceInfo;
this.bitmap = bitmap;
this.allocation = allocation;
this.index = index;
}
}
List<BitmapInfo> bitmapInfos = new ArrayList<>(bitmaps.size());
for (int i = 0; i < bitmaps.size(); i++) {
BitmapInfo bitmapInfo = new BitmapInfo(luminanceInfos[i], bitmaps.get(i), allocations[i], i);
bitmapInfos.add(bitmapInfo);
}
Collections.sort(bitmapInfos, new Comparator<BitmapInfo>() {
@Override
public int compare(BitmapInfo o1, BitmapInfo o2) {
return o1.luminanceInfo.median_value - o2.luminanceInfo.median_value;
}
});
bitmaps.clear();
for (int i = 0; i < bitmapInfos.size(); i++) {
bitmaps.add(bitmapInfos.get(i).bitmap);
luminanceInfos[i] = bitmapInfos.get(i).luminanceInfo;
allocations[i] = bitmapInfos.get(i).allocation;
}
if (MyDebug.LOG) {
for (int i = 0; i < allocations.length; i++) {
Log.d(TAG, i + ": median_value: " + luminanceInfos[i].median_value);
}
}
if (sort_cb != null) {
List<Integer> sort_order = new ArrayList<>();
for (int i = 0; i < bitmapInfos.size(); i++) {
sort_order.add(bitmapInfos.get(i).index);
}
if (MyDebug.LOG)
Log.d(TAG, "sort_order: " + sort_order);
sort_cb.sortOrder(sort_order);
}
}
int median_brightness = -1;
if (use_mtb) {
median_brightness = luminanceInfos[base_bitmap].median_value;
if (MyDebug.LOG)
Log.d(TAG, "median_brightness: " + median_brightness);
}
for (int i = 0; i < allocations.length; i++) {
int median_value = -1;
if (use_mtb) {
median_value = luminanceInfos[i].median_value;
if (MyDebug.LOG)
Log.d(TAG, i + ": median_value: " + median_value);
/*if( median_value < 16 ) {
// needed for testHDR2, testHDR28
if( MyDebug.LOG )
Log.d(TAG, "image too dark to do alignment");
mtb_allocations[i] = null;
continue;
}*/
}
if (use_mtb && luminanceInfos[i].noisy) {
if (MyDebug.LOG)
Log.d(TAG, "unable to compute median luminance safely");
mtb_allocations[i] = null;
continue;
}
mtb_allocations[i] = Allocation.createTyped(rs, Type.createXY(rs, Element.U8(rs), mtb_width, mtb_height));
// set parameters
if (use_mtb)
createMTBScript.set_median_value(median_value);
createMTBScript.set_start_x(mtb_x);
createMTBScript.set_start_y(mtb_y);
createMTBScript.set_out_bitmap(mtb_allocations[i]);
if (MyDebug.LOG)
Log.d(TAG, "call createMTBScript");
Script.LaunchOptions launch_options = new Script.LaunchOptions();
// launch_options.setX((int)(width*0.25), (int)(width*0.75));
// launch_options.setY((int)(height*0.25), (int)(height*0.75));
// createMTBScript.forEach_create_mtb(allocations[i], mtb_allocations[i], launch_options);
launch_options.setX(mtb_x, mtb_x + mtb_width);
launch_options.setY(mtb_y, mtb_y + mtb_height);
if (use_mtb)
createMTBScript.forEach_create_mtb(allocations[i], launch_options);
else {
if (floating_point && i == 0)
createMTBScript.forEach_create_greyscale_f(allocations[i], launch_options);
else
createMTBScript.forEach_create_greyscale(allocations[i], launch_options);
}
if (MyDebug.LOG)
Log.d(TAG, "time after createMTBScript: " + (System.currentTimeMillis() - time_s));
/*if( MyDebug.LOG ) {
// debugging
byte [] mtb_bytes = new byte[mtb_width*mtb_height];
mtb_allocations[i].copyTo(mtb_bytes);
int [] pixels = new int[mtb_width*mtb_height];
for(int j=0;j<mtb_width*mtb_height;j++) {
byte b = mtb_bytes[j];
pixels[j] = Color.argb(255, b, b, b);
}
Bitmap mtb_bitmap = Bitmap.createBitmap(pixels, mtb_width, mtb_height, Bitmap.Config.ARGB_8888);
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/mtb_bitmap" + i + ".jpg");
try {
OutputStream outputStream = new FileOutputStream(file);
mtb_bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outputStream);
outputStream.close();
MainActivity mActivity = (MainActivity) context;
mActivity.getStorageUtils().broadcastFile(file, true, false, true);
}
catch(IOException e) {
e.printStackTrace();
}
mtb_bitmap.recycle();
}*/
}
if (MyDebug.LOG)
Log.d(TAG, "### time after all createMTBScript: " + (System.currentTimeMillis() - time_s));
// The initial step_size N should be a power of 2; the maximum offset we can achieve by the algorithm is N-1.
// For pictures resolution 4160x3120, this gives max_ideal_size 27, and initial_step_size 32.
// On tests testHDR1 to testHDR35, the max required offset was 24 pixels (for testHDR33) even when using
// inital_step_size of 64.
// Note, there isn't really a performance cost in allowing higher initial step sizes (as larger sizes have less
// sampling - since we sample every step_size pixels - though there might be some overhead for every extra call
// to renderscript that we do). But high step sizes have a risk of producing really bad results if we were
// to misidentify cases as needing a large offset.
// n.b., use the full width and height here, not the mtb_width, height
int max_dim = Math.max(width, height);
int max_ideal_size = max_dim / 150;
int initial_step_size = 1;
while (initial_step_size < max_ideal_size) {
initial_step_size *= 2;
}
if (MyDebug.LOG) {
Log.d(TAG, "max_dim: " + max_dim);
Log.d(TAG, "max_ideal_size: " + max_ideal_size);
Log.d(TAG, "initial_step_size: " + initial_step_size);
}
if (mtb_allocations[base_bitmap] == null) {
if (MyDebug.LOG)
Log.d(TAG, "base image not suitable for image alignment");
return new BrightnessDetails(median_brightness);
}
// create RenderScript
ScriptC_align_mtb alignMTBScript = new ScriptC_align_mtb(rs);
// set parameters
alignMTBScript.set_bitmap0(mtb_allocations[base_bitmap]);
for (int i = 0; i < allocations.length; i++) {
if (i == base_bitmap) {
// don't need to align the "base" reference image
continue;
}
if (mtb_allocations[i] == null) {
if (MyDebug.LOG)
Log.d(TAG, "image " + i + " not suitable for image alignment");
continue;
}
alignMTBScript.set_bitmap1(mtb_allocations[i]);
int step_size = initial_step_size;
while (step_size > 1) {
step_size /= 2;
alignMTBScript.set_off_x(offsets_x[i]);
alignMTBScript.set_off_y(offsets_y[i]);
alignMTBScript.set_step_size(step_size);
if (MyDebug.LOG) {
Log.d(TAG, "call alignMTBScript for image: " + i);
Log.d(TAG, " versus base image: " + base_bitmap);
Log.d(TAG, "step_size: " + step_size);
}
Allocation errorsAllocation = Allocation.createSized(rs, Element.I32(rs), 9);
alignMTBScript.bind_errors(errorsAllocation);
alignMTBScript.invoke_init_errors();
// see note inside align_mtb.rs/align_mtb() for why we sample over a subset of the image
Script.LaunchOptions launch_options = new Script.LaunchOptions();
int stop_x = mtb_width / step_size;
int stop_y = mtb_height / step_size;
if (MyDebug.LOG) {
Log.d(TAG, "stop_x: " + stop_x);
Log.d(TAG, "stop_y: " + stop_y);
}
// launch_options.setX((int)(stop_x*0.25), (int)(stop_x*0.75));
// launch_options.setY((int)(stop_y*0.25), (int)(stop_y*0.75));
launch_options.setX(0, stop_x);
launch_options.setY(0, stop_y);
if (use_mtb)
alignMTBScript.forEach_align_mtb(mtb_allocations[base_bitmap], launch_options);
else
alignMTBScript.forEach_align(mtb_allocations[base_bitmap], launch_options);
if (MyDebug.LOG)
Log.d(TAG, "time after alignMTBScript: " + (System.currentTimeMillis() - time_s));
int best_error = -1;
int best_id = -1;
int[] errors = new int[9];
errorsAllocation.copyTo(errors);
for (int j = 0; j < 9; j++) {
int this_error = errors[j];
if (MyDebug.LOG)
Log.d(TAG, " errors[" + j + "]: " + this_error);
if (best_id == -1 || this_error < best_error) {
best_error = this_error;
best_id = j;
}
}
if (MyDebug.LOG)
Log.d(TAG, " best_id " + best_id + " error: " + best_error);
if (best_id != -1) {
int this_off_x = best_id % 3;
int this_off_y = best_id / 3;
this_off_x--;
this_off_y--;
if (MyDebug.LOG) {
Log.d(TAG, "this_off_x: " + this_off_x);
Log.d(TAG, "this_off_y: " + this_off_y);
}
offsets_x[i] += this_off_x * step_size;
offsets_y[i] += this_off_y * step_size;
if (MyDebug.LOG) {
Log.d(TAG, "offsets_x is now: " + offsets_x[i]);
Log.d(TAG, "offsets_y is now: " + offsets_y[i]);
}
}
}
}
/*for(int i=0;i<allocations.length;i++) {
offsets_x[i] = 0;
offsets_y[i] = 0;
}*/
return new BrightnessDetails(median_brightness);
}
use of android.renderscript.RenderScript in project android_packages_apps_Dialer by LineageOS.
the class VideoCallFragment method blur.
private static void blur(Context context, Bitmap image, float blurRadius) {
RenderScript renderScript = RenderScript.create(context);
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
Allocation allocationIn = Allocation.createFromBitmap(renderScript, image);
Allocation allocationOut = Allocation.createFromBitmap(renderScript, image);
blurScript.setRadius(blurRadius);
blurScript.setInput(allocationIn);
blurScript.forEach(allocationOut);
allocationOut.copyTo(image);
blurScript.destroy();
allocationIn.destroy();
allocationOut.destroy();
}
use of android.renderscript.RenderScript in project 9GAG by stormzhang.
the class Blur method fastblur.
@SuppressLint("NewApi")
public static Bitmap fastblur(Context context, Bitmap sentBitmap, int radius) {
if (Build.VERSION.SDK_INT > 16) {
Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
final RenderScript rs = RenderScript.create(context);
final Allocation input = Allocation.createFromBitmap(rs, sentBitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
final Allocation output = Allocation.createTyped(rs, input.getType());
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setRadius(radius);
script.setInput(input);
script.forEach(output);
output.copyTo(bitmap);
return bitmap;
}
Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
if (radius < 1) {
return (null);
}
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int[] pix = new int[w * h];
Log.e("pix", w + " " + h + " " + pix.length);
bitmap.getPixels(pix, 0, w, 0, 0, w, h);
int wm = w - 1;
int hm = h - 1;
int wh = w * h;
int div = radius + radius + 1;
int[] r = new int[wh];
int[] g = new int[wh];
int[] b = new int[wh];
int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
int[] vmin = new int[Math.max(w, h)];
int divsum = (div + 1) >> 1;
divsum *= divsum;
int[] dv = new int[256 * divsum];
for (i = 0; i < 256 * divsum; i++) {
dv[i] = (i / divsum);
}
yw = yi = 0;
int[][] stack = new int[div][3];
int stackpointer;
int stackstart;
int[] sir;
int rbs;
int r1 = radius + 1;
int routsum, goutsum, boutsum;
int rinsum, ginsum, binsum;
for (y = 0; y < h; y++) {
rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
for (i = -radius; i <= radius; i++) {
p = pix[yi + Math.min(wm, Math.max(i, 0))];
sir = stack[i + radius];
sir[0] = (p & 0xff0000) >> 16;
sir[1] = (p & 0x00ff00) >> 8;
sir[2] = (p & 0x0000ff);
rbs = r1 - Math.abs(i);
rsum += sir[0] * rbs;
gsum += sir[1] * rbs;
bsum += sir[2] * rbs;
if (i > 0) {
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
} else {
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
}
}
stackpointer = radius;
for (x = 0; x < w; x++) {
r[yi] = dv[rsum];
g[yi] = dv[gsum];
b[yi] = dv[bsum];
rsum -= routsum;
gsum -= goutsum;
bsum -= boutsum;
stackstart = stackpointer - radius + div;
sir = stack[stackstart % div];
routsum -= sir[0];
goutsum -= sir[1];
boutsum -= sir[2];
if (y == 0) {
vmin[x] = Math.min(x + radius + 1, wm);
}
p = pix[yw + vmin[x]];
sir[0] = (p & 0xff0000) >> 16;
sir[1] = (p & 0x00ff00) >> 8;
sir[2] = (p & 0x0000ff);
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
rsum += rinsum;
gsum += ginsum;
bsum += binsum;
stackpointer = (stackpointer + 1) % div;
sir = stack[(stackpointer) % div];
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
rinsum -= sir[0];
ginsum -= sir[1];
binsum -= sir[2];
yi++;
}
yw += w;
}
for (x = 0; x < w; x++) {
rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
yp = -radius * w;
for (i = -radius; i <= radius; i++) {
yi = Math.max(0, yp) + x;
sir = stack[i + radius];
sir[0] = r[yi];
sir[1] = g[yi];
sir[2] = b[yi];
rbs = r1 - Math.abs(i);
rsum += r[yi] * rbs;
gsum += g[yi] * rbs;
bsum += b[yi] * rbs;
if (i > 0) {
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
} else {
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
}
if (i < hm) {
yp += w;
}
}
yi = x;
stackpointer = radius;
for (y = 0; y < h; y++) {
// Preserve alpha channel: ( 0xff000000 & pix[yi] )
pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];
rsum -= routsum;
gsum -= goutsum;
bsum -= boutsum;
stackstart = stackpointer - radius + div;
sir = stack[stackstart % div];
routsum -= sir[0];
goutsum -= sir[1];
boutsum -= sir[2];
if (x == 0) {
vmin[y] = Math.min(y + r1, hm) * w;
}
p = x + vmin[y];
sir[0] = r[p];
sir[1] = g[p];
sir[2] = b[p];
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
rsum += rinsum;
gsum += ginsum;
bsum += binsum;
stackpointer = (stackpointer + 1) % div;
sir = stack[stackpointer];
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
rinsum -= sir[0];
ginsum -= sir[1];
binsum -= sir[2];
yi += w;
}
}
Log.e("pix", w + " " + h + " " + pix.length);
bitmap.setPixels(pix, 0, w, 0, 0, w, h);
return (bitmap);
}
Aggregations