Exemplo n.º 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()];
        }