<?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>Stoney's Zone &#187; Computer Graphics</title>
	<atom:link href="http://stoney.sb.org/wordpress/category/computer-graphics/feed/" rel="self" type="application/rss+xml" />
	<link>http://stoney.sb.org/wordpress</link>
	<description>Thoughts about stuff</description>
	<lastBuildDate>Thu, 24 Dec 2009 16:49:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>2D Matrix Decomposition</title>
		<link>http://stoney.sb.org/wordpress/2009/07/2d-matrix-decomposition/</link>
		<comments>http://stoney.sb.org/wordpress/2009/07/2d-matrix-decomposition/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 22:29:27 +0000</pubDate>
		<dc:creator>stoney</dc:creator>
				<category><![CDATA[Computer Graphics]]></category>
		<category><![CDATA[Mathematica]]></category>

		<guid isPermaLink="false">http://stoney.sb.org/wordpress/?p=114</guid>
		<description><![CDATA[This demonstrates the polar decomposition of 2D matrices into their angle, scale, and shear components. The polarDecomp algorithm is taken from Shoemake &#38; Duff &#8220;Matrix Animation and Polar Decomposition&#8220;, 1992. I found that the formula for the polar decomposition, on page 3, produces rotation matrices that don&#8217;t account for diagonal axis flipping (swapping x with [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://stoney.sb.org/mm/2dMatrixDecomposition.html" target="_blank">This</a> demonstrates the polar decomposition of 2D matrices into their angle, scale, and shear components. The polarDecomp algorithm is taken from Shoemake &amp; Duff &#8220;<a href="http://www.cs.wisc.edu/graphics/Courses/838-s2002/Papers/polar-decomp.pdf" target="_blank">Matrix Animation and Polar Decomposition</a>&#8220;, 1992.</p>
<p><span id="more-114"></span></p>
<p>I found that the formula for the polar decomposition, on page 3, produces rotation matrices that don&#8217;t account for diagonal axis flipping (swapping x with y). The article seems to factor out the flipping, but I didn&#8217;t want to have a fourth component. Removing the sign(det(M)) factor eliminated this problem by incorporating the flipping into the rotation matrix.</p>
<p>It&#8217;s very desirable to decompose a matrix generated by {angle, scale, shear} back into the same part values, but this happens only when scale is positive and shear is zero. If not, the rotation is affected. At least, the parts are stable once derived from the first matrix. This may help in UI, and certainly in interpolation.</p>
<p><a href="http://stoney.sb.org/mm/2dMatrixDecomposition.html" target="_blank">This page</a> is a Mathematica-generated HTML file, with CSS that I tweaked. Unfortunately, it shows Mathematica code as images, to preserve the original appearance. The original Mathematica notebook is <a href="http://stoney.sb.org/mm/2dMatrixDecomposition.nb" target="_blank">here</a>. If you don&#8217;t have Mathematica, you can view it using the free <a href="http://www.wolfram.com/products/player/" target="_blank">Mathematica Player</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://stoney.sb.org/wordpress/2009/07/2d-matrix-decomposition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pixel-perfect Graphics, Revisited</title>
		<link>http://stoney.sb.org/wordpress/2008/11/pixel-perfect-graphics-revisited/</link>
		<comments>http://stoney.sb.org/wordpress/2008/11/pixel-perfect-graphics-revisited/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 23:43:21 +0000</pubDate>
		<dc:creator>stoney</dc:creator>
				<category><![CDATA[Computer Graphics]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[Mathematica]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://stoney.sb.org/wordpress/?p=60</guid>
		<description><![CDATA[Mathematica 7 introduces a new Image function that eliminates all the hassle previously necessary to create and export pixel-perfect raster images. Instead of using ArrayPlot to generate the image, I can now do this: myImage = Image[imageData, ColorSpace->RGBColor] I still need to specify RGBColor to interpret the alpha channel correctly, but the option to Image [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.wolfram.com/products/mathematica/index.html">Mathematica 7</a> introduces a new <a href="http://reference.wolfram.com/mathematica/ref/Image.html?q=Image&#038;lang=en">Image</a> function that eliminates all the hassle <a href="http://stoney.sb.org/wordpress/?p=50">previously necessary</a> to create and export pixel-perfect raster images.</p>
<p><span id="more-60"></span></p>
<p>Instead of using <a href="http://reference.wolfram.com/mathematica/ref/ArrayPlot.html?q=ArrayPlot&#038;lang=en">ArrayPlot</a> to generate the image, I can now do this:</p>
<p>
<pre>
myImage = Image[imageData, ColorSpace->RGBColor]
</pre>
<p><img src="http://stoney.sb.org/wordpress/wp-content/myimage.png" alt="myImage.png" border="0" width="256" height="128" />
</p>
<p>I still need to specify <a href="http://reference.wolfram.com/mathematica/ref/RGBColor.html">RGBColor</a> to interpret the alpha channel correctly, but the option to Image is named <a href="http://reference.wolfram.com/mathematica/ref/ColorSpace.html?q=ColorSpace&#038;lang=en">ColorSpace</a>, instead of the <a href="">ColorFunction</a> used by ArrayPlot.</p>
<p>Exporting works the same way as before:</p>
<pre>
Export["~/Desktop/myImage.png", myImage]
</pre>
</p>
<p>Except that now I can directly export a png with alpha. Much simpler overall in the new Mathematica.</p>
]]></content:encoded>
			<wfw:commentRss>http://stoney.sb.org/wordpress/2008/11/pixel-perfect-graphics-revisited/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is a sampling test image?</title>
		<link>http://stoney.sb.org/wordpress/2008/09/what-is-a-sampling-test-image/</link>
		<comments>http://stoney.sb.org/wordpress/2008/09/what-is-a-sampling-test-image/#comments</comments>
		<pubDate>Mon, 08 Sep 2008 19:45:52 +0000</pubDate>
		<dc:creator>stoney</dc:creator>
				<category><![CDATA[Computer Graphics]]></category>
		<category><![CDATA[Computers]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://stoney.sb.org/wordpress/?p=57</guid>
		<description><![CDATA[I was asked what the purpose is of the test image in my previous post, and why the alpha was set to 0.01 rather than 0 in alternating pixels. The answer to the second question is simpler: pixel manipulation code often zeros the RGB channels of a pixel when it sees that the alpha is [...]]]></description>
			<content:encoded><![CDATA[<p>I was asked what the purpose is of the test image in my <a href="http://stoney.sb.org/wordpress/?p=50">previous post</a>, and why the alpha was set to 0.01 rather than 0 in alternating pixels.<br />
<span id="more-57"></span>The answer to the second question is simpler: pixel manipulation code often zeros the RGB channels of a pixel when it sees that the alpha is zero, which could interfere with the test.</p>
<p>The answer to the first question is more complicated as it requires some background. Combining pixels by sampling, blurring, or compositing them generally requires the pixels to be <em>premultiplied</em> first. If this isn&#8217;t done, then the result can show incorrect colors, halos, and other artifacts.</p>
<p>For example, this test image has alternating pixels of {1.0, 0.0, 0.0, 0.01} and {0.0, 0.0, 0.0, 1.0}, read as {Red, Green, Blue, Alpha}:</p>
<p><img src="http://stoney.sb.org/wordpress/wp-content/premult-test.png" alt="premult-test.png" border="0" width="128" height="128" /></p>
<p>When blurred in Adobe After Effects CS3, using the included directional blur filter set to 3 degrees, we get this:</p>
<p><img src="http://stoney.sb.org/wordpress/wp-content/dir-blur-error.png" alt="dir-blur-error.png" border="0" width="128" height="128" /></p>
<p>You can easily see that the blur did not premultiply the pixels before sampling them. The red channel has leaked out from the transparent pixels and polluted the image.</p>
<p>We can use a simple <em>average</em> operation to show why this happens. Note that we are starting with <em>straight</em> pixels, where the alpha channel is treated as a mask independent of the RGB channels.</p>
<p>The quick-but-wrong way to average two straight pixels is to simply add their alpha, red, green, and blue channels independently and divide the result values by two. When averaging two adjacent pixels in our test image, we would add a red value of 1 to a red value of 0, resulting in 0.5 after the division. Similarly, the alpha values average to 0.5005. The result is a reddish, half-transparent pixel. Since a blur is just a complicated average, that&#8217;s what happened in our test.</p>
<p>The right way to average straight pixels is to first multiply each of the color channels by the alpha value, then do the average. Afterwards, you have to divide the result color channels by the result alpha to produce straight pixels again. In our example, we should average a red value of 1 x 0.01 with one of 0 x 1. The result is a red value of 0.005 and an alpha of 0.5005 (as before). This is a near-black, half-transparent color, and is what we expect visually. The final, straightened red value is 0.005 / 0.5005, or 0.00999.</p>
<p>In general, premultiplying pixels before combining them, and straightening them afterwards, accounts for most of the time spent in these operations. Time that is actually wasted on fully-opaque pixels, where premultiplication has no effect.</p>
<p>This is a surprisingly common problem in practice. I&#8217;ve seen a commercial compositing application that failed to premultiply with any pixel-combining operations. The reaction that I got when I explained the need for premultiplication to the developers showed that they really didn&#8217;t understand the problem, and that they didn&#8217;t see it in practice. I suspect that this would show up mostly on feathered edges of layered images, and would either be mistaken for noise in the original image, or won&#8217;t be recognized as a bug since they weren&#8217;t looking for it.</p>
<p>It takes extra code and complexity to combine straight pixels properly, and to avoid unnecessary premultiplication and straightening, but it doesn&#8217;t matter how fast an implementation is if it produces the wrong result.</p>
<p>For more information on this topic, see the Wikipedia article on <a href="http://en.wikipedia.org/wiki/Alpha_compositing">Alpha Compositing</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://stoney.sb.org/wordpress/2008/09/what-is-a-sampling-test-image/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exporting Pixel-Perfect Graphics in Mathematica</title>
		<link>http://stoney.sb.org/wordpress/2008/09/exporting-pixel-perfect-graphics-in-mathematica/</link>
		<comments>http://stoney.sb.org/wordpress/2008/09/exporting-pixel-perfect-graphics-in-mathematica/#comments</comments>
		<pubDate>Tue, 02 Sep 2008 19:46:06 +0000</pubDate>
		<dc:creator>stoney</dc:creator>
				<category><![CDATA[Computer Graphics]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[Mathematica]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://stoney.sb.org/wordpress/?p=50</guid>
		<description><![CDATA[I keep having to figure out how to export pixel-perfect raster images from Mathematica, which I use for test images with my software. I found that it&#8217;s simpler to use ArrayPlot than Raster now, so I&#8217;m documenting this for my future use (and any one else&#8217;s). Here&#8217;s a simple image function that generates a grid [...]]]></description>
			<content:encoded><![CDATA[<p>I keep having to figure out how to export pixel-perfect raster images from Mathematica, which I use for test images with my software. I found that it&#8217;s simpler to use ArrayPlot than Raster now, so I&#8217;m documenting this for my future use (and any one else&#8217;s).<br />
<span id="more-50"></span>Here&#8217;s a simple image function that generates a grid of black pixels alternating with nearly-transparent red pixels. I use this sort of image to ensure that I&#8217;m sampling correctly.</p>
<pre>
imageFunction[x_, y_] :=
 If[BitAnd[x, 1] == BitAnd[y, 1],
  {0, 0, 0, 1},
  {1, 0, 0, 0.01}]
</pre>
<p>Table then makes a 256 x 128 array of RGBA values from the image function. This is non-square for this example to ensure that the width and height are in the right order later.</p>
<pre>
imageData = Table[
   imageFunction[x, y],
   {y, 0, 127}, {x, 0, 255}];
</pre>
<p>ArrayPlot makes the pixel array viewable by converting it into a Graphics. The ColorFunction produces RGBA pixels (the default ignores alpha), PixelConstrained aligns the image to exact pixel boundaries, and ImageSize produces a result that&#8217;s exactly the right size.</p>
<pre>
apic = ArrayPlot[
  imageData,
  ColorFunction -> RGBColor,
  PixelConstrained -> True,
  ImageSize -> Reverse[Take[Dimensions[imageData], 2]]]
</pre>
<p>This produces the image:</p>
<p>
<img src="http://stoney.sb.org/wordpress/wp-content/apic.png" alt="APic.png" border="0" width="256" height="128" />
</p>
<p>Which is then exported like this:</p>
<pre>
Export["~/Desktop/APic.tif", apic];
</pre>
<p>The result is a pixel-perfect file resulting from a raster image defined in Mathematica.</p>
]]></content:encoded>
			<wfw:commentRss>http://stoney.sb.org/wordpress/2008/09/exporting-pixel-perfect-graphics-in-mathematica/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
