// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageProcessorCore.Quantizers { using System; using System.Threading.Tasks; /// /// Represents a quantized image where the pixels indexed by a color palette. /// /// The pixel format. /// The packed format. uint, long, float. public class QuantizedImage where TColor : IPackedVector where TPacked : struct { /// /// Initializes a new instance of the class. /// /// The image width. /// The image height. /// The color palette. /// The quantized pixels. public QuantizedImage(int width, int height, TColor[] palette, byte[] pixels) { Guard.MustBeGreaterThan(width, 0, nameof(width)); Guard.MustBeGreaterThan(height, 0, nameof(height)); Guard.NotNull(palette, nameof(palette)); Guard.NotNull(pixels, nameof(pixels)); if (pixels.Length != width * height) { throw new ArgumentException( $"Pixel array size must be {nameof(width)} * {nameof(height)}", nameof(pixels)); } this.Width = width; this.Height = height; this.Palette = palette; this.Pixels = pixels; } /// /// Gets the width of this . /// public int Width { get; } /// /// Gets the height of this . /// public int Height { get; } /// /// Gets the color palette of this . /// public TColor[] Palette { get; } /// /// Gets the pixels of this . /// public byte[] Pixels { get; } /// /// Converts this quantized image to a normal image. /// /// /// The /// public Image ToImage() { Image image = new Image(); int pixelCount = this.Pixels.Length; int palletCount = this.Palette.Length - 1; TColor[] pixels = new TColor[pixelCount]; Parallel.For( 0, pixelCount, Bootstrapper.Instance.ParallelOptions, i => { TColor color = this.Palette[Math.Min(palletCount, this.Pixels[i])]; pixels[i] = color; }); image.SetPixels(this.Width, this.Height, pixels); return image; } } }