• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to "crop" a masked BMP?

Guest
Jul 15, 2010 Jul 15, 2010

Copy link to clipboard

Copied

I have a 'puzzle' with the pieces 'scattered' haphazardly around.  These pieces are all instances of my PTPuzzlePiece.as class, which works great for all intents and purposes.  When the user selects a photo from the picture library, the pieces all properly display their 'masked' portion of the image.   See below:

PictureToolsGoalCardScreen1.jpg

PictureToolsGoalCardScreen.jpg

However, I have a problem in that the masked bitmap displayed in each piece is only 'masked' and not cropped.  This means that, for instance on the third piece down on the left hand side, 100 not-visible pixels of bitmap extend below the piece and out of the 'framed' area.  This totally throws off my calculations when I go to scale and then print the screen using the AIR PrintJob, and when I scale that 'card' down to a 'button' on the menu where the user chooses which 'card' they wish to use.

Is there a way to crop the not-visible area out, or copy only the visible pixels as BitmapData into a new Bitmap entity so that the layout is entirely contained within the 'frame'?

I've poured over the documentation and have come up empty.

Thanks for any help.

TOPICS
ActionScript

Views

1.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 15, 2010 Jul 15, 2010

Copy link to clipboard

Copied

why don't you use the mask's size in your calculations?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 15, 2010 Jul 15, 2010

Copy link to clipboard

Copied

Complexity.  I would consider a cropped or copied solution to be more elegant.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 15, 2010 Jul 15, 2010

Copy link to clipboard

Copied

there's decreased complexity with using the mask properties rather than creating a cropped bitmap.

but if you want to crop the bitmap, you can use the bitmapdata class to create a new bitmap of the cropped original.  first, you would create a bitmapdata object of the mask with a background color distinct from the foreground.  then loop though the mask pixels checking for the foreground and copying the puzzle piece's pixel data to the new bitmapdata object.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 15, 2010 Jul 15, 2010

Copy link to clipboard

Copied

If that's the only solution, I'll grant you the complexity comparison

I'm not certain if I completely understand it, but I'll give it a shot.

Thank you.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 15, 2010 Jul 15, 2010

Copy link to clipboard

Copied

you're welcome.

p.s.  please mark this thread as answered, if you can.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 15, 2010 Jul 15, 2010

Copy link to clipboard

Copied

Puzzled... I thought I had marked it as answered.

Found this as a possible less exact but more efficient example as it appears that the 'rect' region of all of my pieces is within the 'frame'... if that makes sense.

var square:Sprite = new Sprite();
var circle:Sprite = new Sprite();
var holder:Sprite = new Sprite();

square
.graphics.beginFill(0,.5);
square
.graphics.drawRect(0,0,100,100);
square
.graphics.endFill();

circle
.graphics.beginFill(0);
circle
.graphics.drawCircle(0,0,50);
circle
.graphics.endFill();

addChild
(holder);
holder
.addChild(square);
holder
.addChild(circle);
square
.mask = circle;

var cloneData:BitmapData = new BitmapData(holder.width,holder.height,true,0x00FFFFFF);
cloneData
.draw(holder);
var clone:Bitmap = new Bitmap(cloneData);
addChild
(clone);
clone
.x = 30;

I'll see how well I get on with that.  I may in the end be forced to do it pixel by pixel, but I'm hoping not.

Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 15, 2010 Jul 15, 2010

Copy link to clipboard

Copied

i don't see how that will help you but you can use the copypixels method of the bitmapdata class to speed the process.  i think it may be a little more difficult to implement than doing  what i suggested but it is more efficient.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 20, 2010 Jul 20, 2010

Copy link to clipboard

Copied

Worked a treat!

            addChild(puzzlePieceBackground);
            addChild(bmpSprite);
            addChild(puzzlePieceFrame);
        } // End PTPuzzlePiece Constructor

        public function addBMP(b:Bitmap):void
        {
            trace("CLASS PTPuzzlePiece FUNCTION addBMP("+b+")");

            var tmpSprite:Sprite = new Sprite();
            tmpSprite.addChild(puzzlePieceMask);
            tmpSprite.mask = puzzlePieceMask;
            addChild(tmpSprite);

            var bmp:Bitmap = new Bitmap(b.bitmapData);
            bmp.x = 0 - pt.x;
            bmp.y = 0 - pt.y
            tmpSprite.addChild(bmp);
           
            var cloneBitmapData:BitmapData = new BitmapData(puzzlePieceMask.width, puzzlePieceMask.height, true, 0x0000ffff);
            cloneBitmapData.draw(tmpSprite);
           
            tmpSprite.removeChild(bmp);
            tmpSprite.mask = null;
            tmpSprite.removeChild(puzzlePieceMask);
            removeChild(tmpSprite);

            bmp = new Bitmap(cloneBitmapData);
            bmpSprite.addChild(bmp);

            fadeTween.continueTo(1,1);
        } // End addBMP function

Thank you.  I was pulling my hair out trying to find a way to not make this card be one more case of special tests and exceptions and handleing to the unified code/operation of the others. (It already is for other reasons).   Thanks for getting me started looking in the right direction!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 20, 2010 Jul 20, 2010

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines