You want to create a mosaic-like effect?

Forget getPixel/setPixel/drawrect.. it'll be too slow. Instead, you should let Flash handle it by creating new Bitmaps, shrinking the image inside it, drawing that to a new bitmapdata, and then using that new bitmapdata on the original size.

Zeh

Andrei Thomaz wrote:
hello list,

I am making some tests to discover if it is possibile to pixelize a bitmap
with AS3, with a reasonable speed. I wrote the code below, but it works only
with images until 50x50 (in a quadcore). You can see the effect
http://www.andreithomaz.com/labs/files/pixel/teste1.html.

I would like to ask you for advices about how I could optimize it. Or if I
just should try Java/C++.


thank you,
andrei



public dynamic class Teste1 extends MovieClip {

        public var m_mcPicture:MovieClip;
        public var m_sprScreen:Sprite;
        private var m_tmTimer:Timer;
        private var m_nPixel:int;
        private var m_bDirection:Boolean;
        private var m_bmpOriginalImage:BitmapData;
        private var m_aPixels:ByteArray;
        private var m_rcImageArea:Rectangle;

        public function Teste1() {

            m_rcImageArea = new Rectangle(0, 0, m_mcPicture.width,
m_mcPicture.height);
            m_nPixel = 1;

            m_bmpOriginalImage = new BitmapData( m_mcPicture.width,
m_mcPicture.height );
            m_bmpOriginalImage.draw(m_mcPicture);
            m_aPixels = m_bmpOriginalImage.getPixels(m_rcImageArea);

            m_mcPicture.visible = false;

            m_sprScreen = new Sprite();
            addChild(m_sprScreen);

            m_tmTimer = new Timer(2000);
            m_tmTimer.addEventListener( TimerEvent.TIMER, _pixelizaImagem );
            m_tmTimer.start();

            m_bDirection = true;
        }

        private function _pixelizaImagem( evt:TimerEvent ):void
        {
            if (m_bDirection) {
                ++m_nPixel;
                if (m_nPixel >= 50) {
                    m_bDirection = false;
                }
            }
            else {
                --m_nPixel;
                if (m_nPixel <= 1) {
                    m_bDirection = true;
                }
            }

            var nRows:int = Math.ceil( (m_mcPicture.height as Number) /
(m_nPixel as Number) );
            var nCols:int = Math.ceil( (m_mcPicture.width as Number) /
(m_nPixel as Number) );
            var i:int;
            var j:int;
            var k:int = 0;
            var l:int = 0;
            var m:int = 0;
            var r:int, g:int, b:int;
            var offset:int;
            var offset2:int;
            var aPixels:ByteArray = new ByteArray();

            var nPixelsPerPixel;
            var nLimitK:int = m_nPixel;
            var nLimitL:int = m_nPixel;

            for (i = 0; i < nRows; i++) {
                if (i == (nRows - 1)) {
                    nLimitK = m_bmpOriginalImage.height - (i * m_nPixel);
                }
                else {
                    nLimitK = m_nPixel;
                }

                for (j = 0; j < nCols; j++) {

                    if (j == (nCols - 1)) {
                        nLimitL = m_bmpOriginalImage.width - (j * m_nPixel);
                    }
                    else {
                        nLimitL = m_nPixel;
                    }

                    offset = i * m_bmpOriginalImage.width * m_nPixel;
                    offset += (j * m_nPixel);

                    r = g = b = 0;

                    for (k = 0; k < nLimitK; k++) {
                        offset2 = (offset + (k * m_bmpOriginalImage.width) )
* 4;
                        for (l = 0; l < nLimitL; l++) {

                            r += m_aPixels[ offset2 + 1];
                            g += m_aPixels[ offset2 + 2 ];
                            b += m_aPixels[ offset2 + 3 ];
                            offset2 += 4;
                        }
                    }
                    nPixelsPerPixel = nLimitK * nLimitL;
                    r /= nPixelsPerPixel;
                    g /= nPixelsPerPixel;
                    b /= nPixelsPerPixel;

                    for (k = 0; k < nLimitK; k++) {
                        offset2 = (offset + (k * m_bmpOriginalImage.width) )
* 4;
                        for (l = 0; l < nLimitL; l++) {

                            aPixels[ offset2 ] = 255;
                            aPixels[ offset2 + 1 ] = r;
                            aPixels[ offset2 + 2 ] = g;
                            aPixels[ offset2 + 3 ] = b;
                            offset2 += 4;
                        }
                    }


                }
            }

            var bmpPixelizedImage:BitmapData = new BitmapData(
m_bmpOriginalImage.width, m_bmpOriginalImage.height);
            bmpPixelizedImage.setPixels(m_rcImageArea, aPixels);

            m_sprScreen.graphics.beginBitmapFill(bmpPixelizedImage);

            m_sprScreen.graphics.moveTo(0, 0);
            m_sprScreen.graphics.lineTo(bmpPixelizedImage.width, 0);
            m_sprScreen.graphics.lineTo(bmpPixelizedImage.width,
bmpPixelizedImage.height);
            m_sprScreen.graphics.lineTo(0, bmpPixelizedImage.height);
            m_sprScreen.graphics.lineTo(0, 0);
            m_sprScreen.graphics.endFill();

        }

    }
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to