Exporting BitmapData in hexa for file saving using swf2exe soft ( like mdm Zinc )

readed 6946 times

This class will help you to have a string in hexadecimal format to save it in a file. It creates the header of the file, the header of the bitmap and the data. This class require the Hexa class. Be sure you have it when compiling.

This class do its job, but I have to admit as soon as you are processing big bitmap, it's slow... very slow. This is due because of the string operation I'm doing to format it to a conform Hexa string.

So check it and give feedbacks if you find any way to improve it or if you have any request.

Actionscript:
  1. /*
  2. * Class written by Xavier MARTIN (xxlm or zeflasher)
  3. * http://dev.webbymx.net
  4. * http://www.webbymx.net
  5. * If you are using this class, I will be glad to receive a picture of you front of your city hall ( or wherever outside )
  6. * To do so please visit http://dev.webbymx.net and go in the about page to get my details...
  7. * Licence: http://creativecommons.org/licenses/by-nc-sa/2.0/fr/deed.en
  8. */
  9.  
  10. import flash.display.BitmapData;
  11. import net.webbymx.files.Hexa;
  12.  
  13.  
  14. class net.webbymx.files.binary.BitmapDataExporter {
  15. /* ****************************************************************************
  16. * PRIVATE VARIABLES
  17. **************************************************************************** */
  18.     private var _bmp          : BitmapData;
  19.     private var _nWidth         : Number;
  20.     private var _nHeight       : Number;
  21.  
  22.     private var _nRemainder  : Number;
  23.     private var _nCurrentStep   : Number;
  24.     private var _nStepLength    : Number;
  25.     private var _nTotalSteps    : Number;
  26.     private var _mcEnterFrame   : MovieClip;
  27.    
  28. /* ****************************************************************************
  29. * PUBLIC VARIABLES
  30. **************************************************************************** */
  31.     public var onProgress      : Function;
  32.     public var onExportDone  : Function;
  33.    
  34. /* ****************************************************************************
  35. * CONSTRUCTOR
  36. **************************************************************************** */
  37.     function BitmapDataExporter( b : BitmapData ) {
  38.         _init( b );
  39.     }
  40.    
  41.     private function _init( b : BitmapData ) : Void {
  42.         _bmp          = b;
  43.         _nWidth         = _bmp.width;
  44.         _nHeight       = _bmp.height;
  45.         _nCurrentStep   = 0;
  46.         _nRemainder     = _nWidth % 4;
  47.         _nTotalSteps    = Math.ceil( _nWidth * _nHeight / 900 );
  48.         _nStepLength    = Math.ceil( _nWidth * _nHeight / _nTotalSteps / 2 ) * 2;
  49.     }
  50.  
  51. /* ****************************************************************************
  52. * PRIVATE FUNCTIONS
  53. **************************************************************************** */
  54.  
  55.  
  56.  
  57. /**
  58. * Return a string of hexa for the file header
  59. * @param    Void
  60. * @return
  61. */
  62.     private function _fileHeader( Void ) : Void {
  63.         var bin : String = "";
  64.  
  65.  
  66.     /*
  67.     * bfType1 : Byte ; (* "B" *)
  68.     * bfType2 : Byte ; (* "M" *)
  69.     * bfSize : LongInt ; (* Size of File. Zero is acceptable *)
  70.     * bfReserved1 : Word ; (* Zero *)
  71.     * bfReserved2 : Word ; (* Zero *)
  72.     * bfOffBits : LongInt ; (* Offset to beginning of BitMap *)
  73.     */
  74.  
  75.  
  76.     //  writing the file type ( B and M == 42 and 4D )
  77.         bin = "424D";
  78.        
  79.     //  writing size ( longInt )
  80.         bin += "00000000";
  81.  
  82.     //  writing reserved, reserved ( word )
  83.         bin += "00000000";
  84.  
  85.     //  writing Offset to beginning of BitMap ( longInt )
  86.         bin += "36000000";
  87.  
  88.         onProgress( bin );
  89.     }
  90.    
  91.     private function _bitmapHeader( Void ) : Void {
  92.         var bin : String = "";
  93.        
  94.         var wh  : String = Hexa.getDWord( _nWidth.toString( 16 ) )
  95.         var hh  : String = Hexa.getDWord( _nHeight.toString( 16 ) )
  96.  
  97.     /*
  98.     * biSize : LongInt ; (* Number of Bytes in Structure *)
  99.     * biWidth : LongInt ; (* Width of BitMap in Pixels *)
  100.     * biHeight : LongInt ; (* Height of BitMap in Pixels *)
  101.     * biPlanes : Word ; (* Planes in target device = 1 *)
  102.     * biBitCount : Word ; (* Bits per Pixel 1, 4, 8, or 24 *)
  103.     * biCompression : LongInt ; (* BI_RGB = 0, BI_RLE8, BI_RLE4 *)
  104.     * biSizeImage : LongInt ; (* Size of Image Part (often ignored) *)
  105.     * biXPelsPerMeter : LongInt ; (* Always Zero *)
  106.     * biYPelsPerMeter : LongInt ; (* Always Zero *)
  107.     * biClrUsed : LongInt ; (* Number of Colors used in Palette *)
  108.     * biClrImportant : LongInt ; (* Number of Colors that are Important *)
  109.     */
  110.    
  111.    
  112.     //  writing Number of Bytes in Structure ( longInt )
  113.         bin = "28000000";
  114.        
  115.     //  writing width &   height
  116.         bin += wh + hh;
  117.        
  118.     //  writing planes
  119.         bin += "0100";
  120.        
  121.     //  writing bitcounts
  122.         bin += "1800";
  123.        
  124.     //  writing compression
  125.         bin += "00000000";
  126.  
  127.     //  writing Size Image
  128.         bin += "00000000";
  129.  
  130.     //  writing biXPelsPerMeter
  131.         bin += "00000000";
  132.        
  133.     //  writing biYPelsPerMeter
  134.         bin += "00000000";
  135.        
  136.     //  writing biClrUsed
  137.         bin += "00000000";
  138.        
  139.     //  writing biClrImportant
  140.         bin += "00000000";
  141.        
  142.         onProgress( bin );
  143.     }
  144.    
  145.    
  146.     private function _buildData( Void ) : Void {   
  147.     //  local access of the function -> speed improvement
  148.         var getString : Function = Hexa.getString;
  149.    
  150.     //  string where the hexa is saved
  151.         var row : String = "";
  152.    
  153.     //  The extracted pixel
  154.         var pix1    : Number;
  155.         var pix2    : Number;
  156.         var pix3    : Number;
  157.  
  158.     //  The max value to consider in the local for loop
  159.         var maxPix  : Number = Math.min( ( _nCurrentStep + 1 ) * _nStepLength, _nWidth * _nHeight);
  160.  
  161.  
  162.     //  writing data
  163.         for( var i : Number = _nCurrentStep * _nStepLength; i <maxPix; i += 2 ) {
  164.            
  165.         //  The pixel to examine
  166.             pix1 = _bmp.getPixel( i % _nWidth, ( _nHeight - 1 ) - Math.floor( i / _nWidth ) );
  167.             pix2 = _bmp.getPixel( (i + 1) % _nWidth, ( _nHeight - 1 ) - Math.floor( (i + 1) / _nWidth ) );
  168.  
  169.             if( i % _nWidth == 0 && i !=0  ) for ( var r : Number = 0; r <_nRemainder; ++r ) row = row + "00";
  170.             row += getString( pix1.toString( 16 ), 6 );
  171.             if( ( i + 1 ) % _nWidth == 0 && ( i + 1 )>= _nWidth ) for ( var r : Number = 0; r <_nRemainder; ++r ) row += "00";
  172.             row += getString( pix2.toString( 16 ), 6 );
  173.            
  174.         }
  175.            
  176.         onProgress( row, ( _nCurrentStep / _nTotalSteps * 100 ) );
  177.  
  178.     }
  179.    
  180. /* ****************************************************************************
  181. * PUBLIC STATIC FUNCTIONS
  182. **************************************************************************** */
  183. /**
  184. * Return the string of hexa to be written in a file or sent
  185. * @param    b
  186. * @param    p
  187. */
  188.     public function export( Void ) : Void { 
  189.        
  190.     //  reference to the instance
  191.         var owner : BitmapDataExporter = this;
  192.        
  193.     //  get the file header
  194.         _fileHeader();
  195.        
  196.     //  get the bitmap header
  197.         _bitmapHeader();
  198.        
  199.     //  create the mc holding the enter frame ( the player will not freeze )
  200.         _mcEnterFrame = _root.createEmptyMovieClip( "mc_BitmapDataExporter", 98765 )
  201.        
  202.     //  build the data onEnterFrame ( do not freeze the player )
  203.         _mcEnterFrame.onEnterFrame = function () {   
  204.         //  get the data
  205.             if ( owner._nCurrentStep <= owner._nTotalSteps ) {
  206.                 owner._buildData();
  207.                 owner._nCurrentStep++;
  208.             } else {
  209.                 owner.onExportDone();
  210.                 this.removeMovieClip()
  211.             }   
  212.         }
  213.     }
  214. }

