Cedar City Group: December 2008

Sunday, December 28, 2008

Uncolor My World, The Factory Pattern In Action part 2

For a preview of the result of this rather long post, please visit
http://www.cedarcitygroup.com/digitaldarkroom/uncolormyworld.aspx


In the days of film photography, black and white photos were considered of higher quality than color photos. Many professional photographers, most notably Ansel Adams, would only shoot black and white photos. In the film days, color negatives could be used to make black and white prints by using black and white paper. To achieve the highest quality prints, however, black and white negatives were used.

But in the days of digital photography, how do you convert a color photograph to black and white?

First attempt, Pixel by Pixel
Just to review, each pixel in color jpeg bitmap is represented by four bytes, the color of the pixel is represented by three bytes, one byte for each of the colors (red, green and blue) The fourth bytes is used to control the opacity of the pixel, commonly referred to as the alpha value. Each byte contains a value from 0 to 255. When the red, green and blue components are the same, the result is a shade of gray ranging from 0x000000 for black to 0xFFFFFF for white. The task of converting a color bitmap to a grayscale bitmap involves using the values of the red, green and blue components of a pixel to compute a shade of gray to be used to accurately represent that color. Based on some analysis that I don't completely understand, a formula to do this turns out to be:

shadeofgray = 0.212671f * red + 0.715160f * green + 0.072169f * blue;


When I initially wrote this filter, I implemented it using a brute force method of computing the grayscale value pixel by pixel. This was the primary reason that I added the code to the ImageProcessBase class to allow unsafe code. After doing some research, I found that the .NET framework provides a high speed method of achieving the same effect, using only managed code.

Dust off your Linear Algebra textbook
The System.Drawing namespace provides the ColorMatrix class that can be used to manipulate colors during the rendering of a bitmap. You can find a pretty good explanation of the ColorMatrix class and the mathematics behind it here. For our purposes, I'll just show the code for my grayscale class.



using System;
using System.Collections;
using System.Runtime.Serialization;
using System.Drawing.Imaging;
using System.Drawing;

namespace ccg.imaging
{
///
/// Converts a color image into a grayscale image
///

///
[Serializable]
[NameAttribute("Convert To Grayscale")]
[DescriptionAttribute("Converts the incoming image to a grayscale image.")]
public class Grayscale: ImageProcessBase
{
public Grayscale()
{

}

protected override System.Drawing.Bitmap ProcessSafe(System.Drawing.Bitmap bitmapin)
{

ColorMatrix cm = new ColorMatrix();

cm.Matrix00 = 0.212671f;
cm.Matrix01 = 0.212671f;
cm.Matrix02 = 0.212671f;
cm.Matrix10 = 0.715160f;
cm.Matrix11 = 0.715160f;
cm.Matrix12 = 0.715160f;
cm.Matrix20 = 0.072169f;
cm.Matrix21 = 0.072169f;
cm.Matrix22 = 0.072169f;

System.Drawing.Bitmap outmap =
new System.Drawing.Bitmap(bitmapin.Width,bitmapin.Height);
Graphics g = Graphics.FromImage(outmap);
ImageAttributes ia = new ImageAttributes();
ia.SetColorMatrix(cm);
g.DrawImage(bitmapin,new Rectangle(0,0,outmap.Width,
outmap.Height),0,0,bitmapin.Width,
bitmapin.Height,GraphicsUnit.Pixel,ia);
g.Dispose();

return outmap;
}
}
}


What can Brown do for you?
In the early days of photography, prints were toned with Sepia as a preservation technique. The original black and white prints were soaked in a solution made from the Sepia officinalis cuttlefish that produced prints that were more resistant to breakdown over time. The result was a brown tinted printed instead of gray. The result was beautiful in many ways. Today, many photos are converted to a Sepia tone to simulate this old fashioned beauty. By changing the values in the ColorMatrix, the same code will generate a Sepia toned bitmap.



using System;
using System.Collections;
using System.Runtime.Serialization;
using System.Drawing.Imaging;
using System.Drawing;

