Skip to main content

[JAI-IMAGEIO] access java allocated raster from C

6 replies [Last post]
Anonymous

Hi, I'm trying to understand if there is a way to:
- read an image through the imageio code
- pass the pointer to the raster to a native c code
- process the same image in C from the same image allocation

I need to pass the data contained in the raster to algorythms that are
written in C and fortran for processing.
Since imageio has native libs, I would imagine that there could some way?

Best regards,
Andrea

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Robert Engels

There are at least two different ways to pass an array to native code.

1. it copies the data into a "native" array, passes the native array, then after the method copies the data back into the java array
2. "pin" the java array, so then it can be passed to the native method, after the native method returns, "unpin" the rray - usually much faster than #1 but can cause other memory management problems

I beleive JNA supports both methods. The nice thing about JNA (vs. JNI) is that you don't have to write all of those JNI Java/C wrappers - you can write it all in Java, and the single JNA dll performs the bridge work. Probably a bit slower than a pure JNI solution if you are making massive amounts of native calls, but for what you are doing, it seems that may not be the case (e.g. a single intensive native call per image)

-----Original Message-----
>From: andrea antonello
>Sent: May 21, 2008 10:42 PM
>To: interest@jai-imageio.dev.java.net
>Subject: Re: [JAI-IMAGEIO] access java allocated raster from C
>
>Hi Robert,
>
>> The simplest solution probably involves using JNA.
>
>in the begin I thought that was a typo for JNI, but then I saw the
>project of which I really didn't know the existence. Thanks for the
>hint, I'll have to document myself.
>
>> It is easy if the Raster is a single array (depends on how the image is read), then pin the array, and pass it to the native code.
>
>I assume that would be the easiest way, since JNi easily supports the
>passing of arrays to native code? I'm not sure I understand what you
>mean here. I'll have a deep look into JNA.
>
>Thanks,
>Andrea
>
>
>
>
>>
>> -----Original Message-----
>>>From: andrea antonello
>>>Sent: May 21, 2008 5:51 PM
>>>To: ML jai-imageio
>>>Subject: [JAI-IMAGEIO] access java allocated raster from C
>>>
>>>Hi, I'm trying to understand if there is a way to:
>>>- read an image through the imageio code
>>>- pass the pointer to the raster to a native c code
>>>- process the same image in C from the same image allocation
>>>
>>>I need to pass the data contained in the raster to algorythms that are
>>>written in C and fortran for processing.
>>>Since imageio has native libs, I would imagine that there could some way?
>>>
>>>Best regards,
>>>Andrea
>>>
>>>---------------------------------------------------------------------
>>>To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
>>>For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
>> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>>
>>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
>For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

andrea antonello

Hi Robert,

> There are at least two different ways to pass an array to native code.
>
> 1. it copies the data into a "native" array, passes the native array, then after the method copies the data back into the java array
> 2. "pin" the java array, so then it can be passed to the native method, after the native method returns, "unpin" the rray - usually much faster than #1 but can cause other memory management problems
>
> I beleive JNA supports both methods. The nice thing about JNA (vs. JNI) is that you don't have to write all of those JNI Java/C wrappers - you can write it all in Java, and the single JNA dll performs the bridge work. Probably a bit slower than a pure JNI solution if you are making massive amounts of native calls, but for what you are doing, it seems that may not be the case (e.g. a single intensive native call per image)

I finally gave the JNA solution a ride and I have to say that I will
happily make use of it.
In fact, since in that case I make single heavy calls, there is no
performance problem.
If someone is interested, I write down some documentation here:
http://jgrasstechtips.blogspot.com/2008/05/how-to-pass-data-from-java-to...

Just to be sure I got it right (pardon me since this question is
driven by ignorance about the memory handling of imageio and about
what the native libs part really does): it is not possible to read
through java imageio an image and then access the memory allocation
(it was read into in java) from some utility methods already present
in the native imageio libs? I'm not sure I am able to be clear in my
explanation. I hope so anyway.

Thanks for the help,
Andrea

