Skip to main content

Creating Java Images from native code

5 replies [Last post]
swpalmer
Offline
Joined: 2003-06-10

I have a need to get image data from native code to a Java Image, e.g. a BufferedImage, in the most efficient manner possible.

Ideally I would like to write to the BufferedImage's raster directly in my native code, or get an image from a DirectByteBuffer of RGB pixel data. If I can reuse the same Image object multiple times to avoid excessive garbage creation that would be best.

I'm looking for options and pointers on how I should implement such a thing while avoiding as much as possible any data copying. It is likely that my data will need to be converted from YUV color space to RGB color space and I would like for that to be the only time the data is copied. So I would convert directly into the BufferedImage with my native color conversion (and in some cases scaler) loop which is already optimized with SIMD assembler.

Thanks in advance.

Reply viewing options

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

Hello,

I think you can do something like this:

1. Create a BufferedImage of appropriate type (say, INT_RGB)
2. get the array from the DataBuffer
(((DataBufferInt)(BI.getRaster().getDataBuffer())).getData() in case
of INT_[A]RGB image type) and pass it to the native code
3. At the native level, use GetPrimitiveArrayCritical to get the
pointer to the data, copy your image data
4. Render your BufferedImage at the java level

This is more or less what Swing does when rendering the native Look
and Feels.

Note that in this case the resulting BufferedImage will not be
HW-accelerated - that is, cached in vram. You can do it by yourself
by creating a VolatileImage and copying the BI to the VI, and then VI
to the screen - this makes sense only if you don't update the BI
on every frame and the cached VI can give you an advantage, though.

Thanks,
Dmitri

On Wed, May 10, 2006 at 02:52:38PM -0700, java2d@javadesktop.org wrote:
> I have a need to get image data from native code to a Java Image, e.g. a BufferedImage, in the most efficient manner possible.
>
> Ideally I would like to write to the BufferedImage's raster directly in my native code, or get an image from a DirectByteBuffer of RGB pixel data. If I can reuse the same Image object multiple times to avoid excessive garbage creation that would be best.
>
> I'm looking for options and pointers on how I should implement such a thing while avoiding as much as possible any data copying. It is likely that my data will need to be converted from YUV color space to RGB color space and I would like for that to be the only time the data is copied. So I would convert directly into the BufferedImage with my native color conversion (and in some cases scaler) loop which is already optimized with SIMD assembler.
>
> Thanks in advance.
> [Message sent by forum member 'swpalmer' (swpalmer)]
>
> http://forums.java.net/jive/thread.jspa?messageID=112638
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

swpalmer
Offline
Joined: 2003-06-10

Thanks Dimitri, You've confirmed my initial guess at what would make the most sense. I have one minor question...

If I were to use TYPE_3BYTE_BGR I assume that the array is padded at the end of each line much like a Windows BITMAP would expect for alignment. The documentation for TYPE_3BYTE_BGR does say "corresponding to a Windows-style BGR color model" but doesn't explicitly mention the per-line alignment restrictions (multiples of 32bits per line). Perhaps the documentation could be explicit about that.

Dmitri Trembovetski

On Thu, May 11, 2006 at 08:12:48AM -0700, java2d@javadesktop.org wrote:
> Thanks Dimitri, You've confirmed my initial guess at what would make the most sense. I have one minor question...
>
> If I were to use TYPE_3BYTE_BGR I assume that the array is padded at the end of each line much like a Windows BITMAP would expect for alignment. The documentation for TYPE_3BYTE_BGR does say "corresponding to a Windows-style BGR color model" but doesn't explicitly mention the per-line alignment restrictions (multiples of 32bits per line). Perhaps the documentation could be explicit about that.
> [Message sent by forum member 'swpalmer' (swpalmer)]

You should be able to figure out the scanline stride from the
image's sample model, and I believe that for TYPE_3BYTE_BGR it's
actually not padded, which means you'll need to work with the data
scanline at a time.

Thanks,
Dmitri

>
> http://forums.java.net/jive/thread.jspa?messageID=112791
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

swpalmer
Offline
Joined: 2003-06-10

I looked at the documentation for SampleModel, DataBuffer, and Raster and didn't find anything that looked like it would give me the scanline stride. It isn't critical as I think I will use INT_BGR anyway.

Thanks,
Scott

Chris Campbell

Hi Scott,

The scanlineStride property isn't defined on the base SampleModel
class, but it is on ComponentSampleModel (which is the SM used by
TYPE_3BYTE_BGR):
http://download.java.net/jdk6/docs/api/java/awt/image/
ComponentSampleModel.html

Thanks,
Chris

On May 11, 2006, at 11:01 AM, java2d@javadesktop.org wrote:
> I looked at the documentation for SampleModel, DataBuffer, and
> Raster and didn't find anything that looked like it would give me
> the scanline stride. It isn't critical as I think I will use
> INT_BGR anyway.
>
>
> Thanks,
> Scott
> [Message sent by forum member 'swpalmer' (swpalmer)]
>
> http://forums.java.net/jive/thread.jspa?messageID=112847
>
> ======================================================================
> =====
> To unsubscribe, send email to listserv@java.sun.com and include in
> the body
> of the message "signoff JAVA2D-INTEREST". For general help, send
> email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".