Exemple #1
0
        private Bitmap DecodeImage(IProgressContext progress = null)
        {
            if (_decodedImage != null)
            {
                return(_decodedImage);
            }

            Func <Bitmap> decodeImageAction;

            if (IsIndexed)
            {
                var transcoder = CreateImageConfiguration(ImageFormat, PaletteFormat)
                                 .Transcode.With(_encodingDefinition.GetIndexEncoding(ImageFormat).IndexEncoding)
                                 .TranscodePalette.With(_encodingDefinition.GetPaletteEncoding(PaletteFormat))
                                 .Build();

                decodeImageAction = () => transcoder.Decode(ImageInfo.ImageData, ImageInfo.PaletteData, ImageInfo.ImageSize, progress);
            }
            else
            {
                var transcoder = CreateImageConfiguration(ImageFormat, PaletteFormat)
                                 .Transcode.With(_encodingDefinition.GetColorEncoding(ImageFormat))
                                 .Build();

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

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

            _bestImage ??= _decodedImage;

            return(_decodedImage);
        }
Exemple #2
0
 private void AssertImageFormatExists(int imageFormat)
 {
     if (EncodingDefinition.GetColorEncoding(imageFormat) == null &&
         EncodingDefinition.GetIndexEncoding(imageFormat) == null)
     {
         throw new InvalidOperationException($"The image format '{imageFormat}' is not supported by the plugin.");
     }
 }
Exemple #3
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 = CreateImageConfiguration(ImageFormat, PaletteFormat)
                             .ConfigureQuantization(options => options.WithColorCount(indexEncoding.MaxColors))
                             .Transcode.With(indexEncoding)
                             .TranscodePalette.With(EncodingDefinition.GetPaletteEncoding(paletteFormat))
                             .Build();
            }
            else
            {
                transcoder = CreateImageConfiguration(ImageFormat, PaletteFormat)
                             .Transcode.With(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);
        }
Exemple #4
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 = CreateImageConfiguration(ImageFormat, PaletteFormat)
                             .ConfigureQuantization(options => options.WithColorCount(indexEncoding.MaxColors).WithPalette(() => palette))
                             .Transcode.With(indexEncoding)
                             .Build();
            }
            else
            {
                transcoder = CreateImageConfiguration(ImageFormat, PaletteFormat)
                             .Transcode.With(EncodingDefinition.GetColorEncoding(imageFormat))
                             .Build();
            }

            return(transcoder.Encode(mipMap).imageData);
        }