3 Replies Latest reply on Jan 10, 2009 10:27 AM by clbeech

    Help understanding BitmapData.threshold()

    Rothrock Level 5
      I've tried to use threshold() several times and it always seems to confound me. The latest attempt was that I was drawing a movieclip onto a bitmapdata and then I wanted to change it so that all non-transparent pixels changed to white (or black) and that everything that was transparent stayed transparent. After much mucking about I came up with this.

      bmp.threshold(bmp, new Rectangle(0, 0, 300,300), new Point(), "!=", 0x0, 0xffffffff, 0xff000000, true);

      But now I find mysel wanting to change it so that only 50% alpha or above goes white and everything else goes transparent, but I don't know what to do. I've read the help files and I know that it is:

      if( (pixel value & mask) operation (threshold & mask) ) change pixel to color

      But I can't ever figure out intuitively what those & operations are going to do. Does anybody have advice on how they have come to understand threshold?

      Or is threshold not really the tool I need? Do I want to use palletteMap instead? If so how have you come to understand palletteMap. Again it confounds me!
        • 1. Re: Help understanding BitmapData.threshold()
          clbeech Level 3
          hey RR - yeah I know what you mean, it seems that the docs are a bit muddled on this one (along with most of the rest of the examples in the bmpdata class ;) i think that much of the confusion is due to 'ordering' the params in the explanations differently than in the method call... go figure LOL!

          but I think i'd go with the threshold method here, seems like palletMap would be cumbersome (although I have yet to use it in anything) but I have used threshold recently to do something just like this and used:

          bmp.threshold(bmp, new Rectangle(0,0,w,h), new Point(0,0), '==', 0x00000000, 0xFFFFFFFF, 0xFF000000, false);

          so i think the thing to do is:

          bmp.threshold(bmp, new Rectangle(0,0,w,h), new Point(0,0), '<', 0x5F000000, 0xFFFFFFFF, 0xFF000000, false);

          (all this time working with hex, and i still don't understand how the values are represented - i really should look that up lol, so i 'think' that 0x5F000000 is 50% transparency not certain)

          i also 'think' (lol) that the thing with this method is (and it befuddles myself as well :) that the operator and the 'copySource' param are opposing, so that you can change the resulting action of the formula by changing either. above i used == and false - whereas you've used != and true - both should perform the same action, so it's always tough to keep the logic straight!
          • 2. Re: Help understanding BitmapData.threshold()
            Rothrock Level 5
            Thanks. I'm glad to know that I'm not alone in finding this confusing. But you helped me in a big way.

            I kept trying to figure out how to do the part where if it is this, change it to my specific color and if it isn't go to transparent (or what have you). I wasn't seeing any different witht he copySource parameter and it was because I was using threshold with the same source and desitnation bitmaps. So even if a pixel failed it was "copied" because it was already there. D'uh! So now I know I need two bitmaps.

            I'm still really confused about the values. Okay 50% alpha is 0x7Fxxxxxx and you can just compare the alpha values with a mask of 0xFF000000. And 25% alpha is 0x40xxxxxx. But then here is where I get confused...

            I made a square of 75% alpha red. So I would think the value of the pixel would be 0xC0FF0000, but it is reported (using getPixel32().toString(16)) as -3f010000 -- so the alpha component is -0x3f if you trace it out. But it still responds passes the comparison as if it was 0xCO.

            I also just tried it with 51% alpha as well. That should be 0x83, but getPixel32().toString(16) shows it as -0x7d.

            Well the little file I made to test some of this out helped me and it is good to know that it seems to work. Even if the traces confusing me more. Perhaps I should just have faith in it and not ask it to show me what it is doing!
            • 3. Re: Help understanding BitmapData.threshold()
              clbeech Level 3
              LMAO! no kidding! do we really 'need' to know...LOL! jk ( of course we do ;)

              dang - i should have mentioned that, in my original code I'd used two - I'd noticed that you were only using one bmp - so i threw it in there quick, since you 'can' use the same source image bmp, but of course, not in this type of case.

              I sure get confused by the hex value system - i guess it's time to learn what the hecks going on there LOL! but really interesting comparisons there that you've found.