RSS

Fixing Corrupt Image Metadata in DeepZoom

31 Aug

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.

Advertisements
 
3 Comments

Posted by on August 31, 2011 in PivotViewer

 

Tags: , ,

3 responses to “Fixing Corrupt Image Metadata in DeepZoom

  1. John Collins

    August 31, 2011 at 4:40 pm

    Thanks!
    I’m trying to show a deepzoom collection I’ve created using the PAuthorLib. I’m getting a Code 1001 DownloadError AG_E_UNKNOWN_ERROR when the collection is loaded and I zoom in and out on the images. Have you guys seen this?

    It’s all on the pivot control client side so I can’t locate the issue.

    One thing I did notice was that the PAuthor created the deepzoom collection using the ns=”http://schemas.microsoft.com/deepzoom/2008″ where the the PivotJITServer is expecting ns=”http://schemas.microsoft.com/deepzoom/2009″

     
    • goodcoffeegoodcode

      August 31, 2011 at 4:47 pm

      Hi John,

      Have you tried using Fiddler? It’s invaluable for pivot viewer problems. Free and downloadable from http://www.fiddler2.com/fiddler2/.

      Happy to look at this for you if you are able to ping me a public url.

      Cheers,

      Chris

       
      • John Collins

        August 31, 2011 at 5:26 pm

        Thanks Chris,
        I don’t have it on a public url, it’s all on localhost for now.
        I’ll try fiddler2 and see what I come up with.

        Thanks,
        John

         

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: