SmartAA – reconstruct anti aliasing with Pixel Bender
21. February 2010 – 02:59Tags: After Effects, aliasing, anti, b-spline, bicubic, Filter, Mitchell-Netravali, Photoshop, Pixel Bender, reconstruct
About three decades ago, pixels where as big as toasters – and mostly green.
Today we have millions of them to deal with, and the terms anti-/aliasing are part of our daily live and every time an image is scaled, it gets interpolated to keep those bad “stairs” away from our eyes.
But what if the source image is aliased in the first place?
Checkout the demo of SmartAA!
While upsaling is the most common cause of aliasing, there are images that are naturally aliased – for example images from a 3D render engine.
Of course there are many ways to antialias cg images when generating them – for example by rendering to a higher resolution than targeted and downscaling the result (FSAA)- or by rendering and merging four slightly offset variations.
While this is as computational intensive as it sounds, modern GPU´s can handle this task rather easily as they have their buffers and hardwired magic to do so, but still it’s not uncommon to come along aliased images in the world of computer graphics.
So is all this material “useless” or can an image that has never seen any antialiasing, or has been upscaled with the nearest neighbour method, be reconstructed?
In fact all that modern imaging software does when rescaling an image is reconstructing its pixels to fit the new dimensions – most times with a bicubic filter.

SmartAA is a bicubic BC-spline reconstruction filter for PixelBender.
Its 3×3 kernel gathers all neighboring pixels of the one to reconstruct and weights them with the Mitchell-Netravali(german link) filter.
In essence, this means that each pixel is modified by the average color of its surrounding friends.
While “average” being the wrong term here as the influence of each pixel is determined by the spline curve of the Mitchell-Netravali filter.
This eliminates those hard, “stair-like” artifacts of the aliasing by visually “softening” or unsharping the image.
However this is not simply a destructive blurring effect but a reconstructive analysis of the source “signal”.
There are many known filters that do this hard job – it´s not easy to reconstruct information that was partly lost (or never there in the first place).

Thus all of them suffer of the same issues – the result always shows some artifacts.
Mostly it’s a visible unsharping, anisotropy or the so-called “ringing“.
In general bicubic reconstruction methods create the best results.
Among the various possible filter methods the windowed Sinc(Lanczos)- and Mitchell-Netravali filter are chosen the most as their output is considered to be the best mix of the inevitable unsharping, anisotropy and ringing.
Just for the record: there are “better” solutions, the so called “adaptive” or “smart” filters that try separate methods on edges, fills and other structures.
They are particularly useful when working with sequences of moving images (video) but again slow to compute.
SmartAA on the other hand is a quite simple implementation of the M.-N. filter and runs blazingly fast when run on the GPU – producing about 70 redraws per sec on a 1024*768 image.
Unfortunately it´s not nearly as fast on the CPU.
If you have a rather slow machine, you will have seen the demo struggling to keep a 24fps framerate, so its just! that bit too slow to to antialias the pixelated output of a flash based C64 emulator.
That would have been cool… would love to see something like that.
I kept it flash compatible anyway – still there might be applications.
Its a matter of personal reception, but to me the M.-N. filter looks better on videos as the inbuild “smooth”-method.
Anyway – flash video has been criticized enaugh for high cpu usage so lets not make things worse
Also: like any reconstruction filter it won´t work in any given scenario as there are some natural and some specific limitations.


So what can SmartAA do and what not? Here is a feature/limitation list:
Features:
- Works with Photoshop, After Effects, Flash
- Supports alpha channel
- Is real-time
Limitations:
- Does no up/down scaling – just reconstruction of anti aliasing
- Is scenario specific – for example text will often look strange
- works only with artifacts in the same pixel space.
Let me elaborate on the last point:

As SmartAA takes only neighbouring pixels into account it isn´t able to reconstruct aliasing effects that span over greater distances than 2 pixels.
For example if an image exists of 4*4 pixel blocks of the same color, those blocks would get rounded on the edges but will still be visible as rounded blocks.
But it works ideal on sources like aliased output from a render engine, or images that have been upscaled x2 with the nearest neighbour method.
Conclusion:
Aliasing should be fought before it ends up within an image as there are more sophisticated methods to keep it from emerging than to reconstruct it after the damage is done.
But sometimes it´s inevitable and if you are looking for a simple but effective way to get rid of that ugly blocks within an image or video – SmartAA will do the trick.
Download:
Grab the Shader here and use it with Photoshop (Pixel Bender Plugin needed), After Effects or Flash!
Hint:
Try to add a sharpen filter or better an unsharpen mask to the result to counter the blur effect.
Don´t forget to use the width/hight properties – SmartAA will work without but there will be artifacts on the upper- and lower right corner.
I hope this is helpful to anyone out there and would love to hear some comments or ideas.
Cheers!

