Example #1
0
        private void RunRepeatFilter(ref PSFilterPdnConfigToken token, IWin32Window window)
        {
            try
            {
                using (LoadPsFilter lps = new LoadPsFilter(EnvironmentParameters, window.Handle, null))
                {
                    lps.SetAbortCallback(AbortCallback);
                    if (token.DescriptorRegistry != null)
                    {
                        lps.SetRegistryValues(token.DescriptorRegistry);
                    }

                    ParameterData parameterData;
                    if (token.FilterParameters.TryGetValue(token.FilterData, out parameterData))
                    {
                        lps.FilterParameters = parameterData;
                    }
                    lps.PseudoResources = token.PseudoResources;
                    lps.IsRepeatEffect  = true;

                    bool result = lps.RunPlugin(token.FilterData, false);

                    if (result)
                    {
                        token.Dest = lps.Dest.Clone();
                    }
                    else if (!string.IsNullOrEmpty(lps.ErrorMessage))
                    {
                        ShowErrorMessage(window, lps.ErrorMessage);
                    }
                }
            }
            catch (FileNotFoundException fnfex)
            {
                ShowErrorMessage(window, fnfex.Message);
            }
            catch (NullReferenceException nrex)
            {
                // The filter probably tried to access an unimplemented callback function without checking if it is valid.
                ShowErrorMessage(window, nrex.Message);
            }
            catch (Win32Exception w32ex)
            {
                ShowErrorMessage(window, w32ex.Message);
            }
            catch (System.Runtime.InteropServices.ExternalException eex)
            {
                ShowErrorMessage(window, eex.Message);
            }
            finally
            {
                filterDone.Set();
            }
        }
Example #2
0
        public static void ShowAboutDialog(PluginData pluginData, IntPtr parentWindowHandle)
        {
            if (pluginData == null)
            {
                throw new ArgumentNullException(nameof(pluginData));
            }

            if (pluginData.HasAboutBox)
            {
                string errorMessage = string.Empty;

                bool result;

                try
                {
                    result = LoadPsFilter.ShowAboutDialog(pluginData, parentWindowHandle, out errorMessage);
                }
                catch (FileNotFoundException)
                {
                    throw;
                }
                catch (SecurityException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    if (ex is OutOfMemoryException || ex is StackOverflowException || ex is ThreadAbortException)
                    {
                        throw;
                    }

                    throw new FilterRunException(ex.Message, ex);
                }

                if (!result && !string.IsNullOrEmpty(errorMessage))
                {
                    throw new FilterRunException(errorMessage);
                }
            }
        }
