BitmapUtilsTests.cs 3.8 KB

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