<?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>Marius Bancila's Blog &#187; ColorMatrix</title>
	<atom:link href="http://mariusbancila.ro/blog/tag/colormatrix/feed/" rel="self" type="application/rss+xml" />
	<link>http://mariusbancila.ro/blog</link>
	<description>Sharing my opinions and ideas!</description>
	<lastBuildDate>Fri, 06 Apr 2012 13:45:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using ColorMatrix for Creating Negative Image</title>
		<link>http://mariusbancila.ro/blog/2009/11/13/using-colormatrix-for-creating-negative-image/</link>
		<comments>http://mariusbancila.ro/blog/2009/11/13/using-colormatrix-for-creating-negative-image/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 08:49:45 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[ColorMatrix]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[negative]]></category>
		<category><![CDATA[transformation]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=395</guid>
		<description><![CDATA[.NET provides two classes for image transformations: Matrix, used for geometric transformations, and ColorMatrix, used for color transformations. One of such color transformations is inverting or negating. This means subtracting each color component from 255. Black (0,0,0) becomes White (255, 255, 255), and Green (0, 255, 0) becomes Magenta (255, 0, 255). You can find [...]]]></description>
			<content:encoded><![CDATA[<p>.NET provides two classes for image transformations: Matrix, used for geometric transformations, and ColorMatrix, used for color transformations. </p>
<p>One of such color transformations is inverting or negating. This means subtracting each color component from 255. Black (0,0,0) becomes White (255, 255, 255), and Green (0, 255, 0) becomes Magenta (255, 0, 255). </p>
<p>You can find many examples on the web that look like this:</p>
<pre name="code" class="csharp">
public Bitmap Transform(Bitmap source)
{
    //create a blank bitmap the same size as original
    Bitmap newBitmap = new Bitmap(source.Width, source.Height);

    //get a graphics object from the new image
    Graphics g = Graphics.FromImage(newBitmap);

    // create the negative color matrix
    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.Matrix00 = colorMatrix.Matrix11 = colorMatrix.Matrix22 = -1f;
    colorMatrix.Matrix33 = colorMatrix.Matrix44 = 1f;

    // create some image attributes
    ImageAttributes attributes = new ImageAttributes();

    attributes.SetColorMatrix(colorMatrix);

    g.DrawImage(source, new Rectangle(0, 0, source.Width, source.Height),
                0, 0, source.Width, source.Height, GraphicsUnit.Pixel, attributes);

    //dispose the Graphics object
    g.Dispose();

    return newBitmap;
}
</pre>
<p>Using this code one can get a negative image.<br />
<div class="wp-caption alignnone" style="width: 497px"><img alt="Original image" src="/blog/wp-content/uploads/2009/11/negativesample1.jpg" title="Original image" width="487" height="396" /><p class="wp-caption-text">Original image</p></div></p>
<div class="wp-caption alignnone" style="width: 497px"><img alt="Negative image" src="/blog/wp-content/uploads/2009/11/negativesample3.jpg" title="Negative image" width="487" height="396" /><p class="wp-caption-text">Negative image</p></div>
<p>This runs fine on Windows XP. But when I ran it on Windows 7, I was getting only a black image. All the pixels were ARGB(255, 0, 0, 0). This was how it looked:</p>
<div class="wp-caption alignnone" style="width: 495px"><img alt="Incorrectly transformed image" src="/blog/wp-content/uploads/2009/11/negativesample2.jpg" title="Incorrectly transformed image" width="485" height="395" /><p class="wp-caption-text">Incorrectly transformed image</p></div>
<p>I was surprised to learn that it worked on Windows XP, but not on Windows 7. I don&#8217;t have Windows Vista to test but I guess it&#8217;s the same as with Windows 7. I thought it must be something in the GDI+ library, because building with .NET 3.5 SP1 or 4.0 Beta 2 didn&#8217;t change a thing.</p>
<p>After trying different things, I figured out what the problem was: the color matrix was incorrect. It must be defined like this:</p>
<pre name="code" class="csharp">
ColorMatrix colorMatrix = new ColorMatrix(
   new float[][]
   {
      new float[] {-1, 0, 0, 0, 0},
      new float[] {0, -1, 0, 0, 0},
      new float[] {0, 0, -1, 0, 0},
      new float[] {0, 0, 0, 1, 0},
      new float[] {1, 1, 1, 0, 1}
   });
</pre>
<p>With this change the Transform function produces a correct negative image, regardless the operating system or the .NET framework version. </p>
<p>However, what I don&#8217;t know yet, is why it worked on Windows XP. The only conclusion I can draw is that the GDI+ implementation has a fault there, that was later corrected. That&#8217;s why an incorrect color matrix produced a correct transformation on Windows XP.</p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2009/11/13/using-colormatrix-for-creating-negative-image/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