18 Responses to “SmartAA – reconstruct anti aliasing with Pixel Bender”
I think you can remove those (ax>=1.0) in the source file, around 5-10% faster in my computer.
BTW, is it a freeware that I can use it in my project?
By Edan Kwan on Feb 22, 2010
Whoo, how did that slip in?
Thanks for pointing this out Edan – of course that condition was completely redundant.
The package has been updated accordingly.
Please feel free to use smartAA in your projects.
Cheers!
By flashgordon on Feb 22, 2010
I removed all useless variables and reassigned the if structure a little bit. from 96fps -> 120fps
http://edankwan.amiciforce.com/SmartAA_edited.pbk
By Edan Kwan on Feb 22, 2010
Nicely done.
I think you just optimized it as far as possible without writing it directly in assembler
You may have noticed, I didn´t even try to speed it up.
I was worried over the performance from the beginning, thats why I went with the M.N. as I assumed it would be faster than the Lanczos (no sine)… also it doesn´t sharpen that much.
But than it became clear to me that it wouldn´t be quite fast enaugh for flash anyway so I stopped caring – it was late
On the other hand it might work fine for a small or rarely redrawed area… and who can argue with optimized code?
I updated the shader with your improvements.
Much appreciated – thx!
By flashgordon on Feb 22, 2010
I really like you using pixel bender to achieve those graphic algorithms in other languages. I am no hardcore programmer and I wish I can be that smart like you to do the same thing.
By Edan Kwan on Feb 23, 2010
I am sure you can.
True, shader programming sometimes seams quite tricky because of the limited language and the math involved, but in the end its just juggling with colors …and a lot of fun
By flashgordon on Feb 23, 2010
I made a SSAO demo in Papervision3D which I was inspired by your SSAO example. You can check it out in my portfolio: http://edankwan.amiciforce.com/
BTW, what do you do for living? Programmer? Can I have your contact?
By Edan Kwan on Feb 25, 2010
I see you are using kode80´s depthshader.
Very nice example – is this SmartSSAO working there?
I made some promising tests with Papervision but decided not to post them because kode80 was faster and pretty much nailed the thing.
And yes I work as a freelancer – will contact you via mail (info@smart-page.net).
By flashgordon on Feb 25, 2010
Didn’t understand everything about how it works, but works really well!
This might be really useful for preloading high res images using low res and upscaling x2. Or doing the smoothing on a whole bunch of stuffs at once instead of separate instances with the built it flash bitmap smoothing.
I think flash by default anti alias all the bitmap content with quality high, i wonder if you put it back to low and use this filter, this might lead to performance and visual improvements. To check.Anyway. Very useful to me.Thanks a lot.
By Mathieu on Mar 18, 2010
Hi Mathieu,
glad you find the shader useful.
Btw: psykopaint rocks
By flashgordon on Mar 18, 2010
Hi,
I just did a shader for Cubic Lens Distortion, but some people pointed out that it did alias more than other lens distortion plug-in of AE.
Would it be ok for me to use your code in it ?
Thx
By francoistarlier on Mar 23, 2010
Hi,
I just did a shader for Cubic Lens Distortion, but some people pointed out that it did alias more than other lens distortion plug-in of AE (which is totally true).
Would it be ok for me to use your code in it ? I would leave the credit to you
http://www.francois-tarlier.com/blog/index.php/2010/03/update-cubic-lens-distortion-pixel-bender-shader-for-ae-with-scale-chroamtic-aberration/
Thx
By francoistarlier on Mar 23, 2010
Hi Francoir,
very interesting shader – I also looked into chromatic aberration recently.
I am glad if my code can improve your project and I would be very interested in seeing the result.
Cheers!
By flashgordon on Mar 23, 2010
thx a lot.
well the chromatic aberration is just a plus, the lens distortion is more the point of this. So I’ll try your code and keep you updated.
thx
By francoistarlier on Mar 23, 2010
Great work!
I’m using SmartAA not only for Images but also for TLF device fonts.
Thanx a lot!
By Masahito Ohtsuka on Feb 24, 2011
Saw your blog post, cool stuff.
While I knew that SmartAA ‘could’ work well for fonts, I never thought about using it as a workaround for embedding – great idea!
By flashgordon on Feb 24, 2011
Hello!
Really great work on this filter. I would like to use it on embedded font text in order to smooth the letters for printing. So I apply it on a bitmapdata with alpha. On some parts of my image I get holes in obliques… I let you have a look: http://imm.io/7f0D
By Kodiak on Jul 8, 2011
Hi Kodiak,
this looks indeed strange, but could be completely normal – I had to see the source image.
Did you set the width/hight properties?
Cheers
By flashgordon on Jul 8, 2011