Esempio n. 1
0
        public override BitmapDataBase ConvertTo(PixelFormat dstFormat)
        {
            if (dstFormat.Format == PixelFormat.Format)
            {
                return(Copy());
            }
            else if (dstFormat.IsIndexed)
            {
                IQuantization quantization = new WuQuantizer();

                if (quantization.StartQuantization(pixels, Convert.ToInt32(Math.Pow(2, dstFormat.BitsPerPixel))))
                {
                    return(new BitmapDataIndexed(Width, Height, dstFormat, quantization.QuantData, quantization.QuantPalette));
                }
                else
                {
                    throw new Exception($"BitmapData: convert to {dstFormat} error. Quantization don't work.");
                }
            }
            else
            {
                var color2data = PixelConverters.GetColorToDataConverter(dstFormat);
                if (color2data != null)
                {
                    return(new BitmapData(Width, Height, dstFormat, color2data(pixels)));
                }
                else
                {
                    throw new Exception($"BitmapData: convert to {dstFormat} error. ColorToDataConverter undefined.");
                }
            }
        }
Esempio n. 2
0
        public override BitmapDataBase ConvertTo(PixelFormat dstFormat)
        {
            if (dstFormat.Format == PixelFormatEnum.Undefined)
            {
                var data2color = PixelConverters.GetDataToColorConverter(PixelFormat);
                if (data2color != null)
                {
                    return(new BitmapDataUndefined(Width, Height, data2color(data)));
                }
                else
                {
                    throw new Exception($"BitmapData: convert to {dstFormat} error. DataToColorConverter undefined.");
                }
            }
            else if (dstFormat.Format == PixelFormat.Format)
            {
                return(Copy());
            }
            else if (dstFormat.IsIndexed)
            {
                var data2color = PixelConverters.GetDataToColorConverter(PixelFormat);
                if (data2color != null)
                {
                    IQuantization quantization = new WuQuantizer();

                    if (quantization.StartQuantization(data2color(data), Convert.ToInt32(Math.Pow(2, dstFormat.BitsPerPixel))))
                    {
                        var data = quantization.QuantData;

                        if (dstFormat.Format == PixelFormatEnum.Indexed4Reverse)
                        {
                            var data2data = PixelConverters.GetDataToDataConverter(PixelFormats.Indexed4, PixelFormats.Indexed4Reverse);
                            data = data2data(data);
                        }

                        return(new BitmapDataIndexed(Width, Height, dstFormat, data, quantization.QuantPalette));
                    }
                    else
                    {
                        throw new Exception($"BitmapData: convert to {dstFormat} error. Quantization don't work.");
                    }
                }
                else
                {
                    throw new Exception($"BitmapData: convert to {dstFormat} error. DataToColorConverter undefined.");
                }
            }
            else
            {
                var data2data = PixelConverters.GetDataToDataConverter(PixelFormat, dstFormat);
                if (data2data == null)
                {
                    var data2color = PixelConverters.GetDataToColorConverter(PixelFormat);
                    var color2data = PixelConverters.GetColorToDataConverter(dstFormat);
                    if (data2color != null & color2data != null)
                    {
                        return(new BitmapData(Width, Height, dstFormat, color2data(data2color(data))));
                    }
                    else
                    {
                        throw new Exception($"BitmapData: convert to {dstFormat} error. ColorToDataConverter or DataToColorConverter undefined.");
                    }
                }
                else
                {
                    return(new BitmapData(Width, Height, dstFormat, data2data(data)));
                }
            }
        }