protected override void RunOverride(MainForm form, object tag) { bool info2Supported = false; IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); IWICPixelFormatInfo2 info = null; try { info = factory.CreateComponentInfo(Parent.Clsid) as IWICPixelFormatInfo2; info2Supported = info != null; } finally { info.ReleaseComObject(); factory.ReleaseComObject(); } if (info2Supported) { ComponentInfoHelper.Check <IWICPixelFormatInfo2, object>(form, Parent.Clsid, Check, tag, this, false); } else { ComponentInfoHelper.Check <IWICPixelFormatInfo, object>(form, Parent.Clsid, Check, tag, this, false); } }
public static void CheckEquals <TInfo>(MainForm form, Func <TInfo> method, RuleBase <ComponentRuleGroup> rule, Func <TInfo, TInfo, DataEntry[][]> compare) where TInfo : class, IWICComponentInfo { IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); TInfo info = factory.CreateComponentInfo(rule.Parent.Clsid) as TInfo; TInfo i = null; try { i = method(); if (i == null) { form.Add(rule, method.ToString(Resources._0_NULL)); } else { foreach (DataEntry[] de in compare(info, i)) { form.Add(rule, string.Format(CultureInfo.CurrentUICulture, Resources.InconsistentComponentInfo, typeof(TInfo).Name, method.ToString("{0}")), de); } } } catch (Exception e) { form.Add(rule, method.ToString(Resources._0_Failed), new DataEntry(e)); } finally { i.ReleaseComObject(); info.ReleaseComObject(); factory.ReleaseComObject(); } }
private bool CheckConvertion(MainForm form, IWICFormatConverterInfo info, Guid from, Guid to) { if (from == to) { return(true); } IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); IWICPalette palette = factory.CreatePalette(); palette.InitializePredefined(WICBitmapPaletteType.WICBitmapPaletteTypeFixedBW, false); IWICBitmap bitmap = null; IWICFormatConverter converter = null; try { try { converter = info.CreateInstance(); } catch (Exception e) { form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(e)); return(false); } try { bitmap = factory.CreateBitmap(1, 1, from, WICBitmapCreateCacheOption.WICBitmapCacheOnLoad); bitmap.SetPalette(palette); } catch (Exception e) { form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(e), new DataEntry(Resources.PixelFormat, from)); return(false); } try { converter.Initialize(bitmap, to, WICBitmapDitherType.WICBitmapDitherTypeNone, palette, 0, WICBitmapPaletteType.WICBitmapPaletteTypeCustom); } catch (Exception e) { form.CheckHRESULT(this, WinCodecError.WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT, e, new DataEntry(Resources.Source, from), new DataEntry(Resources.Destination, to)); return(false); } return(true); } finally { palette.ReleaseComObject(); converter.ReleaseComObject(); bitmap.ReleaseComObject(); factory.ReleaseComObject(); } }
void CheckCopyPalette(MainForm form, DataEntry[] de, Action <IWICPalette> method) { IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); IWICPalette palette = factory.CreatePalette(); try { method(palette); try { if (palette.GetColorCount() == 0) { form.Add(this, method.ToString(Resources._0_ZeroColorPalette), de); } } catch (Exception e) { form.Add(this, method.ToString(Resources._0_IncorrectStatePalette), de, new DataEntry(e)); } } catch (Exception e) { form.CheckHRESULT(this, WinCodecError.WINCODEC_ERR_PALETTEUNAVAILABLE, e, de); } finally { palette.ReleaseComObject(); factory.ReleaseComObject(); } }
protected override void RunOverride(ListView.ListViewItemCollection collection, object tag) { IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); IWICBitmapDecoderInfo info = null; try { info = (IWICBitmapDecoderInfo)factory.CreateComponentInfo(Parent.Clsid); string[] mimes = Extensions.GetString(info.GetMimeTypes).Split(','); foreach (string ext in Extensions.GetString(info.GetFileExtensions).Split(',')) { using (RegistryKey rk = OpenSubKey(collection, Registry.ClassesRoot, ext, new DataEntry[] { new DataEntry("File Extension", ext) })) { if (rk != null) { Check(collection, ext, rk, info); } } } } finally { factory.ReleaseComObject(); info.ReleaseComObject(); } }
static void Main(string[] args) { IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); IWICBitmapDecoder decoder = null; IWICBitmapFrameDecode frame = null; IWICFormatConverter formatConverter = null; try { const string filename = @"<filename>"; decoder = factory.CreateDecoderFromFilename(filename, null, NativeMethods.GenericAccessRights.GENERIC_READ, WICDecodeOptions.WICDecodeMetadataCacheOnDemand); uint count = decoder.GetFrameCount(); frame = decoder.GetFrame(0); uint width = 0; uint height = 0; frame.GetSize(out width, out height); Guid pixelFormat; frame.GetPixelFormat(out pixelFormat); // The frame can use many different pixel formats. // You can copy the raw pixel values by calling "frame.CopyPixels( )". // This method needs a buffer that can hold all bytes. // The total number of bytes is: width x height x bytes per pixel // The disadvantage of this solution is that you have to deal with all possible pixel formats. // You can make your life easy by converting the frame to a pixel format of // your choice. The code below shows how to convert the pixel format to 24-bit RGB. formatConverter = factory.CreateFormatConverter(); formatConverter.Initialize(frame, Consts.GUID_WICPixelFormat24bppRGB, WICBitmapDitherType.WICBitmapDitherTypeNone, null, 0.0, WICBitmapPaletteType.WICBitmapPaletteTypeCustom); uint bytesPerPixel = 3; // Because we have converted the frame to 24-bit RGB uint stride = width * bytesPerPixel; byte[] bytes = new byte[stride * height]; formatConverter.CopyPixels(null, stride, stride * height, bytes); } finally { frame.ReleaseComObject(); decoder.ReleaseComObject(); factory.ReleaseComObject(); } }
private void DisplayImageInternal() { IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); IWICBitmapScaler scaler = factory.CreateBitmapScaler(); IWICFormatConverter formatConvert = factory.CreateFormatConverter(); GCHandle h = new GCHandle(); Image image = rawPictureBox.Image; try { uint pixelColorWidth = 3; // 3 bytes/channel for Consts.GUID_WICPixelFormat24bppRGB, or more generally (((bits / pixel) + 7) / 8) uint width = (uint)rawPictureBox.Width; uint height = (uint)rawPictureBox.Height; scaler.Initialize(frame, width, height, WICBitmapInterpolationMode.WICBitmapInterpolationModeFant); formatConvert.Initialize(scaler, Consts.GUID_WICPixelFormat24bppBGR, WICBitmapDitherType.WICBitmapDitherTypeNone, null, 0.0, WICBitmapPaletteType.WICBitmapPaletteTypeMedianCut); uint stride = width * pixelColorWidth; uint size = stride * height; byte[] pixels = new byte[size]; formatConvert.CopyPixels(null, stride, size, pixels); h = GCHandle.Alloc(pixels, GCHandleType.Pinned); Bitmap bitmap = new Bitmap((int)width, (int)height, (int)stride, PixelFormat.Format24bppRgb, h.AddrOfPinnedObject()); rawPictureBox.Image = bitmap; } catch { } finally { if (image != null) { image.Dispose(); } if (h.IsAllocated) { h.Free(); } scaler.ReleaseComObject(); formatConvert.ReleaseComObject(); factory.ReleaseComObject(); } }
public static uint GetBitPerPixel(Guid pixelFormat) { IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); IWICPixelFormatInfo info = null; try { info = (IWICPixelFormatInfo)factory.CreateComponentInfo(pixelFormat); return(info.GetBitsPerPixel()); } finally { factory.ReleaseComObject(); info.ReleaseComObject(); } }
protected override void RunOverride(MainForm form, object tag) { TempFileCollection files = null; IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); IWICBitmapEncoderInfo info = null; IWICBitmapEncoder encoder = null; IWICStream stream = null; int i = 0; try { files = new TempFileCollection(); info = (IWICBitmapEncoderInfo)factory.CreateComponentInfo(Parent.Clsid); do { stream.ReleaseComObject(); encoder.ReleaseComObject(); stream = factory.CreateStream(); stream.InitializeFromFilename(files.AddExtension(i.ToString(CultureInfo.InvariantCulture)), NativeMethods.GenericAccessRights.GENERIC_WRITE); i++; try { encoder = info.CreateInstance(); encoder.Initialize(stream, WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache); } catch (Exception e) { form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(e)); break; } }while(ProcessEncoder(form, encoder, tag)); } finally { factory.ReleaseComObject(); info.ReleaseComObject(); encoder.ReleaseComObject(); stream.ReleaseComObject(); if (files != null) { files.Delete(); } } }
private void filesListView_SelectedIndexChanged(object sender, EventArgs e) { if (filesListView.SelectedItems.Count > 0) { string file = filesListView.SelectedItems[0].Text; if (decoder != null) { decoder.ReleaseComObject(); decoder = null; } if (frame != null) { frame.ReleaseComObject(); frame = null; } IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); decoder = factory.CreateDecoderFromFilename(file, null, NativeMethods.GenericAccessRights.GENERIC_READ, WICDecodeOptions.WICDecodeMetadataCacheOnLoad); if (decoder != null) { uint frameCount = decoder.GetFrameCount(); if (frameCount > 0) { frame = decoder.GetFrame(0); } if (frame != null) { setupMode = true; SetupDevelopRaw(); setupMode = false; DisplayImage(); } } factory.ReleaseComObject(); } }
protected override bool ProcessEncoder(MainForm form, IWICBitmapEncoder encoder, object tag) { IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); IWICBitmap bitmap = factory.CreateBitmap(16, 16, Consts.GUID_WICPixelFormat32bppBGRA, WICBitmapCreateCacheOption.WICBitmapCacheOnLoad); IWICPalette palette = factory.CreatePalette(); palette.InitializePredefined(WICBitmapPaletteType.WICBitmapPaletteTypeFixedBW, false); IWICColorContext context = factory.CreateColorContext(); context.InitializeFromExifColorSpace(ExifColorSpace.sRGB); ComponentInfoHelper.CheckEquals <IWICBitmapEncoderInfo>(form, encoder.GetEncoderInfo, this, Extensions.CompareInfos); Check(form, encoder.SetPalette, palette); Check(form, encoder.SetThumbnail, bitmap); Check(form, encoder.SetPreview, bitmap); try { encoder.SetColorContexts(1, new IWICColorContext[] { context }); } catch (Exception e) { form.CheckHRESULT(this, WinCodecError.WINCODEC_ERR_UNSUPPORTEDOPERATION, e); } try { encoder.Commit(); form.Add(this, string.Format(CultureInfo.CurrentUICulture, Resources._0_ShouldFail, "IWICBitmapEncoder::Commit(...)"), new DataEntry(Resources.FrameCount, 0)); } catch (Exception e) { form.CheckHRESULT(this, WinCodecError.WINCODEC_ERR_FRAMEMISSING, e, new DataEntry(Resources.FrameCount, 0)); } palette.ReleaseComObject(); bitmap.ReleaseComObject(); factory.ReleaseComObject(); context.ReleaseComObject(); return(base.ProcessEncoder(form, encoder, tag)); }
public static void Check <TInfo, TTag>(MainForm form, Guid clsid, Action <MainForm, TInfo, TTag> check, TTag tag, RuleBase rule, bool checkVersions) where TInfo : class, IWICComponentInfo where TTag : class { IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); TInfo info = null; try { info = (TInfo)factory.CreateComponentInfo(clsid); CheckNotEmptyString(form, info.GetAuthor, rule); CheckNotEmptyString(form, info.GetFriendlyName, rule); if ((info.GetSigningStatus() & WICComponentSigning.WICComponentDisabled) == WICComponentSigning.WICComponentDisabled) { form.Add(rule, Resources.ComponentDisabled); } CheckNotReserverdGuid(form, info.GetVendorGUID, rule); if (checkVersions || info.GetSpecVersion(0, null) > 0) { CheckVersion(form, info.GetSpecVersion, rule); } if (checkVersions || info.GetVersion(0, null) > 0) { CheckVersion(form, info.GetVersion, rule); } check(form, info, tag); } finally { factory.ReleaseComObject(); info.ReleaseComObject(); } }
public static IEnumerable <IWICComponentInfo> GetComponentInfos(WICComponentType type) { IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); IEnumUnknown eu = null; object[] o = new object[1]; try { eu = factory.CreateComponentEnumerator(type, WICComponentEnumerateOptions.WICComponentEnumerateRefresh | WICComponentEnumerateOptions.WICComponentEnumerateDisabled); int hr = 0; while (hr == 0) { uint fetched = 0; hr = eu.Next(1, o, ref fetched); Marshal.ThrowExceptionForHR(hr); if (fetched == 1) { IWICComponentInfo ci = (IWICComponentInfo)o[0]; try { yield return(ci); } finally { ci.ReleaseComObject(); } } } } finally { factory.ReleaseComObject(); eu.ReleaseComObject(); } }
protected override void RunOverride(MainForm form, object tag) { IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); IWICBitmapDecoderInfo info = (IWICBitmapDecoderInfo)factory.CreateComponentInfo(Parent.Clsid); Guid cf; info.GetContainerFormat(out cf); IWICBitmapDecoder decoder = null; try { decoder = info.CreateInstance(); Guid containerFormat; try { decoder.GetContainerFormat(out containerFormat); if (containerFormat != cf) { form.Add(this, Resources.ContainerFormatsDoNotMatch, new DataEntry(Resources.Actual, cf), new DataEntry(Resources.Expected, containerFormat)); } } catch (Exception e) { form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(e)); } ComponentInfoHelper.CheckEquals <IWICBitmapDecoderInfo>(form, decoder.GetDecoderInfo, this, Extensions.CompareInfos); } finally { factory.ReleaseComObject(); info.ReleaseComObject(); decoder.ReleaseComObject(); } base.RunOverride(form, tag); }
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)); }
protected override void RunOverride(MainForm form, object tag) { bool hasFile = false; bool processed = false; IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); IWICBitmapDecoderInfo info = null; IWICBitmapDecoder decoder = null; try { info = (IWICBitmapDecoderInfo)factory.CreateComponentInfo(Parent.Clsid); foreach (string file in GetDecodableFiles(form, Parent.Clsid)) { decoder.ReleaseComObject(); decoder = info.CreateInstance(); hasFile = true; IWICStream stream = factory.CreateStream(); stream.InitializeFromFilename(file, NativeMethods.GenericAccessRights.GENERIC_READ); try { decoder.Initialize(stream, WICDecodeOptions.WICDecodeMetadataCacheOnDemand); if (ProcessDecoder(form, decoder, new DataEntry[] { new DataEntry(file) }, tag)) { for (uint index = 0; index < decoder.GetFrameCount(); index++) { IWICBitmapFrameDecode frame = decoder.GetFrame(index); try { if (ProcessFrameDecode(form, frame, new DataEntry[] { new DataEntry(file), new DataEntry(index) }, tag)) { processed = true; } } finally { frame.ReleaseComObject(); } } } else { processed = true; } } catch (Exception e) { form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(file), new DataEntry(e)); } finally { stream.ReleaseComObject(); } } } finally { factory.ReleaseComObject(); info.ReleaseComObject(); decoder.ReleaseComObject(); } if (!hasFile) { form.Add(Parent, string.Format(CultureInfo.CurrentUICulture, Resources.NoFilesFor_0, Parent.Text)); } else if (!processed) { form.Add(this, string.Format(CultureInfo.CurrentUICulture, Resources.NoFilesFor_0, Text)); } }
protected override bool ProcessEncoder(MainForm form, IWICBitmapEncoder encoder, object tag) { IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); IWICBitmap bitmap = factory.CreateBitmap(1, 1, Consts.GUID_WICPixelFormat128bpp7ChannelsAlpha, WICBitmapCreateCacheOption.WICBitmapCacheOnLoad); IWICBitmapFrameEncode frame = null; try { try { encoder.CreateNewFrame(out frame, null); } catch (Exception e) { form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(e)); } if (frame != null) { try { frame.Initialize(null); } catch (Exception e) { form.Add(this, e.TargetSite.ToString(Resources._0_Failed, "NULL"), new DataEntry(e)); frame.ReleaseComObject(); frame = null; } } if (frame != null) { try { frame.WriteSource(bitmap, null); } catch (Exception e) { form.Add(this, e.TargetSite.ToString(Resources._0_Failed, "..., NULL"), new DataEntry(e)); frame.ReleaseComObject(); frame = null; } } if (frame != null) { try { frame.Commit(); encoder.Commit(); } catch (Exception e) { form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(e)); } } } finally { frame.ReleaseComObject(); bitmap.ReleaseComObject(); factory.ReleaseComObject(); } return(base.ProcessEncoder(form, encoder, tag)); }
IEnumerable <string> GetDecodableFiles(MainForm form, Guid decoderClsid) { IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); IWICBitmapDecoderInfo info = null; IWICBitmapDecoder decoder = null; try { info = (IWICBitmapDecoderInfo)factory.CreateComponentInfo(decoderClsid); if (Settings.Default.Files != null) { foreach (string s in Settings.Default.Files) { IWICStream stream = factory.CreateStream(); try { stream.InitializeFromFilename(s, NativeMethods.GenericAccessRights.GENERIC_READ); bool matches = info.MatchesPattern(stream); stream.Seek(0, 0, IntPtr.Zero); decoder = info.CreateInstance(); if (decoder != null) { WICBitmapDecoderCapabilities?capabilities = null; try { capabilities = decoder.QueryCapability(stream); if (!matches) { QueryCapabilitiesError(form, string.Format(CultureInfo.CurrentUICulture, Resources.PatternDoesnotMatchButPassed, "IWICBitmapDecoder::QueryCapability(...)"), new DataEntry(s)); } } catch (Exception e) { if (Array.IndexOf(queryCapabilitiesErrors, (WinCodecError)Marshal.GetHRForException(e)) < 0) { QueryCapabilitiesError(form, e.TargetSite.ToString(Resources._0_FailedWithIncorrectHRESULT), new DataEntry(s), new DataEntry(Resources.Actual, e), new DataEntry(Resources.Expected, queryCapabilitiesErrors)); } } if (capabilities.HasValue && 0 != (uint)(capabilities.Value & (WICBitmapDecoderCapabilities.WICBitmapDecoderCapabilityCanDecodeSomeImages | WICBitmapDecoderCapabilities.WICBitmapDecoderCapabilityCanDecodeAllImages))) { yield return(s); } } } finally { stream.ReleaseComObject(); decoder.ReleaseComObject(); decoder = null; } } } } finally { factory.ReleaseComObject(); info.ReleaseComObject(); decoder.ReleaseComObject(); } }