benchmarks/assorted-dom/explanations/desaturate.html (80 lines of code) (raw):

<!DOCTYPE html> <html> <head> <meta charset=utf8> <!-- Copyright (C) 2007 Apple Inc. All rights reserved. Copyright (C) 2010 Mozilla Foundation Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --> <title>Kraken JavaScript Benchmark: Gaussian Blur</title> <link rel="stylesheet" href="../kraken.css"> <script> </script> <style> #display { border: 5px solid rgb(0,0,50);}</style> </head> <body> <div id="content"> <h2>Kraken JavaScript Benchmark: Desaturate</h2> <div id="results"> <p>This benchmark <a href="http://en.wikipedia.org/wiki/Colorfulness">desaturates</a> a photo using code from <a href="http://www.pixastic.com/">Pixastic</a>.</p> <p><small>Photo courtesy <a href="http://www.flickr.com/photos/katclay/4296523922/in/photostream/">Kat Clay</a> from Flickr</small>.</p> <img id="image" src="squid.png" width="400" height="267"> <canvas id="canvas" width="400" height="267"></canvas> <script> /* * Pixastic Lib - Desaturation filter - v0.1.1 * Copyright (c) 2008 Jacob Seidelin, jseidelin@nihilogic.dk, http://blog.nihilogic.dk/ * License: [http://www.pixastic.com/lib/license.txt] (MPL 1.1) */ var Pixastic = {}; Pixastic.Actions = {}; Pixastic.Actions.desaturate = { process : function(params) { var useAverage = !!(params.options.average && params.options.average != "false"); var data = params.data; var rect = params.options.rect; var w = rect.width; var h = rect.height; var p = w*h; var pix = p*4, pix1, pix2; if (useAverage) { while (p--) data[pix-=4] = data[pix1=pix+1] = data[pix2=pix+2] = (data[pix]+data[pix1]+data[pix2])/3 } else { while (p--) data[pix-=4] = data[pix1=pix+1] = data[pix2=pix+2] = (data[pix]*0.3 + data[pix1]*0.59 + data[pix2]*0.11); } return true; } } function desaturateTest() { var results = document.getElementById('blur-result'); results.innerHTML = "Running test..."; window.setTimeout(function() { var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); var img = document.getElementById('image') ctx.drawImage(img, 0, 0, img.width, img.height); var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height); var width = imgData.width, height = imgData.height; var data = imgData.data; var len = data.length; var startTime = (new Date()).getTime(); var params = { options: { rect: { width: width, height: height} }, data: imgData.data } Pixastic.Actions.desaturate.process(params); var finishTime = Date.now() - startTime; for (var i = 0; i < data.length; i++) { imgData.data[i] = data[i]; } //imgData.data = data; ctx.putImageData(imgData, 0, 0); results.innerHTML = "Finished: " + finishTime + "ms"; }, 10); } </script> <p><input type="button" value="Desaturate Test" onclick="desaturateTest();">&nbsp;<span id="blur-result"></span></p> </div> </div> </body> </html>