Exemple #1
0
        /// <inheritdoc cref="IIndexedImageAdapter.SetIndexInImage(IndexedBitmapInfo,Point,int,IProgress{ProgressReport})"/>
        public virtual Task <ImageTranscodeResult> SetIndexInImage(IndexedBitmapInfo indexInfo, Point pointInImg, int newIndex, IProgress <ProgressReport> progress)
        {
            // Validity checks
            if (indexInfo == null)
            {
                throw new ArgumentNullException(nameof(indexInfo));
            }
            if (pointInImg.X >= indexInfo.Image.Width || pointInImg.Y >= indexInfo.Image.Height)
            {
                throw new ArgumentOutOfRangeException(nameof(pointInImg));
            }
            if (newIndex < 0 || newIndex >= indexInfo.ColorCount)
            {
                throw new ArgumentOutOfRangeException(nameof(newIndex));
            }
            if (!BitmapInfos.Contains(indexInfo))
            {
                throw new ArgumentException(nameof(indexInfo));
            }

            var newColor = indexInfo.Palette[newIndex];

            return(Task.Factory.StartNew(() =>
            {
                try
                {
                    indexInfo.Image.SetPixel(pointInImg.X, pointInImg.Y, newColor);
                    return new ImageTranscodeResult(indexInfo.Image, indexInfo.Palette);
                }
                catch (Exception ex)
                {
                    return new ImageTranscodeResult(ex);
                }
            }));
        }
Exemple #2
0
        /// <inheritdoc cref="IIndexedImageAdapter.SetPalette(IndexedBitmapInfo,IList{Color},IProgress{ProgressReport})"/>
        public virtual Task <ImageTranscodeResult> SetPalette(IndexedBitmapInfo bitmapInfo, IList <Color> palette, IProgress <ProgressReport> progress)
        {
            // Validity checks
            if (bitmapInfo == null)
            {
                throw new ArgumentNullException(nameof(bitmapInfo));
            }
            if (palette == null)
            {
                throw new ArgumentNullException(nameof(palette));
            }
            if (!BitmapInfos.Contains(bitmapInfo))
            {
                throw new ArgumentException(nameof(bitmapInfo));
            }

            return(Task.Factory.StartNew(() =>
            {
                try
                {
                    var image = TranscodeWithPalette(bitmapInfo, palette, progress);
                    return new ImageTranscodeResult(image, palette);
                }
                catch (Exception ex)
                {
                    return new ImageTranscodeResult(ex);
                }
            }));
        }
Exemple #3
0
        /// <inheritdoc cref="IIndexedImageAdapter.SetColorInPalette(IndexedBitmapInfo,int,Color,IProgress{ProgressReport})"/>
        public virtual Task <ImageTranscodeResult> SetColorInPalette(IndexedBitmapInfo bitmapInfo, int index, Color color, IProgress <ProgressReport> progress)
        {
            // Validity checks
            if (bitmapInfo == null)
            {
                throw new ArgumentNullException(nameof(bitmapInfo));
            }
            if (index < 0 || index >= bitmapInfo.ColorCount)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }
            if (!BitmapInfos.Contains(bitmapInfo))
            {
                throw new ArgumentException(nameof(bitmapInfo));
            }

            var newPalette = new Color[bitmapInfo.Palette.Count];

            bitmapInfo.Palette.CopyTo(newPalette, 0);
            newPalette[index] = color;

            return(Task.Factory.StartNew(() =>
            {
                try
                {
                    var image = TranscodeWithPalette(bitmapInfo, newPalette, progress);
                    return new ImageTranscodeResult(image, newPalette);
                }
                catch (Exception ex)
                {
                    return new ImageTranscodeResult(ex);
                }
            }));
        }
Exemple #4
0
        protected override Bitmap TranscodeWithPalette(IndexedBitmapInfo bitmapInfo, IList <Color> palette, IProgress <ProgressReport> progress)
        {
            var img           = bitmapInfo.Image;
            var indexInfo     = bitmapInfo as IndexedBitmapInfo;
            var indexEncoding = BTX.IndexEncodings[bitmapInfo.ImageEncoding.EncodingIndex];

            var colorList    = Kanvas.Kolors.DecomposeImage(img);
            var indices      = indexEncoding.DecomposeWithPalette(colorList, indexInfo?.Palette);
            var newColorList = indexEncoding.Compose(indices, palette).ToList();

            _format.Texture = Kanvas.Kolors.ComposeImage(newColorList, img.Width, img.Height);
            _format.Palette = palette;

            return(_format.Texture);
        }
Exemple #5
0
 /// <summary>
 /// Transcodes an image based on a new palette.
 /// </summary>
 /// <param name="bitmapInfo">The <see cref="IndexedBitmapInfo"/> that holds all image information.</param>
 /// <param name="palette">The new palette to apply.</param>
 /// <param name="progress"><see cref="IProgress{ProgressReport}"/> to report progress through.</param>
 /// <returns>Transcoded image</returns>
 protected abstract Bitmap TranscodeWithPalette(IndexedBitmapInfo bitmapInfo, IList <Color> palette, IProgress <ProgressReport> progress);