Java2D: not possible to set 32bpp ints?

T

Timo Nentwig

Hi!

It seems that it's not possible to use 32bpp ARGB encoded integers in
Java2D, at least I didn't find such a method in the WriteableRaster API.

Is there same different way to do so?

Timo
 
T

Thomas Weidenfeller

Timo said:
It seems that it's not possible to use 32bpp ARGB encoded integers in
Java2D, at least I didn't find such a method in the WriteableRaster API.

This might do (I didn't try):

Using integers:

Raster.createPackedRaster(
DataBuffer.TYPE_INT, // data type
w, h, // image width, height
4, // bands
8, // bits per band
null); // use default (0,0) location
Using bytes:

Raster.createInterleavedRaster(
DataBuffer.TYPE_BYTE, // data type
w, h, // image width, height
w * 4, // scan line stride
4, // pixel stride
new int[]{3, 2, 1, 0}, // band offset
null); // use default (0, 0) location

/Thomas
 
T

Timo Nentwig

Thomas said:
This might do (I didn't try):

Using integers:

Raster.createPackedRaster(
DataBuffer.TYPE_INT, // data type
w, h, // image width, height
4, // bands
8, // bits per band
null); // use default (0,0) location

What I meant is that I want to encode all the channels in a single int:

int pixel = a << 24 | r << 16 | g << 8 | b;
raster.setPixel(x,y,pixel);


Java2D is incredibly slow :-( At least 5 times slower than a old-style
MemoryImageSource solution.
 
T

Thomas Weidenfeller

Timo said:
What I meant is that I want to encode all the channels in a single int:

That's why I suggested createPackedRaster. If I didn't mix up things, my
example would pack 4 bands (A, R, G, B) with 8 bits per band into
integers. Which should give you, if my math didn't fail me 4 * 8 = 32
bits per pixel, which just happen to fit nicely in one Java 32 bit integer.

/Thomas
 
T

Timo Nentwig

Thomas said:
That's why I suggested createPackedRaster. If I didn't mix up things, my
example would pack 4 bands (A, R, G, B) with 8 bits per band into
integers. Which should give you, if my math didn't fail me 4 * 8 = 32
bits per pixel, which just happen to fit nicely in one Java 32 bit integer.

And how do I set that one integer? There's no Raster.setPixel() that
accepts a single integer.
 
T

Thomas Weidenfeller

Timo said:
And how do I set that one integer? There's no Raster.setPixel() that
accepts a single integer.

If nothing else helps, drill down to the DataBuffer and insert the data
there.

/Thomas
 
H

hondacivic

Timo Nentwig said:
And how do I set that one integer? There's no Raster.setPixel() that
accepts a single integer.

Raster is general and doesn't know directly about any specific format.
getDataBuffer() still is generic.

If you know that the pixels are stored in ints, then you can cast the
DataBuffer that comes out of Raster's getDataBuffer() to
DataBufferInt.

DataBufferInt has a getData(), that returns int[], at which point you
can access the pixels directly.

How about starting with a BufferedImage, which wraps all the
colorspace/format issues all in one package:
BufferedImage image = new BufferedImage(640, 480,
BufferedImage.TYPE_INT_ARGB);
int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
for (int y = 0; y < 480; y++)
for (int x = 0; x < 640; x++)
pixels[y * 640 + x] = x * y;
....
I'm not sure, but the compiler has some nifty optimization features,
where something like this may actually not be any faster:
for (int y = 0; y < 480; y++)
{
int offset = y * 640;
for (int x = 0; x < 640; x++)
pixels[offset + x] = x * y;
}

On an exciting side note, Java's bytecode to machine code translator
is efficient enough that it can compete and sometimes outperform
optimized C++ code! So raster-hacking in Java can be far more
exciting as many people with presumptions about Java are willing to
admit. Discover for yourself.

Also, Java2D is one of *THE* cleanest, if not *THE* cleanest graphics
library ever written. You get a lot of flexibility, performance,
extendability in an amazing package. By the looks of it, it's better
than GDI+, and most definitely way better than GNU land's GTK/GTK++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top