RSS

Monthly Archives: August 2011

Fixing Corrupt Image Metadata in DeepZoom

During the past few months PhotoPivot has created over 1 million DeepZoom images. Given these numbers, we were bound to encounter some problems. One of the most common is source images with corrupt colour palette information. The DeepZoomTools just can’t cope with this and you’ll get an exception like this…

System.ArgumentException: The image has corrupted metadata header.
	System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x88982F63

at System.Windows.Media.ColorContextsHelper(GetColorContextsDelegate getColorContexts)
at System.Windows.Media.Imaging.BitmapFrameDecode.get_ColorContexts()
at System.Windows.Media.Imaging.BitmapSource.CreateCachedBitmap(BitmapFrame frame, BitmapSourceSafeMILHandle wicSource, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, BitmapPalette palette)
at System.Windows.Media.Imaging.BitmapFrameDecode.FinalizeCreation()
at System.Windows.Media.Imaging.BitmapDecoder.SetupFrames(BitmapDecoder decoder, ReadOnlyCollection'1 frames)
at System.Windows.Media.Imaging.BitmapDecoder.get_Frames()
at Microsoft.DeepZoomTools.BitmapTransformer.CreateInputFrame(InputNode inputNode, Int32Rec rectNeeded, Double scaleFactor)
at Microsoft.DeepZoomTools.BitmapTransformer.GetPixelSize(String fileName)
at Microsoft.DeepZoomTools.SparseImageCreator.Create(ICollection'1 images, String destination)
at Microsoft.DeepZoomTools.ImageCreator.Create(String source, String destination)

The approach we took to fixing this was to catch the exception and then remove the palette information from the image before re-submitting. Here’s the first class you’ll need to implement. Setting the second parameter to false when loading the image tells the runtime to ignore any embedded colour management. This is the key.


using System;
using System.Drawing;

namespace GoodCoffeeGoodCode
{
  internal static class ImageColorManagementCorrector
  {
    internal static void Correct(string inputFile, string outputFile)
    {
      using (var bitmap = Bitmap.FromFile(intputFile, false))
      {
        bitmap.Save(outputFile);
      }
    }
  }
}

Next, we extended the standard ImageCreator class found in the DeepZoomTools namespace. This encapsulates the catch, retry routines.


using System;
using System.IO;
using Microsoft.DeepZoomTools;

namespace GoodCoffeeGoodCode
{
  internal class PaletteSafeImageCreator : ImageCreator
  {
    public new void Create(string source, string destination)
    {
      try
      {
        base.Create(source, destination);
      }
      catch (SystemException)
      {
        var cleanSource = GetTempFileName(source);

        ImageColorManagementCorrector.Correct(source, cleanSource);

        base.Create(cleanSource, destination);
      }
    }

    private static string GetTempFileName(string source)
    {
      var fileName = Path.GetFileNameWithoutExtension(source) + "_temp";

      return Path.Combine(Path.GetDirectoryName(source), fileName + Path.GetExtension(source));
    }
  }
}

And that’s it. Just ‘new-up’ a PaletteSafeImageCreator in place of an ImageCreator and you now have the ability to automatically cope with corrupt colour palette metadata in images.

 
3 Comments

Posted by on August 31, 2011 in PivotViewer

 

Tags: , ,