Fast Fourier Transform for Flash

FFT simple usage example

There is smth new I would like to share with Flash community: ASFFT Project. Well project is too loud name as far as it is only small library that allows users to decompose images using Fast Fourier Transform in to Real and Imaginary parts. But if you dig more in to the title you will find lots of amazing possibilities it brings to you. The problem is that it is pretty slow and can’t be used on processing video frames for example but it is fast enough to do different image manipulations without freezing Flash Player.

The lib will contain 2 classes “FFT2D” and “FFT”. First one is to work with two dimensional data mostly presented as images. The second one is for streaming (or not) one dimensional data. Usually used in sound signal manipulations/transformation.
At this very moment only FFT2D class available. I will add one dimensional FFT class in near future.

Simple demo example of FFT usage
Project page at Google Code

FlashSURF -> ASSURF

The shortest blog post of mine.
I’ve received a mail from the Group Product Manager of Adobe Flash Player:
“I’m writing to you because the current name of the library misuses the Flash brand. As you know, Flash® is a well-known trademark of Adobe registered in many countries, and in order for us to protect it we need to be diligent about the proper use of the mark… etc.”

As you may understand Adobe asked me to change the Lib name. I’ve no chance to debate on the subject as far as they are right here and they do what they should do. So after not long conversation new name has been chosen ASSURF. (thanx to Mario Klingemann and Jon Howard for the idea)

So if you have any bookmarks update it:
ASSURF Wiki page
ASSURF SWC Lib development project
ASSURF usage examples project

FlashSURF – moving further

FlashSURF

Past couple of weeks I was working on FlashSURF project updates. More likely cosmetic updates: refactoring, adding methods, etc. I’ve screen-casted several videos showing up some usage possibilities.
Project SVN repo was divided in to 2 separate projects: one for FlashSURF SWC Lib development only and another for example projects. Now FlashSURF become really easy to use as far you don’t have to compile and process it with TDSI every time you testing your project. Provided SWC lib already processed, all you need is to include it in your project.

As I’ve already mentioned I’ve screen-casted some videos. All this features are presented in Examples project and can be easily reproduced.

This one demonstrates multiple image tracking/searching in input video stream. I’ve precalculated several images and store them as references for future tracking. Then we just loops through references to search each on the screen.




Here I’ve implemented selection tool and with the help of it I can easily select regions on the screen to store it as references. I was experimenting with playing cards and as you see it works pretty well.

You will find more examples in the repository with demonstration of panoramic stitching, finding Homography matrix between reference and tracked object on the screen, saving and loading references as local binary files etc.

I do realize there is a lot to add and improve in the project. I kindly ask everyone who is interested in using it to take a part by submitting features/methods requests and sharing improvements that can be done to existing methods. Just submit an issue in Code repo or simply drop me a line.

Async PNG encoding and merging

As you may read in my Twitter last week I was experimenting with asynchronous PNG image encoding and saving. It is very easy task until you need to save a really large/huge image file (for print purpose or whatever). As far as Flash don’t have ability to compress/deflate ByteArrays in append way or simply partly we surely will crash flash player while compressing image data for image larger then 10.000×10.000 px.

Joa provided a link to a flashzlib by Ed McManus and that way the “sign” to use Alchemy to process compression via ZLIB.
But what was my surprise when I found that we easily can compile original ZLIB sources in alchemy and use it in our Alchemy projects without any changes! And that was possible since December 2008! Can you believe it? ;-)
So I downloaded ZLIB sources and compile them in alchemy as the result I got static library I can append to any Alchemy project. All you need is to write wrapper C class to call functions in ZLIB and get the result back.

I also found brilliant optimized PNG class at wonderfl that used different filters for the image data. I’ve encapsulated that filters in my PNG encoder in both synchronous and asynchronous way. Alchemy is used only for the compress of image data all other operations done in Flash. It is also possible to compile PNGLIB for the Alchemy (as easy as ZLIB) but it will result in really huge *.swc file. So it is to you to decide.

See small demo app where you can encode large PNG files asynchronous with compress level and filter options.


Get Adobe Flash player

You can get sources here (including *.as classes, ZLIB *.swc, C wrapper source)
note: you will need to download and compile your own ZLIB if you want to recompile provided *.swc

Example code of different class possibilities:

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
var png:PNGEncoder = new PNGEncoder();
png.addEventListener(Event.COMPLETE, onEncoded);
png.addEventListener(ProgressEvent.PROGRESS, onEncodeProgress);
 
// filter options are available via static properties
// PNGEncoder.FILTER_NONE
// PNGEncoder.FILTER_SUB
// PNGEncoder.FILTER_UP
// PNGEncoder.FILTER_AVERAGE
// PNGEncoder.FILTER_PAETH
 
// we can encode in various ways
// Simple static synch encode
 
