Posts for July 2007

Hobby project - Nikon NEF file reader

So, my new hobby is digital photography. I have bought a slightly used Nikon D50 camera, and has already taken a lot of pictures.

If you're into digital photography, you will know that most cameras can produce both JPG and RAW output, where the JPG images are compressed by the camera firmware before being written to the flash card. The RAW files are uncompressed data as it was sampled by the camera CMOS (typically).  Remembe, JPG is a lossy compression format, so detail will be lost if you just save the JPG's. Therefore, if you are serious about photography, you will want to shoot in RAW.

However, as RAW files are essentially untouched image data, a RAW file needs to be processed by some image processing software before it can be used. This can be a cumbersome process, loading up the camera manufacturers software and maybe Photoshop or another image processing utility, processing each image, perhaps adjusting colors and saving the file into a more common format.

Therefore I have decided to try and create a .NET program that does the conversion for me. For "version 1", it will probably only support the NEF file format that my Nikon produces (Nikon's proprietary RAW format), but perhaps I will add support for others along the way. This program will not be a replacement for professional photo processing software, but is meant to be an easy to use tool, that can be used when one simply needs to convert a NEF file to a JPEG or bitmap representation quickly.

The NEF file format is not documented anywhere that I have been able to find on the web. But, looking at the files, they seem to be based on the TIFF image format, and they carry the TIFF file header.

Also, I have found that Dave Coffin has created an excellent utility for doing just what I want, dcraw. I could just as well just use that, but I think that it will be an interesting challenge to try to implement it myself. But I am quite sure that digging in to the dcraw source code will provide me with most of the details that I need to decode the format. 

Using a HTTP module to enforce a single domain

When I first setup this blog, you could access it on both "" and "". For various reasons, it is best practice to have a single url for the same content (it makes analyzing the site traffic easier, and avoids ambiguous URLs), so I wanted to setup the site to just use the www version of the domain name.

Now, there is a couple of ways you could do this. The easiest way would be to just setup IIS to redirect incoming requests to However, this site is hosted by a hosting company, where I do not have access to the IIS manager. 

Instead, I decided to write a custom HTTP module to do the redirecting. HTTP modules are great, as they allow you to hook into the ASP .NET application pipeline using the evens of the HttpApplication object. This makes it is a very strong tool to build advanced functionality. In this case, I decided to handle the BeginRequest event to check if the domain name is correct. If not, I issue a HTTP Redirect and ends the request. This way, all of the work associated with rendering the page is avoided if we just want to redirect anyway. The code is really simple, and basically goes like this:

         /// <summary>
        /// Handles the BeginRequest event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HandleBeginRequest(object sender, EventArgs e)
            HttpApplication app = (HttpApplication) sender;
            string currentDomain = app.Context.Request.Url.DnsSafeHost.ToLower();
            if ( currentDomain != TargetDomain )
                string newUrl = String.Format("http://{0}/{1}", TargetDomain, app.Request.Url.PathAndQuery);

 Another advantage of this method, is that once it is configured in web.config, it always works. If you had used the IIS method, you would have to remember to configure IIS correctly again if the site switches server.

You can download the code as well as the compiled assembly here: (19,18 kb)

This is a domain enforcement module in its simplest form. It simply looks at all incoming requests, and if they don't match the specified domain, the request is redirected to the corresponding url on the correct domain. Feel free to use the module, expand it or use it as inspiration. Just don't blame me if it kills a kitten.

To use the module, simply add it to the <httpModules> section in web.config:

<add name="DomainEnforcement" type="dr.BlogEngine.DomainEnforcement.DomainEnforcementModule,dr.BlogEngine.DomainEnforcement"/>

Then add an appSetting specifying the domain you would like to use as the primary domain:

  <add key="EnforceDomain" value="" />

That's all there is to it.

Setting up BlogEngine.NET

After deciding to get this blog, I needed to find some software to run it on. I briefly considered writing it myself, but decided that it would be too much effort, and that I probably never would be able to finish it, considering work and other projects.

So I started hunting around for some open source blogging software, and after an hour of googling, I decided on BlogEngine.NET. My main considerations where that it needed to be open source .NET (so I can fix stuff if it breaks), highly customizable and easy to extend. It was also important that it would work without a database, since my current web host only provides access to a sluggish and unreliable MySQL server (but hey, it's cheap ;-)

I also considered dasBlog, but BlogEngine.NET seemed simpler and more suitable for my simple needs. 

Setting stuff up was simpler than expected.  BlogEngine.NET target audience is developers, according to their web page, so I was prepared to be hand-editing a myriad of obscure configuration files - But after downloading the source and creating a virtual directory on my local machine for the application, it just ran. I actually started editing the settings.xml file before I found out that a UI was provided for it. So i customized some settings, and did a bit of changes to one of the default templates, before I was ready to go online. I actually think that setting it up for debugging in Visual Studio took longer than the actual customizations (My home machine runs Vista, and I had some problems with ASP .NET and IIS7, which I was able to resolve by using the "Classic ASP.NET pipelining" mode).

I downloaded the source, so that I could try to compile it for myself and poke a bit around, before ultimately deciding to base my blog on this application. I haven't spent too much time looking at the code, but it seems to be well designed and thought-through.

Anyways, uploading the site via FTP to my web host was no problem. I used Visual Studio to make a precompiled copy of the web site first.

Setting up the blog, customizing it and publishing it took less than 2 hours. I did not get stuck at all, and everything behaved as expected; overall it was a nice experience. So thumbs up to the BlogEngine.NET developers !

Got a blog

So, I decided today that I needed a blog. So here it is, me blogging about programming in general, .NET and my everyday experiences. 

Why does the world need another blog; you might ask ? Well, it is an experiment for now. I will try to provide some interesting content - and at the same time it will be a place for me to document various findings and thoughts. Anyways, it is much better than the old "Web 0.1" static content website that I used to have on this domain ;-)

I encourage everyone to use the comments for questions and/or suggestions. 

Don't know who I am ? 

Well, you don't really need to, in order to read my posts. My name is Dennis Riis and I am a danish software developer / architect, currently working with web technologies. If you want to read more about me, you can always head over to the About me page.