It is small library for work with BitmapData. You can easy create image editor with this code. There are many variants of library usage. Demo and all sources are included.

Description
Library can be used in flex of actionscript 3.0 project. Actually there is only one class in library. It contains public methods that do ations with image.
Next is possible with BitmapUtil:

  • To change the contrast;
  • To change the brightness;
  • To change the saturation;
  • To do the effect of negative;
  • To enlarge or to miniaturize of pictures;
  • To elongate by horizontal or vertical;
  • To crop the rectangular part from picture;
  • To rotate the picture from the centre;
  • To add the border to picture – is simple stripe with defined color and thickness;
  • To add some other pictures – the adding parts can be BitmapData or graphical elements IBitmapDrawable. This function is much parameterized. You can assign the rotation, transparency and scaling for added part.

There are no lines or brush drawing in the library. I didn’t need that in my project. I think it can be implemented too with method addUIToBitmap.

Demo in flex
BitmapUtil demo in flex
Click to image to view sample. All sources here

Inside
Off course its need to load image first. And I’m sure you know how to do that. The one problem can happened with image loading. If it was loaded from server another from swf was running. The picture shown well but it will be impossible to edit its BitmapData. The SecurityError can be thrown. Then you need load policy file like:

loader.load(new URLRequest(url), new LoaderContext(true));

If image was loaded with flash.display.Loader, then the object with type of BitmapData can be obtained from loader.content.
All operation on image pixels processed in client-side. As result we get edited raster, which is need to save. It’s no need to send command like “change contrast +20” and load edited image from server. Flash Player can do that itself.

Usage sample:

var oldBitmap:Bitmap;
var bitmapUtil:BitmapUtil = new BitmapUtil();
var newBitmap = bitmapUtil.contrast(oldBitmap, 50);

So you get and more contrasted image.
I didn’t add docs to all methods in class. I think it will be enough name of method to define its functionality. I defined possible range of methods arguments.

Few notices about BitmapData usage:
1. Its need to clean BitmapData object, if it isn’t needed: BitmapData.dispose().
2. If you call dispose(), that object maybe not erased from memory immediately. If you work with raster, then Flash Player memory usage may growth a lot. It works good if we force actionscript 3.0 garbage collector like Grant said:

try {
    new LocalConnection().connect('foo');
    new LocalConnection().connect('foo');
} catch (e:*) {}

I noticed myself that garbage maybe called while flash window resized.
3. Take in mind that maximum size of BitmapData is 2880 x 2880 px.

Performance
Each BitmapUtil method creates and destroys object with type BitmapData (it contains array with pixels). Some places for optimization are in the code. But application doesn’t looks slow with forced Garbage Collector. And all transformation goes quickly for image 800 x 600.