PNGEncoder.encode(source:BitmapData, opaque:Boolean = false, comressLevel:int = -1, filter:int = 0):ByteArray;
 
// Asynch encode
// you should provide listeners to catch encoding progress and result
 
png.encodeAsync(source:BitmapData, opaque:Boolean = false, comressLevel:int = -1, filter:int = 0);
 
// Asynch multiply Bitmapdata objects merging in single PNG image file
// you should provide listeners to catch encoding progress and result
// you also should provide callback function for returning Bitmapdata objects for each row
 
var currInfo:PNGEncoderInfo = new PNGEncoderInfo(
						resultImageWidth:uint, resultImageHeight:uint,
						numberOfColumns:uint, numberOfRows:uint,
						getNextBitmapsRow:Function, opaque:Boolean,
						compressLevel:int, filter:int);
png.encodeMultiToOne(currInfo);
 
function getNextBitmapsRow(info:PNGEncoderInfo, yi:uint):Vector.<BitmapData>
{
	var currBitmapsRow:Vector.<BitmapData> = new Vector.<BitmapData>();
	// yi argument is current row index
	// info.xn - is number of BitmapData objects in image row
	for (var j:int = 0; j < info.xn; ++j) 
	{
		currBitmapsRow[j] = new BitmapData(800, 600, false, int(Math.random() * 0xFFFFFF));
	}
 
	return currBitmapsRow;
}
 
function onEncoded(e:Event = null):void 
{
	// here is our encoded PNG file ByteArray
	var data:ByteArray = PNGEncoder.encodedPNGData;
}
function onEncodeProgress(e:ProgressEvent):void 
{
	trace('ENCODING PNG: ' + String( Math.round(e.bytesLoaded / e.bytesTotal * 100) ) + '%');
}

FluidSolver 3D | first steps

FluidSolver in 3D

Well finally I got some time to finish Flash based demo application for the FluidSolver in 3D experiment project. This project is based on my previous fluid solver classes and heavily use Joa’s TDSI tool. This is my first try to implement this simulation if flash and decided to start from Alchemy based Memory usage approach. In plans to try Pixel Bender combined with Alchemy… All calculations made in Flash using ByteArray read/write with TDSI to handle data. Particle engine is based on dynamically growing/reducing LinkedList.
During building this demo I’ve lots of test versions, I’ve tried to use Processing to see the performance difference (you will find links to this screencasts later) also finally discovered the great power of GLSL in Java etc. Mostly because of my Processing results I decided to try Pixel Bender/Alchemy approach in near future. I truly belive it can be done faster in Flash.

Sources can be found in my Google repo at FluidSolver3D branch (be careful it is not well organized cause still in heavy development)

OK, here we have Flash demo application that supports different option changes via Right Click menu. You can choose live mode by selecting any local mp3 file to visualize or to switch to manual mode. There is also an option to turn on/off Sparkles on particles.

Fluids Solver in 3D demo application
See screencasted video on Akira Kiteshi – Pinball composition

Here is some Processing based demos:
Fluids 3D | Processing progress
Cube Fluids | fully GLSL based approach

Super Shape in 3D

Super Shape in 3D

Couple of days before I stumbled upon Andre Michelle Super Ellipse example. You can think – nothing special but it is like magic how fast it performs and deforming in different 3D shapes. Next thing after Super Ellipse is Super Shape. It is a lot more complex in computation and I thought Flash would stack with it. But after some test it turns to be possible.
You can find a lot more info on subject at Paul Bourke website.
I have created a demo application that shows basic usage of Super Shape class. Hype project was used for Sound Spectrum control and options adjustments upon sound change. This is a very dirty 3D rendering using built in Flash methods with very very primitive triangle Z sorting.

Here is the source for the class
Demo Application

Simple code example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// create instance with resolution 24
var ss3d:SuperShape3d = new SuperShape3D(24);
 
// update shape by applying 2 Super Shapes parameters
ss3d.update(1.0, 1.0, 7.0, 0.2, 1.7, 1.7,
			1.0, 1.0, 7.0, 0.2, 1.7, 1.7);
 
var proj:PerspectiveProjection = new PerspectiveProjection();
proj.fieldOfView = 75;
var matrix:Matrix3D = new Matrix3D();
matrix.appendScale(64, 64, 64);
matrix.appendTranslation( 0, 0, 128);
matrix.append(proj.toMatrix3D());
 
Utils3D.projectVectors(matrix, ss3d.wVerts, ss3d.projectedVerts, ss3d.uvts);
 
this.graphics.clear();
ss3d.draw(this.graphics, 1, 0x333333, 0);

Steering Behaviors AS3 lib

Steering Behaviors in 3D

