BitmapUtilsTests.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Media;
  6. using System.Windows.Media.Imaging;
  7. using PixiEditor.Models.DataHolders;
  8. using PixiEditor.Models.ImageManipulation;
  9. using PixiEditor.Models.Layers;
  10. using PixiEditor.Models.Position;
  11. using Xunit;
  12. namespace PixiEditorTests.ModelsTests.ImageManipulationTests
  13. {
  14. public class BitmapUtilsTests
  15. {
  16. [Fact]
  17. public void TestBytesToWriteableBitmap()
  18. {
  19. int width = 10;
  20. int height = 10;
  21. Coordinates[] coloredPoints = {new Coordinates(0, 0), new Coordinates(3, 6), new Coordinates(9, 9)};
  22. WriteableBitmap bmp = BitmapFactory.New(width, height);
  23. for (int i = 0; i < coloredPoints.Length; i++)
  24. {
  25. bmp.SetPixel(coloredPoints[i].X, coloredPoints[i].Y, Colors.Green);
  26. }
  27. var byteArray = bmp.ToByteArray();
  28. var convertedBitmap = BitmapUtils.BytesToWriteableBitmap(width, height, byteArray);
  29. for (int i = 0; i < coloredPoints.Length; i++)
  30. {
  31. Assert.Equal(Colors.Green,convertedBitmap.GetPixel(coloredPoints[i].X, coloredPoints[i].Y));
  32. }
  33. }
  34. [Fact]
  35. public void TestThatCombineLayersReturnsCorrectBitmap()
  36. {
  37. Coordinates[] cords = {new Coordinates(0, 0), new Coordinates(1, 1)};
  38. Layer[] layers = {new Layer("test", 2,2), new Layer("test2", 2, 2) };
  39. layers[0].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new []{cords[0]}, Colors.Green));
  40. layers[1].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new[] { cords[1] }, Colors.Red));
  41. var outputBitmap = BitmapUtils.CombineLayers(layers, 2, 2);
  42. Assert.Equal(Colors.Green, outputBitmap.GetPixel(0,0));
  43. Assert.Equal(Colors.Red, outputBitmap.GetPixel(1,1));
  44. }
  45. [Fact]
  46. public void TestThatCombineLayersReturnsCorrectBitmapWithSamePixels()
  47. {
  48. Coordinates[] cords = { new Coordinates(0, 0) };
  49. Layer[] layers = { new Layer("test", 2, 2), new Layer("test2", 2, 2) };
  50. layers[0].SetPixels(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Green));
  51. layers[1].SetPixels(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Red));
  52. var outputBitmap = BitmapUtils.CombineLayers(layers, 2, 2);
  53. Assert.Equal(Colors.Red, outputBitmap.GetPixel(0, 0));
  54. }
  55. [Fact]
  56. public void TestThatGetPixelsForSelectionReturnsCorrectPixels()
  57. {
  58. Coordinates[] cords = { new Coordinates(0, 0),
  59. new Coordinates(1,1), new Coordinates(0, 1), new Coordinates(1, 0) };
  60. Layer[] layers = { new Layer("test", 2, 2), new Layer("test2", 2, 2) };
  61. layers[0].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new []{cords[0]}, Colors.Green));
  62. layers[1].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new [] { cords[1] }, Colors.Red));
  63. var output = BitmapUtils.GetPixelsForSelection(layers, cords);
  64. List<Color> colors = new List<Color>();
  65. foreach (var layerColor in output.ToArray())
  66. {
  67. foreach (var color in layerColor.Value)
  68. {
  69. colors.Add(color);
  70. }
  71. }
  72. Assert.Single(colors.Where(x=> x == Colors.Green));
  73. Assert.Single(colors.Where(x=> x == Colors.Red));
  74. Assert.Equal(6, colors.Count(x => x.A == 0)); //6 because layer is 4 pixels,
  75. //2 * 4 = 8, 2 other color pixels, so 8 - 2 = 6
  76. }
  77. }
  78. }