Beispiel #1
0
        private static unsafe void EncodeImage(PixelBuffer image, WICFlags flags, IWICBitmapFrameEncode frame)
        {
            Guid pfGuid;

            if (!ToWIC(image.Format, out pfGuid))
            {
                throw new NotSupportedException("Format not supported");
            }

            frame.Initialize();
            frame.SetSize(image.Width, image.Height);
            frame.SetResolution(72, 72);
            var targetGuid = pfGuid;

            frame.SetPixelFormat(ref targetGuid);

            if (targetGuid != pfGuid)
            {
                using (var source = Factory.CreateBitmapFromMemory(image.Width, image.Height, pfGuid, image.RowStride, image.BufferStride, image.DataPointer.ToPointer()))
                {
                    using (var converter = Factory.CreateFormatConverter())
                    {
                        using (var palette = Factory.CreatePalette())
                        {
                            palette.InitializeFromBitmap(source, 256, true);
                            converter.Initialize(source, targetGuid, GetWICDither(flags), palette, 0, BitmapPaletteType.Custom);

                            var bpp = GetBitsPerPixel(targetGuid);
                            if (bpp == 0)
                            {
                                throw new NotSupportedException("Unable to determine the Bpp for the target format");
                            }

                            var rowPitch   = (image.Width * bpp + 7) / 8;
                            var slicePitch = rowPitch * image.Height;

                            var temp = SdxUtilities.AllocateMemory(slicePitch);
                            try
                            {
                                converter.CopyPixels(rowPitch, slicePitch, temp);
                                frame.SetPalette(palette);
                                frame.WritePixels(image.Height, temp, rowPitch, slicePitch);
                            }
                            finally
                            {
                                SdxUtilities.FreeMemory(temp);
                            }
                        }
                    }
                }
            }
            else
            {
                // No conversion required
                frame.WritePixels(image.Height, image.DataPointer, image.RowStride, image.BufferStride);
            }

            frame.Commit();
        }
Beispiel #2
0
        public static void SetPalette(this IWICBitmapFrameEncode frame, IWICPalette palette)
        {
            if (frame == null)
            {
                throw new ArgumentNullException(nameof(frame));
            }

            frame.SetPalette(palette).ThrowOnError();
        }
        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));
        }