namespace ccg.imaging
{
///
/// Converts a color image into a sepia image
///

///
[Serializable]
[NameAttribute("Convert To Sepia")]
[DescriptionAttribute("Converts the incoming image to a sepia image.")]
public class Sepia: ImageProcessBase
{
public Sepia()
{

}

protected override System.Drawing.Bitmap ProcessSafe(System.Drawing.Bitmap bitmapin)
{
ColorMatrix cm = new ColorMatrix();

cm.Matrix00 = 0.393f;
cm.Matrix01 = 0.349f;
cm.Matrix02 = 0.272f;
cm.Matrix10 = 0.769f;
cm.Matrix11 = 0.686f;
cm.Matrix12 = 0.534f;
cm.Matrix20 = 0.189f;
cm.Matrix21 = 0.168f;
cm.Matrix22 = 0.131f;

System.Drawing.Bitmap outmap = new
System.Drawing.Bitmap(bitmapin.Width,bitmapin.Height);
Graphics g = Graphics.FromImage(outmap);
ImageAttributes ia = new ImageAttributes();
ia.SetColorMatrix(cm);
g.DrawImage(bitmapin,new Rectangle(0,0,outmap.Width,
outmap.Height),0,0,bitmapin.Width,bitmapin.Height,GraphicsUnit.Pixel,ia);
g.Dispose();

return outmap;
}
}
}


Keeping it loose
Now that we have a couple of classes that actually manipulate images, it's time to take a look at the factory itself. 'Loose Coupling' is a term that you may hear a lot in development circles. The basic idea is to reduce the amount of information shared between components of an application. This allows changes to be made to one component without forcing changes or even recompilation of other components. This is what the factory pattern is all about.

In this application, I have elected to place all of the shared information in a separate dll called ccg.imaging.foundation. This dll contains all of the base classes and the factory itself (for now). A reference to this dll will need to be added to any application (winforms or webforms) that intends to use the ccg.imaging components. Any dll projects that implement the abstract classes will also need to reference the foundation dll. The important point here is that it is not necessary for the application to directly reference any other imaging dlls. The factory class can use reflection to activate the processing components providing the ability to update the component dlls without recompiling the entire application. I've created a FactoryBase class to provide some basic Factory services.



using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace ccg.imaging
{

public class FilterFactoryBase
{
// a dictionary used to store object specifications
private Dictionary<string, string> _filters = null;

protected FilterFactoryBase()
{
_filters = new Dictionary<string,string>();
}

// a method provided so that the subclassed factory can provide an
// application dependent id for the class, the fully qualified name of
// the class and the path to the assembly that contains the class
protected void AddFilter(string id, string classname, string assemblypath)
{
// store the path and classname in the dictionary using a delimited string
_filters.Add(id, assemblypath + "|" + classname);
}


// a method to activate a class identified by the application specified id
public ImageProcessBase ActivateFilter(string id)
{
ImageProcessBase objfilter = null;

// retrieve the specification from the dictionary
string classspec = _filters[id];
try
{
// if the classspec is found
if (!string.IsNullOrEmpty(classspec))
{
// break the stored spec down into its components (see AddFilter() method)
string[] typespec = classspec.Split('|');
string assemblypath = typespec[0].Trim();
string classname = typespec[1].Trim();

// make sure the needed assembly is loaded
Assembly remoteassembly = Assembly.LoadFrom(assemblypath);
// retrieve the Type from the assembly
Type remotetype = remoteassembly.GetType(classname);
// create an instance of the class
objfilter = (ImageProcessBase)Activator.CreateInstance(remotetype);
}
}
catch
{

}

// will be null if the class is not found or cannot be activated
return objfilter;
}
}
}


Now all the FilterFactory subclass needs to do is provide the application defined id, the class name and assembly path for each class that it wants to activate.



using System;
using System.Collections;
using System.Collections.Generic;

namespace ccg.imaging
{
///
/// This Class provides a means to find out what filters are available
/// and what they do.
///

public class FilterFactory: FilterFactoryBase
{
public FilterFactory()
{
string dllpath = AppDomain.CurrentDomain.BaseDirectory +
"bin\\ccg.imaging.dll";
AddFilter("grayscale", "ccg.imaging.Grayscale", dllpath );
AddFilter("sepia", "ccg.imaging.Sepia", dllpath);
}
}
}


Pulling it all together
http://www.cedarcitygroup.com/digitaldarkroom/uncolormyworld.aspx is an example web page that uses these classes to manipulate a fixed image according to gestures made on the user interface.

The main aspx page places the name of the image to be used in a session variable and provides the user input controls. An image tag is present with its source set to a second aspx page where all the magic happens. When the process button is pressed, the source for the image tag is changed so that the user's selections are passed to the processing page in the form of url parameters. Following is the code for the processing page.



using System.Configuration;
using System.Collections;
using System.Web;
using ccg.imaging;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

public partial class digitaldarkroom_darkroomprocess : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// load the image using a utility class to resolve pathname and whatnot
darkroombl bl = new darkroombl();
Bitmap output = bl.GetImage(Request["image1"].ToString());

// use the colorization command to obtain the object to use
//to implement the command
string colorcommand = Request["color"].ToString().ToLower();

// if the user specifies "full color", then we need to do nothing
if (!string.IsNullOrEmpty(colorcommand) && !colorcommand.Equals("full color"))
{
// instantiate the factory
FilterFactoryBase filterfactory = new FilterFactory();
// activate the class based on the user's input
ImageProcessBase proc = filterfactory.ActivateFilter(colorcommand);
//process the image
output = proc.Process(output);
}

// send the resulting image back as a jpeg
Response.ContentType = "image/jpeg";
MemoryStream ms = new MemoryStream();
output.Save(ms,ImageFormat.Jpeg);
ms.WriteTo(Response.OutputStream);

}
}


What does all this banjo work give us?
So, what is the final result?

1. The application that uses the image process classes does not need to have a reference to the assembly they are located in. Only the ccg.imaging.foundation is referenced by the top level project. Additional filters can be added to the dll without reguiring the application to be rebuilt.

2. The factory class only requires string values to be initialized. Right now, the class specifications are hard coded in the factory's constructor. They could be read from the web.config file or even loaded from a database.

3. The actual processing classes can be moved to different assemblies easily. The factory is the only class that would needed to be updated.

In the next post I'll implement some more filters and look at how reflection can be used to provide some more customizable capabilities.

Thursday, December 18, 2008

The psychology of software development....

I'm going to get back to my series on the factory class soon, but there was something on my mind yesterday that I wanted to post here. Before I start, I'd like to ask a couple of questions:

Have you ever sat in front of you computer, wracking your brain to solve a problem, then have the solution just come to you later when you are doing something totally unrelated to programming?

Have you ever been close to a project deadline and found yourself being totally unproductive because you can't stay focused?

Software development is an amazing pursuit. It requires knowledge, but at its core it requires creativity. Most non-developers don't understand that a developer can have the same relationship with an application that a songwriter has with a song.

The human mind is an amazing thing, as someone who uses their mind as a means of income, I suggest that you be attentive to how your mind works and when you are productive and when you are not.

I know that my experience is not the same as everyone, all of our minds work differently, but this has been my experience.

If I am dealing with a difficult problem, I'll work on it until I realize that I'm not going to find the answer quickly. At that point, if I walk away, and do something that I enjoy, the answer will just 'come to me'. My theory is that the mind keeps working on the problem even though you are consciously working on it, when it finds the answer, it will notify you.

There is tremendous power in explaining the problem to someone else. Find another developer that has time to listen to you. Start explaining the problem to them, most of the time, the process of explaining it to someone else will help you find the issue. My theory: by vocalizing the problem, you move the problem to a different part of the brain that might have a better chance at solving it.

