Showing posts with label RenderScript. Show all posts
Showing posts with label RenderScript. Show all posts

Tuesday, July 12, 2016

Android RenderScript mono filter 예제

// mono.rs
#pragma version(1)
#pragma rs java_package_name(com.example.renderscriptsample)

float3 gMonoMult = {0.2125, 0.7154, 0.0721};

void root(const uchar4 *v_in, uchar4 *v_out) {
    float4 f4 = rsUnpackColor8888(*v_in);
    float3 mono = dot(f4.rgb, gMonoMult);
    *v_out = rsPackColorTo8888(mono);
}

MainActivity.java
package com.example.renderscriptsample;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.renderscript.RenderScript;
import android.view.Menu;
import android.widget.ImageView;

public class MainActivity extends Activity {
 // RenderScript
 private RenderScript mRS;
 private Allocation mInAllocation;
 private Allocation mOutAllocation;
 private ScriptC_mono mScript;

 private Bitmap loadBitmap(int resource) {
  final BitmapFactory.Options options = new BitmapFactory.Options();
  options.inPreferredConfig = Bitmap.Config.ARGB_8888;
  return BitmapFactory.decodeResource(getResources(), resource, options);
 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  // bitmap load
  final Bitmap bm = loadBitmap(R.drawable.data);
  final Bitmap bmOut = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());

  // renderscript init
  mRS = RenderScript.create(this);
  mInAllocation = Allocation.createFromBitmap(mRS, bm, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
  mOutAllocation = Allocation.createFromBitmap(mRS, bm, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
  mScript = new ScriptC_mono(mRS, getResources(), R.raw.mono);

  // renderscript run
  mScript.forEach_root(mInAllocation, mOutAllocation);
  mOutAllocation.copyTo(bmOut);
 
  // set output bitmap
  final ImageView iv = (ImageView) findViewById(R.id.imageView1);
  iv.setImageBitmap(bmOut);

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}

Android RenderScript FilterScript 예제

FilterScript

  • Introduced in Android 4.2 (API Level 17), Filterscript defines a subset of Renderscript that focuses on image processing operations, such as those that you would typically write with an OpenGL ES fragment shader.
  • 현재 android developer의 renderscript 항목에는 기본적으로 filterscript를 사용하기를 설명하고 있다.
    • http://developer.android.com/guide/topics/renderscript/compute.html
Usage
  • Inputs and return values of root functions cannot contain pointers. The default root function signature contains pointers, so you must use the __attribute__((kernel)) attribute to declare a custom root function when using Filterscript.
  • Built-in types cannot exceed 32-bits.
  • Filterscript must always use relaxed floating point precision by using the rs_fp_relaxed pragma.
    • Most applications can use rs_fp_relaxed without any side effects. This may be very beneficial on some architectures due to additional optimizations only available with relaxed precision (such as SIMD CPU instructions).
  • Filterscript files must end with an .fs extension, instead of an .rs extension
    • Android-18에서는 fs대신에 rs를 써도 된다.

//mono.fs
#pragma version(1)
#pragma rs java_package_name(com.example.renderscriptsample)
#pragma rs_fp_relaxed
// for version 17
// can called by forEach_invert(in, out)
uchar4 __attribute__((kernel)) invert(uchar4 in, uint32_t x, uint32_t y) {
  uchar4 out = in;
  out.r = 255 - in.r;
  out.g = 255 - in.g;
  out.b = 255 - in.b;
  return out;
}


//MainActivity.java
// renderscript init
mRS = RenderScript.create(this);
mInAllocation = Allocation.createFromBitmap(mRS, bm, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
mOutAllocation = Allocation.createFromBitmap(mRS, bm, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
mScript = new ScriptC_mono(mRS, getResources(), R.raw.mono);
// renderscript run
mScript.forEach_invert(mInAllocation, mOutAllocation);

Saturday, April 4, 2015

Android RenderScript 세피아 필터

#include "rs_matrix.rsh"

#pragma version(1)
#pragma rs java_package_name(com.example.android.rs.hellocompute)

float gFactor;// 0.0f: color, 1.0f: mono
float3 m0 = {0.3588f, 0.7044f, 0.1368f};
float3 m1 = {0.2990f, 0.5870f, 0.1140f};
float3 m2 = {0.2392f, 0.4696f, 0.0912f};

void root(const uchar4 *v_in, uchar4 *v_out) {
    float4 f4 = rsUnpackColor8888(*v_in);
    float3 out;
    out.r = dot(f4.rgb, m0);
    out.g = dot(f4.rgb, m1);
    out.b = dot(f4.rgb, m2);
 
    if(out.r > 1.f)
     out.r = 1.f;
    if(out.g > 1.f)
     out.g = 1.f;
    if(out.b > 1.f)
     out.b = 1.f;

 float3 res = f4.rgb + (out - f4.rgb)*gFactor;
    *v_out = rsPackColorTo8888(res);
}

Wednesday, April 1, 2015

Android RenderScript 내장함수 위치

내장함수 있는곳:
/android-sdk/platform-tools/renderscript/include/*.rsh

rs_cl.rsh
rs_graphics.rsh
rs_object.rsh
rs_time.rsh
rs_types.rsh
rs_core.rsh
rs_math.rsh
rs_quaternion.rsh
rs_debug.rsh
rs_matrix.rsh
rs_allocation.rsh

Android 4.1에서 RenderScript 변경점

Android-16에서 변경점:
구버전:
1. RSSurfaceView는 GLSurfaceView와 유사한 동작을 하며 GLRenderer를 set한다.
2. RenderScriptGL이라는 객체를 사용하며, GLSL shader program을 지원한다.
3. 2번과 연동되어 rsg_graphics.rsh를 지원하며 void main()대신에 int main()을 사용한다.

신버전:
1. RSSurfaceView, RenderScriptGL을 사용하지 않는다.
2. 따라서 GLSL을 사용하지 못하고 독자적으로 .rs에서 내장함수를 사용한다.
3. rsg_graphics.rsh를 사용할수 없다. int main()을 RenderScriptGL을 사용하지 않고는 call할 방법이 없다.