/// <summary>
        /// This function takes a raster object and returns the formatted name of
        /// the object for display in the UI.
        /// </summary>
        /// <param name="inputRaster">Object whose name is to be found</param>
        /// <returns>Name of the object</returns>
        private string GetInputRasterName(object inputRaster)
        {
            if ((inputRaster is IRasterDataset))
            {
                IRasterDataset rasterDataset = (IRasterDataset)inputRaster;
                return(rasterDataset.CompleteName);
            }

            if ((inputRaster is IRaster))
            {
                IRaster myRaster = (IRaster)inputRaster;
                return(((IRaster2)myRaster).RasterDataset.CompleteName);
            }

            if (inputRaster is IDataset)
            {
                IDataset dataset = (IDataset)inputRaster;
                return(dataset.Name);
            }

            if (inputRaster is IName)
            {
                if (inputRaster is IDatasetName)
                {
                    IDatasetName inputDSName = (IDatasetName)inputRaster;
                    return(inputDSName.Name);
                }

                if (inputRaster is IFunctionRasterDatasetName)
                {
                    IFunctionRasterDatasetName inputFRDName = (IFunctionRasterDatasetName)inputRaster;
                    return(inputFRDName.BrowseName);
                }

                if (inputRaster is IMosaicDatasetName)
                {
                    IMosaicDatasetName inputMDName = (IMosaicDatasetName)inputRaster;
                    return("MD");
                }

                IName inputName = (IName)inputRaster;
                return(inputName.NameString);
            }

            if (inputRaster is IRasterFunctionTemplate)
            {
                IRasterFunctionTemplate rasterFunctionTemplate =
                    (IRasterFunctionTemplate)inputRaster;
                return(rasterFunctionTemplate.Function.Name);
            }

            if (inputRaster is IRasterFunctionVariable)
            {
                IRasterFunctionVariable rasterFunctionVariable =
                    (IRasterFunctionVariable)inputRaster;
                return(rasterFunctionVariable.Name);
            }

            return("");
        }
 /// <summary>
 /// Resolve the 'Raster' variable if it contains field names with the corresponding values.
 /// </summary>
 /// <param name="pRow">The row corresponding to the function raster dataset.</param>
 private void ResolveRasterVal(IRow pRow)
 {
     try
     {
         // Get the Raster property.
         object myRasterObject = myProperties.GetProperty("Raster");
         // Check to see if it is a variable
         if (myRasterObject is IRasterFunctionVariable)
         {
             IRasterFunctionVariable rasterVar = ((IRasterFunctionVariable)myRasterObject);
             object rasterVal = FindPropertyInRow(rasterVar, pRow);
             if (rasterVal != null && rasterVal is string)
             {
                 // Open the Raster Dataset from the path provided.
                 string         datasetPath   = (string)rasterVal;
                 IRasterDataset rasterDataset = null;
                 rasterVar.Value = rasterDataset;
             }
         }
     }
     catch (Exception exc)
     {
         System.Exception myExc = new System.Exception(
             "Exception caught in ResolveRasterVal: " + exc.Message, exc);
         throw myExc;
     }
 }
        /// <summary>
        /// Dereference and return the value of the property whose name is given if necessary.
        /// </summary>
        /// <param name="name">Name of the property to check.</param>
        /// <returns>Dereferenced value of the property corresponding to the name given.</returns>
        public object GetDereferencedValue(string name)
        {
            object value = myProperties.GetProperty(name);

            if (value != null && value is IRasterFunctionVariable && !(value is IRasterFunctionTemplate))
            {
                IRasterFunctionVariable rFVar = (IRasterFunctionVariable)value;
                return(rFVar.Value);
            }
            return(value);
        }
        /// <summary>
        /// Apply the properties set in the form to the arguments object.
        /// </summary>
        public void Apply()
        {
            if (!isFormReadOnly) // If the form is read only, do not update.
            {
                if (templateMode)
                {
                    // If in template mode, use the values from the page to
                    // update the variables.
                    if (myForm.InputRaster != null)
                    {
                        if (myForm.InputRaster is IRasterFunctionVariable)
                        {
                            myRasterVar = (IRasterFunctionVariable)myForm.InputRaster;
                        }
                        else
                        {
                            myRasterVar.Value = myForm.InputRaster;
                        }
                    }
                    myBlendPercentageVar.Value    = myForm.BlendPercentage;
                    myWatermarkImagePathVar.Value = myForm.WatermarkImagePath;
                    myXGapVar.Value = myForm.XGap;
                    myYGapVar.Value = myForm.YGap;
                    // Then set the variables on the arguments object.
                    IRasterFunctionArguments rasterFunctionArgs =
                        (IRasterFunctionArguments)myArgs;
                    rasterFunctionArgs.PutValue("Raster", myRasterVar);
                    rasterFunctionArgs.PutValue("BlendPercentage", myBlendPercentageVar);
                    rasterFunctionArgs.PutValue("XGap", myXGapVar);
                    rasterFunctionArgs.PutValue("YGap", myYGapVar);
                    rasterFunctionArgs.PutValue("WatermarkImagePath", myWatermarkImagePathVar);
                }
                else if (myArgs != null)
                {
                    // If not in template mode, update the arguments object
                    // with the values from the form.
                    myArgs.BlendPercentage    = myForm.BlendPercentage;
                    myArgs.XGap               = myForm.XGap;
                    myArgs.YGap               = myForm.YGap;
                    myArgs.WatermarkImagePath = myForm.WatermarkImagePath;
                    if (myForm.InputRaster != null)
                    {
                        myArgs.Raster = myForm.InputRaster;
                    }
                }
            }

            myForm.IsFormDirty = false;
        }
        IRasterFunctionVariable myBandIndicesVar; // Variable for the Band Indices property.
        #endregion

        public NDVICustomFunctionUIClass()
        {
            myForm = new NDVICustomFunctionUIForm();
            myArgs = null;
            myPriority = 100;
            myPageSite = null;
            myHelpFile = "";
            mySupportedID = new UIDClass();
            // The UID of the NDVICustomFunction object.
            mySupportedID.Value = "{" + "652642F3-9106-4EB3-9262-A4C39E03BC56" + "}";
            templateMode = false;

            myRasterVar = null;
            myBandIndicesVar = null;
        }
        IRasterFunctionVariable myBandIndicesVar; // Variable for the Band Indices property.
        #endregion

        public NDVICustomFunctionUIClass()
        {
            myForm        = new NDVICustomFunctionUIForm();
            myArgs        = null;
            myPriority    = 100;
            myPageSite    = null;
            myHelpFile    = "";
            mySupportedID = new UIDClass();
            // The UID of the NDVICustomFunction object.
            mySupportedID.Value = "{" + "652642F3-9106-4EB3-9262-A4C39E03BC56" + "}";
            templateMode        = false;

            myRasterVar      = null;
            myBandIndicesVar = null;
        }
        /// <summary>
        /// Resolve the 'WatermarkImagePath' variable if it contains field names with the corresponding values.
        /// </summary>
        /// <param name="pRow">The row corresponding to the function raster dataset.</param>
        private void ResolveWatermarkPathVal(IRow pRow)
        {
            // Get the WatermarkImagePath property.
            object myRasterObject = myProperties.GetProperty("WatermarkImagePath");

            // Check to see if it is a variable
            if (myRasterObject is IRasterFunctionVariable)
            {
                IRasterFunctionVariable wipVar = ((IRasterFunctionVariable)myRasterObject);
                object rasterVal = FindPropertyInRow(wipVar, pRow);
                if (rasterVal != null && rasterVal is string)
                {
                    // Get the blend percentage value from string
                    wipVar.Value = (string)rasterVal;
                }
            }
        }
