color conversion

i want to convert a color as an argb int to an color as an unsigned short 565. how dow i do this. it has to be fast.

Do you mean you need it pixel by pixel or you have an image of intargb format
and you want to convert it? If it’s the latter, just create an image of type
ushort 565, and copy one image into another.
You might want to set alpha compositing to Src to speed up the conversion.

Something like this should work:


dstBI = new BufferedImage(srcBI.getWidth(), srcBI.getHeight(), BufferedImage.TYPE_USHORT_565_RGB);
Graphics2D g2d = (Graphics2D)dstBI.getGraphics();
g2d.setComposite(AlphaComposite.Src);
g2d.drawImage(srcBI, 0, 0, null);

Dmitri
Java2D Team

i want to convert a single int in ARGB format to a single short in 565ushort format. I cant seem to get it wrk right.

start with:


int argb = aaaaaaaaRRRRRrrrGGGGGGggBBBBBbbb;
short rgb565 = RRRRRGGGGGGBBBBB;

rgb565 = (short) (((argb >>> 8) & 0xF800)
    | ((argb >>> 5) & 0x07E0)
    | ((argb >>> 3) & 0x001F));