protected override bool ProcessFrameDecode(MainForm form, IWICBitmapFrameDecode frame, DataEntry[] de, object tag) { CheckGetColorContexts(form, de, frame.GetColorContexts); CheckGetBitmapSource(form, de, frame.GetThumbnail, WinCodecError.WINCODEC_ERR_CODECNOTHUMBNAIL); uint width; uint height; frame.GetSize(out width, out height); Guid pixelFormat; frame.GetPixelFormat(out pixelFormat); uint stride = (PixelFormatInfoRule.GetBitPerPixel(pixelFormat) * width + 7) / 8; byte[] buffer = new byte[stride * height]; WICRect rect = new WICRect(); rect.Height = (int)Math.Min(height, int.MaxValue); rect.Width = (int)Math.Min(width, int.MaxValue); try { frame.CopyPixels(rect, stride, stride * height, buffer); try { frame.CopyPixels(null, stride, stride * height, buffer); } catch (Exception e) { form.Add(this, e.TargetSite.ToString(Resources._0_Failed, "NULL"), de, new DataEntry(e)); } } catch { } return(true); }
protected override bool ProcessFrameDecode(MainForm form, IWICBitmapFrameDecode frame, DataEntry[] de, object tag) { IWICBitmapSourceTransform transform = null; try { transform = (IWICBitmapSourceTransform)frame; } catch (InvalidCastException) { return(false); } if (transform == null) { form.Add(this, string.Format(CultureInfo.CurrentUICulture, Resources._0_NULL, "IWICBitmapFrameDecode::QueryInterface(IID_IWICBitmapSourceTransform, ...)"), de); } else { try { if (!transform.DoesSupportTransform(WICBitmapTransformOptions.WICBitmapTransformRotate0)) { form.Add(this, string.Format(CultureInfo.CurrentUICulture, Resources._0_NotExpectedValue, "IWICBitmapSourceTransform::DoesSupportTransform(WICBitmapTransformRotate0, ...)"), de, new DataEntry(Resources.Actual, "FALSE"), new DataEntry(Resources.Expected, "TRUE")); } uint width; uint height; frame.GetSize(out width, out height); uint widthSaved = width; uint heightSaved = height; transform.GetClosestSize(ref width, ref height); if (width != widthSaved || height != heightSaved) { form.Add(this, Resources.IWICBitmapSourceTransform_ChangedSize, de, new DataEntry(Resources.Expected, new Size((int)widthSaved, (int)heightSaved)), new DataEntry(Resources.Actual, new Size((int)width, (int)height))); } Guid pixelFormat; frame.GetPixelFormat(out pixelFormat); Guid pixelFormatSaved = pixelFormat; transform.GetClosestPixelFormat(ref pixelFormat); if (pixelFormat != pixelFormatSaved) { form.Add(this, Resources.IWICBitmapSourceTransform_ChangedPixelformat, de, new DataEntry(Resources.Expected, pixelFormatSaved), new DataEntry(Resources.Actual, pixelFormat)); } uint stride = (PixelFormatInfoRule.GetBitPerPixel(pixelFormat) * width + 7) / 8; byte[] buffer = new byte[stride * height]; try { transform.CopyPixels(null, width, height, null, WICBitmapTransformOptions.WICBitmapTransformRotate0, stride, (uint)buffer.LongLength, buffer); } catch (Exception e) { form.Add(this, e.TargetSite.ToString(Resources._0_Failed, "NULL, uiWidth, uiHeight, NULL, WICBitmapTransformRotate0, ..."), de, new DataEntry(e), new DataEntry("uiWidth", width), new DataEntry("uiHeight", height)); } } finally { transform.ReleaseComObject(); } } return(true); }
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)); }