<?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>I Never Finish Anythi &#187; manipulation</title>
	<atom:link href="http://ineverfinishanythi.com/tag/manipulation/feed/" rel="self" type="application/rss+xml" />
	<link>http://ineverfinishanythi.com</link>
	<description>&#34;There are two kinds of people, those who finish what they start and so on.&#34;</description>
	<lastBuildDate>Mon, 18 May 2009 15:19:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Tilt-Shift Effect in AS3/Flex</title>
		<link>http://ineverfinishanythi.com/2009/05/18/tilt-shift-effect-in-as3flex/</link>
		<comments>http://ineverfinishanythi.com/2009/05/18/tilt-shift-effect-in-as3flex/#comments</comments>
		<pubDate>Mon, 18 May 2009 14:26:37 +0000</pubDate>
		<dc:creator>nicholas</dc:creator>
				<category><![CDATA[flex]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[effect]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[manipulation]]></category>
		<category><![CDATA[tilt]]></category>
		<category><![CDATA[tilt-shift]]></category>

		<guid isPermaLink="false">http://ineverfinishanythi.com/?p=34</guid>
		<description><![CDATA[Most of us have seen tutorials on how to use Adobe Photoshop to create a tilt-shift effect by using a selection mask and a lens blur. I thought I’d try to reproduce the effect using AS3. First off, a demo () for the people who want to see if this is even worth the time [...]]]></description>
			<content:encoded><![CDATA[<p>Most of us have seen tutorials on how to use Adobe Photoshop to create a tilt-shift effect by using a selection mask and a lens blur. I thought I’d try to reproduce the effect using AS3.<br />
First off, a <a href="http://ineverfinishanythi.com/flex/tiltshift/tiltShift.html">demo</a> (<a href="http://ineverfinishanythi.com/flex/tiltshift/tiltShift.html" target="_blank"><img class="alignnone size-full wp-image-53" title="Open in a New Window" src="http://ineverfinishanythi.com/infa/wp-content/uploads/newwindow.png" alt="Open in a New Window" width="22" height="22" /></a>) for the people who want to see if this is even worth the time to read. <img src='http://ineverfinishanythi.com/infa/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  View source is enabled, if you want the code. The top slider controls the size of the focus area, the second slider controls the location of the focus area, and the checkbox controls whether or not to boost the saturation (because really bright colors look more fake).<br />
Now that you’ve played with it, I’ll begin explaining what’s going on <em>in</em> the code.<br />
<span id="more-34"></span><br />
Note: This code uses Grant Skinner’s ColorMatrix class, which you can get <a href="http://www.gskinner.com/blog/archives/2007/12/colormatrix_upd.html">here</a> (<a href="http://www.gskinner.com/blog/archives/2007/12/colormatrix_upd.html" target="_blank"><img class="alignnone size-full wp-image-53" title="Open in a New Window" src="http://ineverfinishanythi.com/infa/wp-content/uploads/newwindow.png" alt="Open in a New Window" width="22" height="22" /></a>).</p>
<pre class="brush: as3;">
private var original:BitmapData; // class-level variable
var picBMD:BitmapData;
var alphaMask:BitmapData = null;
var mask:Sprite = new Sprite();
var m:Matrix = new Matrix();
var focusSize:int = hsFocusSize.value;
var focusLoc:int = hsFocusLoc.value;

if (original == null) {
	original = (pic.content as Bitmap).bitmapData.clone();
}
picBMD = original.clone();

picBMD.applyFilter(picBMD, picBMD.rect, picBMD.rect.topLeft, new BlurFilter(5, 5, 3));

m.createGradientBox(picBMD.width, picBMD.height * (focusSize / 100), 90 / (180 / Math.PI), 0, picBMD.height * ((focusLoc - focusSize) / 100));
mask.graphics.beginGradientFill(&quot;linear&quot;, new Array(0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF), new Array(0, 1, 1, 0), new Array(0, 85, 170, 255), m);
mask.graphics.drawRect(0, 0, picBMD.width, picBMD.height);
alphaMask = new BitmapData(picBMD.width, picBMD.height, true, 0xFFFFFF);
alphaMask.draw(mask);

picBMD.copyPixels(original, original.rect, original.rect.topLeft, alphaMask, alphaMask.rect.topLeft, true);

if (chkSaturate.selected) {
	var cm:ColorMatrix = new ColorMatrix();
	cm.adjustSaturation(20);
	picBMD.applyFilter(picBMD, picBMD.rect, picBMD.rect.topLeft, new ColorMatrixFilter(cm));
}

(pic.content as Bitmap).bitmapData = picBMD;
</pre>
<p>
Lines 1–7: Variable declaration. <code>original</code> is a class-level variable that holds the unaltered version of the bitmapData. <code>focusSize</code> is how big an area to focus and <code>focusLoc</code> is where on the y-axis to position the focus. Both of these are percentages.</p>
<p>Lines 9–12: If we haven’t stored the unaltered bitmapData yet, do it now. Clone that data to use in our filter.</p>
<p>Line 14: Normally, the tutorials call for a Lens Blur. We’ll settle for just doing a 5x5 blur on the entire image.</p>
<p>Line 16: Use our matrix to create a gradient box that is the same width as the image and the height as we specified with our <code>focusSize</code> percentage. The <code>90 / (180 / Math.PI)</code> is just saying that we want our gradient to direct itself vertically at a 90 degree angle (converting 90 degrees to radians). Because the gradient is vertical, we use 0 for <code>tx</code> because it doesn’t matter where we position it horizontally. <code>ty</code> is set to the location specified by <code>focusLoc</code>.</p>
<p>Line 17: Begin our gradient fill in the <code>mask</code> sprite. We have to use a sprite because <code>BitmapData</code> objects don’t have <code>graphics</code> properties. The fill should be linear, white (0xFFFFFF) and transparent on the edges. <code>new Array(0, 85, 170, 255)</code> means that the gradient colors and transparency levels should begin drawing at 0 and change to the next set at 85, where 0–255 is a scale like 0–100%.</p>
<p>Line 18: Draw the gradient onto the sprite.</p>
<p>Lines 19–20: Set up our alphaMask to be the same size as our image and draw the contents of our sprite onto it.</p>
<p>Line 22: This is the actual effect, now. Copy the pixels from the original image, using the transparency mask, onto our blurred image. This will cause the image to go from blurred to clear using the gradient we created.</p>
<p>Lines 24–28: If we’re over-saturating the image, create a new <a href="http://www.gskinner.com/blog/archives/2007/12/colormatrix_upd.html">ColorMatrix</a> (<a hred="http://www.gskinner.com/blog/archives/2007/12/colormatrix_upd.html" target="_blank"><img class="alignnone size-full wp-image-53" title="Open in a New Window" src="http://ineverfinishanythi.com/infa/wp-content/uploads/newwindow.png" alt="Open in a New Window" width="22" height="22" /></a>), courtesy of Grant Skinner, and adjust the saturation to boost by 20. This will make all of the colors look a little too bright (and fake, like a model). Apply the filter as normal.</p>
<p>Line 30: Set our image to use the filtered <code>BitmapData</code>.
</p>
<p>
That’s all there is to it. A nice, little effect and not too difficult.</p>
]]></content:encoded>
			<wfw:commentRss>http://ineverfinishanythi.com/2009/05/18/tilt-shift-effect-in-as3flex/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
