public void SetPaletteTest()
		{
			Bitmap Bitmap = new Bitmap(2, 2, PixelFormat.Format8bppIndexed);
			var Colors = new Color[] {
				Color.FromArgb(255, 0, 0, 0),
				Color.FromArgb(255, 255, 255, 255),
				Color.FromArgb(255, 255, 0, 0),
				Color.FromArgb(255, 0, 255, 0),
				Color.FromArgb(255, 0, 0, 255),
			};
			Bitmap.SetPalette(Colors);
			Assert.Inconclusive();
		}
		private void InitSprite(Bitmap bmp, int cols, int rows)
		{
			if (bmp.PixelFormat != PixelFormat.Format8bppIndexed)
				throw new Exception("Image must be 8bpp indexed");
			var image = bmp.Bitmap8ToPixels();
			var paleta = bmp.Palette;
			var imgsz = bmp.Size;
			var sz = new Size(imgsz.Width / cols, imgsz.Height / rows);
			var rect = new Rectangle(Point.Empty, sz);
			for (var f = 0; f < rows; f++)
			{
				for (var g = 0; g < cols; g++)
				{
					rect.Location = new Point(g * sz.Width, f * sz.Height);
					var img = new Bitmap(rect.Width, rect.Height, PixelFormat.Format8bppIndexed);
					img.SetPalette(paleta.Entries);
					img.PixelsToBitmap8(rect, image);
					RawSprites.Add(img);
				}
			}
		}
        public void TransformPerPixelAdvanced(PixelFormat targetFormat, List<Color> palette, out  Image targetImage, IList<Point> path = null, Int32 parallelTaskCount = 4, params TransformPixelAdvancedFunction[] passes)
        {
            // checks parameters
            Guard.CheckNull(targetFormat, "targetFormat");

            // creates a target bitmap in an appropriate format
            targetImage = new Bitmap(Width, Height, targetFormat);

            // sets image palette if needed
            if (targetFormat.IsIndexed()) targetImage.SetPalette(palette);

            // wraps target image to a buffer
            using (ImageBuffer target = new ImageBuffer(targetImage, ImageLockMode.WriteOnly))
            {
                TransformPerPixelBase(target, path, parallelTaskCount, passes);
            }
        }
		public static Bitmap ToIndexFormat(this Bitmap image, Color[] palette)
		{
			var sz = image.Size;
			var res = new Bitmap(sz.Width, sz.Height, PixelFormat.Format8bppIndexed);
			res.SetPalette(palette);
			var bmpdataori = image.Bitmap32ToPixels();
			var bmpdatadest = new byte[sz.Height][];
			var dic = new Dictionary<int, int>();
			for (var f = 0; f < sz.Height; f++)
			{
				var pixelsori = bmpdataori[f];
				var pixeldest = ReduceLine(pixelsori, palette,dic);
				bmpdatadest[f] = pixeldest;
				Debug.Print($"Scanline {f} colors {dic.Count}");
			}
			res.PixelsToBitmap8(bmpdatadest);
			return res;
		}
        /// <summary>
        /// Changes the pixel format.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="targetFormat">The target format.</param>
        /// <param name="quantizer">The color quantizer.</param>
        /// <returns>The converted image in a target format.</returns>
        public static Image ChangePixelFormat(this Image image, PixelFormat targetFormat, IColorQuantizer quantizer)
        {
            // checks for image validity
            if (image == null)
            {
                const String message = "Cannot change a pixel format for a null image.";
                throw new ArgumentNullException(message);
            }

            // checks whether a target format is supported
            if (!targetFormat.IsSupported())
            {
                String message = string.Format("A pixel format '{0}' is not supported.", targetFormat);
                throw new NotSupportedException(message);
            }

            // checks whether there is a quantizer for a indexed format
            if (targetFormat.IsIndexed() && quantizer == null)
            {
                String message = string.Format("A quantizer is cannot be null for indexed pixel format '{0}'.", targetFormat);
                throw new NotSupportedException(message);
            }

            // creates an image with the target format
            Bitmap result = new Bitmap(image.Width, image.Height, targetFormat);
            ColorPalette imagePalette = image.Palette;

            // gathers some information about the target format
            Boolean hasSourceAlpha = image.PixelFormat.HasAlpha();
            Boolean hasTargetAlpha = targetFormat.HasAlpha();
            Boolean isSourceIndexed = image.PixelFormat.IsIndexed();
            Boolean isTargetIndexed = targetFormat.IsIndexed();
            Boolean isSourceDeepColor = image.PixelFormat.IsDeepColor();
            Boolean isTargetDeepColor = targetFormat.IsDeepColor();

            // if palette is needed create one first
            if (isTargetIndexed)
            {
                quantizer.Prepare(image);
                image.AddColorsToQuantizer(quantizer);
                Int32 targetColorCount = result.GetPaletteColorCount();
                List<Color> palette = quantizer.GetPalette(targetColorCount);
                result.SetPalette(palette);
            }

            Action<Pixel, Pixel> changeFormat = (sourcePixel, targetPixel) =>
            {
                // if both source and target formats are deep color formats, copies a value directly
                if (isSourceDeepColor && isTargetDeepColor)
                {
                    UInt64 value = sourcePixel.Value;
                    targetPixel.SetValue(value);
                }
                else
                {
                    // retrieves a source image color
                    Color color = isSourceIndexed ? imagePalette.Entries[sourcePixel.Index] : sourcePixel.Color;

                    // if alpha is not present in the source image, but is present in the target, make one up
                    if (!hasSourceAlpha && hasTargetAlpha)
                    {
                        Int32 argb = 255 << 24 | color.R << 16 | color.G << 8 | color.B;
                        color = Color.FromArgb(argb);
                    }

                    // sets the color to a target pixel
                    if (isTargetIndexed)
                    {
                        // for the indexed images, determines a color from the octree
                        Byte paletteIndex = (Byte) quantizer.GetPaletteIndex(color);
                        targetPixel.SetIndex(paletteIndex);
                    }
                    else
                    {
                        // for the non-indexed images, sets the color directly
                        targetPixel.SetColor(color);
                    }
                }
            };

            // process image -> changes format
            image.ProcessImagePixels(result, changeFormat);

            // returns the image in the target format
            return result;
        }