コード例 #1
0
        public void Test_PatternCreator_GetEmbroidery()
        {
            var mockCanvasConverter = new Mock<ICanvasConverter>();
            var mockPatternMapGenerator = new Mock<IPatternMapGenerator>();
            var mockDecoratorCompositions = new Mock<IDecoratorsComposition>();

            mockPatternMapGenerator.Setup(map => map.Generate(It.IsAny<Canvas>(), It.IsAny<Settings>())).Returns(new Canvas(3, 3));
            mockCanvasConverter.Setup(conv => conv.ConvertBitmapToCanvas(It.IsAny<Bitmap>())).Returns(new Canvas(4, 3));
            mockDecoratorCompositions.Setup(decor => decor.Decorate(It.IsAny<Canvas>(), It.IsAny<Canvas>(), It.IsAny<Settings>()));

            Settings settings = new Settings();
            settings.CellsCount = 3;
            settings.Coefficient = 2;
            settings.Palette = new Palette(new Color[]{Color.Red, Color.Blue});
            settings.DecoratorsComposition = mockDecoratorCompositions.Object;

            EmbroideryCreator creator = new EmbroideryCreator();
            creator.CanvasConverter = mockCanvasConverter.Object;
            creator.PatternMapGenerator = mockPatternMapGenerator.Object;

            Bitmap image = new Bitmap(4, 3);

            creator.GetEmbroidery(image, settings);

            mockCanvasConverter.Verify(conv => conv.ConvertBitmapToCanvas(image));
            mockPatternMapGenerator.Verify(pat => pat.Generate(It.IsAny<Canvas>(), settings));
            mockDecoratorCompositions.Verify(decor => decor.Decorate(It.IsAny<Canvas>(), It.IsAny<Canvas>(), settings));
            mockCanvasConverter.Verify(conv => conv.ConvertCanvasToBitmap(It.IsAny<Canvas>()));
        }
コード例 #2
0
        public static Bitmap CreateEmbroidery(Bitmap image, int resolutionCoefficient, int cellsCount, Color[] palette, char[] symbols, Color symbolColor, GridType type)
        {
            IKernel kernel = new StandardKernel(new PropertiesModel());

            var patternMapGenerator = kernel.Get<IPatternMapGenerator>();
            var canvasConverter = kernel.Get<ICanvasConverter>();

            var patternCreator = new EmbroideryCreator()
            {
                PatternMapGenerator = patternMapGenerator,
                CanvasConverter = canvasConverter,
            };

            Settings settings = new Settings()
            {
                CellsCount = cellsCount,
                Coefficient = resolutionCoefficient,
                Palette = new Palette(palette),
                Symbols = symbols,
                SymbolColor = symbolColor,
                GridType = type,
                DecoratorsComposition = new DecoratorsComposition()
            };

            if (settings.Palette != null)
                settings.DecoratorsComposition.AddDecorator(new CellsDecorator());
            if (settings.Symbols != null)
                settings.DecoratorsComposition.AddDecorator(new SymbolsDecorator());
            if (settings.GridType != Core.GridType.None)
                settings.DecoratorsComposition.AddDecorator(new GridDecorator());

            Bitmap result = patternCreator.GetEmbroidery(image, settings);

            return result;
        }
コード例 #3
0
        public void Test_Settings_CreateColorSymbolRelationException3()
        {
            Settings settings = new Settings();
            settings.Palette = new Palette(new Color[]{Color.Red});

            settings.CreateColorSymbolRelation();
        }
コード例 #4
0
        public void Test_Settings_CreateColorSymbolRelationException2()
        {
            Settings settings = new Settings();
            settings.Palette = new Palette();

            settings.CreateColorSymbolRelation();
        }
コード例 #5
0
        public void Test_Settings_CreateColorSymbolRelationException4()
        {
            Settings settings = new Settings();
            settings.Palette = new Palette(new Color[] { Color.Red, Color.Black });
            settings.Symbols = new char[] { '1' };

            settings.CreateColorSymbolRelation();
        }
コード例 #6
0
        public void Test_PatternCreator_GetEmbroideryException1()
        {
            Settings settings = new Settings();

            EmbroideryCreator creator = new EmbroideryCreator();

            Bitmap image = new Bitmap(4, 2);
            creator.GetEmbroidery(image, settings);
        }
