/// <summary> /// Transcodes an image using <see cref="Transcode(BitmapInfo,EncodingInfo,EncodingInfo,IProgress{ProgressReport})"/>. /// </summary> /// <param name="bitmapInfo">The <see cref="BitmapInfo"/> that holds all image information.</param> /// <param name="imageEncoding">The indexed <see cref="EncodingInfo"/> to transcode the image into.</param> /// <param name="paletteEncoding">The non-indexed <see cref="EncodingInfo"/> to transcode the palette into.</param> /// <param name="progress"><see cref="IProgress{ProgressReport}"/> to report progress through.</param> /// <returns><see cref="ImageTranscodeResult"/> which holds the transcoded information or the exception of this process.</returns> /// <remarks>Implements necessary null checks, exception catching and task handling.</remarks> // TODO: Add possibilities of giving quantization information. public virtual Task <ImageTranscodeResult> TranscodeImage(BitmapInfo bitmapInfo, EncodingInfo imageEncoding, EncodingInfo paletteEncoding, IProgress <ProgressReport> progress) { // Validity checks if (bitmapInfo == null) { throw new ArgumentNullException(nameof(bitmapInfo)); } if (imageEncoding == null) { throw new ArgumentNullException(nameof(imageEncoding)); } if (paletteEncoding == null) { throw new ArgumentNullException(nameof(paletteEncoding)); } if (!BitmapInfos.Contains(bitmapInfo)) { throw new ArgumentException(nameof(bitmapInfo)); } if (!ImageEncodingInfos.Contains(imageEncoding)) { throw new ArgumentException(nameof(imageEncoding)); } if (!PaletteEncodingInfos.Contains(paletteEncoding)) { throw new ArgumentException(nameof(paletteEncoding)); } if (!imageEncoding.IsIndexed) { throw new EncodingNotSupported(imageEncoding); } if (paletteEncoding.IsIndexed) { throw new IndexedEncodingNotSupported(paletteEncoding); } //// If encodings unchanged, don't transcode. //if (bitmapInfo.ImageEncoding == imageEncoding) //{ // if (!(bitmapInfo is IndexedBitmapInfo indexInfo)) // return Task.Factory.StartNew(() => new ImageTranscodeResult(bitmapInfo.Image)); // if (indexInfo.PaletteEncoding == paletteEncoding) // return Task.Factory.StartNew(() => new ImageTranscodeResult(bitmapInfo.Image)); //} return(Task.Factory.StartNew(() => { try { var data = Transcode(bitmapInfo, imageEncoding, paletteEncoding, progress); return new ImageTranscodeResult(data.Image, data.Palette); } catch (Exception ex) { return new ImageTranscodeResult(ex); } })); }
public void Load(StreamInfo input, BaseReadOnlyDirectoryNode node) { _format = new BTX(input.FileData); if (_format.HasPalette) { var indexEncodingInfo = ImageEncodingInfos.FirstOrDefault(x => x.EncodingIndex == (int)_format.Header.Format); var paletteEncodingInfo = PaletteEncodingInfos.FirstOrDefault(x => x.EncodingIndex == (int)_format.Header.Format); _bitmapInfos = new List <BitmapInfo> { new IndexedBitmapInfo(_format.Texture, indexEncodingInfo, _format.Palette, paletteEncodingInfo) }; } else { _bitmapInfos = new List <BitmapInfo> { new BitmapInfo(_format.Texture, ImageEncodingInfos.First(x => x.EncodingIndex == (int)_format.Header.Format)) } }; }
/// <inheritdoc cref="IIndexedImageAdapter.Commit(BitmapInfo,Bitmap,EncodingInfo,IList{Color},EncodingInfo)"/> public virtual bool Commit(BitmapInfo bitmapInfo, Bitmap image, EncodingInfo imageEncoding, IList <Color> palette, EncodingInfo paletteEncoding) { // Validity checks if (bitmapInfo == null) { throw new ArgumentNullException(nameof(bitmapInfo)); } if (image == null) { throw new ArgumentNullException(nameof(image)); } if (imageEncoding == null) { throw new ArgumentNullException(nameof(imageEncoding)); } if (!BitmapInfos.Contains(bitmapInfo)) { throw new ArgumentException(nameof(bitmapInfo)); } if (!ImageEncodingInfos.Contains(imageEncoding)) { throw new ArgumentException(nameof(imageEncoding)); } if (!imageEncoding.IsIndexed) { throw new EncodingNotSupported(imageEncoding); } if (imageEncoding.IsIndexed) { if (palette == null) { throw new ArgumentNullException(nameof(palette)); } if (paletteEncoding == null) { throw new ArgumentNullException(nameof(paletteEncoding)); } if (!PaletteEncodingInfos.Contains(paletteEncoding)) { throw new ArgumentException(nameof(paletteEncoding)); } if (paletteEncoding.IsIndexed) { throw new IndexedEncodingNotSupported(paletteEncoding); } } // If format changed from indexed to non-indexed or vice versa. if (bitmapInfo.ImageEncoding.IsIndexed != imageEncoding.IsIndexed) { BitmapInfos[BitmapInfos.IndexOf(bitmapInfo)] = imageEncoding.IsIndexed ? new IndexedBitmapInfo(image, imageEncoding, palette, paletteEncoding) : new BitmapInfo(image, imageEncoding); } else // If format changed without having its "type" changed. { bitmapInfo.Image = image; bitmapInfo.ImageEncoding = imageEncoding; if (bitmapInfo is IndexedBitmapInfo indexInfo) { indexInfo.Palette = palette; indexInfo.PaletteEncoding = paletteEncoding; } } return(true); }