0

How can I use byte datatype in RS layer on android? I'm targeting Android 4.1 and up for this.

I'm reading a image file in a byte[] in my Java class and intend to pass this byte[] to RenderScript for some processing and then get the byte[] data back in Java class. I'm quite aware of doing this stuff had the data been int[]. So in other words my question would be - what is the equivalent datatype of byte in RenderScript?

Gagan
  • 1,497
  • 1
  • 12
  • 27
  • 1
    Can you elaborate on why you don't want to use Allocation? Can you explain what your are doing with the byte datatype. – Morrison Chang Nov 29 '13 at 17:35
  • Have edited my question. I could Allocation too as far as I can do it in Android 4.1, 4.2, 4.3 and 4.4 – Gagan Nov 30 '13 at 02:24

1 Answers1

1

You can access it as char in the .rs file. For instance, you can create a kernel that looks like this:

char __attribute__((kernel)) foo(char c) {
    return c + 1;
}

You will still need to create an Allocation in Java to pass that data to the kernel (reading and/or writing), and use copyTo()/copyFrom() to pass the Byte buffers back and forth.

Stephen Hines
  • 2,612
  • 1
  • 13
  • 12
  • Perfect. Just about what I was looking for. However when I send my `char[]` using `rsSendToClient` back to Java I get the `mData[]` in Big-Endian format. Is there a direct API in RenderScript to send/receive the data in Little-Endian format? – Gagan Nov 30 '13 at 07:06
  • 1
    No. I still don't understand why you prefer rsSendToClient(). Since I work on RS, I know that this is the slowest path back to Java (rather than using Allocations and the associated copy routines. In the future, with `USAGE_SHARED` you may be able to even get zero-copy for data types other than Bitmaps. – Stephen Hines Nov 30 '13 at 09:44
  • Can I use `Allocation` in android 4.1? AFAIK (ref:Google IO 2013) `rsSetElementAt` API is recently added in android RS layer. Please correct me if I'm wrong. If not `rsSetElementAt`, then is there any other way to transfer `byte[]` between RS and Java layers? – Gagan Nov 30 '13 at 17:28
  • You can, but I think there is a better way. Even for targeting 4.1, you should be using the support library. This lets you use the features of v18 on devices running v8 (FroYo) or higher. With the support library, you can use `rsSetElementAt()`. – Stephen Hines Nov 30 '13 at 18:11