Created
November 13, 2011 05:33
-
-
Save cameron314/1361653 to your computer and use it in GitHub Desktop.
PNGEncoder2 benchmark source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.display.*; | |
import flash.events.*; | |
import flash.net.*; | |
import flash.utils.*; | |
import ru.inspirit.encoders.png.PNGEncoder; | |
import flash.system.ApplicationDomain; | |
import com.adobe.images.PNGEncoder; | |
import mx.graphics.codec.PNGEncoderAsync; | |
import fl.controls.ComboBox; | |
import flash.text.TextField; | |
import fl.controls.ProgressBar; | |
import by.blooddy.crypto.image.PNGEncoder; | |
import fl.data.DataProvider; | |
public class Main extends MovieClip | |
{ | |
private var cboLevel : ComboBox; | |
private var bmp : BitmapData; | |
private var bmpOpaque : BitmapData; | |
private var current : int; | |
private var start : int; | |
private var frames : int; | |
private var pngToSave : ByteArray; | |
public function Main() | |
{ | |
current = -1; | |
cboLevel = new ComboBox(); | |
cboLevel.x = 9; | |
cboLevel.y = 56; | |
cboLevel.width = 145; | |
addChild(cboLevel); | |
cboLevel.addItem({ label: "UNCOMPRESSED", data: "0" }); | |
cboLevel.addItem({ label: "FAST", data: "1" }); | |
cboLevel.addItem({ label: "NORMAL", data: "2" }); | |
cboLevel.addItem({ label: "GOOD", data: "3" }); | |
cboLevel.selectedIndex = 1; | |
reset(); | |
txtOutput.text = ""; | |
btnBrowse.addEventListener(MouseEvent.CLICK, onBrowse); | |
btnGo.addEventListener(MouseEvent.CLICK, onGo); | |
btnSave.addEventListener(MouseEvent.CLICK, onSave); | |
this.addEventListener(Event.ENTER_FRAME, onFrame); | |
} | |
private function reset() | |
{ | |
current = -1; | |
txtFileInfo.text = ""; | |
progress.visible = false; | |
cboLevel.enabled = false; | |
chkAlpha.enabled = false; | |
btnGo.enabled = false; | |
btnSave.enabled = false; | |
} | |
private function onBrowse(e : MouseEvent) | |
{ | |
reset(); | |
btnBrowse.enabled = false; | |
var fileReference : FileReference = new FileReference(); | |
fileReference.addEventListener(Event.COMPLETE, onDataAvailable); | |
fileReference.addEventListener(Event.SELECT, function (e : Event) { | |
txtFileInfo.text = fileReference.name; | |
fileReference.load(); | |
}); | |
fileReference.addEventListener(Event.CANCEL, function (e : Event) { | |
btnBrowse.enabled = true; | |
}); | |
fileReference.browse([ new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png") ]); | |
} | |
private function onDataAvailable(e : Event) | |
{ | |
var fileReference : FileReference = FileReference(e.target); | |
var loader : Loader = new Loader(); | |
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded); | |
loader.loadBytes(fileReference.data); | |
} | |
private function onImageLoaded(e : Event) | |
{ | |
var loader : Loader = LoaderInfo(e.target).loader; | |
bmp = new BitmapData(int(loader.width), int(loader.height), true, 0x00FFFFFF); | |
bmp.draw(loader); | |
bmpOpaque = new BitmapData(int(loader.width), int(loader.height), false, 0x00FFFFFF); | |
bmpOpaque.draw(loader); | |
cboLevel.enabled = true; | |
chkAlpha.enabled = true; | |
btnGo.enabled = true; | |
btnBrowse.enabled = true; | |
} | |
private function totalBytes() : int | |
{ | |
var bytesPerPixel : int = 4; | |
return int(bytesPerPixel * bmp.width * bmp.height); | |
} | |
private function getFileInfo() : String | |
{ | |
return bmp.width + "x" + bmp.height + " " + (totalBytes() / 1024 / 1024).toPrecision(2) + "MB"; | |
} | |
private function onGo(e : MouseEvent) | |
{ | |
cboLevel.enabled = false; | |
chkAlpha.enabled = false; | |
btnGo.enabled = false; | |
btnBrowse.enabled = false; | |
btnSave.enabled = false; | |
txtOutput.appendText("Sync (" + getFileInfo() + "):\n\n"); | |
advance(); | |
} | |
private function onSave(e : MouseEvent) | |
{ | |
var fileReference = new FileReference(); | |
fileReference.save(pngToSave, "output.png"); | |
} | |
private function onFrame(e : Event) | |
{ | |
++frames; | |
} | |
private function advance() | |
{ | |
++current; | |
if (current >= 7) { | |
end(); | |
return; | |
} | |
var bmp = chkAlpha.selected ? this.bmp : this.bmpOpaque; | |
switch (cboLevel.value) { | |
case "0": PNGEncoder2.level = CompressionLevel.UNCOMPRESSED; break; | |
case "1": PNGEncoder2.level = CompressionLevel.FAST; break; | |
case "2": PNGEncoder2.level = CompressionLevel.NORMAL; break; | |
case "3": PNGEncoder2.level = CompressionLevel.GOOD; break; | |
} | |
if (current == 4) { // First async | |
txtOutput.appendText("\nAsync (" + getFileInfo() + "):\n\n"); | |
} | |
var syncEncoderPng = null; | |
var asyncEncoder = null; | |
start = getTimer(); | |
frames = 0; | |
switch (current) { | |
// ---- Sync ---- | |
case 0: | |
txtOutput.appendText("PNGEncoder2:\t\t\t"); | |
syncEncoderPng = PNGEncoder2.encode(bmp); | |
break; | |
case 1: | |
txtOutput.appendText("Adobe PNGEncoder:\t"); | |
syncEncoderPng = com.adobe.images.PNGEncoder.encode(bmp); | |
break; | |
case 2: | |
txtOutput.appendText("In-Spirit PNGEncoder:\t"); | |
syncEncoderPng = ru.inspirit.encoders.png.PNGEncoder.encode(bmp, !chkAlpha.selected, -1, ru.inspirit.encoders.png.PNGEncoder.FILTER_PAETH); | |
break; | |
case 3: | |
txtOutput.appendText("Bloody PNG encoder:\t"); | |
syncEncoderPng = chkAlpha.selected ? | |
by.blooddy.crypto.image.PNGEncoder.encode(bmp, 4) : | |
by.blooddy.crypto.image.PNG24Encoder.encode(bmp, 4); | |
break; | |
// ---- Async ---- | |
case 4: | |
txtOutput.appendText("PNGEncoder2:\t\t\t"); | |
asyncEncoder = PNGEncoder2.encodeAsync(bmp); | |
asyncEncoder.targetFPS = int(stage.frameRate); | |
break; | |
case 5: | |
txtOutput.appendText("BIT-101 encoder:\t\t"); | |
asyncEncoder = new mx.graphics.codec.PNGEncoderAsync(); | |
asyncEncoder.encode(bmp); | |
break; | |
case 6: | |
txtOutput.appendText("In-Spirit PNGEncoder:\t"); | |
asyncEncoder = new ru.inspirit.encoders.png.PNGEncoder(); | |
asyncEncoder.encodeAsync(bmp, !chkAlpha.selected, -1, ru.inspirit.encoders.png.PNGEncoder.FILTER_PAETH); | |
break; | |
} | |
if (syncEncoderPng != null) { | |
encoderComplete(syncEncoderPng, true); | |
} | |
else if (asyncEncoder != null) { | |
asyncEncoder.addEventListener(Event.COMPLETE, asyncEncoderComplete); | |
progress.source = asyncEncoder; | |
progress.visible = true; | |
} | |
} | |
private function asyncEncoderComplete(e : Event) | |
{ | |
var png : ByteArray; | |
if (e.target is PNGEncoder2) { | |
png = e.target.png; | |
} | |
else if (e.target is ru.inspirit.encoders.png.PNGEncoder) { | |
png = ru.inspirit.encoders.png.PNGEncoder.encodedPNGData; | |
} | |
else if (e.target is mx.graphics.codec.PNGEncoderAsync) { | |
png = e.target.png; | |
} | |
if (current == 4) { | |
pngToSave = png; | |
btnSave.enabled = true; | |
} | |
progress.visible = false; | |
encoderComplete(png, false); | |
} | |
private function encoderComplete(png : ByteArray, wasSync : Boolean) | |
{ | |
var end : int = getTimer(); | |
var ratio : Number = 1 - png.length / totalBytes(); | |
var percent : String = Math.round(ratio * 100) + "%"; | |
var size = (png.length / 1024.0).toFixed(0) + "KB"; | |
var fps = ""; | |
if (!wasSync && end - start > 0) { | |
fps = " " + int(frames / (end - start) * 1000) + "/" + int(stage.frameRate) + "FPS"; | |
} | |
txtOutput.appendText((end - start) + "ms " + percent + " (" + size + ")" + fps + "\n"); | |
txtOutput.scrollV = txtOutput.maxScrollV; | |
advance(); | |
} | |
private function end() | |
{ | |
current = -1; | |
txtOutput.appendText("\n-----------------------------\n\n"); | |
cboLevel.enabled = true; | |
chkAlpha.enabled = true; | |
btnBrowse.enabled = true; | |
btnGo.enabled = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment