Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>( too slow :) ) so this is the sae as above with a linear loop instead of 2 nested loops.</p> <pre><code>//creates a new BitmapData, with transparency, white 0xFFFFFF var bd:BitmapData = new BitmapData( 100, 100, false, 0xFFFFFF ); //stores the width and height of the image var w:int = bd.width; var h:int = bd.height; var i:int = w * h; var x:int, y:int, col; //decremental loop are said to be faster :) while ( i-- ) { //this is the position of each pixel in x &amp; y x = i % w; y = int( i / w ); //gets the current color of the pixel ( 0xFFFFFF ) col = bd.getPixel( x, y ); //assign the 0xFF0000 ( red ) color to the pixel bd.setPixel( x, y, 0xFF0000 ); } addChild( new Bitmap( bd ) );//a nice red block </code></pre> <p>note that if you're using a bitmapData with an alpha channel (say if you load the image, the alpha will be turned on automatically ) you 'll have to use </p> <pre><code>bd.getPixel32( x, y );// returns a uint : 0xFF000000 //and bd.setPixel32( x, y, UINT );// 0xFF000000 </code></pre> <p>EDIT : I 've done a quick bench :</p> <pre><code>package { import flash.display.BitmapData; import flash.display.Sprite; import flash.utils.getTimer; public class pixels extends Sprite { private var bd:BitmapData = new BitmapData( 100, 100, false, 0xFFFFFF ); public function pixels() { var i:int, total:int = 100, t:int = 0; t = getTimer(); i = total; while( i-- ) { whileLoop( bd ); } trace( 'while:', getTimer() - t ); t = getTimer(); i = total; while( i-- ) { forLoop( bd ); } trace( 'for:', getTimer() - t ); } private function forLoop( bd:BitmapData ):void { var i:int, j:int; var col:int; for ( i = 0; i &lt; bd.width; i++ ) { for ( j = 0; j &lt; bd.height; j++ ) { col = bd.getPixel( i, j ); // +/- 790 ms } } //for ( i = 0; i &lt; bd.width; i++ ) for ( j = 0; j &lt; bd.height; j++ ) col = bd.getPixel( i, j ); // +/-530 ms //var w:int = bd.width; //var h:int = bd.height; //for ( i = 0; i &lt; w; i++ ) for ( j = 0; j &lt; h; j++ ) col = bd.getPixel( i, j ); // +/-250 ms } private function whileLoop( bd:BitmapData ):void { var w:int = bd.width; var h:int = bd.height; var i:int = w * h; var col:int; while ( i-- ) { col = bd.getPixel( i % w, int( i / w ) ); // +/- 580 ms } //while ( i-- ) col = bd.getPixel( i % w, int( i / w ) ); // +/- 330 ms } } } </code></pre> <p>for 100 * ( 100 * 100 ) getPixel, the fastest (on my machine) is the one-line for loop with local variables. ( +/- 250 ms ) then the one-line while( +/- 330 ms ) :)</p> <p>storing local variables w and h for width and height makes the for loops twice faster :) </p> <p>good to know </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload