PhotoSauce Blog

0 Comments

I wrote my first auto-cropping image scaler/resizer for ASP.NET for a client about five years ago. It was about 35 lines of System.Drawing code, plus some math for the cropping, basic disk caching... nothing fancy, really. You might have written something similar or used one of the many implementations like it. It was reasonably fast, and the disk caching made up for cases where it wasn't. It produced images of reasonable quality. It was no Photoshop (as the more artistically-inclined folk in the company would remind me from time to time), but it was all automatic. The images weren't as sharp, and sometimes the colors were washed out or contrast wasn't quite right. But it was the best I could do with .NET, so we all lived with it, even as the client put more and more focus on the quality of their product photography and added a more visual focus to their website.

Earlier this year, though, on hearing the latest complaints about the image quality on the site, I decided it was time to put some real energy into the problem and see what I could do. I have a decent background in video-processing software, so I knew there were ways to get better quality than GDI+ was producing, and faster. I researched the available solutions for .NET and it seemed that, unfortunately, System.Drawing/GDI+ was still the standard for quality. There were some solutions that wrapped it up in a nicer API, but they were still GDI+ at the core, and I wanted something better.

It was at that point I ran across a couple of interesting blog posts by Bertrand Le Roy from Microsoft. He wrote first about image resizing in ASP.NET using WPF , and then again using the Windows Imaging Component (WIC) directly. They both looked promising. They offered vastly better performance and at least similar image quality (or so it seemed). I grabbed his samples and put together some tests. The results were... disappointing. WPF and WIC were both fast, but the image quality left a lot to be desired. GDI+ blew them away quality-wise, sad as that may be.

That was when the idea for MagicScaler hit me. I figured I could take advantage of the speed and scalability of WIC, while handling the image resampling myself to improve the quality to at least GDI+ levels. Then with the extra processing power left over, I could do some tasteful sharpening and hopefully get the Photoshop-like results my clients were looking for, if not better.

Two (mostly sleepless) weeks later, I deployed the first version of MagicScaler to their site, and there was much rejoicing. I've made many improvements since then, and I now feel I have the best image scaling solution available for ASP.NET.

What's so magical about it? Well, it has a unique combination of performance, image quality, and ease-of-use that you won't find anywhere else. Here are some of the highlights:

Format Support

MagicScaler leverages WIC for decoding and encoding, meaning you get support for any codec Windows supports (JPEG, TIFF, GIF, PNG, BMP, ICO, and HD Photo/JPEG-XR natively) plus any installable WIC codec, like those in the Microsoft Camera RAW Codec Pack, and many other third-party options (WebP, Jpeg2000, PSD, and other codecs are available). Interestingly, on Windows 7 and above, GDI+ also uses WIC for decoding/encoding, but it doesn't support third-party codecs or even all the ones included with Windows.

Performance

WIC is designed to be lightweight and fast. When you open a folder of images on your PC and Windows Explorer shows you thumbnails as fast as you can scroll, that's WIC at work. Doing the same task with System.Drawing/GDI+ would bring your system to its metaphorical knees. GDI+ is designed for dealing with a single image at a time, and the performance shows. WPF does better, as its imaging functionality is just a loose wrapper around WIC, but depending how you use it, the scalability can be very poor. MagicScaler is optimized for scalability and incorporates some advanced performance techniques that you won’t find elsewhere, such as native DCT-domain scaling/rotation and planar processing of JPEG images.

Memory Usage

WIC has the ability to process images a single scanline at a time, resulting in dramatically reduced memory requirements vs GDI+. GDI+ decodes the entire source image to RGBA format for processing when you use its high quality scaling, which can have huge memory demands. A single 24MP image (standard for the newest DSLR cameras) will require a minimum of 96MB of memory just for the decoded bitmap, plus any memory required for intermediate processing and for the final image construction. Processing multiple images in parallel (which fortunately GDI+ won't do) could easily overwhelm your worker process memory space and cause unexpected AppPool recycling. WIC is able to reduce this memory demand to just a few hundred KB per image by processing a single output line at a time. MagicScaler is fully integrated with the WIC pipeline, so it has the same benefits.

Image Quality

MagicScaler uses its own implementation of best-of-breed resampling algorithms to give you the best quality scaling available. WIC's scaler is fast, but the quality is quite poor, even with the best settings. GDI+ has a decent scaler if you get the settings right, but it's slow and still fails at some things, such as incorrectly processing gamma-adjusted sRGB values. MagicScaler supports resampling in linear light space, which results in more accuracy in highlights/shadows and better contrast.

Color Management

WIC is fully color managed, so images with embedded or linked ICC profiles can be correctly converted. MagicScaler, of course, takes advantage of this. GDI+ has basic color management support, but it fails to apply color management when converting from CMYK to RGB formats. GDI+ also suffers a performance penalty when doing a null conversion from sRGB to sRGB because of embedded profiles (common with JPEGs from digital cameras). MagicScaler will recognize those scenarios and skip the conversion, resulting in yet further improved performance.

Ease-of-Use

MagicScaler uses adaptive logic to give you the best combination of speed and quality for a given task. For example, its hybrid scaling modes take advantage of WIC's speed to scale to an intermediate size and then finishes the job with high-quality scaling to the final size. The logic is designed to give the best balance for the majority of images you'll process, but it's also customizable if you want to take more control over the algorithms and settings used. If you don't know your Bicubic from your Bilinear or your Mitchell from your Lanczos, MagicScaler has you covered with its defaults. If you know exactly what you want, you can tell MagicScaler to do your bidding.

Auto-Sharpening

Usually, when an image is scaled to a different size, the result is more blurry than the original. WIC, and by extension WPF, do nothing about this. GDI+ has a sharpening interpolator, but most people will consider the results blurry, especially when compared with something like Photoshop. Photoshop uses a more aggressive sharpening interpolator during scaling. This gives pleasing results in most cases but can cause moiré effects with detailed patterns or can accentuate textures in an unappealing way. For the best quality, nothing compares to an Unsharp Mask operation on the final image. MagicScaler implements an automatic Unsharp Mask, also using adaptive settings, and also customizable.

A picture is worth a thousand words

So let’s get to it… I’ll give you a teaser for now. This is a screenshot from my test harness showing the relative performance and quality of MagicScaler vs WIC, WPF, and GDI+. The original image is a 24MP JPEG, available here. Processing times shown are an average of 10 runs (open 6000x4000 JPEG source, resize to 400x267, save as JPEG), followed by the standard deviation over those runs. The MagicScaler sample and timing include its post-resize sharpening. I think the results speak for themselves.

magicscalerscreenie

I'll leave it at that for this post. MagicScaler will be releasing to Nuget soon, so stay tuned. Over the next few days/weeks, I'll be discussing some of the magic behind MagicScaler as well as some performance and quality comparisons with other solutions.

Post comment