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); }
private void AssertPaletteFormatExists(int paletteFormat) { if (EncodingDefinition.GetPaletteEncoding(paletteFormat) == null) { throw new InvalidOperationException($"The palette format '{paletteFormat}' is not supported by the plugin."); } }
/// <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()); }
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); }
private byte[] EncodePalette(IList <Color> palette, int paletteFormat) { return(EncodingDefinition.GetPaletteEncoding(paletteFormat) .Save(palette, new EncodingSaveContext(new Size(1, palette.Count), TaskCount))); }