{"id":481,"date":"2013-03-24T18:48:48","date_gmt":"2013-03-24T17:48:48","guid":{"rendered":"http:\/\/blog.ansuz.nl\/?p=481"},"modified":"2013-03-24T18:48:48","modified_gmt":"2013-03-24T17:48:48","slug":"renderscript","status":"publish","type":"post","link":"http:\/\/blog.ansuz.nl\/index.php\/2013\/03\/24\/renderscript\/","title":{"rendered":"Renderscript"},"content":{"rendered":"<p>To do some faster image manipulation I&#8217;ve been looking at <a href=\"http:\/\/developer.android.com\/guide\/topics\/renderscript\/index.html\" target=\"_blank\">Renderscript<\/a>.<\/p>\n<blockquote><p>Renderscript provides a platform-independent computation engine that operates at the native level. Use it to accelerate your apps that require extensive computational horsepower.<\/p><\/blockquote>\n<p>This made it a better candidate than using the <a href=\"http:\/\/developer.android.com\/tools\/sdk\/ndk\/overview.html\" target=\"_blank\">NDK with JNI<\/a>.\u00a0It&#8217;s also easier to maintain as an Android developer.<\/p>\n<p>What I wanted to achieve is a fast way to apply <a href=\"http:\/\/en.wikipedia.org\/wiki\/Gaussian_blur\" target=\"_blank\">Gaussian blur<\/a>. Googling around will provide you plenty ways to do this in Java. It let me to a <a href=\"http:\/\/xjaphx.wordpress.com\/2011\/06\/22\/image-processing-convolution-matrix\/\" target=\"_blank\">blog post about using a Convolution matrix<\/a> combined with various kernels to achieve different effects. Unfortunately doing this on large images in Java is slow.<\/p>\n<p>The other option was to use JNI, but I don&#8217;t feel confident enough about writing C\/C++. There are, however, some libraries out there worth looking at. <a href=\"http:\/\/opencv.org\/\" target=\"_blank\">OpenCV<\/a> and <a href=\"https:\/\/github.com\/lightbox\/PhotoProcessing\/tree\/master\/PhotoProcessing\" target=\"_blank\">Lightbox&#8217;\u00a0PhotoProcessing<\/a> for instance. But when you&#8217;re just trying to use one or two effects, this seems a bit overkill.<\/p>\n<p>Last but not least is Renderscript. This is a bit more accessible than the NDK and only results in a slight loss of performance compared to using the NDK. The only requirement is <a href=\"http:\/\/developer.android.com\/reference\/android\/renderscript\/package-summary.html\" target=\"_blank\">Android 3+<\/a> (API level 11 and above).<br \/>\nIf you&#8217;re looking to get started with Renderscript, Google has a good <a href=\"http:\/\/android-developers.blogspot.com.es\/2011\/02\/introducing-renderscript.html\" target=\"_blank\">introduction<\/a> and more\u00a0in-depth\u00a0article about the <a href=\"http:\/\/android-developers.blogspot.se\/2011\/03\/renderscript.html\" target=\"_blank\">design goals of Renderscript<\/a>. The <a href=\"http:\/\/developer.android.com\/reference\/renderscript\/globals.html\" target=\"_blank\">Renderscript reference<\/a> comes in very handy when you are writing your own scripts.<br \/>\nAt first I was happy to find the\u00a0<a href=\"http:\/\/developer.android.com\/reference\/android\/renderscript\/ScriptIntrinsicBlur.html\" target=\"_blank\">ScriptIntrinsicBlur<\/a> class, but unfortunately that one is only available from API level 17. Discarding that option, I decided to implement my own class to create a Gaussian blur effect. To start work on a fast algorithm for Gaussian blur, I had a look at how this is being done for games and came across a blog post about &#8220;<a href=\"http:\/\/rastergrid.com\/blog\/2010\/09\/efficient-gaussian-blur-with-linear-sampling\/\" target=\"_blank\">Efficient Gaussian blur<\/a>&#8220;. I&#8217;ve made a first (yet to be further optimized) version and tried it out, you can find the script at <a href=\"https:\/\/github.com\/wjwarren\/droid-asylum\/blob\/master\/src\/nl\/ansuz\/android\/asylum\/renderscript\/effects\/GaussianBlur.rs\" target=\"_blank\">Github<\/a>. This implementation already renders a Gaussian blur effect extremely fast. It looks great when using the emulator, but unfortunately when used on a phone it seems to produce some <a href=\"http:\/\/en.wikipedia.org\/wiki\/Scan_line\" target=\"_blank\">scanline<\/a>-like side-effect.<\/p>\n<p>The biggest downside of Renderscript is that it is only available on Android 3+. It does, however, look like Google is working on a compatibility library for\u00a0Android 2.2. When you clone &#8220;<a href=\"https:\/\/android.googlesource.com\/platform\/frameworks\/support\" target=\"_blank\">https:\/\/android.googlesource.<wbr>com\/platform\/frameworks\/<wbr>support<\/wbr><\/wbr><\/a>&#8220;, there seems to be a &#8220;renderscript\\v8&#8221; folder. I tried building it using the supplied Make file, but there were some errors.<\/p>\n<p>I hope the compatibility library for Renderscript arrives soon, so it can be used across a bigger range of devices.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>To do some faster image manipulation I&#8217;ve been looking at Renderscript. Renderscript provides a platform-independent computation engine that operates at the native level. Use it to accelerate your apps that require extensive computational horsepower. This made it a better candidate &hellip; <a href=\"http:\/\/blog.ansuz.nl\/index.php\/2013\/03\/24\/renderscript\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[119],"class_list":["post-481","post","type-post","status-publish","format-standard","hentry","category-android","tag-renderscript"],"_links":{"self":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/481","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/comments?post=481"}],"version-history":[{"count":7,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/481\/revisions"}],"predecessor-version":[{"id":489,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/481\/revisions\/489"}],"wp:attachment":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/media?parent=481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/categories?post=481"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/tags?post=481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}