Exemple #1
0
        public void TestSelectedSymbols()
        {
            var image = new CodedImage {
                Size = new Size(5, 5)
            };

            image.CompletePalette();
            image[1, 1] = new CodedColor(100);
            image[2, 2] = new CodedColor(200);
            image.CompletePalette();
            Assert.AreEqual(3, image.Palette.Count, "Precondition");

            var font     = SAEResources.GetAllFonts().First();
            var symboler = new ImageSymbolsController4Test(new ImageSymbolsManipulator(image), new[] { font }, font);

            symboler.IncludeNumbers = false;
            symboler.IncludeLetters = true;
            symboler.IncludeSymbols = false;

            Assert.AreEqual(117, symboler.AvailableSymbols.Count);
            Assert.AreEqual(3, symboler.SelectedSymbols.Count(), "Should select enough symbols for available colors.");
            Assert.AreEqual(3, symboler.SelectedCount);

            symboler.CallManipulationsCoreFired = false;
            symboler.SwitchSelection(symboler.SelectedSymbols.First());

            Assert.AreEqual(2, symboler.SelectedSymbols.Count());
            Assert.AreEqual(2, symboler.SelectedCount);
            Assert.IsTrue(symboler.CallManipulationsCoreFired, "Should call manipulations when selected symbols have changed.");

            symboler.CallManipulationsCoreFired = false;
            symboler.AddRandomSymbols();

            Assert.AreEqual(3, symboler.SelectedSymbols.Count(), "Should select enough symbols for available colors.");
            Assert.AreEqual(3, symboler.SelectedCount);
            Assert.IsTrue(symboler.CallManipulationsCoreFired, "Should call manipulations when selected symbols have changed.");

            symboler.IncludeNumbers = true;

            Assert.AreEqual(3, symboler.SelectedSymbols.Count(), "Should not reset selected symbols after adding more available symbols.");
            Assert.AreEqual(3, symboler.SelectedCount);

            symboler.SwitchSelection('0');

            Assert.AreEqual(4, symboler.SelectedSymbols.Count(), "Should select enough symbols for available colors.");
            Assert.AreEqual(4, symboler.SelectedCount);

            symboler.CallManipulationsCoreFired = false;
            symboler.AddRandomSymbols();

            Assert.AreEqual(4, symboler.SelectedSymbols.Count(), "Should not select extra symbols nor remove existing.");
            Assert.AreEqual(4, symboler.SelectedCount);
            Assert.IsFalse(symboler.CallManipulationsCoreFired, "Should not call manipulations if nothing changed.");
        }
        public override void TestRestoreManipulationsCore()
        {
            var image = new CodedImage {
                Size = new Size(30, 20), Palette = new CodedPalette()
            };

            image.CompletePalette();
            for (int x = 0; x < 30; x++)
            {
                for (int y = 0; y < 20; y++)
                {
                    image[x, y] = new CodedColor((byte)(x * 8), (byte)(y * 12), (byte)((x + y) * 5));
                }
            }

            var dummyManipulator = new ImageManipulatorTest.DummyManipulator(image);
            var colorer          = new ImageColorsManipulator(dummyManipulator);
            var childManipulator = new ImageManipulatorTest.DummyManipulator(colorer);

            colorer.QuantizeColors(3);
            Assert.AreEqual(3, colorer.ManipulatedImage.Palette.Count);

            colorer.ManipulatedImage.Palette.Add(new CodedColor(254, 254, 254));
            childManipulator.RestoreManipulationsCoreFired = false;

            Assert.AreEqual(4, colorer.ManipulatedImage.Palette.Count, "Precondition");
            Assert.IsFalse(childManipulator.RestoreManipulationsCoreFired);

            dummyManipulator.CallOnImageChanged();

            Assert.AreEqual(3, colorer.ManipulatedImage.Palette.Count);
            Assert.IsFalse(childManipulator.RestoreManipulationsCoreFired);
            Assert.NotNull(childManipulator.ManipulatedImage);
            Assert.IsTrue(childManipulator.RestoreManipulationsCoreFired);
        }
