示例#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);
        }
示例#2
0
 private void AssertPaletteFormatExists(int paletteFormat)
 {
     if (EncodingDefinition.GetPaletteEncoding(paletteFormat) == null)
     {
         throw new InvalidOperationException($"The palette format '{paletteFormat}' is not supported by the plugin.");
     }
 }
示例#3
0
        /// <summary>
        /// Decode given palette data without buffering.
        /// </summary>
        /// <param name="paletteData">Palette data to decode.</param>
        /// <param name="context"></param>
        /// <returns>Decoded palette.</returns>
        private IList <Color> DecodePalette(byte[] paletteData, IProgressContext context = null)
        {
            var paletteEncoding = EncodingDefinition.GetPaletteEncoding(PaletteFormat);

            return(paletteEncoding
                   .Load(paletteData, new EncodingLoadContext(new Size(1, paletteData.Length * 8 / paletteEncoding.BitsPerValue), TaskCount))
                   .ToArray());
        }
示例#4
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);
        }
示例#5
0
 private byte[] EncodePalette(IList <Color> palette, int paletteFormat)
 {
     return(EncodingDefinition.GetPaletteEncoding(paletteFormat)
            .Save(palette, new EncodingSaveContext(new Size(1, palette.Count), TaskCount)));
 }