« FlexCamp Presentation (part 1 of 3) : FileReference load() | Main | FlexCamp Presentation (part 3 of 3) : FileReference save() »
FlexCamp Presentation (part 2 of 3) : Pixel Bender
| By Rich Tretola | September 9, 2009 | |
| 7,067 views |
In part 1 of this series, I showed how to load a file into the Flash Player by using the FileReference load() method. In this case we are loading an image, but you can load any file type.
In this part, I will show how to use Pixel Bender filters to filter the loaded image.

In part 1 we discussed how to load a file into the Flash Player via the FileReference class. Part 2 will discuss how to use Pixel Bender filters, and finally in part 3 will discuss how to save the file back to the file system.
Click here to run the application (View source is enabled)
So, here is how it works. Lets review the code below.
- On lines 21-32, I am embedding 4 Pixel Bender filters. Note: you don’t need to embed the filters, you can load them at run time from your server via URLLoader or even allow the end user to load them at run time using the FileReference load() method that we previously discussed.
- Next on lines 35-38, I create 4 corresponding Shader variables
- On lines 41-44, I create 4 corresponding ShaderFilter variables
- On lines 63-70 (which is part of the init() function called on applicationComplete()), I instantiate the Shader and ShaderFilter objects by passing in the Pixel Bixel bender classes and casting them as byteArray objects.
- At this point we have 4 filters available within the application, so all that is left to do is apply them to the image. I have 4 button components (lines 142 – 155), each calls the addFilter() method on line 122 passing in a ShaderFilter. Ex: clicking on the Pixelate button, calls the addFilter() method and passed in the pixelateFilter ShaderFilter instance. The addFilter() method does the following: creates a local variable named filters which is initially set to the current filters that may be applied to the image on line 123. Next, the filter passed in is pushed onto the filters array and finally the image.filters property is set to the new filters array. Doing it the way I have will cause a cumulative effect as additional filters are added, you can also have simply set the images array to the single filter passed in like this: image.filters = [filter]; which would simply use the newest filter as the only filter
- Finally, line 152 contains a button which when clicked calls the removeFilter() which simply sets the image.filters array to null.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" backgroundColor="0xFFFFFF" applicationComplete="init();" viewSourceURL="srcview/index.html"> <mx:Script> <![CDATA[ import mx.controls.Alert; import mx.formatters.NumberFormatter; import mx.graphics.codec.PNGEncoder; // encoder to create file to save private var png:PNGEncoder = new PNGEncoder(); // allowed file types private var fileTypes:FileFilter = new FileFilter("Image", "*.jpg;*.jpeg;*;*.gif;*.png;*"); // FileReference classes for upload and download [Bindable] private var loadFileRef:FileReference = new FileReference(); private var saveFileRef:FileReference = new FileReference(); // filter objects [Embed("filters/pixelate.pbj", mimeType="application/octet-stream")] private var PixelateFilterClass:Class; [Embed("filters/SmartNormalMap.pbj", mimeType="application/octet-stream")] private var SmartNormalMapFilterClass:Class; [Embed("filters/twirl.pbj", mimeType="application/octet-stream")] private var TwirlFilterClass:Class; [Embed("filters/ZoomBlurFocus.pbj", mimeType="application/octet-stream")] private var ZoomBlurFocusClass:Class; // Shaders private var pixelateShader:Shader; private var smartNormalMapShader:Shader; private var twirlShader:Shader; private var zoomBlurFocusShader:Shader; // ShaderFilters private var pixelateFilter:ShaderFilter; private var smartNormalMapFilter:ShaderFilter; private var twirlFilter:ShaderFilter; private var zoomBlurFocusFilter:ShaderFilter; // format file size private var numberFormatter:NumberFormatter = new NumberFormatter(); // timers to track upload time private var startTime:Date; private var endTime:Date; // call on applicationComplete private function init():void { // add event listeners for upload loadFileRef.addEventListener(Event.SELECT, fileSelected); loadFileRef.addEventListener(ProgressEvent.PROGRESS, loadProgress); loadFileRef.addEventListener(Event.COMPLETE, loadCompleted); loadFileRef.addEventListener(IOErrorEvent.IO_ERROR, ioerror); // create new shaders and filters pixelateShader = new Shader(new PixelateFilterClass() as ByteArray); pixelateFilter = new ShaderFilter(pixelateShader); smartNormalMapShader = new Shader(new SmartNormalMapFilterClass() as ByteArray); smartNormalMapFilter = new ShaderFilter(smartNormalMapShader); twirlShader = new Shader(new TwirlFilterClass() as ByteArray); twirlFilter = new ShaderFilter(twirlShader); zoomBlurFocusShader = new Shader(new ZoomBlurFocusClass() as ByteArray); zoomBlurFocusFilter = new ShaderFilter(zoomBlurFocusShader); } // launch the file system browse dialog and filter file types private function browseAndUpload():void { log.text=""; image.source=null; removeFilters(); loadFileRef.browse([fileTypes]); } // called after a file has been selected within the file system browser private function fileSelected(evt:Event):void { log.text += "file size: " + numberFormatter.format(loadFileRef.size) +" bytes\n"; log.text += "file name: " + loadFileRef.name +"\n"; startTime = new Date(); loadFileRef.load(); } // load progress captured for progress bar private function loadProgress(evt:ProgressEvent):void { progressBar.visible = true; progressBar.setProgress( Number(evt.bytesLoaded / evt.bytesTotal), 1 ); progressBar.label = numberFormatter.format(evt.bytesLoaded) + " of " + numberFormatter.format(evt.bytesTotal) + " bytes uploaded"; } // load has completed private function loadCompleted(evt:Event):void { image.source = loadFileRef.data; progressBar.setProgress(1,1); progressBar.visible = false; endTime = new Date(); log.text += "upload time: " + (endTime.time - startTime.time) + " milliseconds"; } // load error public function ioerror(evt:IOErrorEvent):void{ Alert.show(evt.text,"Error"); } // capture image and save back to client private function capture():void{ var bitmapData:BitmapData = new BitmapData(image.width, image.height); bitmapData.draw(image); var ba:ByteArray = png.encode(bitmapData); // split off the original extension and replace with png var ext:String = loadFileRef.name.split(".").pop(); saveFileRef.save(ba,loadFileRef.name.replace(ext,"png")); } // add PixelBender filter private function addFilter(filter:ShaderFilter):void{ var filters:Array = image.filters; filters.push(filter); image.filters = filters; } // remove all filters private function removeFilters():void{ image.filters = null; } // return true when image is available private function buttonsEnabled(bytes:ByteArray):Boolean{ if(bytes.length)return true; return false; } ]]> </mx:Script> <mx:HBox> <mx:Button label="Load file" click="browseAndUpload()"/> <mx:Button label="Save file" click="capture()" enabled="{buttonsEnabled(loadFileRef.data)}"/> <mx:Button label="Pixelate" click="addFilter(pixelateFilter)" enabled="{buttonsEnabled(loadFileRef.data)}"/> <mx:Button label="SmartNormalMap" click="addFilter(smartNormalMapFilter)" enabled="{buttonsEnabled(loadFileRef.data)}"/> <mx:Button label="Twirl" click="addFilter(twirlFilter)" enabled="{buttonsEnabled(loadFileRef.data)}"/> <mx:Button label="ZoomBlurFocus" click="addFilter(zoomBlurFocusFilter)" enabled="{buttonsEnabled(loadFileRef.data)}"/> <mx:Button label="Remove All Filters" click="removeFilters()" enabled="{buttonsEnabled(loadFileRef.data)}"/> </mx:HBox> <mx:TextArea id="log" width="700" height="60" /> <mx:ProgressBar id="progressBar" visible="false" mode="manual"/> <mx:Image id="image" maxHeight="600" maxWidth="800"/> </mx:Application> |
Part 3 will demonstrate how to use the FileReference save() method to save the newly filtered image back to the file system.
Topics: Flash Player, Flex, Tutorials | 3 Comments »









September 9th, 2009 at 9:50 am
[...] This post was mentioned on Twitter by Adobe Software. Adobe Software said: FlexCamp Presentation (part 2 of 3) : Pixel Bender http://bit.ly/1R7rHP [...]
September 9th, 2009 at 1:46 pm
http://www.musdepo.com/video/ тайна скачать фильм
Reply to this comment
September 11th, 2009 at 11:27 am
[...] 11/09/2009 Posted in 5584 by robertopriz on September 11, 2009 Pixerl Bender http://blog.everythingflex.com/2009/09/09/flexcamp-presentation-part-2-of-3-filereference-load/ [...]