关于PaletteData的生成:
case Gdip.PixelFormat16bppARGB1555:
case Gdip.PixelFormat16bppRGB555:
paletteData = new PaletteData(0x7C00, 0x3E0, 0x1F);
break;
case Gdip.PixelFormat16bppRGB565:
paletteData = new PaletteData(0xF800, 0x7E0, 0x1F);
break;
case Gdip.PixelFormat24bppRGB:
paletteData = new PaletteData(0xFF, 0xFF00, 0xFF0000);
break;
case Gdip.PixelFormat32bppRGB:
case Gdip.PixelFormat32bppARGB:
paletteData = new PaletteData(0xFF00, 0xFF0000, 0xFF000000);
break;
32位ImageData中的data是以RGBA的顺序存储的。data[0]:red,data[1]:green,data[2]:blue,data[3]:alpha
从byte[]中读取RGB pixel:
public static int getPixelFromRGBA( int depth, byte[] data )
{
switch ( depth )
{
case 32 :
return ( ( data[0] & 0xFF ) << 24 )
+ ( ( data[1] & 0xFF ) << 16 )
+ ( ( data[2] & 0xFF ) << 8 )
+ ( data[3] & 0xFF );
case 24 :
return ( ( data[0] & 0xFF ) << 16 )
+ ( ( data[1] & 0xFF ) << 8 )
+ ( data[2] & 0xFF );
case 16 :
return ( ( data[1] & 0xFF ) << 8 ) + ( data[0] & 0xFF );
case 8 :
return data[0] & 0xFF;
}
SWT.error( SWT.ERROR_UNSUPPORTED_DEPTH );
return 0;
}
从pixel中取出RGB值:
RGB rgb = imagedata.palette.getRGB( pixel );
生成一个空的32位图片:
ImageData dest = new ImageData( width,
height,
32,
new PaletteData( 0xFF00, 0xFF0000, 0xFF000000 ) );
24位透明图片转成32位透明图片:
public static ImageData convertToRGBA( ImageData src )
{
ImageData dest = new ImageData( src.width,
src.height,
32,
new PaletteData( 0xFF00, 0xFF0000, 0xFF000000 ) );
for ( int x = 0; x < src.width; x++ )
{
for ( int y = 0; y < src.height; y++ )
{
int pixel = src.getPixel( x, y );
RGB rgb = src.palette.getRGB( pixel );
byte[] rgba = new byte[4];
rgba[0] = (byte) rgb.red;
rgba[1] = (byte) rgb.green;
rgba[2] = (byte) rgb.blue;
if ( pixel == src.transparentPixel )
{
rgba[3] = (byte) ( 0 );
}
else
{
rgba[3] = (byte) ( 255 );
}
dest.setPixel( x, y, getPixelFromRGBA( 32, rgba ) );
}
}
return dest;
}