<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>Visual-Experiments.com &#187; sop</title>
	<atom:link href="http://www.visual-experiments.com/tag/sop/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.visual-experiments.com</link>
	<description>ASTRE Henri experiments with Ogre3D and web stuff</description>
	<lastBuildDate>Mon, 16 Jan 2017 18:59:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>How to bypass WebGL Sop restriction v2</title>
		<link>http://www.visual-experiments.com/2011/12/05/how-to-bypass-webgl-sop-restriction-v2/</link>
		<comments>http://www.visual-experiments.com/2011/12/05/how-to-bypass-webgl-sop-restriction-v2/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 22:17:29 +0000</pubDate>
		<dc:creator>Henri</dc:creator>
				<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[restriction]]></category>
		<category><![CDATA[same-origin-policy]]></category>
		<category><![CDATA[sop]]></category>
		<category><![CDATA[webgl]]></category>
		<guid isPermaLink="false">http://www.visual-experiments.com/?p=2060</guid>
		<description><![CDATA[I&#8217;ve previously posted a trick to bypass the WebGL same origin policy restriction in a particular context. But my solution had a major drawback: it was slow due to jpeg decoding in javascript. Hopefully I&#8217;ve implemented a new solution which is way much faster thanks to John Bauman suggestion and it works like a charm! [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve previously posted a trick to <a href="http://www.visual-experiments.com/2011/10/03/how-to-bypass-webgl-sop-restriction/">bypass the WebGL same origin policy</a> restriction in a particular context. But my solution had a major drawback: it was slow due to jpeg decoding in javascript. Hopefully I&#8217;ve implemented a new solution which is way much faster thanks to <a href="http://www.visual-experiments.com/2011/10/03/how-to-bypass-webgl-sop-restriction/comment-page-1/#comment-8711">John Bauman suggestion</a> and it works like a charm! </p>
<pre class="brush: jscript; highlight: [10]; title: ;">
var xhr = new XMLHttpRequest();
xhr.open(&quot;GET&quot;, JPEG_URL, true);
xhr.responseType = &quot;arraybuffer&quot;;
xhr.onload = function() {
	var img = new Image();
	img.onload = function() {
		var tex = new THREE.Texture(this);
		//do something with this texture...
	};
	img.src = arrayBufferDataUri(xhr.response);
};
xhr.send(null);
</pre>
<p>The function &#8220;<strong>arrayBufferDataUri</strong>&#8221; at the line 10 allow to speed-up the conversion from ArrayBuffer to base64 ascii string by avoiding an extra copy needed to use <strong>window.btoa</strong>. I&#8217;ve found this function here: <a href="http://jsperf.com/encoding-xhr-image-data/5">http://jsperf.com/encoding-xhr-image-data/5</a>.</p>
<pre class="brush: jscript; title: ;">
//From: http://jsperf.com/encoding-xhr-image-data/5
function arrayBufferDataUri(raw) {
	var base64 = '';
	var encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
	var bytes = new Uint8Array(raw);
	var byteLength = bytes.byteLength;
	var byteRemainder = byteLength % 3;
	var mainLength = byteLength - byteRemainder;
	var a, b, c, d;
	var chunk;
	// Main loop deals with bytes in chunks of 3
	for (var i = 0; i &lt; mainLength; i = i + 3) {
		// Combine the three bytes into a single integer
		chunk = (bytes[i] &lt;&lt; 16) | (bytes[i + 1] &lt;&lt; 8 ) | bytes[i + 2];
		// Use bitmasks to extract 6-bit segments from the triplet
		a = (chunk &amp; 16515072) &gt;&gt; 18; // 16515072 = (2^6 - 1) &lt;&lt; 18
		b = (chunk &amp; 258048) &gt;&gt; 12;   // 258048   = (2^6 - 1) &lt;&lt; 12
		c = (chunk &amp; 4032) &gt;&gt; 6;      // 4032     = (2^6 - 1) &lt;&lt; 6
		d = chunk &amp; 63;               // 63       = 2^6 - 1
		// Convert the raw binary segments to the appropriate ASCII encoding
		base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d]
	}
	// Deal with the remaining bytes and padding
	if (byteRemainder == 1) {
		chunk = bytes[mainLength];
		a = (chunk &amp; 252) &gt;&gt; 2; // 252 = (2^6 - 1) &lt;&lt; 2
		// Set the 4 least significant bits to zero
		b = (chunk &amp; 3) &lt;&lt; 4;   // 3   = 2^2 - 1
		base64 += encodings[a] + encodings[b] + '==';
	}
	else if (byteRemainder == 2) {
		chunk = (bytes[mainLength] &lt;&lt; 8 ) | bytes[mainLength + 1];
		a = (chunk &amp; 16128) &gt;&gt; 8; // 16128 = (2^6 - 1) &lt;&lt; 8
		b = (chunk &amp; 1008) &gt;&gt; 4;  // 1008  = (2^6 - 1) &lt;&lt; 4
		// Set the 2 least significant bits to zero
		c = (chunk &amp; 15) &lt;&lt; 2;    // 15    = 2^4 - 1
		base64 += encodings[a] + encodings[b] + encodings[c] + '=';
	}
	return &quot;data:image/jpeg;base64,&quot; + base64;
}
</pre>
<p>I&#8217;ve only used this code for my <a href="http://www.visual-experiments.com/2011/10/03/three-js-photosynth-viewer-with-pictures-and-new-ui/">Google Chrome PhotoSynth WebGL extension</a>. This is why I don&#8217;t have added extra check for other browser (line 10: <em>xhr.mozResponseArrayBuffer || xhr.response</em>).</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.visual-experiments.com%2F2011%2F12%2F05%2Fhow-to-bypass-webgl-sop-restriction-v2%2F&amp;title=How%20to%20bypass%20WebGL%20Sop%20restriction%20v2"><img src="http://www.visual-experiments.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.visual-experiments.com/2011/12/05/how-to-bypass-webgl-sop-restriction-v2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to bypass WebGL Sop restriction</title>
		<link>http://www.visual-experiments.com/2011/10/03/how-to-bypass-webgl-sop-restriction/</link>
		<comments>http://www.visual-experiments.com/2011/10/03/how-to-bypass-webgl-sop-restriction/#comments</comments>
		<pubDate>Mon, 03 Oct 2011 16:54:59 +0000</pubDate>
		<dc:creator>Henri</dc:creator>
				<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[restriction]]></category>
		<category><![CDATA[same-origin-policy]]></category>
		<category><![CDATA[sop]]></category>
		<category><![CDATA[webgl]]></category>
		<guid isPermaLink="false">http://www.visual-experiments.com/?p=1969</guid>
		<description><![CDATA[Google Chrome and Firefox are blocking cross-domain image in WebGL (because of a security issue). This is really annoying when you want to display image from other domain in WebGL. To bypass this same-origin-policy restriction you have several options: Ask the other domain to add a CORS http header Use a proxy Use my trick [...]]]></description>
			<content:encoded><![CDATA[<p>Google Chrome and Firefox are blocking cross-domain image in WebGL (because of a <a href="http://www.contextis.com/resources/blog/webgl/">security issue</a>). This is really annoying when you want to display image from other domain in WebGL. To bypass this same-origin-policy restriction you have several options:</p>
<ul style="margin-left: 20px;">
<li>Ask the other domain to add a <a href="http://enable-cors.org/">CORS http header</a></li>
<li>Use a proxy</li>
<li>Use my trick <img src='http://www.visual-experiments.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </li>
</ul>
<p>Ok, to be honest<strong> my &#8220;trick&#8221; only work for browser extensions</strong> (tested with Google Chrome extension only). How is it working?</p>
<ul style="margin-left: 20px;">
<li>Add the domain in the list of allowed cross-domain ajax call in the manifest of the extension.</li>
<li>Do a binary ajax request on the jpeg</li>
<li>Parse the jpeg in javascript using <a href="https://github.com/notmasteryet/jpgjs">jpgjs</a></li>
<li>Fill the WebGL texture with the jpeg decoded in JS</li>
</ul>
<p>The drawbacks are that it can only be used by browser extensions and that JS jpeg decoding is slow compared to native decoding.</p>
<p><strong>I&#8217;ve used this workaround to add the pictures in my WebGL PhotoSynth Viewer extension.</strong></p>
<p style="font-size: 20px;color: red">I&#8217;ve posted a <a href="http://www.visual-experiments.com/2011/12/05/how-to-bypass-webgl-sop-restriction-v2/">new version</a> which is really much faster.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.visual-experiments.com%2F2011%2F10%2F03%2Fhow-to-bypass-webgl-sop-restriction%2F&amp;title=How%20to%20bypass%20WebGL%20Sop%20restriction"><img src="http://www.visual-experiments.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.visual-experiments.com/2011/10/03/how-to-bypass-webgl-sop-restriction/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