Example #3
0
        static void RunFilter()
        {
            PluginData           pdata    = pipeClient.GetPluginData();
            PSFilterShimSettings settings = pipeClient.GetShimSettings();

            Region selectionRegion = null;

            if (!string.IsNullOrEmpty(settings.RegionDataPath))
            {
                RegionDataWrapper wrapper = DataContractSerializerUtil.Deserialize <RegionDataWrapper>(settings.RegionDataPath);

                using (Region temp = new Region())
                {
                    RegionData rgnData = temp.GetRegionData();
                    rgnData.Data = wrapper.GetData();

                    selectionRegion = new Region(rgnData);
                }
            }
            try
            {
                ParameterData filterParameters = null;
                try
                {
                    filterParameters = DataContractSerializerUtil.Deserialize <ParameterData>(settings.ParameterDataPath);
                }
                catch (FileNotFoundException)
                {
                }

                PseudoResourceCollection pseudoResources = null;
                try
                {
                    pseudoResources = DataContractSerializerUtil.Deserialize <PseudoResourceCollection>(settings.PseudoResourcePath);
                }
                catch (FileNotFoundException)
                {
                }

                DescriptorRegistryValues registryValues = null;
                try
                {
                    registryValues = DataContractSerializerUtil.Deserialize <DescriptorRegistryValues>(settings.DescriptorRegistryPath);
                }
                catch (FileNotFoundException)
                {
                }

                using (LoadPsFilter lps = new LoadPsFilter(settings, selectionRegion))
                {
                    lps.SetAbortCallback(pipeClient.AbortFilter);

                    if (!settings.RepeatEffect)
                    {
                        // As Paint.NET does not currently allow custom progress reporting only set this callback for the effect dialog.
                        lps.SetProgressCallback(pipeClient.UpdateFilterProgress);
                    }

                    if (filterParameters != null)
                    {
                        // Ignore the filters that only use the data handle, e.g. Filter Factory.
                        byte[] parameterData = filterParameters.GlobalParameters.GetParameterDataBytes();

                        if (parameterData != null || filterParameters.AETEDictionary != null)
                        {
                            lps.FilterParameters = filterParameters;
                            lps.IsRepeatEffect   = settings.RepeatEffect;
                        }
                    }

                    if (pseudoResources != null)
                    {
                        lps.PseudoResources = pseudoResources;
                    }

                    if (registryValues != null)
                    {
                        lps.SetRegistryValues(registryValues);
                    }

                    bool result = lps.RunPlugin(pdata, settings.ShowAboutDialog);

                    if (result)
                    {
                        if (!settings.ShowAboutDialog)
                        {
                            PSFilterShimImage.Save(settings.DestinationImagePath, lps.Dest);

                            if (!lps.IsRepeatEffect)
                            {
                                DataContractSerializerUtil.Serialize(settings.ParameterDataPath, lps.FilterParameters);
                                DataContractSerializerUtil.Serialize(settings.PseudoResourcePath, lps.PseudoResources);

                                registryValues = lps.GetRegistryValues();
                                if (registryValues != null)
                                {
                                    DataContractSerializerUtil.Serialize(settings.DescriptorRegistryPath, registryValues);
                                }
                            }
                        }
                    }
                    else
                    {
                        pipeClient.SetProxyErrorMessage(lps.ErrorMessage);
                    }
                }
            }
            catch (BadImageFormatException ex)
            {
                pipeClient.SetProxyErrorMessage(ex.Message);
            }
            catch (EntryPointNotFoundException epnf)
            {
                pipeClient.SetProxyErrorMessage(epnf.Message);
            }
            catch (FileNotFoundException fx)
            {
                pipeClient.SetProxyErrorMessage(fx.Message);
            }
            catch (NullReferenceException ex)
            {
#if DEBUG
                pipeClient.SetProxyErrorMessage(ex.Message + Environment.NewLine + ex.StackTrace);
#else
                pipeClient.SetProxyErrorMessage(ex.Message);
#endif
            }
            catch (Win32Exception ex)
            {
                pipeClient.SetProxyErrorMessage(ex.Message);
            }
            finally
            {
                if (selectionRegion != null)
                {
                    selectionRegion.Dispose();
                    selectionRegion = null;
                }
            }
        }
Example #4
0
        public bool RunFilter(PluginData pluginData, bool showUI)
        {
            if (pluginData == null)
            {
                throw new ArgumentNullException(nameof(pluginData));
            }

            if (disposed)
            {
                throw new ObjectDisposedException(nameof(PSFilterHost));
            }

            if (!showUI && filterParameters == null)
            {
                throw new InvalidOperationException(Resources.ParametersNotSet);
            }

            bool result = false;

            using (LoadPsFilter lps = new LoadPsFilter(source, primaryColor, secondaryColor, selectedRegion, owner))
            {
                if (abortFunc != null)
                {
                    lps.SetAbortFunc(abortFunc);
                }

                if (pickColor != null)
                {
                    lps.SetPickColor(pickColor);
                }

                if (UpdateProgress != null)
                {
                    lps.SetProgressFunc(new ProgressProc(OnFilterReportProgress));
                }

                if (filterParameters != null)
                {
                    lps.ParameterData = filterParameters;
                    lps.ShowUI        = showUI;
                }

                if (pseudoResources != null)
                {
                    lps.PseudoResources = pseudoResources;
                }

                if (hostInfo != null)
                {
                    lps.HostInformation = hostInfo;
                }

                if (hostColorProfiles != null)
                {
                    lps.SetColorProfiles(hostColorProfiles);
                }

                if (sessionSettings != null)
                {
                    lps.SetPluginSettings(sessionSettings);
                }

                try
                {
                    result = lps.RunPlugin(pluginData);
                }
                catch (FileNotFoundException)
                {
                    throw;
                }
                catch (SecurityException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    if (ex is OutOfMemoryException || ex is StackOverflowException || ex is ThreadAbortException)
                    {
                        throw;
                    }

                    throw new FilterRunException(ex.Message, ex);
                }

                if (result)
                {
#if GDIPLUS
                    dest = lps.Dest.ToGdipBitmap();
#else
                    dest = lps.Dest.ToBitmapSource();
                    dest.Freeze();
#endif

                    filterParameters = lps.ParameterData;
                    pseudoResources  = lps.PseudoResources;
                    hostInfo         = lps.HostInformation;
                    sessionSettings  = lps.GetPluginSettings();
                }
                else if (!string.IsNullOrEmpty(lps.ErrorMessage))
                {
                    throw new FilterRunException(lps.ErrorMessage);
                }
            }

            return(result);
        }