protected override bool ProcessEncoder(MainForm form, IWICBitmapEncoder encoder, object tag)
        {
            Tag t = (Tag)tag;
            IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory();
            IWICPalette        palette = factory.CreatePalette();

            palette.InitializePredefined(WICBitmapPaletteType.WICBitmapPaletteTypeFixedBW, false);
            IWICBitmapFrameEncode frame  = null;
            IWICBitmapFrameEncode frame2 = null;

            IPropertyBag2[] bag = new IPropertyBag2[1];
            try
            {
                MethodInfo mi = typeof(IWICBitmapEncoder).GetMethod("CreateNewFrame");
                try
                {
                    encoder.CreateNewFrame(out frame, bag);
                }
                catch (Exception e)
                {
                    form.Add(this, mi.ToString(Resources._0_Failed), new DataEntry(Resources.FrameIndex, 0), new DataEntry(e));
                }

                if (frame == null)
                {
                    form.Add(this, mi.ToString(Resources._0_NULL), new DataEntry(Resources.Parameter, mi.GetParameters()[0].Name));
                }
                else
                {
                    if (bag[0] == null)
                    {
                        form.Add(this, mi.ToString(Resources._0_NULL), new DataEntry(Resources.Parameter, mi.GetParameters()[1].Name));
                    }
                    try
                    {
                        frame.Initialize(bag[0]);
                        frame.SetSize(1, 1);
                        frame.SetPalette(palette);
                        Guid pixelFormat = Guid.Empty;

                        List <Guid> allPixelFormats = new List <Guid>(PixelFormatInfoRule.AllPixelFormats);
                        allPixelFormats.Add(Consts.GUID_WICPixelFormatDontCare);

                        foreach (Guid g in allPixelFormats)
                        {
                            pixelFormat = g;
                            frame.SetPixelFormat(ref pixelFormat);
                            if (g == pixelFormat)
                            {
                                if (Array.IndexOf(t.PixelFormats, g) < 0)
                                {
                                    form.Add(this, string.Format(CultureInfo.CurrentUICulture, Resources.DidNotChangeUnsupportedPixelFormat, "IWICBitmapFrameEncode::SetPixelFormat(...)"), new DataEntry(Resources.PixelFormat, g), new DataEntry(Resources.SupportedPixelFormats, t.PixelFormats));
                                }
                            }
                            else
                            {
                                if (Array.IndexOf(t.PixelFormats, g) >= 0)
                                {
                                    form.Add(this, string.Format(CultureInfo.CurrentUICulture, Resources.ChangedSupportedPixelFormat, "IWICBitmapFrameEncode::SetPixelFormat(...)"), new DataEntry(Resources.Expected, g), new DataEntry(Resources.Actual, pixelFormat));
                                }
                            }
                        }
                        pixelFormat = Consts.GUID_WICPixelFormat32bppBGRA;
                        frame.SetPixelFormat(ref pixelFormat);
                        byte[] buffer = new byte[(PixelFormatInfoRule.GetBitPerPixel(pixelFormat) + 7) / 8];
                        frame.WritePixels(1, (uint)buffer.Length, (uint)buffer.Length, buffer);
                        frame.Commit();

                        try
                        {
                            encoder.CreateNewFrame(out frame2, null);
                            if (!t.SupportsMultiframe)
                            {
                                form.Add(this, mi.ToString(Resources._0_ShouldFail), new DataEntry(WinCodecError.WINCODEC_ERR_UNSUPPORTEDOPERATION));
                            }
                        }
                        catch (Exception e)
                        {
                            if (t.SupportsMultiframe)
                            {
                                form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(Resources.FrameIndex, 1), new DataEntry(e));
                            }
                            else
                            {
                                form.CheckHRESULT(this, WinCodecError.WINCODEC_ERR_UNSUPPORTEDOPERATION, e, new DataEntry(Resources.FrameIndex, 1));
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(e));
                    }
                }
            }
            finally
            {
                frame2.ReleaseComObject();
                encoder.ReleaseComObject();
                bag.ReleaseComObject();
                factory.ReleaseComObject();
                palette.ReleaseComObject();
            }

            return(base.ProcessEncoder(form, encoder, tag));
        }