Last week I was working on project where I needed one of my not very old experiment as base. It was about creating a game using steering behaviors logic. I was playing with this stuff some time ago and now I’ve to re-factor it. So why not to create a public lib for simple usage. The main goal of re-factoring was to make usage as simple as possible, also giving users an ability to add their own behaviors easy. The second point is performance (all iterations done using Linked Lists). This lib doesn’t have all possible behaviors and may be you wont find smth you are looking for but as for me I think that it simple enough to add new ideas in minutes!
I’ve included examples in the sources to show the basic usage. One of the examples is using Away3DLite library for 3D rendering. (you have to download lib yourself)

You can get the sources here

Old examples from my blog:

Steering Behaviors in 3D | Flocking
Steering Behaviors in 3D

Median Filtering [updated]

Median Filter

If you follow me on Twitter you know that last days Mario, Nicoptere and me were trying to discover the fastest Median Filtering solution for Flash. Our first attempts were around 7 secs per 350×360 image size with small kernel 7×7! You may think that this is very slow, but Median Filtering is slow in all environments cause of its architecture. Anyway there is a constant time solution described here that will result in constant processing time for any radius. It is also mentioned that this is the fastest possible solution for now!
So no surprise all of us tried to implement it. ;) After some unlucky results Nicoptere decided to leave the scene but this is all because of lack of time I think! ;) Mario as he always do start trying to implement Linked Lists to make the process as fast as it possible. I’ve to say that Mario’s version is the fastest for now except one condition I describe later.
As for me I started with the simple Vector arrays and you will see this code inside my sources. This approach wasn’t very fast but stable for different kernel sizes. And the final version created using Linked Lists as Mario recommended. As it turns out that my version runs a little bit slower then Mario’s at small radius but performs faster when we extend filter radius size (starting radius 7).

Update:
I’ve merged my Histogram class with Mario’s filter applying structure (it is faster and more accurate) what results in faster and a lot more stable filter results. Now the speed doesn’t grow upon radius but it performs event faster with larger radius!
Added ByteArray based method using Joa’s TDSI tool. It performs faster at small radius but upon radius grow Linked List approach may become faster.
I’ve also added another ByteArray based method that works amazingly fast! Unfortunately this method will have unexpected results starting radius 16 in worst cases due to a tricky operation of packing integers in to single entry. I put it in MedianFilter Class under constantTimeBALimit name. But it is not included in demo application.

Test results:
Pure coded Linked Lists: 7×7 kernel: 0.8-1.17 sec; 101×101 kernel: 0.7-1.14 sec
ByteArray with TDSI: 7×7 kernel: 0.7-0.86 sec; 101×101 kernel: 1.0-1.17 sec
ByteArray with TDSI (Limited): 7×7 kernel: 0.43-0.51 sec; 101×101 kernel: 0.6-0.74 sec

(all tests done using standalone non-debug Flash Player version, Mac and PC)

Resources:
Test Application
Median Filter Class

Median Filter Median Filter Median Filter

FlashSURF Lib [initial release]

Flash SURF

Finally I finished all needed preparations for the first initial release of FlashSURF Lib. You’ve already seen some videos posted in twitter. Now I’m publishing all the sources to my google repo. I think this project has a lot of opportunities and I’m really waiting for the response from Flash Community with the advices, ideas, etc on how to improve or what to add and so on…
Currently it supports basic features extraction, matching them to provided reference image and estimating projection matrix between correspondences (Homography). This is the start point to try implement marker-less Augmented Reality or panorama building application or images searching etc. As you see there are tons of ideas in what direction to extend it and I hope you will share yours with me and others as I did. In this case we can end up with smth interesting and useful for everyone!

P.S.: This project is using Joa’s TDSI Tool!

Here you will find all the sources and small test application where I tried to describe the main features of the library.

And here is some videos and another reconstructed panorama:
Talking about FlashSURF at Flash2000 Event
My first tests of the library

Flash SURF

FluidSolverHD [Alchemy version]

FluidSolverHD [Alchemy version]

It was just a question of time for me to try rewriting FluidSolver Class to Alchemy one. It wasn’t very hard and I was waiting for really speed boosted version. As the result it is faster but not as much as I wanted ;) I’ve tried to include Particle engine inside Alchemy code but it turns out that rendering particles is not really easy task and costs a lot. I’ve also used Joa’s TDSI again but it is not really needed as far as we don’t have to write/read Alchemy memory a lot (you can un-comment this part in FluidSolverHD class). Anyway the main idea of the class is drawing colors of Fluids and in this part it works great. I’ve included old test application but with some rewritten methods. Without using particles (25.000) it runs 50-60 FPS using grid size 100×56 for solver iterations. It could be easily changed up to 150 in width and still have 40 FPS! (All this results are in non-debug Flash Player version)

You can launch preview application by clicking image above or this link. (please note: you will need Flash 10 to view it)
Sources could be found in my google repo.

Have fun!