Skip to main content

[JAVA2D] How to create a BufferedImage from a transposed array of bytes

1 reply [Last post]
Anonymous

Hello everybody !

I need to display and manipulate big 2D arrays of bytes. The problem is
the order of the elements: the fastest axis is the vertical one. For
example if the array is of size n1*n2, the horizontal axis is of size
n2, the vertical one is of size n1 then the element at (x,y) will be
x*n1 + y (x is the horizontal direction).

The question is how to create a BufferedImage from this array, without
duplicating the data and oriented the right way (so I don't have to make
any transformation when I display it).

So far I transpose the array so the fastest axis is the horizontal one
and then I use something like this to get a BufferedImage:

//data is a byte[n2*n1] - the transposed version of the original
WritableRaster wr = Raster
.createInterleavedRaster( new DataBufferByte(data, data.length),
n2, n1, n2, 1, new int[]{0}, null );

//colorModel is a 256 colors IndexColorModel
BufferedImage img = new BufferedImage(colorModel, wr, false, null);

Does anyone know how to avoid the transposition? What is the most
efficient way to deal with this problem (low memory footprint and fast
display). Do I have to provide a SampleModel that knows how to deal with
the transposed matrix?

Thank you,
-Iulian

===========================================================================
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".

Reply viewing options

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

> I need to display and manipulate big 2D arrays of bytes. The problem
is
> the order of the elements: the fastest axis is the vertical one. For
> example if the array is of size n1*n2, the horizontal axis is of size
> n2, the vertical one is of size n1 then the element at (x,y) will be
> x*n1 + y (x is the horizontal direction).
>
> Does anyone know how to avoid the transposition? What is the most
> efficient way to deal with this problem (low memory footprint and fast
> display). Do I have to provide a SampleModel that knows how to deal
> with the transposed matrix?

A SampleModel would be one way to do it, but it would probably be
better to just create the image with the array as you have it and then
draw it with an AffineTransform of (0, 1, 1, 0, 0, 0) which specifies
the reverse transposition.

That may not be faster on some configurations that use software to
transform images when compared to your dedicated transposition loops,
but it will probably be faster as we start accelerating image
transforms. Right now the "disabled by default" OpenGL pipeline in 1.5
is the only one that accelerates image transforms...

...jim

===========================================================================
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".