Пример #1
0
        public WicPaletizer(WicTransform prev, uint colors = 256u) : base(prev)
        {
            var newFormat = Consts.GUID_WICPixelFormat8bppIndexed;

            if (!Context.Settings.IndexedColor || Context.PixelFormat.FormatGuid == newFormat || Context.IsGreyscale)
            {
                return;
            }

            var conv = AddRef(Wic.CreateFormatConverter());

            if (!conv.CanConvert(Context.PixelFormat.FormatGuid, newFormat))
            {
                throw new NotSupportedException("Can't convert to destination pixel format");
            }

            var bmp = AddRef(Wic.CreateBitmapFromSource(Source, WICBitmapCreateCacheOption.WICBitmapCacheOnDemand));

            var pal = AddRef(Wic.CreatePalette());

            pal.InitializeFromBitmap(bmp, colors, false);
            Context.DestPalette = pal;

            conv.Initialize(bmp, newFormat, WICBitmapDitherType.WICBitmapDitherTypeErrorDiffusion, pal, 10.0, WICBitmapPaletteType.WICBitmapPaletteTypeCustom);
            Source = conv;
            Context.PixelFormat = PixelFormat.Cache[Source.GetPixelFormat()];
        }
Пример #2
0
        public WicPixelFormatConverter(WicTransform prev) : base(prev)
        {
            var newFormat = Consts.GUID_WICPixelFormat24bppBGR;

            if (Context.HasAlpha)
            {
                newFormat = Context.PixelFormat.FormatGuid == Consts.GUID_WICPixelFormat32bppPBGRA ? Consts.GUID_WICPixelFormat32bppPBGRA : Consts.GUID_WICPixelFormat32bppBGRA;
            }
            else if (Context.IsGreyscale)
            {
                newFormat = Consts.GUID_WICPixelFormat8bppGray;
            }
            else if (Context.PixelFormat.ColorRepresentation == PixelColorRepresentation.Cmyk)
            {
                newFormat = Consts.GUID_WICPixelFormat32bppCMYK;
            }

            if (Context.PixelFormat.FormatGuid == newFormat)
            {
                return;
            }

            var conv = AddRef(Wic.CreateFormatConverter());

            if (!conv.CanConvert(Context.PixelFormat.FormatGuid, newFormat))
            {
                throw new NotSupportedException("Can't convert to destination pixel format");
            }

            conv.Initialize(Source, newFormat, WICBitmapDitherType.WICBitmapDitherTypeNone, null, 0.0, WICBitmapPaletteType.WICBitmapPaletteTypeCustom);
            Source = conv;
            Context.PixelFormat = PixelFormat.Cache[Source.GetPixelFormat()];
        }
Пример #3
0
        public void WriteSource(WicTransform prev)
        {
            var src = prev.Source;

            var iformat = src.GetPixelFormat();
            var oformat = iformat;

            Frame.SetPixelFormat(ref oformat);
            if (oformat != iformat)
            {
                var pal = default(IWICPalette);
                var ptt = WICBitmapPaletteType.WICBitmapPaletteTypeCustom;
                if (PixelFormat.Cache[oformat].NumericRepresentation == PixelNumericRepresentation.Indexed)
                {
                    pal = AddRef(Wic.CreatePalette());
                    pal.InitializePredefined(WICBitmapPaletteType.WICBitmapPaletteTypeFixedGray256, false);
                    ptt = WICBitmapPaletteType.WICBitmapPaletteTypeFixedGray256;
                }

                var conv = AddRef(Wic.CreateFormatConverter());
                conv.Initialize(src, oformat, WICBitmapDitherType.WICBitmapDitherTypeNone, pal, 0.0, ptt);
                src = conv;
            }

            Frame.WriteSource(src, null);

            Frame.Commit();
            Encoder.Commit();
        }
Пример #4
0
        public WicPlanarConverter(WicTransform prevY, WicTransform prevCbCr) : base(prevY)
        {
            var cfmt  = Consts.GUID_WICPixelFormat24bppBGR;
            var conv  = AddRef(Wic.CreateFormatConverter());
            var pconv = conv as IWICPlanarFormatConverter;

            pconv.Initialize(new IWICBitmapSource[] { prevY.Source, prevCbCr.Source }, 2, cfmt, WICBitmapDitherType.WICBitmapDitherTypeNone, null, 0.0, WICBitmapPaletteType.WICBitmapPaletteTypeCustom);
            Source = pconv;

            if (Context.SourceColorContext != null)
            {
                var trans = AddRef(Wic.CreateColorTransform());
                trans.Initialize(Source, Context.SourceColorContext, Context.DestColorContext, cfmt);
                Source = trans;
            }
        }
Пример #5
0
        public void WriteSource(WicTransform prev)
        {
            var src = prev.Source;

            var iformat = src.GetPixelFormat();
            var oformat = iformat;

            Frame.SetPixelFormat(ref oformat);
            if (oformat != iformat)
            {
                // TODO grey -> indexed support
                //var pal = AddRef(Wic.CreatePalette());
                //pal.InitializePredefined(WICBitmapPaletteType.WICBitmapPaletteTypeFixedGray256, false);
                var conv = AddRef(Wic.CreateFormatConverter());
                conv.Initialize(src, oformat, WICBitmapDitherType.WICBitmapDitherTypeNone, null, 0.0, WICBitmapPaletteType.WICBitmapPaletteTypeCustom);
                src = conv;
            }

            Frame.WriteSource(src, null);

            Frame.Commit();
            Encoder.Commit();
        }