Hmmm..

According to some very naive and simple tests, creating and drawing a BufferedImage is faster than creating and drawing an Image from a MemoryImageSource, at least on my Mac. The time creating the image from a preallocated int array is really neglible in both cases though. So I'd make some application-specific benchmarks before I would take that advice for fact. The "new" (compared to ImageSource) BufferedImage API is also more likely to use a native code/HW accelerated code path from what I understand.

The difference in steps is't that great. In the sample code below, it's 6 vs 7 lines of code to create the image (naive way). But yes, the extra flexibility you get from BufferedImage/Raster probably comes with some cost when it comes to complexity (or maybe just learning curve).

                int w;
                int h;
                int[] rgb = new int[w * h];

                // ... create some fancy image directly to the byte array ...

DirectColorModel cm = (DirectColorModel) ColorModel.getRGBdefault();

                // MemorImageSource vesion
MemoryImageSource source = new MemoryImageSource(w, h, cm, rgb, 0, w, null); Image image = Toolkit.getDefaultToolkit().createImage(source);

                // BufferedImage version
                DataBufferInt buffer = new DataBufferInt(rgb, w * h);
WritableRaster raster = Raster.createPackedRaster(buffer, w, h, w, cm.getMasks(), null); image = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);

To replace a region of the image, you would probably create a new Raster for your subregion and use raster.setRect(x, y, subregion). But if you really like, you could simply manipulate the rgb int array directly. Not sure that's much faster though.


Best regards,

--
Harald K



On 6. juli. 2008, at 06.28, Ken Warner wrote:

The absolute fastest way is to first stay away from BufferedImage.

Use MemoryImageSource. It's real easy to use -- as opposed to BufferedImage which nobody really understands and takes 10 steps to do one simple thing.

Make an image using MemoryImageSource then use BufferStrategy (not BufferedImage)
to render to the screen.

[EMAIL PROTECTED] wrote:
I have an int array where each element represents the RGB value for a pixel and I need to push that data into an image buffer (which is preferably a VolatileImage but may be a BufferedImage if using a VI for this is not possible) such that a subregion of the image buffer takes on the pixels in the int array. What's the best way to achieve this in the absolute fastest possible way?

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

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

Reply via email to