« タイマー | メイン | EGLImageKHR を使うテクスチャの高速書き換えはハードル高い »

2015年06月09日

Android:: RenderScript で画像の拡大-ニアレストネイバー

    

RenderScript で forEach する時、入力と出力指定するので、サイズが違う時どうするのかわからなかったが、入力を指定しなければいけるようだ。
つまり、uchar4 function(uint32_t x, uint32_t y) 的な定義をすれば、出力側のサイズ回呼ばれる様子。
単純にニアレストネイバーで2倍拡大する場合なら、以下のような RenderScript のソースコードで出来る。

#pragma version(1)
#pragma rs java_package_name(com.kaede_software.renderscripttest)
#include "rs_core.rsh"
rs_allocation in;
uchar4 __attribute__((kernel)) scalex2(uint32_t x, uint32_t y) {
return rsGetElementAt_uchar4( in, x/2, y/2 );
}

補間が必要なら、rsGetElementAt_uchar4 で複数画素取得すればできそう。
範囲外アクセスしないように min/max で以下のようにクリッピングする。
uint32_t x1 = min((int32_t)x+1, width-1);
uint32_t x2 = max((int32_t)x-1, 0);
width は外部から。

Java 側のソースコードは以下。

BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap, options );
RenderScript rs = RenderScript.create(this);
Allocation input = Allocation.createFromBitmap(rs, src);
int w2 = src.getWidth()*2;
int h2 = src.getHeight()*2;
Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(w2).setY(h2);
Allocation out = Allocation.createTyped(rs, rgbaType.create());
ScriptC_scale script = new ScriptC_scale(rs);
script.set_in(input);
script.forEach_scalex2( out );
Bitmap bmpout = Bitmap.createBitmap(w2, h2, Bitmap.Config.ARGB_8888);
out.copyTo(bmpout);


投稿者 Takenori : 2015年06月09日 16:44




comments powered by Disqus
Total : Today : Yesterday : なかのひと