Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display progressive JPEG in Flash during the loading

I need to display the image in progressive JPEG format ( http://en.wikipedia.org/wiki/JPEG#JPEG_compression , not to be confused with progressive display of sequential JPEG). Flash supports loading of progressive JPEG, but I have no idea how to display it during the loading. Brief googling gives me progressive loading of sequential JPEG and nothing else.

like image 594
Pehat Avatar asked Jun 28 '12 14:06

Pehat


People also ask

What is Progressive JPEG format?

A progressive JPEG image is encoded differently than a standard or baseline JPEG image. It loads in successive waves until a clear picture is formed. This can improve a website's performance as the images seems to be loading faster.

What is the difference between baseline and progressive JPEG?

Less waiting time for usersWith baseline JPEG, your users see a large white space on the screen or a loading circle spinning until the image has finished loading. With progressive JPEG, visitors can already see the entire image at first sight. Although it's blurry, they can still understand what's in the image.

What is a progressive image?

A progressive image is interlaced meaning the image will start out as low quality, however will continue to improve in resolution with each additional "pass". Traditionally, images load from the top-down (for JPEG images these are called Baseline JPEG).


1 Answers

It would go something like this:

// the loader containing the image
var loading:Boolean = false;
var loader:Loader = new Loader();
addChild(loader);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function() {
    loading = false;
    trace(loader.width, loader.height);
});

var bytes:ByteArray = new ByteArray();

var stream:URLStream = new URLStream();
stream.addEventListener(ProgressEvent.PROGRESS, onProgress);
stream.addEventListener(Event.COMPLETE, onProgress);
stream.load(new URLRequest(imageURL));

function onProgress(e:Event):void {
    stream.readBytes(bytes, bytes.length);
    if((bytes.length > 4096 && !loading) || e.type == Event.COMPLETE) {
        loading = true;
        loader.loadBytes(bytes);
    }
}

Notice that the loadBytes process is asynchronous. Also, when you try it with an unparseable bytearray (usually the first calls to onProgress, when there's not enough image data to process) it fails silently, so you need to somehow guarantee that you have enough data... in this case I used if(bytes.length > 4096) ;)

like image 145
Cay Avatar answered Oct 23 '22 01:10

Cay