示例#1
0
        public void Test_Canvas_ReduceResolution()
        {
            Canvas canvas = new Canvas(new Resolution(8, 5));
            #region Filling canvas
            for (int y = 0; y < 4; y++)
                for (int x = 0; x < 6; x++)
                {
                     if((x == 4 && y == 0) ||
                        (x == 5 && y == 0) ||
                        (x == 5 && y == 1))
                        canvas.SetColor(x, y, Color.Green);
                    else if ((x == 0 && y == 2) ||
                            (x == 1 && y == 2) ||
                            (x == 2 && y == 2) ||
                            (x == 3 && y == 2) ||
                            (x == 2 && y == 3))
                        canvas.SetColor(x, y, Color.Blue);
                    else canvas.SetColor(x, y, Color.Red);
                }
            #endregion

            Canvas expectedCanvas = new Canvas(new Resolution(3, 2));
            #region Filling expected canvas
            for(int y = 0; y < 2; y++)
                for (int x = 0; x < 3; x++)
                {
                    if (x == 2 && y == 0)
                        expectedCanvas.SetColor(x, y, Color.FromArgb(255, 63, 96, 0));
                    else if (x == 1 && y == 1)
                        expectedCanvas.SetColor(x, y, Color.FromArgb(255, 63, 0, 191));
                    else if (x == 0 && y == 1)
                        expectedCanvas.SetColor(x, y, Color.FromArgb(255, 127, 0, 127));
                    else
                        expectedCanvas.SetColor(x, y, Color.FromArgb(255, 255, 0, 0));
                }
            #endregion

            Canvas actualCanvas = canvas.ReduceResolution(3, 2, 2);

            Assert.IsTrue(actualCanvas.Width == expectedCanvas.Width && actualCanvas.Height == expectedCanvas.Height);

            for (int y = 0; y < actualCanvas.Height; y++)
                for (int x = 0; x < actualCanvas.Width; x++)
                {
                    Color actualColor = actualCanvas.GetColor(x, y);
                    Color expectedColor = expectedCanvas.GetColor(x, y);
                    Assert.IsTrue(actualColor == expectedColor);
                }
        }
        public Canvas Generate(Canvas canvas, Settings settings)
        {
            if (settings.Palette == null || settings.Palette.Count == 0)
                throw new NullReferenceException("Check that Palette isn't null or it has any colors");
            if (settings.CellsCount <= 0)
                throw new WrongInitializedException("Square count has to be initialized and more than zero");

            if (canvas.Width < settings.CellsCount)
                throw new WrongResolutionException("Image's width must be higher or input less cells");

            int cellWidth = canvas.Width / settings.CellsCount;

            if (canvas.Height < cellWidth)
                throw new WrongResolutionException("Image's height must be higher");

            int newHeight = canvas.Height / cellWidth;
            int newWidth = settings.CellsCount;

            Canvas tempCanvas = canvas.ReduceResolution(newWidth, newHeight, cellWidth);

            List<Color> colors = settings.Palette.GetAllColorsList();

            //Parallel.For with Partition
            Parallel.ForEach(Partitioner.Create(0, tempCanvas.Height), rangeHeight =>
                {
                    for (int y = rangeHeight.Item1; y < rangeHeight.Item2; y++)
                        Parallel.ForEach(Partitioner.Create(0, tempCanvas.Width), rangeWidth =>
                            {
                                for (int x = rangeWidth.Item1; x < rangeWidth.Item2; x++)
                                {
                                    Color oldColor = tempCanvas.GetColor(x, y);
                                    Color colorAmoung = ChooseColorAmoung(oldColor, colors);
                                    tempCanvas.SetColor(x, y, colorAmoung);
                                }
                            });
                });

            #region Just Parallel.For
            /*
            Parallel.For(0, tempCanvas.Height, y =>
                {
                    Parallel.For(0, tempCanvas.Width, x =>
                        {
                            Color oldColor = tempCanvas.GetColor(x, y);
                            Color colorAmoung = ChooseColorAmoung(oldColor, colors);
                            tempCanvas.SetColor(x, y, colorAmoung);
                        });

                });
            */
            #endregion

            #region Obsolete realization
            /*for (int y = 0; y < tempCanvas.Height; y++)
                for (int x = 0; x < tempCanvas.Width; x++)
                {
                    Color oldColor = tempCanvas.GetColor(x, y);
                    Color colorAmoung = ChooseColorAmoung(oldColor, colors);
                    tempCanvas.SetColor(x, y, colorAmoung);
                }
            */
            #endregion

            return tempCanvas;
        }