<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Dan Byström's Bwain</title>
	<atom:link href="http://danbystrom.se/feed/" rel="self" type="application/rss+xml" />
	<link>http://danbystrom.se</link>
	<description>Blog without an interesting name</description>
	<lastBuildDate>Mon, 09 Jan 2012 08:26:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='danbystrom.se' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Dan Byström's Bwain</title>
		<link>http://danbystrom.se</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://danbystrom.se/osd.xml" title="Dan Byström&#039;s Bwain" />
	<atom:link rel='hub' href='http://danbystrom.se/?pushpress=hub'/>
		<item>
		<title>factor10 is the future</title>
		<link>http://danbystrom.se/2010/12/23/factor10-is-the-future/</link>
		<comments>http://danbystrom.se/2010/12/23/factor10-is-the-future/#comments</comments>
		<pubDate>Thu, 23 Dec 2010 12:22:41 +0000</pubDate>
		<dc:creator>Dan Byström</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://danbystrom.se/?p=343</guid>
		<description><![CDATA[After running my own company, Visual Design Softscape AB, for over 15 years, I&#8217;ve decided that it&#8217;s about time to take a step forward. As of 1st January 2011, I&#8217;ll be joining the guys at factor10 full-time. I really look forward to have inspiring colleagues to help improve my work &#8211; and hopefully that will work both [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=343&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>After running my own company, Visual Design Softscape AB, for over 15 years, I&#8217;ve decided that it&#8217;s about time to take a step forward.</p>
<p>As of 1st January 2011, I&#8217;ll be joining the guys at <a href="http://www.factor10.com">factor10</a> full-time.</p>
<p>I really look forward to have inspiring colleagues to help improve my work &#8211; and hopefully that will work both ways. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/danbystrom.wordpress.com/343/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/danbystrom.wordpress.com/343/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/danbystrom.wordpress.com/343/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/danbystrom.wordpress.com/343/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/danbystrom.wordpress.com/343/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/danbystrom.wordpress.com/343/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/danbystrom.wordpress.com/343/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/danbystrom.wordpress.com/343/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/danbystrom.wordpress.com/343/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/danbystrom.wordpress.com/343/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/danbystrom.wordpress.com/343/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/danbystrom.wordpress.com/343/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/danbystrom.wordpress.com/343/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/danbystrom.wordpress.com/343/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=343&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://danbystrom.se/2010/12/23/factor10-is-the-future/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d518503a8b459ab2066d1623872e94bd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">danbystrom</media:title>
		</media:content>
	</item>
		<item>
		<title>Brev till Tele2</title>
		<link>http://danbystrom.se/2010/11/28/brev-till-tele2/</link>
		<comments>http://danbystrom.se/2010/11/28/brev-till-tele2/#comments</comments>
		<pubDate>Sun, 28 Nov 2010 19:25:01 +0000</pubDate>
		<dc:creator>Dan Byström</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://danbystrom.se/?p=338</guid>
		<description><![CDATA[Växjö 2010-11-14 Snälla, Sluta skicka fakturor för ett abonnemang som är uppsagt sedan fem månader tillbaka! Jag har varit i kontakt med er flera gånger om detta och varje gång lovar ni att det ska sluta, men nästa månad trillar det ändå in en ny faktura. Jag bifogar kopia av en chat där operatören bekräftar [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=338&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:right;">Växjö 2010-11-14</p>
<h2 style="text-align:left;">Snälla,</h2>
<p>Sluta skicka fakturor för ett abonnemang som är uppsagt sedan fem månader tillbaka!</p>
<p>Jag har varit i kontakt med er flera gånger om detta och varje gång lovar ni att det ska sluta, men nästa månad trillar det ändå in en ny faktura. Jag bifogar kopia av en chat där operatören bekräftar att fakturorna är helt felaktiga och där hon lovar att detta ska upphöra. Något som alltså inte skett.</p>
<p>Abonnemanget blev jag tvungen att säga upp efter att i fyra månader (och otaliga timmars väntan i telefonköer) väntat på att få mitt gamla telefonnummer porterat (som skulle tagit tre veckor). Svaret var alltid bara ”det måste göras en ny felanmälan”.</p>
<p>När jag slutligen sa upp abonnemanget blev jag lovad återbetalning för de fyra månaderna. Någon återbetalning har jag inte sett till. Däremot fortsätter ni som sagt att trakassera mig med en ny faktura varje månad, som jag får kontakta er om (med ca 40 minuters väntan varje gång, först tjugo minuter för supporten och sedan vidarekoppling till ekonomiavdelningen som tar tjugo minuter till).</p>
<p>Återbetalning kan göras till:</p>
<p>Handelsbanken<br />
Clearingnummer 6879<br />
Bankkonto: xxx xxx xxx</p>
<p>Jag förväntar mig inte bara återbetalning, utan även skälig kompensation för alla de timmar jag suttit i er telefonkö och för att ni nu helt omotiverat lämnat ärendet till inkasso.</p>
<p>Med förhoppning om att detta ärende nu nått sitt slut,</p>
<p>Dan Byström</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/danbystrom.wordpress.com/338/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/danbystrom.wordpress.com/338/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/danbystrom.wordpress.com/338/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/danbystrom.wordpress.com/338/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/danbystrom.wordpress.com/338/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/danbystrom.wordpress.com/338/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/danbystrom.wordpress.com/338/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/danbystrom.wordpress.com/338/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/danbystrom.wordpress.com/338/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/danbystrom.wordpress.com/338/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/danbystrom.wordpress.com/338/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/danbystrom.wordpress.com/338/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/danbystrom.wordpress.com/338/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/danbystrom.wordpress.com/338/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=338&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://danbystrom.se/2010/11/28/brev-till-tele2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d518503a8b459ab2066d1623872e94bd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">danbystrom</media:title>
		</media:content>
	</item>
		<item>
		<title>WTF???</title>
		<link>http://danbystrom.se/2009/11/30/wtf/</link>
		<comments>http://danbystrom.se/2009/11/30/wtf/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 10:21:19 +0000</pubDate>
		<dc:creator>Dan Byström</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://danbystrom.se/2009/11/30/wtf/</guid>
		<description><![CDATA[Ekeforshus<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=331&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://translate.google.se/#sv|en|tidsbokning"><img class="aligncenter size-full wp-image-330" title="Google Translate" src="http://danbystrom.files.wordpress.com/2009/11/googletranslate.jpg" alt="" width="675" height="353" /></a>  <a href="http://www.vdsoft.se"><span style="color:#ffffff;">Ekeforshus</span></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/danbystrom.wordpress.com/331/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/danbystrom.wordpress.com/331/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/danbystrom.wordpress.com/331/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/danbystrom.wordpress.com/331/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/danbystrom.wordpress.com/331/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/danbystrom.wordpress.com/331/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/danbystrom.wordpress.com/331/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/danbystrom.wordpress.com/331/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/danbystrom.wordpress.com/331/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/danbystrom.wordpress.com/331/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/danbystrom.wordpress.com/331/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/danbystrom.wordpress.com/331/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/danbystrom.wordpress.com/331/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/danbystrom.wordpress.com/331/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=331&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://danbystrom.se/2009/11/30/wtf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d518503a8b459ab2066d1623872e94bd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">danbystrom</media:title>
		</media:content>

		<media:content url="http://danbystrom.files.wordpress.com/2009/11/googletranslate.jpg" medium="image">
			<media:title type="html">Google Translate</media:title>
		</media:content>
	</item>
		<item>
		<title>Thumbnails with glass table reflection in GDI+</title>
		<link>http://danbystrom.se/2009/01/12/thumbnails-with-glass-table-reflection-in-gdi/</link>
		<comments>http://danbystrom.se/2009/01/12/thumbnails-with-glass-table-reflection-in-gdi/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 09:04:47 +0000</pubDate>
		<dc:creator>Dan Byström</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[GDI+]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://danbystrom.se/?p=260</guid>
		<description><![CDATA[I&#8217;ve been playing around with image processing lately and since my last post about loading thumbnail images from files I couldn&#8217;t help myself from trying to roll my own &#8220;Web 2.0 reflection effect&#8221; directly in .NET 2.0 with no 3D support whatsoever. Actually, I think was more inspired by Windows Vista&#8217;s thumbnails (to the right) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=260&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-294" title="vistathumbnails" src="http://danbystrom.files.wordpress.com/2009/01/vistathumbnails.jpg" alt="vistathumbnails" width="160" height="112" />I&#8217;ve been playing around with image processing lately and since my last post about loading thumbnail images from files I couldn&#8217;t help myself from trying to roll my own &#8220;Web 2.0 reflection effect&#8221; directly in .NET 2.0 with no 3D support whatsoever. Actually, I think was more inspired by Windows Vista&#8217;s thumbnails (to the right) than the web.</p>
<p>This is what I eventually came up with:</p>
<p><img class="alignnone size-full wp-image-281" title="reflectionsamples1" src="http://danbystrom.files.wordpress.com/2009/01/reflectionsamples1.jpg" alt="reflectionsamples1" width="695" height="241" /></p>
<p>Although this is all easy &#8211; since there were a few things that couldn&#8217;t be done in &#8220;pure&#8221; GDI+ and then some uncommon approaches involved in my solution, I think that there may be some people out there who don&#8217;t find this totally trivial. So I thought that it might be worth writing this down.</p>
<p>From the original picture I work through four steps:</p>
<p><img class="alignnone size-full wp-image-287" title="reflectionsteps2" src="http://danbystrom.files.wordpress.com/2009/01/reflectionsteps2.jpg" alt="reflectionsteps2" width="393" height="288" /></p>
<p><span style="font-size:x-large;">1.</span> The first step merely shrinks the original picture to the desired size and puts a frame around it. This is trivial:</p>
<p><pre class="brush: csharp;">
	protected virtual Bitmap createFramedBitmap( Bitmap bmpSource, Size szFull )
	{
		Bitmap bmp = new Bitmap( szFull.Width, szFull.Height );
		using ( Graphics g = Graphics.FromImage( bmp ) )
		{
			g.FillRectangle( FrameBrush, 0, 0, szFull.Width, szFull.Height );
			g.DrawRectangle( BorderPen, 0, 0, szFull.Width - 1, szFull.Height - 1 );
			g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
			g.DrawImage(
				bmpSource,
				new Rectangle( FrameWidth, FrameWidth, szFull.Width - FrameWidth * 2, szFull.Height - FrameWidth * 2 ),
				new Rectangle( Point.Empty, bmpSource.Size ),
				GraphicsUnit.Pixel );
		}
		return bmp;
	}
</pre></p>
<p>(Note the InterpolationMode property. It is important in order to resize the image with good quality!)</p>
<p><span style="font-size:x-large;">2.</span> The second step is substantially more involved. It takes the result from step 1 and does this, all in one go:</p>
<ol>
<li>Flip the image upside down (omitting the upper and lower parts of frame, since we don&#8217;t want them to be present in the reflection).</li>
<li>Apply a <a href="http://en.wikipedia.org/wiki/Gaussian_blur">Gaussian blur</a> convolution effect to make the image look&#8230;, well, blurred&#8230; <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </li>
<li>Wash out some color to make the reflection a little bit grayish.</li>
<li>Apply an alpha blend fall out.</li>
</ol>
<p>Flipping the image and the color wash-out can both be done directly in GDI+. Flip either with Bitmap.RotateFlip or with a transformation matrix and use a ColorMatrix to alter the colors. But since neither a blur effect nor an alpha blend can be done without direct pixel manipulation I did it all in one go. For the blur effect, see Christian Graus excellent article series <a href="http://www.codeproject.com/KB/GDI-plus/csharpfilters.aspx">Image Processing for Dummies with C# and GDI+</a>. I&#8217;ve blogged earlier on how to perform alpha blending previously by drawing the blend using a PathGradientBrush or a LinearGradientBrush in <a href="http://danbystrom.se/2008/08/24/soft-edged-images-in-gdi/">Soft edged images in GDI+</a>. This time I will calculate the alpha value instead. The calculation is done once for every scan line and is located in a virtual function so that this formula can be overridden.</p>
<p>All four &#8220;effects&#8221; are handled in this loop:</p>
<p><pre class="brush: csharp;">
	for ( int y = height-1 ; y &gt;= 0 ; y-- )
	{
		byte alpha = (byte)(255 * calculateAlphaFallout( (double)(height - y) / height ));
		Pixel* pS = (Pixel*)bdS.Scan0.ToPointer() + bdS.Width * (bdS.Height - y - FrameWidth - 1);
		Pixel* pD = (Pixel*)bdD.Scan0.ToPointer() + bdD.Width * y;
		for ( int x = bdD.Width ; x &gt; 0 ; x--, pD++, pS++ )
		{
			int R = gaussBlur( &amp;pS-&gt;R, nWidthInPixels );
			int G = gaussBlur( &amp;pS-&gt;G, nWidthInPixels );
			int B = gaussBlur( &amp;pS-&gt;B, nWidthInPixels );
			pD-&gt;R = (byte)((R * 3 + G * 2 + B * 2) / 7);
			pD-&gt;G = (byte)((R * 2 + G * 3 + B * 2) / 7);
			pD-&gt;B = (byte)((R * 2 + G * 2 + B * 3) / 7);
			pD-&gt;A = alpha;
		}
	}
</pre></p>
<p>Flipping happens on line 4, blurring on lines 8-10 (the gaussBlur function is just a one-liner). Color wash-out is done on lines 11-13 and the alpha fall-out on lines 3 and 14.</p>
<p>The very observant reader will notice that I let the blurring wrap from one edge to another. This is a hack, but it works since the left and right edges are always exactly identical. In production code, it might be a good idea to make the blurring optional (or even to provide a user-defined convolution matrix) and also to do the same from the color wash-out which currently uses hard-coded values.</p>
<p><span style="font-size:x-large;">3.</span> Create the &#8220;half sheared&#8221; bitmap:</p>
<p>This transform cannot be accomplished using a linear transformation, but (after taking quite a a detour on this) I realized that it can be done embarrassingly simple:</p>
<p><pre class="brush: csharp;">
	using ( Graphics g = Graphics.FromImage( Thumbnail ) )
		for ( int x = 0 ; x &lt; sz.Width ; x++ )
			g.DrawImage(
				bmpFramed,
				new RectangleF( x, 0, 1, sz.Height - Skew * (float)(sz.Width - x) / sz.Width ),
				new RectangleF( x, 0, 1, sz.Height ),
				GraphicsUnit.Pixel );
</pre></p>
<p>I simply use DrawImage to draw each column by its own, transferring one column from the framed image from step 1 to a column of different height. Note that it is extremely important that we pass <strong>float</strong>s and not <strong>int</strong>s &#8211; in the latter case the result will be a disaster.</p>
<p><span style="font-size:x-large;">4.</span> Draw the reflection image through a shear transform, like this:</p>
<p><pre class="brush: csharp;">
	using ( Graphics g = Graphics.FromImage( Thumbnail ) )
	{
		System.Drawing.Drawing2D.Matrix m = g.Transform;
		m.Shear( 0, (float)Skew / sz.Width);
		m.Translate( 0, sz.Height - Skew - 1 );
		g.Transform = m;
		g.DrawImage( bmpReflection, Point.Empty );
	}
 </pre></p>
<p><a href="http://www.visual-design.se/blog/reflectedthumbnaildemo.zip">Download demo source code</a></p>
<h2>A Lesson Learned</h2>
<p>At first, I tried to do the shearing in both step 3 &amp; 4 myself using code similar to this (still one column at a time):</p>
<p><pre class="brush: csharp;">
	// this was a bad idea
	private static void paintRowWithResize(
		BitmapData bdDst,
		BitmapData bdSrc,
		int nDstColumn,
		int nSrcColumn,
		int nDstRow,
		double dblSrcRow,
		int nRows,
		double dblStep )
	{
		unchecked
		{
			unsafe
			{
				Pixel* pD = (Pixel*)bdDst.Scan0.ToPointer() + nDstColumn + nDstRow * bdDst.Width;
				Pixel* pS = (Pixel*)bdSrc.Scan0.ToPointer() + nSrcColumn;
				while ( nRows -- &gt; 0 )
				{
					int nYSrc = (int)dblSrcRow;
					Pixel p1 = pS[nYSrc * bdSrc.Width];
					Pixel p2 = p2 = pS[(nYSrc + 1) * bdSrc.Width];
					double frac2 = dblSrcRow - nYSrc;
					double frac1 = 1.0 - frac2;
					pD-&gt;R = (byte)(p1.R * frac1 + p2.R * frac2);
					pD-&gt;G = (byte)(p1.G * frac1 + p2.G * frac2);
					pD-&gt;B = (byte)(p1.B * frac1 + p2.B * frac2);
					pD-&gt;A = (byte)(p1.A * frac1 + p2.A * frac2);

					dblSrcRow += dblStep;
					pD += bdDst.Width;
				}
			}
		}
	}
</pre></p>
<p>The result looked perfectly good, but after thinking about it for awhile I realized that this piece of code actually is completely and utterly wrong in the general case: it only works when the alpha values of the two adjacent pixels are very close. In other cases the result will be poor.</p>
<p>Perhaps the easiest way to see this is to think about what happens when we want a 50% mix of a completely transparent pixel and a completely opaque while pixel. Intuitively I think it&#8217;s clear that we want the result to be a white pixel with 50% transparency. However, if we represent the transparent pixel with (0,0,0,0) (the most common value I&#8217;d guess, although (0,x,x,x) is transparent regardless of the value of x) we get a <strong>gray</strong> half transparent pixel instead (127,127,127,127). Not right at all. The reason I thought my attempt looked good in the first place was just because I had a gray border around the images!</p>
<p>So how <strong>do</strong> we mix pixels with alpha values? Obviously &#8220;normal&#8221; <a href="http://en.wikipedia.org/wiki/Alpha_Blend">alpha blending</a> is not sufficient when we have alpha values on <strong>both</strong> pixels&#8230; after thinking about this for a few minutes,  I said to myself &#8220;why not ask someone who knows instead?&#8221;. That someone is of course Graphics.DrawImage, and so I ended up with much cleaner code. And although I never bothered to figure out how to mix and blend pixels when both pixels contain alpha values I ended up realizing this:</p>
<p style="padding-left:30px;"><em>Graphics.DrawImage has quite a bit of work to do when we draw a 32-bit bitmap on top of another. If we have some a-priori knowledge of the nature of the bitmaps we&#8217;re working with (are any of them totally opaque?) then it is actually possible to do this ourselves much faster than Graphics.DrawImage has a chance to, because it is forced to work with the general case: both bitmaps may be semi-transparent.</em></p>
<p>I will get back on how we in some cases can outperform Graphics.DrawImage (when it comes to speed), and hopefully a real life case when we actually bother. Stay tuned. <a href="http://www.vdsoft.se"><span style="color:#ffffff;">Ekeforshus</span></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/danbystrom.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/danbystrom.wordpress.com/260/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/danbystrom.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/danbystrom.wordpress.com/260/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/danbystrom.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/danbystrom.wordpress.com/260/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/danbystrom.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/danbystrom.wordpress.com/260/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/danbystrom.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/danbystrom.wordpress.com/260/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/danbystrom.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/danbystrom.wordpress.com/260/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/danbystrom.wordpress.com/260/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/danbystrom.wordpress.com/260/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=260&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://danbystrom.se/2009/01/12/thumbnails-with-glass-table-reflection-in-gdi/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d518503a8b459ab2066d1623872e94bd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">danbystrom</media:title>
		</media:content>

		<media:content url="http://danbystrom.files.wordpress.com/2009/01/vistathumbnails.jpg" medium="image">
			<media:title type="html">vistathumbnails</media:title>
		</media:content>

		<media:content url="http://danbystrom.files.wordpress.com/2009/01/reflectionsamples1.jpg" medium="image">
			<media:title type="html">reflectionsamples1</media:title>
		</media:content>

		<media:content url="http://danbystrom.files.wordpress.com/2009/01/reflectionsteps2.jpg" medium="image">
			<media:title type="html">reflectionsteps2</media:title>
		</media:content>
	</item>
		<item>
		<title>Jeff Minter is back</title>
		<link>http://danbystrom.se/2009/01/12/jeff-minter-is-back/</link>
		<comments>http://danbystrom.se/2009/01/12/jeff-minter-is-back/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 07:59:15 +0000</pubDate>
		<dc:creator>Dan Byström</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://danbystrom.se/?p=314</guid>
		<description><![CDATA[The man who once brought us Metagalactic Llamas Battle at the Edge of Time, Sheep in Space and Revenge of the Mutant Camals has returned with: Space Giraffe That feels good to know. Ekeforshus<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=314&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The man who once brought us Metagalactic Llamas Battle at the Edge of Time, Sheep in Space and Revenge of the Mutant Camals has returned with:</p>
<p style="padding-left:30px;"><a href="http://www.llamasoft.co.uk/games/space-giraffe">Space Giraffe</a></p>
<p>That feels good to know. <a href="http://www.vdsoft.se"><span style="color:#ffffff;">Ekeforshus</span></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/danbystrom.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/danbystrom.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/danbystrom.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/danbystrom.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/danbystrom.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/danbystrom.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/danbystrom.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/danbystrom.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/danbystrom.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/danbystrom.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/danbystrom.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/danbystrom.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/danbystrom.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/danbystrom.wordpress.com/314/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=314&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://danbystrom.se/2009/01/12/jeff-minter-is-back/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d518503a8b459ab2066d1623872e94bd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">danbystrom</media:title>
		</media:content>
	</item>
		<item>
		<title>Image.GetThumbnailImage and beyond</title>
		<link>http://danbystrom.se/2009/01/05/imagegetthumbnailimage-and-beyond/</link>
		<comments>http://danbystrom.se/2009/01/05/imagegetthumbnailimage-and-beyond/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 11:09:53 +0000</pubDate>
		<dc:creator>Dan Byström</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[GDI+]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://danbystrom.se/?p=187</guid>
		<description><![CDATA[Once I tried to use Image.GetThumbnailImage because I wanted to fire up small thumbnails as fast as possible. So I tried: This turned out to be completely useless, since Image.FromFile first loads the full image so that no performance is gained whatsoever. Trying to google after a solution only resulted in tons of articles saying [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=187&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Once I tried to use <strong>Image.GetThumbnailImage</strong> because I wanted to fire up small thumbnails as fast as possible. So I tried:</p>
<p><pre class="brush: csharp;">
	// totally and completely, utterly useless
	private Bitmap getThumbnailImage( string filename, int width, int height )
	{
		using ( Image img = Image.FromFile( filename ) )
			return (Bitmap)img.GetThumbnailImage( width, height, null, IntPtr.Zero );
	}
</pre></p>
<p>This turned out to be completely useless, since <strong>Image.FromFile</strong> first loads the full image so that no performance is gained whatsoever. Trying to google after a solution only resulted in tons of articles saying that <strong>Image.GetThumbnailImage</strong> is pretty useless and shouldn&#8217;t be used. So I dropped it and solved my problem in a completely different way. Now I just stumbled across an overloaded version <strong>Image.FromStream</strong> which I haven&#8217;t noticed before:</p>
<p><a href="http://msdn.microsoft.com/en-us/library/21zw9ah6(VS.80).aspx">Image.FromStream( Stream stream, bool useEmbeddedColorManagement, bool validateImageData )</a></p>
<p>This opens up for some interesting usages. For example it means that we really can get a thumbnail fast:</p>
<p><pre class="brush: csharp;">
	// way, way faster, but still pretty useless
	private Bitmap getThumbnailImage( string filename, int width, int height )
	{
		using ( FileStream fs = new FileStream( filename, FileMode.Open ) )
		using ( Image img = Image.FromStream( fs, true, false ) )
			return (Bitmap)img.GetThumbnailImage( width, height, null, IntPtr.Zero );
	}
</pre></p>
<p>This is still pretty useless, since this way we really don&#8217;t know how to get a proportional thumbnail. This is something that seems to be lacking in GDI+: an easy way to rescale images proportionally. Quite frankly: how often are we interested in non-proportional rescales? Not that often, I&#8217;d say! Here&#8217;s a better version:</p>
<p><pre class="brush: csharp;">
	// actually works...
	private Bitmap getThumbnailImage( string filename, int width )
	{
		using ( FileStream fs = new FileStream( filename, FileMode.Open ) )
		using ( Image img = Image.FromStream( fs, true, false ) )
			return (Bitmap)img.GetThumbnailImage(
				width,
				width * img.Height / img.Width,
				null,
				IntPtr.Zero );
	}
</pre></p>
<p>But if we arm ourselves with a way to rescale images proportionally, something Microsoft apparently decided to leave as an exercise for each and every programmer who wants to do even the simplest things with images in GDI+:</p>
<p><pre class="brush: csharp;">
	public static Size adaptProportionalSize(
		Size szMax,
		Size szReal )
	{
		int nWidth;
		int nHeight;
		double sMaxRatio;
		double sRealRatio;

		if ( szMax.Width &lt; 1 || szMax.Height &lt; 1 || szReal.Width &lt; 1 || szReal.Height &lt; 1 )
			return Size.Empty;

		sMaxRatio = (double)szMax.Width / (double)szMax.Height;
		sRealRatio = (double)szReal.Width / (double)szReal.Height;

		if ( sMaxRatio &lt; sRealRatio )
		{
			nWidth = Math.Min( szMax.Width, szReal.Width );
			nHeight = (int)Math.Round( nWidth / sRealRatio );
		}
		else
		{
			nHeight = Math.Min( szMax.Height, szReal.Height );
			nWidth = (int)Math.Round( nHeight * sRealRatio );
		}

		return new Size( nWidth, nHeight );
	}
</pre></p>
<p>With that, we can fire up a thumbnail image fast, with a given maximum allowed size while still proportional:</p>
<p><pre class="brush: csharp;">
	// even better...
	private Bitmap getThumbnailImage( string filename, Size szMax )
	{
		using ( FileStream fs = new FileStream( filename, FileMode.Open ) )
		using ( Image img = Image.FromStream( fs, true, false ) )
		{
			Size sz = adaptProportionalSize( szMax, img.Size );
			return (Bitmap)img.GetThumbnailImage(
				sz.Width,
				sz.Height,
				null,
				IntPtr.Zero );
		}
	}
</pre></p>
<p>So, it appears that <strong>Image.GetThumbnailImage</strong> had its use after all! But there&#8217;s more we can do with this.</p>
<p>Even though we managed to load thumbnail images fast and proportionally, the quality isn&#8217;t particularly good. That&#8217;s seems to be the main concern among those who advocate not using Image.GetThumbnailImage at all. If a thumbnail is found in the image file it has already been resized once, and resizing a resized image once more certainly won&#8217;t improve the quality, especially if a crappy resizing algorithm is being used. Let&#8217;s see what we can do about this. If we&#8217;re working with JPG images coming from a digital camera, we can most probably find the &#8220;real&#8221; thumbnail image like this:</p>
<p><pre class="brush: csharp;">
	private Bitmap getExifThumbnail( string filename )
	{
		using ( FileStream fs = new FileStream( filename, FileMode.Open ) )
		using ( Image img = Image.FromStream( fs, true, false ) )
		{
			foreach ( PropertyItem pi in img.PropertyItems )
				if ( pi.Id == 20507 )
					return (Bitmap)Image.FromStream( new MemoryStream( pi.Value ) );
		}
		return null;
	}
</pre></p>
<p>If we can retrieve a thumbnail this way, it will be in its original size and so we can skip an implicit resize. If we want to rescale it anyway, we can do it in a high quality fashion. I don&#8217;t know if this is something everybody knows &#8211; I had worked with GDI+ for quite some time before I found it &#8211; but fact is that resizing an image like this give good performance but crappy result:</p>
<p><pre class="brush: csharp;">
	// poor image quality
	Bitmap bmpResized = new Bitmap( bmpOriginal, newWidth, newHeight );
</pre></p>
<p>Instead, try the following:</p>
<p><pre class="brush: csharp;">
	// superior image quality
	Bitmap bmpResized = new Bitmap( newWidth, newHeight );
	using ( Graphics g = Graphics.FromImage( bmpResized ) )
	{
		g.InterpolationMode = InterpolationMode.HighQualityBicubic;
		g.DrawImage(
			bmpOriginal,
			new Rectangle( Point.Empty, bmpResized.Size ),
			new Rectangle( Point.Empty, bmpOriginal.Size ),
			GraphicsUnit.Pixel );
	}
</pre></p>
<p>With these code pieces glued together I can now get a thumbnail from a JPG image both faster and with better quality than with my original Image.ImageFromThumbnail attempt!</p>
<p><strong>UPDATE</strong>: This technique is used &#8220;live&#8221; in the demo source code accompanying this post: <a href="http://danbystrom.se/2009/01/12/thumbnails-with-glass-table-reflection-in-gdi/">Thumbnails with glass table reflection in GDI+</a>.</p>
<p>Finally, one more thing that I just come to think of while I was typing this. I have complained in earlier posts that <strong>Image.FromFile</strong> for some obscure reason keeps the file locked until disposed of. I just realized that there is an easy way around his:</p>
<p><pre class="brush: csharp;">
	using ( FileStream fs = new FileStream( filename, FileMode.Open ) )
		bmp = (Bitmap)Image.FromStream( fs );
</pre></p>
<p>Behold &#8211; now the image is loaded and the file is NOT locked! <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  <a href="http://www.vdsoft.se"><span style="color:#ffffff;">Ekeforshus</span></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/danbystrom.wordpress.com/187/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/danbystrom.wordpress.com/187/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/danbystrom.wordpress.com/187/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/danbystrom.wordpress.com/187/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/danbystrom.wordpress.com/187/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/danbystrom.wordpress.com/187/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/danbystrom.wordpress.com/187/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/danbystrom.wordpress.com/187/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/danbystrom.wordpress.com/187/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/danbystrom.wordpress.com/187/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/danbystrom.wordpress.com/187/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/danbystrom.wordpress.com/187/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/danbystrom.wordpress.com/187/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/danbystrom.wordpress.com/187/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=187&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://danbystrom.se/2009/01/05/imagegetthumbnailimage-and-beyond/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d518503a8b459ab2066d1623872e94bd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">danbystrom</media:title>
		</media:content>
	</item>
		<item>
		<title>Optimizing away II.3</title>
		<link>http://danbystrom.se/2009/01/01/optimizing-away-ii3/</link>
		<comments>http://danbystrom.se/2009/01/01/optimizing-away-ii3/#comments</comments>
		<pubDate>Thu, 01 Jan 2009 19:26:47 +0000</pubDate>
		<dc:creator>Dan Byström</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://danbystrom.se/?p=206</guid>
		<description><![CDATA[Oh, the pain, the pain and the embarrassment&#8230; I just came to realize that although a &#8220;long&#8221; in C# is 64 bits, in C++ it is still 32 bits. In order to get a 64 bit value in MSVC++ you must type either &#8220;long long&#8221; or &#8220;__int64&#8243;. I didn&#8217;t know that. This means that although [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=206&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Oh, the pain, the pain and the embarrassment&#8230;</p>
<p>I just came to realize that although a &#8220;long&#8221; in C# is 64 bits, in C++ it is still 32 bits. In order to get a 64 bit value in MSVC++ you must type either &#8220;long long&#8221; or &#8220;__int64&#8243;. I didn&#8217;t know that. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p>
<p>This means that although the assembler function I just <a href="http://danbystrom.se/2008/12/22/optimizing-away-ii/">presented </a>correctly calculates a 64 bit value, it will be truncated to 32 bits because the surrounding C++ function is declared as a long.</p>
<p>This in turn means that for bitmaps larger than 138 x 138 pixels &#8211; the correct result cannot be guaranteed. (With 64 bit values, the bitmap can instead be 9724315 x 9724315 pixels in size before an overflow can occur.)</p>
<p>Unfortunately, although I had unit test to verify the correctness of the function, I only tested with small bitmaps.</p>
<p>I have <a href="http://www.visual-design.se/blog/fic.zip">uploaded a new version</a>. <a href="http://www.vdsoft.se"><span style="color:#ffffff;">Ekeforshus</span></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/danbystrom.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/danbystrom.wordpress.com/206/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/danbystrom.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/danbystrom.wordpress.com/206/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/danbystrom.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/danbystrom.wordpress.com/206/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/danbystrom.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/danbystrom.wordpress.com/206/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/danbystrom.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/danbystrom.wordpress.com/206/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/danbystrom.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/danbystrom.wordpress.com/206/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/danbystrom.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/danbystrom.wordpress.com/206/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=206&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://danbystrom.se/2009/01/01/optimizing-away-ii3/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d518503a8b459ab2066d1623872e94bd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">danbystrom</media:title>
		</media:content>
	</item>
		<item>
		<title>Optimizing away II.2</title>
		<link>http://danbystrom.se/2008/12/30/optimizing-away-ii2/</link>
		<comments>http://danbystrom.se/2008/12/30/optimizing-away-ii2/#comments</comments>
		<pubDate>Tue, 30 Dec 2008 13:20:58 +0000</pubDate>
		<dc:creator>Dan Byström</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://danbystrom.se/?p=197</guid>
		<description><![CDATA[I was asked to upload the source and binary for my last post.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=197&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I was asked to upload the <a href="http://www.visual-design.se/blog/fic.zip">source and binary</a> for my <a href="http://danbystrom.se/2008/12/22/optimizing-away-ii/">last post</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/danbystrom.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/danbystrom.wordpress.com/197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/danbystrom.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/danbystrom.wordpress.com/197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/danbystrom.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/danbystrom.wordpress.com/197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/danbystrom.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/danbystrom.wordpress.com/197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/danbystrom.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/danbystrom.wordpress.com/197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/danbystrom.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/danbystrom.wordpress.com/197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/danbystrom.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/danbystrom.wordpress.com/197/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=197&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://danbystrom.se/2008/12/30/optimizing-away-ii2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d518503a8b459ab2066d1623872e94bd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">danbystrom</media:title>
		</media:content>
	</item>
		<item>
		<title>Optimizing away II</title>
		<link>http://danbystrom.se/2008/12/22/optimizing-away-ii/</link>
		<comments>http://danbystrom.se/2008/12/22/optimizing-away-ii/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 11:17:39 +0000</pubDate>
		<dc:creator>Dan Byström</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://danbystrom.se/?p=172</guid>
		<description><![CDATA[Continued from Optimizing away. Ok, now I have worked up the courage. Prepare yourself for a major disappointment. I really do not know how to tweak that C#-loop to run a nanosecond faster. But I can do the same calculation much faster. How? Just my old favorite party trick. It goes like this: 1. Add [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=172&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Continued from <a href="http://danbystrom.se/2008/12/16/optimizing-away/">Optimizing away</a>. Ok, now I have worked up the courage.</p>
<p>Prepare yourself for a major disappointment. I really do not know how to tweak that C#-loop to run a nanosecond faster. But I can do the same calculation much faster. How? Just my old favorite party trick. It goes like this:</p>
<p>1. Add a new project to your solution</p>
<p>2. Chose Visual C++ / CLR / Class Library</p>
<p>3. Insert the following managed class:</p>
<p><pre class="brush: csharp;">
	public ref class FastImageCompare
	{
	public:
		static double compare( void* p1, void* p2, int count )
		{
			return NativeCode::fastImageCompare( p1, p2, count );
		}
		static double compare( IntPtr p1, IntPtr p2, int count )
		{
			return NativeCode::fastImageCompare( p1.ToPointer(), p2.ToPointer(), count );
		}
	};
</pre></p>
<p>4. Insert the following function into an unmanaged class (which I happened to call NativeCode):</p>
<p><pre class="brush: csharp;">
unsigned long long NativeCode::fastImageCompare( void* p1, void* p2, int count )
{
	int high32 = 0;

	_asm
	{
		push	ebx
		push	esi
		push	edi

		mov		esi, p1
		mov		edi, p2
		xor		eax, eax
again:
		dec		count
		js		done

		movzx	ebx, [esi]
		movzx	edx, [edi]
		sub		edx, ebx
		imul	edx, edx

		movzx	ebx, [esi+1]
		movzx	ecx, [edi+1]
		sub		ebx, ecx
		imul	ebx, ebx
		add		edx, ebx

		movzx	ebx, [esi+2]
		movzx	ecx, [edi+2]
		sub		ebx, ecx
		imul	ebx, ebx
		add		edx, ebx

		add		esi, 4
		add		edi, 4

		add		eax, edx
		jnc		again

		inc		high32
		jmp		again
done:
		mov		edx, high32

		pop		edi
		pop		esi
		pop		ebx
	}

}
</pre></p>
<p>Yeah. That&#8217;s it. Hand tuned assembly language within a .NET Assembly. <strong>UPDATE </strong>2009-01-01: return type of the function changed from &#8220;unsigned long&#8221; to &#8220;unsigned long long&#8221;, <a href="http://danbystrom.se/2009/01/01/optimizing-away-ii3/">see here</a>.</p>
<p>I guess that&#8217;s almost cheating. And we will be locked inside the Intel platform. Most people won&#8217;t mind I guess, but other may have very strong feelings about it. If we really would like to exploit this kind of optimizations while still be portable (to Mono/Mac for example) one possibility would be to load the assembly with native code dynamically. If it fails we could fall back to an alternative version written in pure managed code.</p>
<p>(I know from experience that some people with lesser programming skills react to this with a &#8220;what? it must be a crappy compiler if you can write faster code by yourself&#8221;. Let me assure you that this is not the case. On the contrary: I&#8217;m amazed about the quality of the code emitted by the C# + .NET JIT compilers.)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/danbystrom.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/danbystrom.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/danbystrom.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/danbystrom.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/danbystrom.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/danbystrom.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/danbystrom.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/danbystrom.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/danbystrom.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/danbystrom.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/danbystrom.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/danbystrom.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/danbystrom.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/danbystrom.wordpress.com/172/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=172&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://danbystrom.se/2008/12/22/optimizing-away-ii/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d518503a8b459ab2066d1623872e94bd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">danbystrom</media:title>
		</media:content>
	</item>
		<item>
		<title>Optimizing away</title>
		<link>http://danbystrom.se/2008/12/16/optimizing-away/</link>
		<comments>http://danbystrom.se/2008/12/16/optimizing-away/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 11:57:45 +0000</pubDate>
		<dc:creator>Dan Byström</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://danbystrom.se/?p=161</guid>
		<description><![CDATA[Follow-up on Improving performance&#8230;  and Genetic Programming: Evolution of Mona Lisa. I just tested that I can optimize this loop: so that it runs even 60% faster. Don’t dare to tell you how, however. EDIT: Continued here Ekeforshus<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=161&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Follow-up on <a href="http://danbystrom.se/2008/12/14/improving-performance/">Improving performance&#8230;</a>  and <a href="http://rogeralsing.com/2008/12/07/genetic-programming-evolution-of-mona-lisa/">Genetic Programming: Evolution of Mona Lisa</a>.<br />
I just tested that I can optimize this loop:</p>
<p><pre class="brush: csharp;">
unchecked
{
    unsafe
    {
        fixed ( Pixel* psourcePixels = sourcePixels )
        {
            Pixel* p1 = (Pixel*)bd.Scan0.ToPointer();
            Pixel* p2 = psourcePixels;
            for ( int i = sourcePixels.Length ; i &gt; 0 ; i--, p1++, p2++ )
            {
                int r = p1-&gt;R - p2-&gt;R;
                int g = p1-&gt;G - p2-&gt;G;
                int b = p1-&gt;B - p2-&gt;B;
                error += r * r + g * g + b * b;
            }
        }
    }
}
</pre></p>
<p>so that it runs even 60% faster. Don’t dare to tell you how, however.</p>
<p>EDIT: <a href="http://danbystrom.se/2008/12/22/optimizing-away-ii/">Continued here</a>  <a href="http://www.vdsoft.se"><span style="color:#ffffff;">Ekeforshus</span></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/danbystrom.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/danbystrom.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/danbystrom.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/danbystrom.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/danbystrom.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/danbystrom.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/danbystrom.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/danbystrom.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/danbystrom.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/danbystrom.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/danbystrom.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/danbystrom.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/danbystrom.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/danbystrom.wordpress.com/161/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=danbystrom.se&amp;blog=2755780&amp;post=161&amp;subd=danbystrom&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://danbystrom.se/2008/12/16/optimizing-away/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d518503a8b459ab2066d1623872e94bd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">danbystrom</media:title>
		</media:content>
	</item>
	</channel>
</rss>
