Felipe
Felipe

Reputation: 11887

What's wrong with this piece of code?

I'm trying to make a image editor in Flex 4.5.

However, there's one little thing that doesn't work as appropriate. Here's my code:

            private function returnCropIndicatorBmpDataForTopLeft():BitmapData{

            var topLeftX:int     = 0;
            var topLeftY:int     = 0;

            var topRightX:int    = _bmpData.width;
            var topRightY:int    = 0;

            var bottomRightX:int = _bmpData.width;
            var bottomRightY:int = _bmpData.height;

            var bottomLeftX:int  = 0;
            var bottomLeftY:int  = _bmpData.height;

            var cropIconX:int    = _mouseX;
            var cropIconY:int    = _mouseY;

            var temporaryBitmapData:BitmapData = _bmpData;

            var originalColor:uint;
            var dimmedColor:uint = 0x202020;

            for (var i:int = 0; i < _bmpData.width; i++) 
            {
                for (var j:int = 0; j < _bmpData.height; j++) 
                {
                    originalColor = _bmpData.getPixel(i,j);

                    if(i>cropIconX && j>cropIconY){
                        temporaryBitmapData.setPixel(i,j,originalColor);
                    }else{
                        temporaryBitmapData.setPixel(i,j,dimmedColor);
                    }
                }
            }
            /*
            *by the end of this loop, we have, in the temporaryBitmapData variable, a version of the _bmpData,
            *the area to be cropped out dimmed a little bit.
            */

            return temporaryBitmapData;
        }

if you look carefully inside the first if statement in the innermost for loop,

temporaryBitmapData.setPixel(i,j,originalColor);

that bit of code should do the following:

If this (i,j) pixel is outside of the "to-be-cropped-out area", then repaint it with the original pixel color.

It just can't seem to get that to work!!!!

I've substituted that line with some hardcoded value (say, 0xFFFFFF for white) and it did work, so the problem is not there....

Hope you guys can help me out I've spent more than 4 hours on this already trying many different approaches!!

P.S.> I debugged this code in FB 4.5 and placed a breakpoint in that very if statement. For some reason, the _bmpData variable, which is where I want to get the original pixel color from , shows a small red square next to it.... Perhaps that is indicative of something.. perhaps it's somehow thread-'locked'?? I don't know, hope someone can figure out!

EDIT the problem I'm having is the following: the dimming of the image works fine, but if I then move the mouse cursor back to an area that got dimmed out, then the original image doesn't get repainted, as expected.

Upvotes: 0

Views: 164

Answers (1)

sberry
sberry

Reputation: 132028

There must be something else going on there... I just tested your code and it worked fine for me.

package  {

    import flash.display.MovieClip;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.net.*;
    import flash.display.Loader;
    import flash.events.Event;

    public class ImageCropper extends MovieClip {

        protected var _bmpData:BitmapData;
        protected var _mouseX:int;
        protected var _mouseY:int;
        var imageLoader:Loader;

        public function ImageCropper() {
            _mouseX = 80;
            _mouseY = 50;
            imageLoader = new Loader();
            var image:URLRequest = new URLRequest("http://www.travelooce.com/pics/bear_picnic_table.jpg");
            imageLoader.load(image);
            imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, _imageLoaded);
            imageLoader.x = 0;
            imageLoader.y = 0;

        }

        public function _imageLoaded($evt:Event):void {
            _bmpData = new BitmapData(imageLoader.width, imageLoader.height, false);
            _bmpData.draw(imageLoader);
            var bmp2:BitmapData = returnCropIndicatorBmpDataForTopLeft();
            var bmp:Bitmap = new Bitmap(bmp2);
            addChild(bmp);
        }

        private function returnCropIndicatorBmpDataForTopLeft():BitmapData{

            var topLeftX:int     = 0;
            var topLeftY:int     = 0;

            var topRightX:int    = _bmpData.width;
            var topRightY:int    = 0;

            var bottomRightX:int = _bmpData.width;
            var bottomRightY:int = _bmpData.height;

            var bottomLeftX:int  = 0;
            var bottomLeftY:int  = _bmpData.height;

            var cropIconX:int    = _mouseX;
            var cropIconY:int    = _mouseY;

            // This is the important line to change.
            var temporaryBitmapData:BitmapData = _bmpData.clone();

            var originalColor:uint;
            var dimmedColor:uint = 0x202020;

            for (var i:int = 0; i < _bmpData.width; i++) 
            {
                for (var j:int = 0; j < _bmpData.height; j++) 
                {
                    originalColor = _bmpData.getPixel(i,j);

                    if(i>cropIconX && j>cropIconY){
                        temporaryBitmapData.setPixel(i,j,originalColor);
                    }else{
                        temporaryBitmapData.setPixel(i,j,dimmedColor);
                    }
                }
            }
            /*
            *by the end of this loop, we have, in the temporaryBitmapData variable, a version of the _bmpData,
            *the area to be cropped out dimmed a little bit.
            */

            return temporaryBitmapData;
        }
    }

}

Output:

http://cl.ly/3d0h023C1p392O270I2Lenter image description here

Upvotes: 1

Related Questions