Exemple #3
0
        public void TestCallManipulators()
        {
            var image = new CodedImage {
                Size = new Size(50, 50)
            };

            image.CompletePalette();
            for (int x = 0; x < 50; x += 5)
            {
                for (int y = 0; y < 50; y++)
                {
                    image[x, y]     = new CodedColor(0);
                    image[x + 1, y] = new CodedColor(255, 255, 255);
                    image[x + 2, y] = new CodedColor(255, 0, 0);
                    image[x + 3, y] = new CodedColor(127, 0, 0);
                    image[x + 4, y] = new CodedColor(0, 255, 0);
                }
            }

            image.CompletePalette();
            Assert.AreEqual(5, image.Palette.Count, "Precondition: there are 5 different colors in source image.");

            var palette1 = new CodedPalette {
                Name = "Palette 1"
            };

            palette1.Add(new CodedColor(0));
            palette1.Add(new CodedColor(255, 255, 255));
            palette1.Add(new CodedColor(255, 0, 0));

            var palette2 = new CodedPalette {
                Name = "Palette 2"
            };

            palette2.Add(new CodedColor(0));
            palette2.Add(new CodedColor(255, 255, 255));

            var colorer = new ImageColorsController(new ImageColorsManipulator(image), new[] { palette1, palette2 }, palette1);

            colorer.Manipulator.ManipulatedImage.CompletePalette();
            Assert.AreEqual(3, colorer.Manipulator.ManipulatedImage.Palette.Count, "Colors reducing manipulation should be applied.");

            colorer.PaletteName = "Palette 2";
            colorer.Manipulator.ManipulatedImage.CompletePalette();
            Assert.AreEqual(2, colorer.Manipulator.ManipulatedImage.Palette.Count, "Colors reducing manipulation should be applied with new maximum available colors number.");
        }
        public void TestReset()
        {
            var image = new CodedImage {
                Size = new Size(2, 2), Palette = new CodedPalette()
            };

            image.CompletePalette();
            image[0, 0] = new CodedColor(1)
            {
                ColorCode = "100"
            };
            image[0, 1] = new CodedColor(1)
            {
                ColorCode = "100"
            };
            image[1, 0] = new CodedColor(1)
            {
                ColorCode = "100"
            };
            image[1, 1] = new CodedColor(1)
            {
                ColorCode = "100"
            };

            var imageManipulator = new DummyManipulator(image);

            Assert.AreEqual(new Size(2, 2), imageManipulator.ManipulatedImage.Size);
            Assert.AreEqual(new CodedColor(1), imageManipulator.ManipulatedImage[0, 0]);
            Assert.AreEqual("100", imageManipulator.ManipulatedImage[0, 0].ColorCode);

            imageManipulator.ManipulatedImage.Size  = new Size(5, 5);
            imageManipulator.ManipulatedImage[0, 0] = new CodedColor(0)
            {
                ColorCode = "200"
            };

            Assert.AreEqual(new Size(5, 5), imageManipulator.ManipulatedImage.Size);
            Assert.AreEqual(new CodedColor(0), imageManipulator.ManipulatedImage[0, 0]);
            Assert.AreEqual("200", imageManipulator.ManipulatedImage[0, 0].ColorCode);

            imageManipulator.RestoreManipulationsCoreFired = false;

            var childManipulator = new DummyManipulator(imageManipulator);

            Assert.NotNull(childManipulator.ManipulatedImage);

            imageManipulator.Reset();

            Assert.AreEqual(new Size(2, 2), imageManipulator.ManipulatedImage.Size);
            Assert.AreEqual(new CodedColor(1), imageManipulator.ManipulatedImage[0, 0]);
            Assert.AreEqual("100", imageManipulator.ManipulatedImage[0, 0].ColorCode);

            Assert.IsTrue(imageManipulator.ResetFired);
            Assert.IsFalse(imageManipulator.RestoreManipulationsCoreFired);
            Assert.IsFalse(childManipulator.ResetFired);
            Assert.IsTrue(childManipulator.RestoreManipulationsCoreFired);
        }
Exemple #5
0
        public void TestSerialize()
        {
            var image = new CodedImage {
                Size = new Size(2, 3), SourceImageFileName = @"c:\Temp\Test Image.jpg"
            };

            image.CompletePalette();
            image[0, 0] = new CodedColor(1);
            image[0, 1] = new CodedColor(100);
            image[0, 2] = new CodedColor(2);
            image[1, 0] = new CodedColor(2);
            image[1, 1] = new CodedColor(100);
            image[1, 2] = new CodedColor(3);
            Assert.AreEqual(4, image.Palette.Count, "Precondition");

            var tempFileName = Path.GetTempFileName();

            try
            {
                using (var fs = new FileStream(tempFileName, FileMode.Create))
                {
                    image.SaveToStream(fs);
                }

                using (var fs = new FileStream(tempFileName, FileMode.Open))
                {
                    var reloadedImage = ImageSerializer.LoadFromStream(fs);

                    Assert.AreEqual(new Size(2, 3), reloadedImage.Size);
                    Assert.AreEqual(@"c:\Temp\Test Image.jpg", reloadedImage.SourceImageFileName);

                    Assert.IsNotNull(reloadedImage.Palette);
                    Assert.AreEqual(4, reloadedImage.Palette.Count);
                    Assert.IsTrue(reloadedImage.Palette[1] is CodedColor);
                    Assert.IsTrue(reloadedImage.Palette[2] is CodedColor);
                    Assert.IsTrue(reloadedImage.Palette[3] is CodedColor);
                    Assert.IsTrue(reloadedImage.Palette[100] is CodedColor);

                    Assert.AreEqual(new CodedColor(1), reloadedImage[0, 0]);
                    Assert.AreEqual(new CodedColor(100), reloadedImage[0, 1]);
                    Assert.AreEqual(new CodedColor(2), reloadedImage[0, 2]);
                    Assert.AreEqual(new CodedColor(2), reloadedImage[1, 0]);
                    Assert.AreEqual(new CodedColor(100), reloadedImage[1, 1]);
                    Assert.AreEqual(new CodedColor(3), reloadedImage[1, 2]);
                }
            }
            finally
            {
                if (File.Exists(tempFileName))
                {
                    File.Delete(tempFileName);
                }
            }
        }
        /// <summary>
        /// Inintializes data object from image.
        /// </summary>
        /// <param name="image">Source image.</param>
        public ImageDataObject(CodedImage image)
        {
            image.CompletePalette();

            SetData(DataFormatType, image.WriteToString());
            SetData(DataFormats.Bitmap, image.ToBitmap());

            var csv = ToCsv(image);

            SetData(DataFormats.CommaSeparatedValue, csv);
            SetData(DataFormats.Text, csv);
        }
Exemple #7
0
        public override void TestRestoreManipulationsCore()
        {
            var srcImage = new CodedImage {
                Size = new Size(3, 2)
            };

            srcImage.CompletePalette();
            Assert.AreEqual(1, srcImage.Palette.Count, "Precondition - there should be one color for #00000000.");

            var symboler = new ImageSymbolsManipulator(srcImage);

            Assert.AreSame(symboler.SourceImage, symboler.ManipulatedImage);

            var font = SAEResources.GetAllFonts().First();

            symboler.ApplySymbols(new[] { 'x' }, font);

            Assert.AreEqual('x', srcImage.Palette.First <CodedColor>().SymbolChar);

            int[] pixels;
            using (srcImage.LockPixels(out pixels))
            {
                for (int i = 0; i < pixels.Length; i++)
                {
                    pixels[i] = 0x00ffffff;
                }
            }
            srcImage.CompletePalette();
            srcImage.Palette.SymbolsFont = "abc";

            Assert.AreEqual(1, srcImage.Palette.Count, "There should be one color for #00ffffff.");
            Assert.AreEqual(' ', srcImage.Palette.First <CodedColor>().SymbolChar, "Space should be default symbol for new colors.");
            Assert.AreNotEqual(font.Name, srcImage.Palette.SymbolsFont);

            symboler.RestoreManipulations();

            Assert.AreEqual('x', srcImage.Palette.First <CodedColor>().SymbolChar);
            Assert.AreEqual(font.Name, srcImage.Palette.SymbolsFont);
        }
        public void TestQuantizeColors()
        {
            var image = new CodedImage {
                Size = new Size(30, 20), Palette = new CodedPalette()
            };

            image.CompletePalette();
            for (int x = 0; x < 30; x++)
            {
                for (int y = 0; y < 20; y++)
                {
                    image[x, y] = new CodedColor((byte)(x * 8), (byte)(y * 12), (byte)((x + y) * 5));
                }
            }

            var destPalette =
                new CodedPalette
            {
                new CodedColor(0, 0, 0)
                {
                    ColorCode = "100"
                },
                new CodedColor(255, 255, 255)
                {
                    ColorCode = "200"
                },
                new CodedColor(127, 127, 127)
                {
                    ColorCode = "300"
                }
            };

            var colorer = new ImageColorsManipulator(image);

            Assert.AreEqual(600, colorer.ManipulatedImage.Palette.Count, "Precondition");

            colorer.QuantizeColors(3, destPalette);
            Assert.AreEqual(3, colorer.ManipulatedImage.Palette.Count);
            Assert.AreEqual("100", colorer.ManipulatedImage.Palette[0].ColorCode);
            Assert.AreEqual("200", colorer.ManipulatedImage.Palette[new CodedColor(255, 255, 255).GetHashCode()].ColorCode);
            Assert.AreEqual("300", colorer.ManipulatedImage.Palette[new CodedColor(127, 127, 127).GetHashCode()].ColorCode);
        }
        public void TestConstructor()
        {
            var image = new CodedImage {
                Size = new Size(2, 2), Palette = new CodedPalette()
            };

            image.CompletePalette();
            image[0, 0] = new CodedColor(1)
            {
                ColorCode = "100"
            };
            image[0, 1] = new CodedColor(2)
            {
                ColorCode = "200"
            };
            image[1, 0] = new CodedColor(1)
            {
                ColorCode = "100"
            };
            image[1, 1] = new CodedColor(3)
            {
                ColorCode = "300"
            };

            var imageManipulator = new DummyManipulator(image);

            Assert.AreSame(image, imageManipulator.SourceImage);
            Assert.IsNotNull(imageManipulator.ManipulatedImage);
            Assert.AreNotSame(imageManipulator.SourceImage, imageManipulator.ManipulatedImage);

            Assert.AreEqual(new Size(2, 2), imageManipulator.ManipulatedImage.Size);
            Assert.AreEqual(new CodedColor(1), imageManipulator.ManipulatedImage[0, 0]);
            Assert.AreEqual(new CodedColor(2), imageManipulator.ManipulatedImage[0, 1]);
            Assert.AreEqual(new CodedColor(1), imageManipulator.ManipulatedImage[1, 0]);
            Assert.AreEqual(new CodedColor(3), imageManipulator.ManipulatedImage[1, 1]);

            Assert.AreEqual(3, imageManipulator.ManipulatedImage.Palette.Count);
            Assert.AreEqual("100", imageManipulator.ManipulatedImage.Palette[1].ColorCode);
            Assert.AreEqual("200", imageManipulator.ManipulatedImage.Palette[2].ColorCode);
            Assert.AreEqual("300", imageManipulator.ManipulatedImage.Palette[3].ColorCode);
        }
        public void TestAction()
        {
            var image = new CodedImage {
                Size = new Size(2, 3)
            };

            image.CompletePalette();
            var oldColor = new CodedColor(100);
            var newColor = new CodedColor(200);
            var action   = new UndoRedoActionChangePixelColor(image, 1, 2, oldColor, newColor);

            image[1, 2] = new CodedColor(0);

            Assert.AreEqual(new CodedColor(0), image[1, 2], "Precondition");

            action.Undo();
            Assert.AreEqual(oldColor, image[1, 2]);

            action.Redo();
            Assert.AreEqual(newColor, image[1, 2]);
        }
