Feeling

    三人行,必有我师焉

   ::  :: 新随笔 :: 联系 ::  :: 管理 ::
  185 随笔 :: 0 文章 :: 392 评论 :: 0 Trackbacks
关于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;
    }

posted on 2012-01-15 13:49 三人行,必有我师焉 阅读(3540) 评论(1)  编辑  收藏

评论

# re: SWT ImageData的一些小结[未登录] 2013-03-16 20:51
用 SWT 怎么创建一个 空的 透明的图片?  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航:
 
GitHub |  开源中国社区 |  maven仓库 |  文件格式转换