views:

566

answers:

4

The purpose of the function is to covert the background to transparent and then return the bitmapdata, but it seems it do not work. The code is following:

 private function transparentConverter( source:BitmapData, threshold:Number = 0.1 ):BitmapData
  {
   var bitmapData:BitmapData = new BitmapData( source.width, source.height, true, 0x00000000  );
   bitmapData.lock();
   var color:uint = source.getPixel( 0, 0 );

   var x:uint, y:uint;
   for ( y = 0; y < source.height; y++ )
   {
    for ( x = 0; x < source.width; x++ )
    {
     var c1:uint = source.getPixel( x, y );
     var c2:uint = color;
     var rx:uint = Math.abs((( c1 & 0xff0000 ) >> 16 ) - (( c2 & 0xff0000 ) >> 16 ));
     var gx:uint = Math.abs((( c1 & 0xff00) >> 8 ) - (( c2 & 0xff00 ) >> 8 ));
     var bx:uint = Math.abs(( c1 & 0xff ) - ( c2 & 0xff ));

     var dist:uint = Math.sqrt( rx*rx + gx*gx + bx*bx );

     if ( dist <= threshold )
     {
      bitmapData.setPixel32( x, y, 0x00000000 );
     }else
      {
       bitmapData.setPixel32( x, y, c1 );
      }
    }
   }
   bitmapData.unlock();
   return bitmapData;
  }

Please advice.

A: 

answered here already :

http://stackoverflow.com/questions/608087/flex-actionscript-white-to-transparent/608520#608520

Theo.T
Thanks Theo, that is the place I got the code and I tried to modify it. But, after I modify it, it seems it do not work.
oh sorry. have you tried to simply set the threshold to 0xFF or something for testing, 0.1 meaning the color is exactly matching. Also trace() the getPixel(0,0) to make sure it has the expected value.
Theo.T
Additionally I don't think your example would work if the source is set to transparent (just in case it is)
Theo.T
Thank Theo, I will try threshold to 0xFF to test it. Actually, the source is a bitmapdata, if the 0.1 is exactly, it should become a transparent bitmapdata, I think. However, it seems nothing happen.
The getPixel(0,0) return 16777215, that is the white color and that is correct in my case.
The threshold set to 0xFF has the same result, nothing happened.
Strange, the function looks all good. I suppose you add the return value to a Bitmap ? i.e. bitmap.bitmapData = transparentConverter(...);
Theo.T
A: 

Hi, Theo, the function seems can run, but take some time, however no bitmapdata return.

I receive the bitmpadata by the following code:

bitmapData = transparentConverter( bitmapData );

var bitmap:Bitmap = new Bitmap( bitmapData );

image1.source = bitmap;

The image do not appear.

And also, I can trace( c1 ), and I get a long list of data. Thanks for your response.

michael
A: 

Can you try this :

// This bitmapData should be defined for real, wherever you get that from ...
var source:BitmapData;  

if(source == null)
   trace("The source cannot be empty");

// Here you get the transformed BitmapData
var destination:bitmapData = transparentConverter( source );

// You apply it to a Bitmap in order to visualize it
var viewBitmap:Bitmap = new Bitmap(destination);

// You add the Bitmap to the stage so you can see it
addChild(viewBitmap);
Theo.T
A: 

Theo, thanks for your effeort. I think the problem is the fuctions takes too logn to run. After I try your script, a warning messages come out. It said the progam takes more than 15 seconds to run and told me to stop running.

I think the function should be OK, but, maybe, not quite practical.

Theo, thank for your time and advice again.

I think the question can be closed.

michael