Realistic deadlines motivate, unrealistic deadlines kill productivity. Software development requires creativity. Creativity requires that you be relaxed. Deadlines obviously have to be set, but they have to be realistic. I have found myself many time fighting unrealistic deadlines. What tends to happen is that my mind tends to focus on the deadline instead of the software. Stress kills productivity, do what ever you can to stay relaxed.

Many times over the years, I've been in situations where deadlines were set based on business needs with no respect for the amount of work that needed to be done. Since software is basically invisible, folks that don't write software typically don't understand the amount of effort that it requires. Many times I've had clients give a 15 minute explanation of what they want and the ask "How long will it take?". When asked this question, I usually try to ask questions that help them related to the process of software development. My favorite is: "How long does it take to solve a crossword puzzle?" The answer is: "it depends on the puzzle". That's right, and there are usually puzzles in an application that you can't completely understand until you get into it.

Just a few notes from an old code dog...

Tuesday, December 16, 2008

Google Webmaster Tools....

I spent most of the day today working with Google's web master tools. I've used them before, but they have been expanded greatly. I whipped up a quick program to search a website development directory and generate a google compatible site map (using the factory pattern of course)

I'll probably post the code at some point, I have some ideas on how to improve it. If you have a site, and want to be able to generate a sitemap for it quickly, give me a call.

If you are a webmaster and want to see what the webmaster tools can do for you, go to Google Webmaster Tools.

Friday, December 12, 2008

The Factory Pattern in Action....

In my opinion, the factory pattern is a powerful mechanism for processing data. The cool thing about it is that you can break a complex process down into small manageable pieces. Those pieces can then be applied interchangeably to create a flexible powerful process.

One of my own personal interests for years has image processing. I've been taking photographs since I was a kid. Now that photographs are digital instead of chemical, it presents some really interesting possibilities for the analytical side of the brain.

I've been toying with the idea of creating an automated image processing program for years, I actually had a pretty cool version of it working a few years ago, but I've never been able to polish it up enough to make a product out of it. What I can do though, is write about what I was working on. It may give other people some ideas or teach someone something cool about the .NET framework.

Professional imaging programs like Photoshop and Fireworks provide a "batch" mode where you can determine a set of operations to perform on a group of images. This is very useful feature for webmasters. But how would you design something like that if you needed to go beyond the operations that those programs provide?

My plan was to break any possible operation on an image (resizing, convert to black and white, lighten/darken etc.) into a single object. Each operation would have a common method that performed its operation. The "Process" method would accept an image as a parameter and return the processed image when it was done.

Once a collection of these objects was built, they could be mixed and matched into an image processing "pipeline" that could do some pretty amazing stuff on a large group of images.

Here is an ideal base class for the image processing pipeline....



using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.Serialization;

namespace ccg.imaging
{

[Serializable]
public class IdealImageProcessBase
{
public virtual Bitmap Process(System.Drawing.Bitmap bitmapin)
{
return bitmapin;
}
}
}



Reality quickly sets in
This "do nothing" base class for image processing is simple and works, however, in reality it needs some work.

The problem is, when you are dealing with images, you are dealing with large volumes of data. The System.Drawing.Imaging namespace provides a ton of very powerful features for manipulating images, but at some point, it may be necessary to manipulate the image 1 pixel at a time. The System.Drawing.Bitmap object provides the GetPixel() and SetPixel() methods for doing this, but, being managed code, they get very slow when processing large images.

Take A Walk On The Wild Side
Back in the day before C had anything after it, nothing was 'safe'. Before the days of managed code, you could easily write code that stepped all over itself. The hallmark of the C language was its speed. C was a language with no boundaries, direct manipulation of memory locations provided a tremendous amount of speed and flexibility...and danger. It was very easy to write code that had unintended results, I won't bore you with the details, just suffice it to say that operations as simple as concatenation of strings presented a lot of danger to your application.

Managed code provides a lot of safety for your applications, but the trade off is a lot of overhead. Thankfully, you can still get down to processing memory directly, but it requires some special security privileges and object locking.