Exemple #8
0
        IRasterFunctionVariable myWatermarkImagePathVar; // Variable for WatermarkImagePath property.
        #endregion

        public WatermarkFunctionUIClass()
        {
            myForm              = new WatermarkFunctionUIForm();
            myArgs              = null;
            myPriority          = 100;
            myPageSite          = null;
            myHelpFile          = "";
            mySupportedID       = new UIDClass();
            mySupportedID.Value = "{" + "168721E7-7010-4a36-B886-F644437B164D" + "}";
            templateMode        = false;
            isFormReadOnly      = false;

            myRasterVar             = null;
            myBlendPercentageVar    = null;
            myWatermarkLocationVar  = null;
            myWatermarkImagePathVar = null;
        }
        IRasterFunctionVariable myWatermarkImagePathVar; // Variable for WatermarkImagePath property.
        #endregion

        public WatermarkFunctionUIClass()
        {
            myForm = new WatermarkFunctionUIForm();
            myArgs = null;
            myPriority = 100;
            myPageSite = null;
            myHelpFile = "";
            mySupportedID = new UIDClass();
            mySupportedID.Value = "{" + "168721E7-7010-4a36-B886-F644437B164D" + "}";
            templateMode = false;
            isFormReadOnly = false;

            myRasterVar = null;
            myBlendPercentageVar = null;
            myWatermarkLocationVar = null;
            myWatermarkImagePathVar = null;
        }
        public WatermarkFunctionUIClass()
        {
            myForm              = new WatermarkFunctionUIForm();
            myArgs              = null;
            myPriority          = 100;
            myPageSite          = null;
            myHelpFile          = "";
            mySupportedID       = new UIDClass();
            mySupportedID.Value = "{" + "25BE29A6-AAF9-496E-AE73-130D5947682D" + "}";
            templateMode        = false;
            isFormReadOnly      = false;

            myRasterVar             = null;
            myBlendPercentageVar    = null;
            myWatermarkImagePathVar = null;
            myXGapVar = null;
            myYGapVar = null;
        }
        bool templateMode; // Flag to specify template mode.

        #endregion Fields

        #region Constructors

        public WatermarkFunctionUIClass()
        {
            myForm = new WatermarkFunctionUIForm();
            myArgs = null;
            myPriority = 100;
            myPageSite = null;
            myHelpFile = "";
            mySupportedID = new UIDClass();
            mySupportedID.Value = "{" + "25BE29A6-AAF9-496E-AE73-130D5947682D" + "}";
            templateMode = false;
            isFormReadOnly = false;

            myRasterVar = null;
            myBlendPercentageVar = null;
            myWatermarkImagePathVar = null;
            myXGapVar = null;
            myYGapVar = null;
        }
        /// <summary>
        /// Resolve the 'BlendPercentage' variable if it contains field names with the corresponding values.
        /// </summary>
        /// <param name="pRow">The row corresponding to the function raster dataset.</param>
        private void ResolveBlendPVal(IRow pRow)
        {
            // Get the BlendPercentage property.
            object myRasterObject = myProperties.GetProperty("BlendPercentage");

            // Check to see if it is a variable
            if (myRasterObject is IRasterFunctionVariable)
            {
                IRasterFunctionVariable bpVar = ((IRasterFunctionVariable)myRasterObject);
                object rasterVal = FindPropertyInRow(bpVar, pRow);
                if (rasterVal != null && rasterVal is string)
                {
                    // Get the blend percentage value from string
                    try { bpVar.Value = Convert.ToDouble((string)rasterVal); }
                    catch (Exception) { }
                }
            }
        }
 /// <summary>
 /// Activate the form. 
 /// </summary>
 /// <returns>Handle to the form</returns>
 public int Activate()
 {
     if (templateMode)
     {
         // In template mode, set the form values using the RasterFunctionVariables
         myBandIndicesVar = null;
         myForm.InputRaster = myRasterVar;
         myForm.BandIndices = (string)myBandIndicesVar.Value;
     }
     else
     {
         // Otherwise use the arguments object to update the form values.
         myForm.InputRaster = myArgs.Raster;
         myForm.BandIndices = myArgs.BandIndices;
     }
     myForm.UpdateUI();
     myForm.Activate();
     return myForm.Handle.ToInt32();
 }
 /// <summary>
 /// Activate the form.
 /// </summary>
 /// <returns>Handle to the form</returns>
 public int Activate()
 {
     if (templateMode)
     {
         // In template mode, set the form values using the RasterFunctionVariables
         myBandIndicesVar   = null;
         myForm.InputRaster = myRasterVar;
         myForm.BandIndices = (string)myBandIndicesVar.Value;
     }
     else
     {
         // Otherwise use the arguments object to update the form values.
         myForm.InputRaster = myArgs.Raster;
         myForm.BandIndices = myArgs.BandIndices;
     }
     myForm.UpdateUI();
     myForm.Activate();
     return(myForm.Handle.ToInt32());
 }
        /// <summary>
        /// Check the Name and Alias properties of the given Raster Function Variable to see
        /// if they contain a reference to a field and get the value of the corresponding field if needed.
        /// </summary>
        /// <param name="rasterFunctionVar">The Raster Function Variable to check.</param>
        /// <param name="pRow">The row corresponding to the function raster dataset.</param>
        /// <returns></returns>
        private object FindPropertyInRow(IRasterFunctionVariable rasterFunctionVar, IRow pRow)
        {
            string       varName  = "";
            IStringArray varNames = new StrArrayClass();

            varName = rasterFunctionVar.Name;
            // If the name of  the variable contains '@Field'
            if (varName.Contains("@Field."))
            {
                varNames.Add(varName); // Add it to the list of names.
            }
            // Check the aliases of the variable
            for (int i = 0; i < rasterFunctionVar.Aliases.Count; ++i)
            {
                // Check the list of aliases for the '@Field' string
                varName = rasterFunctionVar.Aliases.get_Element(i);
                if (varName.Contains("@Field."))
                {
                    varNames.Add(varName); // and add any that are found to the list of names.
                }
            }

            // Use the list of names and find the value by looking up the appropriate field.
            for (int i = 0; i < varNames.Count; ++i)
            {
                // Get the variable name containing the field string
                varName = varNames.get_Element(i);
                // Replace the '@Field' with nothing to get just the name of the field.
                string  fieldName = varName.Replace("@Field.", "");
                IFields rowFields = pRow.Fields;
                // Look up the index of the field name in the row.
                int fieldIndex = rowFields.FindField(fieldName);
                // If it is a valid index and the field type is string, return the value.
                if (fieldIndex != -1 &&
                    ((rowFields.get_Field(fieldIndex)).Type == esriFieldType.esriFieldTypeString))
                {
                    return(pRow.get_Value(fieldIndex));
                }
            }
            // If no value has been returned yet, return null.
            return(null);
        }
        /// <summary>
        /// Apply the properties set in the form to the arguments object.
        /// </summary>
        public void Apply()
        {
            if (!isFormReadOnly) // If the form is read only, do not update.
            {
                if (templateMode)
                {
                    // If in template mode, use the values from the page to
                    // update the variables.
                    if (myForm.InputRaster != null)
                    {
                        if (myForm.InputRaster is IRasterFunctionVariable)
                        {
                            myRasterVar = (IRasterFunctionVariable)myForm.InputRaster;
                        }
                        else
                        {
                            myRasterVar.Value = myForm.InputRaster;
                        }
                    }
                    myBandIndicesVar.Value = myForm.BandIndices;
                    // Then set the variables on the arguments object.
                    IRasterFunctionArguments rasterFunctionArgs =
                        (IRasterFunctionArguments)myArgs;
                    rasterFunctionArgs.PutValue("BandIndices", myBandIndicesVar);
                    rasterFunctionArgs.PutValue("Raster", myRasterVar);
                }
                else if (myArgs != null)
                {
                    // If not in template mode, update the arguments object
                    // with the values from the form.
                    myArgs.BandIndices = myForm.BandIndices;
                    if (myForm.InputRaster != null)
                    {
                        myArgs.Raster = myForm.InputRaster;
                    }
                }
            }

            myForm.IsFormDirty = false;
        }
        /// <summary>
        /// Check the Name and Alias properties of the given Raster Function Variable to see
        /// if they contain a field name and get the value of the corresponding field if needed.
        /// </summary>
        /// <param name="rasterFunctionVar">The Raster Function Variable to check.</param>
        /// <param name="pRow">The row corresponding to the function raster dataset.</param>
        /// <returns></returns>
        private object FindPropertyInRow(IRasterFunctionVariable rasterFunctionVar, IRow pRow)
        {
            string varName = "";
            IStringArray varNames = new StrArray();
            varName = rasterFunctionVar.Name;
            // If the name of  the variable contains '@Field'
            if (varName.Contains("@Field."))
                varNames.Add(varName); // Add it to the list of names.
            // Check the aliases of the variable
            for (int i = 0; i < rasterFunctionVar.Aliases.Count; ++i)
            {
                // Check the list of aliases for the '@Field' string
                varName = rasterFunctionVar.Aliases.get_Element(i);
                if (varName.Contains("@Field."))
                    varNames.Add(varName); // and add any that are found to the list of names.
            }

            // Use the list of names and find the value by looking up the appropriate field.
            for (int i = 0; i < varNames.Count; ++i)
            {
                // Get the variable name containing the field string
                varName = varNames.get_Element(i);
                // Replace the '@Field' with nothing to get just the name of the field.
                string fieldName = varName.Replace("@Field.", "");
                IFields rowFields = pRow.Fields;
                // Look up the index of the field name in the row.
                int fieldIndex = rowFields.FindField(fieldName);
                // If it is a valid index and the field type is string, return the value.
                if (fieldIndex != -1 &&
                   ((rowFields.get_Field(fieldIndex)).Type == esriFieldType.esriFieldTypeString))
                    return pRow.get_Value(fieldIndex);
            }
            // If no value has been returned yet, return null.
            return null;
        }
        /// <summary>
        /// Set the necessary objects required for the form. In this case
        /// the form is given an arguments object in edit mode, or is required
        /// to create one in create mode. After getting or creating the arguments
        /// object, template mode is checked for and handled. The template mode
        /// requires all parameters of the arguments object to converted to variables.
        /// </summary>
        /// <param name="objects">Set of objects required for the form.</param>
        public void SetObjects(ESRI.ArcGIS.esriSystem.ISet objects)
        {
            try
            {
                // Recurse through the objects
                objects.Reset();
                for (int i = 0; i < objects.Count; i++)
                {
                    object currObject = objects.Next();
                    // Find the properties to be set.
                    if (currObject is IPropertySet)
                    {
                        IPropertySet uiParameters = (IPropertySet)currObject;
                        object       names, values;
                        uiParameters.GetAllProperties(out names, out values);

                        bool disableForm = false;
                        try { disableForm = Convert.ToBoolean(uiParameters.GetProperty("RFxPropPageIsReadOnly")); }
                        catch (Exception) { }

                        if (disableForm)
                        {
                            isFormReadOnly = true;
                        }
                        else
                        {
                            isFormReadOnly = false;
                        }

                        // Check if the arguments object exists in the property set.
                        object functionArgument = null;
                        try { functionArgument = uiParameters.GetProperty("RFxArgument"); }
                        catch (Exception) { }
                        // If not, the form is in create mode.
                        if (functionArgument == null)
                        {
                            #region Create Mode
                            // Create a new arguments object.
                            myArgs = new NDVICustomFunctionArguments();
                            // Create a new property and set the arguments object on it.
                            uiParameters.SetProperty("RFxArgument", myArgs);
                            // Check if a default raster is supplied.
                            object defaultRaster = null;
                            try { defaultRaster = uiParameters.GetProperty("RFxDefaultInputRaster"); }
                            catch (Exception) { }
                            if (defaultRaster != null) // If it is, set it to the raster property.
                            {
                                myArgs.Raster = defaultRaster;
                            }
                            // Check if the form is in template mode.
                            templateMode = (bool)uiParameters.GetProperty("RFxTemplateEditMode");
                            if (templateMode)
                            {
                                // Since we are in create mode already, new variables have to be
                                // created for each property of the arguments object.
                                #region Create Variables
                                if (defaultRaster != null)
                                {
                                    // If a default raster is supplied and it is a variable,
                                    // there is no need to create one.
                                    if (defaultRaster is IRasterFunctionVariable)
                                    {
                                        myRasterVar = (IRasterFunctionVariable)defaultRaster;
                                    }
                                    else
                                    {
                                        // Create variable object for the InputRaster property.
                                        myRasterVar           = new RasterFunctionVariableClass();
                                        myRasterVar.Value     = defaultRaster;
                                        myRasterVar.Name      = "InputRaster";
                                        myRasterVar.IsDataset = true;
                                    }
                                }

                                // Create a variable for the BandIndices property.
                                myBandIndicesVar      = new RasterFunctionVariableClass();
                                myBandIndicesVar.Name = "BandIndices";
                                // Use the default value from the arguments object
                                myBandIndicesVar.Value = myArgs.BandIndices;

                                // Set the variables created as properties on the arguments object.
                                IRasterFunctionArguments rasterFunctionArgs =
                                    (IRasterFunctionArguments)myArgs;
                                rasterFunctionArgs.PutValue("Raster", myRasterVar);
                                rasterFunctionArgs.PutValue("BandIndices", myBandIndicesVar);
                                #endregion
                            }
                            #endregion
                        }
                        else
                        {
                            #region  Edit Mode
                            // Get the arguments object from the property set.
                            myArgs = (INDVICustomFunctionArguments)functionArgument;
                            // Check if the form is in template mode.
                            templateMode = (bool)uiParameters.GetProperty("RFxTemplateEditMode");
                            if (templateMode)
                            {
                                #region Edit Template
                                // In template edit mode, the variables from the arguments object
                                // are extracted.
                                IRasterFunctionArguments rasterFunctionArgs =
                                    (IRasterFunctionArguments)myArgs;
                                object raster = rasterFunctionArgs.GetValue("Raster");

                                // Create or Open the Raster variable.
                                if (raster is IRasterFunctionVariable)
                                {
                                    myRasterVar = (IRasterFunctionVariable)raster;
                                }
                                else
                                {
                                    myRasterVar       = new RasterFunctionVariableClass();
                                    myRasterVar.Name  = "InputRaster";
                                    myRasterVar.Value = raster;
                                }
                                #endregion
                            }
                            #endregion
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                string errorMsg = exc.Message;
            }
        }
        /// <summary>
        /// Initialize the Raster function using the argument object. This is one of the two
        /// main functions to implement for a custom Raster function. The raster object is
        /// dereferenced if required and given to the RasterFuntionHelper object to bind.
        /// </summary>
        /// <param name="pArguments">Arguments object used for initialization</param>
        public void Bind(object pArguments)
        {
            try
            {
                // Check if the Arguments object is of the correct type.
                INDVICustomFunctionArguments customFunctionArgs = null;
                if (pArguments is INDVICustomFunctionArguments)
                {
                    customFunctionArgs = (INDVICustomFunctionArguments)pArguments;
                    object inputRaster = customFunctionArgs.Raster;
                    if (customFunctionArgs.Raster is IRasterFunctionVariable)
                    {
                        IRasterFunctionVariable rasterFunctionVariable =
                            (IRasterFunctionVariable)customFunctionArgs.Raster;
                        inputRaster = rasterFunctionVariable.Value;
                    }

                    // Call the Bind method of the Raster Function Helper object.
                    myFunctionHelper.Bind(inputRaster);
                }
                else
                {
                    // Throw an error if incorrect arguments object is passed.
                    throw new System.Exception(
                              "Incorrect arguments object. Expected: INDVICustomFunctionArguments");
                }

                // Check to see if Band Indices exist.
                if (customFunctionArgs.BandIndices != null && customFunctionArgs.BandIndices != "")
                {
                    myBandIndices = customFunctionArgs.BandIndices.Split(' ');
                }
                else
                {
                    // If not, throw an error.
                    throw new System.Exception(
                              "Incorrect parameters specified. Expected: Valid band indices.");
                }

                // Create a new RasterInfo object and initialize from the FunctionHelper object.
                // A new RasterInfo Object is created because assigning myFunctionHelper.RasterInfo
                // directly creates a reference.
                myRasterInfo                        = new RasterInfo();
                myRasterInfo.BandCount              = myFunctionHelper.RasterInfo.BandCount;
                myRasterInfo.BlockHeight            = myFunctionHelper.RasterInfo.BlockHeight;
                myRasterInfo.BlockWidth             = myFunctionHelper.RasterInfo.BlockWidth;
                myRasterInfo.CellSize               = myFunctionHelper.RasterInfo.CellSize;
                myRasterInfo.Extent                 = myFunctionHelper.RasterInfo.Extent;
                myRasterInfo.FirstPyramidLevel      = myFunctionHelper.RasterInfo.FirstPyramidLevel;
                myRasterInfo.Format                 = myFunctionHelper.RasterInfo.Format;
                myRasterInfo.GeodataXform           = myFunctionHelper.RasterInfo.GeodataXform;
                myRasterInfo.MaximumPyramidLevel    = myFunctionHelper.RasterInfo.MaximumPyramidLevel;
                myRasterInfo.NativeExtent           = myFunctionHelper.RasterInfo.NativeExtent;
                myRasterInfo.NativeSpatialReference = myFunctionHelper.RasterInfo.NativeSpatialReference;
                myRasterInfo.NoData                 = myFunctionHelper.RasterInfo.NoData;
                myRasterInfo.Origin                 = myFunctionHelper.RasterInfo.Origin;
                myRasterInfo.PixelType              = rstPixelType.PT_FLOAT; // Output pixel type should be output of the NDVI.
                myRasterInfo.Resampling             = myFunctionHelper.RasterInfo.Resampling;
                myRasterInfo.SupportBandSelection   = myFunctionHelper.RasterInfo.SupportBandSelection;

                // Store required input properties.
                myInpPixeltype = myRasterInfo.PixelType;
                myInpNumBands  = myRasterInfo.BandCount;

                // Set output pixel properties.
                myRasterInfo.BandCount = 1;
                myPixeltype            = rstPixelType.PT_FLOAT;

                // Perform validation to see if the indices passed are valid.
                if (myInpNumBands < 2 || myBandIndices.Length < 2)
                {
                    // If not, throw an error.
                    throw new System.Exception(
                              "Incorrect parameters specified. Expected: Valid band indices.");
                }
                for (int i = 0; i < myBandIndices.Length; ++i)
                {
                    int currBand = Convert.ToInt16(myBandIndices[i]) - 1;
                    if ((currBand < 0) || (currBand > myInpNumBands))
                    {
                        // If not, throw an error.
                        throw new System.Exception(
                                  "Incorrect parameters specified. Expected: Valid band indices.");
                    }
                }
            }
            catch (Exception exc)
            {
                System.Exception myExc = new System.Exception(
                    "Exception caught in Bind method: " + exc.Message, exc);
                throw myExc;
            }
        }
        /// <summary>
        /// Apply the properties set in the form to the arguments object.
        /// </summary>
        public void Apply()
        {
            if (!isFormReadOnly) // If the form is read only, do not update.
            {
                if (templateMode)
                {
                    // If in template mode, use the values from the page to
                    // update the variables.
                    if (myForm.InputRaster != null)
                    {
                        if (myForm.InputRaster is IRasterFunctionVariable)
                            myRasterVar = (IRasterFunctionVariable)myForm.InputRaster;
                        else
                            myRasterVar.Value = myForm.InputRaster;
                    }
                    myBlendPercentageVar.Value = myForm.BlendPercentage;
                    myWatermarkImagePathVar.Value = myForm.WatermarkImagePath;
                    myXGapVar.Value = myForm.XGap;
                    myYGapVar.Value = myForm.YGap;
                    // Then set the variables on the arguments object.
                    IRasterFunctionArguments rasterFunctionArgs =
                        (IRasterFunctionArguments)myArgs;
                    rasterFunctionArgs.PutValue("Raster", myRasterVar);
                    rasterFunctionArgs.PutValue("BlendPercentage", myBlendPercentageVar);
                    rasterFunctionArgs.PutValue("XGap", myXGapVar);
                    rasterFunctionArgs.PutValue("YGap", myYGapVar);
                    rasterFunctionArgs.PutValue("WatermarkImagePath", myWatermarkImagePathVar);
                }
                else if (myArgs != null)
                {
                    // If not in template mode, update the arguments object
                    // with the values from the form.
                    myArgs.BlendPercentage = myForm.BlendPercentage;
                    myArgs.XGap = myForm.XGap;
                    myArgs.YGap = myForm.YGap;
                    myArgs.WatermarkImagePath = myForm.WatermarkImagePath;
                    if (myForm.InputRaster != null)
                        myArgs.Raster = myForm.InputRaster;
                }
            }

            myForm.IsFormDirty = false;
        }
        /// <summary>
        /// Set the necessary objects required for the form. In this case
        /// the form is given an arguments object in edit mode, or is required 
        /// to create one in create mode. After getting or creating the arguments
        /// object, template mode is checked for and handled. The template mode 
        /// requires all parameters of the arguments object to converted to variables.
        /// </summary>
        /// <param name="objects">Set of objects required for the form.</param>
        public void SetObjects(ESRI.ArcGIS.esriSystem.ISet objects)
        {
            try
            {
                // Recurse through the objects
                objects.Reset();
                for (int i = 0; i < objects.Count; i++)
                {
                    object currObject = objects.Next();
                    // Find the properties to be set.
                    if (currObject is IPropertySet)
                    {
                        IPropertySet uiParameters = (IPropertySet)currObject;
                        object names, values;
                        uiParameters.GetAllProperties(out names, out values);

                        bool disableForm = false;
                        try { disableForm = Convert.ToBoolean(uiParameters.GetProperty("RFxPropPageIsReadOnly")); }
                        catch (Exception) { }

                        if (disableForm)
                            isFormReadOnly = true;
                        else
                            isFormReadOnly = false;

                        // Check if the arguments object exists in the property set.
                        object functionArgument = null;
                        try { functionArgument = uiParameters.GetProperty("RFxArgument"); }
                        catch (Exception) { }
                        // If not, the form is in create mode.
                        if (functionArgument == null)
                        {
                            #region Create Mode
                            // Create a new arguments object.
                            myArgs = new NDVICustomFunctionArguments();
                            // Create a new property and set the arguments object on it.
                            uiParameters.SetProperty("RFxArgument", myArgs);
                            // Check if a default raster is supplied.
                            object defaultRaster = null;
                            try { defaultRaster = uiParameters.GetProperty("RFxDefaultInputRaster"); }
                            catch (Exception) { }
                            if (defaultRaster != null) // If it is, set it to the raster property.
                                myArgs.Raster = defaultRaster;
                            // Check if the form is in template mode.
                            templateMode = (bool)uiParameters.GetProperty("RFxTemplateEditMode");
                            if (templateMode)
                            {
                                // Since we are in create mode already, new variables have to be 
                                // created for each property of the arguments object.
                                #region Create Variables
                                if (defaultRaster != null)
                                {
                                    // If a default raster is supplied and it is a variable,
                                    // there is no need to create one.
                                    if (defaultRaster is IRasterFunctionVariable)
                                        myRasterVar = (IRasterFunctionVariable)defaultRaster;
                                    else
                                    {
                                        // Create variable object for the InputRaster property.
                                        myRasterVar = new RasterFunctionVariableClass();
                                        myRasterVar.Value = defaultRaster;
                                        myRasterVar.Name = "InputRaster";
                                        myRasterVar.IsDataset = true;
                                    }
                                }

                                // Create a variable for the BandIndices property.
                                myBandIndicesVar = new RasterFunctionVariableClass();
                                myBandIndicesVar.Name = "BandIndices";
                                // Use the default value from the arguments object
                                myBandIndicesVar.Value = myArgs.BandIndices;

                                // Set the variables created as properties on the arguments object.
                                IRasterFunctionArguments rasterFunctionArgs =
                                    (IRasterFunctionArguments)myArgs;
                                rasterFunctionArgs.PutValue("Raster", myRasterVar);
                                rasterFunctionArgs.PutValue("BandIndices", myBandIndicesVar);
                                #endregion
                            }
                            #endregion
                        }
                        else
                        {
                            #region  Edit Mode
                            // Get the arguments object from the property set.
                            myArgs = (INDVICustomFunctionArguments)functionArgument;
                            // Check if the form is in template mode.
                            templateMode = (bool)uiParameters.GetProperty("RFxTemplateEditMode");
                            if (templateMode)
                            {
                                #region Edit Template
                                // In template edit mode, the variables from the arguments object
                                // are extracted.
                                IRasterFunctionArguments rasterFunctionArgs =
                                    (IRasterFunctionArguments)myArgs;
                                object raster = rasterFunctionArgs.GetValue("Raster");

                                // Create or Open the Raster variable.
                                if (raster is IRasterFunctionVariable)
                                    myRasterVar = (IRasterFunctionVariable)raster;
                                else
                                {
                                    myRasterVar = new RasterFunctionVariableClass();
                                    myRasterVar.Name = "InputRaster";
                                    myRasterVar.Value = raster;
                                }
                                #endregion
                            }
                            #endregion
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                string errorMsg = exc.Message;
            }
        }
        /// <summary>
        /// Apply the properties set in the form to the arguments object.
        /// </summary>
        public void Apply()
        {
            if (!isFormReadOnly) // If the form is read only, do not update.
            {
                if (templateMode)
                {
                    // If in template mode, use the values from the page to
                    // update the variables.
                    if (myForm.InputRaster != null)
                    {
                        if (myForm.InputRaster is IRasterFunctionVariable)
                            myRasterVar = (IRasterFunctionVariable)myForm.InputRaster;
                        else
                            myRasterVar.Value = myForm.InputRaster;
                    }
                    myBandIndicesVar.Value = myForm.BandIndices;
                    // Then set the variables on the arguments object.
                    IRasterFunctionArguments rasterFunctionArgs =
                        (IRasterFunctionArguments)myArgs;
                    rasterFunctionArgs.PutValue("BandIndices", myBandIndicesVar);
                    rasterFunctionArgs.PutValue("Raster", myRasterVar);
                }
                else if (myArgs != null)
                {
                    // If not in template mode, update the arguments object
                    // with the values from the form.
                    myArgs.BandIndices = myForm.BandIndices;
                    if (myForm.InputRaster != null)
                        myArgs.Raster = myForm.InputRaster;
                }
            }

            myForm.IsFormDirty = false;
        }
        /// <summary>
        /// Initialize the Raster function using the argument object. This is one of the two
        /// main functions to implement for a custom Raster function. The raster object is
        /// dereferenced if required and given to the RasterFuntionHelper object to bind.
        /// </summary>
        /// <param name="pArguments">Arguments object used for initialization</param>
        public void Bind(object pArguments)
        {
            try
            {
                // Check if the Arguments object is of the correct type.
                IWatermarkFunctionArguments watermarkFuncArgs = null;
                if (pArguments is IWatermarkFunctionArguments)
                {
                    watermarkFuncArgs    = (IWatermarkFunctionArguments)pArguments;
                    myBlendPercentage    = watermarkFuncArgs.BlendPercentage;
                    myWatermarkImagePath = watermarkFuncArgs.WatermarkImagePath;
                    xgap = watermarkFuncArgs.XGap;
                    ygap = watermarkFuncArgs.YGap;

                    object inputRaster = watermarkFuncArgs.Raster;
                    if (watermarkFuncArgs.Raster is IRasterFunctionVariable)
                    {
                        IRasterFunctionVariable rasterFunctionVariable =
                            (IRasterFunctionVariable)watermarkFuncArgs.Raster;
                        inputRaster = rasterFunctionVariable.Value;
                    }

                    // Call the Bind method of the Raster Function Helper object.
                    myFunctionHelper.Bind(inputRaster);
                }
                else
                {
                    // Throw an error if incorrect arguments object is passed.
                    throw new System.Exception(
                              "Incorrect arguments object. Expected: IWatermarkFunctionArguments");
                }

                // Get the raster info and Pixel Type from the RasterFunctionHelper object.
                myRasterInfo = myFunctionHelper.RasterInfo;
                myPixeltype  = myRasterInfo.PixelType;

                // Convert blending percentage to blending value.
                if (myBlendPercentage >= 0.0 && myBlendPercentage <= 100.0)
                {
                    blendValue = myBlendPercentage / 100.0;
                }
                else /// A value of 50% is used as default.
                {
                    blendValue = 0.50;
                }

                if (myWatermarkImagePath != "")
                {
                    // Load the watermark image from the path provided
                    myWatermarkImage = new Bitmap(myWatermarkImagePath);
                    // and check the pixel type of the loaded image to see if its compatible.
                    if (myWatermarkImage.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb &&
                        myWatermarkImage.PixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb)
                    {
                        // Throw error if the image is not compatible.
                        throw new System.Exception(
                                  "Invalid watermark image. Please provide one with 8 bits per band in ARGB or RGB format.");
                    }

                    // Cleanup
                    myWatermarkImage.Dispose();
                    myWatermarkImage = null;
                }
            }
            catch (Exception exc)
            {
                #region Cleanup
                if (myWatermarkImage != null)
                {
                    myWatermarkImage.Dispose();
                }
                myWatermarkImage = null;
                #endregion

                System.Exception myExc = new System.Exception(
                    "Exception caught in Bind method of Watermark Function. " + exc.Message, exc);
                throw myExc;
            }
        }