>
> -----Original Message-----
>>From: andrea antonello
>>Sent: May 21, 2008 10:42 PM
>>To: interest@jai-imageio.dev.java.net
>>Subject: Re: [JAI-IMAGEIO] access java allocated raster from C
>>
>>Hi Robert,
>>
>>> The simplest solution probably involves using JNA.
>>
>>in the begin I thought that was a typo for JNI, but then I saw the
>>project of which I really didn't know the existence. Thanks for the
>>hint, I'll have to document myself.
>>
>>> It is easy if the Raster is a single array (depends on how the image is read), then pin the array, and pass it to the native code.
>>
>>I assume that would be the easiest way, since JNi easily supports the
>>passing of arrays to native code? I'm not sure I understand what you
>>mean here. I'll have a deep look into JNA.
>>
>>Thanks,
>>Andrea
>>
>>
>>
>>
>>>
>>> -----Original Message-----
>>>>From: andrea antonello
>>>>Sent: May 21, 2008 5:51 PM
>>>>To: ML jai-imageio
>>>>Subject: [JAI-IMAGEIO] access java allocated raster from C
>>>>
>>>>Hi, I'm trying to understand if there is a way to:
>>>>- read an image through the imageio code
>>>>- pass the pointer to the raster to a native c code
>>>>- process the same image in C from the same image allocation
>>>>
>>>>I need to pass the data contained in the raster to algorythms that are
>>>>written in C and fortran for processing.
>>>>Since imageio has native libs, I would imagine that there could some way?
>>>>
>>>>Best regards,
>>>>Andrea
>>>>
>>>>---------------------------------------------------------------------
>>>>To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
>>>>For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>>>>
>>>
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
>>> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>>>
>>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
>>For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

robert engels

You can get access to the memory directly.

BufferedImage i = ImageIO.read(...)

Raster r = i.getRaster()

DataBuffer db = r.getDataBuffer();

if(db instanceof DataBufferByte) {
byte[] bytes = ((DataBufferByte)db).getData();
} else if(...) {

The type of DataBuffer can usually be controlled by using a
ImageTypeSpecifier when reading the image, or passing a BufferedImage
to ImageIO.

On May 27, 2008, at 8:53 AM, andrea antonello wrote:

> Hi Robert,
>
>> There are at least two different ways to pass an array to native
>> code.
>>
>> 1. it copies the data into a "native" array, passes the native
>> array, then after the method copies the data back into the java array
>> 2. "pin" the java array, so then it can be passed to the native
>> method, after the native method returns, "unpin" the rray -
>> usually much faster than #1 but can cause other memory management
>> problems
>>
>> I beleive JNA supports both methods. The nice thing about JNA (vs.
>> JNI) is that you don't have to write all of those JNI Java/C
>> wrappers - you can write it all in Java, and the single JNA dll
>> performs the bridge work. Probably a bit slower than a pure JNI
>> solution if you are making massive amounts of native calls, but
>> for what you are doing, it seems that may not be the case (e.g. a
>> single intensive native call per image)
>
> I finally gave the JNA solution a ride and I have to say that I will
> happily make use of it.
> In fact, since in that case I make single heavy calls, there is no
> performance problem.
> If someone is interested, I write down some documentation here:
> http://jgrasstechtips.blogspot.com/2008/05/how-to-pass-data-from-
> java-to-c-with.html
>
> Just to be sure I got it right (pardon me since this question is
> driven by ignorance about the memory handling of imageio and about
> what the native libs part really does): it is not possible to read
> through java imageio an image and then access the memory allocation
> (it was read into in java) from some utility methods already present
> in the native imageio libs? I'm not sure I am able to be clear in my
> explanation. I hope so anyway.
>
> Thanks for the help,
> Andrea
>
>
>
>>
>> -----Original Message-----
>>> From: andrea antonello
>>> Sent: May 21, 2008 10:42 PM
>>> To: interest@jai-imageio.dev.java.net
>>> Subject: Re: [JAI-IMAGEIO] access java allocated raster from C
>>>
>>> Hi Robert,
>>>
>>>> The simplest solution probably involves using JNA.
>>>
>>> in the begin I thought that was a typo for JNI, but then I saw the
>>> project of which I really didn't know the existence. Thanks for the
>>> hint, I'll have to document myself.
>>>
>>>> It is easy if the Raster is a single array (depends on how the
>>>> image is read), then pin the array, and pass it to the native code.
>>>
>>> I assume that would be the easiest way, since JNi easily supports
>>> the
>>> passing of arrays to native code? I'm not sure I understand what you
>>> mean here. I'll have a deep look into JNA.
>>>
>>> Thanks,
>>> Andrea
>>>
>>>
>>>
>>>
>>>>
>>>> -----Original Message-----
>>>>> From: andrea antonello
>>>>> Sent: May 21, 2008 5:51 PM
>>>>> To: ML jai-imageio
>>>>> Subject: [JAI-IMAGEIO] access java allocated raster from C
>>>>>
>>>>> Hi, I'm trying to understand if there is a way to:
>>>>> - read an image through the imageio code
>>>>> - pass the pointer to the raster to a native c code
>>>>> - process the same image in C from the same image allocation
>>>>>
>>>>> I need to pass the data contained in the raster to algorythms
>>>>> that are
>>>>> written in C and fortran for processing.
>>>>> Since imageio has native libs, I would imagine that there could
>>>>> some way?
>>>>>
>>>>> Best regards,
>>>>> Andrea
>>>>>
>>>>> ------------------------------------------------------------------
>>>>> ---
>>>>> To unsubscribe, e-mail: interest-unsubscribe@jai-
>>>>> imageio.dev.java.net
>>>>> For additional commands, e-mail: interest-help@jai-
>>>>> imageio.dev.java.net
>>>>>
>>>>
>>>>
>>>>
>>>>
>>>> -------------------------------------------------------------------
>>>> --
>>>> To unsubscribe, e-mail: interest-unsubscribe@jai-
>>>> imageio.dev.java.net
>>>> For additional commands, e-mail: interest-help@jai-
>>>> imageio.dev.java.net
>>>>
>>>>
>>>
>>> --------------------------------------------------------------------
>>> -
>>> To unsubscribe, e-mail: interest-unsubscribe@jai-
>>> imageio.dev.java.net
>>> For additional commands, e-mail: interest-help@jai-
>>> imageio.dev.java.net
>>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
>> For additional commands, e-mail: interest-help@jai-
>> imageio.dev.java.net
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-
> imageio.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

demonduck
Offline
Joined: 2008-03-14

>The type of DataBuffer can usually be controlled by using a
>ImageTypeSpecifier when reading the image, or passing a BufferedImage
>to ImageIO.

I tried that, using a DataBufferInt as the data buffer type.

The read slowed by over 100% -- it took more than twice as long to
read the image.

Robert Engels

The simplest solution probably involves using JNA.

It is easy if the Raster is a single array (depends on how the image is read), then pin the array, and pass it to the native code.

-----Original Message-----
>From: andrea antonello
>Sent: May 21, 2008 5:51 PM
>To: ML jai-imageio
>Subject: [JAI-IMAGEIO] access java allocated raster from C
>
>Hi, I'm trying to understand if there is a way to:
>- read an image through the imageio code
>- pass the pointer to the raster to a native c code
>- process the same image in C from the same image allocation
>
>I need to pass the data contained in the raster to algorythms that are
>written in C and fortran for processing.
>Since imageio has native libs, I would imagine that there could some way?
>
>Best regards,
>Andrea
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
>For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

andrea antonello

Hi Robert,

> The simplest solution probably involves using JNA.

in the begin I thought that was a typo for JNI, but then I saw the
project of which I really didn't know the existence. Thanks for the
hint, I'll have to document myself.

> It is easy if the Raster is a single array (depends on how the image is read), then pin the array, and pass it to the native code.

I assume that would be the easiest way, since JNi easily supports the
passing of arrays to native code? I'm not sure I understand what you
mean here. I'll have a deep look into JNA.

Thanks,
Andrea

>
> -----Original Message-----
>>From: andrea antonello
>>Sent: May 21, 2008 5:51 PM
>>To: ML jai-imageio
>>Subject: [JAI-IMAGEIO] access java allocated raster from C
>>
>>Hi, I'm trying to understand if there is a way to:
>>- read an image through the imageio code
>>- pass the pointer to the raster to a native c code
>>- process the same image in C from the same image allocation
>>
>>I need to pass the data contained in the raster to algorythms that are
>>written in C and fortran for processing.
>>Since imageio has native libs, I would imagine that there could some way?
>>
>>Best regards,
>>Andrea
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
>>For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net