Tag Archive for 'mosaic'

JPEG Async Encoder class meets merging multi-BitmapData

I often use PNG/JPEG Encoding in Flash and during my projects I faced some problems with this classes.
The main problem is speed and CPU usage when encoding large BitmapData objects. First I decided to rewrite PNG Encoder class provided by Tinic Uro and optimized by Aral but we cant make it encoding data asynchronous because it is using built in ByteArray compress method to GZIP image data. When encoder calls this method on large data Flash Player completely freezes. It can take up to 15 seconds to compress data. And you can do nothing all this time but waiting. May be there is an alternate way to compress ByteArray but I didnt find any.

So I turned to JPEG Encoder class provided by Adobe. After some research I found a solution for Async Encoding. As far as it was designed for Flex I rewrite the class merging everything in one file. Now it is all in one solution. You can encode BitmapData and ByteArray objects Async listening for Progress and Complete Events or encode data through one method call.
I’ve also added method for merging several BitmapData objects in to one JPEG image. This method is very useful when you need to generate large image but can’t do it because of the Flash built in BitmapData size restrictions.
I’m using this class in my Flickr Mosaic engine to encode result mosaic image. See it in action merging several BitmapData objects into one JPEG file.

Class itself could be seen in my Google Code repository within Flickr Mosaic sources.

See some usage examples:

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
import ru.inspirit.utils.JPGEncoder;
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.utils.ByteArray;
 
var bmp1:BitmapData = new BitmapData(640, 400, false, 0xFF0000);
var bmp2:BitmapData = new BitmapData(640, 400, false, 0x00FF00);
var bmp3:BitmapData = new BitmapData(640, 400, false, 0x0000FF);
var bmp4:BitmapData = new BitmapData(640, 400, false, 0xFFFF00);
 
var je:JPGEncoder = new JPGEncoder(90);
je.addEventListener(ProgressEvent.PROGRESS, onEncodingProgress);
je.addEventListener(Event.COMPLETE, onEncodingComplete);
 
// Encoding several BitmapData objects into one image
// You should provide 'square' of Bitmaps in 2 dimensional array
je.encodeMultiToOne([
				[bmp1, bmp2],
				[bmp3, bmp4]
				]);
 
// Simple encode
/*
var ImageByteArray:ByteArray = je.encode(bmp1); 
*/
 
// Async encode
// You should provide listeners (see above) to handle data ready event
/*
je.encodeAsync(bmp1);
*/
 
private function onEncodingComplete(e:Event):void 
{
	// Async Encoding Complete
	var ImageByteArray:ByteArray = je.ImageData;
}
 
private function onEncodingProgress(e:ProgressEvent):void 
{
	trace('ENCODING PROGRESS: ' + Math.round(e.bytesLoaded/e.bytesTotal * 100) + '%');
}

Flickr Mosaic at Google Code

Inspirit Google Code RepositoryMy personal Google Code repository is opened!
After some deep thought ;) I decided to share my Flickr Mosaic project code as first release.

You will find full project source but with minimum code explanation. Sorry about that, I’ll answer all questions if you have any. Also you can comment out there and add issues.

Flickr Mosaic is an Adobe Flash web application that uses Flickr photos as tiles to create mosaics from your images.

You can choose any image from your hard drive or capture using webcam, then tune some options and launch render process! Thats all! As the result you can zoom mosaic with mouse wheel or use simple shortcuts: CTRL +/-
(You will need Flash Player 10 )
And here some mosaic results (be careful: source images are about 4-6Mb):
Flickr MosaicFlickr MosaicFlickr MosaicFlickr Mosaic

Last project in 2008 [2009.defa.ru]

2009.defa.ruMy last project in year 2008 was New Year Greeting site for Defa Interactive agency. This site is based on my Mosaic prototype that I created in 2007 :) and then completely rewrite it for new Flash 10 features. The main idea is simple: it lets people to convert their images/photos to mosaic one using different Flickr images as tiles source.

After all you can send the result mosaic to you friend with your greeting and also to print as 2009 year calendar for free!
This was my last project as Flash Developer at Defa Interactive. ;) New Year – new plans!