Sources Download the BitmapDataExporter class Unknown

Here is a little example

Actionscript:
  1. var bmp : BitmapData = new BitmapData( 100, 100, false, 0xFF0000 );
  2. var bmpExport : BitmapDataExporter = new BitmapDataExporter( bmp );
  3.            
  4. mdm.FileSystem.BinaryFile.setData( "" );
  5. mdm.FileSystem.BinaryFile.writeData( mdm.Application.path + "test.bmp" );
  6.  
  7. bmpExport.onProgress = function ( b : String, percent : Number ) : Void {
  8.     mdm.FileSystem.BinaryFile.setData( b );
  9.     mdm.FileSystem.BinaryFile.appendData( mdm.Application.path + "test.bmp" );
  10.         trace( "percent done: " + percent.toString() );
  11. }
  12.        
  13. bmpExport.onExportDone = function ( Void ) : Void { 
  14.     trace( "export done" );
  15. }
  16.        
  17. bmpExport.export();

Hope it will help some of you...

Take care.

No related posts.

Rate it: 1 Star2 Stars3 Stars4 Stars5 Stars
(1 votes, average: 2.00 out of 5)

1 Comment(s)

  1. Pingback by barney.PE.kr - Flash Bitmap 이미지를 PNG로 저장하기 on August 3, 2008 12:08 pm

    [...] Exporting BitmapData in hexa for file saving using swf2exe soft ( like mdm Zinc ) [...]

Comments RSS TrackBack Identifier URI

Leave a comment


Dev by MX is proudly powered by WordPress and themed by Mukka-mu