/**
 * cvi_filter_benchmark.js 1.2 (13-Jan-2011) (c) by Christian Effenberger. All Rights Reserved.
**/

var cvi_filter_benchmark = {
	run: function(image) {
		var canvas=document.createElement('canvas'); canvas.height=128; canvas.width=128; canvas.source=image;
		var source,src,buffer,ctx,context=canvas.getContext("2d"), prepared=(context.getImageData?true:false);
		var filter=[
			{f:'gamma', s:1.3 },	
			{f:'brightness', s:1.3},	
			{f:'contrast', s:1.3},	
			{f:'exposure', s:1.3},	
			{f:'invert'},	
			{f:'tritone'},	
			{f:'solarize'},	
			{f:'grayscale'},	
			{f:'sepia'},	
			{f:'threshold'},
			{f:'smooth', s:4},
			{f:'spinblur', s:8},
			{f:'zoomblur', s:16},
			{f:'motionblur', s:[12,0]},
			{f:'invertalpha'},
			{f:'mixrgb', s:[[255,0,255],[0,0,0]]},
			{f:'adjustrgba', s:[.5,1,1.5,1]},
			{f:'adjusthsba', s:[.5,1,1.5,1]},
			{f:'adjustyuva', s:[.5,1,1.5,1]},
			{f:'posterize', s:2},
			{f:'colorkey', s:[[76,110,141],[141,162,181]]},
			{f:'chromakey', s:[190,10,18,50,100]},
			{f:'outline', s:[1,0,'sobel']},
			{f:'convolve', m:[[ 1, 1, 0],[ 1, 1,-1],[ 0,-1,-1]], s:[1,0]},
			{f:'anaglyph'},
			{f:'alphamask'},
			{f:'multiplyalpha'},
			{f:'unmultiplyalpha'},
			{f:'tiltshift', s:[.5,.4,0,3]},
			{f:'stackblur', s:10}
		];
		var img=new Image();
		img.onload=function() {
			if(prepared&&(typeof cvi_filter!='undefined')&&filter!=null&&filter.length>0) {
				iw=Math.round(canvas.width); ih=Math.round(canvas.height);
				source=document.createElement('canvas'); source.height=ih+4; source.width=iw+4; src=source.getContext("2d");
				buffer=document.createElement('canvas'); buffer.height=ih; buffer.width=iw; ctx=buffer.getContext("2d");
				if(src&&ctx) {ctx.clearRect(0,0,iw,ih); ctx.drawImage(img,0,0,iw,ih); 
					src.clearRect(0,0,iw+4,ih+4); src.drawImage(img,0,0,iw+4,ih+4); src.drawImage(img,2,2,iw,ih); 
					var timer={d:null, t:null,
						start:function (){timer.d=new Date(); timer.t=timer.d.getTime();},
						stop: function (){timer.d=new Date(); return (timer.d.getTime()-timer.t);}
					}
					if(canvas.timer) {window.clearInterval(canvas.timer);} var r='',i=0,q=filter.length; 
					canvas.timer=window.setInterval(function() {
						timer.start(); cvi_filter.add(source,buffer,filter[i],iw,ih); r+=(timer.stop()/1000).toFixed(3)+'<br/>'; i++;
						if(i>=q) {window.clearInterval(canvas.timer); document.getElementById('benchmarks').innerHTML=r;}
					},20);
				}
			}
		}
		img.src=canvas.source;
	}
}

