0

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.

3 Answers3

0

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.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
michael
  • 1,160
  • 7
  • 19
0

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
  • 8,905
  • 3
  • 24
  • 38
0

Theo, thanks for your effort. I think the problem is the functions takes too long to run. After I try your script, a warning messages come out. It said the program 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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
michael
  • 1,160
  • 7
  • 19