Exemple #2
0
        protected override bool ProcessFrameDecode(MainForm form, IWICBitmapFrameDecode frame, DataEntry[] de, object tag)
        {
            Guid pixelFormatOriginal;

            frame.GetPixelFormat(out pixelFormatOriginal);

            IWICDevelopRaw raw = null;

            try
            {
                raw = (IWICDevelopRaw)frame;
                if (raw == null)
                {
                    form.Add(this, string.Format(CultureInfo.CurrentUICulture, Resources._0_NULL, "IWICBitmapFrameDecode::QueryInterface(IID_IWICDevelopRaw, ...)"), de);
                }
                else
                {
                    CheckPixelFormat(form, raw, de, pixelFormatOriginal);

                    WICRawCapabilitiesInfo capabilities = new WICRawCapabilitiesInfo();
                    capabilities.cbSize = (uint)Marshal.SizeOf(capabilities);
                    try
                    {
                        raw.QueryRawCapabilitiesInfo(ref capabilities);
                    }
                    catch (Exception e)
                    {
                        form.Add(this, e.TargetSite.ToString(Resources._0_Failed), de, new DataEntry(e));

                        return(true);
                    }
                    bool supports = false;
                    foreach (FieldInfo fi in typeof(WICRawCapabilitiesInfo).GetFields())
                    {
                        if (fi.FieldType == typeof(WICRawCapabilities) || fi.FieldType == typeof(WICRawRotationCapabilities))
                        {
                            if (!Enum.IsDefined(fi.FieldType, fi.GetValue(capabilities)))
                            {
                                form.Add(this, string.Format(CultureInfo.CurrentUICulture, Resources._0_UnexpectedFieldValue, fi.Name), de, new DataEntry(Resources.Expected, Enum.GetValues(fi.FieldType)), new DataEntry(Resources.Actual, fi.GetValue(capabilities)));
                            }
                            supports |= 0 != (uint)Convert.ChangeType(fi.GetValue(capabilities), typeof(uint));
                        }
                    }
                    if (!supports)
                    {
                        form.Add(this, Resources.DevelopRaw_NoSupportedFeatures, de);
                    }

                    CheckGetSupported <double>(form, GetSupported(capabilities.ContrastSupport), raw.GetContrast, de, 0);
                    CheckGetSupported(form, GetSupported(capabilities.DestinationColorProfileSupport), raw.GetColorContexts, de).ReleaseComObject();
                    CheckGetSupported <double>(form, GetSupported(capabilities.ExposureCompensationSupport), raw.GetExposureCompensation, de, 0);
                    CheckGetSupported <double>(form, GetSupported(capabilities.GammaSupport), raw.GetGamma, de, 1);
                    uint?kelvinWhitePoint = CheckGetSupported <uint>(form, GetSupported(capabilities.KelvinWhitePointSupport), raw.GetWhitePointKelvin, de, null);
                    WICNamedWhitePoint?namedWhitePoint = CheckGetSupported <WICNamedWhitePoint>(form, GetSupported(capabilities.NamedWhitePointSupport), raw.GetNamedWhitePoint, de, null);
                    CheckGetSupported <double>(form, GetSupported(capabilities.NoiseReductionSupport), raw.GetNoiseReduction, de, 0);
                    uint[]           rgbWhitePoint = CheckGetSupported(form, GetSupported(capabilities.RGBWhitePointSupport), raw.GetWhitePointRGB, de, null);
                    WICRawRenderMode?renderingMode = CheckGetSupported <WICRawRenderMode>(form, GetSupported(capabilities.RenderModeSupport), raw.GetRenderMode, de, WICRawRenderMode.WICRawRenderModeNormal);
                    CheckGetSupported <double>(form, GetSupported(capabilities.RotationSupport), raw.GetRotation, de, 0);
                    CheckGetSupported <double>(form, GetSupported(capabilities.SaturationSupport), raw.GetSaturation, de, 0);
                    CheckGetSupported <double>(form, GetSupported(capabilities.SharpnessSupport), raw.GetSharpness, de, 0);
                    CheckGetSupported <double>(form, GetSupported(capabilities.TintSupport), raw.GetTint, de, 0);
                    WICRawToneCurvePoint[] toneCurve = CheckGetSupported(form, GetSupported(capabilities.ToneCurveSupport), raw.GetToneCurve, de);

                    CheckSetSupported <double>(form, SetSupported(capabilities.ContrastSupport), raw.GetContrast, raw.SetContrast, raw, WICRawChangeNotification.WICRawChangeNotification_Contrast, de, pixelFormatOriginal, -1, 0, 1);
                    CheckSetSupported <double>(form, SetSupported(capabilities.ExposureCompensationSupport), raw.GetExposureCompensation, raw.SetExposureCompensation, raw, WICRawChangeNotification.WICRawChangeNotification_ExposureCompensation, de, pixelFormatOriginal, 0, -5, 5);
                    CheckSetSupported <double>(form, SetSupported(capabilities.GammaSupport), raw.GetGamma, raw.SetGamma, raw, WICRawChangeNotification.WICRawChangeNotification_Gamma, de, pixelFormatOriginal, 1, 0.2, 5);
                    CheckSetSupported <double>(form, SetSupported(capabilities.NoiseReductionSupport), raw.GetNoiseReduction, raw.SetNoiseReduction, raw, WICRawChangeNotification.WICRawChangeNotification_NoiseReduction, de, pixelFormatOriginal, 0, 1);
                    CheckSetSupported <double>(form, SetSupported(capabilities.SaturationSupport), raw.GetSaturation, raw.SetSaturation, raw, WICRawChangeNotification.WICRawChangeNotification_Saturation, de, pixelFormatOriginal, 0, -1, 1);
                    CheckSetSupported <double>(form, SetSupported(capabilities.SharpnessSupport), raw.GetSharpness, raw.SetSharpness, raw, WICRawChangeNotification.WICRawChangeNotification_Sharpness, de, pixelFormatOriginal, 0, 1);
                    CheckSetSupported <double>(form, SetSupported(capabilities.TintSupport), raw.GetTint, raw.SetTint, raw, WICRawChangeNotification.WICRawChangeNotification_Tint, de, pixelFormatOriginal, 0, -1, 1);
                    CheckSetSupported <WICRawRenderMode>(form, SetSupported(capabilities.RenderModeSupport), raw.GetRenderMode, raw.SetRenderMode, raw, WICRawChangeNotification.WICRawChangeNotification_RenderMode, de, pixelFormatOriginal, WICRawRenderMode.WICRawRenderModeDraft, WICRawRenderMode.WICRawRenderModeNormal, WICRawRenderMode.WICRawRenderModeBestQuality);

                    try
                    {
                        uint max, min, step;
                        raw.GetKelvinRangeInfo(out min, out max, out step);
                        MethodInfo mi = typeof(IWICDevelopRaw).GetMethod("GetKelvinRangeInfo");
                        if ((max < min) ||
                            (min == max && step != 0) ||
                            (min != max && (step == 0 || ((max - min) % step) != 0)))
                        {
                            ParameterInfo[] pi = mi.GetParameters();
                            form.Add(this, mi.ToString(Resources._0_NotExpectedValue), de, new DataEntry(pi[0].Name, max), new DataEntry(pi[1].Name, min), new DataEntry(pi[2].Name, step));
                        }
                        else
                        {
                            uint[] goodTemps = min == max ? new uint[] { min } : new uint[] { min, min + step, max, max - step };
                            uint[] badTemps  = step == 1 ? new uint[] { min - step, max + step } : new uint[] { min - step, max + step, min + step / 2, max - step / 2 };

                            CheckSetSupported <uint>(form, null, raw.GetWhitePointKelvin, raw.SetWhitePointKelvin, raw, WICRawChangeNotification.WICRawChangeNotification_KelvinWhitePoint, de, pixelFormatOriginal, goodTemps);
                            CheckSetSupported <uint>(form, WinCodecError.WINCODEC_ERR_VALUEOUTOFRANGE, raw.GetWhitePointKelvin, raw.SetWhitePointKelvin, raw, WICRawChangeNotification.WICRawChangeNotification_KelvinWhitePoint, de, pixelFormatOriginal, badTemps);
                        }
                    }
                    catch (Exception e)
                    {
                        form.Add(this, e.TargetSite.ToString(Resources._0_Failed), de, new DataEntry(e));
                    }

                    CheckSetDestinationColorContextSupported(form, SetSupported(capabilities.DestinationColorProfileSupport), raw, de, pixelFormatOriginal);

                    switch (capabilities.RotationSupport)
                    {
                    case WICRawRotationCapabilities.WICRawRotationCapabilityFullySupported:
                        CheckSetSupported <double>(form, null, raw.GetRotation, raw.SetRotation, raw, WICRawChangeNotification.WICRawChangeNotification_Rotation, de, pixelFormatOriginal, rotation);
                        break;

                    case WICRawRotationCapabilities.WICRawRotationCapabilityNinetyDegreesSupported:
                        CheckSetSupported <double>(form, null, raw.GetRotation, raw.SetRotation, raw, WICRawChangeNotification.WICRawChangeNotification_Rotation, de, pixelFormatOriginal, rotation90);
                        CheckSetSupported <double>(form, WinCodecError.WINCODEC_ERR_VALUEOUTOFRANGE, raw.GetRotation, raw.SetRotation, raw, WICRawChangeNotification.WICRawChangeNotification_Rotation, de, pixelFormatOriginal, rotation90);
                        break;

                    default:
                        CheckSetSupported <double>(form, WinCodecError.WINCODEC_ERR_UNSUPPORTEDOPERATION, raw.GetRotation, raw.SetRotation, raw, WICRawChangeNotification.WICRawChangeNotification_Rotation, de, pixelFormatOriginal, rotation);
                        break;
                    }

                    CheckSetNamedWhitePointSupported(form, SetSupported(capabilities.NamedWhitePointSupport), capabilities.NamedWhitePointSupportMask, raw, de, pixelFormatOriginal);
                    CheckSetRgbWhitePointSupported(form, SetSupported(capabilities.RGBWhitePointSupport), raw, de, pixelFormatOriginal);
                    CheckSetToneCurveSupported(form, SetSupported(capabilities.ToneCurveSupport), raw, de, pixelFormatOriginal);

                    foreach (WICRawParameterSet ps in Enum.GetValues(typeof(WICRawParameterSet)))
                    {
                        try
                        {
                            raw.LoadParameterSet(ps);

                            IPropertyBag2 pb = null;
                            try
                            {
                                pb = raw.GetCurrentParameterSet();
                            }
                            catch (Exception e)
                            {
                                form.Add(this, e.TargetSite.ToString(Resources._0_Failed), de, new DataEntry(e));
                            }
                            finally
                            {
                                pb.ReleaseComObject();
                            }
                        }
                        catch (Exception e)
                        {
                            form.Add(this, e.TargetSite.ToString(Resources._0_Failed), de, new DataEntry(e), new DataEntry(Resources.Value, ps));
                        }
                    }
                }
            }
            catch (InvalidCastException)
            {
                return(false);
            }

            return(true);
        }