@ -43,6 +43,7 @@ import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel ;
import java.awt.image.DataBuffer ;
import java.awt.image.DataBufferByte ;
import java.awt.image.DataBufferUShort ;
import java.awt.image.DirectColorModel ;
import java.io.IOException ;
import java.io.InputStream ;
@ -65,12 +66,15 @@ public class AWTLoader implements AssetLoader {
Transparency . BITMASK ,
DataBuffer . TYPE_BYTE ) ;
private byte [ ] extractImageData ( BufferedImage img ) {
private Object extractImageData ( BufferedImage img ) {
DataBuffer buf = img . getRaster ( ) . getDataBuffer ( ) ;
switch ( buf . getDataType ( ) ) {
case DataBuffer . TYPE_BYTE :
DataBufferByte byteBuf = ( DataBufferByte ) buf ;
return byteBuf . getData ( ) ;
case DataBuffer . TYPE_USHORT :
DataBufferUShort shortBuf = ( DataBufferUShort ) buf ;
return shortBuf . getData ( ) ;
}
return null ;
}
@ -87,25 +91,55 @@ public class AWTLoader implements AssetLoader {
}
}
private void flipImage ( short [ ] img , int width , int height , int bpp ) {
int scSz = ( width * bpp ) / 8 ;
scSz / = 2 ; // Because shorts are 2 bytes
byte [ ] sln = new byte [ scSz ] ;
int y2 = 0 ;
for ( int y1 = 0 ; y1 < height / 2 ; y1 + + ) {
y2 = height - y1 - 1 ;
System . arraycopy ( img , y1 * scSz , sln , 0 , scSz ) ;
System . arraycopy ( img , y2 * scSz , img , y1 * scSz , scSz ) ;
System . arraycopy ( sln , 0 , img , y2 * scSz , scSz ) ;
}
}
public Image load ( BufferedImage img , boolean flipY ) {
int width = img . getWidth ( ) ;
int height = img . getHeight ( ) ;
switch ( img . getType ( ) ) {
case BufferedImage . TYPE_4BYTE_ABGR : // most common in PNG images w/ alpha
byte [ ] dataBuf1 = ( byte [ ] ) extractImageData ( img ) ;
if ( flipY )
flipImage ( dataBuf1 , width , height , 32 ) ;
ByteBuffer data1 = BufferUtils . createByteBuffer ( img . getWidth ( ) * img . getHeight ( ) * 4 ) ;
data1 . put ( dataBuf1 ) ;
return new Image ( Format . ABGR8 , width , height , data1 ) ;
case BufferedImage . TYPE_3BYTE_BGR : // most common in JPEG images
byte [ ] dataBuf = extractImageData ( img ) ;
byte [ ] dataBuf2 = ( byte [ ] ) extractImageData ( img ) ;
if ( flipY )
flipImage ( dataBuf , width , height , 24 ) ;
ByteBuffer data = BufferUtils . createByteBuffer ( img . getWidth ( ) * img . getHeight ( ) * 3 ) ;
data . put ( dataBuf ) ;
return new Image ( Format . BGR8 , width , height , data ) ;
flipImage ( dataBuf2 , width , height , 24 ) ;
ByteBuffer data2 = BufferUtils . createByteBuffer ( img . getWidth ( ) * img . getHeight ( ) * 3 ) ;
data2 . put ( dataBuf2 ) ;
return new Image ( Format . BGR8 , width , height , data2 ) ;
case BufferedImage . TYPE_BYTE_GRAY : // grayscale fonts
byte [ ] dataBuf2 = extractImageData ( img ) ;
byte [ ] dataBuf3 = ( byte [ ] ) extractImageData ( img ) ;
if ( flipY )
flipImage ( dataBuf2 , width , height , 8 ) ;
ByteBuffer data2 = BufferUtils . createByteBuffer ( img . getWidth ( ) * img . getHeight ( ) ) ;
data2 . put ( dataBuf2 ) ;
return new Image ( Format . Luminance8 , width , height , data2 ) ;
flipImage ( dataBuf3 , width , height , 8 ) ;
ByteBuffer data3 = BufferUtils . createByteBuffer ( img . getWidth ( ) * img . getHeight ( ) ) ;
data3 . put ( dataBuf3 ) ;
return new Image ( Format . Luminance8 , width , height , data3 ) ;
case BufferedImage . TYPE_USHORT_GRAY : // grayscale heightmap
short [ ] dataBuf4 = ( short [ ] ) extractImageData ( img ) ;
if ( flipY )
flipImage ( dataBuf4 , width , height , 16 ) ;
ByteBuffer data4 = BufferUtils . createByteBuffer ( img . getWidth ( ) * img . getHeight ( ) * 2 ) ;
data4 . asShortBuffer ( ) . put ( dataBuf4 ) ;
return new Image ( Format . Luminance16 , width , height , data4 ) ;
default :
break ;
}
@ -163,11 +197,18 @@ public class AWTLoader implements AssetLoader {
public Object load ( AssetInfo info ) throws IOException {
if ( ImageIO . getImageWritersBySuffix ( info . getKey ( ) . getExtension ( ) ) ! = null ) {
InputStream in = info . openStream ( ) ;
boolean flip = ( ( TextureKey ) info . getKey ( ) ) . isFlipY ( ) ;
InputStream in = null ;
try {
in = info . openStream ( ) ;
Image img = load ( in , flip ) ;
in . close ( ) ;
return img ;
} finally {
if ( in ! = null ) {
in . close ( ) ;
}
}
}
return null ;
}