Esempio n. 1
0
        public WicEncoder(IStream stm, WicProcessingContext ctx)
        {
            var frame = (IWICBitmapFrameEncode)null;

            if (ctx.Settings.SaveFormat == FileFormat.Jpeg)
            {
                Encoder = AddRef(Wic.CreateEncoder(Consts.GUID_ContainerFormatJpeg, null));
                Encoder.Initialize(stm, WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache);

                var bag = (IPropertyBag2)null;
                Encoder.CreateNewFrame(out frame, ref bag);
                AddRef(frame);
                AddRef(bag);

                if (ctx.Settings.JpegSubsampleMode != ChromaSubsampleMode.Default)
                {
                    var props = new PROPBAG2[] { new PROPBAG2 {
                                                     pstrName = "ImageQuality"
                                                 }, new PROPBAG2 {
                                                     pstrName = "JpegYCrCbSubsampling"
                                                 } };
                    bag.Write(2, props, new object[] { ctx.Settings.JpegQuality / 100f, (byte)ctx.Settings.JpegSubsampleMode });
                }
                else
                {
                    var props = new PROPBAG2[] { new PROPBAG2 {
                                                     pstrName = "ImageQuality"
                                                 } };
                    bag.Write(1, props, new object[] { ctx.Settings.JpegQuality / 100f });
                }

                frame.Initialize(bag);
            }
            else if (ctx.Settings.SaveFormat == FileFormat.Gif)
            {
                Encoder = AddRef(Wic.CreateEncoder(Consts.GUID_ContainerFormatGif, null));
                Encoder.Initialize(stm, WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache);
                Encoder.SetPalette(ctx.DestPalette);

                Encoder.CreateNewFrame(out frame, null);
                AddRef(frame);

                frame.Initialize(null);
            }
            else if (ctx.Settings.SaveFormat == FileFormat.Bmp)
            {
                Encoder = AddRef(Wic.CreateEncoder(Consts.GUID_ContainerFormatBmp, null));
                Encoder.Initialize(stm, WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache);

                var bag = (IPropertyBag2)null;
                Encoder.CreateNewFrame(out frame, ref bag);
                AddRef(frame);
                AddRef(bag);

                var props = new PROPBAG2[] { new PROPBAG2 {
                                                 pstrName = "EnableV5Header32bppBGRA"
                                             } };
                bag.Write(1, props, new object[] { ctx.PixelFormat == Consts.GUID_WICPixelFormat32bppBGRA });

                frame.Initialize(bag);
            }
            else if (ctx.Settings.SaveFormat == FileFormat.Tiff)
            {
                Encoder = AddRef(Wic.CreateEncoder(Consts.GUID_ContainerFormatTiff, null));
                Encoder.Initialize(stm, WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache);

                var bag = (IPropertyBag2)null;
                Encoder.CreateNewFrame(out frame, ref bag);
                AddRef(frame);
                AddRef(bag);

                var props = new PROPBAG2[] { new PROPBAG2 {
                                                 pstrName = "TiffCompressionMethod"
                                             } };
                bag.Write(1, props, new object[] { (byte)WICTiffCompressionOption.WICTiffCompressionNone });

                frame.Initialize(bag);
            }
            else
            {
                Encoder = AddRef(Wic.CreateEncoder(Consts.GUID_ContainerFormatPng, null));
                Encoder.Initialize(stm, WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache);

                Encoder.CreateNewFrame(out frame, null);
                AddRef(frame);

                frame.Initialize(null);
            }

            frame.SetResolution(96d, 96d);
            frame.SetSize(ctx.Width, ctx.Height);

            if (ctx.Settings.IndexedColor && ctx.PixelFormat == Consts.GUID_WICPixelFormat8bppIndexed)
            {
                frame.SetPalette(ctx.DestPalette);
            }

            if (ctx.Metadata?.Count > 0)
            {
                var metawriter = frame.GetMetadataQueryWriterNoThrow();
                if (metawriter != null)
                {
                    AddRef(metawriter);
                    foreach (var nv in ctx.Metadata)
                    {
                        metawriter.SetMetadataByNameNoThrow(nv.Key, nv.Value);
                    }
                }
            }

            // TODO setting
            //if (ctx.DestColorContext != null)
            //	frame.SetColorContexts(1, new IWICColorContext[] { ctx.DestColorContext });

            Frame = frame;
        }