<?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:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>JMSoftware &#187; pixel</title>
	<atom:link href="http://www.jmsoftware.co.uk/blog/tag/pixel/feed" rel="self" type="application/rss+xml" />
	<link>http://www.jmsoftware.co.uk</link>
	<description>Software Development</description>
	<lastBuildDate>Sat, 19 Jun 2010 14:02:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
		<item>
		<title>C# Snippet for Accessing Bitmap Data with Unsafe Code</title>
		<link>http://www.jmsoftware.co.uk/blog/c-snippet-for-accessing-bitmap-data-with-unsafe-code</link>
		<comments>http://www.jmsoftware.co.uk/blog/c-snippet-for-accessing-bitmap-data-with-unsafe-code#comments</comments>
		<pubDate>Mon, 30 Mar 2009 17:37:55 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[pixel]]></category>
		<category><![CDATA[Snippet]]></category>
		<category><![CDATA[unsafe]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.jmsoftware.co.uk/?p=175</guid>
		<description><![CDATA[Since GetPixel is slow and I keep having to access bitmap data with unsafe code, I thought it would be a nice thing to make into a snippet. Paste the following code into a text file, and save it as &#8230; <a href="http://www.jmsoftware.co.uk/blog/c-snippet-for-accessing-bitmap-data-with-unsafe-code">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Since GetPixel is slow and I keep having to access bitmap data with unsafe code, I thought it would be a nice thing to make into a snippet.<span id="more-175"></span></p>
<p>Paste the following code into a text file, and save it as &#8220;usbmp.snippet&#8221;. In Visual Studio go to &#8220;Tools/Code Snippets Manager&#8230;&#8221; and import the file.</p>
<hr />
<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"&gt;
  &lt;CodeSnippet Format="1.0.0"&gt;
    &lt;Header&gt;
      &lt;Title&gt;Unsafe Bitmap Processing&lt;/Title&gt;
      &lt;Shortcut&gt;usbmp&lt;/Shortcut&gt;
      &lt;Description&gt;Code for accessing a bitmaps pixel data with unsafe code&lt;/Description&gt;
      &lt;Author&gt;Jonathan Mathews Software&lt;/Author&gt;
    &lt;/Header&gt;
    &lt;Snippet&gt;
      &lt;Declarations&gt;
        &lt;Object&gt;
          &lt;ID&gt;bitmap&lt;/ID&gt;
          &lt;Type&gt;System.Drawing.Bitmap&lt;/Type&gt;
          &lt;ToolTip&gt;Replace with a bitmap in your application.&lt;/ToolTip&gt;
          &lt;Default&gt;bitmap&lt;/Default&gt;
        &lt;/Object&gt;
      &lt;/Declarations&gt;
      &lt;Code Language="CSharp"&gt;
        &lt;![CDATA[unsafe
                {
                    BitmapData data = $bitmap$.LockBits(
                                                    new Rectangle(new Point(0, 0), $bitmap$.Size), 
                                                    ImageLockMode.ReadOnly, 
                                                    PixelFormat.Format24bppRgb);

                    byte* pointer = (byte*)data.Scan0;

                    int padding = data.Stride - ($bitmap$.Width * 3);

                    for (int y = 0; y &lt; $bitmap$.Height; y++)
                    {
                        for (int x = 0; x &lt; $bitmap$.Width; x++)
                        {
                            // pointer[2]; // R
                            // pointer[1]; // G
                            // pointer[0]; // B
                            $end$

                            pointer += 3;
                        }

                        pointer += padding;
                    }

                    $bitmap$.UnlockBits(data);
                }]]&gt;
      &lt;/Code&gt;
    &lt;/Snippet&gt;
  &lt;/CodeSnippet&gt;
&lt;/CodeSnippets&gt;</code></pre>
<hr />
<p>Notes:</p>
<ul>
<li>Use it by entering &#8220;usbmp&#8221; in your code, and pressing tab twice. Edit the bitmap name to your bitmap.</li>
<li>You&#8217;ll need to add <code>using System.Drawing.Imaging;</code> if it&#8217;s not there already.</li>
<li>If you want to edit the pixels instead of just reading them, change &#8220;ImageLockMode.ReadOnly&#8221;.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.jmsoftware.co.uk/blog/c-snippet-for-accessing-bitmap-data-with-unsafe-code/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Making sure a number is between 0 and 255</title>
		<link>http://www.jmsoftware.co.uk/blog/making-sure-a-number-is-between-0-and-255</link>
		<comments>http://www.jmsoftware.co.uk/blog/making-sure-a-number-is-between-0-and-255#comments</comments>
		<pubDate>Tue, 10 Jul 2007 13:21:47 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[between]]></category>
		<category><![CDATA[bitwise_operations]]></category>
		<category><![CDATA[byte]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[limit]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[max]]></category>
		<category><![CDATA[min]]></category>
		<category><![CDATA[number]]></category>
		<category><![CDATA[pixel]]></category>
		<category><![CDATA[Theory]]></category>

		<guid isPermaLink="false">http://www.jmsoftware.co.uk/blog/making-sure-a-number-is-between-0-and-255</guid>
		<description><![CDATA[Recently while reworking the code for the Ascgen dotNET I needed to make sure that a variable is between 0 and 255 before converting it to a byte, and I needed it to be as fast as possible since it &#8230; <a href="http://www.jmsoftware.co.uk/blog/making-sure-a-number-is-between-0-and-255">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Recently while reworking the code for the <a href="/software/ascgen">Ascgen dotNET</a> I needed to make sure that a variable is between 0 and 255 before converting it to a byte, and I needed it to be as fast as possible since it can get called several million times a second.<span id="more-9"></span></p>
<p>After realising that there really wasn&#8217;t a built in function like:</p>
<p><code>Result[x, y] = Math.Between(0, 255, pixel);</code></p>
<p>I initially used the minimum and maximum functions:</p>
<p><code>Result[x, y] = Math.Max(0, Math.Min(255, pixel));</code></p>
<p>This works, and is a pretty neat little piece of code. However, after profiling the application it quickly became obvious that it was slowing things down. A look at the compiled code shows that even when built for release, Min and Max are left as function calls instead of being inlined.</p>
<p>Looking into bitwise operations, we see that using a bitwise AND with 255 will trim the value to be between 0 and 255. Unfortunately it loops around, so that isn&#8217;t what we need:</p>
<pre>
  100101100   300
&#038; 011111111   255
= 000101100   044
</pre>
<p>The next option was to just check it manually:</p>
<pre><code>if (pixel &amp;gt; 255) {
	Result[x, y] = 255;
}
else if (pixel &amp;lt; 0) {
	Result[x, y] = 0;
}
else {
	Result[x, y] = (byte)pixel;
}</code></pre>
<p>Which is better, or:</p>
<pre><code>Result[x, y] = (byte)(pixel &gt; 255 ? 255 : (pixel &lt; 0 ? 0 : pixel));</code></pre>
<p>Which is silly and harder to maintain.</p>
<p>Either way a new problem is that most of the pixel values are between 0 and 255, therefore two checks will usually be required. Thanks to a great suggestion by biznatchio, we can instead do it in one comparison.</p>
<p>We saw earlier that &#8220;value &#038;= 255&#8243; will trim the value to be between 0 and 255. So, if we just compare the value to the trimmed value we will know if it is in range:</p>
<pre><code>if ((pixel &amp; 255) == pixel) {
	Result[x, y] = (byte)pixel;
}
else if (pixel &amp;gt; 255) {
	Result[x, y] = 255;
}
else {
	Result[x, y] = 0;
}</code></pre>
<p>This will be slower then before if the value is &lt;0 or &gt;255. As our values will be in range most of the time, this method is the best.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jmsoftware.co.uk/blog/making-sure-a-number-is-between-0-and-255/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
	</channel>
</rss>
