Skip to main content

RE: [JAVA2D] converting Java Image to Mac OS native image

1 reply [Last post]

Seems like nobody has answered this (or I missed it) so I'll give
you my thoughts. I'm not that familiar with images in Cocoa, it's
been a while since i wrote any Cocoa code. But without knowing the
API for, say, NSImage, I'll tell you what i can guess.

The first thing I'll say is, in my opinion, you do NOT want to deal
with images at the JPEG or GIF or format level. I would get a hold
of a RenderedImage (or BufferedImage) and go from there, that way
you don't care about the source format.

The fundamental pieces of a RenderedImage are its SampleModel, its
ColorModel, and its Rasters -- one Raster if the image is not tiled,
which for normal-sized images is probably the case.

You'll have to figure out the NSImage API and see if it makes sense
to subclass it, or to construct one with data you get from Java.
The SampleModel and ColorModel just consist of some parameters like
number of bands, bits per pixel etc. Those things you should be able
to convert into whatever an NSImage needs.

The last piece is the Raster, which itself consists of a DataBuffer
and a SampleModel. The SampleModel usually has the same values as
the one you get from the RenderedImage, so the only item of note is
the DataBuffer. And when you decompose this, it's really just an
array of whatever data type your image is (typically byte[] unless
you have packed pixels or 16-bit data).

So the fundamental part of passing your image from Java to JNI/native
code is passing the data in the DataBuffer, along with whatever
properties you need to build the NSImage. If you want to process
tiled images, you may want to do things slightly differently, since
you don't want to load all the tiles into the heap at once.

Hope this helps,


> -----Original Message-----
> From: Discussion list for Java 2D API
> [mailto:JAVA2D-INTEREST@JAVA.SUN.COM] On Behalf Of Rob Ross
> Sent: Wednesday, September 20, 2006 3:42 PM
> Subject: [JAVA2D] converting Java Image to Mac OS native image
> Hi. Long shot here, but having no luck finding sample code...
> I'm writing some JNI code that will run on Mac OS X. I need
> to call a native method and pass it a Java image object, and
> be able to convert that to a Mac OS X native image type, like
> an NSImage.
> I'm pretty sure that the Java2D team knows how to do for
> Windows, since it must happen at some point in your Java 2D
> Windows native/ implementation code.
> Maybe I just need to be able to pass the Java image data as a
> byte[] in some well-known byte structure that represents a
> JPEG or GIF, etc, and I can convert it from there.
> Any pointers?
> Thanks,
> Rob Ross, Lead Software Engineer
> E! Networks
> ---------------------------------------------------
> "Beware of he who would deny you access to information, for
> in his heart he dreams himself your master." -- Commissioner
> Pravin Lal
> ==============================================================
> =============
> To unsubscribe, send email to and
> include in the body of the message "signoff JAVA2D-INTEREST".
> For general help, send email to and
> include in the body of the message "help".

To unsubscribe, send email to and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to and include in the body of the message "help".

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-06-13

Thanks for answering, even late. Interestingly, your post was the most helpful of any other responses I received on this subject, even though you didn't offer any specific example code. In this case the basic concepts were more important.

I had to do some pretty low-level debugging to get this to work. I created a BufferedImage in Java, painted my Icon into it, and output the pixel Raster as a grid of hex values, and then got all the meta data I could from that image. Then I did the same for the same image file, opened from a Cocoa app. I was eventually able to figure out what all the parameter values should be by comparing the two Rasters and metadata in memory.

I had to use a "weird" BufferedImage type of TYPE_3BYTE_BGR, then swap the B & R bytes from the Raster since the Cocoa libraries wants them in RGB. I couldn't get TYPE_INT_ARGB to work. I don't think Cocoa can deal with the alpha values in the same Raster, but I was tired of debugging this problem so I haven't followed up on that. For now I can pass Icon images to Cocoa via JNI and create a native NSBitmapImageRep, so I am somewhat relieved that this finally works.

Again, thank you for the help.

Rob Ross