Posts filed under 'Bitmap/BitmapData'
I miss writing classes w/ 50 or so lines of code and here is one. This is a take off of Lee Brimelow’s Example of Dynamic Sound Generation. I’m using the BitmapData to create a tone.
[as]
package
{
import flash.display.*;
import flash.events.*;
import flash.media.*;
import flash.net.URLRequest;
import flash.system.Capabilities;
import flash.text.TextField;
public class DynamicSound extends Sprite
{
private var sound:Sound;
private var noise:Number = 0;
private var _loader:Loader;
private var _bm:Bitmap;
private var _verText:TextField;
public function DynamicSound():void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
_verText = new TextField();
_verText.text = Capabilities.playerType + ” (” + Capabilities.version + “)”;
_loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
_loader.load(new URLRequest(”http://www.scarlet.nl/~ivo/photo_ASTRO3.JPEG”));
}
private function onComplete(evt:Event):void
{
_bm = Bitmap(_loader.content);
addChild(_bm);
addChild(_verText);
sound = new Sound();
sound.addEventListener(Event.SAMPLES_CALLBACK, onCallback);
sound.play();
}
private function onCallback(e:SamplesCallbackEvent):void
{
for(var i:uint=0; i<512; i++)
{
noise += _bm.bitmapData.getPixel(mouseX, mouseY)/ 44100;
var sample:Number = noise * Math.PI * 2;
sound.samplesCallbackData.writeFloat(Math.sin(sample));
sound.samplesCallbackData.writeFloat(Math.sin(sample));
}
}
}
}
[/as]
Quick and Dirty!
May 20th, 2008
Alright, this example is very similar to the other examples I have done. But with this one I am scaling all pixels at the same rate. Just click on it the image in the example Source | Example
[as] import flash.display.*;
import caurina.transitions.Tweener;
import flash.sampler.*;
var pixHolder:Sprite = new Sprite();
var img:Img = new Img();
//pixHolder.opaqueBackground = true;
addChild(pixHolder);
var coords_array:Array = [];
var pixels_array:Array = [];
var bmd:BitmapData = new BitmapData(img.width, img.height, false);
bmd.draw(img, new Matrix());
bmd.lock();
startSampling();
for (var i:int = 0; i < img.width; i++)
{
for (var j:int = 0; j < img.height; j++)
{
var coords:Object = {};
//var pixData:BitmapData = new BitmapData(1, 1, false, bmd.getPixel(i, j));
//var pixBit:Bitmap = new Bitmap(pixData, PixelSnapping.ALWAYS, true);
var pixSprite:Sprite = new Sprite();
pixSprite.graphics.beginFill(bmd.getPixel(i, j), 1);
pixSprite.graphics.drawRect(0, 0, 1, 1);
pixSprite.graphics.endFill();
pixSprite.x = i;
pixSprite.y = j;
//pixSprite.filters = [new BlurFilter(5, 5, 3)];
//pixSprite.addChild(pixBit);
pixHolder.addChild(pixSprite);
coords.sprite = pixSprite;
coords.x = i;
coords.y = j;
coords_array.push(coords);
}
}
trace(”what: ” +getSampleCount() )
bmd.unlock();
bmd.dispose();
stage.addEventListener(MouseEvent.CLICK, updating, false, 0, true);
function updating(evt:MouseEvent):void
{
removeEventListener(MouseEvent.CLICK, updating);
trace(”over”);
for(var k:int = 0; k < coords_array.length; k++)
{
Tweener.addTween(coords_array[k].sprite, {x:coords_array[k].sprite.x * 2, scaleX:coords_array[k].sprite.scaleX * 2, scaleY:coords_array[k].sprite.scaleY * 2, delay:.5, y:coords_array[k].sprite.y * 2, transition:”linear”, time:.5});
}
trace(getSampleCount() )
}
[/as]
April 29th, 2008

Alright, in this example, I am cutting each pixel in an image and piecing it back together again. Only this time I animating the pixel to a new location, piecing back the image at 200%. I think this can open a lot doors in what you can do with a bitmap.
Here is the Bitmap test 2 example
bitmaptest2.fla
Here is the code
[as]import flash.display.*;
import caurina.transitions.Tweener;
//holds all the pixels
var pixHolder:Sprite = new Sprite();
//linkage to img in library
var img:Img = new Img();
addChild(pixHolder);
//array that will hold info of each pixel
var pixels_array:Array = [];
//bitmapdata of the img
var bmd:BitmapData = new BitmapData(img.width, img.height, false);
//put the img bitmapdata in the new bitmapdata object
bmd.draw(img, new Matrix());
//I heard this helps performance
bmd.lock();
//loop thru each pixel
//i.e. i= 0; j = 0 then i=1; j = 0 then i = 2; j = 0 you get it right?
for (var i:int = 0; i < img.width; i++)
{
for (var j:int = 0; j < img.height; j++)
{
//create an obj to hold each pixels info
var coords:Object = {};
//in this example I decided to use a rect graphic instead of a single bitmap
//for fun
var pixSprite:Sprite = new Sprite();
//make a pixSprite for each pixel
pixSprite.graphics.beginFill(bmd.getPixel(i, j), 1);
pixSprite.graphics.drawRect(0, 0, 1, 1);
pixSprite.graphics.endFill();
// add an event listener to each pixel to animate
pixSprite.addEventListener(MouseEvent.MOUSE_OVER, updating, false, 0, true);
//put pixels in their right place
pixSprite.x = i;
pixSprite.y = j;
pixHolder.addChild(pixSprite);
}
}
bmd.unlock();
//you don’t need the data in memory any more
bmd.dispose();
function updating(evt:MouseEvent):void
{
//the moused over pixel removes listener, scales 200% and moves down to make a img at 200%
evt.currentTarget.removeEventListener(MouseEvent.MOUSE_OVER, updating);
trace(”over”);
Tweener.addTween(evt.currentTarget, {scaleX:2, scaleY:2, x: img.width + evt.currentTarget.x * 2, y: img.height + evt.currentTarget.y * 2, transition:”linear”, time:.5});
}
[/as]
April 23rd, 2008

So the last couple of weeks I have been jacking around w/ Bitmaps and BitmapData. I’ve always wanted to try it but never had the courage. Now that I have, I wish I’d have done it sooner.
In this first example I’m just copying each pixel throwing it to the wind and using Tweener to animate it to a spot. I don’t think Tweener is the best option course as you can see it runs slow. I’m also using Grant Skinner’s Memory Gauge to illustrate how slow it is. This is just a proof of concept anyway.
Here is the Bitmap and BitmapData src v1
Here is the code
[as]
// code written by Josh Weatherspoon of http://swfitgood.com
import flash.display.*;
import caurina.transitions.Tweener;
//holds all the “pixels”
var pixHolder:Sprite = new Sprite();
//image of me in the library thru linkage
var img:Img = new Img();
//pixHolder.opaqueBackground = true;
addChild(pixHolder);
//array that will hold info of each pixel
var coords_array:Array = [];
//bitmapdata of the img
var bmd:BitmapData = new BitmapData(img.width, img.height, false);
//put the img bitmapdata in the new bitmapdata object
bmd.draw(img, new Matrix());
//I heard this helps performance
bmd.lock();
//loop thru each pixel
//i.e. i= 0; j = 0 then i=1; j = 0 then i = 2; j = 0 you get it right?
for (var i:int = 0; i < img.width; i++)
{
for (var j:int = 0; j < img.height; j++)
{
//create an obj to hold each pixels info
var coords:Object = {};
//var pixData:BitmapData = new BitmapData(1, 1, false, bmd.getPixel(i, j));
//var pixBit:Bitmap = new Bitmap(pixData, PixelSnapping.ALWAYS, true);
//in this example I decided to use a circlular graphic instead of a single bitmap
//for fun
var pixSprite:Sprite = new Sprite();
//make a pixSprite for each pixel
pixSprite.graphics.beginFill(bmd.getPixel(i, j), 1);
pixSprite.graphics.drawCircle(0, 0, 1);
//set the pixel to a random location on the stage
pixSprite.x = Math.random() * stage.stageWidth;
pixSprite.y = Math.random() * stage.stageHeight;
//pixSprite.filters = [new BlurFilter(5, 5, 3)];
//pixSprite.addChild(pixBit);
pixHolder.addChild(pixSprite);
//gather info to access each pixel individually
coords.sprite = pixSprite;
coords.x = i;
coords.y = j;
coords_array.push(coords);
}
}
bmd.unlock();
// on mouse over start animating
stage.addEventListener(MouseEvent.MOUSE_OVER, updating, false, 0, true);
function updating(evt:MouseEvent):void
{
// loop thru each pixel and move it to the original location
stage.removeEventListener(MouseEvent.MOUSE_OVER, updating);
for(var k:int = 0; k < coords_array.length; k++)
{
Tweener.addTween(coords_array[k].sprite, {x:coords_array[k].x, scaleX:1, scaleY:1, delay: .3, y:coords_array[k].y, transition:”linear”, time:10});
}
}
[/as]
April 21st, 2008