12 messages in net.java.dev.jna.usersRe: [jna-users] Getting pointers from...
FromSent OnAttachments
Douglas AdlerAug 20, 2008 12:03 pm 
Timothy WallAug 20, 2008 12:55 pm 
Wayne MeissnerAug 20, 2008 6:37 pm 
Douglas AdlerAug 21, 2008 11:56 am 
Timothy WallAug 21, 2008 12:18 pm 
Wayne MeissnerAug 21, 2008 3:47 pm 
Wayne MeissnerAug 21, 2008 3:55 pm 
Timothy WallAug 22, 2008 7:41 am 
Wayne MeissnerAug 22, 2008 3:57 pm 
Timothy WallAug 22, 2008 4:04 pm 
Wayne MeissnerAug 22, 2008 4:39 pm 
Scott PalmerAug 22, 2008 6:47 pm 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] Getting pointers from javaActions...
From:Timothy Wall (twal@dev.java.net)
Date:Aug 20, 2008 12:55:07 pm
List:net.java.dev.jna.users

On Aug 20, 2008, at 3:04 PM, Douglas Adler wrote:

I am trying to allocate the memory for a bitmap in java and have a C library manipulate it.

I have found that I can do the following:

Memory ptr = new Memory(pixWidth * pixHeight * pixDepth);

I then pass the ptr to the C routines via JNA, when the library is done I do the following to get the image to the screen:

renderImage = (BufferedImage) createImage(width, height); int [] buffer = ptr.getIntArray((long) 0, (int) ptr.getSize() / 4);

renderImage.setRGB(0, 0, pixWidth, pixHeight, buffer, 0, pixWidth);

g2d.drawImage(renderImage, 0, 0, width, height, null);

All of this works fine, except it can’t keep up at a decent frame rate if the bitmap gets large (> 320x240).

If I comment out the ptr.getIntArray() line then both the native library and the java app can run without dropping frames.

Is there a way to get a JNA Pointer directly from the int [] so that I don’t have to do the copy that ptr.getIntArray() must be doing? That way the setRGB() method will simply operate on the existing int [] that is being manipulated in the C library as ptr.

Pointer, java.nio.Buffer, and primitive arrays are interchangeable, just change your function declaration (or declare all three and compare performance).

In addition, there's a Pointer.read() method that reads into an existing int[] to avoid allocating a new one every time.