Exemplo n.º 1
0
        private (IList <byte[]> imageData, byte[] paletteData) EncodeImage(Bitmap image, int imageFormat, int paletteFormat = -1,
                                                                           IProgressContext progress = null)
        {
            // Create transcoder
            IImageTranscoder transcoder;

            if (IsIndexEncoding(imageFormat))
            {
                var indexEncoding = _encodingDefinition.GetIndexEncoding(imageFormat).IndexEncoding;
                transcoder = ImageConfiguration.Clone()
                             .ConfigureQuantization(options => options.WithColorCount(indexEncoding.MaxColors))
                             .TranscodeWith(size => indexEncoding)
                             .TranscodePaletteWith(() => _encodingDefinition.GetPaletteEncoding(paletteFormat))
                             .Build();
            }
            else
            {
                transcoder = ImageConfiguration.Clone()
                             .TranscodeWith(size => _encodingDefinition.GetColorEncoding(imageFormat))
                             .Build();
            }

            byte[] mainImageData   = null;
            byte[] mainPaletteData = null;
            ExecuteActionWithProgress(() => (mainImageData, mainPaletteData) = transcoder.Encode(image, progress), progress);

            var imageData = new byte[ImageInfo.MipMapCount + 1][];

            imageData[0] = mainImageData;

            // Decode palette if present, only when mip maps are needed
            IList <Color> decodedPalette = null;

            if (mainPaletteData != null && ImageInfo.MipMapCount > 0)
            {
                decodedPalette = DecodePalette(mainPaletteData);
            }

            // Encode mip maps
            var(width, height) = (image.Width / 2, image.Height / 2);
            for (var i = 0; i < ImageInfo.MipMapCount; i++)
            {
                imageData[i + 1] = EncodeMipMap(ResizeImage(image, width, height), imageFormat, decodedPalette);

                width  /= 2;
                height /= 2;
            }

            return(imageData, mainPaletteData);
        }
Exemplo n.º 2
0
        // TODO: Use progress
        private byte[] EncodeMipMap(Bitmap mipMap, int imageFormat, IList <Color> palette = null)
        {
            IImageTranscoder transcoder;

            if (IsIndexEncoding(imageFormat))
            {
                var indexEncoding = _encodingDefinition.GetIndexEncoding(imageFormat).IndexEncoding;
                transcoder = ImageConfiguration.Clone()
                             .ConfigureQuantization(options => options.WithColorCount(indexEncoding.MaxColors).WithPalette(() => palette))
                             .TranscodeWith(size => indexEncoding)
                             .Build();
            }
            else
            {
                transcoder = ImageConfiguration.Clone()
                             .TranscodeWith(size => _encodingDefinition.GetColorEncoding(imageFormat))
                             .Build();
            }

            return(transcoder.Encode(mipMap).imageData);
        }
Exemplo n.º 3
0
        private Bitmap DecodeImage(IProgressContext progress = null)
        {
            if (_decodedImage != null)
            {
                return(_decodedImage);
            }

            Func <Bitmap> decodeImageAction;

            if (IsIndexed)
            {
                var transcoder = ImageConfiguration.Clone()
                                 .TranscodeWith(size => _encodingDefinition.GetIndexEncoding(ImageFormat).IndexEncoding)
                                 .TranscodePaletteWith(() => _encodingDefinition.GetPaletteEncoding(PaletteFormat))
                                 .Build();

                decodeImageAction = () => transcoder.Decode(ImageInfo.ImageData, ImageInfo.PaletteData, ImageInfo.ImageSize, progress);
            }
            else
            {
                var transcoder = ImageConfiguration.Clone()
                                 .TranscodeWith(size => _encodingDefinition.GetColorEncoding(ImageFormat))
                                 .Build();

                decodeImageAction = () => transcoder.Decode(ImageInfo.ImageData, ImageInfo.ImageSize, progress);
            }

            ExecuteActionWithProgress(() => _decodedImage = decodeImageAction(), progress);

            if (_bestImage == null)
            {
                _bestImage = _decodedImage;
            }

            return(_decodedImage);
        }