Exemplo n.º 1
0
        /// <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);
                }
            }));
        }
Exemplo n.º 2
0
 /// <summary>
 /// Transcodes an image to a non-indexed encoding based on any method or library decided by the plugin.
 /// </summary>
 /// <param name="bitmapInfo">The <see cref="BitmapInfo"/> that holds all image information.</param>
 /// <param name="imageEncoding">The <see cref="EncodingInfo"/> to transcode the image into.</param>
 /// <param name="progress"><see cref="IProgress{ProgressReport}"/> to report progress through.</param>
 /// <returns>Transcoded image.</returns>
 protected abstract Bitmap Transcode(BitmapInfo bitmapInfo, EncodingInfo imageEncoding, IProgress <ProgressReport> progress);
Exemplo n.º 3
0
 /// <summary>
 /// Transcodes an image to an indexed encoding based on any method or library decided by the plugin.
 /// </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 <see cref="EncodingInfo"/> to transcode the palette into.</param>
 /// <param name="progress"><see cref="IProgress{ProgressReport}"/> to report progress through.</param>
 /// <returns>Transcoded image and palette.</returns>
 // TODO: Add possibilities of giving quantization information.
 protected abstract (Bitmap Image, IList <Color> Palette) Transcode(BitmapInfo bitmapInfo, EncodingInfo imageEncoding, EncodingInfo paletteEncoding, IProgress <ProgressReport> progress);
Exemplo n.º 4
0
        /// <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);
        }