Exemple #11
0
        void Initialize()
        {
            var image = new CodedImage {
                Size = new Size(1, 1)
            };

            image.CompletePalette();
            image.Palette.SymbolsFont = SymbolsFont.Name;
            var manipulator = new ImageSymbolsManipulator(image);

            symboler = new ImageSymbolsController(new ImageSymbolsManipulator(manipulator), new[] { SymbolsFont }, SymbolsFont);
            symboler.IncludeLetters     = true;
            symboler.IncludeNumbers     = true;
            symboler.IncludeSymbols     = true;
            symboler.MaxSelectedSymbols = 1;

            symboler.AvailableSymbols.RemoveAll(pair => usedSymbols.Contains(pair.Key));
            symboler.ClearAllSelection();

            scrollControlSymbols.Controller = new VisualSymbolsController(symboler, new Size(scrollControlSymbols.Width, scrollControlSymbols.Height));
            scrollControlSymbols.Controller.VisualImageChanged += Controller_VisualImageChanged;
        }
Exemple #12
0
        public void TestApplySymbols()
        {
            var srcImage = new CodedImage {
                Size = new Size(3, 2)
            };

            int[] pixels;
            using (srcImage.LockPixels(out pixels))
            {
                for (int i = 0; i < pixels.Length; i++)
                {
                    pixels[i] = i % 3;
                }
            }
            srcImage.CompletePalette();

            Assert.AreEqual(3, srcImage.Palette.Count, "Precondition - there should be 3 colors.");

            var symboler = new ImageSymbolsManipulator(srcImage);

            Assert.AreSame(symboler.SourceImage, symboler.ManipulatedImage);

            var font = SAEResources.GetAllFonts().First();

            symboler.ApplySymbols(new[] { ' ', '.', 'X' }, font);

            Assert.AreEqual(font.Name, srcImage.Palette.SymbolsFont);

            var colors = srcImage.Palette.OrderByDarknes(true).Cast <CodedColor>().ToArray();

            Assert.AreEqual(3, colors.Length);
            Assert.AreEqual(2, colors[0].Argb);
            Assert.AreEqual(' ', colors[0].SymbolChar);
            Assert.AreEqual(1, colors[1].Argb);
            Assert.AreEqual('.', colors[1].SymbolChar);
            Assert.AreEqual(0, colors[2].Argb);
            Assert.AreEqual('X', colors[2].SymbolChar);
        }