`

[转]Flex 处理bmp图片as

 
阅读更多
http://bbs.9ria.com/viewthread.php?tid=96796&extra=page%3D1%26amp%3Borderby%3Ddateline%26amp%3Bfilter%3D2592000



分享下处理bmp图片的as, 网上看到的拿来了
package hy.com
{
                import flash.display.BitmapData;
                import flash.errors.IOError;
                import flash.utils.ByteArray;
                import flash.utils.Endian;
               
                public class BMPDecoder {
                        //___________________________________________________________ const
                        private const BITMAP_HEADER_TYPE:String = "BM";
                        private const BITMAP_FILE_HEADER_SIZE:int = 14;
                        private const BITMAP_CORE_HEADER_SIZE:int = 12;
                        private const BITMAP_INFO_HEADER_SIZE:int = 40;
                        private const COMP_RGB      :int = 0;
                        private const COMP_RLE8     :int = 1;
                        private const COMP_RLE4     :int = 2;
                        private const COMP_BITFIELDS:int = 3;
                        private const BIT1 :int = 1;
                        private const BIT4 :int = 4;
                        private const BIT8 :int = 8;
                        private const BIT16:int = 16;
                        private const BIT24:int = 24;
                        private const BIT32:int = 32;
                        //___________________________________________________________ vars
                        private var bytes:ByteArray;
                        private var palette:Array;
                        private var bd:BitmapData;
                        private var nFileSize:uint;
                        private var nReserved1:uint;
                        private var nReserved2:uint;
                        private var nOffbits:uint;
                        private var nInfoSize:uint;
                        private var nWidth:int;
                        private var nHeight:int;
                        private var nPlains:uint;
                        private var nBitsPerPixel:uint;
                        private var nCompression:uint;
                        private var nSizeImage:uint;
                        private var nXPixPerMeter:int;
                        private var nYPixPerMeter:int;
                        private var nColorUsed:uint;
                        private var nColorImportant:uint;
                        private var nRMask:uint;
                        private var nGMask:uint;
                        private var nBMask:uint;
                        private var nRPos:uint;
                        private var nGPos:uint;
                        private var nBPos:uint;
                        private var nRMax:uint;
                        private var nGMax:uint;
                        private var nBMax:uint;
                       
                public function BMPDecoder(){
                        nRPos = 0;
                        nGPos = 0;
                        nBPos = 0;
                }
               
                /**
                 * decoder
                 *
                 * @param BMP file ByteArray
                 */
                public function decode( data:ByteArray ):BitmapData {
                        bytes = data;
                        bytes.endian = Endian.LITTLE_ENDIAN;
                        bytes.position = 0;
                        readFileHeader();
                        nInfoSize = bytes.readUnsignedInt();
                        switch ( nInfoSize ) {
                                case BITMAP_CORE_HEADER_SIZE:
                                        readCoreHeader();
                                        break;
                                case BITMAP_INFO_HEADER_SIZE:
                                        readInfoHeader();
                                        break;
                                default:
                                        readExtendedInfoHeader();
                                        break;
                        }
                        bd = new BitmapData( nWidth, nHeight );
                        switch ( nBitsPerPixel ){
                                case BIT1:
                                        readColorPalette();
                                        decode1BitBMP();
                                        break;
                                case BIT4:
                                        readColorPalette();
                                        if ( nCompression == COMP_RLE4 ){
                                                decode4bitRLE();
                                        } else {
                                                decode4BitBMP();
                                        }
                                        break;
                                case BIT8:
                                        readColorPalette();
                                        if ( nCompression == COMP_RLE8 ){
                                                decode8BitRLE();
                                        } else {
                                                decode8BitBMP();
                                        }
                                        break;
                                case BIT16:
                                        readBitFields();
                                        checkColorMask();
                                        decode16BitBMP();
                                        break;
                                case BIT24:
                                        decode24BitBMP();
                                        break;
                                case BIT32:
                                        readBitFields();
                                        checkColorMask();
                                        decode32BitBMP();
                                        break;
                                default:
                                        throw new VerifyError("invalid bits per pixel : " + nBitsPerPixel );
                        }
                        return bd;
                }
                /**
                 * read BITMAP FILE HEADER
                 */
                private function readFileHeader():void {
                        var fileHeader:ByteArray = new ByteArray();
                        fileHeader.endian = Endian.LITTLE_ENDIAN;
                        try {
                                bytes.readBytes( fileHeader, 0, BITMAP_FILE_HEADER_SIZE );
                                if ( fileHeader.readUTFBytes( 2 ) != BITMAP_HEADER_TYPE ){
                                        throw new VerifyError("invalid bitmap header type");
                                }
                                nFileSize  = fileHeader.readUnsignedInt();
                                nReserved1 = fileHeader.readUnsignedShort();
                                nReserved2 = fileHeader.readUnsignedShort();
                                nOffbits   = fileHeader.readUnsignedInt();
                        } catch ( e:IOError ) {
                                throw new VerifyError("invalid file header");
                        }
                }
                /**
                 * read BITMAP CORE HEADER
                 */
                private function readCoreHeader():void {
                        var coreHeader:ByteArray = new ByteArray();
                        coreHeader.endian = Endian.LITTLE_ENDIAN;
                        try {
                                bytes.readBytes( coreHeader, 0, BITMAP_CORE_HEADER_SIZE - 4 );
                                nWidth  = coreHeader.readShort();
                                nHeight = coreHeader.readShort();
                                nPlains = coreHeader.readUnsignedShort();
                                nBitsPerPixel = coreHeader.readUnsignedShort();
                        } catch ( e:IOError ) {
                                throw new VerifyError("invalid core header");
                        }
                }
                /**
                 * read BITMAP INFO HEADER
                 */
                private function readInfoHeader():void {
                        var infoHeader:ByteArray = new ByteArray();
                        infoHeader.endian = Endian.LITTLE_ENDIAN;
                        try {
                                bytes.readBytes( infoHeader, 0, BITMAP_INFO_HEADER_SIZE - 4 );
                                nWidth  = infoHeader.readInt();
                                nHeight = infoHeader.readInt();
                                nPlains = infoHeader.readUnsignedShort();
                                nBitsPerPixel = infoHeader.readUnsignedShort();
                                nCompression = infoHeader.readUnsignedInt();
                                nSizeImage = infoHeader.readUnsignedInt();
                                nXPixPerMeter = infoHeader.readInt();
                                nYPixPerMeter = infoHeader.readInt();
                                nColorUsed = infoHeader.readUnsignedInt();
                                nColorImportant = infoHeader.readUnsignedInt();
                        } catch ( e:IOError ) {
                                throw new VerifyError("invalid info header");
                        }
                }
                /**
                 * read the extend info of BITMAP INFO HEADER
                 */
                private function readExtendedInfoHeader():void {
                        var infoHeader:ByteArray = new ByteArray();
                        infoHeader.endian = Endian.LITTLE_ENDIAN;
                        try {
                                bytes.readBytes( infoHeader, 0, nInfoSize - 4 );
                                nWidth  = infoHeader.readInt();
                                nHeight = infoHeader.readInt();
                                nPlains = infoHeader.readUnsignedShort();
                                nBitsPerPixel = infoHeader.readUnsignedShort();
                                nCompression = infoHeader.readUnsignedInt();
                                nSizeImage = infoHeader.readUnsignedInt();
                                nXPixPerMeter = infoHeader.readInt();
                                nYPixPerMeter = infoHeader.readInt();
                                nColorUsed = infoHeader.readUnsignedInt();
                                nColorImportant = infoHeader.readUnsignedInt();
                                if ( infoHeader.bytesAvailable >= 4 ) nRMask = infoHeader.readUnsignedInt();
                                if ( infoHeader.bytesAvailable >= 4 ) nGMask = infoHeader.readUnsignedInt();
                                if ( infoHeader.bytesAvailable >= 4 ) nBMask = infoHeader.readUnsignedInt();
                        } catch ( e:IOError ) {
                                throw new VerifyError("invalid info header");
                        }
                }
                /**
                 * read bitfields
                 */
                private function readBitFields():void {
                        if ( nCompression == COMP_RGB ){
                                if ( nBitsPerPixel == BIT16 ){
                                        // RGB555
                                        nRMask = 0x00007c00;
                                        nGMask = 0x000003e0;
                                        nBMask = 0x0000001f;
                                } else {
                                        //RGB888;
                                        nRMask = 0x00ff0000;
                                        nGMask = 0x0000ff00;
                                        nBMask = 0x000000ff;
                                }
                        } else if ( ( nCompression == COMP_BITFIELDS ) && ( nInfoSize < 52 ) ){
                                try {
                                        nRMask = bytes.readUnsignedInt();
                                        nGMask = bytes.readUnsignedInt();
                                        nBMask = bytes.readUnsignedInt();
                                } catch ( e:IOError ) {
                                        throw new VerifyError("invalid bit fields");
                                }
                        }
                }
                /**
                 * read color palette
                 */
                private function readColorPalette():void {
                        var i:int;
                        var len:int = ( nColorUsed > 0 ) ? nColorUsed : Math.pow( 2, nBitsPerPixel );
                        palette = new Array( len );
                        for ( i = 0; i < len; ++i ){
                                palette[ i ] = bytes.readUnsignedInt();
                        }
                }
                /**
                 * decode 1 bit BMP
                 */
                private function decode1BitBMP():void {
                        var x:int;
                        var y:int;
                        var i:int;
                        var col:int;
                        var buf:ByteArray = new ByteArray();
                        var line:int = nWidth / 8;
                        if ( line % 4 > 0 ){
                                line = ( ( line / 4 | 0 ) + 1 ) * 4;
                        }
                        try {
                                for ( y = nHeight - 1; y >= 0; --y ){
                                        buf.length = 0;
                                        bytes.readBytes( buf, 0, line );
                                        for ( x = 0; x < nWidth; x += 8 ){
                                                col = buf.readUnsignedByte();
                                                for ( i = 0; i < 8; ++i ){
                                                        bd.setPixel( x + i, y, palette[ col >> ( 7 - i ) & 0x01 ] );
                                                }
                                        }
                                }
                        } catch ( e:IOError ) {
                                throw new VerifyError("invalid image data");
                        }
                }
                /**
                 * decode 4bit RLE
                 */
                private function decode4bitRLE():void {
                        var x:int;
                        var y:int;
                        var i:int;
                        var n:int;
                        var col:int;
                        var data:uint;
                        var buf:ByteArray = new ByteArray();
                        try {
                                for ( y = nHeight - 1; y >= 0; --y ){
                                        buf.length = 0;
                                        while ( bytes.bytesAvailable > 0 ){
                                                n = bytes.readUnsignedByte();
                                                if ( n > 0 ){
                                                        // encode data
                                                        data = bytes.readUnsignedByte();
                                                        for ( i = 0; i < n/2; ++i ){
                                                                buf.writeByte( data );
                                                        }
                                                } else {
                                                        n = bytes.readUnsignedByte();
                                                        if ( n > 0 ){
                                                                // abs mode
                                                                bytes.readBytes( buf, buf.length, n/2 );
                                                                buf.position += n/2;
                                                                if ( n/2 + 1 >> 1 << 1 != n/2 ){
                                                                        bytes.readUnsignedByte();
                                                                }
                                                        } else {
                                                                // EOL
                                                                break;
                                                        }
                                                }
                                        }
                                        buf.position = 0;
                                        for ( x = 0; x < nWidth; x += 2 ){
                                                col = buf.readUnsignedByte();
                                                bd.setPixel( x, y, palette[ col >> 4 ] );
                                                bd.setPixel( x + 1, y, palette[ col & 0x0f ] );
                                        }
                                }
                        } catch ( e:IOError ) {
                                throw new VerifyError("invalid image data");
                        }
                }
                /**
                 * decode 4bit (no Compression)
                 */
                private function decode4BitBMP():void {
                        var x:int;
                        var y:int;
                        var i:int;
                        var col:int;
                        var buf:ByteArray = new ByteArray();
                        var line:int = nWidth / 2;
                        if ( line % 4 > 0 ){
                                line = ( ( line / 4 | 0 ) + 1 ) * 4;
                        }
                        try {
                                for ( y = nHeight - 1; y >= 0; --y ){
                                        buf.length = 0;
                                        bytes.readBytes( buf, 0, line );
                                        for ( x = 0; x < nWidth; x += 2 ){
                                                col = buf.readUnsignedByte();
                                                bd.setPixel( x, y, palette[ col >> 4 ] );
                                                bd.setPixel( x + 1, y, palette[ col & 0x0f ] );
                                        }
                                }
                        } catch ( e:IOError ) {
                                throw new VerifyError("invalid image data");
                        }
                }
                /**
                 * decode 8bit( RLE Compression )
                 */
                private function decode8BitRLE():void {
                        var x:int;
                        var y:int;
                        var i:int;
                        var n:int;
                        var col:int;
                        var data:uint;
                        var buf:ByteArray = new ByteArray();
                        try {
                                for ( y = nHeight - 1; y >= 0; --y ){
                                        buf.length = 0;
                                        while ( bytes.bytesAvailable > 0 ){
                                                n = bytes.readUnsignedByte();
                                                if ( n > 0 ){
                                                        // encode data
                                                        data = bytes.readUnsignedByte();
                                                        for ( i = 0; i < n; ++i ){
                                                                buf.writeByte( data );
                                                        }
                                                } else {
                                                        n = bytes.readUnsignedByte();
                                                        if ( n > 0 ){
                                                                // abs mode data
                                                                bytes.readBytes( buf, buf.length, n );
                                                                buf.position += n;
                                                                if ( n + 1 >> 1 << 1 != n ){
                                                                        bytes.readUnsignedByte();
                                                                }
                                                        } else {
                                                                // EOL
                                                                break;
                                                        }
                                                }
                                        }
                                        buf.position = 0;
                                        for ( x = 0; x < nWidth; ++x ){
                                                bd.setPixel( x, y, palette[ buf.readUnsignedByte() ] );
                                        }
                                }
                        } catch ( e:IOError ) {
                                throw new VerifyError("invalid image data");
                        }
                }
                /**
                 * decode 8bit(no Compression)
                 */
                private function decode8BitBMP():void {
                        var x:int;
                        var y:int;
                        var i:int;
                        var col:int;
                        var buf:ByteArray = new ByteArray();
                        var line:int = nWidth;
                        if ( line % 4 > 0 ){
                                line = ( ( line / 4 | 0 ) + 1 ) * 4;
                        }
                        try {
                                for ( y = nHeight - 1; y >= 0; --y ){
                                        buf.length = 0;
                                        bytes.readBytes( buf, 0, line );
                                        for ( x = 0; x < nWidth; ++x ){
                                                bd.setPixel( x, y, palette[ buf.readUnsignedByte() ] );
                                        }
                                }
                        } catch ( e:IOError ) {
                                throw new VerifyError("invalid image data");
                        }
                }
                /**
                 * decode 16bit
                 */
                private function decode16BitBMP():void {
                        var x:int;
                        var y:int;
                        var col:int;
                        try {
                                for ( y = nHeight - 1; y >= 0; --y ){
                                        for ( x = 0; x < nWidth; ++x ){
                                                col = bytes.readUnsignedShort();
                                                bd.setPixel( x, y, ( ( ( col & nRMask ) >> nRPos )*0xff/nRMax << 16 ) + ( ( ( col & nGMask ) >> nGPos )*0xff/nGMax << 8 ) + ( ( ( col & nBMask ) >> nBPos )*0xff/nBMax << 0 ) );
                                        }
                                }
                        } catch ( e:IOError ) {
                                throw new VerifyError("invalid image data");
                        }
                }
                /**
                 * decode 24bit BMP
                 */
                private function decode24BitBMP():void {
                        var x:int;
                        var y:int;
                        var col:int;
                        var buf:ByteArray = new ByteArray();
                        var line:int = nWidth * 3;
                        if ( line % 4 > 0 ){
                                line = ( ( line / 4 | 0 ) + 1 ) * 4;
                        }
                        try {
                                for ( y = nHeight - 1; y >= 0; --y ){
                                        buf.length = 0;
                                        bytes.readBytes( buf, 0, line );
                                        for ( x = 0; x < nWidth; ++x ){
                                                bd.setPixel( x, y, buf.readUnsignedByte() + ( buf.readUnsignedByte() << 8 ) + ( buf.readUnsignedByte() << 16 ) );
                                        }
                                }
                        } catch ( e:IOError ) {
                                throw new VerifyError("invalid image data");
                        }
                }
                /**
                 * decode 32bit BMP
                 */
                private function decode32BitBMP():void {
                        var x:int;
                        var y:int;
                        var col:int;
                        try {
                                for ( y = nHeight - 1; y >= 0; --y ){
                                        for ( x = 0; x < nWidth; ++x ){
                                                col = bytes.readUnsignedInt();
                                                bd.setPixel( x, y, ( ( ( col & nRMask ) >> nRPos )*0xff/nRMax << 16 ) + ( ( ( col & nGMask ) >> nGPos )*0xff/nGMax << 8 ) + ( ( ( col & nBMask ) >> nBPos )*0xff/nBMax << 0 ) );
                                        }
                                }
                        } catch ( e:IOError ) {
                                throw new VerifyError("invalid image data");
                        }
                }
                /**
                 * check color mask
                 */
                private function checkColorMask():void {
                        if ( ( nRMask & nGMask ) | ( nGMask & nBMask ) | ( nBMask & nRMask ) ){
                                throw new VerifyError("invalid bit fields");
                        }
                        while ( ( ( nRMask >> nRPos ) & 0x00000001 ) == 0 ){
                                nRPos++;
                        }
                        while ( ( ( nGMask >> nGPos ) & 0x00000001 ) == 0 ){
                                nGPos++;
                        }
                        while ( ( ( nBMask >> nBPos ) & 0x00000001 ) == 0 ){
                                nBPos++;
                        }
                        nRMax = nRMask >> nRPos;
                        nGMax = nGMask >> nGPos;
                        nBMax = nBMask >> nBPos;
                }
                /**
                 * print information
                 */
                public function traceInfo():void {
                        trace("---- FILE HEADER ----");
                        trace("nFileSize: " + nFileSize );
                        trace("nReserved1: " + nReserved1 );
                        trace("nReserved2: " + nReserved2 );
                        trace("nOffbits: " + nOffbits );
                        trace("---- INFO HEADER ----");
                        trace("nWidth: " + nWidth );
                        trace("nHeight: " + nHeight );
                        trace("nPlains: " + nPlains );
                        trace("nBitsPerPixel: " + nBitsPerPixel );
                        if ( nInfoSize >= 40 ){
                                trace("nCompression: " + nCompression );
                                trace("nSizeImage: " + nSizeImage );
                                trace("nXPixPerMeter: " + nXPixPerMeter );
                                trace("nYPixPerMeter: " + nYPixPerMeter );
                                trace("nColorUsed: " + nColorUsed );
                                trace("nColorUsed: " + nColorImportant );
                        }
                        if ( nInfoSize >= 52 ){
                                trace("nRMask: " + nRMask.toString( 2 ) );
                                trace("nGMask: " + nGMask.toString( 2 ) );
                                trace("nBMask: " + nBMask.toString( 2 ) );
                        }
                }
        }
}
分享到:
评论

相关推荐

    FLEX中显示类型为BMP的图片

    然而,Flex的原生支持主要针对JPEG和PNG等更现代的图像格式,因此要显示BMP图片,我们需要自定义解码和渲染过程。 这篇名为“FLEX中显示类型为BMP的图片”的博客文章提供了如何在Flex应用中处理BMP图像的方法。首先...

    Flex相册 Flex图片

    Flex相册 Flex图片

    flex 两个图片播放小程序

    综上所述,"flex 两个图片播放小程序"利用了Flex的BitmapData、Image组件、事件处理和动画机制,创建了一个能够实现图片浏览、放大和缩小功能的应用。这样的小程序在用户体验、图像处理和性能优化方面都有一定的技术...

    FLex 左右滑动图片墙

    3. **ActionScript 3(AS3)编程**:AS3是Flex的主要编程语言,用于处理事件监听、图片加载、动画等逻辑。例如,我们可以添加事件监听器来响应用户的滑动操作,然后更新图片的位置。 4. **图片加载**:为了显示图片...

    flex+spring图片上传及预览

    在处理bmp图片时,由于bmp格式通常较大且不适用于网络传输,我们可能需要将其转换为更高效的格式,如JPEG或PNG。在Java中,我们可以使用ImageIO库进行图片格式转换。首先读取bmp图像,然后创建一个新的ImageWriter...

    as3 flex mxml调用.as文件例子

    在Flex项目中,MXML和AS3通常一起工作,MXML负责视图,而AS3处理业务逻辑和数据操作。 标题“as3 flex mxml调用.as文件例子”指出,我们要讨论的是如何在MXML文件中引用和使用AS3编写的类。这通常涉及到以下步骤: ...

    flex加载动态图片

    flexBulider页面加载动态图片的显示

    Flex4.6+java+servlet上传图片例子+图片上传预览

    Flex处理用户界面和文件选择,而Java Servlet处理文件的接收和存储,两者协同工作,为用户提供了一种安全且高效的图片上传体验。在实际应用中,这种架构可以被广泛应用于各种需要上传和处理图片的Web服务中。

    flex和as3之间互相调用的方法

    在Flex项目中,用户界面通常由MXML组件定义,而业务逻辑和数据处理则通过AS3代码实现。本文将深入探讨如何在Flex和AS3之间进行互相调用,以实现更高效的应用程序设计。 首先,我们了解Flex组件和AS3脚本的基本关系...

    flex实现对图片的旋转与拖动

    flex实现对图片的旋转与拖动,简单的小例子,希望对大家有所帮助

    flex 园 线 flex as3 园 线

    flex as3 园 线flex as3 园 线flex as3 园 线flex as3 园 线flex as3 园 线flex as3 园 线flex as3 园 线flex as3 园 线flex as3 园 线flex as3 园 线flex as3 园 线flex as3 园 线flex as3 园 线flex as3 园 线

    Flex(MXML+AS)实现的计算器

    AS,即ActionScript,是Flex应用程序的核心编程语言,主要用于处理逻辑和交互。在这个“Flex(MXML+AS)实现的计算器”项目中,开发者使用了MXML来设计计算器的界面,而ActionScript则用于实现计算器的各种数学运算...

    flex图表导出图片

    本篇文章将深入探讨如何在Flex中实现图表的图片导出功能。 首先,让我们了解Flex中的图表组件。Flex提供了一系列强大的图表组件,如BarChart、LineChart、PieChart等,这些组件基于Adobe Flex SDK,可以方便地创建...

    flex svg代码生成图片

    "Flex SVG 代码生成图片" 在 Flex 应用程序中,使用 SVG 资源可以实现图表生成图片并下载。以下是关于 Flex SVG 代码生成图片的知识点: 一、静态显示 SVG 图像 在 Flex 应用程序中,可以使用 SVG 资源,但只能...

    flex批量上传图片带预览功能

    在处理图片上传时,Flex的灵活性和强大的数据处理能力使其成为理想的工具。 批量上传图片是指用户可以一次性选择多个图片文件进行上传,而不是逐个选择。这种功能通常需要后端服务器的支持,以处理大量的文件传输和...

    Flex图片上传实例

    ### Flex图片上传实例详解 在本篇文章中,我们将深入探讨一个具体的Flex图片上传实例,并对其中涉及的关键技术点进行详细解读。此实例不仅提供了一个实际的上传图片的应用场景,而且还包含了一些实用的代码片段,这...

    图片压缩flex demo

    它涵盖了文件I/O、图片加载、图像处理和压缩算法等多个知识点,对于理解如何在Flex环境中处理图片资源非常有帮助。通过这个示例,开发者可以学习如何优化图片质量和大小,提高应用程序的性能和用户体验。

    FLEX 背景图片设置

    注意,我们使用了`BitmapAsset`类来加载嵌入的图片资源,这是AS3中处理嵌入图片的一种方法。 此外,你可能需要考虑背景图片的拉伸和裁剪问题。默认情况下,`&lt;s:Image&gt;`或`Image`对象会尝试保持原始图片的比例。如果...

    as3/flex4.6

    ### Flex4.6与AS3知识点详解 #### 一、Flex4.6简介与特性 **Flex4.6**作为一款强大的开源框架,主要用于构建跨平台的企业级应用程序、网页游戏、桌面应用及移动应用等。它基于ActionScript 3 (AS3) 开发,为开发者...

    flex鱼眼显示图片的例子

    这个"flex鱼眼显示图片的例子"是一个Flex项目,它展示了如何使用Flex技术实现一个鱼眼效果的图片显示功能。鱼眼效果通常指的是将图像扭曲成类似鱼眼镜头所拍摄的弧形视野,这种效果在视觉上往往能带来独特的体验。 ...

Global site tag (gtag.js) - Google Analytics