protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            //release any resources that might be hanging out
            generatorDiff   = null;
            generatorAux    = null;
            generatorSmooth = null;

            //dispose of all of the Bitmaps used for processing
            diffuseNormMap?.Dispose();
            diffuseNormMap = null;
            diffuseDiffMap?.Dispose();
            diffuseDiffMap = null;
            diffuseColDiffMap?.Dispose();
            diffuseColDiffMap = null;

            auxNormMap?.Dispose();
            auxNormMap = null;
            auxDiffMap?.Dispose();
            auxDiffMap = null;
            auxColDiffMap?.Dispose();
            auxColDiffMap = null;

            smoothNormMap?.Dispose();
            smoothNormMap = null;
            smoothDiffMap?.Dispose();
            smoothDiffMap = null;
            smoothColDiffMap?.Dispose();
            smoothColDiffMap = null;

            //and all of the bitmaps used for loading
            diffuseMap?.Dispose(); //diffuse input
            auxMap?.Dispose();     //metal/specular input
            smoothMap?.Dispose();  //metal/specular input
            maskMap?.Dispose();    //mask

            diffuseMap = null;     //diffuse input
            auxMap     = null;     //metal/specular input
            smoothMap  = null;     //metal/specular input
            maskMap    = null;     //mask

            //WPF image controls have no dispose method...

            //trigger GC just to clean up as much as possible
            System.GC.Collect();
        }
        private void generatationSequence(object sender, DoWorkEventArgs doWork)
        {
            try
            {
                int valid = 0;
                valid += diffuseMap == null ? 0 : 1;
                valid += auxMap == null ? 0 : 1;
                valid += smoothMap == null ? 0 : 1;
                if (valid == 0)
                {
                    valid = 1;
                }

                double offset = 100 / valid;
                double div    = 1 * valid;

                valid = 0;
                if (diffuseMap != null)
                {
                    generatorDiff = new NormGenerator(diffuseMap, maskMap, a, new NormParams(), valid * offset, div);
                    generatorDiff.generate(sender, doWork);
                    valid++;
                }

                if (auxMap != null)
                {
                    generatorAux = new NormGenerator(auxMap, maskMap, b, new NormParams(), valid * offset, div);
                    generatorAux.generate(sender, doWork);
                    valid++;
                }

                if (smoothMap != null)
                {
                    generatorSmooth = new NormGenerator(smoothMap, maskMap, c, new NormParams(), valid * offset, div);
                    generatorSmooth.generate(sender, doWork);
                    valid++;
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                Debug.WriteLine(e.StackTrace);
            }
        }
        private void generateFinished()
        {
            diffText   = string.Empty;
            auxText    = string.Empty;
            smoothText = string.Empty;

            if (generatorDiff != null)
            {
                diffuseNormMap      = (DirectBitmap)generatorDiff.dest;
                diffuseDiffMap      = (DirectBitmap)generatorDiff.difference;
                diffuseColDiffMap   = (DirectBitmap)generatorDiff.coloredDiff;
                diffuseNormImage    = ImageTools.BitmapToBitmapImage(diffuseNormMap.Bitmap);
                diffuseDiffImage    = ImageTools.BitmapToBitmapImage(diffuseDiffMap.Bitmap);
                diffuseColDiffImage = ImageTools.BitmapToBitmapImage(diffuseColDiffMap.Bitmap);
                diffText            = generatorDiff.outText;
            }

            if (generatorAux != null)
            {
                auxNormMap      = (DirectBitmap)generatorAux.dest;
                auxDiffMap      = (DirectBitmap)generatorAux.difference;
                auxColDiffMap   = (DirectBitmap)generatorAux.coloredDiff;
                auxNormImage    = ImageTools.BitmapToBitmapImage(auxNormMap.Bitmap);
                auxDiffImage    = ImageTools.BitmapToBitmapImage(auxDiffMap.Bitmap);
                auxColDiffImage = ImageTools.BitmapToBitmapImage(auxColDiffMap.Bitmap);
                auxText         = generatorAux.outText;
                if (string.IsNullOrEmpty(auxText))
                {
                    auxText = "0,0,0";
                }
            }
            else
            {
                auxText = "0,0,0";
            }

            if (generatorSmooth != null)
            {
                smoothNormMap      = (DirectBitmap)generatorSmooth.dest;
                smoothDiffMap      = (DirectBitmap)generatorSmooth.difference;
                smoothColDiffMap   = (DirectBitmap)generatorSmooth.coloredDiff;
                smoothNormImage    = ImageTools.BitmapToBitmapImage(smoothNormMap.Bitmap);
                smoothDiffImage    = ImageTools.BitmapToBitmapImage(smoothDiffMap.Bitmap);
                smoothColDiffImage = ImageTools.BitmapToBitmapImage(smoothColDiffMap.Bitmap);
                smoothText         = generatorSmooth.outText;
                if (string.IsNullOrEmpty(smoothText))
                {
                    smoothText = "0,0,0";
                }
            }
            else
            {
                smoothText = "0,0,0";
            }

            generatorDiff   = null;
            generatorAux    = null;
            generatorSmooth = null;

            //dispose of all of the Bitmaps used for processing
            diffuseNormMap?.Dispose();
            diffuseNormMap = null;
            diffuseDiffMap?.Dispose();
            diffuseDiffMap = null;
            diffuseColDiffMap?.Dispose();
            diffuseColDiffMap = null;

            auxNormMap?.Dispose();
            auxNormMap = null;
            auxDiffMap?.Dispose();
            auxDiffMap = null;
            auxColDiffMap?.Dispose();
            auxColDiffMap = null;

            smoothNormMap?.Dispose();
            smoothNormMap = null;
            smoothDiffMap?.Dispose();
            smoothDiffMap = null;
            smoothColDiffMap?.Dispose();
            smoothColDiffMap = null;

            updatePreview();
        }