Here is my real-world image processing base class...



using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.Serialization;
using System.Security;

namespace ccg.imaging
{
///
/// This is a base class to provide low level image
/// processing services for different
/// filters and image processors.
///

///
[Serializable]
public class ImageProcessBase
{

protected static bool _allowunsafe;
protected static bool _permissionchecked = false;

#region Bitmap Locking and Unlocking

//--- locks the bitmaps data bits so that we can access them more speedily
protected System.Drawing.Imaging.BitmapData LockBits(Bitmap b)
{
// GDI+ still lies to us - the return format is BGR, NOT RGB.
return b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
}

protected virtual void UnlockBits(Bitmap b, BitmapData bmd)
{
b.UnlockBits(bmd);
}

#endregion

public ImageProcessBase()
{
if (!_permissionchecked)
{
_permissionchecked = true;
try
{
System.Security.Permissions.SecurityPermission perm =
new System.Security.Permissions.SecurityPermission(
System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode);
_allowunsafe = true;
}
catch
{
_allowunsafe = false;
}
}
}

public virtual Bitmap Process(System.Drawing.Bitmap bitmapin)
{
if (_allowunsafe)
return ProcessUnsafe(bitmapin);
else
return ProcessSafe(bitmapin);
}

protected virtual Bitmap ProcessUnsafe(Bitmap bitmapin)
{
return ProcessSafe(bitmapin);
}

protected virtual Bitmap ProcessSafe(Bitmap bitmapin)
{
return bitmapin;
}


[DataAttribute(DataAttributeType.Hidden)]
public string FilterName
{
get
{
Type t = this.GetType();
object [] attrs = t.GetCustomAttributes(true);
if (attrs != null)
{
foreach(object attr in attrs)
if (attr is NameAttribute)
return ((NameAttribute)attr).Value;
}
return t.Name;
}
}

[DataAttribute(DataAttributeType.Hidden)]
public string FilterDescription
{
get
{
Type t = this.GetType();
object [] attrs = t.GetCustomAttributes(true);
if (attrs != null)
{
foreach(object attr in attrs)
if (attr is DescriptionAttribute)
return ((DescriptionAttribute)attr).Value;
}
return t.Name;
}
}
}




Now the first ImageProcessor object checks to see if unmanaged code is allowed. By making the _permissionchecked and _allowunsafe properties static, we know that this will only be checked once. The Process() method uses the _allowunsafe property to determine whether or not to call the ProcessUnsafe() method. Since the default implementation of ProcessUnsafe() is to call ProcessSafe(), objects are not required to have an unsafe implementation. The base class provides the methods to Lock and Unlock the bitmap object for unsafe processing and also provides some attribute properties to provide information to the GUI.

All of this will lead to Factory Pattern, I promise, this is just the initial base class implementation. The next installment will cover the implementation of a couple of real processor classes and possibly the factory...

Monday, December 8, 2008

Recommendations....

Many times employers and clients want to see recommendations when they are considering hiring a new employee or contractor. If I have done a project for you, or we have worked together, I would appreciate any glowing compliments that you might have about our time together.

Saturday, December 6, 2008

My Favorite Pattern....


In my first post to this blog I mentioned that you are unlikely to find any ground breaking software ideas. My target audience is the beginning to intermediate programmer that is learning coding and design. I stand by that, that is the spirit with which this is written.

Several years ago, the "Gang of Four" came out with their book "Design Patterns: Elements of Reusable Object-Oriented Software". Much to my chagrin, I've never read the book cover to cover. I have resorted to reading interpretations by others. To be honest, I've only used a few of their patterns in real life. The Factory Method Pattern, however, is one that I find myself using over and over. There is a ton of information available on the net about it. There is nothing new here, but this is my contribution to the reader on how I use it.

The factory method pattern allows multiple similar objects to be created and used by the application, without the application having to know a lot of the details of what the objects are doing. In my opinion, one of the keys to writing a good maintainable application is to remove as much detail as possible from the top level code. The details need to be "hidden" in lower level objects. The trouble with this idea however is that you create many small objects that do similar yet different functions. These objects need to be able to share commonly needed functionality while being able to add their own distinct functionality. This is where the factory method pattern excels.

Here are a few real world examples that I've implemented lately:

An application needs to be able to generate several reports. Each report has its own set of parameters that it needs. Some reports require a specific date, while others need a date range for example. There is a common UI used to provide the parameters for the report, once the needed data is provided, each report does it's own thing in generating the output.

A food broker needs to be able to communicate with multiple external entities (manufacturers, distributors, customers etc.) via EDI. The EDI format is standard, and like all standards, everybody has their own version of it. The application needs to communicate with all external entities using their version of the "standard".

A company that hosts conferences needs to be able to update a Microsoft Dynamics CRM database on site during the conference. They have a custom application that uses bar codes to identify attendees that need to be updated. Several different types of business entities need to be updated. The workstations that the attendees visit communicates with a common server and send transactions depending on the update needed. The server needs to identify the transaction and send the correct update to Dynamics CRM.

Enter the factory method pattern...

The factory method pattern allows several similar yet different classes to be created easily. Each class can share common code (placed in the base abstract class) but has the ability to modify the functionality to meet its individual needs. The factory class is responsible for identifying the needed concrete object and creating it. The controlling code can then use the created object as needed without knowing the specific class that has been created.

In the report example listed above, the base class contains needed UI support routines as well as an abstract GenerateReport() method. Each concrete implementation can override the UI routines as needed as well as implementing the needed report generation. The factory creates the concrete report object based on the user's selection. The controlling code interrogates the created object to find out what parameters are needed and shows or hides input controls based on the information needed. Once the parameters for the report are provided, the GenerateReport() method is called to create the final output.

The EDI example may be obvious, the abstract base class contains common EDI functionality, the concrete classes implement each entities version of the standard. The factory determines who we are talking to and generates the appropriate class. The differences in formats is completely hidden from the controlling application.

In the Microsoft Dynamics CRM example, the server receives a transaction from a workstation, the factory determines what type of concrete class is needed, the concrete class implement the logic needed to update the appropriate type, the abstract class provides support logic needed by all concrete classes.

So what are the benefits of doing things this way? Here are a few off the top of my head:

1. The top level code doesn't contain the detail of what is begin updated and how. The logic at the top level is minimal, understandable and maintainable.

2. For each detailed concrete class, the code required is minimal. If you design the base abstract class well, then you will find that each concrete class contains very little code, only the code required to implement the differences will appear.

Ok, I have to come up with some shortcomings of the pattern...

Hmmm....well, I guess some folks my struggle with inheritance, however, if you are going to be a successful developer using object oriented languages you will need to become very familiar with that. You especially need to be very familiar with some of the qualifiers like public, protected, static and abstract. I love the static keyword, it means very different things depending on where its used.

Documentation is key. It is difficult to look at the code and determine what is going on unless you are familiar with the specific technique. Learn UML, a picture is worth a thousand words goes for documentation as well. Assume that the person coming behind you to maintain your code is a beginner. You will thank me one day because if you stay in this business long enough, there will come a time when you look at your own code and say "What was I thinking?"

Thursday, December 4, 2008

Window Class Name Is Not Valid....

I have a Windows Forms application that I wrote several years ago (my first production C# application actually) that occasionally throws an exception when it starts. The Error message says "Window Class Name Is Not Valid" and it happens when the startup code tries to show the main form.

I did some searching around and found that there are several people that have encountered the same error. In my case, it only happened when I tried to debug the program. It used to happen once, and then I would retry running the program and it would work correctly. I hit the wall though, this time it happened every time I executed the application.

I read this fairly lengthy thread about the problem. The solution that worked for me was turning off the Visual Studio Hosting Process.

Right click on the project that is giving you trouble, select properties, select the Debug tab. The Visual Studio Hosting Process selection is a check box at the end of the list. When I unchecked it, my project would debug every time.