12 Replies Latest reply on Jun 13, 2006 4:57 PM by Chunick

    Question for you imaging gurus

    Level 7
      I have a bitmap member which I call a mask because it is layered on top of
      another image thereby 'masking' the underlying image (the mask is solid
      black). I have implemented a means where the user can cut 'holes' in the
      mask to reveal portions of the underlying image. The user presses and holds
      the mouse button and moves around the screen to define the hole they want to
      cut out of the mask. As they do so, I am recording points (the mouseLoc) and
      putting them into a list.

      When the user releases the mouse button I take this list of points and treat
      them like the vertices of a polygon. Even though the shape drawn by the user
      may look quite irregular, it is still in fact a polygon. I then test each
      and every point inside of the smallest rectangle that contains the polygon
      to see if it lies within the polygon using a well known algorithm. If the
      point is within the polygon, I use sexPixel to change it to white. This
      creates a hole in the mask. Since the sprite's ink is transparent, the
      underlying image shows through.

      All in all it works pretty well and I am happy with the results. However
      larger holes (or more complex, irregular shapes) take longer to change all
      of the pixels to white. This isn't surprising considering the somewhat brute
      force method employed. The initial recording of points isn't impacted by the
      size or shape of the hole, only the routine that loops through all of the
      points inside of the bounding rectangle.

      So my question is, does anyone have any ideas about how I might improve
      this? Thanks.

      Dave


        • 1. Re: Question for you imaging gurus
          Level 7
          I might create an algorithm that breaks the shape created by the user into
          the largest rectangles possible with only triangles left over. Then I would
          change the color not using setPixel but regular imaging lingo. If you must
          use setPixel to convert some odd shapes so be it but it is slow. Converting
          the rectangles first would probably speed things up quite a bit.

          --
          Craig Wollman
          Word of Mouth Productions

          phone 212 928 9581
          fax 212 928 9582
          159-00 Riverside Drive West #5H-70
          NY, NY 10032
          www.wordofmouthpros.com


          "Dave C" <private@email.com> wrote in message
          news:e6jrsm$10b$1@forums.macromedia.com...
          >I have a bitmap member which I call a mask because it is layered on top of
          > another image thereby 'masking' the underlying image (the mask is solid
          > black). I have implemented a means where the user can cut 'holes' in the
          > mask to reveal portions of the underlying image. The user presses and
          > holds
          > the mouse button and moves around the screen to define the hole they want
          > to
          > cut out of the mask. As they do so, I am recording points (the mouseLoc)
          > and
          > putting them into a list.
          >
          > When the user releases the mouse button I take this list of points and
          > treat
          > them like the vertices of a polygon. Even though the shape drawn by the
          > user
          > may look quite irregular, it is still in fact a polygon. I then test each
          > and every point inside of the smallest rectangle that contains the polygon
          > to see if it lies within the polygon using a well known algorithm. If the
          > point is within the polygon, I use sexPixel to change it to white. This
          > creates a hole in the mask. Since the sprite's ink is transparent, the
          > underlying image shows through.
          >
          > All in all it works pretty well and I am happy with the results. However
          > larger holes (or more complex, irregular shapes) take longer to change all
          > of the pixels to white. This isn't surprising considering the somewhat
          > brute
          > force method employed. The initial recording of points isn't impacted by
          > the
          > size or shape of the hole, only the routine that loops through all of the
          > points inside of the bounding rectangle.
          >
          > So my question is, does anyone have any ideas about how I might improve
          > this? Thanks.
          >
          > Dave
          >
          >


          • 2. Re: Question for you imaging gurus
            Level 7
            Dave C wrote:
            > polygon, I use sexPixel to change it to white.

            This will only breed more pixels :o)

            > All in all it works pretty well and I am happy with the results.
            <but slow>
            > So my question is, does anyone have any ideas about how I might
            > improve this? Thanks.

            You could draw the boundary of the area to be filled and then use the
            undocumented floodFill.
            http://www.lingoworkshop.com/Articles/Undocumented.php

            Andrew


            • 3. Re: Question for you imaging gurus
              Level 7
              Hmm, interesting. I can certainly see where that could speed up things. I
              will have to look into how I might go about breaking it up into smaller
              rects. That's seems like it could be difficult. Thanks for the idea.



              • 4. Re: Question for you imaging gurus
                Level 7
                LOL, didn't get it at first until I saw my typo.

                Thanks for the heads up on floodfill (wasn't aware of it). A quick test
                shows that works nicely. On hitch though, right now I have nothing that
                prevents the user from drawing complex polygons
                ( http://en.wikipedia.org/wiki/Image:Complex_polygon.png) and the current
                algorithm correctly fills these in. Using floodfill I would have to identify
                when these occur and locate a point inside each region. Floodfill is very
                fast though.


                "Andrew Morton" <akm@in-press.co.uk.invalid> wrote in message
                news:e6juci$47v$1@forums.macromedia.com...
                > Dave C wrote:
                > > polygon, I use sexPixel to change it to white.
                >
                > This will only breed more pixels :o)
                >
                > > All in all it works pretty well and I am happy with the results.
                > <but slow>
                > > So my question is, does anyone have any ideas about how I might
                > > improve this? Thanks.
                >
                > You could draw the boundary of the area to be filled and then use the
                > undocumented floodFill.
                > http://www.lingoworkshop.com/Articles/Undocumented.php
                >
                > Andrew
                >
                >


                • 5. OT - XYZ*Bert
                  Level 7
                  Hi all -
                  Our company changed hosts recently, so at the same time I picked up a new
                  domain for my games. I thought I'd post it here as lately I've noticed that
                  I've been getting a lot of requests for XYZ*Bert in my server logs. So, I
                  figured I'd better get it back online...

                  Our new site, just for games, is: http://www.blurredgames.com

                  It's new... it's in development...there's nothing there but a few games...
                  More to come soon however, so keep checking back.



                  --
                  Dave -
                  Adobe Community Expert
                  www.blurredistinction.com
                  http://www.adobe.com/communities/experts/


                  • 6. Re: Question for you imaging gurus
                    Chunick Level 3
                    check out my 'polygon selection tool' (listed at the left)
                    http://www.chunick.com/director.htm

                    the code can be downloaded (well, can be found in the downloaded .dir file) from the right under 'Experiments' listed as #18.

                    Basically, what it'd doing is drawing a vectorshape, filling it with the appropriate colour (in this case, grayscale shade) and then getting the image of the vectorshape to use as the alpha.

                    Instead of creating a new image object and applying the polygon image as the alpha you'd just apply the polygon image as an alpha onto your current image... you may need to make a couple other changes to the code depending on your needs, but it should work relatively well "out of the box" so-to-speak.
                    • 7. Re: OT - XYZ*Bert
                      Level 7
                      oops. You may want to repost this as a seperate topic.


                      • 8. Re: Question for you imaging gurus
                        Level 7
                        I can't seem to get to the website. I assume this is a temporary condition
                        and will try back later.


                        • 9. Re: Question for you imaging gurus
                          Level 7
                          OK, so I can modify a vector shape's vertexList easy enough to create an
                          already filled shape that exactly matches the shape drawn by the user. Very
                          cool.

                          "just apply the polygon image as an alpha onto your current image" Any good
                          tutorials on using Alpha in Director. I have looked at this some in the past
                          but it is still a mystery to me. I have a nagging feeling not knowing this
                          is holding me back from making some handy code.


                          • 10. Re: Question for you imaging gurus
                            James Newton, ACP Level 3
                            This assumes that:
                            * your vectorShape member is called "vectorShape"
                            * your bitmap member is called "bitmap"
                            * your bitmap member is 32-bit, with the alpha channel active and currently opaque

                            vHoleAlpha = member("vectorShape").image.extractAlpha()
                            vRect = vHoleAlpha.rect
                            vHoleAlpha.copyPixels(vHoleAlpha, vRect, vRect, [#ink: #notCopy]) -- inverts black and white

                            vBitmapImage = member("Bitmap").image
                            vBitmapAlpha = vBitmapImage.extractAlpha()
                            vBitmapAlpha.copyPixels(vHoleAlpha, vRect, vRect)
                            vBitmapImage.setAlpha(vBitmapAlpha)
                            • 11. Re: Question for you imaging gurus
                              Level 7
                              vBitmapAlpha.copyPixels(vHoleAlpha, vRect, vRect,[#ink #lightest])

                              I made one slight modification, adding an ink parameter to the second
                              copyPixel statement. This prevents black pixels from overwriting white
                              pixels in the case where a rect overlaps an existing 'hole'. This technique
                              is vastly superior to my previous implementation, which worked albeit
                              slowly.

                              Many, many thanks to Craig, Andrew, Dave, Josh and James. Your willingness
                              to share your vast and valuable knowledge is commendable. Director's imaging
                              capabilities seems to be one of it's most unique and powerful features. Your
                              help has been crucial in helping me get my mind around it.

                              This code is going to be used to add features to a personal project of mine.
                              It is a freeware game that I made. If there is no objection, I would like to
                              mention each of you (and add links to your site) on a credits page of the
                              game.

                              Thanks Again

                              Dave


                              • 12. Re: Question for you imaging gurus
                                Chunick Level 3
                                quote:

                                Originally posted by: Newsgroup User

                                Many, many thanks to Craig, Andrew, Dave, Josh and James. Your willingness
                                to share your vast and valuable knowledge is commendable. Director's imaging
                                capabilities seems to be one of it's most unique and powerful features. Your
                                help has been crucial in helping me get my mind around it.

                                This code is going to be used to add features to a personal project of mine.
                                It is a freeware game that I made. If there is no objection, I would like to
                                mention each of you (and add links to your site) on a credits page of the
                                game.

                                Thanks Again

                                Dave

                                Thank-you and good luck with your game. I'm more than happy to see my name in the credits of your game. You'll have to maybe send a link to where it can be downloaded when it's all done.