<?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>Centre of the Universe &#187; C#</title>
	<atom:link href="http://www.sergemeunier.com/blog/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sergemeunier.com/blog</link>
	<description>The musings of a mad software developer</description>
	<lastBuildDate>Tue, 01 Jun 2010 20:26:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Writing a family tree application in C# &#8211; Importing a Gedcom file &#8211; Part 2</title>
		<link>http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-sharp-importing-a-gedcom-file-part-2/</link>
		<comments>http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-sharp-importing-a-gedcom-file-part-2/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 08:51:22 +0000</pubDate>
		<dc:creator>Serge Meunier</dc:creator>
				<category><![CDATA[Genealogy]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.sergemeunier.com/blog/?p=1636</guid>
		<description><![CDATA[Now that we know how gedcom files work (covered in part 1), we need to actually parse the file.
The way in which the gedcom is structured is that each new record entry starts on a line with a 0. All lines with a 1 under that belong to that record. Similarly, lines with a 2 [...]


Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-sharp-importing-a-gedcom-file-part-1/' rel='bookmark' title='Permanent Link: Writing a family tree application in C# &#8211; Importing a Gedcom file &#8211; Part 1'>Writing a family tree application in C# &#8211; Importing a Gedcom file &#8211; Part 1</a> <small>In my previous post in this series, I introduced the...</small></li>
<li><a href='http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-the-underlying-structure/' rel='bookmark' title='Permanent Link: Writing a family tree application in C# &#8211; The underlying structure'>Writing a family tree application in C# &#8211; The underlying structure</a> <small>I love being able to trace my family tree, and...</small></li>
<li><a href='http://www.sergemeunier.com/blog/building-a-gravity-simulator-in-o3d-part-2/' rel='bookmark' title='Permanent Link: Building a gravity simulator in O3D &#8211; Part 2'>Building a gravity simulator in O3D &#8211; Part 2</a> <small>In the last post, I introduced the physics engine for...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Now that we know how gedcom files work (covered in <a href="http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-sharp-importing-a-gedcom-file-part-1/">part 1</a>), we need to actually parse the file.</p>
<p>The way in which the gedcom is structured is that each new record entry starts on a line with a 0. All lines with a 1 under that belong to that record. Similarly, lines with a 2 prefix, belond to the the entry prefixed by a 1 directly above it.</p>
<p>This makes processing rather straightforward. All we need to do, is read the file line by line, then split up the line into its components, and process accordingly.</p>
<p>All lines have a number (0, 1 or 2) as the first field in the line, and each field is seperated by a space, for example <i>1 SOUR FamilyTraces</i></p>
<p>The second field in the line is usually the keyword which tells us what the data represents. Some keywords will not need an extra data field in the third field, but usually, these types of lines which are grouped together with that line.<br />
As an example, the VERS and FORM lines are linked to the GEDC keyword as the 2 in the first field shows they belong to the preceding line.</p>
<pre name="code" class="text">1 GEDC
2 VERS 5.5
2 FORM LINEAGE-LINKED
</pre>
<p>There is an exception to the second field being the keyword, and that is in the individual, family and note record entry fields which take the format <i>0 @Ixxx@ INDI</i> for an individual record for example.</p>
<p>Based on all of that, all we need to do, is split up the line into the three fields, and then process each keyword independantly from each other, while keeping track of which type of record we are currently in, so that we can assign the data to the correct structure.</p>
<p>Here is the full code for the parser, that works pretty much exactly as described above. I have not implemented the full spec of the gedcom file format, but only enough as I need for the application. It is easily modified, however, to add the rest of the spec in if needed.</p>
<p>Usually, now would be the time where I list the source code for the code I have talked about in this blog post, but due to some bug with Wordpress and the plug-in I am using to render my code samples nicely, the code for this section is crashing Firefox, so I have included the code for the GedcomParser class as a link to a <a href="http://www.sergemeunier.com/code/GedcomParser.txt" target="_blank">text file containing the code</a>.</p>
<p>In the next post I will cover how to import the data we have read from the gedcom file into our database.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.sergemeunier.com%2Fblog%2Fwriting-a-family-tree-application-in-c-sharp-importing-a-gedcom-file-part-2%2F&amp;linkname=Writing%20a%20family%20tree%20application%20in%20C%23%20%26%238211%3B%20Importing%20a%20Gedcom%20file%20%26%238211%3B%20Part%202"><img src="http://www.sergemeunier.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>

<p>Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-sharp-importing-a-gedcom-file-part-1/' rel='bookmark' title='Permanent Link: Writing a family tree application in C# &#8211; Importing a Gedcom file &#8211; Part 1'>Writing a family tree application in C# &#8211; Importing a Gedcom file &#8211; Part 1</a> <small>In my previous post in this series, I introduced the...</small></li>
<li><a href='http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-the-underlying-structure/' rel='bookmark' title='Permanent Link: Writing a family tree application in C# &#8211; The underlying structure'>Writing a family tree application in C# &#8211; The underlying structure</a> <small>I love being able to trace my family tree, and...</small></li>
<li><a href='http://www.sergemeunier.com/blog/building-a-gravity-simulator-in-o3d-part-2/' rel='bookmark' title='Permanent Link: Building a gravity simulator in O3D &#8211; Part 2'>Building a gravity simulator in O3D &#8211; Part 2</a> <small>In the last post, I introduced the physics engine for...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-sharp-importing-a-gedcom-file-part-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Writing a family tree application in C# &#8211; Importing a Gedcom file &#8211; Part 1</title>
		<link>http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-sharp-importing-a-gedcom-file-part-1/</link>
		<comments>http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-sharp-importing-a-gedcom-file-part-1/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 08:13:40 +0000</pubDate>
		<dc:creator>Serge Meunier</dc:creator>
				<category><![CDATA[Genealogy]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.sergemeunier.com/blog/?p=1624</guid>
		<description><![CDATA[In my previous post in this series, I introduced the Family Traces family tree application. Now, I am going to go through the importing of a gedcom file.
Firstly, what is a gedcom file?
The gedcom file format is a text file format for the transmission and storage of genealogical data, created by the Church of the [...]


Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-sharp-importing-a-gedcom-file-part-2/' rel='bookmark' title='Permanent Link: Writing a family tree application in C# &#8211; Importing a Gedcom file &#8211; Part 2'>Writing a family tree application in C# &#8211; Importing a Gedcom file &#8211; Part 2</a> <small>Now that we know how gedcom files work (covered in...</small></li>
<li><a href='http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-the-underlying-structure/' rel='bookmark' title='Permanent Link: Writing a family tree application in C# &#8211; The underlying structure'>Writing a family tree application in C# &#8211; The underlying structure</a> <small>I love being able to trace my family tree, and...</small></li>
<li><a href='http://www.sergemeunier.com/blog/an-implementation-of-a-binary-tree-in-c-sharp/' rel='bookmark' title='Permanent Link: An implementation of a binary tree in C#'>An implementation of a binary tree in C#</a> <small>Binary trees are a very useful data structure, and are...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-the-underlying-structure/">previous post</a> in this series, I introduced the Family Traces family tree application. Now, I am going to go through the importing of a gedcom file.</p>
<p>Firstly, what is a gedcom file?</p>
<p>The gedcom file format is a text file format for the transmission and storage of genealogical data, created by the Church of the Latter Day Saints for their genealogical projects.</p>
<p>A standard gedcom file has a header section, individual records, family records and notes. The various records are linked to each other by ids, in the format of @I1@ for an individual record for example. For a full spec of the gedcom format, have a look <a href="http://homepages.rootsweb.ancestry.com/~pmcbride/gedcom/55gcappa.htm">here</a>.</p>
<p>Here is a sample gedcom file</p>
<pre name="code" class="text">0 HEAD
1 SOUR FamilyTraces
2 NAME Family Traces
2 CORP Serge Meunier
1 DEST Standard GEDCOM
1 DATE 2010/02/16
1 CHAR ANSEL
1 GEDC
2 VERS 5.5
2 FORM LINEAGE-LINKED
0 @I1@ INDI
1 John/Smith/
2 GIVN John
2 SURN Smith
1 SEX M
1 BIRT
2 DATE 15 dec 1950
2 PLAC
1 FAMC @F2@
1 FAMS @F1@
0 @I2@ INDI
1 Jane/Doe/
2 GIVN Jane
2 SURN Doe
1 SEX F
1 FAMC @F-1@
0 @F1@ FAM
1 HUSB @I1@
1 WIFE @I2@
1 CHIL @F3@
0 TRLR
</pre>
<p><span id="more-1624"></span><br />
Before we start processing the file, we need to have some data structures to hold the data from the file.</p>
<pre name="code" class="C-Sharp">
    public struct GedcomHeader
    {
        public string Source;
        public string SourceVersion;
        public string SourceName;
        public string SourceCorporation;
        public string Destination;
        public string Date;
        public string File;
        public string CharacterEncoding;
        public string GedcomVersion;
        public string GedcomForm;
    }

    public struct GedcomIndividual
    {
        public string Id;
        public string GivenName;
        public string Surname;
        public string Suffix;
        public string Sex;
        public string BirthDate;
        public string BirthPlace;
        public string Occupation;
        public string Description;
        public string Nationality;
        public string DiedDate;
        public string DiedPlace;
        public string DiedCause;
        public string ParentFamilyId;
        public string SpouseFamilyId;
        public ArrayList Notes;

        public GedcomIndividual(string id)
        {
            Id = id;
            GivenName = "";
            Surname = "";
            Suffix = "";
            Sex = "";
            BirthDate = "";
            BirthPlace = "";
            Occupation = "";
            Description = "";
            Nationality = "";
            DiedDate = "";
            DiedPlace = "";
            DiedCause = "";
            ParentFamilyId = "";
            SpouseFamilyId = "";
            Notes = new ArrayList();
        }
    }

    public struct GedcomFamily
    {
        public string Id;
        public string HusbandId;
        public string WifeId;
        public string MarriageDate;
        public string MarriagePlace;
        public ArrayList Children;
        public ArrayList Notes;

        public GedcomFamily(string id)
        {
            Id = id;
            HusbandId = "";
            WifeId = "";
            MarriageDate = "";
            MarriagePlace = "";
            Children = new ArrayList();
            Notes = new ArrayList();
        }
    }

    public struct GedcomNote
    {
        public string Id;
        public string Text;

        public GedcomNote(string id)
        {
            Id = id;
            Text = "";
        }
    }
</pre>
<p>We also need a set of enumerations we will use to keep track of where we are in the file. This is important because the meaning of a field in the file is dependant on the place in the file it occurs, so we need to be aware of what we need to do at all times, but more on that later.</p>
<pre name="code" class="C-Sharp">
    public enum GedcomRecordEnum
    {
        None,
        Header,
        Individual,
        Family,
        Note
    }

    public enum GedcomSubRecordEnum
    {
        None,
        HeaderSource,
        HeaderGedcom,
        IndividualName,
        IndividualBirth,
        IndividualDeath,
        FamilyChildren,
        FamilyMarriage
    }
</pre>
<p>Now we need to get to parsing the file, which is covered in <a href="http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-sharp-importing-a-gedcom-file-part-2/">Importing a Gedcom file &#8211; Part 2</a></p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.sergemeunier.com%2Fblog%2Fwriting-a-family-tree-application-in-c-sharp-importing-a-gedcom-file-part-1%2F&amp;linkname=Writing%20a%20family%20tree%20application%20in%20C%23%20%26%238211%3B%20Importing%20a%20Gedcom%20file%20%26%238211%3B%20Part%201"><img src="http://www.sergemeunier.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>

<p>Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-sharp-importing-a-gedcom-file-part-2/' rel='bookmark' title='Permanent Link: Writing a family tree application in C# &#8211; Importing a Gedcom file &#8211; Part 2'>Writing a family tree application in C# &#8211; Importing a Gedcom file &#8211; Part 2</a> <small>Now that we know how gedcom files work (covered in...</small></li>
<li><a href='http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-the-underlying-structure/' rel='bookmark' title='Permanent Link: Writing a family tree application in C# &#8211; The underlying structure'>Writing a family tree application in C# &#8211; The underlying structure</a> <small>I love being able to trace my family tree, and...</small></li>
<li><a href='http://www.sergemeunier.com/blog/an-implementation-of-a-binary-tree-in-c-sharp/' rel='bookmark' title='Permanent Link: An implementation of a binary tree in C#'>An implementation of a binary tree in C#</a> <small>Binary trees are a very useful data structure, and are...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-sharp-importing-a-gedcom-file-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Writing a family tree application in C# &#8211; The underlying structure</title>
		<link>http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-the-underlying-structure/</link>
		<comments>http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-the-underlying-structure/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 10:03:13 +0000</pubDate>
		<dc:creator>Serge Meunier</dc:creator>
				<category><![CDATA[Genealogy]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.sergemeunier.com/blog/?p=1621</guid>
		<description><![CDATA[I love being able to trace my family tree, and have used several good genealogy applications which I have found very useful, including Family Tree Legends and Family Tree Builder. The problem I have though, is that they do not always contain everything which I am looking for, so I decided to write a family [...]


Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-sharp-importing-a-gedcom-file-part-1/' rel='bookmark' title='Permanent Link: Writing a family tree application in C# &#8211; Importing a Gedcom file &#8211; Part 1'>Writing a family tree application in C# &#8211; Importing a Gedcom file &#8211; Part 1</a> <small>In my previous post in this series, I introduced the...</small></li>
<li><a href='http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-sharp-importing-a-gedcom-file-part-2/' rel='bookmark' title='Permanent Link: Writing a family tree application in C# &#8211; Importing a Gedcom file &#8211; Part 2'>Writing a family tree application in C# &#8211; Importing a Gedcom file &#8211; Part 2</a> <small>Now that we know how gedcom files work (covered in...</small></li>
<li><a href='http://www.sergemeunier.com/blog/the-problem-with-south-african-genealogy/' rel='bookmark' title='Permanent Link: The problem with South African genealogy'>The problem with South African genealogy</a> <small>South Africans are a bunch of avid genealogists. So much...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>I love being able to trace my family tree, and have used several good genealogy applications which I have found very useful, including Family Tree Legends and Family Tree Builder. The problem I have though, is that they do not always contain everything which I am looking for, so I decided to write a family tree application to plug in the holes.</p>
<p>Family Traces, which you can download the full source for <a href="http://www.sergemeunier.com/downloads/FamilyTraces.zip">here</a>, is a fully functional, albeit bare-boned family tree application, which can import and export Gedcom files, calculate ancestors and descendants, and allow you to edit the family tree. Everything is there for a starting point, from where you can develop further reports and functionality.</p>
<p>All of that will come in later posts in this series though. For now, I am going to focus on the core of the application &#8211; the data layout. This layout is created within an Access database included with the application.</p>
<p>The two main components of any family tree are individuals and families. </p>
<p>Starting with the individual, each person in the family tree will have a record in the database. The most important data we need for an individual are:</p>
<ul>
<li>Individual Id</li>
<li>Name</li>
<li>Birth date</li>
<li>Birth place</li>
<li>Death date</li>
<li>Death place</li>
</ul>
<p>These fields are vital in determining if the person we are looking at is the same person listed in genealogical records, such as birth certificates, genealogical lists etc. </p>
<p>Now data collection for an individual is not restricted to these fields, and you are free to create as many fields as your want for things like physical attributes, cause of death, photos and general notes.</p>
<p>Moving onto families now, a family is what connects different individuals together into a tree.</p>
<p>The main family record will have </p>
<ul>
<li>Family Id</li>
<li>Husband Id</li>
<li>Wife Id</li>
<li>Marriage date</li>
<li>Marriage place</li>
</ul>
<p>This links the husband and wife individual records into the family, and allows you to track the marriage date and place, which is also important in genealogical research, as it may provide crucial hints to the individuals, particularly if the birth and death dates of the individuals are unsure.</p>
<p>The last thing we need is to be able to link individuals as children within a family, and this is done using a separate table, which keeps track of the family id, as well as the individual id of the child, and each family can have multiple individuals attached to it as children.</p>
<p>In the database setup for the application, I also included a field in the individual record for the parent family record, which makes it easier to link the individual to a family as a child when processing the tree. This means that we are able to find the family that the individual is a child of without having to check the family records directly.</p>
<p>Now that is all there is to the core of a genealogy application, so feel free to play around with the application, and I will explain more about how the application works in the next tutorial&#8230;</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.sergemeunier.com%2Fblog%2Fwriting-a-family-tree-application-in-c-the-underlying-structure%2F&amp;linkname=Writing%20a%20family%20tree%20application%20in%20C%23%20%26%238211%3B%20The%20underlying%20structure"><img src="http://www.sergemeunier.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>

<p>Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-sharp-importing-a-gedcom-file-part-1/' rel='bookmark' title='Permanent Link: Writing a family tree application in C# &#8211; Importing a Gedcom file &#8211; Part 1'>Writing a family tree application in C# &#8211; Importing a Gedcom file &#8211; Part 1</a> <small>In my previous post in this series, I introduced the...</small></li>
<li><a href='http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-sharp-importing-a-gedcom-file-part-2/' rel='bookmark' title='Permanent Link: Writing a family tree application in C# &#8211; Importing a Gedcom file &#8211; Part 2'>Writing a family tree application in C# &#8211; Importing a Gedcom file &#8211; Part 2</a> <small>Now that we know how gedcom files work (covered in...</small></li>
<li><a href='http://www.sergemeunier.com/blog/the-problem-with-south-african-genealogy/' rel='bookmark' title='Permanent Link: The problem with South African genealogy'>The problem with South African genealogy</a> <small>South Africans are a bunch of avid genealogists. So much...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.sergemeunier.com/blog/writing-a-family-tree-application-in-c-the-underlying-structure/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Image Processing in C#: Decreasing the colour depth</title>
		<link>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-decreasing-the-colour-depth/</link>
		<comments>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-decreasing-the-colour-depth/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 05:48:21 +0000</pubDate>
		<dc:creator>Serge Meunier</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Image processing]]></category>

		<guid isPermaLink="false">http://www.sergemeunier.com/blog/?p=1592</guid>
		<description><![CDATA[Decreasing the colour depth involves converting colour values to standard values.
Specifying an offset, preferably a value evenly divisible by 256, such as 16, 24 or 32, we then need to make sure that the red, green and blue components&#8217; values get rounded off to multiples of this offset.
For example, using an offset of 16, value [...]


Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-colour-filters/' rel='bookmark' title='Permanent Link: Image Processing in C#: Colour filters'>Image Processing in C#: Colour filters</a> <small>Implementing a colour filter is a rather simple affair. All...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-contrast/' rel='bookmark' title='Permanent Link: Image Processing in C#: Contrast'>Image Processing in C#: Contrast</a> <small>Contrast in a picture is a measure of how close...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-converting-an-image-to-greyscale/' rel='bookmark' title='Permanent Link: Image Processing in C#: Converting an image to greyscale'>Image Processing in C#: Converting an image to greyscale</a> <small>Converting an image to greyscale is a relatively easy effect...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Decreasing the colour depth involves converting colour values to standard values.</p>
<p>Specifying an offset, preferably a value evenly divisible by 256, such as 16, 24 or 32, we then need to make sure that the red, green and blue components&#8217; values get rounded off to multiples of this offset.</p>
<p>For example, using an offset of 16, value colour values would be 0, 15, 31, 47,&#8230;&#8230;, 255. and all values would need to be rounded off to these values for each of the colour components.</p>
<p>To be able to do this, we take the component value, and then add half of the offset to the value. We then subtract the modulo of this value and the offset. This then gives values along the lines of 0, 16, 32, &#8230;., 256, so we will need to subtract 1 from this, but will need to put in a correction for 0, since that value needs to remain as it is.<br />
<div id="attachment_1593" class="wp-caption aligncenter" style="width: 490px"><img src="http://www.sergemeunier.com/blog/wp-content/uploads/2010/01/Garfield-DecreaseColorDepth.jpg" alt="Decreasing the colour depth using an offset of 16" title="Decreasing the colour depth using an offset of 16" width="480" height="180" class="size-full wp-image-1593" /><p class="wp-caption-text">Decreasing the colour depth using an offset of 16</p></div><br />
You can download the full code for the sample application which contains code for all the image effects covered in the series <a href="http://www.sergemeunier.com/downloads/ImageProcessor.zip">here</a>.</p>
<pre name="code" class="C-Sharp">
public void ApplyDecreaseColourDepth(int offset)
{
    int A, R, G, B;
    Color pixelColor;

    for (int y = 0; y < bitmapImage.Height; y++)
    {
        for (int x = 0; x < bitmapImage.Width; x++)
        {
            pixelColor = bitmapImage.GetPixel(x, y);
            A = pixelColor.A;
            R = ((pixelColor.R + (offset / 2)) - ((pixelColor.R + (offset / 2)) % offset) - 1);
            if (R < 0)
            {
                R = 0;
            }
            G = ((pixelColor.G + (offset / 2)) - ((pixelColor.G + (offset / 2)) % offset) - 1);
            if (G < 0)
            {
                G = 0;
            }
            B = ((pixelColor.B + (offset / 2)) - ((pixelColor.B + (offset / 2)) % offset) - 1);
            if (B < 0)
            {
                B = 0;
            }
            bitmapImage.SetPixel(x, y, Color.FromArgb(A, R, G, B));
        }
    }

}
</pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.sergemeunier.com%2Fblog%2Fimage-processing-in-c-sharp-decreasing-the-colour-depth%2F&amp;linkname=Image%20Processing%20in%20C%23%3A%20Decreasing%20the%20colour%20depth"><img src="http://www.sergemeunier.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>

<p>Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-colour-filters/' rel='bookmark' title='Permanent Link: Image Processing in C#: Colour filters'>Image Processing in C#: Colour filters</a> <small>Implementing a colour filter is a rather simple affair. All...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-contrast/' rel='bookmark' title='Permanent Link: Image Processing in C#: Contrast'>Image Processing in C#: Contrast</a> <small>Contrast in a picture is a measure of how close...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-converting-an-image-to-greyscale/' rel='bookmark' title='Permanent Link: Image Processing in C#: Converting an image to greyscale'>Image Processing in C#: Converting an image to greyscale</a> <small>Converting an image to greyscale is a relatively easy effect...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-decreasing-the-colour-depth/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Image Processing in C#: Getting that old-time effect with sepia</title>
		<link>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-getting-that-old-time-effect-with-sepia/</link>
		<comments>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-getting-that-old-time-effect-with-sepia/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 05:38:22 +0000</pubDate>
		<dc:creator>Serge Meunier</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Image processing]]></category>

		<guid isPermaLink="false">http://www.sergemeunier.com/blog/?p=1588</guid>
		<description><![CDATA[Back in the old days, and here I am talking about the 19th century old days, it was very common to apply a sepia tinting to photographs. This was a chemical process, which preserved the photos for longer, although it was also used for its aesthetic appeal.
These days, with digital photograpy, we don&#8217;t need to [...]


Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-converting-an-image-to-greyscale/' rel='bookmark' title='Permanent Link: Image Processing in C#: Converting an image to greyscale'>Image Processing in C#: Converting an image to greyscale</a> <small>Converting an image to greyscale is a relatively easy effect...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-colour-filters/' rel='bookmark' title='Permanent Link: Image Processing in C#: Colour filters'>Image Processing in C#: Colour filters</a> <small>Implementing a colour filter is a rather simple affair. All...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-adjusting-the-brightness/' rel='bookmark' title='Permanent Link: Image Processing in C#: Adjusting the brightness'>Image Processing in C#: Adjusting the brightness</a> <small>To increase the brightness of an image, all you need...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Back in the old days, and here I am talking about the 19th century old days, it was very common to apply a sepia tinting to photographs. This was a chemical process, which preserved the photos for longer, although it was also used for its aesthetic appeal.</p>
<p>These days, with digital photograpy, we don&#8217;t need to preserve photos like in those early days, but it still does give a nice warm tone to a photo, and is great for making a modern photo appear old-fashioned.</p>
<p>Converting a photo to sepia is a relatively easy effect. </p>
<p>For each pixel in the image, we first convert the pixel to greyscale. We then add a value to the green component, and double that value to the red component to give the sepia effect.</p>
<p>The depth of the sepia effect is determined by this value, which we pass here as a parameter. A value of 0 will give a standard greyscale image.<br />
<div id="attachment_1589" class="wp-caption aligncenter" style="width: 490px"><img src="http://www.sergemeunier.com/blog/wp-content/uploads/2010/01/Garfield-Sepia.jpg" alt="Applying a Sepia value of 20" title="Applying a Sepia value of 20" width="480" height="180" class="size-full wp-image-1589" /><p class="wp-caption-text">Applying a Sepia value of 20</p></div><br />
You can download the full code for the sample application which contains code for all the image effects covered in the series <a href="http://www.sergemeunier.com/downloads/ImageProcessor.zip">here</a>.</p>
<pre name="code" class="C-Sharp">
public void ApplySepia(int depth)
{
    int A, R, G, B;
    Color pixelColor;

    for (int y = 0; y < bitmapImage.Height; y++)
    {
        for (int x = 0; x < bitmapImage.Width; x++)
        {
            pixelColor = bitmapImage.GetPixel(x, y);
            A = pixelColor.A;
            R = (int)((0.299 * pixelColor.R) + (0.587 * pixelColor.G) + (0.114 * pixelColor.B));
            G = B = R;

            R += (depth * 2);
            if (R > 255)
            {
                R = 255;
            }
            G += depth;
            if (G > 255)
            {
                G = 255;
            }

            bitmapImage.SetPixel(x, y, Color.FromArgb(A, R, G, B));
        }
    }
}
</pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.sergemeunier.com%2Fblog%2Fimage-processing-in-c-sharp-getting-that-old-time-effect-with-sepia%2F&amp;linkname=Image%20Processing%20in%20C%23%3A%20Getting%20that%20old-time%20effect%20with%20sepia"><img src="http://www.sergemeunier.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>

<p>Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-converting-an-image-to-greyscale/' rel='bookmark' title='Permanent Link: Image Processing in C#: Converting an image to greyscale'>Image Processing in C#: Converting an image to greyscale</a> <small>Converting an image to greyscale is a relatively easy effect...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-colour-filters/' rel='bookmark' title='Permanent Link: Image Processing in C#: Colour filters'>Image Processing in C#: Colour filters</a> <small>Implementing a colour filter is a rather simple affair. All...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-adjusting-the-brightness/' rel='bookmark' title='Permanent Link: Image Processing in C#: Adjusting the brightness'>Image Processing in C#: Adjusting the brightness</a> <small>To increase the brightness of an image, all you need...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-getting-that-old-time-effect-with-sepia/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Image Processing in C#: Embossing</title>
		<link>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-embossing/</link>
		<comments>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-embossing/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 05:52:03 +0000</pubDate>
		<dc:creator>Serge Meunier</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Image processing]]></category>

		<guid isPermaLink="false">http://www.sergemeunier.com/blog/?p=1584</guid>
		<description><![CDATA[One of the most striking effects to apply to an image is embossing. This is effect is a form of edge-detection, and is also done using convolution, which you can read more about on my post on Smoothing using convolution.
The convolution grid we use in embossing is a little different to the others we have [...]


Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-sharpening-the-image/' rel='bookmark' title='Permanent Link: Image Processing in C#: Sharpening the image'>Image Processing in C#: Sharpening the image</a> <small>Sharpening an image is merely the inverse of what we...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-smoothing-using-convolution/' rel='bookmark' title='Permanent Link: Image Processing in C#: Smoothing using convolution'>Image Processing in C#: Smoothing using convolution</a> <small>Smoothing, which is just a type of blurring is based...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-applying-a-mean-removal/' rel='bookmark' title='Permanent Link: Image Processing in C#: Applying a mean removal'>Image Processing in C#: Applying a mean removal</a> <small>Another type of sharpening effect is mean removal. It works...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>One of the most striking effects to apply to an image is embossing. This is effect is a form of edge-detection, and is also done using convolution, which you can read more about on my post on <a href="http://www.sergemeunier.com/blog/image-processing-in-c-sharp-smoothing-using-convolution/">Smoothing using convolution</a>.</p>
<p>The convolution grid we use in embossing is a little different to the others we have looked at so far. It has an offset of 127, and a fixed factor of 4. The offset makes the the image appear on average as a medium gray.</p>
<p>The grid we will use then is</p>
<table border='1'>
<tr>
<td>-1</td>
<td>0</td>
<td>-1</td>
</tr>
<tr>
<td>0</td>
<td>4</td>
<td>0</td>
</tr>
<tr>
<td>-1</td>
<td>0</td>
<td>-1</td>
</tr>
</table>
<p><div id="attachment_1585" class="wp-caption aligncenter" style="width: 490px"><img src="http://www.sergemeunier.com/blog/wp-content/uploads/2010/01/Garfield-Emboss.jpg" alt="Embossing" title="Embossing" width="480" height="180" class="size-full wp-image-1585" /><p class="wp-caption-text">Embossing</p></div><br />
You can download the full code for the sample application which contains code for all the image effects covered in the series <a href="http://www.sergemeunier.com/downloads/ImageProcessor.zip">here</a>.</p>
<pre name="code" class="C-Sharp">
public void ApplyEmboss(double weight)
{
    ConvolutionMatrix matrix = new ConvolutionMatrix(3);
    matrix.SetAll(1);
    matrix.Matrix[0, 0] = -1;
    matrix.Matrix[1, 0] = 0;
    matrix.Matrix[2, 0] = -1;
    matrix.Matrix[0, 1] = 0;
    matrix.Matrix[1, 1] = weight;
    matrix.Matrix[2, 1] = 0;
    matrix.Matrix[0, 2] = -1;
    matrix.Matrix[1, 2] = 0;
    matrix.Matrix[2, 2] = -1;
    matrix.Factor = 4;
    matrix.Offset = 127;
    bitmapImage = Convolution3x3(bitmapImage, matrix);

}
</pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.sergemeunier.com%2Fblog%2Fimage-processing-in-c-sharp-embossing%2F&amp;linkname=Image%20Processing%20in%20C%23%3A%20Embossing"><img src="http://www.sergemeunier.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>

<p>Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-sharpening-the-image/' rel='bookmark' title='Permanent Link: Image Processing in C#: Sharpening the image'>Image Processing in C#: Sharpening the image</a> <small>Sharpening an image is merely the inverse of what we...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-smoothing-using-convolution/' rel='bookmark' title='Permanent Link: Image Processing in C#: Smoothing using convolution'>Image Processing in C#: Smoothing using convolution</a> <small>Smoothing, which is just a type of blurring is based...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-applying-a-mean-removal/' rel='bookmark' title='Permanent Link: Image Processing in C#: Applying a mean removal'>Image Processing in C#: Applying a mean removal</a> <small>Another type of sharpening effect is mean removal. It works...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-embossing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Image Processing in C#: Applying a mean removal</title>
		<link>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-applying-a-mean-removal/</link>
		<comments>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-applying-a-mean-removal/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 05:42:58 +0000</pubDate>
		<dc:creator>Serge Meunier</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Image processing]]></category>

		<guid isPermaLink="false">http://www.sergemeunier.com/blog/?p=1578</guid>
		<description><![CDATA[Another type of sharpening effect is mean removal. It works similarly to the standard sharpening, except it subtracts values from all the surrounding pixels evenly.
The grid which we used for this effect is as follows


-1
-1
-1


-1
11
-1


-1
-1
-1


This gives a much more noticeable sharpening effect on the image.
You can download the full code for the sample application which [...]


Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-sharpening-the-image/' rel='bookmark' title='Permanent Link: Image Processing in C#: Sharpening the image'>Image Processing in C#: Sharpening the image</a> <small>Sharpening an image is merely the inverse of what we...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-embossing/' rel='bookmark' title='Permanent Link: Image Processing in C#: Embossing'>Image Processing in C#: Embossing</a> <small>One of the most striking effects to apply to an...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-smoothing-using-convolution/' rel='bookmark' title='Permanent Link: Image Processing in C#: Smoothing using convolution'>Image Processing in C#: Smoothing using convolution</a> <small>Smoothing, which is just a type of blurring is based...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Another type of sharpening effect is mean removal. It works similarly to the <a href="http://www.sergemeunier.com/blog/image-processing-in-c-sharp-sharpening-the-image/">standard sharpening</a>, except it subtracts values from all the surrounding pixels evenly.</p>
<p>The grid which we used for this effect is as follows</p>
<table border='1'>
<tr>
<td>-1</td>
<td>-1</td>
<td>-1</td>
</tr>
<tr>
<td>-1</td>
<td>11</td>
<td>-1</td>
</tr>
<tr>
<td>-1</td>
<td>-1</td>
<td>-1</td>
</tr>
</table>
<p>This gives a much more noticeable sharpening effect on the image.</p>
<p><div id="attachment_1579" class="wp-caption aligncenter" style="width: 490px"><img src="http://www.sergemeunier.com/blog/wp-content/uploads/2010/01/Garfield-MeanRemoval.jpg" alt="Mean removal" title="Mean removal" width="480" height="180" class="size-full wp-image-1579" /><p class="wp-caption-text">Mean removal</p></div><br />
You can download the full code for the sample application which contains code for all the image effects covered in the series <a href="http://www.sergemeunier.com/downloads/ImageProcessor.zip">here</a>.</p>
<pre name="code" class="C-Sharp">
public void ApplyMeanRemoval(double weight)
{
    ConvolutionMatrix matrix = new ConvolutionMatrix(3);
    matrix.SetAll(1);
    matrix.Matrix[0, 0] = -1;
    matrix.Matrix[1, 0] = -1;
    matrix.Matrix[2, 0] = -1;
    matrix.Matrix[0, 1] = -1;
    matrix.Matrix[1, 1] = weight;
    matrix.Matrix[2, 1] = -1;
    matrix.Matrix[0, 2] = -1;
    matrix.Matrix[1, 2] = -1;
    matrix.Matrix[2, 2] = -1;
    matrix.Factor = weight - 8;
    bitmapImage = Convolution3x3(bitmapImage, matrix);

}
</pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.sergemeunier.com%2Fblog%2Fimage-processing-in-c-sharp-applying-a-mean-removal%2F&amp;linkname=Image%20Processing%20in%20C%23%3A%20Applying%20a%20mean%20removal"><img src="http://www.sergemeunier.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>

<p>Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-sharpening-the-image/' rel='bookmark' title='Permanent Link: Image Processing in C#: Sharpening the image'>Image Processing in C#: Sharpening the image</a> <small>Sharpening an image is merely the inverse of what we...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-embossing/' rel='bookmark' title='Permanent Link: Image Processing in C#: Embossing'>Image Processing in C#: Embossing</a> <small>One of the most striking effects to apply to an...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-smoothing-using-convolution/' rel='bookmark' title='Permanent Link: Image Processing in C#: Smoothing using convolution'>Image Processing in C#: Smoothing using convolution</a> <small>Smoothing, which is just a type of blurring is based...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-applying-a-mean-removal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Image Processing in C#: Sharpening the image</title>
		<link>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-sharpening-the-image/</link>
		<comments>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-sharpening-the-image/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 05:36:04 +0000</pubDate>
		<dc:creator>Serge Meunier</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Image processing]]></category>

		<guid isPermaLink="false">http://www.sergemeunier.com/blog/?p=1573</guid>
		<description><![CDATA[Sharpening an image is merely the inverse of what we did to blur the image. It also uses the convolution function, which you can read in my post about Smoothing using convolution.
Here we supply a convolution matrix set up as follows:


0
-2
0


-2
11
-2


0
-2
0


Here our weighting factor would total 3, since the negative numbers get subtracted from the [...]


Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-embossing/' rel='bookmark' title='Permanent Link: Image Processing in C#: Embossing'>Image Processing in C#: Embossing</a> <small>One of the most striking effects to apply to an...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-smoothing-using-convolution/' rel='bookmark' title='Permanent Link: Image Processing in C#: Smoothing using convolution'>Image Processing in C#: Smoothing using convolution</a> <small>Smoothing, which is just a type of blurring is based...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-applying-a-mean-removal/' rel='bookmark' title='Permanent Link: Image Processing in C#: Applying a mean removal'>Image Processing in C#: Applying a mean removal</a> <small>Another type of sharpening effect is mean removal. It works...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Sharpening an image is merely the inverse of what we did to blur the image. It also uses the convolution function, which you can read in my post about <a href="http://www.sergemeunier.com/blog/image-processing-in-c-sharp-smoothing-using-convolution/">Smoothing using convolution</a>.</p>
<p>Here we supply a convolution matrix set up as follows:</p>
<table border='1'>
<tr>
<td>0</td>
<td>-2</td>
<td>0</td>
</tr>
<tr>
<td>-2</td>
<td>11</td>
<td>-2</td>
</tr>
<tr>
<td>0</td>
<td>-2</td>
<td>0</td>
</tr>
</table>
<p>Here our weighting factor would total 3, since the negative numbers get subtracted from the weighting total. </p>
<p><div id="attachment_1574" class="wp-caption aligncenter" style="width: 490px"><img src="http://www.sergemeunier.com/blog/wp-content/uploads/2010/01/Garfield-Sharpen.jpg" alt="Shapening our image" title="Shapening our image" width="480" height="180" class="size-full wp-image-1574" /><p class="wp-caption-text">Shapening our image</p></div><br />
You can download the full code for the sample application which contains code for all the image effects covered in the series <a href="http://www.sergemeunier.com/downloads/ImageProcessor.zip">here</a>.</p>
<pre name="code" class="C-Sharp">
public void ApplySharpen(double weight)
{
    ConvolutionMatrix matrix = new ConvolutionMatrix(3);
    matrix.SetAll(1);
    matrix.Matrix[0, 0] = 0;
    matrix.Matrix[1, 0] = -2;
    matrix.Matrix[2, 0] = 0;
    matrix.Matrix[0, 1] = -2;
    matrix.Matrix[1, 1] = weight;
    matrix.Matrix[2, 1] = -2;
    matrix.Matrix[0, 2] = 0;
    matrix.Matrix[1, 2] = -2;
    matrix.Matrix[2, 2] = 0;
    matrix.Factor = weight - 8;
    bitmapImage = Convolution3x3(bitmapImage, matrix);

}
</pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.sergemeunier.com%2Fblog%2Fimage-processing-in-c-sharp-sharpening-the-image%2F&amp;linkname=Image%20Processing%20in%20C%23%3A%20Sharpening%20the%20image"><img src="http://www.sergemeunier.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>

<p>Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-embossing/' rel='bookmark' title='Permanent Link: Image Processing in C#: Embossing'>Image Processing in C#: Embossing</a> <small>One of the most striking effects to apply to an...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-smoothing-using-convolution/' rel='bookmark' title='Permanent Link: Image Processing in C#: Smoothing using convolution'>Image Processing in C#: Smoothing using convolution</a> <small>Smoothing, which is just a type of blurring is based...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-applying-a-mean-removal/' rel='bookmark' title='Permanent Link: Image Processing in C#: Applying a mean removal'>Image Processing in C#: Applying a mean removal</a> <small>Another type of sharpening effect is mean removal. It works...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-sharpening-the-image/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Image Processing in C#: Gaussian blur</title>
		<link>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-gaussian-blur/</link>
		<comments>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-gaussian-blur/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 05:39:57 +0000</pubDate>
		<dc:creator>Serge Meunier</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Image processing]]></category>

		<guid isPermaLink="false">http://www.sergemeunier.com/blog/?p=1565</guid>
		<description><![CDATA[Gaussian blur also uses convolution to create the effect. If you have not yet read my blog post on Smoothing using convolution, I would advise you to read that post first, as in that post I explain how the convolution function works.
Gaussian blur works very similarly to the smoothing function, in that both are a [...]


Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-sharpening-the-image/' rel='bookmark' title='Permanent Link: Image Processing in C#: Sharpening the image'>Image Processing in C#: Sharpening the image</a> <small>Sharpening an image is merely the inverse of what we...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-embossing/' rel='bookmark' title='Permanent Link: Image Processing in C#: Embossing'>Image Processing in C#: Embossing</a> <small>One of the most striking effects to apply to an...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-smoothing-using-convolution/' rel='bookmark' title='Permanent Link: Image Processing in C#: Smoothing using convolution'>Image Processing in C#: Smoothing using convolution</a> <small>Smoothing, which is just a type of blurring is based...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Gaussian blur also uses convolution to create the effect. If you have not yet read my blog post on <a href="http://www.sergemeunier.com/blog/image-processing-in-c-sharp-smoothing-using-convolution/">Smoothing using convolution</a>, I would advise you to read that post first, as in that post I explain how the convolution function works.</p>
<p>Gaussian blur works very similarly to the smoothing function, in that both are a type of blur, but the Gaussian blur gives a more natural looking blur. </p>
<p>It achieves this by not have a flat weighting, but instead has a weighting based on a Gaussian curve, thus for our 3&#215;3 grid, the values we want to supply the convolution function is as follows:</p>
<table border='1'>
<tr>
<td>1</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>4</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>1</td>
</tr>
</table>
<p>The factor parameter is set to the sum of each pixel in the grid, which in this case, is again, 16, and the offset is set to 0.</p>
<p>Now we get a nice blur effect.<br />
<div id="attachment_1566" class="wp-caption aligncenter" style="width: 490px"><img src="http://www.sergemeunier.com/blog/wp-content/uploads/2010/01/Garfield-Guassian.jpg" alt="Applying a guassian blur" title="Applying a guassian blur" width="480" height="180" class="size-full wp-image-1566" /><p class="wp-caption-text">Applying a guassian blur</p></div><br />
You can download the full code for the sample application which contains code for all the image effects covered in the series <a href="http://www.sergemeunier.com/downloads/ImageProcessor.zip">here</a>.</p>
<pre name="code" class="C-Sharp">
public void ApplyGaussianBlur(double peakValue)
{
    ConvolutionMatrix matrix = new ConvolutionMatrix(3);
    matrix.SetAll(1);
    matrix.Matrix[0, 0] = peakValue / 4;
    matrix.Matrix[1, 0] = peakValue / 2;
    matrix.Matrix[2, 0] = peakValue / 4;
    matrix.Matrix[0, 1] = peakValue / 2;
    matrix.Matrix[1, 1] = peakValue;
    matrix.Matrix[2, 1] = peakValue / 2;
    matrix.Matrix[0, 2] = peakValue / 4;
    matrix.Matrix[1, 2] = peakValue / 2;
    matrix.Matrix[2, 2] = peakValue / 4;
    matrix.Factor = peakValue * 4;
    bitmapImage = Convolution3x3(bitmapImage, matrix);

}
</pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.sergemeunier.com%2Fblog%2Fimage-processing-in-c-sharp-gaussian-blur%2F&amp;linkname=Image%20Processing%20in%20C%23%3A%20Gaussian%20blur"><img src="http://www.sergemeunier.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>

<p>Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-sharpening-the-image/' rel='bookmark' title='Permanent Link: Image Processing in C#: Sharpening the image'>Image Processing in C#: Sharpening the image</a> <small>Sharpening an image is merely the inverse of what we...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-embossing/' rel='bookmark' title='Permanent Link: Image Processing in C#: Embossing'>Image Processing in C#: Embossing</a> <small>One of the most striking effects to apply to an...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-smoothing-using-convolution/' rel='bookmark' title='Permanent Link: Image Processing in C#: Smoothing using convolution'>Image Processing in C#: Smoothing using convolution</a> <small>Smoothing, which is just a type of blurring is based...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-gaussian-blur/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Image Processing in C#: Smoothing using convolution</title>
		<link>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-smoothing-using-convolution/</link>
		<comments>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-smoothing-using-convolution/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 05:30:30 +0000</pubDate>
		<dc:creator>Serge Meunier</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Image processing]]></category>

		<guid isPermaLink="false">http://www.sergemeunier.com/blog/?p=1559</guid>
		<description><![CDATA[Smoothing, which is just a type of blurring is based on a concept called convolution.
How this works is that you take a square section of pixels, often 3&#215;3 but can be any size, although the larger you make the grid, the less pixels that will get affected by the effect around the edges.
The centre pixel [...]


Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-embossing/' rel='bookmark' title='Permanent Link: Image Processing in C#: Embossing'>Image Processing in C#: Embossing</a> <small>One of the most striking effects to apply to an...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-sharpening-the-image/' rel='bookmark' title='Permanent Link: Image Processing in C#: Sharpening the image'>Image Processing in C#: Sharpening the image</a> <small>Sharpening an image is merely the inverse of what we...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-gaussian-blur/' rel='bookmark' title='Permanent Link: Image Processing in C#: Gaussian blur'>Image Processing in C#: Gaussian blur</a> <small>Gaussian blur also uses convolution to create the effect. If...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Smoothing, which is just a type of blurring is based on a concept called convolution.</p>
<p>How this works is that you take a square section of pixels, often 3&#215;3 but can be any size, although the larger you make the grid, the less pixels that will get affected by the effect around the edges.</p>
<p>The centre pixel of the grid is the main pixel we are interested in. To use convolution, what we do is assign weightings to each pixel in the grid, depending on what effect we are trying to achieve, and then assign the result to the centre pixel.</p>
<p>The basic data structure we use for the convolution is a class which contains the 2D array, as well as a weighting factor, and an offset.</p>
<pre name="code" class="C-Sharp">
    public class ConvolutionMatrix
    {
        public int MatrixSize = 3;

        public double[,] Matrix;
        public double Factor = 1;
        public double Offset = 1;

        public ConvolutionMatrix(int size)
        {
            MatrixSize = 3;
            Matrix = new double[size, size];
        }

        public void SetAll(double value)
        {
            for (int i = 0; i < MatrixSize; i++)
            {
                for (int j = 0; j < MatrixSize; j++)
                {
                    Matrix[i, j] = value;
                }
            }
        }
    }
</pre>
<p>Now, having this class, we can implement the convolution algorithm</p>
<pre name="code" class="C-Sharp">
public Bitmap Convolution3x3(Bitmap b, ConvolutionMatrix m)
{
    Bitmap newImg = (Bitmap)b.Clone();
    Color[,] pixelColor = new Color[3, 3];
    int A, R, G, B;

    for (int y = 0; y < b.Height - 2; y++)
    {
        for (int x = 0; x < b.Width - 2; x++)
        {
            pixelColor[0, 0] = b.GetPixel(x, y);
            pixelColor[0, 1] = b.GetPixel(x, y + 1);
            pixelColor[0, 2] = b.GetPixel(x, y + 2);
            pixelColor[1, 0] = b.GetPixel(x + 1, y);
            pixelColor[1, 1] = b.GetPixel(x + 1, y + 1);
            pixelColor[1, 2] = b.GetPixel(x + 1, y + 2);
            pixelColor[2, 0] = b.GetPixel(x + 2, y);
            pixelColor[2, 1] = b.GetPixel(x + 2, y + 1);
            pixelColor[2, 2] = b.GetPixel(x + 2, y + 2);

            A = pixelColor[1, 1].A;

            R = (int)((((pixelColor[0, 0].R * m.Matrix[0, 0]) +
                         (pixelColor[1, 0].R * m.Matrix[1, 0]) +
                         (pixelColor[2, 0].R * m.Matrix[2, 0]) +
                         (pixelColor[0, 1].R * m.Matrix[0, 1]) +
                         (pixelColor[1, 1].R * m.Matrix[1, 1]) +
                         (pixelColor[2, 1].R * m.Matrix[2, 1]) +
                         (pixelColor[0, 2].R * m.Matrix[0, 2]) +
                         (pixelColor[1, 2].R * m.Matrix[1, 2]) +
                         (pixelColor[2, 2].R * m.Matrix[2, 2]))
                                / m.Factor) + m.Offset);

            if (R < 0)
            {
                R = 0;
            }
            else if (R > 255)
            {
                R = 255;
            }

            G = (int)((((pixelColor[0, 0].G * m.Matrix[0, 0]) +
                         (pixelColor[1, 0].G * m.Matrix[1, 0]) +
                         (pixelColor[2, 0].G * m.Matrix[2, 0]) +
                         (pixelColor[0, 1].G * m.Matrix[0, 1]) +
                         (pixelColor[1, 1].G * m.Matrix[1, 1]) +
                         (pixelColor[2, 1].G * m.Matrix[2, 1]) +
                         (pixelColor[0, 2].G * m.Matrix[0, 2]) +
                         (pixelColor[1, 2].G * m.Matrix[1, 2]) +
                         (pixelColor[2, 2].G * m.Matrix[2, 2]))
                                / m.Factor) + m.Offset);

            if (G < 0)
            {
                G = 0;
            }
            else if (G > 255)
            {
                G = 255;
            }

            B = (int)((((pixelColor[0, 0].B * m.Matrix[0, 0]) +
                         (pixelColor[1, 0].B * m.Matrix[1, 0]) +
                         (pixelColor[2, 0].B * m.Matrix[2, 0]) +
                         (pixelColor[0, 1].B * m.Matrix[0, 1]) +
                         (pixelColor[1, 1].B * m.Matrix[1, 1]) +
                         (pixelColor[2, 1].B * m.Matrix[2, 1]) +
                         (pixelColor[0, 2].B * m.Matrix[0, 2]) +
                         (pixelColor[1, 2].B * m.Matrix[1, 2]) +
                         (pixelColor[2, 2].B * m.Matrix[2, 2]))
                                / m.Factor) + m.Offset);

            if (B < 0)
            {
                B = 0;
            }
            else if (B > 255)
            {
                B = 255;
            }
            newImg.SetPixel(x+1, y+1, Color.FromArgb(A, R, G, B));
        }
    }
    return newImg;
}
</pre>
<p>Finally, after all that, we are ready to implement a smoothing function. To smooth an image, we assign a particular value to the centre pixel, and then 1 to all the surrounding pixels, so the array would look something like this</p>
<table border='1'>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>8</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
</table>
<p>The factor parameter is set to the sum of each pixel in the grid, which in this case would be 16, and the offset is set to 0. The offset shifts all the values of the colour components by the specified amount.</p>
<p>Now all that is left is calling the convolution function.<br />
<div id="attachment_1560" class="wp-caption aligncenter" style="width: 490px"><img src="http://www.sergemeunier.com/blog/wp-content/uploads/2010/01/Garfield-Smooth.jpg" alt="Smoothing" title="Smoothing" width="480" height="180" class="size-full wp-image-1560" /><p class="wp-caption-text">Smoothing</p></div><br />
You can download the full code for the sample application which contains code for all the image effects covered in the series <a href="http://www.sergemeunier.com/downloads/ImageProcessor.zip">here</a>.</p>
<pre name="code" class="C-Sharp">
        public void ApplySmooth(double weight)
        {
            ConvolutionMatrix matrix = new ConvolutionMatrix(3);
            matrix.SetAll(1);
            matrix.Matrix[1,1] = weight;
            matrix.Factor = weight + 8;
            bitmapImage = Convolution3x3(bitmapImage, matrix);

        }
</pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.sergemeunier.com%2Fblog%2Fimage-processing-in-c-sharp-smoothing-using-convolution%2F&amp;linkname=Image%20Processing%20in%20C%23%3A%20Smoothing%20using%20convolution"><img src="http://www.sergemeunier.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>

<p>Related posts:<ol><li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-embossing/' rel='bookmark' title='Permanent Link: Image Processing in C#: Embossing'>Image Processing in C#: Embossing</a> <small>One of the most striking effects to apply to an...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-sharpening-the-image/' rel='bookmark' title='Permanent Link: Image Processing in C#: Sharpening the image'>Image Processing in C#: Sharpening the image</a> <small>Sharpening an image is merely the inverse of what we...</small></li>
<li><a href='http://www.sergemeunier.com/blog/image-processing-in-c-sharp-gaussian-blur/' rel='bookmark' title='Permanent Link: Image Processing in C#: Gaussian blur'>Image Processing in C#: Gaussian blur</a> <small>Gaussian blur also uses convolution to create the effect. If...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.sergemeunier.com/blog/image-processing-in-c-sharp-smoothing-using-convolution/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