コード例 #7
0
        public void Test_CellsDecorator()
        {
            Settings settings = new Settings();
            settings.CellsCount = 3;
            settings.Coefficient = 2;
            settings.Palette = new Palette(new Color[] { Color.Red, Color.Green, Color.Blue });

            Canvas pattern = new Canvas(new Resolution(3, 2));
            #region FillingPattern
             for (int y = 0; y < 2; y++)
                for (int x = 0; x < 3; x++)
                {
                    if (x == 2 && y == 0)
                        pattern.SetColor(x, y, Color.Green);
                    else if (x == 1 && y == 1)
                       pattern.SetColor(x, y, Color.Blue);
                    else
                        pattern.SetColor(x, y, Color.Red);
                }
            #endregion

            Canvas expectedCanvas = new Canvas(new Resolution(6, 4));
            #region FillingFillingExpectedCanvas
            for (int y = 0; y < 4; y++)
                for (int x = 0; x < 6; x++)
                {
                    if ((x == 4 && y == 0) ||
                        (x == 5 && y == 0) ||
                        (x == 4 && y == 1) ||
                        (x == 5 && y == 1)
                        )
                        expectedCanvas.SetColor(x, y, Color.Green);
                    else if ((x == 2 && y == 2) ||
                            (x == 3 && y == 2) ||
                            (x == 2 && y == 3) ||
                            (x == 3 && y == 3)
                            )
                        expectedCanvas.SetColor(x, y, Color.Blue);
                    else
                        expectedCanvas.SetColor(x, y, Color.Red);
                }
            #endregion

            Canvas actual = new Canvas(new Resolution(6, 4));

            CellsDecorator decorator = new CellsDecorator();
            decorator.Decorate(actual, pattern, settings);

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

            for (int y = 0; y < expectedCanvas.Height; y++)
                for (int x = 0; x < expectedCanvas.Width; x++)
                    Assert.IsTrue(actual.GetColor(x, y) == expectedCanvas.GetColor(x, y));
        }
コード例 #8
0
        public void Test_Settings_Decorate()
        {
            Settings settings = new Settings();

            var mock = new Mock<IDecoratorsComposition>();
            mock.Setup(dec => dec.Decorate(It.IsAny<Canvas>(), It.IsAny<Canvas>(), settings));

            settings.DecoratorsComposition = mock.Object;
            settings.Decorate(new Canvas(1, 1), new Canvas(2, 2));

            mock.Verify(d => d.Decorate(It.IsAny<Canvas>(), It.IsAny<Canvas>(), settings));
        }
コード例 #9
0
        public void Test_PatternCretor_GetEmbroideryException3()
        {
            Settings settings = new Settings()
            {
                CellsCount = 1,
                Coefficient = 1,
            };

            EmbroideryCreator creator = new EmbroideryCreator();

            Bitmap image = new Bitmap(5, 3);
            creator.GetEmbroidery(image, settings);
        }
コード例 #10
0
        public void Test_Settings_CreateColorSymbolRelation()
        {
            Settings settings = new Settings();
            settings.Palette = new Palette(new Color[] { Color.Red, Color.Green });
            settings.Symbols = new char[] { '1', '2', '3', '4' };

            settings.CreateColorSymbolRelation();

            Dictionary<Color, char> expected = new Dictionary<Color, char>();
            expected.Add(Color.Red, '1');
            expected.Add(Color.Green, '2');

            Assert.IsTrue(expected.Count == settings.ColorSymbolRelation.Count);

            foreach (var key in expected.Keys)
                Assert.AreEqual(expected[key], settings.ColorSymbolRelation[key]);
        }
コード例 #11
0
        public Bitmap GetEmbroidery(Bitmap image, Settings settings)
        {
            if (settings.CellsCount <= 0)
                throw new WrongInitializedException("cellsCount has to be > 0");
            if (settings.Coefficient <= 0)
                throw new WrongInitializedException("coefficient has to be > 0");
            if (settings.Palette == null || settings.Palette.Count == 0)
                throw new WrongInitializedException("palette has to be initialized");

            Canvas imageCanvas = CanvasConverter.ConvertBitmapToCanvas(image);

            log.WriteEntry("---------Time spent----------");
            log.WriteEntry(@"-----Convert Bitmap To Canvas: " + Environment.NewLine +
                            "---------resol: " + image.Width.ToString() + "x" + image.Height.ToString());

            Canvas pattern = PatternMapGenerator.Generate(imageCanvas, settings);
            log.WriteEntry(@"-----Generate pattern: "+ Environment.NewLine +
                            "--------resol: " + imageCanvas.Width.ToString() + "x" + imageCanvas.Height.ToString() + Environment.NewLine +
                            "--------cells: " + settings.CellsCount.ToString());

            Resolution resolution = new Resolution(pattern.Width * settings.Coefficient, pattern.Height * settings.Coefficient);

            Canvas result = new Canvas(resolution);

            settings.Decorate(result, pattern);

            Bitmap resultImage = CanvasConverter.ConvertCanvasToBitmap(result);

            log.WriteEntry(@"-----Convert Canvas To Bitmap: " + Environment.NewLine +
                            "---------resol: " + result.Width.ToString() + "x" + result.Height.ToString());
            log.WriteEntry("---------End time spent-------");

            return resultImage;
        }
コード例 #12
0
        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;
        }
コード例 #13
0
        public void Test_PatternMapGenerator_GenerateException5()
        {
            PatternMapGenerator generator = new PatternMapGenerator();

            Settings settings = new Settings()
            {
                Palette = new Palette(new Color[] { Color.Red }),
                CellsCount = 2
            };

            generator.Generate(new Canvas(4, 1), settings);
        }
コード例 #14
0
        public void Test_PatternMapGenerator_GenerateException2()
        {
            PatternMapGenerator generator = new PatternMapGenerator();

            Settings settings = new Settings()
            {
                Palette = new Palette()
            };

            generator.Generate(new Canvas(1, 1), settings);
        }